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

@ -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,
}