@@ -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.
6567type 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.
8486type 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 .
230235func (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).
294298func (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.
329333func (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 ()
0 commit comments