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
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-04-12 13:08:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2018-04-25 07:13:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/whisper/whisperv6"
|
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"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-05-03 07:35:58 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2018-05-26 07:37:13 +00:00
|
|
|
|
|
|
|
// to access logs in the test with `-log` flag
|
|
|
|
_ "github.com/status-im/status-go/t/utils"
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PeerPoolSimulationSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
bootnode *p2p.Server
|
|
|
|
peers []*p2p.Server
|
2018-05-26 07:37:13 +00:00
|
|
|
port uint16
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerPoolSimulationSuite(t *testing.T) {
|
2018-05-26 07:37:13 +00:00
|
|
|
s := new(PeerPoolSimulationSuite)
|
|
|
|
s.port = 33731
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PeerPoolSimulationSuite) nextPort() uint16 {
|
|
|
|
s.port++
|
|
|
|
return s.port
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PeerPoolSimulationSuite) SetupTest() {
|
2018-05-26 07:37:13 +00:00
|
|
|
bootnodePort := s.nextPort()
|
2018-04-10 06:44:09 +00:00
|
|
|
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,
|
2018-05-26 07:37:13 +00:00
|
|
|
ListenAddr: fmt.Sprintf("0.0.0.0:%d", bootnodePort),
|
2018-04-10 06:44:09 +00:00
|
|
|
PrivateKey: key,
|
|
|
|
DiscoveryV5: true,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.Require().NoError(s.bootnode.Start())
|
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-04-10 06:44:09 +00:00
|
|
|
for i := range s.peers {
|
|
|
|
key, _ := crypto.GenerateKey()
|
2018-04-25 07:13:59 +00:00
|
|
|
whisper := whisperv6.New(nil)
|
2018-04-10 06:44:09 +00:00
|
|
|
peer := &p2p.Server{
|
|
|
|
Config: p2p.Config{
|
|
|
|
MaxPeers: 10,
|
|
|
|
Name: common.MakeName("peer-"+strconv.Itoa(i), "1.0"),
|
2018-05-26 07:37:13 +00:00
|
|
|
ListenAddr: fmt.Sprintf("0.0.0.0:%d", s.nextPort()),
|
2018-04-10 06:44:09 +00:00
|
|
|
PrivateKey: key,
|
|
|
|
DiscoveryV5: true,
|
|
|
|
NoDiscovery: true,
|
|
|
|
BootstrapNodesV5: []*discv5.Node{bootnodeV5},
|
2018-04-25 07:13:59 +00:00
|
|
|
Protocols: whisper.Protocols(),
|
2018-04-10 06:44:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
s.NoError(peer.Start())
|
|
|
|
s.peers[i] = peer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) TearDown() {
|
|
|
|
s.bootnode.Stop()
|
|
|
|
for _, p := range s.peers {
|
|
|
|
p.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:08:49 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) getPeerFromEvent(events <-chan *p2p.PeerEvent, etype p2p.PeerEventType) (nodeID discover.NodeID) {
|
|
|
|
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-05-26 07:37:13 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) TestPeerPoolCache() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
topic := discv5.Topic("cap=test")
|
|
|
|
config := map[discv5.Topic]params.Limits{
|
|
|
|
topic: params.NewLimits(1, 1),
|
|
|
|
}
|
2018-06-06 13:39:27 +00:00
|
|
|
peerPoolOpts := &Options{100 * time.Millisecond, 100 * time.Millisecond, 0, true, 100 * time.Millisecond}
|
2018-05-26 07:37:13 +00:00
|
|
|
cache, err := newInMemoryCache()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
peerPool := NewPeerPool(config, cache, peerPoolOpts)
|
|
|
|
|
|
|
|
// start peer pool
|
|
|
|
s.Require().NoError(peerPool.Start(s.peers[1]))
|
|
|
|
defer peerPool.Stop()
|
|
|
|
|
|
|
|
// check if cache is passed to topic pools
|
|
|
|
for _, topicPool := range peerPool.topics {
|
|
|
|
s.Equal(cache, topicPool.cache)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:08:49 +00:00
|
|
|
func (s *PeerPoolSimulationSuite) TestSingleTopicDiscoveryWithFailover() {
|
2018-06-08 09:20:30 +00:00
|
|
|
s.T().Skip("Skipping due to being flaky")
|
|
|
|
|
2018-05-26 07:37:13 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// 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)
|
2018-04-25 07:13:59 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope struct {
|
|
|
|
Type string
|
|
|
|
Event json.RawMessage
|
|
|
|
}
|
|
|
|
s.NoError(json.Unmarshal([]byte(jsonEvent), &envelope))
|
2018-05-26 07:37:13 +00:00
|
|
|
|
|
|
|
switch typ := envelope.Type; typ {
|
|
|
|
case signal.EventDiscoveryStarted, signal.EventDiscoveryStopped:
|
2018-04-25 07:13:59 +00:00
|
|
|
poolEvents <- envelope.Type
|
2018-05-03 07:35:58 +00:00
|
|
|
case signal.EventDiscoverySummary:
|
2018-04-25 07:13:59 +00:00
|
|
|
poolEvents <- envelope.Type
|
2018-05-15 07:48:40 +00:00
|
|
|
var summary []*p2p.PeerInfo
|
2018-04-25 07:13:59 +00:00
|
|
|
s.NoError(json.Unmarshal(envelope.Event, &summary))
|
|
|
|
summaries <- summary
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 15:19:11 +00:00
|
|
|
defer signal.ResetDefaultNodeNotificationHandler()
|
2018-05-26 07:37:13 +00:00
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
topic := discv5.Topic("cap=test")
|
|
|
|
// simulation should only rely on fast sync
|
|
|
|
config := map[discv5.Topic]params.Limits{
|
2018-04-25 07:13:59 +00:00
|
|
|
topic: params.NewLimits(1, 1), // limits are chosen for simplicity of the simulation
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-06-06 13:39:27 +00:00
|
|
|
peerPoolOpts := &Options{100 * time.Millisecond, 100 * time.Millisecond, 0, true, 0}
|
2018-05-15 15:05:57 +00:00
|
|
|
cache, err := newInMemoryCache()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
peerPool := NewPeerPool(config, cache, peerPoolOpts)
|
2018-05-26 07:37:13 +00:00
|
|
|
|
|
|
|
// create and start topic registry
|
2018-04-10 06:44:09 +00:00
|
|
|
register := NewRegister(topic)
|
2018-05-26 07:37:13 +00:00
|
|
|
err = register.Start(s.peers[0])
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// subscribe for peer events before starting the peer pool
|
2018-04-10 06:44:09 +00:00
|
|
|
events := make(chan *p2p.PeerEvent, 20)
|
|
|
|
subscription := s.peers[1].SubscribeEvents(events)
|
|
|
|
defer subscription.Unsubscribe()
|
2018-05-26 07:37:13 +00:00
|
|
|
|
|
|
|
// start the peer pool
|
|
|
|
s.Require().NoError(peerPool.Start(s.peers[1]))
|
2018-04-10 06:44:09 +00:00
|
|
|
defer peerPool.Stop()
|
2018-05-03 07:35:58 +00:00
|
|
|
s.Equal(signal.EventDiscoveryStarted, s.getPoolEvent(poolEvents))
|
2018-05-29 11:24:23 +00:00
|
|
|
|
|
|
|
// wait for the peer to be found and connected
|
2018-05-26 07:37:13 +00:00
|
|
|
connectedPeer := s.getPeerFromEvent(events, p2p.PeerEventTypeAdd)
|
|
|
|
s.Equal(s.peers[0].Self().ID, connectedPeer)
|
|
|
|
// as the upper limit was reached, Discovery should be stoped
|
|
|
|
s.Equal(signal.EventDiscoverySummary, s.getPoolEvent(poolEvents))
|
2018-06-06 13:39:27 +00:00
|
|
|
s.Equal(signal.EventDiscoveryStopped, s.getPoolEvent(poolEvents))
|
2018-05-26 07:37:13 +00:00
|
|
|
s.Len(<-summaries, 1)
|
2018-04-25 07:13:59 +00:00
|
|
|
|
2018-05-26 07:37:13 +00:00
|
|
|
// stop topic register and the connected peer
|
2018-05-01 15:19:11 +00:00
|
|
|
register.Stop()
|
2018-04-12 13:08:49 +00:00
|
|
|
s.peers[0].Stop()
|
2018-05-26 07:37:13 +00:00
|
|
|
disconnectedPeer := s.getPeerFromEvent(events, p2p.PeerEventTypeDrop)
|
|
|
|
s.Equal(connectedPeer, disconnectedPeer)
|
|
|
|
s.Equal(signal.EventDiscoverySummary, s.getPoolEvent(poolEvents))
|
|
|
|
s.Len(<-summaries, 0)
|
|
|
|
// Discovery should be restarted because the number of peers dropped
|
|
|
|
// below the lower limit.
|
2018-05-03 07:35:58 +00:00
|
|
|
s.Equal(signal.EventDiscoveryStarted, s.getPoolEvent(poolEvents))
|
2018-05-26 07:37:13 +00:00
|
|
|
|
|
|
|
// register the second peer
|
|
|
|
err = register.Start(s.peers[2])
|
|
|
|
s.Require().NoError(err)
|
2018-04-12 13:08:49 +00:00
|
|
|
defer register.Stop()
|
2018-04-25 07:13:59 +00:00
|
|
|
s.Equal(s.peers[2].Self().ID, s.getPeerFromEvent(events, p2p.PeerEventTypeAdd))
|
2018-05-26 07:37:13 +00:00
|
|
|
// Discovery can be stopped again.
|
2018-05-03 07:35:58 +00:00
|
|
|
s.Require().Equal(signal.EventDiscoverySummary, s.getPoolEvent(poolEvents))
|
2018-06-06 13:39:27 +00:00
|
|
|
s.Equal(signal.EventDiscoveryStopped, s.getPoolEvent(poolEvents))
|
2018-05-26 07:37:13 +00:00
|
|
|
s.Len(<-summaries, 1)
|
2018-04-10 06:44:09 +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,
|
|
|
|
DiscoveryV5: true,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, peer.Start())
|
|
|
|
defer peer.Stop()
|
|
|
|
require.NotNil(t, peer.DiscV5)
|
2018-05-01 15:19:11 +00:00
|
|
|
|
2018-06-06 13:39:27 +00:00
|
|
|
poolOpts := &Options{DefaultFastSync, DefaultSlowSync, 0, true, 100 * time.Millisecond}
|
2018-05-15 09:16:25 +00:00
|
|
|
pool := NewPeerPool(nil, nil, poolOpts)
|
2018-05-01 15:19:11 +00:00
|
|
|
require.NoError(t, pool.Start(peer))
|
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-04-16 18:15:03 +00:00
|
|
|
require.Nil(t, peer.DiscV5)
|
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,
|
|
|
|
DiscoveryV5: true,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, server.Start())
|
|
|
|
defer server.Stop()
|
|
|
|
require.NotNil(t, server.DiscV5)
|
|
|
|
|
|
|
|
// start PeerPool
|
2018-06-06 13:39:27 +00:00
|
|
|
poolOpts := &Options{DefaultFastSync, DefaultSlowSync, time.Millisecond * 100, true, 100 * time.Millisecond}
|
2018-05-15 09:16:25 +00:00
|
|
|
pool := NewPeerPool(nil, nil, poolOpts)
|
2018-05-01 15:19:11 +00:00
|
|
|
require.NoError(t, pool.Start(server))
|
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")
|
|
|
|
}
|
|
|
|
require.Nil(t, server.DiscV5)
|
|
|
|
|
|
|
|
// 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-05-01 15:19:11 +00:00
|
|
|
require.NotNil(t, server.DiscV5)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
require.Nil(t, server.DiscV5)
|
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,
|
|
|
|
DiscoveryV5: true,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, server.Start())
|
|
|
|
defer server.Stop()
|
|
|
|
require.NotNil(t, server.DiscV5)
|
|
|
|
|
|
|
|
// start PeerPool
|
2018-06-06 13:39:27 +00:00
|
|
|
poolOpts := &Options{DefaultFastSync, DefaultSlowSync, time.Millisecond * 100, false, 100 * time.Millisecond}
|
2018-05-15 09:16:25 +00:00
|
|
|
pool := NewPeerPool(nil, nil, poolOpts)
|
|
|
|
require.NoError(t, pool.Start(server))
|
|
|
|
|
|
|
|
// wait 2x timeout duration
|
|
|
|
<-time.After(pool.opts.DiscServerTimeout * 2)
|
|
|
|
require.NotNil(t, server.DiscV5)
|
|
|
|
}
|