Skip to content

Commit 4b4f03c

Browse files
nolashzelig
authored andcommitted
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
1 parent df48897 commit 4b4f03c

14 files changed

Lines changed: 370 additions & 125 deletions

File tree

‎p2p/protocols/protocol_test.go‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/ethereum/go-ethereum/rlp"
2929

30+
"github.com/ethereum/go-ethereum/crypto"
3031
"github.com/ethereum/go-ethereum/p2p"
3132
"github.com/ethereum/go-ethereum/p2p/enode"
3233
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
@@ -144,8 +145,11 @@ func newProtocol(pp *p2ptest.TestPeerPool) func(*p2p.Peer, p2p.MsgReadWriter) er
144145
}
145146

146147
func protocolTester(pp *p2ptest.TestPeerPool) *p2ptest.ProtocolTester {
147-
conf := adapters.RandomNodeConfig()
148-
return p2ptest.NewProtocolTester(conf.ID, 2, newProtocol(pp))
148+
prvkey, err := crypto.GenerateKey()
149+
if err != nil {
150+
panic(err)
151+
}
152+
return p2ptest.NewProtocolTester(prvkey, 2, newProtocol(pp))
149153
}
150154

151155
func protoHandshakeExchange(id enode.ID, proto *protoHandshake) []p2ptest.Exchange {
@@ -260,9 +264,12 @@ func TestProtocolHook(t *testing.T) {
260264
return peer.Run(handle)
261265
}
262266

263-
conf := adapters.RandomNodeConfig()
264-
tester := p2ptest.NewProtocolTester(conf.ID, 2, runFunc)
265-
err := tester.TestExchanges(p2ptest.Exchange{
267+
prvkey, err := crypto.GenerateKey()
268+
if err != nil {
269+
panic(err)
270+
}
271+
tester := p2ptest.NewProtocolTester(prvkey, 2, runFunc)
272+
err = tester.TestExchanges(p2ptest.Exchange{
266273
Expects: []p2ptest.Expect{
267274
{
268275
Code: 0,

‎p2p/simulations/adapters/inproc.go‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/node"
2929
"github.com/ethereum/go-ethereum/p2p"
3030
"github.com/ethereum/go-ethereum/p2p/enode"
31+
"github.com/ethereum/go-ethereum/p2p/enr"
3132
"github.com/ethereum/go-ethereum/p2p/simulations/pipes"
3233
"github.com/ethereum/go-ethereum/rpc"
3334
)
@@ -71,8 +72,13 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
7172
s.mtx.Lock()
7273
defer s.mtx.Unlock()
7374

74-
// check a node with the ID doesn't already exist
7575
id := config.ID
76+
// verify that the node has a private key in the config
77+
if config.PrivateKey == nil {
78+
return nil, fmt.Errorf("node is missing private key: %s", id)
79+
}
80+
81+
// check a node with the ID doesn't already exist
7682
if _, exists := s.nodes[id]; exists {
7783
return nil, fmt.Errorf("node already exists: %s", id)
7884
}
@@ -87,6 +93,24 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
8793
}
8894
}
8995

96+
// dialer in simulations based on ENR records
97+
// doesn't work unless we explicitly set localhost record
98+
ip := enr.IP(net.IPv4(127, 0, 0, 1))
99+
config.Record.Set(&ip)
100+
tcpPort := enr.TCP(0)
101+
config.Record.Set(&tcpPort)
102+
103+
err := enode.SignV4(&config.Record, config.PrivateKey)
104+
if err != nil {
105+
return nil, fmt.Errorf("unable to generate ENR: %v", err)
106+
}
107+
nod, err := enode.New(enode.V4ID{}, &config.Record)
108+
if err != nil {
109+
return nil, fmt.Errorf("unable to create enode: %v", err)
110+
}
111+
log.Trace("simnode new", "record", config.Record)
112+
config.node = nod
113+
90114
n, err := node.New(&node.Config{
91115
P2P: p2p.Config{
92116
PrivateKey: config.PrivateKey,

‎p2p/simulations/adapters/types.go‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/node"
3131
"github.com/ethereum/go-ethereum/p2p"
3232
"github.com/ethereum/go-ethereum/p2p/enode"
33+
"github.com/ethereum/go-ethereum/p2p/enr"
3334
"github.com/ethereum/go-ethereum/rpc"
3435
)
3536

@@ -99,6 +100,12 @@ type NodeConfig struct {
99100
// services registered by calling the RegisterService function)
100101
Services []string
101102

103+
// Enode
104+
node *enode.Node
105+
106+
// ENR Record with entries to overwrite
107+
Record enr.Record
108+
102109
// function to sanction or prevent suggesting a peer
103110
Reachable func(id enode.ID) bool
104111

@@ -168,26 +175,27 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
168175

169176
// Node returns the node descriptor represented by the config.
170177
func (n *NodeConfig) Node() *enode.Node {
171-
return enode.NewV4(&n.PrivateKey.PublicKey, net.IP{127, 0, 0, 1}, int(n.Port), int(n.Port))
178+
return n.node
172179
}
173180

174181
// RandomNodeConfig returns node configuration with a randomly generated ID and
175182
// PrivateKey
176183
func RandomNodeConfig() *NodeConfig {
177-
key, err := crypto.GenerateKey()
184+
prvkey, err := crypto.GenerateKey()
178185
if err != nil {
179186
panic("unable to generate key")
180187
}
181188

182-
id := enode.PubkeyToIDV4(&key.PublicKey)
183189
port, err := assignTCPPort()
184190
if err != nil {
185191
panic("unable to assign tcp port")
186192
}
193+
194+
enodId := enode.PubkeyToIDV4(&prvkey.PublicKey)
187195
return &NodeConfig{
188-
ID: id,
189-
Name: fmt.Sprintf("node_%s", id.String()),
190-
PrivateKey: key,
196+
PrivateKey: prvkey,
197+
ID: enodId,
198+
Name: fmt.Sprintf("node_%s", enodId.String()),
191199
Port: port,
192200
EnableMsgEvents: true,
193201
}

‎p2p/testing/protocoltester.go‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package testing
2525

2626
import (
2727
"bytes"
28+
"crypto/ecdsa"
2829
"fmt"
2930
"io"
3031
"io/ioutil"
@@ -51,7 +52,7 @@ type ProtocolTester struct {
5152
// NewProtocolTester constructs a new ProtocolTester
5253
// it takes as argument the pivot node id, the number of dummy peers and the
5354
// protocol run function called on a peer connection by the p2p server
54-
func NewProtocolTester(id enode.ID, nodeCount int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
55+
func NewProtocolTester(prvkey *ecdsa.PrivateKey, nodeCount int, run func(*p2p.Peer, p2p.MsgReadWriter) error) *ProtocolTester {
5556
services := adapters.Services{
5657
"test": func(ctx *adapters.ServiceContext) (node.Service, error) {
5758
return &testNode{run}, nil
@@ -62,23 +63,30 @@ func NewProtocolTester(id enode.ID, nodeCount int, run func(*p2p.Peer, p2p.MsgRe
6263
}
6364
adapter := adapters.NewSimAdapter(services)
6465
net := simulations.NewNetwork(adapter, &simulations.NetworkConfig{})
65-
if _, err := net.NewNodeWithConfig(&adapters.NodeConfig{
66-
ID: id,
66+
nodeConfig := &adapters.NodeConfig{
67+
PrivateKey: prvkey,
6768
EnableMsgEvents: true,
6869
Services: []string{"test"},
69-
}); err != nil {
70+
}
71+
if _, err := net.NewNodeWithConfig(nodeConfig); err != nil {
7072
panic(err.Error())
7173
}
72-
if err := net.Start(id); err != nil {
74+
if err := net.Start(nodeConfig.ID); err != nil {
7375
panic(err.Error())
7476
}
7577

76-
node := net.GetNode(id).Node.(*adapters.SimNode)
78+
node := net.GetNode(nodeConfig.ID).Node.(*adapters.SimNode)
7779
peers := make([]*adapters.NodeConfig, nodeCount)
7880
nodes := make([]*enode.Node, nodeCount)
7981
for i := 0; i < nodeCount; i++ {
8082
peers[i] = adapters.RandomNodeConfig()
8183
peers[i].Services = []string{"mock"}
84+
if _, err := net.NewNodeWithConfig(peers[i]); err != nil {
85+
panic(fmt.Sprintf("error initializing peer %v: %v", peers[i].ID, err))
86+
}
87+
if err := net.Start(peers[i].ID); err != nil {
88+
panic(fmt.Sprintf("error starting peer %v: %v", peers[i].ID, err))
89+
}
8290
nodes[i] = peers[i].Node()
8391
}
8492
events := make(chan *p2p.PeerEvent, 1000)
@@ -94,7 +102,7 @@ func NewProtocolTester(id enode.ID, nodeCount int, run func(*p2p.Peer, p2p.MsgRe
94102
network: net,
95103
}
96104

97-
self.Connect(id, peers...)
105+
self.Connect(nodeConfig.ID, peers...)
98106

99107
return self
100108
}
@@ -108,13 +116,6 @@ func (t *ProtocolTester) Stop() {
108116
// p2p/simulations network connection with the in memory network adapter
109117
func (t *ProtocolTester) Connect(selfID enode.ID, peers ...*adapters.NodeConfig) {
110118
for _, peer := range peers {
111-
log.Trace(fmt.Sprintf("start node %v", peer.ID))
112-
if _, err := t.network.NewNodeWithConfig(peer); err != nil {
113-
panic(fmt.Sprintf("error starting peer %v: %v", peer.ID, err))
114-
}
115-
if err := t.network.Start(peer.ID); err != nil {
116-
panic(fmt.Sprintf("error starting peer %v: %v", peer.ID, err))
117-
}
118119
log.Trace(fmt.Sprintf("connect to %v", peer.ID))
119120
if err := t.network.Connect(selfID, peer.ID); err != nil {
120121
panic(fmt.Sprintf("error connecting to peer %v: %v", peer.ID, err))

‎swarm/api/config.go‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/ethereum/go-ethereum/common"
27+
"github.com/ethereum/go-ethereum/common/hexutil"
2728
"github.com/ethereum/go-ethereum/contracts/ens"
2829
"github.com/ethereum/go-ethereum/crypto"
2930
"github.com/ethereum/go-ethereum/node"
@@ -47,9 +48,8 @@ type Config struct {
4748
*storage.FileStoreParams
4849
*storage.LocalStoreParams
4950
*network.HiveParams
50-
Swap *swap.LocalProfile
51-
Pss *pss.PssParams
52-
//*network.SyncParams
51+
Swap *swap.LocalProfile
52+
Pss *pss.PssParams
5353
Contract common.Address
5454
EnsRoot common.Address
5555
EnsAPIs []string
@@ -79,10 +79,9 @@ type Config struct {
7979
func NewConfig() (c *Config) {
8080

8181
c = &Config{
82-
LocalStoreParams: storage.NewDefaultLocalStoreParams(),
83-
FileStoreParams: storage.NewFileStoreParams(),
84-
HiveParams: network.NewHiveParams(),
85-
//SyncParams: network.NewDefaultSyncParams(),
82+
LocalStoreParams: storage.NewDefaultLocalStoreParams(),
83+
FileStoreParams: storage.NewFileStoreParams(),
84+
HiveParams: network.NewHiveParams(),
8685
Swap: swap.NewDefaultSwapParams(),
8786
Pss: pss.NewPssParams(),
8887
ListenAddr: DefaultHTTPListenAddr,
@@ -117,7 +116,7 @@ func (c *Config) Init(prvKey *ecdsa.PrivateKey) {
117116

118117
pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
119118
pubkeyhex := common.ToHex(pubkey)
120-
keyhex := crypto.Keccak256Hash(pubkey).Hex()
119+
keyhex := hexutil.Encode(network.PrivateKeyToBzzKey(prvKey))
121120

122121
c.PublicKey = pubkeyhex
123122
c.BzzKey = keyhex

‎swarm/network/discovery_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import (
2929
*/
3030
func TestDiscovery(t *testing.T) {
3131
params := NewHiveParams()
32-
s, pp := newHiveTester(t, params, 1, nil)
32+
s, pp, err := newHiveTester(t, params, 1, nil)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
3336

3437
node := s.Nodes[0]
3538
raddr := NewAddr(node)
@@ -40,7 +43,7 @@ func TestDiscovery(t *testing.T) {
4043
defer pp.Stop()
4144

4245
// send subPeersMsg to the peer
43-
err := s.TestExchanges(p2ptest.Exchange{
46+
err = s.TestExchanges(p2ptest.Exchange{
4447
Label: "outgoing subPeersMsg",
4548
Expects: []p2ptest.Expect{
4649
{

‎swarm/network/enr.go‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package network
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/ethereum/go-ethereum/p2p"
8+
"github.com/ethereum/go-ethereum/p2p/enode"
9+
"github.com/ethereum/go-ethereum/p2p/protocols"
10+
"github.com/ethereum/go-ethereum/rlp"
11+
"github.com/ethereum/go-ethereum/swarm/log"
12+
)
13+
14+
// ENRAddrEntry is the entry type to store the bzz key in the enode
15+
type ENRAddrEntry struct {
16+
data []byte
17+
}
18+
19+
func NewENRAddrEntry(addr []byte) *ENRAddrEntry {
20+
return &ENRAddrEntry{
21+
data: addr,
22+
}
23+
}
24+
25+
func (b ENRAddrEntry) Address() []byte {
26+
return b.data
27+
}
28+
29+
// ENRKey implements enr.Entry
30+
func (b ENRAddrEntry) ENRKey() string {
31+
return "bzzkey"
32+
}
33+
34+
// EncodeRLP implements rlp.Encoder
35+
func (b ENRAddrEntry) EncodeRLP(w io.Writer) error {
36+
log.Debug("in encoderlp", "b", b, "p", fmt.Sprintf("%p", &b))
37+
return rlp.Encode(w, &b.data)
38+
}
39+
40+
// DecodeRLP implements rlp.Decoder
41+
func (b *ENRAddrEntry) DecodeRLP(s *rlp.Stream) error {
42+
byt, err := s.Bytes()
43+
if err != nil {
44+
return err
45+
}
46+
b.data = byt
47+
log.Debug("in decoderlp", "b", b, "p", fmt.Sprintf("%p", &b))
48+
return nil
49+
}
50+
51+
type ENRLightNodeEntry bool
52+
53+
func (b ENRLightNodeEntry) ENRKey() string {
54+
return "bzzlightnode"
55+
}
56+
57+
type ENRBootNodeEntry bool
58+
59+
func (b ENRBootNodeEntry) ENRKey() string {
60+
return "bzzbootnode"
61+
}
62+
63+
func getENRBzzPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *protocols.Spec) *BzzPeer {
64+
var lightnode ENRLightNodeEntry
65+
var bootnode ENRBootNodeEntry
66+
67+
// retrieve the ENR Record data
68+
record := p.Node().Record()
69+
record.Load(&lightnode)
70+
record.Load(&bootnode)
71+
72+
// get the address; separate function as long as we need swarm/network:NewAddr() to call it
73+
addr := getENRBzzAddr(p.Node())
74+
75+
// build the peer using the retrieved data
76+
return &BzzPeer{
77+
Peer: protocols.NewPeer(p, rw, spec),
78+
LightNode: bool(lightnode),
79+
BzzAddr: addr,
80+
}
81+
}
82+
83+
func getENRBzzAddr(nod *enode.Node) *BzzAddr {
84+
var addr ENRAddrEntry
85+
86+
record := nod.Record()
87+
record.Load(&addr)
88+
89+
return &BzzAddr{
90+
OAddr: addr.data,
91+
UAddr: []byte(nod.String()),
92+
}
93+
}

0 commit comments

Comments
 (0)