2018-04-10 06:44:09 +00:00
|
|
|
package peers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-15 15:05:57 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeersRange(t *testing.T) {
|
2018-05-15 15:05:57 +00:00
|
|
|
peersDB, err := newInMemoryCache()
|
2018-04-10 06:44:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
topic := discv5.Topic("test")
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
peerCount := 3
|
|
|
|
peers, err := createDiscv5Peers(peerCount)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
addedPeerIDs := make(map[string]struct{})
|
2018-04-10 06:44:09 +00:00
|
|
|
for _, peer := range peers {
|
|
|
|
assert.NoError(t, peersDB.AddPeer(peer, topic))
|
2018-11-14 07:03:58 +00:00
|
|
|
addedPeerIDs[peer.String()] = struct{}{}
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
// check if all peers are cached
|
|
|
|
cachedNodes := peersDB.GetPeersRange(topic, peerCount)
|
|
|
|
require.Len(t, cachedNodes, peerCount)
|
|
|
|
// and returned peers are these we just created
|
|
|
|
for _, node := range cachedNodes {
|
|
|
|
_, ok := addedPeerIDs[node.String()]
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test removing peers
|
|
|
|
pk, err := peers[0].ID.Pubkey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, peersDB.RemovePeer(enode.PubkeyToIDV4(pk), topic))
|
|
|
|
cachedNodes = peersDB.GetPeersRange(topic, peerCount)
|
|
|
|
require.Len(t, cachedNodes, peerCount-1)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-07-11 06:49:41 +00:00
|
|
|
|
|
|
|
func TestMultipleTopics(t *testing.T) {
|
|
|
|
peersDB, err := newInMemoryCache()
|
|
|
|
require.NoError(t, err)
|
|
|
|
topics := []discv5.Topic{discv5.Topic("first"), discv5.Topic("second")}
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
peerCount := 3
|
|
|
|
peers, err := createDiscv5Peers(peerCount)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-07-11 06:49:41 +00:00
|
|
|
for i := range topics {
|
|
|
|
for _, peer := range peers {
|
|
|
|
assert.NoError(t, peersDB.AddPeer(peer, topics[i]))
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
|
2018-07-11 06:49:41 +00:00
|
|
|
for i := range topics {
|
2018-11-14 07:03:58 +00:00
|
|
|
nodes := peersDB.GetPeersRange(topics[i], peerCount+1)
|
|
|
|
assert.Len(t, nodes, peerCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newInMemoryCache creates a cache for tests
|
|
|
|
func newInMemoryCache() (*Cache, error) {
|
|
|
|
memdb, err := leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewCache(memdb), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDiscv5Peers(count int) ([]*discv5.Node, error) {
|
|
|
|
nodes := make([]*discv5.Node, count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
id, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-07-11 06:49:41 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
nodes[i] = discv5.NewNode(
|
|
|
|
discv5.PubkeyID(&id.PublicKey),
|
|
|
|
net.IPv4(10, 10, 0, byte(i)),
|
|
|
|
32311,
|
|
|
|
32311,
|
|
|
|
)
|
2018-07-11 06:49:41 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
return nodes, nil
|
2018-07-11 06:49:41 +00:00
|
|
|
}
|