2018-12-05 13:57:05 +00:00
|
|
|
package mailservers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2018-12-12 09:39:00 +00:00
|
|
|
"time"
|
2018-12-05 13:57:05 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-12-05 13:57:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
peerEventsBuffer = 10 // sufficient buffer to avoid blocking a p2p feed.
|
2020-01-20 20:56:06 +00:00
|
|
|
whisperEventsBuffer = 20 // sufficient buffer to avod blocking a eventSub envelopes feed.
|
2018-12-05 13:57:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PeerAdderRemover is an interface for adding or removing peers.
|
|
|
|
type PeerAdderRemover interface {
|
|
|
|
AddPeer(node *enode.Node)
|
|
|
|
RemovePeer(node *enode.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerEventsSubscriber interface to subscribe for p2p.PeerEvent's.
|
|
|
|
type PeerEventsSubscriber interface {
|
|
|
|
SubscribeEvents(chan *p2p.PeerEvent) event.Subscription
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
// EnvelopeEventSubscriber interface to subscribe for types.EnvelopeEvent's.
|
2019-10-09 14:22:53 +00:00
|
|
|
type EnvelopeEventSubscriber interface {
|
2019-11-23 17:57:05 +00:00
|
|
|
SubscribeEnvelopeEvents(chan<- types.EnvelopeEvent) types.Subscription
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type p2pServer interface {
|
|
|
|
PeerAdderRemover
|
|
|
|
PeerEventsSubscriber
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnectionManager creates an instance of ConnectionManager.
|
2020-01-20 20:56:06 +00:00
|
|
|
func NewConnectionManager(server p2pServer, eventSub EnvelopeEventSubscriber, target, maxFailures int, timeout time.Duration) *ConnectionManager {
|
2018-12-05 13:57:05 +00:00
|
|
|
return &ConnectionManager{
|
2018-12-12 09:39:00 +00:00
|
|
|
server: server,
|
2020-01-20 20:56:06 +00:00
|
|
|
eventSub: eventSub,
|
2018-12-12 09:39:00 +00:00
|
|
|
connectedTarget: target,
|
2019-01-21 14:00:10 +00:00
|
|
|
maxFailures: maxFailures,
|
2018-12-12 09:39:00 +00:00
|
|
|
notifications: make(chan []*enode.Node),
|
|
|
|
timeoutWaitAdded: timeout,
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionManager manages keeps target of peers connected.
|
|
|
|
type ConnectionManager struct {
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
server p2pServer
|
|
|
|
eventSub EnvelopeEventSubscriber
|
2018-12-05 13:57:05 +00:00
|
|
|
|
2018-12-12 09:39:00 +00:00
|
|
|
notifications chan []*enode.Node
|
|
|
|
connectedTarget int
|
|
|
|
timeoutWaitAdded time.Duration
|
2019-01-21 14:00:10 +00:00
|
|
|
maxFailures int
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify sends a non-blocking notification about new nodes.
|
|
|
|
func (ps *ConnectionManager) Notify(nodes []*enode.Node) {
|
|
|
|
ps.wg.Add(1)
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2018-12-05 13:57:05 +00:00
|
|
|
select {
|
|
|
|
case ps.notifications <- nodes:
|
|
|
|
case <-ps.quit:
|
|
|
|
}
|
|
|
|
ps.wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start subscribes to a p2p server and handles new peers and state updates for those peers.
|
|
|
|
func (ps *ConnectionManager) Start() {
|
|
|
|
ps.quit = make(chan struct{})
|
|
|
|
ps.wg.Add(1)
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2018-12-12 09:39:00 +00:00
|
|
|
state := newInternalState(ps.server, ps.connectedTarget, ps.timeoutWaitAdded)
|
2018-12-05 13:57:05 +00:00
|
|
|
events := make(chan *p2p.PeerEvent, peerEventsBuffer)
|
|
|
|
sub := ps.server.SubscribeEvents(events)
|
2019-11-23 17:57:05 +00:00
|
|
|
whisperEvents := make(chan types.EnvelopeEvent, whisperEventsBuffer)
|
2020-01-20 20:56:06 +00:00
|
|
|
whisperSub := ps.eventSub.SubscribeEnvelopeEvents(whisperEvents)
|
2019-11-23 17:57:05 +00:00
|
|
|
requests := map[types.Hash]struct{}{}
|
|
|
|
failuresPerServer := map[types.EnodeID]int{}
|
2018-12-12 09:39:00 +00:00
|
|
|
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
defer whisperSub.Unsubscribe()
|
|
|
|
defer ps.wg.Done()
|
2018-12-05 13:57:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ps.quit:
|
|
|
|
return
|
|
|
|
case err := <-sub.Err():
|
|
|
|
log.Error("retry after error subscribing to p2p events", "error", err)
|
2018-12-12 09:39:00 +00:00
|
|
|
return
|
2018-12-05 13:57:05 +00:00
|
|
|
case err := <-whisperSub.Err():
|
2020-01-20 20:56:06 +00:00
|
|
|
log.Error("retry after error suscribing to eventSub events", "error", err)
|
2018-12-12 09:39:00 +00:00
|
|
|
return
|
2018-12-05 13:57:05 +00:00
|
|
|
case newNodes := <-ps.notifications:
|
2018-12-12 09:39:00 +00:00
|
|
|
state.processReplacement(newNodes, events)
|
2018-12-05 13:57:05 +00:00
|
|
|
case ev := <-events:
|
2018-12-12 09:39:00 +00:00
|
|
|
processPeerEvent(state, ev)
|
2018-12-05 13:57:05 +00:00
|
|
|
case ev := <-whisperEvents:
|
2019-01-21 14:00:10 +00:00
|
|
|
// TODO treat failed requests the same way as expired
|
2018-12-05 13:57:05 +00:00
|
|
|
switch ev.Event {
|
2019-11-23 17:57:05 +00:00
|
|
|
case types.EventMailServerRequestSent:
|
2018-12-05 13:57:05 +00:00
|
|
|
requests[ev.Hash] = struct{}{}
|
2019-11-23 17:57:05 +00:00
|
|
|
case types.EventMailServerRequestCompleted:
|
2019-01-21 14:00:10 +00:00
|
|
|
// reset failures count on first success
|
|
|
|
failuresPerServer[ev.Peer] = 0
|
2018-12-05 13:57:05 +00:00
|
|
|
delete(requests, ev.Hash)
|
2019-11-23 17:57:05 +00:00
|
|
|
case types.EventMailServerRequestExpired:
|
2018-12-05 13:57:05 +00:00
|
|
|
_, exist := requests[ev.Hash]
|
|
|
|
if !exist {
|
|
|
|
continue
|
|
|
|
}
|
2019-01-21 14:00:10 +00:00
|
|
|
failuresPerServer[ev.Peer]++
|
2019-06-12 09:16:00 +00:00
|
|
|
log.Debug("request to a mail server expired, disconnect a peer", "address", ev.Peer)
|
2019-01-21 14:00:10 +00:00
|
|
|
if failuresPerServer[ev.Peer] >= ps.maxFailures {
|
|
|
|
state.nodeDisconnected(ev.Peer)
|
|
|
|
}
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop gracefully closes all background goroutines and waits until they finish.
|
|
|
|
func (ps *ConnectionManager) Stop() {
|
|
|
|
if ps.quit == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ps.quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
close(ps.quit)
|
|
|
|
ps.wg.Wait()
|
|
|
|
ps.quit = nil
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:39:00 +00:00
|
|
|
func (state *internalState) processReplacement(newNodes []*enode.Node, events <-chan *p2p.PeerEvent) {
|
2019-11-23 17:57:05 +00:00
|
|
|
replacement := map[types.EnodeID]*enode.Node{}
|
2018-12-12 09:39:00 +00:00
|
|
|
for _, n := range newNodes {
|
2019-11-23 17:57:05 +00:00
|
|
|
replacement[types.EnodeID(n.ID())] = n
|
2018-12-12 09:39:00 +00:00
|
|
|
}
|
|
|
|
state.replaceNodes(replacement)
|
|
|
|
if state.ReachedTarget() {
|
|
|
|
log.Debug("already connected with required target", "target", state.target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if state.timeout != 0 {
|
|
|
|
log.Debug("waiting defined timeout to establish connections",
|
|
|
|
"timeout", state.timeout, "target", state.target)
|
|
|
|
timer := time.NewTimer(state.timeout)
|
|
|
|
waitForConnections(state, timer.C, events)
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInternalState(srv PeerAdderRemover, target int, timeout time.Duration) *internalState {
|
|
|
|
return &internalState{
|
|
|
|
options: options{target: target, timeout: timeout},
|
|
|
|
srv: srv,
|
2019-11-23 17:57:05 +00:00
|
|
|
connected: map[types.EnodeID]struct{}{},
|
|
|
|
currentNodes: map[types.EnodeID]*enode.Node{},
|
2018-12-12 09:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type options struct {
|
|
|
|
target int
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type internalState struct {
|
|
|
|
options
|
|
|
|
srv PeerAdderRemover
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
connected map[types.EnodeID]struct{}
|
|
|
|
currentNodes map[types.EnodeID]*enode.Node
|
2018-12-12 09:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (state *internalState) ReachedTarget() bool {
|
|
|
|
return len(state.connected) >= state.target
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (state *internalState) replaceNodes(new map[types.EnodeID]*enode.Node) {
|
2018-12-12 09:39:00 +00:00
|
|
|
for nid, n := range state.currentNodes {
|
2018-12-05 13:57:05 +00:00
|
|
|
if _, exist := new[nid]; !exist {
|
2019-10-04 15:21:24 +00:00
|
|
|
delete(state.connected, nid)
|
2018-12-12 09:39:00 +00:00
|
|
|
state.srv.RemovePeer(n)
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
if !state.ReachedTarget() {
|
2018-12-05 13:57:05 +00:00
|
|
|
for _, n := range new {
|
2018-12-12 09:39:00 +00:00
|
|
|
state.srv.AddPeer(n)
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
state.currentNodes = new
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (state *internalState) nodeAdded(peer types.EnodeID) {
|
2018-12-12 09:39:00 +00:00
|
|
|
n, exist := state.currentNodes[peer]
|
2018-12-05 13:57:05 +00:00
|
|
|
if !exist {
|
|
|
|
return
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
if state.ReachedTarget() {
|
|
|
|
state.srv.RemovePeer(n)
|
2018-12-05 13:57:05 +00:00
|
|
|
} else {
|
2019-11-23 17:57:05 +00:00
|
|
|
state.connected[types.EnodeID(n.ID())] = struct{}{}
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (state *internalState) nodeDisconnected(peer types.EnodeID) {
|
2018-12-12 09:39:00 +00:00
|
|
|
n, exist := state.currentNodes[peer] // unrelated event
|
2018-12-05 13:57:05 +00:00
|
|
|
if !exist {
|
|
|
|
return
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
_, exist = state.connected[peer] // check if already disconnected
|
2018-12-05 13:57:05 +00:00
|
|
|
if !exist {
|
|
|
|
return
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
if len(state.currentNodes) == 1 { // keep node connected if we don't have another choice
|
2018-12-05 13:57:05 +00:00
|
|
|
return
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
state.srv.RemovePeer(n) // remove peer permanently, otherwise p2p.Server will try to reconnect
|
|
|
|
delete(state.connected, peer)
|
|
|
|
if !state.ReachedTarget() { // try to connect with any other selected (but not connected) node
|
|
|
|
for nid, n := range state.currentNodes {
|
|
|
|
_, exist := state.connected[nid]
|
2018-12-05 13:57:05 +00:00
|
|
|
if exist || peer == nid {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
state.srv.AddPeer(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processPeerEvent(state *internalState, ev *p2p.PeerEvent) {
|
|
|
|
switch ev.Type {
|
|
|
|
case p2p.PeerEventTypeAdd:
|
|
|
|
log.Debug("connected to a mailserver", "address", ev.Peer)
|
2019-11-23 17:57:05 +00:00
|
|
|
state.nodeAdded(types.EnodeID(ev.Peer))
|
2018-12-12 09:39:00 +00:00
|
|
|
case p2p.PeerEventTypeDrop:
|
|
|
|
log.Debug("mailserver disconnected", "address", ev.Peer)
|
2019-11-23 17:57:05 +00:00
|
|
|
state.nodeDisconnected(types.EnodeID(ev.Peer))
|
2018-12-12 09:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForConnections(state *internalState, timeout <-chan time.Time, events <-chan *p2p.PeerEvent) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-events:
|
|
|
|
processPeerEvent(state, ev)
|
|
|
|
if state.ReachedTarget() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|