p2p/discover: move back to fast list when endpoint changed or dead

This commit is contained in:
Felix Lange 2024-04-20 10:19:29 +02:00
parent 7667ebfceb
commit b44673fa29
2 changed files with 44 additions and 31 deletions

View file

@ -66,7 +66,7 @@ type Config struct {
func (cfg Config) withDefaults() Config { func (cfg Config) withDefaults() Config {
// Node table configuration: // Node table configuration:
if cfg.PingInterval == 0 { if cfg.PingInterval == 0 {
cfg.PingInterval = 10 * time.Second cfg.PingInterval = 5 * time.Second
} }
if cfg.RefreshInterval == 0 { if cfg.RefreshInterval == 0 {
cfg.RefreshInterval = 30 * time.Minute cfg.RefreshInterval = 30 * time.Minute

View file

@ -28,8 +28,8 @@ import (
const never = mclock.AbsTime(math.MaxInt64) const never = mclock.AbsTime(math.MaxInt64)
type tableRevalidation struct { type tableRevalidation struct {
newNodes revalidationList fast revalidationList
nodes revalidationList slow revalidationList
activeReq map[enode.ID]struct{} activeReq map[enode.ID]struct{}
} }
@ -42,22 +42,21 @@ type revalidationResponse struct {
func (tr *tableRevalidation) init(cfg *Config) { func (tr *tableRevalidation) init(cfg *Config) {
tr.activeReq = make(map[enode.ID]struct{}) tr.activeReq = make(map[enode.ID]struct{})
tr.newNodes.nextTime = never tr.fast.nextTime = never
tr.newNodes.interval = cfg.PingInterval / 3 tr.fast.interval = cfg.PingInterval
tr.nodes.nextTime = never tr.slow.nextTime = never
tr.nodes.interval = cfg.PingInterval tr.slow.interval = cfg.PingInterval * 3
} }
// nodeAdded is called when the table receives a new node. // nodeAdded is called when the table receives a new node.
func (tr *tableRevalidation) nodeAdded(tab *Table, n *node) { func (tr *tableRevalidation) nodeAdded(tab *Table, n *node) {
tr.newNodes.push(n, tab.cfg.Clock.Now(), &tab.rand) tr.fast.push(n, tab.cfg.Clock.Now(), &tab.rand)
} }
// nodeRemoved is called when a node was removed from the table. // nodeRemoved is called when a node was removed from the table.
func (tr *tableRevalidation) nodeRemoved(n *node) { func (tr *tableRevalidation) nodeRemoved(n *node) {
wasnew := tr.newNodes.remove(n) if !tr.fast.remove(n) {
if !wasnew { tr.slow.remove(n)
tr.nodes.remove(n)
} }
} }
@ -65,22 +64,22 @@ func (tr *tableRevalidation) nodeRemoved(n *node) {
// It returns the next time it should be invoked, which is used in the Table main loop // It returns the next time it should be invoked, which is used in the Table main loop
// to schedule a timer. However, run can be called at any time. // to schedule a timer. However, run can be called at any time.
func (tr *tableRevalidation) run(tab *Table, now mclock.AbsTime) (nextTime mclock.AbsTime) { func (tr *tableRevalidation) run(tab *Table, now mclock.AbsTime) (nextTime mclock.AbsTime) {
if n := tr.newNodes.get(now, &tab.rand, tr.activeReq); n != nil { if n := tr.fast.get(now, &tab.rand, tr.activeReq); n != nil {
tr.startRequest(tab, n, true) tr.startRequest(tab, n, true)
tr.newNodes.schedule(now, &tab.rand) tr.fast.schedule(now, &tab.rand)
} }
if n := tr.nodes.get(now, &tab.rand, tr.activeReq); n != nil { if n := tr.slow.get(now, &tab.rand, tr.activeReq); n != nil {
tr.startRequest(tab, n, false) tr.startRequest(tab, n, false)
tr.nodes.schedule(now, &tab.rand) tr.slow.schedule(now, &tab.rand)
} }
if tr.newNodes.nextTime == never { if tr.fast.nextTime == never {
return tr.nodes.nextTime return tr.slow.nextTime
} }
if tr.nodes.nextTime == never { if tr.slow.nextTime == never {
return tr.newNodes.nextTime return tr.fast.nextTime
} }
return min(tr.newNodes.nextTime, tr.nodes.nextTime) return min(tr.fast.nextTime, tr.slow.nextTime)
} }
// startRequest spawns a revalidation request for node n. // startRequest spawns a revalidation request for node n.
@ -122,6 +121,7 @@ func (tab *Table) doRevalidate(resp revalidationResponse, node *enode.Node) {
// handleResponse processes the result of a revalidation request. // handleResponse processes the result of a revalidation request.
func (tr *tableRevalidation) handleResponse(tab *Table, resp revalidationResponse) { func (tr *tableRevalidation) handleResponse(tab *Table, resp revalidationResponse) {
now := tab.cfg.Clock.Now()
n := resp.n n := resp.n
b := tab.bucket(n.ID()) b := tab.bucket(n.ID())
delete(tr.activeReq, n.ID()) delete(tr.activeReq, n.ID())
@ -135,26 +135,32 @@ func (tr *tableRevalidation) handleResponse(tab *Table, resp revalidationRespons
if n.livenessChecks == 0 || resp.isNewNode { if n.livenessChecks == 0 || resp.isNewNode {
tab.deleteInBucket(b, n.ID()) tab.deleteInBucket(b, n.ID())
} }
// Move to fast queue.
if !resp.isNewNode {
tr.moveToList(&tr.fast, &tr.slow, n, now, &tab.rand)
}
return return
} }
// The node responded. // The node responded.
n.livenessChecks++ n.livenessChecks++
n.isValidatedLive = true n.isValidatedLive = true
tab.log.Debug("Revalidated node", "b", b.index, "id", n.ID(), "checks", n.livenessChecks) var endpointChanged bool
if resp.newRecord != nil { if resp.newRecord != nil {
updated := tab.bumpInBucket(b, resp.newRecord) endpointChanged := tab.bumpInBucket(b, resp.newRecord)
if updated { if endpointChanged {
// If the node changed its advertised endpoint, the updated ENR is not served // If the node changed its advertised endpoint, the updated ENR is not served
// until it has been revalidated. // until it has been revalidated.
n.isValidatedLive = false n.isValidatedLive = false
} }
} }
tab.log.Debug("Revalidated node", "b", b.index, "id", n.ID(), "checks", n.livenessChecks, "changed", endpointChanged)
// Move node over to main queue after first validation. // Move node over to slow queue after first validation.
if resp.isNewNode { if resp.isNewNode && !endpointChanged {
tr.newNodes.remove(n) tr.moveToList(&tr.slow, &tr.fast, n, now, &tab.rand)
tr.nodes.push(n, tab.cfg.Clock.Now(), &tab.rand) } else if endpointChanged {
tr.moveToList(&tr.fast, &tr.slow, n, now, &tab.rand)
} }
// Store potential seeds in database. // Store potential seeds in database.
@ -163,6 +169,13 @@ func (tr *tableRevalidation) handleResponse(tab *Table, resp revalidationRespons
} }
} }
func (tr *tableRevalidation) moveToList(dest, source *revalidationList, n *node, now mclock.AbsTime, rand randomSource) {
if !source.remove(n) {
panic("moveToList: node not in source list")
}
dest.push(n, now, rand)
}
// revalidationList holds a list nodes and the next revalidation time. // revalidationList holds a list nodes and the next revalidation time.
type revalidationList struct { type revalidationList struct {
nodes []*node nodes []*node
@ -185,6 +198,10 @@ func (rq *revalidationList) get(now mclock.AbsTime, rand randomSource, exclude m
return nil return nil
} }
func (rq *revalidationList) schedule(now mclock.AbsTime, rand randomSource) {
rq.nextTime = now.Add(time.Duration(rand.Int63n(int64(rq.interval))))
}
func (rq *revalidationList) push(n *node, now mclock.AbsTime, rand randomSource) { func (rq *revalidationList) push(n *node, now mclock.AbsTime, rand randomSource) {
rq.nodes = append(rq.nodes, n) rq.nodes = append(rq.nodes, n)
if rq.nextTime == never { if rq.nextTime == never {
@ -192,10 +209,6 @@ func (rq *revalidationList) push(n *node, now mclock.AbsTime, rand randomSource)
} }
} }
func (rq *revalidationList) schedule(now mclock.AbsTime, rand randomSource) {
rq.nextTime = now.Add(time.Duration(rand.Int63n(int64(rq.interval))))
}
func (rq *revalidationList) remove(n *node) bool { func (rq *revalidationList) remove(n *node) bool {
i := slices.Index(rq.nodes, n) i := slices.Index(rq.nodes, n)
if i == -1 { if i == -1 {