2022-01-12 16:02:01 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2024-09-19 20:16:53 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-01-12 16:02:01 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-09-05 14:25:26 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2024-07-26 21:34:57 +00:00
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2022-01-12 16:02:01 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/services/mailservers"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (m *Messenger) AllMailservers() ([]mailservers.Mailserver, error) {
|
2023-11-25 23:24:20 +00:00
|
|
|
// Get configured fleet
|
2022-01-31 10:33:56 +00:00
|
|
|
fleet, err := m.getFleet()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
// Get default mailservers for given fleet
|
2024-09-05 14:25:26 +00:00
|
|
|
allMailservers := mailservers.DefaultMailserversByFleet(fleet)
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
// Add custom configured mailservers
|
|
|
|
if m.mailserversDatabase != nil {
|
|
|
|
customMailservers, err := m.mailserversDatabase.Mailservers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range customMailservers {
|
|
|
|
if c.Fleet == fleet {
|
|
|
|
allMailservers = append(allMailservers, c)
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 14:25:26 +00:00
|
|
|
return allMailservers, nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (m *Messenger) setupStorenodes(storenodes []mailservers.Mailserver) error {
|
|
|
|
if m.transport.WakuVersion() != 2 {
|
2024-09-05 14:25:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-11-25 23:24:20 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
for _, storenode := range storenodes {
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
peerInfo, err := storenode.PeerInfo()
|
2024-09-05 14:25:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-11-24 20:32:02 +00:00
|
|
|
}
|
2024-09-05 14:25:26 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
for _, addr := range utils.EncapsulatePeerID(peerInfo.ID, peerInfo.Addrs...) {
|
|
|
|
_, err := m.transport.AddStorePeer(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (m *Messenger) getFleet() (string, error) {
|
|
|
|
var fleet string
|
|
|
|
dbFleet, err := m.settings.GetFleet()
|
2024-02-20 15:49:39 +00:00
|
|
|
if err != nil {
|
2024-09-19 20:16:53 +00:00
|
|
|
return "", err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2024-09-19 20:16:53 +00:00
|
|
|
if dbFleet != "" {
|
|
|
|
fleet = dbFleet
|
|
|
|
} else if m.config.clusterConfig.Fleet != "" {
|
|
|
|
fleet = m.config.clusterConfig.Fleet
|
|
|
|
} else {
|
|
|
|
fleet = params.FleetStatusProd
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2024-09-19 20:16:53 +00:00
|
|
|
return fleet, nil
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2024-01-30 18:13:18 +00:00
|
|
|
func (m *Messenger) asyncRequestAllHistoricMessages() {
|
2024-09-19 20:16:53 +00:00
|
|
|
if !m.config.codeControlFlags.AutoRequestHistoricMessages || m.transport.WakuVersion() == 1 {
|
2024-07-26 21:34:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 18:13:18 +00:00
|
|
|
m.logger.Debug("asyncRequestAllHistoricMessages")
|
2024-07-26 21:34:57 +00:00
|
|
|
|
2024-01-30 18:13:18 +00:00
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2024-02-20 15:49:39 +00:00
|
|
|
_, err := m.RequestAllHistoricMessages(false, true)
|
2024-01-30 18:13:18 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to request historic messages", zap.Error(err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (m *Messenger) GetPinnedStorenode() (peer.ID, error) {
|
2022-01-31 10:33:56 +00:00
|
|
|
fleet, err := m.getFleet()
|
|
|
|
if err != nil {
|
2024-09-19 20:16:53 +00:00
|
|
|
return "", err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
pinnedMailservers, err := m.settings.GetPinnedMailservers()
|
|
|
|
if err != nil {
|
2024-09-19 20:16:53 +00:00
|
|
|
return "", err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
pinnedMailserver, ok := pinnedMailservers[fleet]
|
|
|
|
if !ok {
|
2024-09-19 20:16:53 +00:00
|
|
|
return "", nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
|
|
|
fleetMailservers := mailservers.DefaultMailservers()
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
for _, c := range fleetMailservers {
|
|
|
|
if c.Fleet == fleet && c.ID == pinnedMailserver {
|
2024-09-19 20:16:53 +00:00
|
|
|
return c.PeerID()
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
if m.mailserversDatabase != nil {
|
|
|
|
customMailservers, err := m.mailserversDatabase.Mailservers()
|
|
|
|
if err != nil {
|
2024-09-19 20:16:53 +00:00
|
|
|
return "", err
|
2023-11-25 23:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range customMailservers {
|
|
|
|
if c.Fleet == fleet && c.ID == pinnedMailserver {
|
2024-09-19 20:16:53 +00:00
|
|
|
return c.PeerID()
|
2023-11-25 23:24:20 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
return "", nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-03-21 14:18:36 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (m *Messenger) UseStorenodes() (bool, error) {
|
|
|
|
return m.settings.CanUseMailservers()
|
|
|
|
}
|
2023-11-24 20:32:02 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (m *Messenger) Storenodes() ([]peer.ID, error) {
|
|
|
|
mailservers, err := m.AllMailservers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-24 20:32:02 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
var result []peer.ID
|
|
|
|
for _, m := range mailservers {
|
|
|
|
peerID, err := m.PeerID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, peerID)
|
2023-11-24 20:32:02 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
return result, nil
|
2023-11-24 20:32:02 +00:00
|
|
|
}
|
2023-12-15 19:50:12 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
func (m *Messenger) checkForStorenodeCycleSignals() {
|
|
|
|
if m.transport.WakuVersion() != 2 {
|
|
|
|
return
|
|
|
|
}
|
2023-12-15 19:50:12 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
changed := m.transport.OnStorenodeChanged()
|
|
|
|
notWorking := m.transport.OnStorenodeNotWorking()
|
|
|
|
available := m.transport.OnStorenodeAvailable()
|
2023-12-15 19:50:12 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
allMailservers, err := m.AllMailservers()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("Could not retrieve mailserver list", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
2023-12-15 19:50:12 +00:00
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
mailserverMap := make(map[peer.ID]mailservers.Mailserver)
|
|
|
|
for _, ms := range allMailservers {
|
|
|
|
peerID, err := ms.PeerID()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("could not retrieve peerID", zap.Error(err))
|
|
|
|
return
|
2023-12-15 19:50:12 +00:00
|
|
|
}
|
2024-09-19 20:16:53 +00:00
|
|
|
mailserverMap[peerID] = ms
|
2023-12-15 19:50:12 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 20:16:53 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-notWorking:
|
|
|
|
signal.SendMailserverNotWorking()
|
|
|
|
|
|
|
|
case activeMailserver := <-changed:
|
|
|
|
if activeMailserver != "" {
|
|
|
|
ms, ok := mailserverMap[activeMailserver]
|
|
|
|
if ok {
|
|
|
|
signal.SendMailserverChanged(&ms)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
signal.SendMailserverChanged(nil)
|
|
|
|
}
|
|
|
|
case activeMailserver := <-available:
|
|
|
|
if activeMailserver != "" {
|
|
|
|
ms, ok := mailserverMap[activeMailserver]
|
|
|
|
if ok {
|
|
|
|
signal.SendMailserverAvailable(&ms)
|
|
|
|
}
|
|
|
|
m.asyncRequestAllHistoricMessages()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 19:50:12 +00:00
|
|
|
}
|