Skip to content

Commit 350a87d

Browse files
authored
p2p/discover: add support for EIP-868 (v4 ENR extension) (#19540)
This change implements EIP-868. The UDPv4 transport announces support for the extension in ping/pong and handles enrRequest messages. There are two uses of the extension: If a remote node announces support for EIP-868 in their pong, node revalidation pulls the node's record. The Resolve method requests the record unconditionally.
1 parent 8deec2e commit 350a87d

6 files changed

Lines changed: 406 additions & 136 deletions

File tree

‎p2p/discover/table.go‎

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ const (
5353
bucketIPLimit, bucketSubnet = 2, 24 // at most 2 addresses from the same /24
5454
tableIPLimit, tableSubnet = 10, 24
5555

56-
maxFindnodeFailures = 5 // Nodes exceeding this limit are dropped
57-
refreshInterval = 30 * time.Minute
58-
revalidateInterval = 10 * time.Second
59-
copyNodesInterval = 30 * time.Second
60-
seedMinTableTime = 5 * time.Minute
61-
seedCount = 30
62-
seedMaxAge = 5 * 24 * time.Hour
56+
refreshInterval = 30 * time.Minute
57+
revalidateInterval = 10 * time.Second
58+
copyNodesInterval = 30 * time.Second
59+
seedMinTableTime = 5 * time.Minute
60+
seedCount = 30
61+
seedMaxAge = 5 * 24 * time.Hour
6362
)
6463

64+
// Table is the 'node table', a Kademlia-like index of neighbor nodes. The table keeps
65+
// itself up-to-date by verifying the liveness of neighbors and requesting their node
66+
// records when announcements of a new record version are received.
6567
type Table struct {
6668
mutex sync.Mutex // protects buckets, bucket content, nursery, rand
6769
buckets [nBuckets]*bucket // index of known nodes by distance
@@ -80,12 +82,13 @@ type Table struct {
8082
nodeAddedHook func(*node) // for testing
8183
}
8284

83-
// transport is implemented by UDP transports.
85+
// transport is implemented by the UDP transports.
8486
type transport interface {
8587
Self() *enode.Node
8688
lookupRandom() []*enode.Node
8789
lookupSelf() []*enode.Node
88-
ping(*enode.Node) error
90+
ping(*enode.Node) (seq uint64, err error)
91+
requestENR(*enode.Node) (*enode.Node, error)
8992
}
9093

9194
// bucket contains nodes, ordered by their last activity. the entry
@@ -175,14 +178,16 @@ func (tab *Table) ReadRandomNodes(buf []*enode.Node) (n int) {
175178
return i + 1
176179
}
177180

178-
// Resolve searches for a specific node with the given ID.
179-
// It returns nil if the node could not be found.
180-
func (tab *Table) Resolve(n *enode.Node) *enode.Node {
181+
// getNode returns the node with the given ID or nil if it isn't in the table.
182+
func (tab *Table) getNode(id enode.ID) *enode.Node {
181183
tab.mutex.Lock()
182-
cl := tab.closest(n.ID(), 1, false)
183-
tab.mutex.Unlock()
184-
if len(cl.entries) > 0 && cl.entries[0].ID() == n.ID() {
185-
return unwrapNode(cl.entries[0])
184+
defer tab.mutex.Unlock()
185+
186+
b := tab.bucket(id)
187+
for _, e := range b.entries {
188+
if e.ID() == id {
189+
return unwrapNode(e)
190+
}
186191
}
187192
return nil
188193
}
@@ -226,7 +231,7 @@ func (tab *Table) refresh() <-chan struct{} {
226231
return done
227232
}
228233

229-
// loop schedules refresh, revalidate runs and coordinates shutdown.
234+
// loop schedules runs of doRefresh, doRevalidate and copyLiveNodes.
230235
func (tab *Table) loop() {
231236
var (
232237
revalidate = time.NewTimer(tab.nextRevalidateTime())
@@ -288,9 +293,8 @@ loop:
288293
close(tab.closed)
289294
}
290295

291-
// doRefresh performs a lookup for a random target to keep buckets
292-
// full. seed nodes are inserted if the table is empty (initial
293-
// bootstrap or discarded faulty peers).
296+
// doRefresh performs a lookup for a random target to keep buckets full. seed nodes are
297+
// inserted if the table is empty (initial bootstrap or discarded faulty peers).
294298
func (tab *Table) doRefresh(done chan struct{}) {
295299
defer close(done)
296300

@@ -324,8 +328,8 @@ func (tab *Table) loadSeedNodes() {
324328
}
325329
}
326330

327-
// doRevalidate checks that the last node in a random bucket is still live
328-
// and replaces or deletes the node if it isn't.
331+
// doRevalidate checks that the last node in a random bucket is still live and replaces or
332+
// deletes the node if it isn't.
329333
func (tab *Table) doRevalidate(done chan<- struct{}) {
330334
defer func() { done <- struct{}{} }()
331335

@@ -336,7 +340,17 @@ func (tab *Table) doRevalidate(done chan<- struct{}) {
336340
}
337341

338342
// Ping the selected node and wait for a pong.
339-
err := tab.net.ping(unwrapNode(last))
343+
remoteSeq, err := tab.net.ping(unwrapNode(last))
344+
345+
// Also fetch record if the node replied and returned a higher sequence number.
346+
if last.Seq() < remoteSeq {
347+
n, err := tab.net.requestENR(unwrapNode(last))
348+
if err != nil {
349+
tab.log.Debug("ENR request failed", "id", last.ID(), "addr", last.addr(), "err", err)
350+
} else {
351+
last = &node{Node: *n, addedAt: last.addedAt, livenessChecks: last.livenessChecks}
352+
}
353+
}
340354

341355
tab.mutex.Lock()
342356
defer tab.mutex.Unlock()

‎p2p/discover/table_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,34 @@ func TestTable_addSeenNode(t *testing.T) {
368368
checkIPLimitInvariant(t, tab)
369369
}
370370

371+
// This test checks that ENR updates happen during revalidation. If a node in the table
372+
// announces a new sequence number, the new record should be pulled.
373+
func TestTable_revalidateSyncRecord(t *testing.T) {
374+
transport := newPingRecorder()
375+
tab, db := newTestTable(transport)
376+
<-tab.initDone
377+
defer db.Close()
378+
defer tab.close()
379+
380+
// Insert a node.
381+
var r enr.Record
382+
r.Set(enr.IP(net.IP{127, 0, 0, 1}))
383+
id := enode.ID{1}
384+
n1 := wrapNode(enode.SignNull(&r, id))
385+
tab.addSeenNode(n1)
386+
387+
// Update the node record.
388+
r.Set(enr.WithEntry("foo", "bar"))
389+
n2 := enode.SignNull(&r, id)
390+
transport.updateRecord(n2)
391+
392+
tab.doRevalidate(make(chan struct{}, 1))
393+
intable := tab.getNode(id)
394+
if !reflect.DeepEqual(intable, n2) {
395+
t.Fatalf("table contains old record with seq %d, want seq %d", intable.Seq(), n2.Seq())
396+
}
397+
}
398+
371399
// gen wraps quick.Value so it's easier to use.
372400
// it generates a random value of the given value's type.
373401
func gen(typ interface{}, rand *rand.Rand) interface{} {

‎p2p/discover/table_util_test.go‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func fillTable(tab *Table, nodes []*node) {
9898
type pingRecorder struct {
9999
mu sync.Mutex
100100
dead, pinged map[enode.ID]bool
101+
records map[enode.ID]*enode.Node
101102
n *enode.Node
102103
}
103104

@@ -107,38 +108,53 @@ func newPingRecorder() *pingRecorder {
107108
n := enode.SignNull(&r, enode.ID{})
108109

109110
return &pingRecorder{
110-
dead: make(map[enode.ID]bool),
111-
pinged: make(map[enode.ID]bool),
112-
n: n,
111+
dead: make(map[enode.ID]bool),
112+
pinged: make(map[enode.ID]bool),
113+
records: make(map[enode.ID]*enode.Node),
114+
n: n,
113115
}
114116
}
115117

116-
func (t *pingRecorder) Self() *enode.Node {
117-
return nullNode
118+
// setRecord updates a node record. Future calls to ping and
119+
// requestENR will return this record.
120+
func (t *pingRecorder) updateRecord(n *enode.Node) {
121+
t.mu.Lock()
122+
defer t.mu.Unlock()
123+
t.records[n.ID()] = n
118124
}
119125

120-
func (t *pingRecorder) ping(n *enode.Node) error {
126+
// Stubs to satisfy the transport interface.
127+
func (t *pingRecorder) Self() *enode.Node { return nullNode }
128+
func (t *pingRecorder) lookupSelf() []*enode.Node { return nil }
129+
func (t *pingRecorder) lookupRandom() []*enode.Node { return nil }
130+
func (t *pingRecorder) close() {}
131+
132+
// ping simulates a ping request.
133+
func (t *pingRecorder) ping(n *enode.Node) (seq uint64, err error) {
121134
t.mu.Lock()
122135
defer t.mu.Unlock()
123136

124137
t.pinged[n.ID()] = true
125138
if t.dead[n.ID()] {
126-
return errTimeout
127-
} else {
128-
return nil
139+
return 0, errTimeout
129140
}
141+
if t.records[n.ID()] != nil {
142+
seq = t.records[n.ID()].Seq()
143+
}
144+
return seq, nil
130145
}
131146

132-
func (t *pingRecorder) lookupSelf() []*enode.Node {
133-
return nil
134-
}
147+
// requestENR simulates an ENR request.
148+
func (t *pingRecorder) requestENR(n *enode.Node) (*enode.Node, error) {
149+
t.mu.Lock()
150+
defer t.mu.Unlock()
135151

136-
func (t *pingRecorder) lookupRandom() []*enode.Node {
137-
return nil
152+
if t.dead[n.ID()] || t.records[n.ID()] == nil {
153+
return nil, errTimeout
154+
}
155+
return t.records[n.ID()], nil
138156
}
139157

140-
func (t *pingRecorder) close() {}
141-
142158
func hasDuplicates(slice []*node) bool {
143159
seen := make(map[enode.ID]bool)
144160
for i, e := range slice {

0 commit comments

Comments
 (0)