Skip to content

Commit 61e56bb

Browse files
authored
Introduce p2p/enode as the generalized node representation for peer and discovery code. The new package becomes the home for node records, local nodes, URL parsing, and the node database, while the v4 identity scheme moves out of p2p/enr to decouple ENR handling from Ethereum-specific crypto. Port discovery, peer management, node APIs, and simulations to enode.Node and enode.ID. The discovery wire protocol stays unchanged, but APIs move away from discover.Node and NodeID and now require explicit record validation. Simulation helpers now track complete nodes and the updated tests cover the new connect and network behavior. These changes align the fork with the upstream geth enode refactor and make later p2p and discovery updates easier to carry. Existing simulation snapshots are incompatible with the new node identifier representation.
1 parent df6c26e commit 61e56bb

73 files changed

Lines changed: 6988 additions & 4865 deletions

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: 21 additions & 8 deletions
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
)
@@ -37,7 +38,7 @@ func main() {
3738
var (
3839
listenAddr = flag.String("addr", ":30301", "listen address")
3940
genKey = flag.String("genkey", "", "generate a node key")
40-
writeAddr = flag.Bool("writeaddress", false, "write out the node's pubkey hash and quit")
41+
writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit")
4142
nodeKeyFile = flag.String("nodekey", "", "private key filename")
4243
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
4344
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)")
@@ -85,7 +86,7 @@ func main() {
8586
}
8687

8788
if *writeAddr {
88-
fmt.Printf("%v\n", discover.PubkeyID(&nodeKey.PublicKey))
89+
fmt.Printf("%x\n", crypto.FromECDSAPub(&nodeKey.PublicKey)[1:])
8990
os.Exit(0)
9091
}
9192

@@ -111,26 +112,38 @@ func main() {
111112
if !realaddr.IP.IsLoopback() {
112113
go nat.Map(natm, nil, "udp", realaddr.Port, realaddr.Port, "ethereum discovery")
113114
}
114-
// TODO: react to external IP changes over time.
115115
if ext, err := natm.ExternalIP(); err == nil {
116116
realaddr = &net.UDPAddr{IP: ext, Port: realaddr.Port}
117117
}
118118
}
119119

120+
printNotice(&nodeKey.PublicKey, *realaddr)
121+
120122
if *runv5 {
121-
if _, err := discv5.ListenUDP(nodeKey, conn, realaddr, "", restrictList); err != nil {
123+
if _, err := discv5.ListenUDP(nodeKey, conn, "", restrictList); err != nil {
122124
utils.Fatalf("%v", err)
123125
}
124126
} else {
127+
db, _ := enode.OpenDB("")
128+
ln := enode.NewLocalNode(db, nodeKey)
125129
cfg := discover.Config{
126-
PrivateKey: nodeKey,
127-
AnnounceAddr: realaddr,
128-
NetRestrict: restrictList,
130+
PrivateKey: nodeKey,
131+
NetRestrict: restrictList,
129132
}
130-
if _, err := discover.ListenUDP(conn, cfg); err != nil {
133+
if _, err := discover.ListenUDP(conn, ln, cfg); err != nil {
131134
utils.Fatalf("%v", err)
132135
}
133136
}
134137

135138
select {}
136139
}
140+
141+
func printNotice(nodeKey *ecdsa.PublicKey, addr net.UDPAddr) {
142+
if addr.IP.IsUnspecified() {
143+
addr.IP = net.IP{127, 0, 0, 1}
144+
}
145+
n := enode.NewV4(nodeKey, addr.IP, 0, addr.Port)
146+
fmt.Println(n.String())
147+
fmt.Println("Note: you're using cmd/bootnode, a developer tool.")
148+
fmt.Println("We recommend using a regular node as bootstrap node for production deployments.")
149+
}

cmd/p2psim/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747
"github.com/XinFinOrg/XDPoSChain/crypto"
4848
"github.com/XinFinOrg/XDPoSChain/internal/flags"
4949
"github.com/XinFinOrg/XDPoSChain/p2p"
50-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
50+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
5151
"github.com/XinFinOrg/XDPoSChain/p2p/simulations"
5252
"github.com/XinFinOrg/XDPoSChain/p2p/simulations/adapters"
5353
"github.com/XinFinOrg/XDPoSChain/rpc"
@@ -300,7 +300,7 @@ func createNode(ctx *cli.Context) error {
300300
if err != nil {
301301
return err
302302
}
303-
config.ID = discover.PubkeyID(&privKey.PublicKey)
303+
config.ID = enode.PubkeyToIDV4(&privKey.PublicKey)
304304
config.PrivateKey = privKey
305305
}
306306
if services := ctx.String("services"); services != "" {

cmd/utils/flags.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ import (
6161
"github.com/XinFinOrg/XDPoSChain/miner"
6262
"github.com/XinFinOrg/XDPoSChain/node"
6363
"github.com/XinFinOrg/XDPoSChain/p2p"
64-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
6564
"github.com/XinFinOrg/XDPoSChain/p2p/discv5"
65+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
6666
"github.com/XinFinOrg/XDPoSChain/p2p/nat"
6767
"github.com/XinFinOrg/XDPoSChain/p2p/netutil"
6868
"github.com/XinFinOrg/XDPoSChain/params"
@@ -956,19 +956,19 @@ func setAllowlistAndDenylistForPeers(ctx *cli.Context, cfg *p2p.Config) {
956956
// setup allowlist for peers
957957
if ctx.IsSet(PeersAllowlistFlag.Name) {
958958
urls := SplitAndTrim(ctx.String(PeersAllowlistFlag.Name))
959-
cfg.AllowPeers = make(map[discover.NodeID]struct{}, len(urls))
959+
cfg.AllowPeers = make(map[enode.ID]struct{}, len(urls))
960960
for _, url := range urls {
961961
if url != "" {
962-
node1, err1 := discover.HexID(url)
962+
node1, err1 := enode.ParseID(url)
963963
if err1 == nil {
964964
cfg.AllowPeers[node1] = struct{}{}
965965
log.Info("Add peer to allowlist", "id", node1)
966966
continue
967967
}
968-
node2, err2 := discover.ParseNode(url)
968+
node2, err2 := enode.ParseV4(url)
969969
if err2 == nil {
970-
cfg.AllowPeers[node2.ID] = struct{}{}
971-
log.Info("Add peer to allowlist", "enode", url, "id", node2.ID)
970+
cfg.AllowPeers[node2.ID()] = struct{}{}
971+
log.Info("Add peer to allowlist", "enode", url, "id", node2.ID())
972972
continue
973973
}
974974
log.Crit("Invalid peer id for allowlist", "url", url, "err1", err1, "err2", err2)
@@ -979,19 +979,19 @@ func setAllowlistAndDenylistForPeers(ctx *cli.Context, cfg *p2p.Config) {
979979
// setup denylist for peers
980980
if ctx.IsSet(PeersDenylistFlag.Name) {
981981
urls := SplitAndTrim(ctx.String(PeersDenylistFlag.Name))
982-
cfg.DenyPeers = make(map[discover.NodeID]struct{}, len(urls))
982+
cfg.DenyPeers = make(map[enode.ID]struct{}, len(urls))
983983
for _, url := range urls {
984984
if url != "" {
985-
node1, err1 := discover.HexID(url)
985+
node1, err1 := enode.ParseID(url)
986986
if err1 == nil {
987987
cfg.DenyPeers[node1] = struct{}{}
988988
log.Info("Add peer to denylist", "id", node1)
989989
continue
990990
}
991-
node2, err2 := discover.ParseNode(url)
991+
node2, err2 := enode.ParseV4(url)
992992
if err2 == nil {
993-
cfg.DenyPeers[node2.ID] = struct{}{}
994-
log.Info("Add peer to denylist", "enode", url, "id", node2.ID)
993+
cfg.DenyPeers[node2.ID()] = struct{}{}
994+
log.Info("Add peer to denylist", "enode", url, "id", node2.ID())
995995
continue
996996
}
997997
log.Crit("Invalid peer id for denylist", "url", url, "err1", err1, "err2", err2)
@@ -1006,10 +1006,10 @@ func removeDenylistedPeers(cfg *p2p.Config) {
10061006
return
10071007
}
10081008

1009-
filteredNodes := make([]*discover.Node, 0, len(cfg.BootstrapNodes))
1009+
filteredNodes := make([]*enode.Node, 0, len(cfg.BootstrapNodes))
10101010
for _, node := range cfg.BootstrapNodes {
1011-
if _, ok := cfg.DenyPeers[node.ID]; ok {
1012-
log.Info("Remove denylisted peer", "enode", node, "id", node.ID)
1011+
if _, ok := cfg.DenyPeers[node.ID()]; ok {
1012+
log.Info("Remove denylisted peer", "enode", node, "id", node.ID())
10131013
continue
10141014
}
10151015
filteredNodes = append(filteredNodes, node)
@@ -1045,11 +1045,11 @@ func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
10451045
cfg.BootstrapNodes = mustParseBootnodes(urls)
10461046
}
10471047

1048-
func mustParseBootnodes(urls []string) []*discover.Node {
1049-
nodes := make([]*discover.Node, 0, len(urls))
1048+
func mustParseBootnodes(urls []string) []*enode.Node {
1049+
nodes := make([]*enode.Node, 0, len(urls))
10501050
for _, url := range urls {
10511051
if url != "" {
1052-
node, err := discover.ParseNode(url)
1052+
node, err := enode.ParseV4(url)
10531053
if err != nil {
10541054
log.Crit("Bootstrap URL invalid", "enode", url, "err", err)
10551055
return nil

eth/handler.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/event"
4040
"github.com/XinFinOrg/XDPoSChain/log"
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
"github.com/XinFinOrg/XDPoSChain/rlp"
4545
)
@@ -184,7 +184,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
184184
NodeInfo: func() interface{} {
185185
return manager.NodeInfo()
186186
},
187-
PeerInfo: func(id discover.NodeID) interface{} {
187+
PeerInfo: func(id enode.ID) interface{} {
188188
if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
189189
return p.Info()
190190
}

eth/helper_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
"github.com/XinFinOrg/XDPoSChain/ethdb"
4141
"github.com/XinFinOrg/XDPoSChain/event"
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/params"
4545
"github.com/holiman/uint256"
4646
)
@@ -206,7 +206,7 @@ func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*te
206206
app, net := p2p.MsgPipe()
207207

208208
// Generate a random id and create the peer
209-
var id discover.NodeID
209+
var id enode.ID
210210
rand.Read(id[:])
211211

212212
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
@@ -26,7 +26,7 @@ import (
2626
"github.com/XinFinOrg/XDPoSChain/core/types"
2727
"github.com/XinFinOrg/XDPoSChain/eth/downloader"
2828
"github.com/XinFinOrg/XDPoSChain/log"
29-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
29+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
3030
)
3131

3232
const (
@@ -69,7 +69,7 @@ func (pm *ProtocolManager) syncTransactions(p *peer) {
6969
// the transactions in small packs to one peer at a time.
7070
func (pm *ProtocolManager) txsyncLoop() {
7171
var (
72-
pending = make(map[discover.NodeID]*txsync)
72+
pending = make(map[enode.ID]*txsync)
7373
sending = false // whether a send is active
7474
pack = new(txsync) // the pack that is being sent
7575
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())

node/api.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/XinFinOrg/XDPoSChain/internal/debug"
2727
"github.com/XinFinOrg/XDPoSChain/log"
2828
"github.com/XinFinOrg/XDPoSChain/p2p"
29-
"github.com/XinFinOrg/XDPoSChain/p2p/discover"
29+
"github.com/XinFinOrg/XDPoSChain/p2p/enode"
3030
"github.com/XinFinOrg/XDPoSChain/rpc"
3131
)
3232

@@ -61,19 +61,19 @@ func (api *adminAPI) AddPeer(url string) (bool, error) {
6161
return false, ErrNodeStopped
6262
}
6363
// Try to add the url as a static peer and return
64-
node, err := discover.ParseNode(url)
64+
node, err := enode.ParseV4(url)
6565
if err != nil {
6666
return false, fmt.Errorf("invalid enode: %v", err)
6767
}
6868
// only accept the node which is in peer allowlist if the list is not empty
6969
if len(server.AllowPeers) > 0 {
70-
if _, ok := server.AllowPeers[node.ID]; !ok {
71-
return false, fmt.Errorf("peer is not in allowlist: %v, ID: %s", url, node.ID)
70+
if _, ok := server.AllowPeers[node.ID()]; !ok {
71+
return false, fmt.Errorf("peer is not in allowlist: %v, ID: %s", url, node.ID())
7272
}
7373
}
7474
// reject the node which is in peer blacklist
75-
if _, ok := server.DenyPeers[node.ID]; ok {
76-
return false, fmt.Errorf("peer is in blacklist: %v, ID: %s", url, node.ID)
75+
if _, ok := server.DenyPeers[node.ID()]; ok {
76+
return false, fmt.Errorf("peer is in blacklist: %v, ID: %s", url, node.ID())
7777
}
7878
server.AddPeer(node)
7979
return true, nil
@@ -87,7 +87,7 @@ func (api *adminAPI) RemovePeer(url string) (bool, error) {
8787
return false, ErrNodeStopped
8888
}
8989
// Try to remove the url as a static peer and return
90-
node, err := discover.ParseNode(url)
90+
node, err := enode.ParseV4(url)
9191
if err != nil {
9292
return false, fmt.Errorf("invalid enode: %v", err)
9393
}
@@ -102,19 +102,19 @@ func (api *adminAPI) AddTrustedPeer(url string) (bool, error) {
102102
if server == nil {
103103
return false, ErrNodeStopped
104104
}
105-
node, err := discover.ParseNode(url)
105+
node, err := enode.ParseV4(url)
106106
if err != nil {
107107
return false, fmt.Errorf("invalid enode: %v", err)
108108
}
109109
// only accept the node which is in peer allowlist if the list is not empty
110110
if len(server.AllowPeers) > 0 {
111-
if _, ok := server.AllowPeers[node.ID]; !ok {
112-
return false, fmt.Errorf("trusted peer is not in allowlist: %v, ID: %s", url, node.ID)
111+
if _, ok := server.AllowPeers[node.ID()]; !ok {
112+
return false, fmt.Errorf("trusted peer is not in allowlist: %v, ID: %s", url, node.ID())
113113
}
114114
}
115115
// reject the node which is in peer blacklist
116-
if _, ok := server.DenyPeers[node.ID]; ok {
117-
return false, fmt.Errorf("trusted peer is in blacklist: %v, ID: %s", url, node.ID)
116+
if _, ok := server.DenyPeers[node.ID()]; ok {
117+
return false, fmt.Errorf("trusted peer is in blacklist: %v, ID: %s", url, node.ID())
118118
}
119119
server.AddTrustedPeer(node)
120120
return true, nil
@@ -128,7 +128,7 @@ func (api *adminAPI) RemoveTrustedPeer(url string) (bool, error) {
128128
if server == nil {
129129
return false, ErrNodeStopped
130130
}
131-
node, err := discover.ParseNode(url)
131+
node, err := enode.ParseV4(url)
132132
if err != nil {
133133
return false, fmt.Errorf("invalid enode: %v", err)
134134
}

0 commit comments

Comments
 (0)