2018-01-03 01:30:09 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/*
|
|
|
|
the p2p/testing package provides a unit test scheme to check simple
|
|
|
|
protocol message exchanges with one pivot node and a number of dummy peers
|
|
|
|
The pivot test node runs a node.Service, the dummy peers run a mock node
|
|
|
|
that can be used to send and receive messages
|
|
|
|
*/
|
|
|
|
|
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
2018-02-12 20:40:25 +03:00
|
|
|
"bytes"
|
2019-03-15 13:27:17 +03:00
|
|
|
"crypto/ecdsa"
|
2018-01-03 01:30:09 +03:00
|
|
|
"fmt"
|
2018-02-12 20:40:25 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2018-01-03 01:30:09 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-01-03 01:30:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/simulations"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
|
2018-02-12 20:40:25 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2018-01-03 01:30:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProtocolTester is the tester environment used for unit testing protocol
|
|
|
|
// message exchanges. It uses p2p/simulations framework
|
|
|
|
type ProtocolTester struct {
|
|
|
|
*ProtocolSession
|
|
|
|
network *simulations.Network
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProtocolTester constructs a new ProtocolTester
|
|
|
|
// it takes as argument the pivot node id, the number of dummy peers and the
|
|
|
|
// protocol run function called on a peer connection by the p2p server
|
2019-03-15 13:27:17 +03:00
|
|
|
func NewProtocolTester(prvkey *ecdsa.PrivateKey, nodeCount int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
|
2018-01-03 01:30:09 +03:00
|
|
|
services := adapters.Services{
|
|
|
|
"test": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
|
|
|
return &testNode{run}, nil
|
|
|
|
},
|
|
|
|
"mock": func(ctx *adapters.ServiceContext) (node.Service, error) {
|
|
|
|
return newMockNode(), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
adapter := adapters.NewSimAdapter(services)
|
|
|
|
net := simulations.NewNetwork(adapter, &simulations.NetworkConfig{})
|
2019-03-15 13:27:17 +03:00
|
|
|
nodeConfig := &adapters.NodeConfig{
|
|
|
|
PrivateKey: prvkey,
|
2018-01-03 01:30:09 +03:00
|
|
|
EnableMsgEvents: true,
|
|
|
|
Services: []string{"test"},
|
2019-03-15 13:27:17 +03:00
|
|
|
}
|
|
|
|
if _, err := net.NewNodeWithConfig(nodeConfig); err != nil {
|
2018-01-03 01:30:09 +03:00
|
|
|
panic(err.Error())
|
|
|
|
}
|
2019-03-15 13:27:17 +03:00
|
|
|
if err := net.Start(nodeConfig.ID); err != nil {
|
2018-01-03 01:30:09 +03:00
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
2019-03-15 13:27:17 +03:00
|
|
|
node := net.GetNode(nodeConfig.ID).Node.(*adapters.SimNode)
|
2019-03-08 19:30:16 +03:00
|
|
|
peers := make([]*adapters.NodeConfig, nodeCount)
|
|
|
|
nodes := make([]*enode.Node, nodeCount)
|
|
|
|
for i := 0; i < nodeCount; i++ {
|
2018-01-03 01:30:09 +03:00
|
|
|
peers[i] = adapters.RandomNodeConfig()
|
|
|
|
peers[i].Services = []string{"mock"}
|
2019-03-15 13:27:17 +03:00
|
|
|
if _, err := net.NewNodeWithConfig(peers[i]); err != nil {
|
|
|
|
panic(fmt.Sprintf("error initializing peer %v: %v", peers[i].ID, err))
|
|
|
|
}
|
|
|
|
if err := net.Start(peers[i].ID); err != nil {
|
|
|
|
panic(fmt.Sprintf("error starting peer %v: %v", peers[i].ID, err))
|
|
|
|
}
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
nodes[i] = peers[i].Node()
|
2018-01-03 01:30:09 +03:00
|
|
|
}
|
|
|
|
events := make(chan *p2p.PeerEvent, 1000)
|
|
|
|
node.SubscribeEvents(events)
|
|
|
|
ps := &ProtocolSession{
|
|
|
|
Server: node.Server(),
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
Nodes: nodes,
|
2018-01-03 01:30:09 +03:00
|
|
|
adapter: adapter,
|
|
|
|
events: events,
|
|
|
|
}
|
|
|
|
self := &ProtocolTester{
|
|
|
|
ProtocolSession: ps,
|
|
|
|
network: net,
|
|
|
|
}
|
|
|
|
|
2019-03-15 13:27:17 +03:00
|
|
|
self.Connect(nodeConfig.ID, peers...)
|
2018-01-03 01:30:09 +03:00
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the p2p server
|
2019-03-08 19:30:16 +03:00
|
|
|
func (t *ProtocolTester) Stop() {
|
2018-05-08 14:08:43 +03:00
|
|
|
t.Server.Stop()
|
2019-04-25 22:33:18 +03:00
|
|
|
t.network.Shutdown()
|
2018-01-03 01:30:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect brings up the remote peer node and connects it using the
|
|
|
|
// p2p/simulations network connection with the in memory network adapter
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
func (t *ProtocolTester) Connect(selfID enode.ID, peers ...*adapters.NodeConfig) {
|
2018-01-03 01:30:09 +03:00
|
|
|
for _, peer := range peers {
|
|
|
|
log.Trace(fmt.Sprintf("connect to %v", peer.ID))
|
2018-05-08 14:08:43 +03:00
|
|
|
if err := t.network.Connect(selfID, peer.ID); err != nil {
|
2018-01-03 01:30:09 +03:00
|
|
|
panic(fmt.Sprintf("error connecting to peer %v: %v", peer.ID, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// testNode wraps a protocol run function and implements the node.Service
|
|
|
|
// interface
|
|
|
|
type testNode struct {
|
|
|
|
run func(*p2p.Peer, p2p.MsgReadWriter) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testNode) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{{
|
|
|
|
Length: 100,
|
|
|
|
Run: t.run,
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testNode) APIs() []rpc.API {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testNode) Start(server *p2p.Server) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testNode) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockNode is a testNode which doesn't actually run a protocol, instead
|
|
|
|
// exposing channels so that tests can manually trigger and expect certain
|
|
|
|
// messages
|
|
|
|
type mockNode struct {
|
|
|
|
testNode
|
|
|
|
|
|
|
|
trigger chan *Trigger
|
2018-02-12 20:40:25 +03:00
|
|
|
expect chan []Expect
|
2018-01-03 01:30:09 +03:00
|
|
|
err chan error
|
|
|
|
stop chan struct{}
|
|
|
|
stopOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockNode() *mockNode {
|
|
|
|
mock := &mockNode{
|
|
|
|
trigger: make(chan *Trigger),
|
2018-02-12 20:40:25 +03:00
|
|
|
expect: make(chan []Expect),
|
2018-01-03 01:30:09 +03:00
|
|
|
err: make(chan error),
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
|
|
|
mock.testNode.run = mock.Run
|
|
|
|
return mock
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run is a protocol run function which just loops waiting for tests to
|
|
|
|
// instruct it to either trigger or expect a message from the peer
|
|
|
|
func (m *mockNode) Run(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case trig := <-m.trigger:
|
2018-07-13 18:40:28 +03:00
|
|
|
wmsg := Wrap(trig.Msg)
|
|
|
|
m.err <- p2p.Send(rw, trig.Code, wmsg)
|
2018-02-12 20:40:25 +03:00
|
|
|
case exps := <-m.expect:
|
|
|
|
m.err <- expectMsgs(rw, exps)
|
2018-01-03 01:30:09 +03:00
|
|
|
case <-m.stop:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNode) Trigger(trig *Trigger) error {
|
|
|
|
m.trigger <- trig
|
|
|
|
return <-m.err
|
|
|
|
}
|
|
|
|
|
2018-02-12 20:40:25 +03:00
|
|
|
func (m *mockNode) Expect(exp ...Expect) error {
|
2018-01-03 01:30:09 +03:00
|
|
|
m.expect <- exp
|
|
|
|
return <-m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockNode) Stop() error {
|
|
|
|
m.stopOnce.Do(func() { close(m.stop) })
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-12 20:40:25 +03:00
|
|
|
|
|
|
|
func expectMsgs(rw p2p.MsgReadWriter, exps []Expect) error {
|
|
|
|
matched := make([]bool, len(exps))
|
|
|
|
for {
|
|
|
|
msg, err := rw.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actualContent, err := ioutil.ReadAll(msg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var found bool
|
|
|
|
for i, exp := range exps {
|
2018-07-13 18:40:28 +03:00
|
|
|
if exp.Code == msg.Code && bytes.Equal(actualContent, mustEncodeMsg(Wrap(exp.Msg))) {
|
2018-02-12 20:40:25 +03:00
|
|
|
if matched[i] {
|
|
|
|
return fmt.Errorf("message #%d received two times", i)
|
|
|
|
}
|
|
|
|
matched[i] = true
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
expected := make([]string, 0)
|
|
|
|
for i, exp := range exps {
|
|
|
|
if matched[i] {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-13 18:40:28 +03:00
|
|
|
expected = append(expected, fmt.Sprintf("code %d payload %x", exp.Code, mustEncodeMsg(Wrap(exp.Msg))))
|
2018-02-12 20:40:25 +03:00
|
|
|
}
|
|
|
|
return fmt.Errorf("unexpected message code %d payload %x, expected %s", msg.Code, actualContent, strings.Join(expected, " or "))
|
|
|
|
}
|
|
|
|
done := true
|
|
|
|
for _, m := range matched {
|
|
|
|
if !m {
|
|
|
|
done = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if done {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, m := range matched {
|
|
|
|
if !m {
|
|
|
|
return fmt.Errorf("expected message #%d not received", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustEncodeMsg uses rlp to encode a message.
|
|
|
|
// In case of error it panics.
|
|
|
|
func mustEncodeMsg(msg interface{}) []byte {
|
|
|
|
contentEnc, err := rlp.EncodeToBytes(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic("content encode error: " + err.Error())
|
|
|
|
}
|
|
|
|
return contentEnc
|
|
|
|
}
|
2018-07-13 18:40:28 +03:00
|
|
|
|
|
|
|
type WrappedMsg struct {
|
|
|
|
Context []byte
|
|
|
|
Size uint32
|
|
|
|
Payload []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func Wrap(msg interface{}) interface{} {
|
|
|
|
data, _ := rlp.EncodeToBytes(msg)
|
|
|
|
return &WrappedMsg{
|
|
|
|
Size: uint32(len(data)),
|
|
|
|
Payload: data,
|
|
|
|
}
|
|
|
|
}
|