2018-07-16 07:40:40 +00:00
|
|
|
package peers
|
|
|
|
|
|
|
|
import (
|
2018-07-25 14:48:02 +00:00
|
|
|
"context"
|
2021-07-12 14:16:33 +00:00
|
|
|
"time"
|
2018-07-25 14:48:02 +00:00
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-11-14 07:03:58 +00:00
|
|
|
"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"
|
2018-07-16 07:40:40 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
2018-07-25 14:48:02 +00:00
|
|
|
// Verifier verifies if a give node is trusted.
|
|
|
|
type Verifier interface {
|
2018-11-14 07:03:58 +00:00
|
|
|
VerifyNode(context.Context, enode.ID) bool
|
2018-07-25 14:48:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
// MailServerDiscoveryTopic topic name for mailserver discovery.
|
2018-07-17 12:39:30 +00:00
|
|
|
const MailServerDiscoveryTopic = "whispermail"
|
2018-07-16 07:40:40 +00:00
|
|
|
|
|
|
|
// MailServerDiscoveryLimits default mailserver discovery limits.
|
|
|
|
var MailServerDiscoveryLimits = params.Limits{Min: 3, Max: 3}
|
|
|
|
|
2018-07-25 14:48:02 +00:00
|
|
|
// cacheOnlyTopicPool handles a mail server topic pool.
|
|
|
|
type cacheOnlyTopicPool struct {
|
|
|
|
*TopicPool
|
|
|
|
verifier Verifier
|
|
|
|
}
|
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
// newCacheOnlyTopicPool returns instance of CacheOnlyTopicPool.
|
2018-07-25 14:48:02 +00:00
|
|
|
func newCacheOnlyTopicPool(t *TopicPool, verifier Verifier) *cacheOnlyTopicPool {
|
2018-07-16 07:40:40 +00:00
|
|
|
return &cacheOnlyTopicPool{
|
|
|
|
TopicPool: t,
|
2018-07-25 14:48:02 +00:00
|
|
|
verifier: verifier,
|
2018-07-16 07:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxReached checks if the max allowed peers is reached or not. When true
|
|
|
|
// peerpool will stop the discovery process on this TopicPool.
|
|
|
|
// Main difference with basic TopicPool is we want to stop discovery process
|
|
|
|
// when the number of cached peers eq/exceeds the max limit.
|
|
|
|
func (t *cacheOnlyTopicPool) MaxReached() bool {
|
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
if t.limits.Max == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
peers := t.cache.GetPeersRange(t.topic, t.limits.Max)
|
|
|
|
return len(peers) >= t.limits.Max
|
|
|
|
}
|
|
|
|
|
|
|
|
var sendEnodeDiscovered = signal.SendEnodeDiscovered
|
|
|
|
|
|
|
|
// ConfirmAdded calls base TopicPool ConfirmAdded method and sends a signal
|
|
|
|
// confirming the enode has been discovered.
|
2018-11-14 07:03:58 +00:00
|
|
|
func (t *cacheOnlyTopicPool) ConfirmAdded(server *p2p.Server, nodeID enode.ID) {
|
2018-07-25 14:48:02 +00:00
|
|
|
trusted := t.verifier.VerifyNode(context.TODO(), nodeID)
|
|
|
|
if trusted {
|
|
|
|
// add to cache only if trusted
|
|
|
|
t.TopicPool.ConfirmAdded(server, nodeID)
|
|
|
|
sendEnodeDiscovered(nodeID.String(), string(t.topic))
|
|
|
|
t.subtractToLimits()
|
|
|
|
}
|
2018-07-16 07:40:40 +00:00
|
|
|
|
2018-07-25 14:48:02 +00:00
|
|
|
// If a peer was trusted, it was moved to connectedPeers,
|
|
|
|
// signal was sent and we can safely remove it.
|
2018-11-14 07:03:58 +00:00
|
|
|
if peer, ok := t.connectedPeers[nodeID]; ok {
|
2021-07-12 14:16:33 +00:00
|
|
|
// NOTE: removeServerPeer removes the server peer immediately.
|
|
|
|
// which means the next discovery.summary is not going to include
|
|
|
|
// the peer.
|
|
|
|
// We leave some time so that we ensure the signal is propagated
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2021-07-12 14:16:33 +00:00
|
|
|
time.Sleep(200)
|
|
|
|
t.removeServerPeer(server, peer)
|
|
|
|
}()
|
2018-07-25 14:48:02 +00:00
|
|
|
// Delete it from `connectedPeers` immediately to
|
|
|
|
// prevent removing it from the cache which logic is
|
|
|
|
// implemented in TopicPool.
|
2018-11-14 07:03:58 +00:00
|
|
|
delete(t.connectedPeers, nodeID)
|
2018-07-25 14:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// It a peer was not trusted, it is still in pendingPeers.
|
|
|
|
// We should remove it from the p2p.Server.
|
2018-11-14 07:03:58 +00:00
|
|
|
if peer, ok := t.pendingPeers[nodeID]; ok {
|
2021-07-12 14:16:33 +00:00
|
|
|
// NOTE: removeServerPeer removes the server peer immediately.
|
|
|
|
// which means the next discovery.summary is not going to include
|
|
|
|
// the peer.
|
|
|
|
// We leave some time so that we ensure the signal is propagated
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2021-07-12 14:16:33 +00:00
|
|
|
time.Sleep(200)
|
|
|
|
t.removeServerPeer(server, peer.peerInfo)
|
|
|
|
}()
|
|
|
|
|
2018-07-25 14:48:02 +00:00
|
|
|
// Delete it from `connectedPeers` immediately to
|
|
|
|
// prevent removing it from the cache which logic is
|
|
|
|
// implemented in TopicPool.
|
2018-11-14 07:03:58 +00:00
|
|
|
delete(t.pendingPeers, nodeID)
|
2018-07-16 07:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subtractToLimits subtracts one to topic pool limits.
|
|
|
|
func (t *cacheOnlyTopicPool) subtractToLimits() {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
if t.limits.Max > 0 {
|
|
|
|
t.limits.Max = t.limits.Max - 1
|
|
|
|
}
|
|
|
|
if t.limits.Min > 0 {
|
|
|
|
t.limits.Min = t.limits.Min - 1
|
|
|
|
}
|
|
|
|
}
|