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/common"
|
|
|
|
"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"
|
2018-12-12 09:39:00 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2018-12-05 13:57:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
peerEventsBuffer = 10 // sufficient buffer to avoid blocking a p2p feed.
|
|
|
|
whisperEventsBuffer = 20 // sufficient buffer to avod blocking a whisper envelopes feed.
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnvelopeEventSubscbriber interface to subscribe for whisper.EnvelopeEvent's.
|
|
|
|
type EnvelopeEventSubscbriber interface {
|
2018-12-12 09:39:00 +00:00
|
|
|
SubscribeEnvelopeEvents(chan<- whisper.EnvelopeEvent) event.Subscription
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type p2pServer interface {
|
|
|
|
PeerAdderRemover
|
|
|
|
PeerEventsSubscriber
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnectionManager creates an instance of ConnectionManager.
|
2018-12-12 09:39:00 +00:00
|
|
|
func NewConnectionManager(server p2pServer, whisper EnvelopeEventSubscbriber, target 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,
|
|
|
|
whisper: whisper,
|
|
|
|
connectedTarget: target,
|
|
|
|
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{}
|
|
|
|
|
|
|
|
server p2pServer
|
|
|
|
whisper EnvelopeEventSubscbriber
|
|
|
|
|
2018-12-12 09:39:00 +00:00
|
|
|
notifications chan []*enode.Node
|
|
|
|
connectedTarget int
|
|
|
|
timeoutWaitAdded time.Duration
|
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() {
|
|
|
|
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() {
|
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)
|
2018-12-12 09:39:00 +00:00
|
|
|
whisperEvents := make(chan whisper.EnvelopeEvent, whisperEventsBuffer)
|
2018-12-05 13:57:05 +00:00
|
|
|
whisperSub := ps.whisper.SubscribeEnvelopeEvents(whisperEvents)
|
|
|
|
requests := map[common.Hash]struct{}{}
|
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():
|
|
|
|
log.Error("retry after error suscribing to whisper 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:
|
|
|
|
// TODO what about completed but with error? what about expired envelopes?
|
|
|
|
switch ev.Event {
|
2018-12-12 09:39:00 +00:00
|
|
|
case whisper.EventMailServerRequestSent:
|
2018-12-05 13:57:05 +00:00
|
|
|
requests[ev.Hash] = struct{}{}
|
2018-12-12 09:39:00 +00:00
|
|
|
case whisper.EventMailServerRequestCompleted:
|
2018-12-05 13:57:05 +00:00
|
|
|
delete(requests, ev.Hash)
|
2018-12-12 09:39:00 +00:00
|
|
|
case whisper.EventMailServerRequestExpired:
|
2018-12-05 13:57:05 +00:00
|
|
|
_, exist := requests[ev.Hash]
|
|
|
|
if !exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Debug("request to a mail server expired, disconncet a peer", "address", ev.Peer)
|
2018-12-12 09:39:00 +00:00
|
|
|
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) {
|
|
|
|
replacement := map[enode.ID]*enode.Node{}
|
|
|
|
for _, n := range newNodes {
|
|
|
|
replacement[n.ID()] = n
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
connected: map[enode.ID]struct{}{},
|
|
|
|
currentNodes: map[enode.ID]*enode.Node{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type options struct {
|
|
|
|
target int
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type internalState struct {
|
|
|
|
options
|
|
|
|
srv PeerAdderRemover
|
|
|
|
|
|
|
|
connected map[enode.ID]struct{}
|
|
|
|
currentNodes map[enode.ID]*enode.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *internalState) ReachedTarget() bool {
|
|
|
|
return len(state.connected) >= state.target
|
|
|
|
}
|
|
|
|
|
|
|
|
func (state *internalState) replaceNodes(new map[enode.ID]*enode.Node) {
|
|
|
|
for nid, n := range state.currentNodes {
|
2018-12-05 13:57:05 +00:00
|
|
|
if _, exist := new[nid]; !exist {
|
2018-12-12 09:39:00 +00:00
|
|
|
if _, exist := state.connected[nid]; exist {
|
|
|
|
delete(state.connected, nid)
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-12-12 09:39:00 +00:00
|
|
|
func (state *internalState) nodeAdded(peer enode.ID) {
|
|
|
|
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 {
|
2018-12-12 09:39:00 +00:00
|
|
|
state.connected[n.ID()] = struct{}{}
|
2018-12-05 13:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:39:00 +00:00
|
|
|
func (state *internalState) nodeDisconnected(peer enode.ID) {
|
|
|
|
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)
|
|
|
|
state.nodeAdded(ev.Peer)
|
|
|
|
case p2p.PeerEventTypeDrop:
|
|
|
|
log.Debug("mailserver disconnected", "address", ev.Peer)
|
|
|
|
state.nodeDisconnected(ev.Peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|