mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 07:06:42 +00:00
swarm/network, p2p/testing: WIP first test of pss poc
Fails as hive insists on starting discovery even though we don't want it
This commit is contained in:
parent
d4a08fdf7e
commit
9cad74910f
6 changed files with 116 additions and 25 deletions
|
|
@ -2,17 +2,17 @@ package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
"github.com/ethereum/go-ethereum/p2p/adapters"
|
|
||||||
"github.com/ethereum/go-ethereum/logger"
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
"github.com/ethereum/go-ethereum/logger/glog"
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/adapters"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProtocolSession struct {
|
type ProtocolSession struct {
|
||||||
TestNodeAdapter
|
TestNodeAdapter
|
||||||
Ids []*adapters.NodeId
|
Ids []*adapters.NodeId
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestMessenger interface {
|
type TestMessenger interface {
|
||||||
|
|
@ -57,11 +57,10 @@ type Disconnect struct {
|
||||||
Error error // disconnect reason
|
Error error // disconnect reason
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func NewProtocolSession(na adapters.NodeAdapter, ids []*adapters.NodeId) *ProtocolSession {
|
func NewProtocolSession(na adapters.NodeAdapter, ids []*adapters.NodeId) *ProtocolSession {
|
||||||
ps := &ProtocolSession {
|
ps := &ProtocolSession{
|
||||||
TestNodeAdapter: na.(TestNodeAdapter),
|
TestNodeAdapter: na.(TestNodeAdapter),
|
||||||
Ids: ids,
|
Ids: ids,
|
||||||
}
|
}
|
||||||
return ps
|
return ps
|
||||||
}
|
}
|
||||||
|
|
@ -133,7 +132,6 @@ func (self *ProtocolSession) expect(exp Expect) error {
|
||||||
// fatal upon encountering first exchange error
|
// fatal upon encountering first exchange error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TestExchange tests a series of exchanges againsts the session
|
// TestExchange tests a series of exchanges againsts the session
|
||||||
func (self *ProtocolSession) TestExchanges(exchanges ...Exchange) error {
|
func (self *ProtocolSession) TestExchanges(exchanges ...Exchange) error {
|
||||||
// launch all triggers of this exchanges
|
// launch all triggers of this exchanges
|
||||||
|
|
|
||||||
|
|
@ -2,21 +2,21 @@ package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/p2p/adapters"
|
|
||||||
"github.com/ethereum/go-ethereum/p2p/simulations"
|
|
||||||
"github.com/ethereum/go-ethereum/logger"
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
"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/simulations"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProtocolTester struct {
|
type ProtocolTester struct {
|
||||||
*ProtocolSession
|
*ProtocolSession
|
||||||
network *simulations.Network
|
network *simulations.Network
|
||||||
na adapters.NodeAdapter
|
na adapters.NodeAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, run func(id adapters.NodeAdapter) adapters.ProtoCall) *ProtocolTester {
|
func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, run func(id adapters.NodeAdapter) adapters.ProtoCall) *ProtocolTester {
|
||||||
|
|
||||||
simPipe := adapters.NewSimPipe
|
simPipe := adapters.NewSimPipe
|
||||||
net := simulations.NewNetwork(&simulations.NetworkConfig{})
|
net := simulations.NewNetwork(&simulations.NetworkConfig{})
|
||||||
naf := func(conf *simulations.NodeConfig) adapters.NodeAdapter {
|
naf := func(conf *simulations.NodeConfig) adapters.NodeAdapter {
|
||||||
|
|
@ -32,21 +32,21 @@ func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, run func(id ada
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
//na := net.GetNode(id).Adapter()
|
//na := net.GetNode(id).Adapter()
|
||||||
na := net.GetNodeAdapter(id)
|
na := net.GetNodeAdapter(id)
|
||||||
|
|
||||||
ids := adapters.RandomNodeIds(n)
|
ids := adapters.RandomNodeIds(n)
|
||||||
|
|
||||||
ps := NewProtocolSession(na, ids)
|
ps := NewProtocolSession(na, ids)
|
||||||
self := &ProtocolTester{
|
self := &ProtocolTester{
|
||||||
ProtocolSession: ps,
|
ProtocolSession: ps,
|
||||||
network: net,
|
network: net,
|
||||||
na: na,
|
na: na,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.Connect(ids...)
|
self.Connect(ids...)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,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.Ids[0],
|
Peer: s.ProtocolTester.Ids[0],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -59,14 +59,14 @@ func newBzzBaseTester(t *testing.T, n int, addr *peerAddr, ct *protocols.CodeMap
|
||||||
}
|
}
|
||||||
|
|
||||||
return &bzzTester{
|
return &bzzTester{
|
||||||
addr: addr,
|
addr: addr,
|
||||||
ExchangeSession: s,
|
ProtocolTester: s,
|
||||||
cs: cs,
|
cs: cs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type bzzTester struct {
|
type bzzTester struct {
|
||||||
*p2ptest.ExchangeSession
|
*p2ptest.ProtocolTester
|
||||||
addr *peerAddr
|
addr *peerAddr
|
||||||
cs map[string]chan bool
|
cs map[string]chan bool
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
swarm/network/pss.go
Normal file
26
swarm/network/pss.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package network
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pssPeer struct {
|
||||||
|
Peer
|
||||||
|
}
|
||||||
|
|
||||||
|
type PssMsg struct {
|
||||||
|
Recipient pssPeer
|
||||||
|
Payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pm *PssMsg) String() string {
|
||||||
|
return fmt.Sprintf("PssMsg: Recipient: %v", pm.Recipient)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PssMsgHandler(msg interface{}) error {
|
||||||
|
glog.V(logger.Detail).Infof("Pss Handled!")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
67
swarm/network/pss_test.go
Normal file
67
swarm/network/pss_test.go
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
package network
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/adapters"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/simulations"
|
||||||
|
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pssTester struct {
|
||||||
|
*p2ptest.ProtocolTester
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPssTwoToSelf(t *testing.T) {
|
||||||
|
addr := RandomAddr()
|
||||||
|
pt := newPssTester(t, addr, 2)
|
||||||
|
hs_pivot := correctBzzHandshake(addr)
|
||||||
|
for _, id := range pt.Ids {
|
||||||
|
hs_sim := correctBzzHandshake(NewPeerAddrFromNodeId(id))
|
||||||
|
glog.V(logger.Detail).Infof("Will handshake %v with %v", hs_pivot, hs_sim)
|
||||||
|
<-pt.GetPeer(id).Connc
|
||||||
|
pt.TestExchanges(bzzHandshakeExchange(hs_pivot, hs_sim, id)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPssTester(t *testing.T, addr *peerAddr, n int) *pssTester {
|
||||||
|
return newPssBaseTester(t, addr, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPssBaseTester(t *testing.T, addr *peerAddr, n int) *pssTester {
|
||||||
|
ct := BzzCodeMap()
|
||||||
|
ct.Register(&PssMsg{})
|
||||||
|
|
||||||
|
simPipe := adapters.NewSimPipe
|
||||||
|
kp := NewKadParams()
|
||||||
|
to := NewKademlia(addr.OverlayAddr(), kp)
|
||||||
|
pp := NewHive(NewHiveParams(), to)
|
||||||
|
net := simulations.NewNetwork(&simulations.NetworkConfig{})
|
||||||
|
naf := func(conf *simulations.NodeConfig) adapters.NodeAdapter {
|
||||||
|
na := adapters.NewSimNode(conf.Id, net, simPipe)
|
||||||
|
return na
|
||||||
|
}
|
||||||
|
net.SetNaf(naf)
|
||||||
|
|
||||||
|
srv := func(p Peer) error {
|
||||||
|
p.Register(&PssMsg{}, PssMsgHandler)
|
||||||
|
pp.Add(p)
|
||||||
|
p.DisconnectHook(func(err error) {
|
||||||
|
pp.Remove(p)
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
protocall := func(na adapters.NodeAdapter) adapters.ProtoCall {
|
||||||
|
protocol := Bzz(addr.OverlayAddr(), na, ct, srv, nil, nil)
|
||||||
|
return protocol.Run
|
||||||
|
}
|
||||||
|
|
||||||
|
s := p2ptest.NewProtocolTester(t, NodeId(addr), n, protocall)
|
||||||
|
|
||||||
|
return &pssTester{
|
||||||
|
ProtocolTester: s,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue