Skip to content

Commit 30cd5c1

Browse files
authored
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.
1 parent 0ae462f commit 30cd5c1

130 files changed

Lines changed: 3018 additions & 3055 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/bootnode/main.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/log"
3030
"github.com/ethereum/go-ethereum/p2p/discover"
3131
"github.com/ethereum/go-ethereum/p2p/discv5"
32+
"github.com/ethereum/go-ethereum/p2p/enode"
3233
"github.com/ethereum/go-ethereum/p2p/nat"
3334
"github.com/ethereum/go-ethereum/p2p/netutil"
3435
)
@@ -85,7 +86,7 @@ func main() {
8586
}
8687

8788
if *writeAddr {
88-
fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
89+
fmt.Printf("%v\n", enode.PubkeyToIDV4(&nodeKey.PublicKey))
8990
os.Exit(0)
9091
}
9192

‎cmd/faucet/faucet.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ import (
5454
"github.com/ethereum/go-ethereum/log"
5555
"github.com/ethereum/go-ethereum/node"
5656
"github.com/ethereum/go-ethereum/p2p"
57-
"github.com/ethereum/go-ethereum/p2p/discover"
5857
"github.com/ethereum/go-ethereum/p2p/discv5"
58+
"github.com/ethereum/go-ethereum/p2p/enode"
5959
"github.com/ethereum/go-ethereum/p2p/nat"
6060
"github.com/ethereum/go-ethereum/params"
6161
"golang.org/x/net/websocket"
@@ -255,8 +255,10 @@ func newFaucet(genesis *core.Genesis, port int, enodes []*discv5.Node, network u
255255
return nil, err
256256
}
257257
for _, boot := range enodes {
258-
old, _ := discover.ParseNode(boot.String())
259-
stack.Server().AddPeer(old)
258+
old, err := enode.ParseV4(boot.String())
259+
if err != nil {
260+
stack.Server().AddPeer(old)
261+
}
260262
}
261263
// Attach to the client and retrieve and interesting metadatas
262264
api, err := stack.Attach()

‎cmd/p2psim/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747

4848
"github.com/ethereum/go-ethereum/crypto"
4949
"github.com/ethereum/go-ethereum/p2p"
50-
"github.com/ethereum/go-ethereum/p2p/discover"
50+
"github.com/ethereum/go-ethereum/p2p/enode"
5151
"github.com/ethereum/go-ethereum/p2p/simulations"
5252
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
5353
"github.com/ethereum/go-ethereum/rpc"
@@ -285,7 +285,7 @@ func createNode(ctx *cli.Context) error {
285285
if err != nil {
286286
return err
287287
}
288-
config.ID = discover.PubkeyID(&privKey.PublicKey)
288+
config.ID = enode.PubkeyToIDV4(&privKey.PublicKey)
289289
config.PrivateKey = privKey
290290
}
291291
if services := ctx.String("services"); services != "" {

‎cmd/swarm/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"github.com/ethereum/go-ethereum/internal/debug"
3939
"github.com/ethereum/go-ethereum/log"
4040
"github.com/ethereum/go-ethereum/node"
41-
"github.com/ethereum/go-ethereum/p2p/discover"
41+
"github.com/ethereum/go-ethereum/p2p/enode"
4242
"github.com/ethereum/go-ethereum/swarm"
4343
bzzapi "github.com/ethereum/go-ethereum/swarm/api"
4444
swarmmetrics "github.com/ethereum/go-ethereum/swarm/metrics"
@@ -788,10 +788,10 @@ func setSwarmBootstrapNodes(ctx *cli.Context, cfg *node.Config) {
788788
return
789789
}
790790

791-
cfg.P2P.BootstrapNodes = []*discover.Node{}
791+
cfg.P2P.BootstrapNodes = []*enode.Node{}
792792

793793
for _, url := range SwarmBootnodes {
794-
node, err := discover.ParseNode(url)
794+
node, err := enode.ParseV4(url)
795795
if err != nil {
796796
log.Error("Bootstrap URL invalid", "enode", url, "err", err)
797797
}

‎cmd/swarm/run_test.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,15 @@ func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {
234234
// start the node
235235
node.Cmd = runSwarm(t,
236236
"--port", p2pPort,
237+
"--nat", "extip:127.0.0.1",
237238
"--nodiscover",
238239
"--datadir", dir,
239240
"--ipcpath", conf.IPCPath,
240241
"--ens-api", "",
241242
"--bzzaccount", bzzaccount,
242243
"--bzznetworkid", "321",
243244
"--bzzport", httpPort,
244-
"--verbosity", "6",
245+
"--verbosity", "3",
245246
)
246247
node.Cmd.InputLine(testPassphrase)
247248
defer func() {
@@ -284,8 +285,8 @@ func existingTestNode(t *testing.T, dir string, bzzaccount string) *testNode {
284285
if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
285286
t.Fatal(err)
286287
}
287-
node.Enode = fmt.Sprintf("enode://%s@127.0.0.1:%s", nodeInfo.ID, p2pPort)
288-
288+
node.Enode = nodeInfo.Enode
289+
node.IpcPath = conf.IPCPath
289290
return node
290291
}
291292

@@ -309,14 +310,15 @@ func newTestNode(t *testing.T, dir string) *testNode {
309310
// start the node
310311
node.Cmd = runSwarm(t,
311312
"--port", p2pPort,
313+
"--nat", "extip:127.0.0.1",
312314
"--nodiscover",
313315
"--datadir", dir,
314316
"--ipcpath", conf.IPCPath,
315317
"--ens-api", "",
316318
"--bzzaccount", account.Address.String(),
317319
"--bzznetworkid", "321",
318320
"--bzzport", httpPort,
319-
"--verbosity", "6",
321+
"--verbosity", "3",
320322
)
321323
node.Cmd.InputLine(testPassphrase)
322324
defer func() {
@@ -359,9 +361,8 @@ func newTestNode(t *testing.T, dir string) *testNode {
359361
if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil {
360362
t.Fatal(err)
361363
}
362-
node.Enode = fmt.Sprintf("enode://%s@127.0.0.1:%s", nodeInfo.ID, p2pPort)
364+
node.Enode = nodeInfo.Enode
363365
node.IpcPath = conf.IPCPath
364-
365366
return node
366367
}
367368

‎cmd/utils/flags.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ import (
5151
"github.com/ethereum/go-ethereum/metrics/influxdb"
5252
"github.com/ethereum/go-ethereum/node"
5353
"github.com/ethereum/go-ethereum/p2p"
54-
"github.com/ethereum/go-ethereum/p2p/discover"
5554
"github.com/ethereum/go-ethereum/p2p/discv5"
55+
"github.com/ethereum/go-ethereum/p2p/enode"
5656
"github.com/ethereum/go-ethereum/p2p/nat"
5757
"github.com/ethereum/go-ethereum/p2p/netutil"
5858
"github.com/ethereum/go-ethereum/params"
@@ -692,9 +692,9 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
692692
return // already set, don't apply defaults.
693693
}
694694

695-
cfg.BootstrapNodes = make([]*discover.Node, 0, len(urls))
695+
cfg.BootstrapNodes = make([]*enode.Node, 0, len(urls))
696696
for _, url := range urls {
697-
node, err := discover.ParseNode(url)
697+
node, err := enode.ParseV4(url)
698698
if err != nil {
699699
log.Crit("Bootstrap URL invalid", "enode", url, "err", err)
700700
}

‎cmd/wnode/main.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import (
4141
"github.com/ethereum/go-ethereum/crypto"
4242
"github.com/ethereum/go-ethereum/log"
4343
"github.com/ethereum/go-ethereum/p2p"
44-
"github.com/ethereum/go-ethereum/p2p/discover"
44+
"github.com/ethereum/go-ethereum/p2p/enode"
4545
"github.com/ethereum/go-ethereum/p2p/nat"
4646
"github.com/ethereum/go-ethereum/whisper/mailserver"
4747
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
@@ -175,7 +175,7 @@ func initialize() {
175175
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*argVerbosity), log.StreamHandler(os.Stderr, log.TerminalFormat(false))))
176176

177177
done = make(chan struct{})
178-
var peers []*discover.Node
178+
var peers []*enode.Node
179179
var err error
180180

181181
if *generateKey {
@@ -203,7 +203,7 @@ func initialize() {
203203
if len(*argEnode) == 0 {
204204
argEnode = scanLineA("Please enter the peer's enode: ")
205205
}
206-
peer := discover.MustParseNode(*argEnode)
206+
peer := enode.MustParseV4(*argEnode)
207207
peers = append(peers, peer)
208208
}
209209

@@ -747,11 +747,11 @@ func requestExpiredMessagesLoop() {
747747
}
748748

749749
func extractIDFromEnode(s string) []byte {
750-
n, err := discover.ParseNode(s)
750+
n, err := enode.ParseV4(s)
751751
if err != nil {
752752
utils.Fatalf("Failed to parse enode: %s", err)
753753
}
754-
return n.ID[:]
754+
return n.ID().Bytes()
755755
}
756756

757757
// obfuscateBloom adds 16 random bits to the bloom

‎eth/handler.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"github.com/ethereum/go-ethereum/event"
3838
"github.com/ethereum/go-ethereum/log"
3939
"github.com/ethereum/go-ethereum/p2p"
40-
"github.com/ethereum/go-ethereum/p2p/discover"
40+
"github.com/ethereum/go-ethereum/p2p/enode"
4141
"github.com/ethereum/go-ethereum/params"
4242
"github.com/ethereum/go-ethereum/rlp"
4343
)
@@ -147,7 +147,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
147147
NodeInfo: func() interface{} {
148148
return manager.NodeInfo()
149149
},
150-
PeerInfo: func(id discover.NodeID) interface{} {
150+
PeerInfo: func(id enode.ID) interface{} {
151151
if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
152152
return p.Info()
153153
}

‎eth/helper_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"github.com/ethereum/go-ethereum/ethdb"
3838
"github.com/ethereum/go-ethereum/event"
3939
"github.com/ethereum/go-ethereum/p2p"
40-
"github.com/ethereum/go-ethereum/p2p/discover"
40+
"github.com/ethereum/go-ethereum/p2p/enode"
4141
"github.com/ethereum/go-ethereum/params"
4242
)
4343

@@ -148,7 +148,7 @@ func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*te
148148
app, net := p2p.MsgPipe()
149149

150150
// Generate a random id and create the peer
151-
var id discover.NodeID
151+
var id enode.ID
152152
rand.Read(id[:])
153153

154154
peer := pm.newPeer(version, p2p.NewPeer(id, name, nil), net)

‎eth/sync.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/core/types"
2626
"github.com/ethereum/go-ethereum/eth/downloader"
2727
"github.com/ethereum/go-ethereum/log"
28-
"github.com/ethereum/go-ethereum/p2p/discover"
28+
"github.com/ethereum/go-ethereum/p2p/enode"
2929
)
3030

3131
const (
@@ -64,7 +64,7 @@ func (pm *ProtocolManager) syncTransactions(p *peer) {
6464
// the transactions in small packs to one peer at a time.
6565
func (pm *ProtocolManager) txsyncLoop() {
6666
var (
67-
pending = make(map[discover.NodeID]*txsync)
67+
pending = make(map[enode.ID]*txsync)
6868
sending = false // whether a send is active
6969
pack = new(txsync) // the pack that is being sent
7070
done = make(chan error, 1) // result of the send

0 commit comments

Comments
 (0)