2018-04-10 06:44:09 +00:00
|
|
|
package peers
|
|
|
|
|
|
|
|
import (
|
2018-04-19 15:18:49 +00:00
|
|
|
"container/heap"
|
2018-04-10 06:44:09 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-07-04 10:51:47 +00:00
|
|
|
"github.com/status-im/status-go/discovery"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
|
|
|
|
2018-05-22 13:11:21 +00:00
|
|
|
const (
|
|
|
|
// notQueuedIndex used to define that item is not queued in the heap queue.
|
|
|
|
notQueuedIndex = -1
|
|
|
|
)
|
|
|
|
|
2018-06-06 13:39:27 +00:00
|
|
|
// maxCachedPeersMultiplier peers max limit will be multiplied by this number
|
|
|
|
// to get the maximum number of cached peers allowed.
|
2018-11-13 13:58:26 +00:00
|
|
|
var maxCachedPeersMultiplier = 1
|
2018-06-06 13:39:27 +00:00
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
// TopicPoolInterface the TopicPool interface.
|
|
|
|
type TopicPoolInterface interface {
|
|
|
|
StopSearch(server *p2p.Server)
|
|
|
|
BelowMin() bool
|
|
|
|
SearchRunning() bool
|
|
|
|
StartSearch(server *p2p.Server) error
|
2018-11-14 07:03:58 +00:00
|
|
|
ConfirmDropped(server *p2p.Server, nodeID enode.ID) bool
|
2018-07-16 07:40:40 +00:00
|
|
|
AddPeerFromTable(server *p2p.Server) *discv5.Node
|
|
|
|
MaxReached() bool
|
2018-11-14 07:03:58 +00:00
|
|
|
ConfirmAdded(server *p2p.Server, nodeID enode.ID)
|
2018-07-16 07:40:40 +00:00
|
|
|
isStopped() bool
|
|
|
|
Topic() discv5.Topic
|
|
|
|
SetLimits(limits params.Limits)
|
|
|
|
setStopSearchTimeout(delay time.Duration)
|
|
|
|
readyToStopSearch() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTopicPool returns instance of TopicPool.
|
2018-07-04 10:51:47 +00:00
|
|
|
func newTopicPool(discovery discovery.Discovery, topic discv5.Topic, limits params.Limits, slowMode, fastMode time.Duration, cache *Cache) *TopicPool {
|
2018-04-19 15:18:49 +00:00
|
|
|
pool := TopicPool{
|
2018-07-03 11:27:04 +00:00
|
|
|
discovery: discovery,
|
2018-05-22 13:11:21 +00:00
|
|
|
topic: topic,
|
|
|
|
limits: limits,
|
|
|
|
fastMode: fastMode,
|
|
|
|
slowMode: slowMode,
|
|
|
|
fastModeTimeout: DefaultTopicFastModeTimeout,
|
2018-11-14 07:03:58 +00:00
|
|
|
pendingPeers: make(map[enode.ID]*peerInfoItem),
|
2018-05-22 13:11:21 +00:00
|
|
|
discoveredPeersQueue: make(peerPriorityQueue, 0),
|
2018-11-14 07:03:58 +00:00
|
|
|
connectedPeers: make(map[enode.ID]*peerInfo),
|
2018-05-22 13:11:21 +00:00
|
|
|
cache: cache,
|
2018-06-06 13:39:27 +00:00
|
|
|
maxCachedPeers: limits.Max * maxCachedPeersMultiplier,
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-05-22 13:11:21 +00:00
|
|
|
heap.Init(&pool.discoveredPeersQueue)
|
2018-04-19 15:18:49 +00:00
|
|
|
|
|
|
|
return &pool
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TopicPool manages peers for topic.
|
|
|
|
type TopicPool struct {
|
2018-07-04 10:51:47 +00:00
|
|
|
discovery discovery.Discovery
|
2018-07-03 11:27:04 +00:00
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
// configuration
|
|
|
|
topic discv5.Topic
|
|
|
|
limits params.Limits
|
|
|
|
fastMode time.Duration
|
|
|
|
slowMode time.Duration
|
|
|
|
fastModeTimeout time.Duration
|
|
|
|
|
2018-05-10 11:45:51 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
discWG sync.WaitGroup
|
|
|
|
poolWG sync.WaitGroup
|
|
|
|
quit chan struct{}
|
2018-04-10 06:44:09 +00:00
|
|
|
|
|
|
|
running int32
|
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
currentMode time.Duration
|
|
|
|
period chan time.Duration
|
|
|
|
fastModeTimeoutCancel chan struct{}
|
2018-04-10 06:44:09 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
pendingPeers map[enode.ID]*peerInfoItem // contains found and requested to be connected peers but not confirmed
|
|
|
|
discoveredPeersQueue peerPriorityQueue // priority queue to find the most recently discovered peers; does not containt peers requested to connect
|
|
|
|
connectedPeers map[enode.ID]*peerInfo // currently connected peers
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-06-06 13:39:27 +00:00
|
|
|
stopSearchTimeout *time.Time
|
|
|
|
|
|
|
|
maxCachedPeers int
|
|
|
|
cache *Cache
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 13:11:21 +00:00
|
|
|
func (t *TopicPool) addToPendingPeers(peer *peerInfo) {
|
2018-11-14 07:03:58 +00:00
|
|
|
if _, ok := t.pendingPeers[peer.NodeID()]; ok {
|
2018-04-19 15:18:49 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
t.pendingPeers[peer.NodeID()] = &peerInfoItem{
|
2018-05-22 13:11:21 +00:00
|
|
|
peerInfo: peer,
|
|
|
|
index: notQueuedIndex,
|
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 13:11:21 +00:00
|
|
|
// addToQueue adds the passed peer to the queue if it is already pending.
|
|
|
|
func (t *TopicPool) addToQueue(peer *peerInfo) {
|
2018-11-14 07:03:58 +00:00
|
|
|
if p, ok := t.pendingPeers[peer.NodeID()]; ok {
|
2018-05-22 13:11:21 +00:00
|
|
|
heap.Push(&t.discoveredPeersQueue, p)
|
2018-04-19 15:18:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 13:11:21 +00:00
|
|
|
func (t *TopicPool) popFromQueue() *peerInfo {
|
|
|
|
if t.discoveredPeersQueue.Len() == 0 {
|
2018-04-19 15:18:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-05-22 13:11:21 +00:00
|
|
|
item := heap.Pop(&t.discoveredPeersQueue).(*peerInfoItem)
|
|
|
|
item.index = notQueuedIndex
|
2018-04-19 15:18:49 +00:00
|
|
|
return item.peerInfo
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *TopicPool) removeFromPendingPeers(nodeID enode.ID) {
|
2018-05-22 13:11:21 +00:00
|
|
|
peer, ok := t.pendingPeers[nodeID]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(t.pendingPeers, nodeID)
|
|
|
|
if peer.index != notQueuedIndex {
|
|
|
|
heap.Remove(&t.discoveredPeersQueue, peer.index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *TopicPool) updatePendingPeer(nodeID enode.ID, time mclock.AbsTime) {
|
2018-05-22 13:11:21 +00:00
|
|
|
peer, ok := t.pendingPeers[nodeID]
|
2018-04-19 15:18:49 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
peer.discoveredTime = mclock.Now()
|
2018-05-22 13:11:21 +00:00
|
|
|
if peer.index != notQueuedIndex {
|
|
|
|
heap.Fix(&t.discoveredPeersQueue, peer.index)
|
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *TopicPool) movePeerFromPoolToConnected(nodeID enode.ID) {
|
2018-05-22 13:11:21 +00:00
|
|
|
peer, ok := t.pendingPeers[nodeID]
|
2018-04-19 15:18:49 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-05-22 13:11:21 +00:00
|
|
|
t.removeFromPendingPeers(nodeID)
|
2018-04-19 15:18:49 +00:00
|
|
|
t.connectedPeers[nodeID] = peer.peerInfo
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
// SearchRunning returns true if search is running
|
|
|
|
func (t *TopicPool) SearchRunning() bool {
|
|
|
|
return atomic.LoadInt32(&t.running) == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxReached returns true if we connected with max number of peers.
|
|
|
|
func (t *TopicPool) MaxReached() bool {
|
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2018-04-20 07:39:31 +00:00
|
|
|
return len(t.connectedPeers) == t.limits.Max
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 13:08:49 +00:00
|
|
|
// BelowMin returns true if current number of peers is below min limit.
|
|
|
|
func (t *TopicPool) BelowMin() bool {
|
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2018-04-20 07:39:31 +00:00
|
|
|
return len(t.connectedPeers) < t.limits.Min
|
2018-04-12 13:08:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 13:39:27 +00:00
|
|
|
// maxCachedPeersReached returns true if max number of cached peers is reached.
|
|
|
|
func (t *TopicPool) maxCachedPeersReached() bool {
|
|
|
|
if t.maxCachedPeers == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
peers := t.cache.GetPeersRange(t.topic, t.maxCachedPeers)
|
|
|
|
|
|
|
|
return len(peers) >= t.maxCachedPeers
|
|
|
|
}
|
|
|
|
|
|
|
|
// setStopSearchTimeout sets the timeout to stop current topic search if it's not
|
|
|
|
// been stopped before.
|
|
|
|
func (t *TopicPool) setStopSearchTimeout(delay time.Duration) {
|
|
|
|
if t.stopSearchTimeout != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
now := time.Now().Add(delay)
|
|
|
|
t.stopSearchTimeout = &now
|
|
|
|
}
|
|
|
|
|
|
|
|
// isStopSearchDelayExpired returns true if the timeout to stop current topic
|
|
|
|
// search has been accomplished.
|
|
|
|
func (t *TopicPool) isStopSearchDelayExpired() bool {
|
|
|
|
if t.stopSearchTimeout == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return t.stopSearchTimeout.Before(time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
// readyToStopSearch return true if all conditions to stop search are ok.
|
|
|
|
func (t *TopicPool) readyToStopSearch() bool {
|
|
|
|
return t.isStopSearchDelayExpired() || t.maxCachedPeersReached()
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
// updateSyncMode changes the sync mode depending on the current number
|
|
|
|
// of connected peers and limits.
|
|
|
|
func (t *TopicPool) updateSyncMode() {
|
|
|
|
newMode := t.slowMode
|
|
|
|
if len(t.connectedPeers) < t.limits.Min {
|
|
|
|
newMode = t.fastMode
|
|
|
|
}
|
|
|
|
t.setSyncMode(newMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TopicPool) setSyncMode(mode time.Duration) {
|
|
|
|
if mode == t.currentMode {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.period <- mode
|
|
|
|
t.currentMode = mode
|
|
|
|
|
|
|
|
// if selected mode is fast mode and fast mode timeout was not set yet,
|
|
|
|
// do it now
|
|
|
|
if mode == t.fastMode && t.fastModeTimeoutCancel == nil {
|
|
|
|
t.fastModeTimeoutCancel = t.limitFastMode(t.fastModeTimeout)
|
|
|
|
}
|
|
|
|
// remove fast mode timeout as slow mode is selected now
|
|
|
|
if mode == t.slowMode && t.fastModeTimeoutCancel != nil {
|
|
|
|
close(t.fastModeTimeoutCancel)
|
|
|
|
t.fastModeTimeoutCancel = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TopicPool) limitFastMode(timeout time.Duration) chan struct{} {
|
|
|
|
if timeout == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
|
2018-05-10 11:45:51 +00:00
|
|
|
t.poolWG.Add(1)
|
2018-05-01 15:19:11 +00:00
|
|
|
go func() {
|
2018-05-10 11:45:51 +00:00
|
|
|
defer t.poolWG.Done()
|
2018-05-01 15:19:11 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
t.mu.Lock()
|
|
|
|
t.setSyncMode(t.slowMode)
|
|
|
|
t.mu.Unlock()
|
|
|
|
case <-cancel:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cancel
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
// ConfirmAdded called when peer was added by p2p Server.
|
|
|
|
// 1. Skip a peer if it not in our peer table
|
|
|
|
// 2. Add a peer to a cache.
|
|
|
|
// 3. Disconnect a peer if it was connected after we reached max limit of peers.
|
|
|
|
// (we can't know in advance if peer will be connected, thats why we allow
|
|
|
|
// to overflow for short duration)
|
|
|
|
// 4. Switch search to slow mode if it is running.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *TopicPool) ConfirmAdded(server *p2p.Server, nodeID enode.ID) {
|
2018-04-10 06:44:09 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
peerInfoItem, ok := t.pendingPeers[nodeID]
|
2018-11-13 13:58:26 +00:00
|
|
|
inbound := !ok || !peerInfoItem.added
|
2018-11-14 07:03:58 +00:00
|
|
|
|
2018-11-13 13:58:26 +00:00
|
|
|
log.Debug("peer added event", "peer", nodeID.String(), "inbound", inbound)
|
2018-11-14 07:03:58 +00:00
|
|
|
|
2018-11-13 13:58:26 +00:00
|
|
|
if inbound {
|
2018-04-10 06:44:09 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
|
2018-05-22 13:11:21 +00:00
|
|
|
peer := peerInfoItem.peerInfo // get explicit reference
|
2018-04-19 15:18:49 +00:00
|
|
|
|
|
|
|
// established connection means that the node
|
|
|
|
// is a viable candidate for a connection and can be cached
|
2018-05-15 15:05:57 +00:00
|
|
|
if err := t.cache.AddPeer(peer.node, t.topic); err != nil {
|
|
|
|
log.Error("failed to persist a peer", "error", err)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
t.movePeerFromPoolToConnected(nodeID)
|
2018-04-19 15:18:49 +00:00
|
|
|
// if the upper limit is already reached, drop this peer
|
2018-05-22 13:11:21 +00:00
|
|
|
if len(t.connectedPeers) > t.limits.Max {
|
2018-04-10 06:44:09 +00:00
|
|
|
log.Debug("max limit is reached drop the peer", "ID", nodeID, "topic", t.topic)
|
2018-04-19 15:18:49 +00:00
|
|
|
peer.dismissed = true
|
2018-05-22 13:11:21 +00:00
|
|
|
t.removeServerPeer(server, peer)
|
2018-04-10 06:44:09 +00:00
|
|
|
return
|
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
// make sure `dismissed` is reset
|
2018-04-19 15:18:49 +00:00
|
|
|
peer.dismissed = false
|
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
// A peer was added so check if we can switch to slow mode.
|
|
|
|
if t.SearchRunning() {
|
|
|
|
t.updateSyncMode()
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfirmDropped called when server receives drop event.
|
|
|
|
// 1. Skip peer if it is not in our peer table.
|
|
|
|
// 2. If disconnect request - we could drop that peer ourselves.
|
|
|
|
// 3. If connected number will drop below min limit - switch to fast mode.
|
|
|
|
// 4. Delete a peer from cache and peer table.
|
2018-04-12 13:08:49 +00:00
|
|
|
// Returns false if peer is not in our table or we requested removal of this peer.
|
|
|
|
// Otherwise peer is removed and true is returned.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *TopicPool) ConfirmDropped(server *p2p.Server, nodeID enode.ID) bool {
|
2018-04-10 06:44:09 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
// either inbound or connected from another topic
|
2018-11-14 07:03:58 +00:00
|
|
|
peer, exist := t.connectedPeers[nodeID]
|
2018-04-10 06:44:09 +00:00
|
|
|
if !exist {
|
|
|
|
return false
|
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
|
|
|
|
log.Debug("disconnect", "ID", nodeID, "dismissed", peer.dismissed)
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
delete(t.connectedPeers, nodeID)
|
2018-04-19 15:18:49 +00:00
|
|
|
// Peer was removed by us because exceeded the limit.
|
|
|
|
// Add it back to the pool as it can be useful in the future.
|
|
|
|
if peer.dismissed {
|
2018-05-22 13:11:21 +00:00
|
|
|
t.addToPendingPeers(peer)
|
|
|
|
// use queue for peers that weren't added to p2p server
|
|
|
|
t.addToQueue(peer)
|
2018-04-10 06:44:09 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
|
|
|
|
// If there was a network error, this event will be received
|
|
|
|
// but the peer won't be removed from the static nodes set.
|
|
|
|
// That's why we need to call `removeServerPeer` manually.
|
|
|
|
t.removeServerPeer(server, peer)
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
if err := t.cache.RemovePeer(nodeID, t.topic); err != nil {
|
2018-05-15 15:05:57 +00:00
|
|
|
log.Error("failed to remove peer from cache", "error", err)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
// As we removed a peer, update a sync strategy if needed.
|
|
|
|
if t.SearchRunning() {
|
|
|
|
t.updateSyncMode()
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:08:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPeerFromTable checks if there is a valid peer in local table and adds it to a server.
|
|
|
|
func (t *TopicPool) AddPeerFromTable(server *p2p.Server) *discv5.Node {
|
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2018-04-19 15:18:49 +00:00
|
|
|
|
|
|
|
// The most recently added peer is removed from the queue.
|
|
|
|
// If it did not expire yet, it will be added to the server.
|
|
|
|
// TODO(adam): investigate if it's worth to keep the peer in the queue
|
|
|
|
// until the server confirms it is added and in the meanwhile only adjust its priority.
|
2018-05-22 13:11:21 +00:00
|
|
|
peer := t.popFromQueue()
|
2018-04-19 15:18:49 +00:00
|
|
|
if peer != nil && mclock.Now() < peer.discoveredTime+mclock.AbsTime(expirationPeriod) {
|
|
|
|
t.addServerPeer(server, peer)
|
|
|
|
return peer.node
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-04-12 13:08:49 +00:00
|
|
|
return nil
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartSearch creates discv5 queries and runs a loop to consume found peers.
|
|
|
|
func (t *TopicPool) StartSearch(server *p2p.Server) error {
|
|
|
|
if atomic.LoadInt32(&t.running) == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-03 11:27:04 +00:00
|
|
|
if !t.discovery.Running() {
|
2018-04-10 06:44:09 +00:00
|
|
|
return ErrDiscv5NotRunning
|
|
|
|
}
|
2018-05-01 15:19:11 +00:00
|
|
|
atomic.StoreInt32(&t.running, 1)
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2018-05-01 15:19:11 +00:00
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
t.quit = make(chan struct{})
|
2018-06-06 13:39:27 +00:00
|
|
|
t.stopSearchTimeout = nil
|
2018-05-01 15:19:11 +00:00
|
|
|
|
|
|
|
// `period` is used to notify about the current sync mode.
|
|
|
|
t.period = make(chan time.Duration, 2)
|
|
|
|
// use fast sync mode at the beginning
|
|
|
|
t.setSyncMode(t.fastMode)
|
|
|
|
|
|
|
|
// peers management
|
|
|
|
found := make(chan *discv5.Node, 5) // 5 reasonable number for concurrently found nodes
|
|
|
|
lookup := make(chan bool, 10) // sufficiently buffered channel, just prevents blocking because of lookup
|
|
|
|
|
2018-05-15 15:05:57 +00:00
|
|
|
for _, peer := range t.cache.GetPeersRange(t.topic, 5) {
|
|
|
|
log.Debug("adding a peer from cache", "peer", peer)
|
|
|
|
found <- peer
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-05-01 15:19:11 +00:00
|
|
|
|
2018-05-10 11:45:51 +00:00
|
|
|
t.discWG.Add(1)
|
2018-04-10 06:44:09 +00:00
|
|
|
go func() {
|
2018-07-03 11:27:04 +00:00
|
|
|
if err := t.discovery.Discover(string(t.topic), t.period, found, lookup); err != nil {
|
|
|
|
log.Error("error searching foro", "topic", t.topic, "err", err)
|
|
|
|
}
|
2018-05-10 11:45:51 +00:00
|
|
|
t.discWG.Done()
|
2018-04-10 06:44:09 +00:00
|
|
|
}()
|
2018-05-10 11:45:51 +00:00
|
|
|
t.poolWG.Add(1)
|
2018-04-10 06:44:09 +00:00
|
|
|
go func() {
|
|
|
|
t.handleFoundPeers(server, found, lookup)
|
2018-05-10 11:45:51 +00:00
|
|
|
t.poolWG.Done()
|
2018-04-10 06:44:09 +00:00
|
|
|
}()
|
2018-05-01 15:19:11 +00:00
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TopicPool) handleFoundPeers(server *p2p.Server, found <-chan *discv5.Node, lookup <-chan bool) {
|
2018-11-14 07:03:58 +00:00
|
|
|
selfID := discv5.PubkeyID(server.Self().Pubkey())
|
2018-04-10 06:44:09 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.quit:
|
|
|
|
return
|
|
|
|
case <-lookup:
|
|
|
|
case node := <-found:
|
2018-11-14 07:03:58 +00:00
|
|
|
if node.ID == selfID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := t.processFoundNode(server, node); err != nil {
|
|
|
|
log.Error("failed to process found node", "node", node, "error", err)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// processFoundNode called when node is discovered by kademlia search query
|
|
|
|
// 2 important conditions
|
|
|
|
// 1. every time when node is processed we need to update discoveredTime.
|
|
|
|
// peer will be considered as valid later only if it was discovered < 60m ago
|
|
|
|
// 2. if peer is connected or if max limit is reached we are not a adding peer to p2p server
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *TopicPool) processFoundNode(server *p2p.Server, node *discv5.Node) error {
|
2018-04-10 06:44:09 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
pk, err := node.ID.Pubkey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeID := enode.PubkeyToIDV4(pk)
|
|
|
|
|
|
|
|
log.Debug("peer found", "ID", nodeID, "topic", t.topic)
|
2018-04-19 15:18:49 +00:00
|
|
|
|
|
|
|
// peer is already connected so update only discoveredTime
|
2018-11-14 07:03:58 +00:00
|
|
|
if peer, ok := t.connectedPeers[nodeID]; ok {
|
2018-04-19 15:18:49 +00:00
|
|
|
peer.discoveredTime = mclock.Now()
|
2018-11-14 07:03:58 +00:00
|
|
|
return nil
|
2018-04-19 15:18:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
if _, ok := t.pendingPeers[nodeID]; ok {
|
|
|
|
t.updatePendingPeer(nodeID, mclock.Now())
|
2018-04-10 06:44:09 +00:00
|
|
|
} else {
|
2018-05-22 13:11:21 +00:00
|
|
|
t.addToPendingPeers(&peerInfo{
|
2018-04-10 06:44:09 +00:00
|
|
|
discoveredTime: mclock.Now(),
|
|
|
|
node: node,
|
2018-11-14 07:03:58 +00:00
|
|
|
publicKey: pk,
|
2018-04-19 15:18:49 +00:00
|
|
|
})
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-11-13 13:58:26 +00:00
|
|
|
log.Debug(
|
|
|
|
"adding peer to a server", "peer", node.ID.String(),
|
|
|
|
"connected", len(t.connectedPeers), "max", t.maxCachedPeers)
|
2018-04-19 15:18:49 +00:00
|
|
|
// the upper limit is not reached, so let's add this peer
|
2018-06-06 13:39:27 +00:00
|
|
|
if len(t.connectedPeers) < t.maxCachedPeers {
|
2018-11-14 07:03:58 +00:00
|
|
|
t.addServerPeer(server, t.pendingPeers[nodeID].peerInfo)
|
2018-05-22 13:11:21 +00:00
|
|
|
} else {
|
2018-11-14 07:03:58 +00:00
|
|
|
t.addToQueue(t.pendingPeers[nodeID].peerInfo)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
return nil
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 15:18:49 +00:00
|
|
|
func (t *TopicPool) addServerPeer(server *p2p.Server, info *peerInfo) {
|
2018-11-13 13:58:26 +00:00
|
|
|
info.added = true
|
2018-11-14 07:03:58 +00:00
|
|
|
n := enode.NewV4(info.publicKey, info.node.IP, int(info.node.TCP), int(info.node.UDP))
|
|
|
|
server.AddPeer(n)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-19 15:18:49 +00:00
|
|
|
func (t *TopicPool) removeServerPeer(server *p2p.Server, info *peerInfo) {
|
2018-11-13 13:58:26 +00:00
|
|
|
info.added = false
|
2018-11-14 07:03:58 +00:00
|
|
|
n := enode.NewV4(info.publicKey, info.node.IP, int(info.node.TCP), int(info.node.UDP))
|
|
|
|
server.RemovePeer(n)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 13:39:27 +00:00
|
|
|
func (t *TopicPool) isStopped() bool {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
return t.currentMode == 0
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
// StopSearch stops the closes stop
|
2018-07-16 07:40:40 +00:00
|
|
|
func (t *TopicPool) StopSearch(server *p2p.Server) {
|
2018-04-13 08:34:30 +00:00
|
|
|
if !atomic.CompareAndSwapInt32(&t.running, 1, 0) {
|
2018-04-12 13:08:49 +00:00
|
|
|
return
|
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
if t.quit == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-t.quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2018-05-10 11:45:51 +00:00
|
|
|
log.Debug("stoping search", "topic", t.topic)
|
|
|
|
close(t.quit)
|
|
|
|
t.mu.Lock()
|
|
|
|
if t.fastModeTimeoutCancel != nil {
|
|
|
|
close(t.fastModeTimeoutCancel)
|
|
|
|
t.fastModeTimeoutCancel = nil
|
|
|
|
}
|
|
|
|
t.currentMode = 0
|
|
|
|
t.mu.Unlock()
|
|
|
|
// wait for poolWG to exit because it writes to period channel
|
|
|
|
t.poolWG.Wait()
|
|
|
|
close(t.period)
|
|
|
|
t.discWG.Wait()
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-07-16 07:40:40 +00:00
|
|
|
|
|
|
|
// Topic exposes the internal discovery topic.
|
|
|
|
func (t *TopicPool) Topic() discv5.Topic {
|
|
|
|
return t.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLimits set the limits for the current TopicPool.
|
|
|
|
func (t *TopicPool) SetLimits(limits params.Limits) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
t.limits = limits
|
|
|
|
}
|