p2p/discover: rename add node methods

This is to better reflect their purpose. The previous naming of 'seen' and 'verified'
was kind of arbitrary, especially since 'verified' was the stricter one.
This commit is contained in:
Felix Lange 2024-04-22 16:51:12 +02:00
parent 041ce1b7b3
commit 2c2c7f7a07
7 changed files with 32 additions and 30 deletions

View file

@ -165,7 +165,7 @@ func (it *lookup) query(n *node, reply chan<- []*node) {
// Grab as many nodes as possible. Some of them might not be alive anymore, but we'll // Grab as many nodes as possible. Some of them might not be alive anymore, but we'll
// just remove those again during revalidation. // just remove those again during revalidation.
for _, n := range r { for _, n := range r {
it.tab.addSeenNode(n) it.tab.addFoundNode(n)
} }
reply <- r reply <- r
} }

View file

@ -109,7 +109,7 @@ type bucket struct {
type addNodeRequest struct { type addNodeRequest struct {
node *node node *node
isLive bool isInbound bool
} }
func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) { func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) {
@ -300,13 +300,13 @@ func (tab *Table) len() (n int) {
return n return n
} }
// addSeenNode adds a node which may not be live. If the bucket has space available, // addFoundNode adds a node which may not be live. If the bucket has space available,
// adding the node succeeds immediately. Otherwise, the node is added to the replacements // adding the node succeeds immediately. Otherwise, the node is added to the replacements
// list. // list.
// //
// The caller must not hold tab.mutex. // The caller must not hold tab.mutex.
func (tab *Table) addSeenNode(n *node) { func (tab *Table) addFoundNode(n *node) {
req := addNodeRequest{node: n, isLive: false} req := addNodeRequest{node: n, isInbound: false}
select { select {
case tab.addNodeCh <- req: case tab.addNodeCh <- req:
<-tab.addNodeHandled <-tab.addNodeHandled
@ -314,16 +314,16 @@ func (tab *Table) addSeenNode(n *node) {
} }
} }
// addVerifiedNode adds a node whose existence has been verified recently. If the bucket // addInboundNode adds a node from an inbound contact. If the bucket has no space, the
// has no space, the node is added to the replacements list. // node is added to the replacements list.
// //
// There is an additional safety measure: if the table is still initializing the node // There is an additional safety measure: if the table is still initializing the node is
// is not added. This prevents an attack where the table could be filled by just sending // not added. This prevents an attack where the table could be filled by just sending ping
// ping repeatedly. // repeatedly.
// //
// The caller must not hold tab.mutex. // The caller must not hold tab.mutex.
func (tab *Table) addVerifiedNode(n *node) { func (tab *Table) addInboundNode(n *node) {
req := addNodeRequest{node: n, isLive: true} req := addNodeRequest{node: n, isInbound: true}
select { select {
case tab.addNodeCh <- req: case tab.addNodeCh <- req:
<-tab.addNodeHandled <-tab.addNodeHandled
@ -435,7 +435,7 @@ func (tab *Table) loadSeedNodes() {
age := time.Since(tab.db.LastPongReceived(seed.ID(), seed.IP())) age := time.Since(tab.db.LastPongReceived(seed.ID(), seed.IP()))
tab.log.Trace("Found seed node in database", "id", seed.ID(), "addr", seed.addr(), "age", age) tab.log.Trace("Found seed node in database", "id", seed.ID(), "addr", seed.addr(), "age", age)
} }
tab.handleAddNode(addNodeRequest{node: seed, isLive: true}) tab.handleAddNode(addNodeRequest{node: seed, isInbound: true})
} }
} }
@ -488,7 +488,9 @@ func (tab *Table) handleAddNode(req addNodeRequest) {
if req.node.ID() == tab.self().ID() { if req.node.ID() == tab.self().ID() {
return return
} }
if !req.isLive && !tab.isInitDone() { // For nodes from inbound contact, there is an additional safety measure: if the table
// is still initializing the node is not added.
if req.isInbound && !tab.isInitDone() {
return return
} }

View file

@ -71,7 +71,7 @@ func testPingReplace(t *testing.T, newNodeIsResponding, lastInBucketIsResponding
// this node in the bucket if it is unresponsive. // this node in the bucket if it is unresponsive.
transport.dead[last.ID()] = !lastInBucketIsResponding transport.dead[last.ID()] = !lastInBucketIsResponding
transport.dead[replacementNode.ID()] = !newNodeIsResponding transport.dead[replacementNode.ID()] = !newNodeIsResponding
tab.addSeenNode(replacementNode) tab.addFoundNode(replacementNode)
// Wait until the last node was pinged. // Wait until the last node was pinged.
waitForRevalidationPing(t, transport, tab, last.ID()) waitForRevalidationPing(t, transport, tab, last.ID())
@ -127,7 +127,7 @@ func TestTable_IPLimit(t *testing.T) {
for i := 0; i < tableIPLimit+1; i++ { for i := 0; i < tableIPLimit+1; i++ {
n := nodeAtDistance(tab.self().ID(), i, net.IP{172, 0, 1, byte(i)}) n := nodeAtDistance(tab.self().ID(), i, net.IP{172, 0, 1, byte(i)})
tab.addSeenNode(n) tab.addFoundNode(n)
} }
if tab.len() > tableIPLimit { if tab.len() > tableIPLimit {
t.Errorf("too many nodes in table") t.Errorf("too many nodes in table")
@ -145,7 +145,7 @@ func TestTable_BucketIPLimit(t *testing.T) {
d := 3 d := 3
for i := 0; i < bucketIPLimit+1; i++ { for i := 0; i < bucketIPLimit+1; i++ {
n := nodeAtDistance(tab.self().ID(), d, net.IP{172, 0, 1, byte(i)}) n := nodeAtDistance(tab.self().ID(), d, net.IP{172, 0, 1, byte(i)})
tab.addSeenNode(n) tab.addFoundNode(n)
} }
if tab.len() > bucketIPLimit { if tab.len() > bucketIPLimit {
t.Errorf("too many nodes in table") t.Errorf("too many nodes in table")
@ -258,8 +258,8 @@ func TestTable_addVerifiedNode(t *testing.T) {
// Insert two nodes. // Insert two nodes.
n1 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 1}) n1 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 1})
n2 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 2}) n2 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 2})
tab.addSeenNode(n1) tab.addFoundNode(n1)
tab.addSeenNode(n2) tab.addFoundNode(n2)
bucket := tab.bucket(n1.ID()) bucket := tab.bucket(n1.ID())
// Verify bucket content: // Verify bucket content:
@ -272,7 +272,7 @@ func TestTable_addVerifiedNode(t *testing.T) {
newrec := n2.Record() newrec := n2.Record()
newrec.Set(enr.IP{99, 99, 99, 99}) newrec.Set(enr.IP{99, 99, 99, 99})
newn2 := wrapNode(enode.SignNull(newrec, n2.ID())) newn2 := wrapNode(enode.SignNull(newrec, n2.ID()))
tab.addVerifiedNode(newn2) tab.addInboundNode(newn2)
// Check that bucket is updated correctly. // Check that bucket is updated correctly.
newBcontent := []*node{n1, newn2} newBcontent := []*node{n1, newn2}
@ -291,8 +291,8 @@ func TestTable_addSeenNode(t *testing.T) {
// Insert two nodes. // Insert two nodes.
n1 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 1}) n1 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 1})
n2 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 2}) n2 := nodeAtDistance(tab.self().ID(), 256, net.IP{88, 77, 66, 2})
tab.addSeenNode(n1) tab.addFoundNode(n1)
tab.addSeenNode(n2) tab.addFoundNode(n2)
// Verify bucket content: // Verify bucket content:
bcontent := []*node{n1, n2} bcontent := []*node{n1, n2}
@ -304,7 +304,7 @@ func TestTable_addSeenNode(t *testing.T) {
newrec := n2.Record() newrec := n2.Record()
newrec.Set(enr.IP{99, 99, 99, 99}) newrec.Set(enr.IP{99, 99, 99, 99})
newn2 := wrapNode(enode.SignNull(newrec, n2.ID())) newn2 := wrapNode(enode.SignNull(newrec, n2.ID()))
tab.addSeenNode(newn2) tab.addFoundNode(newn2)
// Check that bucket content is unchanged. // Check that bucket content is unchanged.
if !reflect.DeepEqual(tab.bucket(n1.ID()).entries, bcontent) { if !reflect.DeepEqual(tab.bucket(n1.ID()).entries, bcontent) {
@ -330,7 +330,7 @@ func TestTable_revalidateSyncRecord(t *testing.T) {
r.Set(enr.IP(net.IP{127, 0, 0, 1})) r.Set(enr.IP(net.IP{127, 0, 0, 1}))
id := enode.ID{1} id := enode.ID{1}
n1 := wrapNode(enode.SignNull(&r, id)) n1 := wrapNode(enode.SignNull(&r, id))
tab.addSeenNode(n1) tab.addFoundNode(n1)
// Update the node record. // Update the node record.
r.Set(enr.WithEntry("foo", "bar")) r.Set(enr.WithEntry("foo", "bar"))

View file

@ -118,7 +118,7 @@ func fillTable(tab *Table, nodes []*node, setLive bool) {
n.livenessChecks = 1 n.livenessChecks = 1
n.isValidatedLive = true n.isValidatedLive = true
} }
tab.addSeenNode(n) tab.addFoundNode(n)
} }
} }

View file

@ -673,10 +673,10 @@ func (t *UDPv4) handlePing(h *packetHandlerV4, from *net.UDPAddr, fromID enode.I
n := wrapNode(enode.NewV4(h.senderKey, from.IP, int(req.From.TCP), from.Port)) n := wrapNode(enode.NewV4(h.senderKey, from.IP, int(req.From.TCP), from.Port))
if time.Since(t.db.LastPongReceived(n.ID(), from.IP)) > bondExpiration { if time.Since(t.db.LastPongReceived(n.ID(), from.IP)) > bondExpiration {
t.sendPing(fromID, from, func() { t.sendPing(fromID, from, func() {
t.tab.addVerifiedNode(n) t.tab.addInboundNode(n)
}) })
} else { } else {
t.tab.addVerifiedNode(n) t.tab.addInboundNode(n)
} }
// Update node database and endpoint predictor. // Update node database and endpoint predictor.

View file

@ -699,7 +699,7 @@ func (t *UDPv5) handlePacket(rawpacket []byte, fromAddr *net.UDPAddr) error {
} }
if fromNode != nil { if fromNode != nil {
// Handshake succeeded, add to table. // Handshake succeeded, add to table.
t.tab.addSeenNode(wrapNode(fromNode)) t.tab.addInboundNode(wrapNode(fromNode))
} }
if packet.Kind() != v5wire.WhoareyouPacket { if packet.Kind() != v5wire.WhoareyouPacket {
// WHOAREYOU logged separately to report errors. // WHOAREYOU logged separately to report errors.

View file

@ -141,7 +141,7 @@ func TestUDPv5_unknownPacket(t *testing.T) {
// Make node known. // Make node known.
n := test.getNode(test.remotekey, test.remoteaddr).Node() n := test.getNode(test.remotekey, test.remoteaddr).Node()
test.table.addSeenNode(wrapNode(n)) test.table.addFoundNode(wrapNode(n))
test.packetIn(&v5wire.Unknown{Nonce: nonce}) test.packetIn(&v5wire.Unknown{Nonce: nonce})
test.waitPacketOut(func(p *v5wire.Whoareyou, addr *net.UDPAddr, _ v5wire.Nonce) { test.waitPacketOut(func(p *v5wire.Whoareyou, addr *net.UDPAddr, _ v5wire.Nonce) {