mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/network: fixed 0 depth bug and test
This commit is contained in:
parent
4c1d167b67
commit
db6eab7a53
2 changed files with 74 additions and 31 deletions
|
|
@ -160,12 +160,11 @@ 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.Over(), 255, func(p *Peer, po int) bool {
|
d.kad.EachConn(d.kad.BaseAddr(), 255, func(p *Peer, po int) bool {
|
||||||
if uint8(po) < msg.Depth {
|
if uint8(po) < msg.Depth {
|
||||||
return false
|
if !d.seen(p.BzzAddr) {
|
||||||
}
|
peers = append(peers, p.BzzAddr)
|
||||||
if !d.seen(p.BzzAddr) {
|
}
|
||||||
peers = append(peers, p.BzzAddr)
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,12 @@
|
||||||
package network
|
package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"bytes"
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
"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/enode"
|
|
||||||
"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"
|
||||||
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
||||||
|
|
@ -66,50 +66,73 @@ func TestDiscovery(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestSubpeersMsg tests that the correct set of peers is suggested
|
||||||
|
// to another peer with a given depth and kademlia
|
||||||
func TestSubpeersMsg(t *testing.T) {
|
func TestSubpeersMsg(t *testing.T) {
|
||||||
|
|
||||||
|
// This is the defined depth
|
||||||
testDepth := 2
|
testDepth := 2
|
||||||
|
|
||||||
|
// construct ProtocolTester and hive
|
||||||
params := NewHiveParams()
|
params := NewHiveParams()
|
||||||
key, err := crypto.GenerateKey()
|
s, hive := newHiveTester(t, params, 1, nil)
|
||||||
if err != nil {
|
|
||||||
panic("unable to generate key")
|
|
||||||
}
|
|
||||||
node := enode.NewV4(&key.PublicKey, net.IP{127, 0, 0, 1}, 30303, 30303)
|
|
||||||
addr := NewAddr(node)
|
|
||||||
kad := NewKademlia(addr.OAddr, NewKadParams())
|
|
||||||
hive := NewHive(params, kad, nil) // hive
|
|
||||||
s := newBzzBaseTester(t, 1, addr, DiscoverySpec, hive.Run)
|
|
||||||
pivot := s.Nodes[0]
|
|
||||||
|
|
||||||
|
// register some addresses in specific bins (must coincide with testDepth)
|
||||||
registerBzzAddr(0, hive, true) // bin 0
|
registerBzzAddr(0, hive, true) // bin 0
|
||||||
|
registerBzzAddr(0, hive, true) // bin 0
|
||||||
|
registerBzzAddr(1, hive, true) // bin 1
|
||||||
registerBzzAddr(1, hive, true) // bin 1
|
registerBzzAddr(1, hive, true) // bin 1
|
||||||
registerBzzAddr(3, hive, true) // bin 3
|
registerBzzAddr(3, hive, true) // bin 3
|
||||||
registerBzzAddr(4, hive, true) // bin 4
|
registerBzzAddr(4, hive, true) // bin 4
|
||||||
registerBzzAddr(3, hive, false) // add a known but not connected peer
|
registerBzzAddr(3, hive, false) // add a known but not connected peer
|
||||||
registerBzzAddr(1, hive, false) // add a known but not connected peer
|
registerBzzAddr(1, hive, false) // add a known but not connected peer
|
||||||
|
|
||||||
|
// start the hive
|
||||||
|
hive.Start(s.Server)
|
||||||
|
defer hive.Stop()
|
||||||
|
|
||||||
|
// the pivot node is the only one from the ProtocolTester
|
||||||
|
pivot := s.Nodes[0]
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// we need to wait until the pivot node is actually connected to our hive
|
||||||
|
WAIT_PIVOT:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Fatal("Timed out waiting for the pivot node to connect")
|
||||||
|
case <-time.After(100 * time.Millisecond):
|
||||||
|
if _, ok := hive.peers[pivot.ID()]; ok {
|
||||||
|
break WAIT_PIVOT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get BzzAddr of the pivot
|
||||||
|
pivotAddress := hive.peers[pivot.ID()]
|
||||||
|
pivotBzz := pivotAddress.BzzAddr.Over()
|
||||||
|
|
||||||
|
// now we need to identify which peers are expected
|
||||||
|
// 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(hive.BaseAddr(), 256, func(p *Peer, po int) bool {
|
||||||
if po < testDepth {
|
if po < testDepth {
|
||||||
expectedPeers = append(expectedPeers, p.BzzAddr)
|
// don't add the pivot node itself to expectedPeers;
|
||||||
|
// the pivot node was not added as
|
||||||
|
if !bytes.Equal(p.BzzAddr.Over(), b) {
|
||||||
|
expectedPeers = append(expectedPeers, p.BzzAddr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
// start the hive and wait for the connection
|
// the test exchange is as follows:
|
||||||
hive.Start(s.Server)
|
// 1. Trigger a subPeersMsg from pivot to our hive
|
||||||
defer hive.Stop()
|
// 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",
|
||||||
Expects: []p2ptest.Expect{
|
|
||||||
{
|
|
||||||
Code: 0,
|
|
||||||
Msg: &peersMsg{Peers: expectedPeers},
|
|
||||||
Peer: pivot.ID(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Triggers: []p2ptest.Trigger{
|
Triggers: []p2ptest.Trigger{
|
||||||
{
|
{
|
||||||
Code: 1,
|
Code: 1,
|
||||||
|
|
@ -117,6 +140,18 @@ func TestSubpeersMsg(t *testing.T) {
|
||||||
Peer: pivot.ID(),
|
Peer: pivot.ID(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Expects: []p2ptest.Expect{
|
||||||
|
{
|
||||||
|
Code: 1,
|
||||||
|
Msg: &subPeersMsg{Depth: uint8(testDepth)},
|
||||||
|
Peer: pivot.ID(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Code: 0,
|
||||||
|
Msg: &peersMsg{Peers: expectedPeers},
|
||||||
|
Peer: pivot.ID(),
|
||||||
|
},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -125,17 +160,23 @@ func TestSubpeersMsg(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add the BzzAddr to the hive
|
||||||
func registerBzzAddr(po int, hive *Hive, on bool) {
|
func registerBzzAddr(po int, hive *Hive, on bool) {
|
||||||
a := pot.RandomAddressAt(pot.NewAddressFromBytes(hive.BaseAddr()), po)
|
a := pot.RandomAddressAt(pot.NewAddressFromBytes(hive.BaseAddr()), po)
|
||||||
bzzAddr := &BzzAddr{OAddr: a.Bytes(), UAddr: a.Bytes()}
|
bzzAddr := &BzzAddr{OAddr: a.Bytes(), UAddr: a.Bytes()}
|
||||||
if on {
|
if on {
|
||||||
|
// actually connect
|
||||||
peer := newDiscPeer(bzzAddr, a.String(), hive)
|
peer := newDiscPeer(bzzAddr, a.String(), hive)
|
||||||
hive.On(peer)
|
hive.On(peer)
|
||||||
} else {
|
} else {
|
||||||
|
// only add to address book
|
||||||
hive.Register(bzzAddr)
|
hive.Register(bzzAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// as we are not creating a real node via the protocol,
|
||||||
|
// we need to create the discovery peer objects for the additional kademlia
|
||||||
|
// nodes manually
|
||||||
func newDiscPeer(bzzAddr *BzzAddr, name string, hive *Hive) *Peer {
|
func newDiscPeer(bzzAddr *BzzAddr, name string, hive *Hive) *Peer {
|
||||||
p2pPeer := p2p.NewPeer(adapters.RandomNodeConfig().Node().ID(), name, nil)
|
p2pPeer := p2p.NewPeer(adapters.RandomNodeConfig().Node().ID(), name, nil)
|
||||||
peer := NewPeer(&BzzPeer{
|
peer := NewPeer(&BzzPeer{
|
||||||
|
|
@ -146,6 +187,9 @@ func newDiscPeer(bzzAddr *BzzAddr, name string, hive *Hive) *Peer {
|
||||||
return peer
|
return peer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we also need this dummy object otherwise at hive.Stop(),
|
||||||
|
// which will call `Drop` on all nodes, we will have null pointer errors,
|
||||||
|
// as the underlying `p2p.Peer` objects were not created
|
||||||
type dummyMsgRW struct{}
|
type dummyMsgRW struct{}
|
||||||
|
|
||||||
func (d *dummyMsgRW) ReadMsg() (p2p.Msg, error) {
|
func (d *dummyMsgRW) ReadMsg() (p2p.Msg, error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue