2022-01-12 16:02:01 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
2023-11-25 23:24:20 +00:00
|
|
|
"fmt"
|
2022-01-12 16:02:01 +00:00
|
|
|
"math"
|
|
|
|
"math/big"
|
2024-04-18 16:48:02 +00:00
|
|
|
"net"
|
2023-12-14 13:32:38 +00:00
|
|
|
"runtime"
|
2022-01-12 16:02:01 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2023-12-15 19:50:12 +00:00
|
|
|
"sync"
|
2022-01-12 16:02:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2024-07-26 21:34:57 +00:00
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2024-02-20 15:49:39 +00:00
|
|
|
"github.com/status-im/status-go/protocol/storenodes"
|
2022-01-12 16:02:01 +00:00
|
|
|
"github.com/status-im/status-go/services/mailservers"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
const defaultBackoff = 10 * time.Second
|
|
|
|
const graylistBackoff = 3 * time.Minute
|
2024-07-24 15:00:20 +00:00
|
|
|
const backoffByUserAction = 0
|
2023-12-14 13:32:38 +00:00
|
|
|
const isAndroidEmulator = runtime.GOOS == "android" && runtime.GOARCH == "amd64"
|
|
|
|
const findNearestMailServer = !isAndroidEmulator
|
2024-04-26 12:14:17 +00:00
|
|
|
const overrideDNS = runtime.GOOS == "android" || runtime.GOOS == "ios"
|
2024-04-18 16:48:02 +00:00
|
|
|
const bootstrapDNS = "8.8.8.8:53"
|
2022-01-31 10:33:56 +00:00
|
|
|
|
|
|
|
func (m *Messenger) mailserversByFleet(fleet string) []mailservers.Mailserver {
|
2023-12-27 13:53:19 +00:00
|
|
|
return mailservers.DefaultMailserversByFleet(fleet)
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
type byRTTMsAndCanConnectBefore []SortedMailserver
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (s byRTTMsAndCanConnectBefore) Len() int {
|
2022-01-12 16:02:01 +00:00
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (s byRTTMsAndCanConnectBefore) Swap(i, j int) {
|
2022-01-12 16:02:01 +00:00
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (s byRTTMsAndCanConnectBefore) Less(i, j int) bool {
|
|
|
|
// Slightly inaccurate as time sensitive sorting, but it does not matter so much
|
|
|
|
now := time.Now()
|
|
|
|
if s[i].CanConnectAfter.Before(now) && s[j].CanConnectAfter.Before(now) {
|
|
|
|
return s[i].RTTMs < s[j].RTTMs
|
|
|
|
}
|
|
|
|
return s[i].CanConnectAfter.Before(s[j].CanConnectAfter)
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
func (m *Messenger) StartMailserverCycle(mailservers []mailservers.Mailserver) error {
|
|
|
|
m.mailserverCycle.allMailservers = mailservers
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
version := m.transport.WakuVersion()
|
|
|
|
|
|
|
|
switch version {
|
|
|
|
case 1:
|
|
|
|
if m.server == nil {
|
2023-12-19 11:58:44 +00:00
|
|
|
m.logger.Warn("not starting mailserver cycle: no p2p server is set")
|
2023-11-25 23:24:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
m.mailserverCycle.events = make(chan *p2p.PeerEvent, 20)
|
|
|
|
m.mailserverCycle.subscription = m.server.SubscribeEvents(m.mailserverCycle.events)
|
|
|
|
go m.updateWakuV1PeerStatus()
|
|
|
|
|
|
|
|
case 2:
|
2023-12-19 11:58:44 +00:00
|
|
|
if len(mailservers) == 0 {
|
|
|
|
m.logger.Warn("not starting mailserver cycle: empty mailservers list")
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-24 20:32:02 +00:00
|
|
|
for _, storenode := range mailservers {
|
|
|
|
_, err := m.transport.AddStorePeer(storenode.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go m.verifyStorenodeStatus()
|
2023-11-25 23:24:20 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported waku version: %d", version)
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
m.logger.Debug("starting mailserver cycle",
|
|
|
|
zap.Uint("WakuVersion", m.transport.WakuVersion()),
|
|
|
|
zap.Any("mailservers", mailservers),
|
|
|
|
)
|
2022-01-12 16:02:01 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DisconnectActiveMailserver() {
|
|
|
|
m.mailserverCycle.Lock()
|
|
|
|
defer m.mailserverCycle.Unlock()
|
2024-07-24 15:00:20 +00:00
|
|
|
m.disconnectActiveMailserver(graylistBackoff)
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 15:00:20 +00:00
|
|
|
func (m *Messenger) disconnectMailserver(backoffDuration time.Duration) error {
|
2022-01-12 16:02:01 +00:00
|
|
|
if m.mailserverCycle.activeMailserver == nil {
|
2022-01-31 10:33:56 +00:00
|
|
|
m.logger.Info("no active mailserver")
|
|
|
|
return nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
m.logger.Info("disconnecting active mailserver", zap.String("nodeID", m.mailserverCycle.activeMailserver.ID))
|
|
|
|
m.mailPeersMutex.Lock()
|
|
|
|
pInfo, ok := m.mailserverCycle.peers[m.mailserverCycle.activeMailserver.ID]
|
2022-01-12 16:02:01 +00:00
|
|
|
if ok {
|
|
|
|
pInfo.status = disconnected
|
2024-07-24 15:00:20 +00:00
|
|
|
|
|
|
|
pInfo.canConnectAfter = time.Now().Add(backoffDuration)
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailserverCycle.peers[m.mailserverCycle.activeMailserver.ID] = pInfo
|
2022-01-12 16:02:01 +00:00
|
|
|
} else {
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailserverCycle.peers[m.mailserverCycle.activeMailserver.ID] = peerStatus{
|
2022-01-12 16:02:01 +00:00
|
|
|
status: disconnected,
|
2022-01-31 10:33:56 +00:00
|
|
|
mailserver: *m.mailserverCycle.activeMailserver,
|
2024-07-24 15:00:20 +00:00
|
|
|
canConnectAfter: time.Now().Add(backoffDuration),
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailPeersMutex.Unlock()
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-11-24 20:32:02 +00:00
|
|
|
// WakuV2 does not keep an active storenode connection
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-11-24 20:32:02 +00:00
|
|
|
if m.mailserverCycle.activeMailserver.Version == 1 {
|
2022-01-31 10:33:56 +00:00
|
|
|
node, err := m.mailserverCycle.activeMailserver.Enode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
m.server.RemovePeer(node)
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailserverCycle.activeMailserver = nil
|
|
|
|
return nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 15:00:20 +00:00
|
|
|
func (m *Messenger) disconnectActiveMailserver(backoffDuration time.Duration) {
|
|
|
|
err := m.disconnectMailserver(backoffDuration)
|
2022-01-31 10:33:56 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to disconnect mailserver", zap.Error(err))
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
signal.SendMailserverChanged("", "")
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) cycleMailservers() {
|
|
|
|
m.logger.Info("Automatically switching mailserver")
|
|
|
|
|
|
|
|
if m.mailserverCycle.activeMailserver != nil {
|
2024-07-24 15:00:20 +00:00
|
|
|
m.disconnectActiveMailserver(graylistBackoff)
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:54:56 +00:00
|
|
|
useMailserver, err := m.settings.CanUseMailservers()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to get use mailservers", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !useMailserver {
|
|
|
|
m.logger.Info("Skipping mailserver search due to useMailserver being false")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.findNewMailserver()
|
2022-01-12 16:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("Error getting new mailserver", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func poolSize(fleetSize int) int {
|
|
|
|
return int(math.Ceil(float64(fleetSize) / 4))
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (m *Messenger) getFleet() (string, error) {
|
|
|
|
var fleet string
|
|
|
|
dbFleet, err := m.settings.GetFleet()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
if dbFleet != "" {
|
|
|
|
fleet = dbFleet
|
|
|
|
} else if m.config.clusterConfig.Fleet != "" {
|
|
|
|
fleet = m.config.clusterConfig.Fleet
|
|
|
|
} else {
|
|
|
|
fleet = params.FleetProd
|
|
|
|
}
|
|
|
|
return fleet, nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +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
|
2022-01-31 10:33:56 +00:00
|
|
|
allMailservers := m.mailserversByFleet(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 {
|
|
|
|
c.Version = m.transport.WakuVersion()
|
|
|
|
allMailservers = append(allMailservers, c)
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
// Filter mailservers by configured waku version
|
|
|
|
wakuVersion := m.transport.WakuVersion()
|
|
|
|
matchingMailservers := make([]mailservers.Mailserver, 0, len(allMailservers))
|
|
|
|
|
|
|
|
for _, ms := range allMailservers {
|
|
|
|
if ms.Version == wakuVersion {
|
|
|
|
matchingMailservers = append(matchingMailservers, ms)
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
return matchingMailservers, nil
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
type SortedMailserver struct {
|
|
|
|
Address string
|
|
|
|
RTTMs int
|
|
|
|
CanConnectAfter time.Time
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (m *Messenger) findNewMailserver() error {
|
2024-04-18 16:48:02 +00:00
|
|
|
|
|
|
|
// we have to override DNS manually because of https://github.com/status-im/status-mobile/issues/19581
|
|
|
|
if overrideDNS {
|
|
|
|
var dialer net.Dialer
|
|
|
|
net.DefaultResolver = &net.Resolver{
|
|
|
|
PreferGo: false,
|
|
|
|
Dial: func(context context.Context, _, _ string) (net.Conn, error) {
|
|
|
|
conn, err := dialer.DialContext(context, "udp", bootstrapDNS)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
pinnedMailserver, err := m.getPinnedMailserver()
|
2022-01-12 16:02:01 +00:00
|
|
|
if err != nil {
|
2022-01-31 10:33:56 +00:00
|
|
|
m.logger.Error("Could not obtain the pinned mailserver", zap.Error(err))
|
|
|
|
return err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
if pinnedMailserver != nil {
|
|
|
|
return m.connectToMailserver(*pinnedMailserver)
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-19 12:35:19 +00:00
|
|
|
|
2023-11-25 23:24:20 +00:00
|
|
|
allMailservers := m.mailserverCycle.allMailservers
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
// TODO: remove this check once sockets are stable on x86_64 emulators
|
|
|
|
if findNearestMailServer {
|
|
|
|
m.logger.Info("Finding a new mailserver...")
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
var mailserverStr []string
|
|
|
|
for _, m := range allMailservers {
|
|
|
|
mailserverStr = append(mailserverStr, m.Address)
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
if len(allMailservers) == 0 {
|
|
|
|
m.logger.Warn("no mailservers available") // Do nothing...
|
|
|
|
return nil
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
var parseFn func(string) (string, error)
|
|
|
|
if allMailservers[0].Version == 2 {
|
|
|
|
parseFn = mailservers.MultiAddressToAddress
|
|
|
|
} else {
|
|
|
|
parseFn = mailservers.EnodeStringToAddr
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
pingResult, err := mailservers.DoPing(context.Background(), mailserverStr, 500, parseFn)
|
|
|
|
if err != nil {
|
|
|
|
// pinging mailservers might fail, but we don't care
|
|
|
|
m.logger.Warn("mailservers.DoPing failed with", zap.Error(err))
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
var availableMailservers []*mailservers.PingResult
|
|
|
|
for _, result := range pingResult {
|
|
|
|
if result.Err != nil {
|
|
|
|
m.logger.Info("connecting error", zap.String("err", *result.Err))
|
|
|
|
continue // The results with error are ignored
|
|
|
|
}
|
|
|
|
availableMailservers = append(availableMailservers, result)
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
if len(availableMailservers) == 0 {
|
|
|
|
m.logger.Warn("No mailservers available") // Do nothing...
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mailserversByAddress := make(map[string]mailservers.Mailserver)
|
|
|
|
for idx := range allMailservers {
|
|
|
|
mailserversByAddress[allMailservers[idx].Address] = allMailservers[idx]
|
|
|
|
}
|
|
|
|
var sortedMailservers []SortedMailserver
|
|
|
|
for _, ping := range availableMailservers {
|
|
|
|
address := ping.Address
|
|
|
|
ms := mailserversByAddress[address]
|
|
|
|
sortedMailserver := SortedMailserver{
|
|
|
|
Address: address,
|
|
|
|
RTTMs: *ping.RTTMs,
|
|
|
|
}
|
|
|
|
m.mailPeersMutex.Lock()
|
|
|
|
pInfo, ok := m.mailserverCycle.peers[ms.ID]
|
|
|
|
m.mailPeersMutex.Unlock()
|
|
|
|
if ok {
|
|
|
|
if time.Now().Before(pInfo.canConnectAfter) {
|
|
|
|
continue // We can't connect to this node yet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sortedMailservers = append(sortedMailservers, sortedMailserver)
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2023-12-14 13:32:38 +00:00
|
|
|
sort.Sort(byRTTMsAndCanConnectBefore(sortedMailservers))
|
|
|
|
|
|
|
|
// Picks a random mailserver amongs the ones with the lowest latency
|
|
|
|
// The pool size is 1/4 of the mailservers were pinged successfully
|
|
|
|
pSize := poolSize(len(sortedMailservers) - 1)
|
|
|
|
if pSize <= 0 {
|
|
|
|
pSize = len(sortedMailservers)
|
|
|
|
if pSize <= 0 {
|
|
|
|
m.logger.Warn("No mailservers available") // Do nothing...
|
|
|
|
return nil
|
2023-11-24 20:32:02 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(pSize)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msPing := sortedMailservers[r.Int64()]
|
|
|
|
ms := mailserversByAddress[msPing.Address]
|
|
|
|
m.logger.Info("connecting to mailserver", zap.String("address", ms.Address))
|
|
|
|
return m.connectToMailserver(ms)
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
mailserversByAddress := make(map[string]mailservers.Mailserver)
|
|
|
|
for idx := range allMailservers {
|
|
|
|
mailserversByAddress[allMailservers[idx].Address] = allMailservers[idx]
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
pSize := poolSize(len(allMailservers) - 1)
|
2022-02-22 23:15:07 +00:00
|
|
|
if pSize <= 0 {
|
2023-12-14 13:32:38 +00:00
|
|
|
pSize = len(allMailservers)
|
2023-11-24 20:32:02 +00:00
|
|
|
if pSize <= 0 {
|
|
|
|
m.logger.Warn("No mailservers available") // Do nothing...
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-22 23:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(pSize)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-14 13:32:38 +00:00
|
|
|
msPing := allMailservers[r.Int64()]
|
2022-01-31 10:33:56 +00:00
|
|
|
ms := mailserversByAddress[msPing.Address]
|
|
|
|
m.logger.Info("connecting to mailserver", zap.String("address", ms.Address))
|
|
|
|
return m.connectToMailserver(ms)
|
2023-12-14 13:32:38 +00:00
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
func (m *Messenger) mailserverStatus(mailserverID string) connStatus {
|
|
|
|
m.mailPeersMutex.RLock()
|
|
|
|
defer m.mailPeersMutex.RUnlock()
|
|
|
|
peer, ok := m.mailserverCycle.peers[mailserverID]
|
|
|
|
if !ok {
|
|
|
|
return disconnected
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2024-02-20 15:49:39 +00:00
|
|
|
return peer.status
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) connectToMailserver(ms mailservers.Mailserver) error {
|
|
|
|
|
|
|
|
m.logger.Info("connecting to mailserver", zap.Any("peer", ms.ID))
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailserverCycle.activeMailserver = &ms
|
|
|
|
signal.SendMailserverChanged(m.mailserverCycle.activeMailserver.Address, m.mailserverCycle.activeMailserver.ID)
|
2022-01-12 16:02:01 +00:00
|
|
|
|
|
|
|
// Adding a peer and marking it as connected can't be executed sync in WakuV1, because
|
|
|
|
// There's a delay between requesting a peer being added, and a signal being
|
|
|
|
// received after the peer was added. So we first set the peer status as
|
|
|
|
// Connecting and once a peerConnected signal is received, we mark it as
|
|
|
|
// Connected
|
2024-02-20 15:49:39 +00:00
|
|
|
activeMailserverStatus := m.mailserverStatus(ms.ID)
|
2023-11-25 23:24:20 +00:00
|
|
|
if ms.Version != m.transport.WakuVersion() {
|
|
|
|
return errors.New("mailserver waku version doesn't match")
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
if activeMailserverStatus != connected {
|
2023-11-24 20:32:02 +00:00
|
|
|
// WakuV2 does not require having the peer connected to query the peer
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-11-24 20:32:02 +00:00
|
|
|
// Attempt to connect to mailserver by adding it as a peer
|
|
|
|
if ms.Version == 1 {
|
2022-01-31 10:33:56 +00:00
|
|
|
node, err := ms.Enode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.server.AddPeer(node)
|
|
|
|
if err := m.peerStore.Update([]*enode.Node{node}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 20:32:02 +00:00
|
|
|
connectionStatus := connecting
|
|
|
|
if ms.Version == 2 {
|
|
|
|
connectionStatus = connected
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailPeersMutex.Lock()
|
2023-11-24 20:32:02 +00:00
|
|
|
m.mailserverCycle.peers[ms.ID] = peerStatus{
|
|
|
|
status: connectionStatus,
|
|
|
|
lastConnectionAttempt: time.Now(),
|
|
|
|
canConnectAfter: time.Now().Add(defaultBackoff),
|
|
|
|
mailserver: ms,
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailPeersMutex.Unlock()
|
2023-11-24 20:32:02 +00:00
|
|
|
|
|
|
|
if ms.Version == 2 {
|
|
|
|
m.mailserverCycle.activeMailserver.FailedRequests = 0
|
|
|
|
m.logger.Info("mailserver available", zap.String("address", m.mailserverCycle.activeMailserver.UniqueID()))
|
|
|
|
m.EmitMailserverAvailable()
|
|
|
|
signal.SendMailserverAvailable(m.mailserverCycle.activeMailserver.Address, m.mailserverCycle.activeMailserver.ID)
|
2024-06-11 07:45:01 +00:00
|
|
|
peerID, err := m.mailserverCycle.activeMailserver.PeerID()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("could not decode the peer id of mailserver", zap.Error(err))
|
|
|
|
}
|
|
|
|
m.transport.SetStorePeerID(peerID)
|
2023-11-24 20:32:02 +00:00
|
|
|
|
|
|
|
// Query mailserver
|
2024-07-26 21:34:57 +00:00
|
|
|
m.asyncRequestAllHistoricMessages()
|
2023-11-24 20:32:02 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
// getActiveMailserver returns the active mailserver if a communityID is present then it'll return the mailserver
|
|
|
|
// for that community if it has a mailserver setup otherwise it'll return the global mailserver
|
|
|
|
func (m *Messenger) getActiveMailserver(communityID ...string) *mailservers.Mailserver {
|
|
|
|
if len(communityID) == 0 || communityID[0] == "" {
|
|
|
|
return m.mailserverCycle.activeMailserver
|
|
|
|
}
|
|
|
|
ms, err := m.communityStorenodes.GetStorenodeByCommunnityID(communityID[0])
|
|
|
|
if err != nil {
|
2024-03-19 17:11:18 +00:00
|
|
|
if !errors.Is(err, storenodes.ErrNotFound) {
|
|
|
|
m.logger.Error("getting storenode for community, using global", zap.String("communityID", communityID[0]), zap.Error(err))
|
2024-02-20 15:49:39 +00:00
|
|
|
}
|
2024-03-19 17:11:18 +00:00
|
|
|
// if we don't find a specific mailserver for the community, we just use the regular mailserverCycle's one
|
2024-02-20 15:49:39 +00:00
|
|
|
return m.mailserverCycle.activeMailserver
|
|
|
|
}
|
|
|
|
return &ms
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
func (m *Messenger) getActiveMailserverID(communityID ...string) string {
|
|
|
|
ms := m.getActiveMailserver(communityID...)
|
|
|
|
if ms == nil {
|
|
|
|
return ""
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2024-02-20 15:49:39 +00:00
|
|
|
return ms.ID
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
func (m *Messenger) isMailserverAvailable(mailserverID string) bool {
|
|
|
|
return m.mailserverStatus(mailserverID) == connected
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
func mailserverAddressToID(uniqueID string, allMailservers []mailservers.Mailserver) (string, error) {
|
2022-01-31 10:33:56 +00:00
|
|
|
for _, ms := range allMailservers {
|
|
|
|
if uniqueID == ms.UniqueID() {
|
|
|
|
return ms.ID, nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
return "", nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
type ConnectedPeer struct {
|
|
|
|
UniqueID string
|
2022-01-19 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (m *Messenger) mailserverPeersInfo() []ConnectedPeer {
|
|
|
|
var connectedPeers []ConnectedPeer
|
|
|
|
for _, connectedPeer := range m.server.PeersInfo() {
|
|
|
|
connectedPeers = append(connectedPeers, ConnectedPeer{
|
|
|
|
// This is a bit fragile, but should work
|
|
|
|
UniqueID: strings.TrimSuffix(connectedPeer.Enode, "?discport=0"),
|
|
|
|
})
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
return connectedPeers
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (m *Messenger) penalizeMailserver(id string) {
|
|
|
|
m.mailPeersMutex.Lock()
|
|
|
|
defer m.mailPeersMutex.Unlock()
|
|
|
|
pInfo, ok := m.mailserverCycle.peers[id]
|
|
|
|
if !ok {
|
|
|
|
pInfo.status = disconnected
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
pInfo.canConnectAfter = time.Now().Add(graylistBackoff)
|
|
|
|
m.mailserverCycle.peers[id] = pInfo
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
// handleMailserverCycleEvent runs every 1 second or when updating peers to keep the data of the active mailserver updated
|
2022-01-31 10:33:56 +00:00
|
|
|
func (m *Messenger) handleMailserverCycleEvent(connectedPeers []ConnectedPeer) error {
|
2023-11-25 23:24:20 +00:00
|
|
|
m.logger.Debug("mailserver cycle event",
|
|
|
|
zap.Any("connected", connectedPeers),
|
|
|
|
zap.Any("peer-info", m.mailserverCycle.peers))
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailPeersMutex.Lock()
|
2023-05-05 11:36:32 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
for pID, pInfo := range m.mailserverCycle.peers {
|
|
|
|
if pInfo.status == disconnected {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
// Removing disconnected
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
found := false
|
|
|
|
for _, connectedPeer := range connectedPeers {
|
2024-02-20 15:49:39 +00:00
|
|
|
id, err := mailserverAddressToID(connectedPeer.UniqueID, m.mailserverCycle.allMailservers)
|
2022-01-31 10:33:56 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to convert id to hex", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
if pID == id {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found && (pInfo.status == connected || (pInfo.status == connecting && pInfo.lastConnectionAttempt.Add(8*time.Second).Before(time.Now()))) {
|
|
|
|
m.logger.Info("peer disconnected", zap.String("peer", pID))
|
|
|
|
pInfo.status = disconnected
|
|
|
|
pInfo.canConnectAfter = time.Now().Add(defaultBackoff)
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
m.mailserverCycle.peers[pID] = pInfo
|
|
|
|
}
|
2023-01-19 00:11:24 +00:00
|
|
|
m.mailPeersMutex.Unlock()
|
2022-01-31 10:33:56 +00:00
|
|
|
|
2023-01-19 00:11:24 +00:00
|
|
|
// Only evaluate connected peers once a mailserver has been set
|
2023-01-19 11:04:18 +00:00
|
|
|
// otherwise, we would attempt to retrieve history and end up with a mailserver
|
|
|
|
// not available error
|
2023-01-19 00:11:24 +00:00
|
|
|
if m.mailserverCycle.activeMailserver != nil {
|
|
|
|
for _, connectedPeer := range connectedPeers {
|
2024-02-20 15:49:39 +00:00
|
|
|
id, err := mailserverAddressToID(connectedPeer.UniqueID, m.mailserverCycle.allMailservers)
|
2023-01-19 00:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to convert id to hex", zap.Error(err))
|
|
|
|
return err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2023-01-19 00:11:24 +00:00
|
|
|
if id == "" {
|
|
|
|
continue
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2023-01-19 00:11:24 +00:00
|
|
|
m.mailPeersMutex.Lock()
|
|
|
|
pInfo, ok := m.mailserverCycle.peers[id]
|
|
|
|
if !ok || pInfo.status != connected {
|
|
|
|
m.logger.Info("peer connected", zap.String("peer", connectedPeer.UniqueID))
|
|
|
|
pInfo.status = connected
|
|
|
|
if pInfo.canConnectAfter.Before(time.Now()) {
|
|
|
|
pInfo.canConnectAfter = time.Now().Add(defaultBackoff)
|
|
|
|
}
|
|
|
|
m.mailserverCycle.peers[id] = pInfo
|
|
|
|
m.mailPeersMutex.Unlock()
|
|
|
|
|
|
|
|
if id == m.mailserverCycle.activeMailserver.ID {
|
|
|
|
m.mailserverCycle.activeMailserver.FailedRequests = 0
|
|
|
|
m.logger.Info("mailserver available", zap.String("address", connectedPeer.UniqueID))
|
|
|
|
m.EmitMailserverAvailable()
|
|
|
|
signal.SendMailserverAvailable(m.mailserverCycle.activeMailserver.Address, m.mailserverCycle.activeMailserver.ID)
|
|
|
|
}
|
|
|
|
// Query mailserver
|
2024-03-09 09:42:06 +00:00
|
|
|
if m.config.codeControlFlags.AutoRequestHistoricMessages {
|
2024-06-25 09:22:42 +00:00
|
|
|
m.asyncRequestAllHistoricMessages()
|
2024-02-12 22:30:19 +00:00
|
|
|
}
|
2023-01-19 00:11:24 +00:00
|
|
|
} else {
|
|
|
|
m.mailPeersMutex.Unlock()
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-19 00:11:24 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
// Check whether we want to disconnect the mailserver
|
|
|
|
if m.mailserverCycle.activeMailserver != nil {
|
|
|
|
if m.mailserverCycle.activeMailserver.FailedRequests >= mailserverMaxFailedRequests {
|
|
|
|
m.penalizeMailserver(m.mailserverCycle.activeMailserver.ID)
|
|
|
|
signal.SendMailserverNotWorking()
|
|
|
|
m.logger.Info("connecting too many failed requests")
|
|
|
|
m.mailserverCycle.activeMailserver.FailedRequests = 0
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
return m.connectToNewMailserverAndWait()
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mailPeersMutex.Lock()
|
|
|
|
pInfo, ok := m.mailserverCycle.peers[m.mailserverCycle.activeMailserver.ID]
|
|
|
|
m.mailPeersMutex.Unlock()
|
2022-10-26 18:23:41 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
if ok {
|
2022-10-26 18:23:41 +00:00
|
|
|
if pInfo.status != connected && pInfo.lastConnectionAttempt.Add(20*time.Second).Before(time.Now()) {
|
2022-01-31 10:33:56 +00:00
|
|
|
m.logger.Info("penalizing mailserver & disconnecting connecting", zap.String("id", m.mailserverCycle.activeMailserver.ID))
|
|
|
|
|
|
|
|
signal.SendMailserverNotWorking()
|
|
|
|
m.penalizeMailserver(m.mailserverCycle.activeMailserver.ID)
|
2024-07-24 15:00:20 +00:00
|
|
|
m.disconnectActiveMailserver(graylistBackoff)
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
m.cycleMailservers()
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
|
|
|
m.logger.Debug("updated-peers", zap.Any("peers", m.mailserverCycle.peers))
|
|
|
|
|
|
|
|
return nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 18:13:18 +00:00
|
|
|
func (m *Messenger) asyncRequestAllHistoricMessages() {
|
2024-07-26 21:34:57 +00:00
|
|
|
if !m.config.codeControlFlags.AutoRequestHistoricMessages {
|
|
|
|
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-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))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
func (m *Messenger) updateWakuV1PeerStatus() {
|
2022-01-31 10:33:56 +00:00
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
for {
|
|
|
|
select {
|
2022-01-31 10:33:56 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
err := m.handleMailserverCycleEvent(m.mailserverPeersInfo())
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to handle mailserver cycle event", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
ms := m.getActiveMailserver()
|
|
|
|
if ms != nil {
|
|
|
|
node, err := ms.Enode()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to parse enode", zap.Error(err))
|
2022-01-12 16:02:01 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
m.server.AddPeer(node)
|
|
|
|
if err := m.peerStore.Update([]*enode.Node{node}); err != nil {
|
|
|
|
m.logger.Error("failed to update peers", zap.Error(err))
|
|
|
|
continue
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
case <-m.mailserverCycle.events:
|
|
|
|
err := m.handleMailserverCycleEvent(m.mailserverPeersInfo())
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to handle mailserver cycle event", zap.Error(err))
|
|
|
|
return
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
case <-m.quit:
|
|
|
|
close(m.mailserverCycle.events)
|
|
|
|
m.mailserverCycle.subscription.Unsubscribe()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 20:32:02 +00:00
|
|
|
func (m *Messenger) verifyStorenodeStatus() {
|
2022-10-26 18:23:41 +00:00
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
for {
|
2022-01-31 10:33:56 +00:00
|
|
|
select {
|
2022-10-26 18:23:41 +00:00
|
|
|
case <-ticker.C:
|
2023-11-24 20:32:02 +00:00
|
|
|
err := m.disconnectStorenodeIfRequired()
|
2022-10-26 18:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to handle mailserver cycle event", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
case <-m.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func (m *Messenger) getPinnedMailserver() (*mailservers.Mailserver, error) {
|
|
|
|
fleet, err := m.getFleet()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 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 {
|
|
|
|
return nil, err
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
pinnedMailserver, ok := pinnedMailservers[fleet]
|
|
|
|
if !ok {
|
|
|
|
return nil, 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 {
|
|
|
|
return &c, nil
|
|
|
|
}
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range customMailservers {
|
|
|
|
if c.Fleet == fleet && c.ID == pinnedMailserver {
|
|
|
|
c.Version = m.transport.WakuVersion()
|
|
|
|
return &c, nil
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
2022-03-21 14:18:36 +00:00
|
|
|
|
|
|
|
func (m *Messenger) EmitMailserverAvailable() {
|
|
|
|
for _, s := range m.mailserverCycle.availabilitySubscriptions {
|
|
|
|
s <- struct{}{}
|
|
|
|
close(s)
|
|
|
|
l := len(m.mailserverCycle.availabilitySubscriptions)
|
|
|
|
m.mailserverCycle.availabilitySubscriptions = m.mailserverCycle.availabilitySubscriptions[:l-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SubscribeMailserverAvailable() chan struct{} {
|
|
|
|
c := make(chan struct{})
|
|
|
|
m.mailserverCycle.availabilitySubscriptions = append(m.mailserverCycle.availabilitySubscriptions, c)
|
|
|
|
return c
|
|
|
|
}
|
2023-11-24 20:32:02 +00:00
|
|
|
|
|
|
|
func (m *Messenger) disconnectStorenodeIfRequired() error {
|
|
|
|
m.logger.Debug("wakuV2 storenode status verification")
|
|
|
|
|
|
|
|
if m.mailserverCycle.activeMailserver == nil {
|
|
|
|
// No active storenode, find a new one
|
|
|
|
m.cycleMailservers()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we want to disconnect the active storenode
|
|
|
|
if m.mailserverCycle.activeMailserver.FailedRequests >= mailserverMaxFailedRequests {
|
|
|
|
m.penalizeMailserver(m.mailserverCycle.activeMailserver.ID)
|
|
|
|
signal.SendMailserverNotWorking()
|
|
|
|
m.logger.Info("too many failed requests", zap.String("storenode", m.mailserverCycle.activeMailserver.UniqueID()))
|
|
|
|
m.mailserverCycle.activeMailserver.FailedRequests = 0
|
|
|
|
return m.connectToNewMailserverAndWait()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-15 19:50:12 +00:00
|
|
|
|
|
|
|
func (m *Messenger) waitForAvailableStoreNode(timeout time.Duration) bool {
|
|
|
|
// Add 1 second to timeout, because the mailserver cycle has 1 second ticker, which doesn't tick on start.
|
|
|
|
// This can be improved after merging https://github.com/status-im/status-go/pull/4380.
|
|
|
|
// NOTE: https://stackoverflow.com/questions/32705582/how-to-get-time-tick-to-tick-immediately
|
|
|
|
timeout += time.Second
|
|
|
|
|
|
|
|
finish := make(chan struct{})
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2024-02-20 15:49:39 +00:00
|
|
|
for !m.isMailserverAvailable(m.getActiveMailserverID()) {
|
2023-12-15 19:50:12 +00:00
|
|
|
select {
|
|
|
|
case <-m.SubscribeMailserverAvailable():
|
|
|
|
case <-cancel:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
close(finish)
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-finish:
|
|
|
|
case <-time.After(timeout):
|
|
|
|
close(cancel)
|
|
|
|
case <-m.ctx.Done():
|
|
|
|
close(cancel)
|
|
|
|
}
|
|
|
|
|
2024-02-20 15:49:39 +00:00
|
|
|
return m.isMailserverAvailable(m.getActiveMailserverID())
|
2023-12-15 19:50:12 +00:00
|
|
|
}
|