Skip to content

Commit 1bdf536

Browse files
fjlwanwiset25
authored andcommitted
all: new p2p node representation (ethereum#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 e65b81a commit 1bdf536

Some content is hidden

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

64 files changed

+3277
-2856
lines changed

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/XinFinOrg/XDPoSChain/log"
3030
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
3131
"github.com/XinFinOrg/XDPoSChain/p2p/discv5"
32+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
3233
"github.com/XinFinOrg/XDPoSChain/p2p/nat"
3334
"github.com/XinFinOrg/XDPoSChain/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/XinFinOrg/XDPoSChain/log"
5555
"github.com/XinFinOrg/XDPoSChain/node"
5656
"github.com/XinFinOrg/XDPoSChain/p2p"
57-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
5857
"github.com/XinFinOrg/XDPoSChain/p2p/discv5"
58+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
5959
"github.com/XinFinOrg/XDPoSChain/p2p/nat"
6060
"github.com/XinFinOrg/XDPoSChain/params"
6161
"github.com/gorilla/websocket"
@@ -262,8 +262,10 @@ func newFaucet(genesis *core.Genesis, port int, enodes []*discv5.Node, network u
262262
return nil, err
263263
}
264264
for _, boot := range enodes {
265-
old, _ := discover.ParseNode(boot.String())
266-
stack.Server().AddPeer(old)
265+
old, err := enode.ParseV4(boot.String())
266+
if err != nil {
267+
stack.Server().AddPeer(old)
268+
}
267269
}
268270
// Attach to the client and retrieve and interesting metadatas
269271
api, err := stack.Attach()

cmd/p2psim/main.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919
// Here is an example of creating a 2 node network with the first node
2020
// connected to the second:
2121
//
22-
// $ p2psim node create
23-
// Created node01
22+
// $ p2psim node create
23+
// Created node01
2424
//
25-
// $ p2psim node start node01
26-
// Started node01
25+
// $ p2psim node start node01
26+
// Started node01
2727
//
28-
// $ p2psim node create
29-
// Created node02
28+
// $ p2psim node create
29+
// Created node02
3030
//
31-
// $ p2psim node start node02
32-
// Started node02
33-
//
34-
// $ p2psim node connect node01 node02
35-
// Connected node01 to node02
31+
// $ p2psim node start node02
32+
// Started node02
3633
//
34+
// $ p2psim node connect node01 node02
35+
// Connected node01 to node02
3736
package main
3837

3938
import (
@@ -47,7 +46,7 @@ import (
4746

4847
"github.com/XinFinOrg/XDPoSChain/crypto"
4948
"github.com/XinFinOrg/XDPoSChain/p2p"
50-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
49+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
5150
"github.com/XinFinOrg/XDPoSChain/p2p/simulations"
5251
"github.com/XinFinOrg/XDPoSChain/p2p/simulations/adapters"
5352
"github.com/XinFinOrg/XDPoSChain/rpc"
@@ -283,7 +282,7 @@ func createNode(ctx *cli.Context) error {
283282
if err != nil {
284283
return err
285284
}
286-
config.ID = discover.PubkeyID(&privKey.PublicKey)
285+
config.ID = enode.PubkeyToIDV4(&privKey.PublicKey)
287286
config.PrivateKey = privKey
288287
}
289288
if services := ctx.String("services"); services != "" {

cmd/utils/flags.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import (
4747
"github.com/XinFinOrg/XDPoSChain/metrics/exp"
4848
"github.com/XinFinOrg/XDPoSChain/node"
4949
"github.com/XinFinOrg/XDPoSChain/p2p"
50-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
5150
"github.com/XinFinOrg/XDPoSChain/p2p/discv5"
51+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
5252
"github.com/XinFinOrg/XDPoSChain/p2p/nat"
5353
"github.com/XinFinOrg/XDPoSChain/p2p/netutil"
5454
"github.com/XinFinOrg/XDPoSChain/params"
@@ -681,9 +681,10 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
681681
case ctx.GlobalBool(XDCTestnetFlag.Name):
682682
urls = params.TestnetBootnodes
683683
}
684-
cfg.BootstrapNodes = make([]*discover.Node, 0, len(urls))
684+
685+
cfg.BootstrapNodes = make([]*enode.Node, 0, len(urls))
685686
for _, url := range urls {
686-
node, err := discover.ParseNode(url)
687+
node, err := enode.ParseV4(url)
687688
if err != nil {
688689
log.Error("Bootstrap URL invalid", "enode", url, "err", err)
689690
continue

cmd/wnode/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
"github.com/XinFinOrg/XDPoSChain/crypto"
4141
"github.com/XinFinOrg/XDPoSChain/log"
4242
"github.com/XinFinOrg/XDPoSChain/p2p"
43-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
43+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
4444
"github.com/XinFinOrg/XDPoSChain/p2p/nat"
4545
"github.com/XinFinOrg/XDPoSChain/whisper/mailserver"
4646
whisper "github.com/XinFinOrg/XDPoSChain/whisper/whisperv6"
@@ -174,7 +174,7 @@ func initialize() {
174174
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*argVerbosity), log.StreamHandler(os.Stderr, log.TerminalFormat(false))))
175175

176176
done = make(chan struct{})
177-
var peers []*discover.Node
177+
var peers []*enode.Node
178178
var err error
179179

180180
if *generateKey {
@@ -202,7 +202,7 @@ func initialize() {
202202
if len(*argEnode) == 0 {
203203
argEnode = scanLineA("Please enter the peer's enode: ")
204204
}
205-
peer := discover.MustParseNode(*argEnode)
205+
peer := enode.MustParseV4(*argEnode)
206206
peers = append(peers, peer)
207207
}
208208

@@ -748,11 +748,11 @@ func requestExpiredMessagesLoop() {
748748
}
749749

750750
func extractIDFromEnode(s string) []byte {
751-
n, err := discover.ParseNode(s)
751+
n, err := enode.ParseV4(s)
752752
if err != nil {
753753
utils.Fatalf("Failed to parse enode: %s", err)
754754
}
755-
return n.ID[:]
755+
return n.ID().Bytes()
756756
}
757757

758758
// obfuscateBloom adds 16 random bits to the the bloom

eth/handler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"sync/atomic"
2626
"time"
2727

28-
lru "github.com/hashicorp/golang-lru"
29-
3028
"github.com/XinFinOrg/XDPoSChain/common"
3129
"github.com/XinFinOrg/XDPoSChain/consensus"
3230
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
@@ -40,9 +38,10 @@ import (
4038
"github.com/XinFinOrg/XDPoSChain/event"
4139
"github.com/XinFinOrg/XDPoSChain/log"
4240
"github.com/XinFinOrg/XDPoSChain/p2p"
43-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
41+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
4442
"github.com/XinFinOrg/XDPoSChain/params"
4543
"github.com/XinFinOrg/XDPoSChain/rlp"
44+
lru "github.com/hashicorp/golang-lru"
4645
)
4746

4847
const (
@@ -194,7 +193,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
194193
NodeInfo: func() interface{} {
195194
return manager.NodeInfo()
196195
},
197-
PeerInfo: func(id discover.NodeID) interface{} {
196+
PeerInfo: func(id enode.ID) interface{} {
198197
if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
199198
return p.Info()
200199
}

eth/helper_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"github.com/XinFinOrg/XDPoSChain/ethdb"
4040
"github.com/XinFinOrg/XDPoSChain/event"
4141
"github.com/XinFinOrg/XDPoSChain/p2p"
42-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
42+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
4343
"github.com/XinFinOrg/XDPoSChain/params"
4444
)
4545

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

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

156156
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/XinFinOrg/XDPoSChain/core/types"
2626
"github.com/XinFinOrg/XDPoSChain/eth/downloader"
2727
"github.com/XinFinOrg/XDPoSChain/log"
28-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
28+
"github.com/XinFinOrg/XDPoSChain/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

eth/sync_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"github.com/XinFinOrg/XDPoSChain/eth/downloader"
2525
"github.com/XinFinOrg/XDPoSChain/p2p"
26-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
26+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
2727
)
2828

2929
// Tests that fast sync gets disabled as soon as a real block is successfully
@@ -42,8 +42,8 @@ func TestFastSyncDisabling(t *testing.T) {
4242
// Sync up the two peers
4343
io1, io2 := p2p.MsgPipe()
4444

45-
go pmFull.handle(pmFull.newPeer(63, p2p.NewPeer(discover.NodeID{}, "empty", nil), io2))
46-
go pmEmpty.handle(pmEmpty.newPeer(63, p2p.NewPeer(discover.NodeID{}, "full", nil), io1))
45+
go pmFull.handle(pmFull.newPeer(63, p2p.NewPeer(enode.ID{}, "empty", nil), io2))
46+
go pmEmpty.handle(pmEmpty.newPeer(63, p2p.NewPeer(enode.ID{}, "full", nil), io1))
4747

4848
time.Sleep(250 * time.Millisecond)
4949
pmEmpty.synchronise(pmEmpty.peers.BestPeer())

les/handler.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"errors"
2424
"fmt"
2525
"math/big"
26-
"net"
2726
"sync"
2827
"time"
2928

@@ -40,7 +39,6 @@ import (
4039
"github.com/XinFinOrg/XDPoSChain/light"
4140
"github.com/XinFinOrg/XDPoSChain/log"
4241
"github.com/XinFinOrg/XDPoSChain/p2p"
43-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
4442
"github.com/XinFinOrg/XDPoSChain/p2p/discv5"
4543
"github.com/XinFinOrg/XDPoSChain/params"
4644
"github.com/XinFinOrg/XDPoSChain/rlp"
@@ -167,8 +165,7 @@ func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, protoco
167165
var entry *poolEntry
168166
peer := manager.newPeer(int(version), networkId, p, rw)
169167
if manager.serverPool != nil {
170-
addr := p.RemoteAddr().(*net.TCPAddr)
171-
entry = manager.serverPool.connect(peer, addr.IP, uint16(addr.Port))
168+
entry = manager.serverPool.connect(peer, peer.Node())
172169
}
173170
peer.poolEntry = entry
174171
select {
@@ -190,12 +187,6 @@ func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, protoco
190187
NodeInfo: func() interface{} {
191188
return manager.NodeInfo()
192189
},
193-
PeerInfo: func(id discover.NodeID) interface{} {
194-
if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
195-
return p.Info()
196-
}
197-
return nil
198-
},
199190
})
200191
}
201192
if len(manager.SubProtocols) == 0 {
@@ -396,7 +387,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
396387
}
397388

398389
if p.requestAnnounceType == announceTypeSigned {
399-
if err := req.checkSignature(p.pubKey); err != nil {
390+
if err := req.checkSignature(p.ID()); err != nil {
400391
p.Log().Trace("Invalid announcement signature", "err", err)
401392
return err
402393
}

0 commit comments

Comments
 (0)