From 7525c68dc33d5b0588942b9cd79dada3a5876b36 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 6 Feb 2023 12:04:23 -0400 Subject: [PATCH] fix: reseed bootnodes when routing table is empty --- discover/table.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/discover/table.go b/discover/table.go index c91b8b8..45377ca 100644 --- a/discover/table.go +++ b/discover/table.go @@ -53,12 +53,13 @@ const ( bucketIPLimit, bucketSubnet = 2, 24 // at most 2 addresses from the same /24 tableIPLimit, tableSubnet = 10, 24 - refreshInterval = 30 * time.Minute - revalidateInterval = 10 * time.Second - copyNodesInterval = 30 * time.Second - seedMinTableTime = 5 * time.Minute - seedCount = 30 - seedMaxAge = 5 * 24 * time.Hour + refreshInterval = 30 * time.Minute + revalidateInterval = 10 * time.Second + copyNodesInterval = 30 * time.Second + reseedBootnodesInterval = 15 * time.Second + seedMinTableTime = 5 * time.Minute + seedCount = 30 + seedMaxAge = 5 * 24 * time.Hour ) // Table is the 'node table', a Kademlia-like index of neighbor nodes. The table keeps @@ -223,6 +224,7 @@ func (tab *Table) loop() { revalidate = time.NewTimer(tab.nextRevalidateTime()) refresh = time.NewTicker(refreshInterval) copyNodes = time.NewTicker(copyNodesInterval) + reseed = time.NewTicker(reseedBootnodesInterval) refreshDone = make(chan struct{}) // where doRefresh reports completion revalidateDone chan struct{} // where doRevalidate reports completion waiting = []chan struct{}{tab.initDone} // holds waiting callers while doRefresh runs @@ -230,6 +232,7 @@ func (tab *Table) loop() { defer refresh.Stop() defer revalidate.Stop() defer copyNodes.Stop() + defer reseed.Stop() // Start initial refresh. go tab.doRefresh(refreshDone) @@ -262,6 +265,8 @@ loop: revalidateDone = nil case <-copyNodes.C: go tab.copyLiveNodes() + case <-reseed.C: + go tab.doReseedBootnodes() case <-tab.closeReq: break loop } @@ -303,6 +308,13 @@ func (tab *Table) doRefresh(done chan struct{}) { } } +// doReseedNodes checks if the table is empty and inserts the initial bootstrap nodes. +func (tab *Table) doReseedBootnodes() { + if tab.len() == 0 { + tab.loadSeedNodes() + } +} + func (tab *Table) loadSeedNodes() { seeds := wrapNodes(tab.db.QuerySeeds(seedCount, seedMaxAge)) seeds = append(seeds, tab.nursery...)