swarm/network: correct TestSubpeersMsg

This commit is contained in:
Fabio Barone 2019-03-21 12:03:33 -05:00
parent a328917839
commit 7b6422b4a7
2 changed files with 56 additions and 36 deletions

View file

@ -160,11 +160,12 @@ func (d *Peer) handleSubPeersMsg(msg *subPeersMsg) error {
if !d.sentPeers { if !d.sentPeers {
d.setDepth(msg.Depth) d.setDepth(msg.Depth)
var peers []*BzzAddr var peers []*BzzAddr
d.kad.EachConn(d.kad.BaseAddr(), 255, func(p *Peer, po int) bool { d.kad.EachConn(d.Over(), 255, func(p *Peer, po int) bool {
if uint8(po) < msg.Depth { if uint8(po) < msg.Depth {
if !d.seen(p.BzzAddr) { return false
peers = append(peers, p.BzzAddr) }
} if !d.seen(p.BzzAddr) {
peers = append(peers, p.BzzAddr)
} }
return true return true
}) })

View file

@ -19,9 +19,11 @@ package network
import ( import (
"bytes" "bytes"
"context" "context"
"math/rand"
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/protocols" "github.com/ethereum/go-ethereum/p2p/protocols"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
@ -71,11 +73,20 @@ func TestDiscovery(t *testing.T) {
func TestSubpeersMsg(t *testing.T) { func TestSubpeersMsg(t *testing.T) {
// This is the defined depth // This is the defined depth
testDepth := 2 testDepth := rand.Intn(4) + 1
// construct ProtocolTester and hive // construct ProtocolTester and hive
params := NewHiveParams() params := NewHiveParams()
s, hive, err := newHiveTester(t, params, 1, nil) // setup
prvkey, err := crypto.GenerateKey()
if err != nil {
t.Fatal(err)
}
addr := PrivateKeyToBzzKey(prvkey)
to := NewKademlia(addr, NewKadParams())
hive := NewHive(params, to, nil) // hive
s, err := newBzzBaseTester(t, 1, prvkey, DiscoverySpec, hive.Run)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -94,68 +105,76 @@ func TestSubpeersMsg(t *testing.T) {
hive.Start(s.Server) hive.Start(s.Server)
defer hive.Stop() defer hive.Stop()
// the pivot node is the only one from the ProtocolTester // the remote node is the only one from the ProtocolTester
pivot := s.Nodes[0] remote := s.Nodes[0]
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
// we need to wait until the pivot node is actually connected to our hive // we need to wait until the remote node is actually connected to our hive
WAIT_PIVOT: WAIT_PIVOT:
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.Fatal("Timed out waiting for the pivot node to connect") t.Fatal("Timed out waiting for the remote node to connect")
case <-time.After(100 * time.Millisecond): case <-time.After(100 * time.Millisecond):
if _, ok := hive.peers[pivot.ID()]; ok { if _, ok := hive.peers[remote.ID()]; ok {
break WAIT_PIVOT break WAIT_PIVOT
} }
} }
} }
// get BzzAddr of the pivot // get BzzAddr of the remote
pivotAddress := hive.peers[pivot.ID()] remoteAddress := hive.peers[remote.ID()]
pivotBzz := pivotAddress.BzzAddr.Over() remoteBzz := remoteAddress.BzzAddr.Over()
// now we need to identify which peers are expected // now we need to identify which peers are expected
// iterate the hive's connection and only add peers below testDepth // iterate the hive's connection and only add peers below testDepth
var expectedPeers []*BzzAddr var expectedPeers []*BzzAddr
hive.EachConn(hive.BaseAddr(), 256, func(p *Peer, po int) bool { hive.EachConn(remoteBzz, 255, func(p *Peer, po int) bool {
if po < testDepth { if po < testDepth {
// don't add the pivot node itself to expectedPeers; return false
// the pivot node was not added as }
if !bytes.Equal(p.BzzAddr.Over(), pivotBzz) { // don't add the remote node itself to expectedPeers;
expectedPeers = append(expectedPeers, p.BzzAddr) // the remote node was not added as
} if !bytes.Equal(p.BzzAddr.Over(), remoteBzz) {
expectedPeers = append(expectedPeers, p.BzzAddr)
} }
return true return true
}) })
// the test exchange is as follows: // the test exchange is as follows:
// 1. Trigger a subPeersMsg from pivot to our hive // 1. Trigger a subPeersMsg from remote to our hive
// 2. Hive will respond with peersMsg with the set of expected peers // 2. Hive will respond with peersMsg with the set of expected peers
err = s.TestExchanges(p2ptest.Exchange{ err = s.TestExchanges(p2ptest.Exchange{
Label: "incoming subPeersMsg", Label: "incoming subPeersMsg",
Triggers: []p2ptest.Trigger{
{
Code: 1,
Msg: &subPeersMsg{Depth: uint8(testDepth)},
Peer: pivot.ID(),
},
},
Expects: []p2ptest.Expect{ Expects: []p2ptest.Expect{
{ {
Code: 1, Code: 1,
Msg: &subPeersMsg{Depth: uint8(testDepth)}, Msg: &subPeersMsg{Depth: uint8(2)},
Peer: pivot.ID(), Peer: remote.ID(),
},
{
Code: 0,
Msg: &peersMsg{Peers: expectedPeers},
Peer: pivot.ID(),
}, },
}, },
}) },
p2ptest.Exchange{
Label: "trigger subPeers and receive peersMsg",
Triggers: []p2ptest.Trigger{
{
Code: 1,
Msg: &subPeersMsg{Depth: uint8(testDepth)},
Peer: remote.ID(),
Timeout: 3 * time.Second,
},
},
Expects: []p2ptest.Expect{
{
Code: 0,
Msg: &peersMsg{Peers: expectedPeers},
Peer: remote.ID(),
Timeout: 3 * time.Second,
},
},
})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)