Skip to content

Commit

Permalink
p2p/discover: add config option for disabling FINDNODE liveness check (
Browse files Browse the repository at this point in the history
…#30512)

This is for fixing Prysm integration tests.
  • Loading branch information
fjl authored Sep 30, 2024
1 parent 283be23 commit 6b61b54
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
7 changes: 4 additions & 3 deletions p2p/discover/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ type Config struct {
Unhandled chan<- ReadPacket // unhandled packets are sent on this channel

// Node table configuration:
Bootnodes []*enode.Node // list of bootstrap nodes
PingInterval time.Duration // speed of node liveness check
RefreshInterval time.Duration // used in bucket refresh
Bootnodes []*enode.Node // list of bootstrap nodes
PingInterval time.Duration // speed of node liveness check
RefreshInterval time.Duration // used in bucket refresh
NoFindnodeLivenessCheck bool // turns off validation of table nodes in FINDNODE handler

// The options below are useful in very specific cases, like in unit tests.
V5ProtocolID *[6]byte
Expand Down
6 changes: 3 additions & 3 deletions p2p/discover/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ func (tab *Table) findnodeByID(target enode.ID, nresults int, preferLive bool) *
return nodes
}

// appendLiveNodes adds nodes at the given distance to the result slice.
// appendBucketNodes adds nodes at the given distance to the result slice.
// This is used by the FINDNODE/v5 handler.
func (tab *Table) appendLiveNodes(dist uint, result []*enode.Node) []*enode.Node {
func (tab *Table) appendBucketNodes(dist uint, result []*enode.Node, checkLive bool) []*enode.Node {
if dist > 256 {
return result
}
Expand All @@ -280,7 +280,7 @@ func (tab *Table) appendLiveNodes(dist uint, result []*enode.Node) []*enode.Node

tab.mutex.Lock()
for _, n := range tab.bucketAtDistance(int(dist)).entries {
if n.isValidatedLive {
if !checkLive || n.isValidatedLive {
result = append(result, n.Node)
}
}
Expand Down
3 changes: 2 additions & 1 deletion p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,8 @@ func (t *UDPv4) handleFindnode(h *packetHandlerV4, from netip.AddrPort, fromID e

// Determine closest nodes.
target := enode.ID(crypto.Keccak256Hash(req.Target[:]))
closest := t.tab.findnodeByID(target, bucketSize, true).entries
preferLive := !t.tab.cfg.NoFindnodeLivenessCheck
closest := t.tab.findnodeByID(target, bucketSize, preferLive).entries

// Send neighbors in chunks with at most maxNeighbors per packet
// to stay below the packet size limit.
Expand Down
3 changes: 2 additions & 1 deletion p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,8 @@ func (t *UDPv5) collectTableNodes(rip netip.Addr, distances []uint, limit int) [
}
processed[dist] = struct{}{}

for _, n := range t.tab.appendLiveNodes(dist, bn[:0]) {
checkLive := !t.tab.cfg.NoFindnodeLivenessCheck
for _, n := range t.tab.appendBucketNodes(dist, bn[:0], checkLive) {
// Apply some pre-checks to avoid sending invalid nodes.
// Note liveness is checked by appendLiveNodes.
if netutil.CheckRelayAddr(rip, n.IPAddr()) != nil {
Expand Down

0 comments on commit 6b61b54

Please sign in to comment.