mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
swarm/network: protocol test simplifications
This commit is contained in:
parent
a6a006c5e3
commit
29d3936af7
4 changed files with 30 additions and 48 deletions
|
|
@ -11,7 +11,7 @@ func TestDiscovery(t *testing.T) {
|
||||||
to := NewKademlia(addr.OAddr, NewKadParams())
|
to := NewKademlia(addr.OAddr, NewKadParams())
|
||||||
pp := NewHive(NewHiveParams(), to)
|
pp := NewHive(NewHiveParams(), to)
|
||||||
ct := BzzCodeMap(HiveMsgs...)
|
ct := BzzCodeMap(HiveMsgs...)
|
||||||
s := newBzzTester(t, 1, addr, pp, ct, nil)
|
s := newBzzTester(t, addr, pp, ct, nil)
|
||||||
|
|
||||||
s.runHandshakes()
|
s.runHandshakes()
|
||||||
s.TestExchanges(p2ptest.Exchange{
|
s.TestExchanges(p2ptest.Exchange{
|
||||||
|
|
@ -19,7 +19,7 @@ func TestDiscovery(t *testing.T) {
|
||||||
p2ptest.Expect{
|
p2ptest.Expect{
|
||||||
Code: 3,
|
Code: 3,
|
||||||
Msg: &SubPeersMsg{ProxLimit: 0, MinProxBinSize: 8},
|
Msg: &SubPeersMsg{ProxLimit: 0, MinProxBinSize: 8},
|
||||||
Peer: s.ExchangeSession.Id(1),
|
Peer: s.ExchangeSession.Ids[1],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ func TestOverlayRegistration(t *testing.T) {
|
||||||
pp := NewHive(NewHiveParams(), to) // hive
|
pp := NewHive(NewHiveParams(), to) // hive
|
||||||
ct := BzzCodeMap(HiveMsgs...) // bzz protocol code map
|
ct := BzzCodeMap(HiveMsgs...) // bzz protocol code map
|
||||||
|
|
||||||
s := newBzzTester(t, 1, addr, ct, nil)
|
s := newBzzTester(t, addr, pp, ct, nil)
|
||||||
|
|
||||||
// connect to the other peer
|
// connect to the other peer
|
||||||
id := s.Ids[0]
|
id := s.Ids[0]
|
||||||
|
|
@ -62,7 +62,7 @@ func TestRegisterAndConnect(t *testing.T) {
|
||||||
to := NewTestOverlay(addr.OverlayAddr())
|
to := NewTestOverlay(addr.OverlayAddr())
|
||||||
pp := NewHive(NewHiveParams(), to)
|
pp := NewHive(NewHiveParams(), to)
|
||||||
ct := BzzCodeMap(HiveMsgs...)
|
ct := BzzCodeMap(HiveMsgs...)
|
||||||
s := newBzzTester(t, 0, addr, pp, ct, nil)
|
s := newBzzTester(t, addr, pp, ct, nil)
|
||||||
|
|
||||||
// register the node with the peerPool
|
// register the node with the peerPool
|
||||||
id := p2ptest.RandomNodeId()
|
id := p2ptest.RandomNodeId()
|
||||||
|
|
@ -103,7 +103,7 @@ func TestRegisterAndConnect(t *testing.T) {
|
||||||
p2ptest.Expect{
|
p2ptest.Expect{
|
||||||
Code: 3,
|
Code: 3,
|
||||||
Msg: &SubPeersMsg{ProxLimit: 0, MinProxBinSize: 8},
|
Msg: &SubPeersMsg{ProxLimit: 0, MinProxBinSize: 8},
|
||||||
Peer: s.ExchangeSession.Id(1),
|
Peer: s.ExchangeSession.Ids[1],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -43,16 +43,13 @@ type bzzPeer struct {
|
||||||
localAddr *peerAddr
|
localAddr *peerAddr
|
||||||
*peerAddr // remote address
|
*peerAddr // remote address
|
||||||
lastActive time.Time
|
lastActive time.Time
|
||||||
peers map[discover.NodeID]bool
|
//peers map[discover.NodeID]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *bzzPeer) LastActive() time.Time {
|
func (self *bzzPeer) LastActive() time.Time {
|
||||||
return self.lastActive
|
return self.lastActive
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *bzzPeer) Peers() map[discover.NodeID]bool {
|
|
||||||
return self.peers
|
|
||||||
}
|
|
||||||
|
|
||||||
// implemented by peerAddr
|
// implemented by peerAddr
|
||||||
type PeerAddr interface {
|
type PeerAddr interface {
|
||||||
|
|
@ -65,7 +62,6 @@ type Peer interface {
|
||||||
PeerAddr
|
PeerAddr
|
||||||
String() string // pretty printable the Node
|
String() string // pretty printable the Node
|
||||||
ID() discover.NodeID // the key that uniquely identifies the Node for the peerPool
|
ID() discover.NodeID // the key that uniquely identifies the Node for the peerPool
|
||||||
Peers() map[discover.NodeID]bool
|
|
||||||
Send(interface{}) error // can send messages
|
Send(interface{}) error // can send messages
|
||||||
Drop(error) // disconnect this peer
|
Drop(error) // disconnect this peer
|
||||||
Register(interface{}, func(interface{}) error) uint64 // register message-handler callbacks
|
Register(interface{}, func(interface{}) error) uint64 // register message-handler callbacks
|
||||||
|
|
@ -80,7 +76,7 @@ func BzzCodeMap(msgs ...interface{}) *protocols.CodeMap {
|
||||||
|
|
||||||
// Bzz is the protocol constructor
|
// Bzz is the protocol constructor
|
||||||
// returns p2p.Protocol that is to be offered by the node.Service
|
// returns p2p.Protocol that is to be offered by the node.Service
|
||||||
func Bzz(localAddr []byte, na adapters.NodeAdapter, ct *protocols.CodeMap, services func(Peer) error) *p2p.Protocol {
|
func Bzz(localAddr []byte, na adapters.NodeAdapter, ct *protocols.CodeMap, services func(Peer) error, peerInfo func(id discover.NodeID) interface{}, nodeInfo func() interface{}) *p2p.Protocol {
|
||||||
run := func(p *protocols.Peer) error {
|
run := func(p *protocols.Peer) error {
|
||||||
addr := &peerAddr{localAddr, na.LocalAddr()}
|
addr := &peerAddr{localAddr, na.LocalAddr()}
|
||||||
|
|
||||||
|
|
@ -88,7 +84,6 @@ func Bzz(localAddr []byte, na adapters.NodeAdapter, ct *protocols.CodeMap, servi
|
||||||
Peer: p,
|
Peer: p,
|
||||||
network: na,
|
network: na,
|
||||||
localAddr: addr,
|
localAddr: addr,
|
||||||
peers: make(map[discover.NodeID]bool),
|
|
||||||
}
|
}
|
||||||
// protocol handshake and its validation
|
// protocol handshake and its validation
|
||||||
// sets remote peer address
|
// sets remote peer address
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,10 @@ package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/logger/glog"
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
"github.com/ethereum/go-ethereum/p2p/adapters"
|
"github.com/ethereum/go-ethereum/p2p/adapters"
|
||||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
||||||
"github.com/ethereum/go-ethereum/p2p/protocols"
|
"github.com/ethereum/go-ethereum/p2p/protocols"
|
||||||
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
||||||
)
|
)
|
||||||
|
|
@ -36,31 +34,35 @@ func bzzHandshakeExchange(lhs, rhs *bzzHandshake, id *adapters.NodeId) []p2ptest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestBzzProtocol(addr *peerAddr, ct *protocols.CodeMap, services func(Peer) error) func(adapters.NodeAdapter) adapters.ProtoCall {
|
func newBzzTester(t *testing.T, addr *peerAddr, pp *Hive, ct *protocols.CodeMap, services func(Peer) error) *bzzTester {
|
||||||
if ct == nil {
|
if ct == nil {
|
||||||
ct = BzzCodeMap()
|
ct = BzzCodeMap()
|
||||||
}
|
}
|
||||||
// ct.Register(p2ptest.FlushMsg)
|
extraservices := func(p Peer) error {
|
||||||
return func(na adapters.NodeAdapter) adapters.ProtoCall {
|
pp.Add(p)
|
||||||
srv := func(p Peer) error {
|
p.Register(&protocols.Disconnect{}, func(e interface{}) error { pp.Remove(p) })
|
||||||
|
|
||||||
if services != nil {
|
if services != nil {
|
||||||
err := services(p)
|
err := services(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol := Bzz(addr.OverlayAddr(), pp, na, ct, srv)
|
protocall := func(na adapters.NodeAdapter) adapters.ProtoCall {
|
||||||
|
protocol := Bzz(addr.OverlayAddr(), pp, na, ct, extraservices, nil, nil)
|
||||||
return protocol.Run
|
return protocol.Run
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s := p2ptest.NewProtocolTester(t, NodeId(addr), 1, protocall)
|
||||||
|
|
||||||
|
return &bzzTester{
|
||||||
|
addr: addr,
|
||||||
|
// flushCode: 4,
|
||||||
|
ExchangeSession: s,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type bzzTester struct {
|
type bzzTester struct {
|
||||||
|
|
@ -103,21 +105,6 @@ func correctBzzHandshake(addr *peerAddr) *bzzHandshake {
|
||||||
return &bzzHandshake{0, 322, addr}
|
return &bzzHandshake{0, 322, addr}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBzzTester(t *testing.T, addr *peerAddr, pp PeerPool, ct *protocols.CodeMap, services func(Peer) error) *bzzTester {
|
|
||||||
|
|
||||||
extraservices := func(p Peer) error {
|
|
||||||
pp.Add(p)
|
|
||||||
p.Register(&protocols.Disconnect{}, func(e interface{}) error { pp.Remove(p) })
|
|
||||||
return services(p)
|
|
||||||
}
|
|
||||||
s := p2ptest.NewProtocolTester(t, NodeId(addr), 1, newTestBzzProtocol(addr, pp, ct, extraservices))
|
|
||||||
return &bzzTester{
|
|
||||||
addr: addr,
|
|
||||||
// flushCode: 4,
|
|
||||||
ExchangeSession: s,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBzzHandshakeNetworkIdMismatch(t *testing.T) {
|
func TestBzzHandshakeNetworkIdMismatch(t *testing.T) {
|
||||||
pp := p2ptest.NewTestPeerPool()
|
pp := p2ptest.NewTestPeerPool()
|
||||||
addr := RandomAddr()
|
addr := RandomAddr()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue