Skip to content

Commit

Permalink
Merge pull request ethereum#96 from ethersphere/network-testing-frame…
Browse files Browse the repository at this point in the history
…work-node-id

p2p/simulations: Drop adapters.NodeId in favour of discover.NodeID
  • Loading branch information
zelig authored May 28, 2017
2 parents 5e3c9d6 + da6d15d commit fe1d6d2
Show file tree
Hide file tree
Showing 28 changed files with 406 additions and 427 deletions.
25 changes: 25 additions & 0 deletions p2p/discover/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ func (n *Node) UnmarshalText(text []byte) error {
// The node identifier is a marshaled elliptic curve public key.
type NodeID [NodeIDBits / 8]byte

// Bytes returns a byte slice representation of the NodeID
func (n NodeID) Bytes() []byte {
return n[:]
}

// NodeID prints as a long hexadecimal number.
func (n NodeID) String() string {
return fmt.Sprintf("%x", n[:])
Expand All @@ -240,6 +245,26 @@ func (n NodeID) TerminalString() string {
return hex.EncodeToString(n[:8])
}

// BytesID converts a byte slice to a NodeID
func BytesID(b []byte) (NodeID, error) {
var id NodeID
if len(b) != len(id) {
return id, fmt.Errorf("wrong length, want %d bytes", len(id))
}
copy(id[:], b)
return id, nil
}

// MustBytesID converts a byte slice to a NodeID.
// It panics if the byte slice is not a valid NodeID.
func MustBytesID(b []byte) NodeID {
id, err := BytesID(b)
if err != nil {
panic(err)
}
return id
}

// HexID converts a hex string to a NodeID.
// The string may be prefixed with 0x.
func HexID(in string) (NodeID, error) {
Expand Down
47 changes: 24 additions & 23 deletions p2p/protocols/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
)
Expand All @@ -23,9 +24,9 @@ type hs0 struct {
C uint
}

// message to kill/drop the peer with nodeId
// message to kill/drop the peer with nodeID
type kill struct {
C *adapters.NodeId
C discover.NodeID
}

// message to drop connection
Expand Down Expand Up @@ -139,10 +140,10 @@ func newProtocol(pp *p2ptest.TestPeerPool) func(*p2p.Peer, p2p.MsgReadWriter) er

func protocolTester(t *testing.T, pp *p2ptest.TestPeerPool) *p2ptest.ProtocolTester {
conf := adapters.RandomNodeConfig()
return p2ptest.NewProtocolTester(t, conf.Id, 2, newProtocol(pp))
return p2ptest.NewProtocolTester(t, conf.ID, 2, newProtocol(pp))
}

func protoHandshakeExchange(id *adapters.NodeId, proto *protoHandshake) []p2ptest.Exchange {
func protoHandshakeExchange(id discover.NodeID, proto *protoHandshake) []p2ptest.Exchange {

return []p2ptest.Exchange{
p2ptest.Exchange{
Expand Down Expand Up @@ -170,13 +171,13 @@ func runProtoHandshake(t *testing.T, proto *protoHandshake, errs ...error) {
pp := p2ptest.NewTestPeerPool()
s := protocolTester(t, pp)
// TODO: make this more than one handshake
id := s.Ids[0]
id := s.IDs[0]
if err := s.TestExchanges(protoHandshakeExchange(id, proto)...); err != nil {
t.Fatal(err)
}
var disconnects []*p2ptest.Disconnect
for i, err := range errs {
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.Ids[i], Error: err})
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.IDs[i], Error: err})
}
if err := s.TestDisconnected(disconnects...); err != nil {
t.Fatal(err)
Expand All @@ -195,7 +196,7 @@ func TestProtoHandshakeSuccess(t *testing.T) {
runProtoHandshake(t, &protoHandshake{42, "420"})
}

func moduleHandshakeExchange(id *adapters.NodeId, resp uint) []p2ptest.Exchange {
func moduleHandshakeExchange(id discover.NodeID, resp uint) []p2ptest.Exchange {

return []p2ptest.Exchange{
p2ptest.Exchange{
Expand All @@ -222,12 +223,12 @@ func moduleHandshakeExchange(id *adapters.NodeId, resp uint) []p2ptest.Exchange
func runModuleHandshake(t *testing.T, resp uint, errs ...error) {
pp := p2ptest.NewTestPeerPool()
s := protocolTester(t, pp)
id := s.Ids[0]
id := s.IDs[0]
s.TestExchanges(protoHandshakeExchange(id, &protoHandshake{42, "420"})...)
s.TestExchanges(moduleHandshakeExchange(id, resp)...)
var disconnects []*p2ptest.Disconnect
for i, err := range errs {
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.Ids[i], Error: err})
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.IDs[i], Error: err})
}
s.TestDisconnected(disconnects...)
}
Expand All @@ -241,7 +242,7 @@ func TestModuleHandshakeSuccess(t *testing.T) {
}

// testing complex interactions over multiple peers, relaying, dropping
func testMultiPeerSetup(a, b *adapters.NodeId) []p2ptest.Exchange {
func testMultiPeerSetup(a, b discover.NodeID) []p2ptest.Exchange {

return []p2ptest.Exchange{
p2ptest.Exchange{
Expand Down Expand Up @@ -297,29 +298,29 @@ func runMultiplePeers(t *testing.T, peer int, errs ...error) {
pp := p2ptest.NewTestPeerPool()
s := protocolTester(t, pp)

s.TestExchanges(testMultiPeerSetup(s.Ids[0], s.Ids[1])...)
s.TestExchanges(testMultiPeerSetup(s.IDs[0], s.IDs[1])...)
// after some exchanges of messages, we can test state changes
// here this is simply demonstrated by the peerPool
// after the handshake negotiations peers must be added to the pool
// time.Sleep(1)
for !pp.Has(s.Ids[0]) {
for !pp.Has(s.IDs[0]) {
time.Sleep(1)
log.Trace(fmt.Sprintf("missing peer test-0: %v (%v)", pp, s.Ids))
log.Trace(fmt.Sprintf("missing peer test-0: %v (%v)", pp, s.IDs))
}
// if !pp.Has(s.Ids[0]) {
// t.Fatalf("missing peer test-0: %v (%v)", pp, s.Ids)
// if !pp.Has(s.IDs[0]) {
// t.Fatalf("missing peer test-0: %v (%v)", pp, s.IDs)
// }
if !pp.Has(s.Ids[1]) {
t.Fatalf("missing peer test-1: %v (%v)", pp, s.Ids)
if !pp.Has(s.IDs[1]) {
t.Fatalf("missing peer test-1: %v (%v)", pp, s.IDs)
}

// sending kill request for peer with index <peer>
s.TestExchanges(p2ptest.Exchange{
Triggers: []p2ptest.Trigger{
p2ptest.Trigger{
Code: 2,
Msg: &kill{s.Ids[peer]},
Peer: s.Ids[0],
Msg: &kill{s.IDs[peer]},
Peer: s.IDs[0],
},
},
})
Expand All @@ -330,19 +331,19 @@ func runMultiplePeers(t *testing.T, peer int, errs ...error) {
p2ptest.Trigger{
Code: 3,
Msg: &drop{},
Peer: s.Ids[(peer+1)%2],
Peer: s.IDs[(peer+1)%2],
},
},
})
// check the actual discconnect errors on the individual peers
var disconnects []*p2ptest.Disconnect
for i, err := range errs {
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.Ids[i], Error: err})
disconnects = append(disconnects, &p2ptest.Disconnect{Peer: s.IDs[i], Error: err})
}
s.TestDisconnected(disconnects...)
// test if disconnected peers have been removed from peerPool
if pp.Has(s.Ids[peer]) {
t.Fatalf("peer test-%v not dropped: %v (%v)", peer, pp, s.Ids)
if pp.Has(s.IDs[peer]) {
t.Fatalf("peer test-%v not dropped: %v (%v)", peer, pp, s.IDs)
}

}
Expand Down
2 changes: 1 addition & 1 deletion p2p/simulations/adapters/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (d *DockerAdapter) NewNode(config *NodeConfig) (Node, error) {

node := &DockerNode{
ExecNode: ExecNode{
ID: config.Id,
ID: config.ID,
Config: conf,
},
}
Expand Down
9 changes: 5 additions & 4 deletions p2p/simulations/adapters/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/rpc"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) {

// create the node directory using the first 12 characters of the ID
// as Unix socket paths cannot be longer than 256 characters
dir := filepath.Join(e.BaseDir, config.Id.String()[:12])
dir := filepath.Join(e.BaseDir, config.ID.String()[:12])
if err := os.Mkdir(dir, 0755); err != nil {
return nil, fmt.Errorf("error creating node directory: %s", err)
}
Expand All @@ -77,7 +78,7 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) {
conf.Stack.P2P.ListenAddr = "127.0.0.1:0"

node := &ExecNode{
ID: config.Id,
ID: config.ID,
Dir: dir,
Config: conf,
}
Expand All @@ -93,7 +94,7 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) {
// (so for example we can run the node in a remote Docker container and
// still communicate with it).
type ExecNode struct {
ID *NodeId
ID discover.NodeID
Dir string
Config *execNodeConfig
Cmd *exec.Cmd
Expand Down Expand Up @@ -265,7 +266,7 @@ func execP2PNode() {

// read the services and ID from argv
serviceNames := strings.Split(os.Args[1], ",")
id := NewNodeIdFromHex(os.Args[2])
id := discover.MustHexID(os.Args[2])

// decode the config
confEnv := os.Getenv("_P2P_NODE_CONFIG")
Expand Down
16 changes: 8 additions & 8 deletions p2p/simulations/adapters/inproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
defer s.mtx.Unlock()

// check a node with the ID doesn't already exist
id := config.Id
if _, exists := s.nodes[id.NodeID]; exists {
id := config.ID
if _, exists := s.nodes[id]; exists {
return nil, fmt.Errorf("node already exists: %s", id)
}

Expand All @@ -75,11 +75,11 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
}

node := &SimNode{
Id: id,
ID: id,
config: config,
adapter: s,
}
s.nodes[id.NodeID] = node
s.nodes[id] = node
return node, nil
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *SimAdapter) GetNode(id discover.NodeID) (*SimNode, bool) {
// by the underlying service.
type SimNode struct {
lock sync.RWMutex
Id *NodeId
ID discover.NodeID
config *NodeConfig
adapter *SimAdapter
node *node.Node
Expand All @@ -129,7 +129,7 @@ func (self *SimNode) Addr() []byte {

// Node returns a discover.Node representing the SimNode
func (self *SimNode) Node() *discover.Node {
return discover.NewNode(self.Id.NodeID, net.IP{127, 0, 0, 1}, 30303, 30303)
return discover.NewNode(self.ID, net.IP{127, 0, 0, 1}, 30303, 30303)
}

// Client returns an rpc.Client which can be used to communicate with the
Expand Down Expand Up @@ -183,7 +183,7 @@ func (self *SimNode) Start(snapshots map[string][]byte) error {
snapshot = snapshots[name]
}
serviceFunc := self.adapter.services[name]
service := serviceFunc(self.Id, snapshot)
service := serviceFunc(self.ID, snapshot)
self.running = append(self.running, service)
return service, nil
}
Expand Down Expand Up @@ -272,7 +272,7 @@ func (self *SimNode) NodeInfo() *p2p.NodeInfo {
server := self.Server()
if server == nil {
return &p2p.NodeInfo{
ID: self.Id.String(),
ID: self.ID.String(),
Enode: self.Node().String(),
}
}
Expand Down
10 changes: 5 additions & 5 deletions p2p/simulations/adapters/rpc_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type rpcMux struct {
type rpcMsg struct {
Method string `json:"method,omitempty"`
Version string `json:"jsonrpc,omitempty"`
Id json.RawMessage `json:"id,omitempty"`
ID json.RawMessage `json:"id,omitempty"`
Payload json.RawMessage `json:"params,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Error json.RawMessage `json:"error,omitempty"`
Expand Down Expand Up @@ -174,7 +174,7 @@ func (mux *rpcMux) newMsg(msg *rpcMsg) *rpcMsg {
mux.idCounter++
mux.msgMap[id] = msg
newMsg := *msg
newMsg.Id = json.RawMessage(strconv.FormatUint(id, 10))
newMsg.ID = json.RawMessage(strconv.FormatUint(id, 10))
return &newMsg
}

Expand All @@ -185,7 +185,7 @@ func (mux *rpcMux) lookup(msg *rpcMsg) *rpcReply {

// if the message has no ID, it is a subscription notification so
// lookup the original subscribe message
if msg.Id == nil {
if msg.ID == nil {
sub := &rpcSub{}
if err := json.Unmarshal(msg.Payload, sub); err != nil {
return nil
Expand All @@ -194,7 +194,7 @@ func (mux *rpcMux) lookup(msg *rpcMsg) *rpcReply {
}

// lookup the original message and restore the ID
id, err := strconv.ParseUint(string(msg.Id), 10, 64)
id, err := strconv.ParseUint(string(msg.ID), 10, 64)
if err != nil {
return nil
}
Expand All @@ -203,7 +203,7 @@ func (mux *rpcMux) lookup(msg *rpcMsg) *rpcReply {
return nil
}
delete(mux.msgMap, id)
msg.Id = origMsg.Id
msg.ID = origMsg.ID

// if the original message was a subscription, store the subscription
// ID so we can detect notifications
Expand Down
Loading

0 comments on commit fe1d6d2

Please sign in to comment.