Skip to content

Commit

Permalink
discov: increase bucket size for bootnodes (#1727)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatusKysel authored Jul 12, 2023
1 parent 3fd5b0c commit 5742cc2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions p2p/discover/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 18 additions & 12 deletions p2p/discover/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/table_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 5742cc2

Please sign in to comment.