2018-04-10 06:44:09 +00:00
|
|
|
package peers
|
|
|
|
|
|
|
|
import (
|
2018-04-25 07:13:59 +00:00
|
|
|
"encoding/json"
|
2018-04-10 06:44:09 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-07-03 11:27:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-04-16 18:15:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
"github.com/status-im/rendezvous/server"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
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-05-03 07:35:58 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PeerPoolSimulationSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
bootnode *p2p.Server
|
|
|
|
peers []*p2p.Server
|
|
|
|
discovery []discovery.Discovery
|
|
|
|
rendezvousServer *server.Server
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerPoolSimulationSuite(t *testing.T) {
|
2020-07-27 06:55:20 +00:00
|
|
|
s := &PeerPoolSimulationSuite{}
|
2018-05-26 07:37:13 +00:00
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) SetupTest() {
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
name := common.MakeName("bootnode", "1.0")
|
|
|
|
// 127.0.0.1 is invalidated by discovery v5
|
|
|
|
s.bootnode = &p2p.Server{
|
|
|
|
Config: p2p.Config{
|
|
|
|
MaxPeers: 10,
|
|
|
|
Name: name,
|
2020-09-03 07:30:03 +00:00
|
|
|
ListenAddr: ":0",
|
2018-04-10 06:44:09 +00:00
|
|
|
PrivateKey: key,
|
|
|
|
DiscoveryV5: true,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.Require().NoError(s.bootnode.Start())
|
2020-09-03 07:30:03 +00:00
|
|
|
bootnodePort := uint16(s.bootnode.NodeInfo().Ports.Listener)
|
2018-05-26 07:37:13 +00:00
|
|
|
bootnodeV5 := discv5.NewNode(s.bootnode.DiscV5.Self().ID, net.ParseIP("127.0.0.1"), bootnodePort, bootnodePort)
|
2018-04-10 06:44:09 +00:00
|
|
|
|
2018-04-12 13:08:49 +00:00
|
|
|
// 1 peer to initiate connection, 1 peer as a first candidate, 1 peer - for failover
|
|
|
|
s.peers = make([]*p2p.Server, 3)
|
2018-07-04 10:51:47 +00:00
|
|
|
s.discovery = make([]discovery.Discovery, 3)
|
2018-04-10 06:44:09 +00:00
|
|
|
for i := range s.peers {
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
peer := &p2p.Server{
|
|
|
|
Config: p2p.Config{
|
|
|
|
MaxPeers: 10,
|
|
|
|
Name: common.MakeName("peer-"+strconv.Itoa(i), "1.0"),
|
2020-09-03 07:30:03 +00:00
|
|
|
ListenAddr: ":0",
|
2018-04-10 06:44:09 +00:00
|
|
|
PrivateKey: key,
|
|
|
|
NoDiscovery: true,
|
|
|
|
BootstrapNodesV5: []*discv5.Node{bootnodeV5},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.NoError(peer.Start())
|
|
|
|
s.peers[i] = peer
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PeerPoolSimulationSuite) setupEthV5() {
|
|
|
|
for i := range s.peers {
|
|
|
|
peer := s.peers[i]
|
|
|
|
d := discovery.NewDiscV5(peer.PrivateKey, peer.ListenAddr, peer.BootstrapNodesV5)
|
|
|
|
s.NoError(d.Start())
|
|
|
|
s.discovery[i] = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) TearDown() {
|
|
|
|
s.bootnode.Stop()
|
2018-07-03 11:27:04 +00:00
|
|
|
for i := range s.peers {
|
|
|
|
s.peers[i].Stop()
|
|
|
|
s.NoError(s.discovery[i].Stop())
|
2018-05-01 15:19:11 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
if s.rendezvousServer != nil {
|
|
|
|
s.rendezvousServer.Stop()
|
|
|
|
}
|
2018-05-01 15:19:11 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) getPeerFromEvent(events <-chan *p2p.PeerEvent, etype p2p.PeerEventType) (nodeID enode.ID) {
|
2018-04-12 13:08:49 +00:00
|
|
|
select {
|
|
|
|
case ev := <-events:
|
|
|
|
if ev.Type == etype {
|
|
|
|
return ev.Peer
|
|
|
|
}
|
2018-05-26 07:37:13 +00:00
|
|
|
s.Failf("invalid event", "expected %s but got %s for peer %s", etype, ev.Type, ev.Peer)
|
|
|
|
case <-time.After(10 * time.Second):
|
2018-04-12 13:08:49 +00:00
|
|
|
s.Fail("timed out waiting for a peer")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-25 07:13:59 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) getPoolEvent(events <-chan string) string {
|
2018-04-13 08:34:30 +00:00
|
|
|
select {
|
|
|
|
case ev := <-events:
|
|
|
|
return ev
|
2018-05-26 07:37:13 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
s.FailNow("timed out waiting a pool event")
|
2018-04-13 08:34:30 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) TestPeerPoolCacheEthV5() {
|
|
|
|
s.setupEthV5()
|
2018-05-26 07:37:13 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
topic := discv5.Topic("cap=test")
|
|
|
|
config := map[discv5.Topic]params.Limits{
|
|
|
|
topic: params.NewLimits(1, 1),
|
|
|
|
}
|
2018-08-20 13:55:43 +00:00
|
|
|
peerPoolOpts := &Options{100 * time.Millisecond, 100 * time.Millisecond, 0, true, 100 * time.Millisecond, nil, ""}
|
2018-05-26 07:37:13 +00:00
|
|
|
cache, err := newInMemoryCache()
|
|
|
|
s.Require().NoError(err)
|
2018-07-03 11:27:04 +00:00
|
|
|
peerPool := NewPeerPool(s.discovery[1], config, cache, peerPoolOpts)
|
2018-05-26 07:37:13 +00:00
|
|
|
|
|
|
|
// start peer pool
|
2018-08-20 13:55:43 +00:00
|
|
|
s.Require().NoError(peerPool.Start(s.peers[1], nil))
|
2018-05-26 07:37:13 +00:00
|
|
|
defer peerPool.Stop()
|
|
|
|
|
|
|
|
// check if cache is passed to topic pools
|
|
|
|
for _, topicPool := range peerPool.topics {
|
2018-07-16 07:40:40 +00:00
|
|
|
tp := topicPool.(*TopicPool)
|
|
|
|
s.Equal(cache, tp.cache)
|
2018-05-26 07:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
// TestPeerPoolMaxPeersOverflow verifies that following scenario will not occur:
|
2018-04-16 18:15:03 +00:00
|
|
|
// - found peer A and B in the same kademlia cycle
|
|
|
|
// - process peer A
|
|
|
|
// - max limit is reached -> closed discv5 and set it to nil
|
|
|
|
// - process peer B
|
|
|
|
// - panic because discv5 is nil!!!
|
2018-05-01 15:19:11 +00:00
|
|
|
func TestPeerPoolMaxPeersOverflow(t *testing.T) {
|
2018-06-06 13:39:27 +00:00
|
|
|
maxCachedPeersMultiplier = 0
|
2018-05-26 07:37:13 +00:00
|
|
|
signals := make(chan string, 10)
|
2018-05-01 15:19:11 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope struct {
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(jsonEvent), &envelope))
|
|
|
|
signals <- envelope.Type
|
|
|
|
})
|
|
|
|
defer signal.ResetDefaultNodeNotificationHandler()
|
|
|
|
|
2018-04-16 18:15:03 +00:00
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
peer := &p2p.Server{
|
|
|
|
Config: p2p.Config{
|
|
|
|
PrivateKey: key,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, peer.Start())
|
|
|
|
defer peer.Stop()
|
2018-07-04 10:51:47 +00:00
|
|
|
discovery := discovery.NewDiscV5(key, peer.ListenAddr, nil)
|
2018-07-03 11:27:04 +00:00
|
|
|
require.NoError(t, discovery.Start())
|
|
|
|
defer func() { assert.NoError(t, discovery.Stop()) }()
|
|
|
|
require.True(t, discovery.Running())
|
2018-05-01 15:19:11 +00:00
|
|
|
|
2018-08-20 13:55:43 +00:00
|
|
|
poolOpts := &Options{DefaultFastSync, DefaultSlowSync, 0, true, 100 * time.Millisecond, nil, ""}
|
2018-07-03 11:27:04 +00:00
|
|
|
pool := NewPeerPool(discovery, nil, nil, poolOpts)
|
2018-08-20 13:55:43 +00:00
|
|
|
require.NoError(t, pool.Start(peer, nil))
|
2018-05-03 07:35:58 +00:00
|
|
|
require.Equal(t, signal.EventDiscoveryStarted, <-signals)
|
2018-05-01 15:19:11 +00:00
|
|
|
// without config, it will stop the discovery because all topic pools are satisfied
|
|
|
|
pool.events <- &p2p.PeerEvent{Type: p2p.PeerEventTypeAdd}
|
2018-06-06 13:39:27 +00:00
|
|
|
require.Equal(t, signal.EventDiscoverySummary, <-signals)
|
2018-05-03 07:35:58 +00:00
|
|
|
require.Equal(t, signal.EventDiscoveryStopped, <-signals)
|
2018-07-03 11:27:04 +00:00
|
|
|
require.False(t, discovery.Running())
|
2018-05-01 15:19:11 +00:00
|
|
|
// another peer added after discovery is stopped should not panic
|
|
|
|
pool.events <- &p2p.PeerEvent{Type: p2p.PeerEventTypeAdd}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerPoolDiscV5Timeout(t *testing.T) {
|
|
|
|
signals := make(chan string)
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope struct {
|
|
|
|
Type string
|
|
|
|
Event json.RawMessage
|
|
|
|
}
|
|
|
|
require.NoError(t, json.Unmarshal([]byte(jsonEvent), &envelope))
|
|
|
|
// Send signal asynchronously to avoid blocking.
|
|
|
|
// It's better than sending to a buffered channel because
|
|
|
|
// it won't ever block, for example, if two events were expected
|
|
|
|
// but received more.
|
|
|
|
// In this case, a strange PeerEventTypeDrop event was emitted.
|
|
|
|
go func() {
|
|
|
|
switch typ := envelope.Type; typ {
|
2018-05-03 07:35:58 +00:00
|
|
|
case signal.EventDiscoveryStarted, signal.EventDiscoveryStopped:
|
2018-05-01 15:19:11 +00:00
|
|
|
signals <- envelope.Type
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
defer signal.ResetDefaultNodeNotificationHandler()
|
|
|
|
|
|
|
|
// start server
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
server := &p2p.Server{
|
|
|
|
Config: p2p.Config{
|
|
|
|
PrivateKey: key,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, server.Start())
|
|
|
|
defer server.Stop()
|
2018-07-03 11:27:04 +00:00
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
discovery := discovery.NewDiscV5(key, server.ListenAddr, nil)
|
2018-07-03 11:27:04 +00:00
|
|
|
require.NoError(t, discovery.Start())
|
|
|
|
defer func() { assert.NoError(t, discovery.Stop()) }()
|
|
|
|
require.True(t, discovery.Running())
|
2018-05-01 15:19:11 +00:00
|
|
|
|
|
|
|
// start PeerPool
|
2018-08-20 13:55:43 +00:00
|
|
|
poolOpts := &Options{DefaultFastSync, DefaultSlowSync, time.Millisecond * 100, true, 100 * time.Millisecond, nil, ""}
|
2018-07-03 11:27:04 +00:00
|
|
|
pool := NewPeerPool(discovery, nil, nil, poolOpts)
|
2018-08-20 13:55:43 +00:00
|
|
|
require.NoError(t, pool.Start(server, nil))
|
2018-05-03 07:35:58 +00:00
|
|
|
require.Equal(t, signal.EventDiscoveryStarted, <-signals)
|
2018-05-01 15:19:11 +00:00
|
|
|
|
|
|
|
// timeout after finding no peers
|
|
|
|
select {
|
|
|
|
case sig := <-signals:
|
2018-05-03 07:35:58 +00:00
|
|
|
require.Equal(t, signal.EventDiscoveryStopped, sig)
|
2018-05-15 09:16:25 +00:00
|
|
|
case <-time.After(pool.opts.DiscServerTimeout * 2):
|
2018-05-01 15:19:11 +00:00
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
2018-07-03 11:27:04 +00:00
|
|
|
require.False(t, discovery.Running())
|
2018-05-01 15:19:11 +00:00
|
|
|
|
|
|
|
// timeout after discovery restart
|
|
|
|
require.NoError(t, pool.restartDiscovery(server))
|
2018-05-03 07:35:58 +00:00
|
|
|
require.Equal(t, signal.EventDiscoveryStarted, <-signals)
|
2018-07-03 11:27:04 +00:00
|
|
|
require.True(t, discovery.Running())
|
2018-05-01 15:19:11 +00:00
|
|
|
pool.events <- &p2p.PeerEvent{Type: p2p.PeerEventTypeDrop} // required to turn the loop and pick up new timeout
|
|
|
|
select {
|
|
|
|
case sig := <-signals:
|
2018-05-03 07:35:58 +00:00
|
|
|
require.Equal(t, signal.EventDiscoveryStopped, sig)
|
2018-05-15 09:16:25 +00:00
|
|
|
case <-time.After(pool.opts.DiscServerTimeout * 2):
|
2018-05-01 15:19:11 +00:00
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
2018-07-03 11:27:04 +00:00
|
|
|
require.False(t, discovery.Running())
|
2018-04-16 18:15:03 +00:00
|
|
|
}
|
2018-05-15 09:16:25 +00:00
|
|
|
|
|
|
|
func TestPeerPoolNotAllowedStopping(t *testing.T) {
|
|
|
|
// create and start server
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
server := &p2p.Server{
|
|
|
|
Config: p2p.Config{
|
|
|
|
PrivateKey: key,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, server.Start())
|
|
|
|
defer server.Stop()
|
2018-07-03 11:27:04 +00:00
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
discovery := discovery.NewDiscV5(key, server.ListenAddr, nil)
|
2018-07-03 11:27:04 +00:00
|
|
|
require.NoError(t, discovery.Start())
|
|
|
|
defer func() { assert.NoError(t, discovery.Stop()) }()
|
|
|
|
require.True(t, discovery.Running())
|
2018-05-15 09:16:25 +00:00
|
|
|
|
|
|
|
// start PeerPool
|
2018-08-20 13:55:43 +00:00
|
|
|
poolOpts := &Options{DefaultFastSync, DefaultSlowSync, time.Millisecond * 100, false, 100 * time.Millisecond, nil, ""}
|
2018-07-03 11:27:04 +00:00
|
|
|
pool := NewPeerPool(discovery, nil, nil, poolOpts)
|
2018-08-20 13:55:43 +00:00
|
|
|
require.NoError(t, pool.Start(server, nil))
|
2018-05-15 09:16:25 +00:00
|
|
|
|
|
|
|
// wait 2x timeout duration
|
|
|
|
<-time.After(pool.opts.DiscServerTimeout * 2)
|
2018-07-03 11:27:04 +00:00
|
|
|
require.True(t, discovery.Running())
|
2018-05-15 09:16:25 +00:00
|
|
|
}
|
2018-07-16 07:40:40 +00:00
|
|
|
|
|
|
|
func (s *PeerPoolSimulationSuite) TestUpdateTopicLimits() {
|
2018-07-04 10:51:47 +00:00
|
|
|
s.setupEthV5()
|
2018-07-16 07:40:40 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
topic := discv5.Topic("cap=test")
|
|
|
|
config := map[discv5.Topic]params.Limits{
|
|
|
|
topic: params.NewLimits(1, 1),
|
|
|
|
}
|
2018-08-20 13:55:43 +00:00
|
|
|
peerPoolOpts := &Options{100 * time.Millisecond, 100 * time.Millisecond, 0, true, 100 * time.Millisecond, nil, ""}
|
2018-07-16 07:40:40 +00:00
|
|
|
cache, err := newInMemoryCache()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
peerPool := NewPeerPool(s.discovery[1], config, cache, peerPoolOpts)
|
|
|
|
|
|
|
|
// start peer pool
|
2018-08-20 13:55:43 +00:00
|
|
|
s.Require().NoError(peerPool.Start(s.peers[1], nil))
|
2018-07-16 07:40:40 +00:00
|
|
|
defer peerPool.Stop()
|
|
|
|
|
|
|
|
for _, topicPool := range peerPool.topics {
|
|
|
|
tp := topicPool.(*TopicPool)
|
|
|
|
s.Equal(1, tp.limits.Max)
|
|
|
|
s.Equal(1, tp.limits.Min)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updating TopicPool's limits
|
|
|
|
err = peerPool.UpdateTopic("cap=test", params.NewLimits(5, 10))
|
|
|
|
s.Require().NoError(err)
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
for _, topicPool := range peerPool.topics {
|
|
|
|
tp := topicPool.(*TopicPool)
|
|
|
|
tp.mu.RLock()
|
|
|
|
defer tp.mu.RUnlock()
|
|
|
|
|
|
|
|
s.Equal(10, tp.limits.Max)
|
|
|
|
s.Equal(5, tp.limits.Min)
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 14:48:02 +00:00
|
|
|
|
|
|
|
func (s *PeerPoolSimulationSuite) TestMailServerPeersDiscovery() {
|
|
|
|
s.setupEthV5()
|
|
|
|
|
2019-01-10 16:07:16 +00:00
|
|
|
// eliminate peer we won't use
|
|
|
|
s.peers[2].Stop()
|
|
|
|
|
2018-07-25 14:48:02 +00:00
|
|
|
// Buffered channels must be used because we expect the events
|
|
|
|
// to be in the same order. Use a buffer length greater than
|
|
|
|
// the expected number of events to avoid deadlock.
|
|
|
|
poolEvents := make(chan string, 10)
|
|
|
|
summaries := make(chan []*p2p.PeerInfo, 10)
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope struct {
|
|
|
|
Type string
|
|
|
|
Event json.RawMessage
|
|
|
|
}
|
|
|
|
s.NoError(json.Unmarshal([]byte(jsonEvent), &envelope))
|
|
|
|
|
|
|
|
switch typ := envelope.Type; typ {
|
|
|
|
case signal.EventDiscoverySummary:
|
|
|
|
poolEvents <- envelope.Type
|
|
|
|
var summary []*p2p.PeerInfo
|
|
|
|
s.NoError(json.Unmarshal(envelope.Event, &summary))
|
|
|
|
summaries <- summary
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer signal.ResetDefaultNodeNotificationHandler()
|
|
|
|
|
|
|
|
// subscribe for peer events before starting the peer pool
|
|
|
|
events := make(chan *p2p.PeerEvent, 20)
|
|
|
|
subscription := s.peers[1].SubscribeEvents(events)
|
|
|
|
defer subscription.Unsubscribe()
|
|
|
|
|
|
|
|
// create and start topic registry
|
|
|
|
register := NewRegister(s.discovery[0], MailServerDiscoveryTopic)
|
|
|
|
s.Require().NoError(register.Start())
|
|
|
|
|
|
|
|
// create and start peer pool
|
|
|
|
config := map[discv5.Topic]params.Limits{
|
|
|
|
MailServerDiscoveryTopic: params.NewLimits(1, 1),
|
|
|
|
}
|
|
|
|
cache, err := newInMemoryCache()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
peerPoolOpts := &Options{
|
|
|
|
100 * time.Millisecond,
|
|
|
|
100 * time.Millisecond,
|
|
|
|
0,
|
|
|
|
true,
|
|
|
|
100 * time.Millisecond,
|
2018-11-14 07:03:58 +00:00
|
|
|
[]enode.ID{s.peers[0].Self().ID()},
|
2018-08-20 13:55:43 +00:00
|
|
|
"",
|
2018-07-25 14:48:02 +00:00
|
|
|
}
|
|
|
|
peerPool := NewPeerPool(s.discovery[1], config, cache, peerPoolOpts)
|
2018-08-20 13:55:43 +00:00
|
|
|
s.Require().NoError(peerPool.Start(s.peers[1], nil))
|
2018-07-25 14:48:02 +00:00
|
|
|
defer peerPool.Stop()
|
|
|
|
|
|
|
|
// wait for and verify the mail server peer
|
|
|
|
connectedPeer := s.getPeerFromEvent(events, p2p.PeerEventTypeAdd)
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Equal(s.peers[0].Self().ID().String(), connectedPeer.String())
|
2018-07-25 14:48:02 +00:00
|
|
|
|
|
|
|
// wait for a summary event to be sure that ConfirmAdded() was called
|
|
|
|
s.Equal(signal.EventDiscoverySummary, s.getPoolEvent(poolEvents))
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Equal(s.peers[0].Self().ID().String(), (<-summaries)[0].ID)
|
2018-07-25 14:48:02 +00:00
|
|
|
|
|
|
|
// check cache
|
|
|
|
cachedPeers := peerPool.cache.GetPeersRange(MailServerDiscoveryTopic, 5)
|
|
|
|
s.Require().Len(cachedPeers, 1)
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Equal(discv5.PubkeyID(s.peers[0].Self().Pubkey()), cachedPeers[0].ID)
|
2018-07-25 14:48:02 +00:00
|
|
|
|
2018-07-27 09:50:26 +00:00
|
|
|
// wait for another event as the peer should be removed
|
|
|
|
disconnectedPeer := s.getPeerFromEvent(events, p2p.PeerEventTypeDrop)
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Equal(s.peers[0].Self().ID().String(), disconnectedPeer.String())
|
2018-07-25 14:48:02 +00:00
|
|
|
s.Equal(signal.EventDiscoverySummary, s.getPoolEvent(poolEvents))
|
|
|
|
}
|