mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
swarm/pss, p2p/testing, p2p/simulations/adapters: svcs sequence
This commit is contained in:
parent
dedc26bd2c
commit
3a4aee8087
4 changed files with 53 additions and 48 deletions
|
|
@ -224,9 +224,11 @@ func (self *SimNode) Start(snapshot []byte) error {
|
|||
|
||||
services := []node.ServiceConstructor{}
|
||||
|
||||
for name, servicefunc := range self.serviceFuncs {
|
||||
service := servicefunc(self.Id, snapshot)
|
||||
|
||||
|
||||
// we need to control the order of the services
|
||||
// for example, bzz needs to start before pss
|
||||
for _, name := range self.config.Services {
|
||||
service := self.serviceFuncs[name](self.Id, snapshot)
|
||||
services = append(services, func(ctx *node.ServiceContext) (node.Service, error) {
|
||||
self.running[name] = service
|
||||
return service, nil
|
||||
|
|
|
|||
|
|
@ -18,7 +18,9 @@ type ProtocolTester struct {
|
|||
network *simulations.Network
|
||||
}
|
||||
|
||||
func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
|
||||
func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, moreservices adapters.Services, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
|
||||
//func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
|
||||
moreservicesstring := []string{}
|
||||
services := map[string]adapters.ServiceFunc{
|
||||
"test": func(id *adapters.NodeId, _ []byte) node.Service {
|
||||
return &testNode{run}
|
||||
|
|
@ -27,9 +29,13 @@ func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, run func(*p2p.P
|
|||
return newMockNode()
|
||||
},
|
||||
}
|
||||
for name, service := range moreservices {
|
||||
services[name] = service
|
||||
moreservicesstring = append(moreservicesstring, name)
|
||||
}
|
||||
adapter := adapters.NewSimAdapter(services)
|
||||
net := simulations.NewNetwork(adapter, &simulations.NetworkConfig{})
|
||||
if _, err := net.NewNodeWithConfig(&adapters.NodeConfig{Id: id, Services: []string{"test"}}); err != nil {
|
||||
if _, err := net.NewNodeWithConfig(&adapters.NodeConfig{Id: id, Services: append(moreservicesstring, "test")}); err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
if err := net.Start(id); err != nil {
|
||||
|
|
@ -41,6 +47,7 @@ func NewProtocolTester(t *testing.T, id *adapters.NodeId, n int, run func(*p2p.P
|
|||
peerIDs := make([]*adapters.NodeId, n)
|
||||
for i := 0; i < n; i++ {
|
||||
peers[i] = adapters.RandomNodeConfig()
|
||||
peers[i].Services = moreservicesstring
|
||||
peers[i].Services = append(peers[i].Services, "mock")
|
||||
peerIDs[i] = peers[i].Id
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ var pssPingProtocol = &protocols.Spec{
|
|||
},
|
||||
}
|
||||
|
||||
var pssPingTopic = NewTopic(pssPingProtocol.Name, int(pssPingProtocol.Version))
|
||||
|
||||
func newTestPss(addr []byte) *Pss {
|
||||
if addr == nil {
|
||||
addr = network.RandomAddr().OAddr
|
||||
|
|
@ -64,3 +66,24 @@ func newTestPss(addr []byte) *Pss {
|
|||
return ps
|
||||
}
|
||||
|
||||
func newPssPingMsg(ps *Pss, spec *protocols.Spec, topic PssTopic, senderaddr []byte) PssMsg {
|
||||
data := pssPingMsg{
|
||||
Created: time.Now(),
|
||||
}
|
||||
code, found := spec.GetCode(&data)
|
||||
if !found {
|
||||
return PssMsg{}
|
||||
}
|
||||
|
||||
rlpbundle, err := newProtocolMsg(code, data)
|
||||
if err != nil {
|
||||
return PssMsg{}
|
||||
}
|
||||
|
||||
pssmsg := PssMsg{
|
||||
To: ps.Overlay.BaseAddr(),
|
||||
Payload: NewPssEnvelope(senderaddr, topic, rlpbundle),
|
||||
}
|
||||
|
||||
return pssmsg
|
||||
}
|
||||
|
|
|
|||
|
|
@ -176,30 +176,29 @@ func TestPssRegisterHandler(t *testing.T) {
|
|||
func TestPssSimpleLinear(t *testing.T) {
|
||||
nodeconfig := adapters.RandomNodeConfig()
|
||||
addr := network.NewAddrFromNodeId(nodeconfig.Id)
|
||||
pss := newTestPss(addr.OAddr)
|
||||
ps := newTestPss(addr.OAddr)
|
||||
pt := p2ptest.NewProtocolTester(t, nodeconfig.Id, 2, newServices(), ps.Protocols()[0].Run)
|
||||
|
||||
pt := p2ptest.NewProtocolTester(t, nodeconfig.Id, 2, pss.Protocols()[0].Run)
|
||||
/*
|
||||
return []p2ptest.Exchange{
|
||||
p2ptest.Exchange{
|
||||
msg := newPssPingMsg(ps, pssPingProtocol, pssPingTopic, []byte{1,2,3})
|
||||
|
||||
exchange := p2ptest.Exchange{
|
||||
Expects: []p2ptest.Expect{
|
||||
p2ptest.Expect{
|
||||
Code: 0,
|
||||
Msg: lhs,
|
||||
Peer: id,
|
||||
Msg: msg,
|
||||
Peer: pt.Ids[1],
|
||||
},
|
||||
},
|
||||
Triggers: []p2ptest.Trigger{
|
||||
p2ptest.Trigger{
|
||||
Code: 0,
|
||||
Msg: ,
|
||||
Peer: id,
|
||||
Msg: msg,
|
||||
Peer: pt.Ids[1],
|
||||
},
|
||||
},
|
||||
},
|
||||
}*/
|
||||
|
||||
_ = pt
|
||||
}
|
||||
|
||||
pt.TestExchanges(exchange)
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -373,7 +372,7 @@ func newServices() adapters.Services {
|
|||
config.HiveParams.KeepAliveInterval = time.Second
|
||||
|
||||
bzzs[id] = network.NewBzz(config)
|
||||
|
||||
|
||||
return bzzs[id]
|
||||
}
|
||||
|
||||
|
|
@ -390,6 +389,9 @@ func newServices() adapters.Services {
|
|||
return nil
|
||||
}
|
||||
pssp := NewPssParams()
|
||||
for bzzs[id] == nil {
|
||||
time.Sleep(time.Microsecond * 100)
|
||||
}
|
||||
return NewPss(bzzs[id].Kademlia, dpa, pssp)
|
||||
}
|
||||
|
||||
|
|
@ -1301,35 +1303,6 @@ func makeCustomProtocol(name string, version int, ct *protocols.CodeMap, testpee
|
|||
return protocols.NewProtocol(name, uint(version), run, ct, nil, nil)
|
||||
}
|
||||
|
||||
func makeFakeMsg(ps *Pss, ct *protocols.CodeMap, topic PssTopic, senderaddr Addr, content string) PssMsg {
|
||||
data := pssTestPayload{}
|
||||
code, found := ct.GetCode(&data)
|
||||
if !found {
|
||||
return PssMsg{}
|
||||
}
|
||||
|
||||
data.Data = content
|
||||
|
||||
rlpbundle, err := makeMsg(code, data)
|
||||
if err != nil {
|
||||
return PssMsg{}
|
||||
}
|
||||
|
||||
pssenv := pssEnvelope{
|
||||
SenderOAddr: senderaddr.Over(),
|
||||
SenderUAddr: senderaddr.Under(),
|
||||
Topic: topic,
|
||||
TTL: DefaultTTL,
|
||||
Payload: rlpbundle,
|
||||
}
|
||||
pssmsg := PssMsg{
|
||||
Payload: pssenv,
|
||||
}
|
||||
pssmsg.SetRecipient(ps.Overlay.BaseAddr())
|
||||
|
||||
return pssmsg
|
||||
}
|
||||
|
||||
func makePssHandleForward(ps *Pss) func(msg interface{}) error {
|
||||
// for the simple check it passes on the message if it's not for us
|
||||
return func(msg interface{}) error {
|
||||
|
|
|
|||
Loading…
Reference in a new issue