2018-04-10 06:44:09 +00:00
|
|
|
package peers
|
|
|
|
|
|
|
|
import (
|
2018-11-14 07:03:58 +00:00
|
|
|
"net"
|
2018-04-10 06:44:09 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2018-04-10 06:44:09 +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"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TopicPoolSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
peer *p2p.Server
|
|
|
|
topicPool *TopicPool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopicPoolSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(TopicPoolSuite))
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (s *TopicPoolSuite) createDiscV5Node(ip net.IP, port uint16) (enode.ID, *discv5.Node) {
|
|
|
|
id, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
nodeID := enode.PubkeyToIDV4(&id.PublicKey)
|
|
|
|
nodeV5 := discv5.NewNode(discv5.PubkeyID(&id.PublicKey), ip, port, port)
|
|
|
|
return nodeID, nodeV5
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
func (s *TopicPoolSuite) SetupTest() {
|
2018-06-06 13:39:27 +00:00
|
|
|
maxCachedPeersMultiplier = 1
|
2018-04-10 06:44:09 +00:00
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
name := common.MakeName("peer", "1.0")
|
|
|
|
s.peer = &p2p.Server{
|
|
|
|
Config: p2p.Config{
|
|
|
|
MaxPeers: 10,
|
|
|
|
Name: name,
|
|
|
|
ListenAddr: "0.0.0.0:0",
|
|
|
|
PrivateKey: key,
|
|
|
|
NoDiscovery: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.Require().NoError(s.peer.Start())
|
|
|
|
topic := discv5.Topic("cap=cap1")
|
2018-04-20 07:39:31 +00:00
|
|
|
limits := params.NewLimits(1, 2)
|
2018-05-15 15:05:57 +00:00
|
|
|
cache, err := newInMemoryCache()
|
|
|
|
s.Require().NoError(err)
|
2018-07-04 10:51:47 +00:00
|
|
|
s.topicPool = newTopicPool(nil, topic, limits, 100*time.Millisecond, 200*time.Millisecond, cache)
|
2018-04-10 06:44:09 +00:00
|
|
|
s.topicPool.running = 1
|
2018-05-01 15:19:11 +00:00
|
|
|
// This is a buffered channel to simplify testing.
|
|
|
|
// If your test generates more than 10 mode changes,
|
|
|
|
// override this `period` field or consume from it
|
|
|
|
// using `AssertConsumed()`.
|
|
|
|
s.topicPool.period = make(chan time.Duration, 10)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TearDown() {
|
|
|
|
s.peer.Stop()
|
|
|
|
}
|
|
|
|
|
2018-05-01 15:19:11 +00:00
|
|
|
func (s *TopicPoolSuite) AssertConsumed(channel <-chan time.Duration, expected time.Duration, timeout time.Duration) {
|
2018-04-10 06:44:09 +00:00
|
|
|
select {
|
|
|
|
case received := <-channel:
|
|
|
|
s.Equal(expected, received)
|
|
|
|
case <-time.After(timeout):
|
|
|
|
s.FailNow("timed out waiting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 07:37:13 +00:00
|
|
|
func (s *TopicPoolSuite) TestUsingCache() {
|
|
|
|
s.topicPool.limits = params.NewLimits(1, 1)
|
2018-06-06 13:39:27 +00:00
|
|
|
s.topicPool.maxCachedPeers = 1
|
2018-05-26 07:37:13 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
|
|
|
|
|
|
|
cached := s.topicPool.cache.GetPeersRange(s.topicPool.topic, 10)
|
|
|
|
s.Contains(cached, peer1)
|
|
|
|
s.Contains(cached, peer2)
|
|
|
|
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID2)
|
|
|
|
cached = s.topicPool.cache.GetPeersRange(s.topicPool.topic, 10)
|
|
|
|
s.Contains(cached, peer1)
|
|
|
|
s.Contains(cached, peer2)
|
2018-05-26 07:37:13 +00:00
|
|
|
|
|
|
|
// A peer that drops by itself, should be removed from the cache.
|
2018-11-14 07:03:58 +00:00
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID1)
|
2018-05-26 07:37:13 +00:00
|
|
|
s.Equal([]*discv5.Node{peer2}, s.topicPool.cache.GetPeersRange(s.topicPool.topic, 10))
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
func (s *TopicPoolSuite) TestSyncSwitches() {
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID, peer := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer))
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID)
|
2018-05-01 15:19:11 +00:00
|
|
|
s.AssertConsumed(s.topicPool.period, s.topicPool.slowMode, time.Second)
|
2018-11-14 07:03:58 +00:00
|
|
|
s.NotNil(s.topicPool.connectedPeers[nodeID])
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID)
|
2018-05-01 15:19:11 +00:00
|
|
|
s.AssertConsumed(s.topicPool.period, s.topicPool.fastMode, time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestTimeoutFastMode() {
|
|
|
|
s.topicPool.fastModeTimeout = time.Millisecond * 50
|
|
|
|
|
|
|
|
// set fast mode
|
|
|
|
s.topicPool.mu.Lock()
|
|
|
|
s.topicPool.setSyncMode(s.topicPool.fastMode)
|
|
|
|
s.topicPool.mu.Unlock()
|
|
|
|
s.Equal(s.topicPool.fastMode, <-s.topicPool.period)
|
|
|
|
|
|
|
|
// switch to slow mode after `fastModeTimeout`
|
|
|
|
select {
|
|
|
|
case mode := <-s.topicPool.period:
|
|
|
|
s.Equal(s.topicPool.slowMode, mode)
|
|
|
|
case <-time.After(s.topicPool.fastModeTimeout * 2):
|
|
|
|
s.FailNow("timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestSetSyncMode() {
|
|
|
|
s.topicPool.fastModeTimeout = 0
|
|
|
|
|
|
|
|
// set fast mode
|
|
|
|
s.topicPool.setSyncMode(s.topicPool.fastMode)
|
|
|
|
s.Equal(s.topicPool.fastMode, <-s.topicPool.period)
|
|
|
|
s.Equal(s.topicPool.fastMode, s.topicPool.currentMode)
|
|
|
|
|
|
|
|
// skip setting the same mode
|
|
|
|
s.topicPool.setSyncMode(s.topicPool.fastMode)
|
|
|
|
select {
|
|
|
|
case <-s.topicPool.period:
|
|
|
|
s.FailNow("should not have update the mode")
|
|
|
|
default:
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
|
|
|
|
// switch to slow mode
|
|
|
|
cancel := make(chan struct{})
|
|
|
|
s.topicPool.fastModeTimeoutCancel = cancel // should be set to nil
|
|
|
|
s.topicPool.setSyncMode(s.topicPool.slowMode)
|
|
|
|
s.Equal(s.topicPool.slowMode, <-s.topicPool.period)
|
|
|
|
s.Equal(s.topicPool.slowMode, s.topicPool.currentMode)
|
|
|
|
select {
|
|
|
|
case <-cancel:
|
|
|
|
s.Nil(s.topicPool.fastModeTimeoutCancel)
|
|
|
|
default:
|
|
|
|
s.FailNow("cancel should be closed")
|
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestNewPeerSelectedOnDrop() {
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID3, peer3 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
// add 3 nodes and confirm connection for 1 and 2
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer3))
|
2018-05-22 13:11:21 +00:00
|
|
|
s.Len(s.topicPool.pendingPeers, 3)
|
|
|
|
s.Len(s.topicPool.discoveredPeersQueue, 0)
|
2018-11-14 07:03:58 +00:00
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.Contains(s.topicPool.connectedPeers, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
|
|
|
s.Contains(s.topicPool.connectedPeers, nodeID2)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID3)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID3)
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID3)
|
2018-05-22 13:11:21 +00:00
|
|
|
s.Len(s.topicPool.pendingPeers, 1)
|
|
|
|
s.Len(s.topicPool.discoveredPeersQueue, 1)
|
2018-04-19 15:18:49 +00:00
|
|
|
// drop peer1
|
2018-11-14 07:03:58 +00:00
|
|
|
s.True(s.topicPool.ConfirmDropped(s.peer, nodeID1))
|
|
|
|
s.NotContains(s.topicPool.connectedPeers, nodeID1)
|
2018-04-19 15:18:49 +00:00
|
|
|
// add peer from the pool
|
2018-04-12 13:08:49 +00:00
|
|
|
s.Equal(peer3.ID, s.topicPool.AddPeerFromTable(s.peer).ID)
|
2018-05-22 13:11:21 +00:00
|
|
|
s.Len(s.topicPool.pendingPeers, 1)
|
|
|
|
s.Len(s.topicPool.discoveredPeersQueue, 0)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 15:14:05 +00:00
|
|
|
type mockConstantClock struct{}
|
|
|
|
|
|
|
|
func (mockConstantClock) Now() time.Time { return time.Unix(0, 0) }
|
|
|
|
|
|
|
|
// Regression test for https://github.com/status-im/nim-status-client/issues/522
|
|
|
|
func (s *TopicPoolSuite) TestNewPeerLackOfClockPrecision() {
|
|
|
|
_, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
_, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
_, peer3 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
|
|
|
|
s.topicPool.maxPendingPeers = 2
|
|
|
|
|
|
|
|
s.topicPool.clock = mockConstantClock{}
|
|
|
|
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer3))
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
func (s *TopicPoolSuite) TestRequestedDoesntRemove() {
|
2018-04-12 13:08:49 +00:00
|
|
|
// max limit is 1 because we test that 2nd peer will stay in local table
|
|
|
|
// when we request to drop it
|
2018-04-20 07:39:31 +00:00
|
|
|
s.topicPool.limits = params.NewLimits(1, 1)
|
2018-06-06 13:39:27 +00:00
|
|
|
s.topicPool.maxCachedPeers = 1
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
|
|
|
s.False(s.topicPool.connectedPeers[nodeID1].dismissed)
|
|
|
|
s.True(s.topicPool.connectedPeers[nodeID2].dismissed)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID2)
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID2)
|
|
|
|
s.NotContains(s.topicPool.connectedPeers, nodeID2)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID1)
|
|
|
|
s.NotContains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.NotContains(s.topicPool.connectedPeers, nodeID1)
|
2018-04-19 15:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestTheMostRecentPeerIsSelected() {
|
2018-04-20 07:39:31 +00:00
|
|
|
s.topicPool.limits = params.NewLimits(1, 1)
|
2018-06-06 13:39:27 +00:00
|
|
|
s.topicPool.maxCachedPeers = 1
|
2018-04-19 15:18:49 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID3, peer3 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
2018-04-19 15:18:49 +00:00
|
|
|
|
|
|
|
// after these operations, peer1 is confirmed and peer3 and peer2
|
|
|
|
// was added to the pool; peer3 is the most recent one
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer3))
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID3)
|
|
|
|
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID2)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID3)
|
2018-04-19 15:18:49 +00:00
|
|
|
// peer1 has dropped
|
2018-11-14 07:03:58 +00:00
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID1)
|
2018-04-19 15:18:49 +00:00
|
|
|
// and peer3 is take from the pool as the most recent
|
2020-07-24 15:14:05 +00:00
|
|
|
s.True(s.topicPool.pendingPeers[nodeID2].discoveredTime.Before(s.topicPool.pendingPeers[nodeID3].discoveredTime))
|
2018-04-19 15:18:49 +00:00
|
|
|
s.Equal(peer3.ID, s.topicPool.AddPeerFromTable(s.peer).ID)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-05-22 13:11:21 +00:00
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestSelectPeerAfterMaxLimit() {
|
|
|
|
s.topicPool.limits = params.NewLimits(1, 1)
|
2018-06-06 13:39:27 +00:00
|
|
|
s.topicPool.maxCachedPeers = 1
|
2018-05-22 13:11:21 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID3, peer3 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
2018-05-22 13:11:21 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID2)
|
2018-05-22 13:11:21 +00:00
|
|
|
s.Len(s.topicPool.pendingPeers, 1)
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID2)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer3))
|
2018-05-22 13:11:21 +00:00
|
|
|
s.Len(s.topicPool.pendingPeers, 2)
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID3)
|
2018-05-22 13:11:21 +00:00
|
|
|
s.Equal(peer3, s.topicPool.AddPeerFromTable(s.peer))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestReplacementPeerIsCounted() {
|
|
|
|
s.topicPool.limits = params.NewLimits(1, 1)
|
2018-06-06 13:39:27 +00:00
|
|
|
s.topicPool.maxCachedPeers = 1
|
2018-05-22 13:11:21 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID2)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID1)
|
|
|
|
|
|
|
|
s.NotContains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.NotContains(s.topicPool.connectedPeers, nodeID1)
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID2)
|
|
|
|
s.topicPool.pendingPeers[nodeID2].added = true
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
2018-05-22 13:11:21 +00:00
|
|
|
s.True(s.topicPool.MaxReached())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestPeerDontAddTwice() {
|
|
|
|
s.topicPool.limits = params.NewLimits(1, 1)
|
2018-06-06 13:39:27 +00:00
|
|
|
s.topicPool.maxCachedPeers = 1
|
2018-05-22 13:11:21 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
_, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
2018-05-22 13:11:21 +00:00
|
|
|
// peer2 already added to p2p server no reason to add it again
|
|
|
|
s.Nil(s.topicPool.AddPeerFromTable(s.peer))
|
|
|
|
}
|
2018-06-06 13:39:27 +00:00
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestMaxCachedPeers() {
|
|
|
|
s.topicPool.limits = params.NewLimits(1, 1)
|
|
|
|
s.topicPool.maxCachedPeers = 3
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID3, peer3 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer2))
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer3))
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID2)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID3)
|
2018-06-06 13:39:27 +00:00
|
|
|
|
|
|
|
s.Equal(3, len(s.topicPool.connectedPeers))
|
2018-11-14 07:03:58 +00:00
|
|
|
s.False(s.topicPool.connectedPeers[nodeID1].dismissed)
|
|
|
|
s.True(s.topicPool.connectedPeers[nodeID2].dismissed)
|
|
|
|
s.True(s.topicPool.connectedPeers[nodeID3].dismissed)
|
2018-06-06 13:39:27 +00:00
|
|
|
|
|
|
|
cached := s.topicPool.cache.GetPeersRange(s.topicPool.topic, 5)
|
|
|
|
s.Equal(3, len(cached))
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
cachedMap := make(map[discv5.NodeID]*discv5.Node)
|
|
|
|
for _, peer := range cached {
|
|
|
|
cachedMap[peer.ID] = peer
|
|
|
|
}
|
|
|
|
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID2)
|
|
|
|
s.topicPool.ConfirmDropped(s.peer, nodeID3)
|
|
|
|
|
|
|
|
s.Contains(cachedMap, peer1.ID)
|
|
|
|
s.Contains(cachedMap, peer2.ID)
|
|
|
|
s.Contains(cachedMap, peer3.ID)
|
2018-06-06 13:39:27 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
s.Contains(s.topicPool.connectedPeers, nodeID1)
|
|
|
|
s.NotContains(s.topicPool.connectedPeers, nodeID2)
|
|
|
|
s.NotContains(s.topicPool.connectedPeers, nodeID3)
|
|
|
|
|
|
|
|
s.NotContains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID2)
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID3)
|
2018-06-06 13:39:27 +00:00
|
|
|
|
|
|
|
s.True(s.topicPool.maxCachedPeersReached())
|
|
|
|
cached = s.topicPool.cache.GetPeersRange(s.topicPool.topic, 5)
|
|
|
|
s.Equal(3, len(cached))
|
|
|
|
}
|
2018-07-16 07:40:40 +00:00
|
|
|
|
2019-12-10 15:20:22 +00:00
|
|
|
func (s *TopicPoolSuite) TestMaxPendingPeers() {
|
|
|
|
s.topicPool.maxPendingPeers = 2
|
|
|
|
|
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID2, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
nodeID3, peer3 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
pk1, _ := peer1.ID.Pubkey()
|
|
|
|
pk2, _ := peer2.ID.Pubkey()
|
|
|
|
pk3, _ := peer3.ID.Pubkey()
|
|
|
|
|
2020-07-24 15:14:05 +00:00
|
|
|
s.topicPool.addToPendingPeers(&peerInfo{discoveredTime: time.Now(), node: peer1, publicKey: pk1})
|
|
|
|
s.topicPool.addToPendingPeers(&peerInfo{discoveredTime: time.Now(), node: peer2, publicKey: pk2})
|
|
|
|
s.topicPool.addToPendingPeers(&peerInfo{discoveredTime: time.Now(), node: peer3, publicKey: pk3})
|
2019-12-10 15:20:22 +00:00
|
|
|
|
|
|
|
s.Equal(2, len(s.topicPool.pendingPeers))
|
|
|
|
s.Require().NotContains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.Require().Contains(s.topicPool.pendingPeers, nodeID2)
|
|
|
|
s.Require().Contains(s.topicPool.pendingPeers, nodeID3)
|
|
|
|
|
|
|
|
// maxPendingPeers = 0 means no limits.
|
|
|
|
s.topicPool.maxPendingPeers = 0
|
2020-07-24 15:14:05 +00:00
|
|
|
s.topicPool.addToPendingPeers(&peerInfo{discoveredTime: time.Now(), node: peer1, publicKey: pk1})
|
2019-12-10 15:20:22 +00:00
|
|
|
|
|
|
|
s.Equal(3, len(s.topicPool.pendingPeers))
|
|
|
|
s.Require().Contains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.Require().Contains(s.topicPool.pendingPeers, nodeID2)
|
|
|
|
s.Require().Contains(s.topicPool.pendingPeers, nodeID3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestQueueDuplicatePeers() {
|
|
|
|
_, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
_, peer2 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
pk1, _ := peer1.ID.Pubkey()
|
|
|
|
pk2, _ := peer2.ID.Pubkey()
|
2020-07-24 15:14:05 +00:00
|
|
|
peerInfo1 := &peerInfo{discoveredTime: time.Now(), node: peer1, publicKey: pk1}
|
|
|
|
peerInfo2 := &peerInfo{discoveredTime: time.Now(), node: peer2, publicKey: pk2}
|
2019-12-10 15:20:22 +00:00
|
|
|
|
2020-07-24 15:14:05 +00:00
|
|
|
s.topicPool.addToPendingPeers(&peerInfo{discoveredTime: time.Now(), node: peer1, publicKey: pk1})
|
|
|
|
s.topicPool.addToPendingPeers(&peerInfo{discoveredTime: time.Now(), node: peer2, publicKey: pk2})
|
2019-12-10 15:20:22 +00:00
|
|
|
s.topicPool.addToQueue(peerInfo1)
|
|
|
|
s.topicPool.addToQueue(peerInfo2)
|
|
|
|
|
|
|
|
s.Equal(2, len(s.topicPool.discoveredPeersQueue))
|
|
|
|
s.Equal(2, len(s.topicPool.discoveredPeers))
|
|
|
|
|
|
|
|
s.topicPool.addToQueue(peerInfo1)
|
|
|
|
|
|
|
|
s.Equal(2, len(s.topicPool.discoveredPeersQueue))
|
|
|
|
s.Equal(2, len(s.topicPool.discoveredPeers))
|
|
|
|
|
|
|
|
peer := s.topicPool.popFromQueue()
|
|
|
|
|
|
|
|
s.Equal(1, len(s.topicPool.discoveredPeersQueue))
|
|
|
|
s.Equal(1, len(s.topicPool.discoveredPeers))
|
|
|
|
s.Require().NotContains(s.topicPool.discoveredPeers, peer.NodeID())
|
|
|
|
}
|
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
func (s *TopicPoolSuite) TestNewTopicPoolInterface() {
|
|
|
|
limits := params.NewLimits(1, 2)
|
|
|
|
cache, err := newInMemoryCache()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
topic := discv5.Topic("cap=cap1")
|
2018-07-04 10:51:47 +00:00
|
|
|
t := newTopicPool(nil, topic, limits, 100*time.Millisecond, 200*time.Millisecond, cache)
|
2018-07-16 07:40:40 +00:00
|
|
|
s.IsType(&TopicPool{}, t)
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
tp := newTopicPool(nil, MailServerDiscoveryTopic, limits, 100*time.Millisecond, 200*time.Millisecond, cache)
|
2018-07-25 14:48:02 +00:00
|
|
|
cacheTP := newCacheOnlyTopicPool(tp, &testTrueVerifier{})
|
2018-07-16 07:40:40 +00:00
|
|
|
s.IsType(&cacheOnlyTopicPool{}, cacheTP)
|
|
|
|
}
|
2018-11-13 13:58:26 +00:00
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestIgnoreInboundConnection() {
|
|
|
|
s.topicPool.limits = params.NewLimits(0, 0)
|
|
|
|
s.topicPool.maxCachedPeers = 0
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.False(s.topicPool.pendingPeers[nodeID1].dismissed)
|
|
|
|
s.NotContains(s.topicPool.connectedPeers, nodeID1)
|
2018-11-13 13:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TopicPoolSuite) TestConnectedButRemoved() {
|
|
|
|
s.topicPool.limits = params.NewLimits(0, 0)
|
|
|
|
s.topicPool.maxCachedPeers = 1
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
nodeID1, peer1 := s.createDiscV5Node(s.peer.Self().IP(), 32311)
|
|
|
|
s.Require().NoError(s.topicPool.processFoundNode(s.peer, peer1))
|
|
|
|
s.Contains(s.topicPool.pendingPeers, nodeID1)
|
|
|
|
s.topicPool.ConfirmAdded(s.peer, nodeID1)
|
|
|
|
s.Contains(s.topicPool.connectedPeers, nodeID1)
|
|
|
|
s.False(s.topicPool.ConfirmDropped(s.peer, nodeID1))
|
|
|
|
s.False(s.topicPool.pendingPeers[nodeID1].added)
|
2018-11-13 13:58:26 +00:00
|
|
|
}
|