discov: increase bucket size for bootnodes (#1727)

This commit is contained in:
Matus Kysel 2023-07-12 02:48:13 +02:00 committed by GitHub
parent 3fd5b0c149
commit 5742cc286c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 15 deletions

@ -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 {

@ -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 {

@ -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 {

@ -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
}

@ -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
}

@ -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
}