From 9ed87d592d6ec44695cfddbd47d2a8fe8fd95bfc Mon Sep 17 00:00:00 2001 From: Matus Kysel Date: Thu, 22 Jun 2023 09:14:03 +0200 Subject: [PATCH] discov: increase bucket size for bootnodes --- cmd/bootnode/main.go | 1 + p2p/discover/common.go | 1 + p2p/discover/table.go | 30 ++++++++++++++++++------------ p2p/discover/table_util_test.go | 2 +- p2p/discover/v4_udp.go | 2 +- p2p/discover/v5_udp.go | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index 86a81914ff..b1c9799a75 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -134,6 +134,7 @@ func main() { PrivateKey: nodeKey, NetRestrict: restrictList, FilterFunction: filterFunction, + IsBootnode: true, } if *runv5 { if _, err := discover.ListenV5(conn, ln, cfg); err != nil { diff --git a/p2p/discover/common.go b/p2p/discover/common.go index b4de7ed22d..c07334d0b2 100644 --- a/p2p/discover/common.go +++ b/p2p/discover/common.go @@ -80,6 +80,7 @@ type Config struct { ValidSchemes enr.IdentityScheme // allowed identity schemes Clock mclock.Clock FilterFunction NodeFilterFunc // function for filtering ENR entries + IsBootnode bool // defines if it's bootnode } func (cfg Config) withDefaults() Config { diff --git a/p2p/discover/table.go b/p2p/discover/table.go index a6301c999c..8c99280de0 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -40,9 +40,10 @@ import ( ) const ( - alpha = 3 // Kademlia concurrency factor - bucketSize = 16 // Kademlia bucket size - maxReplacements = 10 // Size of per-bucket replacement list + alpha = 3 // Kademlia concurrency factor + bucketSize = 16 // Kademlia bucket size + bootNodeBucketSize = 256 // Bigger bucket size for boot nodes + maxReplacements = 10 // Size of per-bucket replacement list // We keep buckets for the upper 1/15 of distances because // it's very unlikely we'll ever encounter a node that's closer. @@ -66,11 +67,12 @@ const ( // itself up-to-date by verifying the liveness of neighbors and requesting their node // records when announcements of a new record version are received. type Table struct { - mutex sync.Mutex // protects buckets, bucket content, nursery, rand - buckets [nBuckets]*bucket // index of known nodes by distance - nursery []*node // bootstrap nodes - rand *mrand.Rand // source of randomness, periodically reseeded - ips netutil.DistinctNetSet + mutex sync.Mutex // protects buckets, bucket content, nursery, rand + buckets [nBuckets]*bucket // index of known nodes by distance + bucketSize int // size of bucket + nursery []*node // bootstrap nodes + rand *mrand.Rand // source of randomness, periodically reseeded + ips netutil.DistinctNetSet log log.Logger db *enode.DB // database of known nodes @@ -102,7 +104,7 @@ type bucket struct { ips netutil.DistinctNetSet } -func newTable(t transport, db *enode.DB, bootnodes []*enode.Node, log log.Logger, filter NodeFilterFunc) (*Table, error) { +func newTable(t transport, db *enode.DB, bootnodes []*enode.Node, log log.Logger, filter NodeFilterFunc, bootnode bool) (*Table, error) { tab := &Table{ net: t, db: db, @@ -114,6 +116,10 @@ func newTable(t transport, db *enode.DB, bootnodes []*enode.Node, log log.Logger ips: netutil.DistinctNetSet{Subnet: tableSubnet, Limit: tableIPLimit}, log: log, enrFilter: filter, + bucketSize: bucketSize, + } + if bootnode { + tab.bucketSize = bootNodeBucketSize } if err := tab.setFallbackNodes(bootnodes); err != nil { return nil, err @@ -503,7 +509,7 @@ func (tab *Table) addSeenNodeSync(n *node) { // Already in bucket, don't add. return } - if len(b.entries) >= bucketSize { + if len(b.entries) >= tab.bucketSize { // Bucket full, maybe add as replacement. tab.addReplacement(b, n) return @@ -568,7 +574,7 @@ func (tab *Table) addVerifiedNodeSync(n *node) { // Already in bucket, moved to front. return } - if len(b.entries) >= bucketSize { + if len(b.entries) >= tab.bucketSize { // Bucket full, maybe add as replacement. tab.addReplacement(b, n) return @@ -578,7 +584,7 @@ func (tab *Table) addVerifiedNodeSync(n *node) { return } // Add to front of bucket. - b.entries, _ = pushNode(b.entries, n, bucketSize) + b.entries, _ = pushNode(b.entries, n, tab.bucketSize) b.replacements = deleteNode(b.replacements, n) n.addedAt = time.Now() if tab.nodeAddedHook != nil { diff --git a/p2p/discover/table_util_test.go b/p2p/discover/table_util_test.go index 5da68e72e1..b1a209e94b 100644 --- a/p2p/discover/table_util_test.go +++ b/p2p/discover/table_util_test.go @@ -43,7 +43,7 @@ func init() { func newTestTable(t transport) (*Table, *enode.DB) { db, _ := enode.OpenDB("") - tab, _ := newTable(t, db, nil, log.Root(), nil) + tab, _ := newTable(t, db, nil, log.Root(), nil, false) go tab.loop() return tab, db } diff --git a/p2p/discover/v4_udp.go b/p2p/discover/v4_udp.go index 5e106f30ae..2a5edbfd2e 100644 --- a/p2p/discover/v4_udp.go +++ b/p2p/discover/v4_udp.go @@ -143,7 +143,7 @@ func ListenV4(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) { log: cfg.Log, } - tab, err := newTable(t, ln.Database(), cfg.Bootnodes, t.log, cfg.FilterFunction) + tab, err := newTable(t, ln.Database(), cfg.Bootnodes, t.log, cfg.FilterFunction, cfg.IsBootnode) if err != nil { return nil, err } diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go index f88ce33b8b..2c28cbb6f0 100644 --- a/p2p/discover/v5_udp.go +++ b/p2p/discover/v5_udp.go @@ -164,7 +164,7 @@ func newUDPv5(conn UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv5, error) { closeCtx: closeCtx, cancelCloseCtx: cancelCloseCtx, } - tab, err := newTable(t, t.db, cfg.Bootnodes, cfg.Log, cfg.FilterFunction) + tab, err := newTable(t, t.db, cfg.Bootnodes, cfg.Log, cfg.FilterFunction, cfg.IsBootnode) if err != nil { return nil, err }