Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: decrease default routing table size #3023

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/kad-dht/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,37 @@ export interface KadDHTInit {
* can be stored.
*
* Storing more peers means fewer lookups (and network operations) are needed
* to locate a certain peer, but also that more memory is consumed.
* to locate a certain peer, but also that more memory is consumed and more
* CPU while responding to queries (e.g. with more peers in the table sorting
* the closest peers becomes more expensive) and CPU/network during table
* maintenance (e.g. checking peers are still online).
*
* @default 32
* The larger this value, the more prefix bits must be the same for a peer to
* be stored in a KAD bucket, so the fewer nodes that bucket is likely to
* contain.
*
* The total number of peers in the table is a factor of `prefixLength` and
* `kBucketSize`:
*
* ```
* (2 ^ prefixLength) * kBucketSize
* ```
*
* @default 8
*/
prefixLength?: number

/**
* If true, only ever be a DHT client. If false, be a DHT client until told
* to be a DHT server via `setMode`.
*
* In general this should be left as the default because server mode will be
* selected automatically when libp2p establishes that the current node has
* a publicly dialable address.
*
* The exception to this is LAN-only DHT (e.g. for testing purposes) where it
* is safe to assume that the current node is dialable.
*
* @default false
*/
clientMode?: boolean
Expand Down
36 changes: 36 additions & 0 deletions packages/kad-dht/test/routing-table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,40 @@ describe('Routing Table', () => {

await expect(table.find(peer.id)).to.eventually.be.ok()
})

describe('max size', () => {
it('should constrain size to 10', async () => {
const prefixLength = 8
const kBucketSize = 20
const maxSize = Math.pow(2, prefixLength) * kBucketSize

table = new RoutingTable(components, {
logPrefix: '',
metricsPrefix: '',
protocol: PROTOCOL,
network,
prefixLength,
kBucketSize
})
await start(table)

// reset network stub so we can have specific behavior
table.network = network = stubInterface()

// all old peers answer pings, no peers should be evicted
network.sendRequest.callsFake(async function * (from: PeerId) {
yield peerResponseEvent({
from,
messageType: MessageType.PING
})
})

for (let i = 0; i < 2 * maxSize; i++) {
const remotePeer = await createPeerId()
await table.add(remotePeer)
}

expect(table.size).to.be.lessThanOrEqual(maxSize)
})
})
})