mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
subpackages: * adapters: * msgpipes for simulated test connections * rlpx the RLPx adapter for normal non-test use * inproc simulated in-process network adapter * docker placeholder for docker cluster remote adapter * protocols: easy-to-setup modular protocols * simulations: * generic network model * journal, events, snapshots * cytoscape visualisation plugin * resourceful controller suite + REST API server * example: connectivity UX backend * testing: test resource drivers * exchange: trigger/expect style driver for single node and its peers * sessions: for unit testing protocols and protocol modules * (network: for network testing, benchmarking, stats, correctness, fault tolerance) * test peerpool see more in the README-s in each subpackage * p2p/server : conn/disconn hooks * reporter remote client skeleton
247 lines
5.9 KiB
Go
247 lines
5.9 KiB
Go
package network
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
"github.com/ethereum/go-ethereum/p2p/adapters"
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
"github.com/ethereum/go-ethereum/p2p/protocols"
|
|
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
|
)
|
|
|
|
func bzzHandshakeExchange(lhs, rhs *bzzHandshake, id *adapters.NodeId) []p2ptest.Exchange {
|
|
|
|
return []p2ptest.Exchange{
|
|
p2ptest.Exchange{
|
|
Expects: []p2ptest.Expect{
|
|
p2ptest.Expect{
|
|
Code: 0,
|
|
Msg: lhs,
|
|
Peer: id,
|
|
},
|
|
},
|
|
},
|
|
p2ptest.Exchange{
|
|
Triggers: []p2ptest.Trigger{
|
|
p2ptest.Trigger{
|
|
Code: 0,
|
|
Msg: rhs,
|
|
Peer: id,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func newTestBzzProtocol(addr *peerAddr, pp PeerPool, ct *protocols.CodeMap, services func(Node) error) func(adapters.NodeAdapter) adapters.ProtoCall {
|
|
if ct == nil {
|
|
ct = BzzCodeMap()
|
|
}
|
|
ct.Register(p2ptest.FlushMsg)
|
|
return func(na adapters.NodeAdapter) adapters.ProtoCall {
|
|
srv := func(p Node) error {
|
|
if services != nil {
|
|
err := services(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
id := p.ID()
|
|
p.Register(p2ptest.FlushMsg, func(interface{}) error {
|
|
flushc := na.(p2ptest.TestNetAdapter).GetPeer(&adapters.NodeId{id}).Flushc
|
|
flushc <- true
|
|
return nil
|
|
})
|
|
return nil
|
|
}
|
|
|
|
protocol := Bzz(addr.OverlayAddr(), pp, na, na.Messenger(), ct, srv)
|
|
return protocol.Run
|
|
}
|
|
}
|
|
|
|
type bzzTester struct {
|
|
*p2ptest.ExchangeSession
|
|
flushCode int
|
|
addr *peerAddr
|
|
}
|
|
|
|
// should test handshakes in one exchange? parallelisation
|
|
func (s *bzzTester) testHandshake(lhs, rhs *bzzHandshake, disconnects ...*p2ptest.Disconnect) {
|
|
var peers []*adapters.NodeId
|
|
id := NodeId(rhs.Addr)
|
|
if len(disconnects) > 0 {
|
|
for _, d := range disconnects {
|
|
peers = append(peers, d.Peer)
|
|
}
|
|
} else {
|
|
peers = []*adapters.NodeId{id}
|
|
}
|
|
s.TestConnected(false, peers...)
|
|
s.TestExchanges(bzzHandshakeExchange(lhs, rhs, id)...)
|
|
s.TestDisconnected(disconnects...)
|
|
}
|
|
|
|
func (s *bzzTester) flush(ids ...*adapters.NodeId) {
|
|
s.Flush(s.flushCode, ids...)
|
|
}
|
|
|
|
func (s *bzzTester) runHandshakes(ids ...*adapters.NodeId) {
|
|
if len(ids) == 0 {
|
|
ids = s.Ids
|
|
}
|
|
for _, id := range ids {
|
|
glog.V(6).Infof("\n\n\nrun handshake with %v", id)
|
|
time.Sleep(1)
|
|
s.testHandshake(correctBzzHandshake(s.addr), correctBzzHandshake(NodeIdToAddr(id)))
|
|
time.Sleep(1)
|
|
}
|
|
glog.V(6).Infof("flush %v", ids)
|
|
s.flush(ids...)
|
|
}
|
|
|
|
func correctBzzHandshake(addr *peerAddr) *bzzHandshake {
|
|
return &bzzHandshake{0, 322, addr}
|
|
}
|
|
|
|
func newBzzTester(t *testing.T, addr *peerAddr, pp PeerPool, ct *protocols.CodeMap, services func(Node) error) *bzzTester {
|
|
s := p2ptest.NewProtocolTester(t, NodeId(addr), 1, newTestBzzProtocol(addr, pp, ct, services))
|
|
return &bzzTester{
|
|
addr: addr,
|
|
flushCode: 1,
|
|
ExchangeSession: s,
|
|
}
|
|
}
|
|
|
|
func TestBzzHandshakeNetworkIdMismatch(t *testing.T) {
|
|
pp := NewTestPeerPool()
|
|
addr := RandomAddr()
|
|
s := newBzzTester(t, addr, pp, nil, nil)
|
|
id := s.Ids[0]
|
|
s.testHandshake(
|
|
correctBzzHandshake(addr),
|
|
&bzzHandshake{0, 321, NodeIdToAddr(id)},
|
|
&p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("network id mismatch 321 (!= 322)")},
|
|
)
|
|
}
|
|
|
|
func TestBzzHandshakeVersionMismatch(t *testing.T) {
|
|
pp := NewTestPeerPool()
|
|
addr := RandomAddr()
|
|
s := newBzzTester(t, addr, pp, nil, nil)
|
|
id := s.Ids[0]
|
|
s.testHandshake(
|
|
correctBzzHandshake(addr),
|
|
&bzzHandshake{1, 322, NodeIdToAddr(id)},
|
|
&p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("version mismatch 1 (!= 0)")},
|
|
)
|
|
}
|
|
|
|
func TestBzzHandshakeSuccess(t *testing.T) {
|
|
pp := NewTestPeerPool()
|
|
addr := RandomAddr()
|
|
s := newBzzTester(t, addr, pp, nil, nil)
|
|
id := s.Ids[0]
|
|
s.testHandshake(
|
|
correctBzzHandshake(addr),
|
|
&bzzHandshake{0, 322, NodeIdToAddr(id)},
|
|
)
|
|
}
|
|
|
|
func TestBzzPeerPoolAdd(t *testing.T) {
|
|
pp := NewTestPeerPool()
|
|
addr := RandomAddr()
|
|
s := newBzzTester(t, addr, pp, nil, nil)
|
|
|
|
id := s.Ids[0]
|
|
glog.V(6).Infof("handshake with %v", id)
|
|
s.runHandshakes()
|
|
if !pp.Has(id) {
|
|
t.Fatalf("peer '%v' not added: %v", id, pp)
|
|
}
|
|
}
|
|
|
|
func TestBzzPeerPoolRemove(t *testing.T) {
|
|
addr := RandomAddr()
|
|
pp := NewTestPeerPool()
|
|
s := newBzzTester(t, addr, pp, nil, nil)
|
|
s.runHandshakes()
|
|
|
|
id := s.Ids[0]
|
|
pp.Get(id).Drop()
|
|
s.TestDisconnected(&p2ptest.Disconnect{id, fmt.Errorf("p2p: read or write on closed message pipe")})
|
|
if pp.Has(id) {
|
|
t.Fatalf("peer '%v' not removed: %v", id, pp)
|
|
}
|
|
}
|
|
|
|
func TestBzzPeerPoolBothAddRemove(t *testing.T) {
|
|
addr := RandomAddr()
|
|
pp := NewTestPeerPool()
|
|
s := newBzzTester(t, addr, pp, nil, nil)
|
|
s.runHandshakes()
|
|
|
|
id := s.Ids[0]
|
|
if !pp.Has(id) {
|
|
t.Fatalf("peer '%v' not added: %v", id, pp)
|
|
}
|
|
|
|
pp.Get(id).Drop()
|
|
s.TestDisconnected(&p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("p2p: read or write on closed message pipe")})
|
|
if pp.Has(id) {
|
|
t.Fatalf("peer '%v' not removed: %v", id, pp)
|
|
}
|
|
}
|
|
|
|
func TestBzzPeerPoolNotAdd(t *testing.T) {
|
|
addr := RandomAddr()
|
|
pp := NewTestPeerPool()
|
|
s := newBzzTester(t, addr, pp, nil, nil)
|
|
|
|
id := s.Ids[0]
|
|
s.testHandshake(correctBzzHandshake(addr), &bzzHandshake{0, 321, NodeIdToAddr(id)}, &p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("network id mismatch 321 (!= 322)")})
|
|
if pp.Has(id) {
|
|
t.Fatalf("peer %v incorrectly added: %v", id, pp)
|
|
}
|
|
}
|
|
|
|
// TestPeerPool is an example peerPool to demonstrate registration of peer connections
|
|
type TestPeerPool struct {
|
|
lock sync.Mutex
|
|
peers map[discover.NodeID]Node
|
|
}
|
|
|
|
func NewTestPeerPool() *TestPeerPool {
|
|
return &TestPeerPool{peers: make(map[discover.NodeID]Node)}
|
|
}
|
|
|
|
func (self *TestPeerPool) Add(p Node) error {
|
|
self.lock.Lock()
|
|
defer self.lock.Unlock()
|
|
self.peers[p.ID()] = p
|
|
return nil
|
|
}
|
|
|
|
func (self *TestPeerPool) Remove(p Node) {
|
|
self.lock.Lock()
|
|
defer self.lock.Unlock()
|
|
// glog.V(6).Infof("removing peer %v", p.ID())
|
|
delete(self.peers, p.ID())
|
|
}
|
|
|
|
func (self *TestPeerPool) Has(n *adapters.NodeId) bool {
|
|
self.lock.Lock()
|
|
defer self.lock.Unlock()
|
|
_, ok := self.peers[n.NodeID]
|
|
return ok
|
|
}
|
|
|
|
func (self *TestPeerPool) Get(n *adapters.NodeId) Node {
|
|
self.lock.Lock()
|
|
defer self.lock.Unlock()
|
|
return self.peers[n.NodeID]
|
|
}
|