p2p/discover: reset isValidatedLive on inbound endpoint update

This commit is contained in:
Felix Lange 2024-05-27 17:25:41 +02:00
parent 413bdb02c2
commit 41141b5680

View file

@ -513,9 +513,12 @@ func (tab *Table) handleAddNode(req addNodeOp) bool {
} }
b := tab.bucket(req.node.ID()) b := tab.bucket(req.node.ID())
exists, _ := tab.bumpInBucket(b, req.node.Node, req.isInbound) n, wasUpdated := tab.bumpInBucket(b, req.node.Node, req.isInbound)
if exists { if n != nil {
// Already in bucket. // Already in bucket.
if wasUpdated {
n.isValidatedLive = false
}
return false return false
} }
if len(b.entries) >= bucketSize { if len(b.entries) >= bucketSize {
@ -607,36 +610,36 @@ func (tab *Table) deleteInBucket(b *bucket, id enode.ID) *node {
} }
// bumpInBucket updates a node record if it exists in the bucket. // bumpInBucket updates a node record if it exists in the bucket.
func (tab *Table) bumpInBucket(b *bucket, newRecord *enode.Node, isInbound bool) (exists, updated bool) { func (tab *Table) bumpInBucket(b *bucket, newRecord *enode.Node, isInbound bool) (n *node, updated bool) {
i := slices.IndexFunc(b.entries, func(elem *node) bool { i := slices.IndexFunc(b.entries, func(elem *node) bool {
return elem.ID() == newRecord.ID() return elem.ID() == newRecord.ID()
}) })
if i == -1 { if i == -1 {
return false, false // node not in bucket return nil, false // node not in bucket
} }
// Disallow updates unless the sequence number is increased. // Disallow updates unless the sequence number is increased.
// Note there is a special case for discv4: if the node contacts us (isInbound), // Note there is a special case for discv4: if the node contacts us (isInbound),
// it is allowed to update its own entry. // it is allowed to update its own entry.
oldRecord := b.entries[i] n = b.entries[i]
isUpdate := newRecord.Seq() > oldRecord.Seq() isUpdate := newRecord.Seq() > n.Seq()
isDiscv4Update := oldRecord.Seq() == 0 && newRecord.Seq() == 0 && isInbound isDiscv4Update := n.Seq() == 0 && newRecord.Seq() == 0 && isInbound
if !(isUpdate || isDiscv4Update) { if !(isUpdate || isDiscv4Update) {
return true, false return n, false
} }
// Check if there is an endpoint update and validate against IP limits. // Check if there is an endpoint update and validate against IP limits.
if newRecord.IPAddr() != b.entries[i].IPAddr() { if newRecord.IPAddr() != b.entries[i].IPAddr() {
tab.removeIP(b, oldRecord.IP()) tab.removeIP(b, n.IP())
if !tab.addIP(b, newRecord.IP()) { if !tab.addIP(b, newRecord.IP()) {
// It doesn't fit with the limit, put the previous record back. // It doesn't fit with the limit, put the previous record back.
tab.addIP(b, oldRecord.IP()) tab.addIP(b, n.IP())
return true, false return n, false
} }
} }
b.entries[i].Node = newRecord n.Node = newRecord
return true, true return n, true
} }
func (tab *Table) handleTrackRequest(op trackRequestOp) { func (tab *Table) handleTrackRequest(op trackRequestOp) {