@@ -112,9 +112,10 @@ type bucket struct {
112112}
113113
114114type addNodeOp struct {
115- node * enode.Node
116- isInbound bool
117- forceSetLive bool // for tests
115+ node * enode.Node
116+ isInbound bool
117+ forceSetLive bool // for tests
118+ syncExecution bool // for tests
118119}
119120
120121type trackRequestOp struct {
@@ -320,7 +321,7 @@ func (tab *Table) len() (n int) {
320321//
321322// The caller must not hold tab.mutex.
322323func (tab * Table ) addFoundNode (n * enode.Node , forceSetLive bool ) bool {
323- op := addNodeOp {node : n , isInbound : false , forceSetLive : forceSetLive }
324+ op := addNodeOp {node : n , isInbound : false , forceSetLive : forceSetLive , syncExecution : true }
324325 select {
325326 case tab .addNodeCh <- op :
326327 return <- tab .addNodeHandled
@@ -337,9 +338,20 @@ func (tab *Table) addFoundNode(n *enode.Node, forceSetLive bool) bool {
337338// repeatedly.
338339//
339340// The caller must not hold tab.mutex.
340- func (tab * Table ) addInboundNode (n * enode.Node ) bool {
341+ func (tab * Table ) addInboundNode (n * enode.Node ) {
341342 op := addNodeOp {node : n , isInbound : true }
342343 select {
344+ case tab .addNodeCh <- op :
345+ return
346+ case <- tab .closeReq :
347+ return
348+ }
349+ }
350+
351+ // Only for testing purposes
352+ func (tab * Table ) addInboundNodeSync (n * enode.Node ) bool {
353+ op := addNodeOp {node : n , isInbound : true , syncExecution : true }
354+ select {
343355 case tab .addNodeCh <- op :
344356 return <- tab .addNodeHandled
345357 case <- tab .closeReq :
@@ -387,10 +399,16 @@ loop:
387399 tab .revalidation .handleResponse (tab , r )
388400
389401 case op := <- tab .addNodeCh :
390- tab .mutex .Lock ()
391- ok := tab .handleAddNode (op )
392- tab .mutex .Unlock ()
393- tab .addNodeHandled <- ok
402+ // only happens in tests
403+ if op .syncExecution {
404+ ok := tab .handleAddNode (op )
405+ tab .addNodeHandled <- ok
406+ } else {
407+ // async execution as handleAddNode is blocking
408+ go func () {
409+ tab .handleAddNode (op )
410+ }()
411+ }
394412
395413 case op := <- tab .trackRequestCh :
396414 tab .handleTrackRequest (op )
@@ -468,9 +486,7 @@ func (tab *Table) loadSeedNodes() {
468486 addr , _ := seed .UDPEndpoint ()
469487 tab .log .Trace ("Found seed node in database" , "id" , seed .ID (), "addr" , addr , "age" , age )
470488 }
471- tab .mutex .Lock ()
472- tab .handleAddNode (addNodeOp {node : seed , isInbound : false })
473- tab .mutex .Unlock ()
489+ go tab .handleAddNode (addNodeOp {node : seed , isInbound : false })
474490 }
475491}
476492
@@ -492,16 +508,15 @@ func (tab *Table) bucketAtDistance(d int) *bucket {
492508 return tab .buckets [d - bucketMinDistance - 1 ]
493509}
494510
495- //nolint:unused
496- func (tab * Table ) filterNode (n * tableNode ) bool {
511+ func (tab * Table ) filterNode (n * enode.Node ) bool {
497512 if tab .enrFilter == nil {
498513 return false
499514 }
500- if node , err := tab .net .RequestENR (n . Node ); err != nil {
501- tab .log .Debug ("ENR request failed" , "id" , n .ID (), "addr " , n .addr (), "err" , err )
515+ if node , err := tab .net .RequestENR (n ); err != nil {
516+ tab .log .Debug ("ENR request failed" , "id" , n .ID (), "ipAddr " , n .IPAddr (), "updPort" , n . UDP (), "err" , err )
502517 return false
503518 } else if ! tab .enrFilter (node .Record ()) {
504- tab .log .Trace ("ENR record filter out" , "id" , n .ID (), "addr " , n .addr ())
519+ tab .log .Trace ("ENR record filter out" , "id" , n .ID (), "ipAddr " , n .IPAddr (), "updPort" , n . UDP ())
505520 return true
506521 }
507522 return false
@@ -541,6 +556,13 @@ func (tab *Table) handleAddNode(req addNodeOp) bool {
541556 return false
542557 }
543558
559+ if tab .filterNode (req .node ) {
560+ return false
561+ }
562+
563+ tab .mutex .Lock ()
564+ defer tab .mutex .Unlock ()
565+
544566 // For nodes from inbound contact, there is an additional safety measure: if the table
545567 // is still initializing the node is not added.
546568 if req .isInbound && ! tab .isInitDone () {
@@ -570,11 +592,6 @@ func (tab *Table) handleAddNode(req addNodeOp) bool {
570592 wn .isValidatedLive = true
571593 }
572594
573- // TODO(Matus): fix the filterNode feature
574- // if tab.filterNode(wn) {
575- // return false
576- // }
577-
578595 b .entries = append (b .entries , wn )
579596 b .replacements = deleteNode (b .replacements , wn .ID ())
580597 tab .nodeAdded (b , wn )
@@ -705,8 +722,6 @@ func (tab *Table) handleTrackRequest(op trackRequestOp) {
705722 }
706723
707724 tab .mutex .Lock ()
708- defer tab .mutex .Unlock ()
709-
710725 b := tab .bucket (op .node .ID ())
711726 // Remove the node from the local table if it fails to return anything useful too
712727 // many times, but only if there are enough other nodes in the bucket. This latter
@@ -715,10 +730,11 @@ func (tab *Table) handleTrackRequest(op trackRequestOp) {
715730 if fails >= maxFindnodeFailures && len (b .entries ) >= bucketSize / 4 {
716731 tab .deleteInBucket (b , op .node .ID ())
717732 }
733+ tab .mutex .Unlock ()
718734
719735 // Add found nodes.
720736 for _ , n := range op .foundNodes {
721- tab .handleAddNode (addNodeOp {n , false , false })
737+ go tab .handleAddNode (addNodeOp {n , false , false , false })
722738 }
723739}
724740
0 commit comments