swarm, p2p: Prerequities for ENR replacing handshake (#19275)

* swarm/api, swarm/network, p2p/simulations: Prerequisites for handshake remove

* swarm, p2p: Add full sim node configs for protocoltester

* swarm/network: Make stream package pass tests

* swarm/network: Extract peer and addr types out of protocol file

* p2p, swarm: Make p2p/protocols tests pass + rename types.go

* swarm/network: Deactivate ExecAdapter test until binary ENR prep

* swarm/api: Remove comments

* swarm/network: Uncomment bootnode record load
This commit is contained in:
lash
2019-03-15 11:27:17 +01:00
committed by Viktor Trón
parent df488975bd
commit 4b4f03ca37
14 changed files with 370 additions and 125 deletions

View File

@@ -27,6 +27,7 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
@@ -144,8 +145,11 @@ func newProtocol(pp *p2ptest.TestPeerPool) func(*p2p.Peer, p2p.MsgReadWriter) er
}
func protocolTester(pp *p2ptest.TestPeerPool) *p2ptest.ProtocolTester {
conf := adapters.RandomNodeConfig()
return p2ptest.NewProtocolTester(conf.ID, 2, newProtocol(pp))
prvkey, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
return p2ptest.NewProtocolTester(prvkey, 2, newProtocol(pp))
}
func protoHandshakeExchange(id enode.ID, proto *protoHandshake) []p2ptest.Exchange {
@@ -260,9 +264,12 @@ func TestProtocolHook(t *testing.T) {
return peer.Run(handle)
}
conf := adapters.RandomNodeConfig()
tester := p2ptest.NewProtocolTester(conf.ID, 2, runFunc)
err := tester.TestExchanges(p2ptest.Exchange{
prvkey, err := crypto.GenerateKey()
if err != nil {
panic(err)
}
tester := p2ptest.NewProtocolTester(prvkey, 2, runFunc)
err = tester.TestExchanges(p2ptest.Exchange{
Expects: []p2ptest.Expect{
{
Code: 0,

View File

@@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/p2p/simulations/pipes"
"github.com/ethereum/go-ethereum/rpc"
)
@@ -71,8 +72,13 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
// check a node with the ID doesn't already exist
id := config.ID
// verify that the node has a private key in the config
if config.PrivateKey == nil {
return nil, fmt.Errorf("node is missing private key: %s", id)
}
// check a node with the ID doesn't already exist
if _, exists := s.nodes[id]; exists {
return nil, fmt.Errorf("node already exists: %s", id)
}
@@ -87,6 +93,24 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
}
}
// dialer in simulations based on ENR records
// doesn't work unless we explicitly set localhost record
ip := enr.IP(net.IPv4(127, 0, 0, 1))
config.Record.Set(&ip)
tcpPort := enr.TCP(0)
config.Record.Set(&tcpPort)
err := enode.SignV4(&config.Record, config.PrivateKey)
if err != nil {
return nil, fmt.Errorf("unable to generate ENR: %v", err)
}
nod, err := enode.New(enode.V4ID{}, &config.Record)
if err != nil {
return nil, fmt.Errorf("unable to create enode: %v", err)
}
log.Trace("simnode new", "record", config.Record)
config.node = nod
n, err := node.New(&node.Config{
P2P: p2p.Config{
PrivateKey: config.PrivateKey,

View File

@@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rpc"
)
@@ -99,6 +100,12 @@ type NodeConfig struct {
// services registered by calling the RegisterService function)
Services []string
// Enode
node *enode.Node
// ENR Record with entries to overwrite
Record enr.Record
// function to sanction or prevent suggesting a peer
Reachable func(id enode.ID) bool
@@ -168,26 +175,27 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
// Node returns the node descriptor represented by the config.
func (n *NodeConfig) Node() *enode.Node {
return enode.NewV4(&n.PrivateKey.PublicKey, net.IP{127, 0, 0, 1}, int(n.Port), int(n.Port))
return n.node
}
// RandomNodeConfig returns node configuration with a randomly generated ID and
// PrivateKey
func RandomNodeConfig() *NodeConfig {
key, err := crypto.GenerateKey()
prvkey, err := crypto.GenerateKey()
if err != nil {
panic("unable to generate key")
}
id := enode.PubkeyToIDV4(&key.PublicKey)
port, err := assignTCPPort()
if err != nil {
panic("unable to assign tcp port")
}
enodId := enode.PubkeyToIDV4(&prvkey.PublicKey)
return &NodeConfig{
ID: id,
Name: fmt.Sprintf("node_%s", id.String()),
PrivateKey: key,
PrivateKey: prvkey,
ID: enodId,
Name: fmt.Sprintf("node_%s", enodId.String()),
Port: port,
EnableMsgEvents: true,
}

View File

@@ -25,6 +25,7 @@ package testing
import (
"bytes"
"crypto/ecdsa"
"fmt"
"io"
"io/ioutil"
@@ -51,7 +52,7 @@ type ProtocolTester struct {
// 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
func NewProtocolTester(id enode.ID, nodeCount int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
func NewProtocolTester(prvkey *ecdsa.PrivateKey, nodeCount int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
services := adapters.Services{
"test": func(ctx *adapters.ServiceContext) (node.Service, error) {
return &testNode{run}, nil
@@ -62,23 +63,30 @@ func NewProtocolTester(id enode.ID, nodeCount int, run func(*p2p.Peer, p2p.MsgRe
}
adapter := adapters.NewSimAdapter(services)
net := simulations.NewNetwork(adapter, &simulations.NetworkConfig{})
if _, err := net.NewNodeWithConfig(&adapters.NodeConfig{
ID: id,
nodeConfig := &adapters.NodeConfig{
PrivateKey: prvkey,
EnableMsgEvents: true,
Services: []string{"test"},
}); err != nil {
}
if _, err := net.NewNodeWithConfig(nodeConfig); err != nil {
panic(err.Error())
}
if err := net.Start(id); err != nil {
if err := net.Start(nodeConfig.ID); err != nil {
panic(err.Error())
}
node := net.GetNode(id).Node.(*adapters.SimNode)
node := net.GetNode(nodeConfig.ID).Node.(*adapters.SimNode)
peers := make([]*adapters.NodeConfig, nodeCount)
nodes := make([]*enode.Node, nodeCount)
for i := 0; i < nodeCount; i++ {
peers[i] = adapters.RandomNodeConfig()
peers[i].Services = []string{"mock"}
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))
}
nodes[i] = peers[i].Node()
}
events := make(chan *p2p.PeerEvent, 1000)
@@ -94,7 +102,7 @@ func NewProtocolTester(id enode.ID, nodeCount int, run func(*p2p.Peer, p2p.MsgRe
network: net,
}
self.Connect(id, peers...)
self.Connect(nodeConfig.ID, peers...)
return self
}
@@ -108,13 +116,6 @@ func (t *ProtocolTester) Stop() {
// p2p/simulations network connection with the in memory network adapter
func (t *ProtocolTester) Connect(selfID enode.ID, peers ...*adapters.NodeConfig) {
for _, peer := range peers {
log.Trace(fmt.Sprintf("start node %v", peer.ID))
if _, err := t.network.NewNodeWithConfig(peer); err != nil {
panic(fmt.Sprintf("error starting peer %v: %v", peer.ID, err))
}
if err := t.network.Start(peer.ID); err != nil {
panic(fmt.Sprintf("error starting peer %v: %v", peer.ID, err))
}
log.Trace(fmt.Sprintf("connect to %v", peer.ID))
if err := t.network.Connect(selfID, peer.ID); err != nil {
panic(fmt.Sprintf("error connecting to peer %v: %v", peer.ID, err))