p2p/discover: fix addedAt time

This commit is contained in:
Felix Lange 2024-04-22 16:40:59 +02:00
parent 0048a0f54d
commit 041ce1b7b3
2 changed files with 19 additions and 12 deletions

View file

@ -30,19 +30,21 @@ import (
) )
type BucketNode struct { type BucketNode struct {
Node *enode.Node `json:"node"` Node *enode.Node `json:"node"`
AddedAt time.Time `json:"added"` AddedTable time.Time `json:"addedToTable"`
Checks int `json:"checks"` AddedBucket time.Time `json:"addedToBucket"`
Live bool `json:"live"` Checks int `json:"checks"`
Live bool `json:"live"`
} }
// node represents a host on the network. // node represents a host on the network.
// The fields of Node may not be modified. // The fields of Node may not be modified.
type node struct { type node struct {
*enode.Node *enode.Node
addedAt time.Time // time when the node was added to the table addedToTable time.Time // first time node was added to bucket or replacement list
addedToBucket time.Time // time it was added in the actual bucket
livenessChecks uint // how often liveness was checked livenessChecks uint // how often liveness was checked
isValidatedLive bool isValidatedLive bool // true if existence of node is considered validated right now
} }
type encPubkey [64]byte type encPubkey [64]byte

View file

@ -164,10 +164,11 @@ func (tab *Table) Nodes() [][]BucketNode {
nodes[i] = make([]BucketNode, len(b.entries)) nodes[i] = make([]BucketNode, len(b.entries))
for j, n := range b.entries { for j, n := range b.entries {
nodes[i][j] = BucketNode{ nodes[i][j] = BucketNode{
Node: n.Node, Node: n.Node,
Checks: int(n.livenessChecks), Checks: int(n.livenessChecks),
Live: n.isValidatedLive, Live: n.isValidatedLive,
AddedAt: n.addedAt, AddedTable: n.addedToTable,
AddedBucket: n.addedToBucket,
} }
} }
} }
@ -511,7 +512,6 @@ func (tab *Table) handleAddNode(req addNodeRequest) {
// Add to bucket. // Add to bucket.
b.entries = append(b.entries, req.node) b.entries = append(b.entries, req.node)
b.replacements = deleteNode(b.replacements, req.node) b.replacements = deleteNode(b.replacements, req.node)
req.node.addedAt = time.Now()
tab.nodeAdded(b, req.node) tab.nodeAdded(b, req.node)
} }
@ -524,6 +524,8 @@ func (tab *Table) addReplacement(b *bucket, n *node) {
if !tab.addIP(b, n.IP()) { if !tab.addIP(b, n.IP()) {
return return
} }
n.addedToTable = time.Now()
var removed *node var removed *node
b.replacements, removed = pushNode(b.replacements, n, maxReplacements) b.replacements, removed = pushNode(b.replacements, n, maxReplacements)
if removed != nil { if removed != nil {
@ -532,6 +534,10 @@ func (tab *Table) addReplacement(b *bucket, n *node) {
} }
func (tab *Table) nodeAdded(b *bucket, n *node) { func (tab *Table) nodeAdded(b *bucket, n *node) {
if n.addedToTable == (time.Time{}) {
n.addedToTable = time.Now()
}
n.addedToBucket = time.Now()
tab.revalidation.nodeAdded(tab, n) tab.revalidation.nodeAdded(tab, n)
if tab.nodeAddedHook != nil { if tab.nodeAddedHook != nil {
tab.nodeAddedHook(b, n) tab.nodeAddedHook(b, n)
@ -576,7 +582,6 @@ func (tab *Table) deleteInBucket(b *bucket, id enode.ID) *node {
b.replacements = slices.Delete(b.replacements, rindex, rindex+1) b.replacements = slices.Delete(b.replacements, rindex, rindex+1)
b.entries = append(b.entries, rep) b.entries = append(b.entries, rep)
tab.nodeAdded(b, rep) tab.nodeAdded(b, rep)
tab.log.Debug("Replaced dead node", "b", b.index, "id", n.ID(), "ip", n.IP(), "r", rep.ID(), "rip", rep.IP()) tab.log.Debug("Replaced dead node", "b", b.index, "id", n.ID(), "ip", n.IP(), "r", rep.ID(), "rip", rep.IP())
return rep return rep
} }