Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
61 changes: 0 additions & 61 deletions .eslintrc.cjs

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ lib

# others
package-lock.json

# benchmark
benchmark_data/
5 changes: 0 additions & 5 deletions .mocharc.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions .prettierrc.cjs

This file was deleted.

12 changes: 12 additions & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"extends": [
"@chainsafe/biomejs-config",
"@chainsafe/biomejs-config/recommended",
"@chainsafe/biomejs-config/nodejs",
"@chainsafe/biomejs-config/esm"
],
"files": {
"includes": ["**/packages/**/*/src/**/*.ts", "**/packages/**/*/test/**/*.ts"]
}
}
27 changes: 8 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"check-types": "pnpm -r check-types",
"build": "pnpm -r build",
"lint": "pnpm -r lint",
"lint": "biome check",
"prepublishOnly": "pnpm build",
"publish": "pnpm publish -r --access public --no-git-check",
"test": "pnpm -r test",
Expand All @@ -21,24 +21,13 @@
"lint"
],
"devDependencies": {
"@chainsafe/eslint-plugin-node": "^11.2.3",
"@dapplion/benchmark": "^0.2.4",
"@types/chai": "^4.2.0",
"@types/debug": "^4.1.5",
"@types/eslint": "^6.1.3",
"@types/mocha": "^8.0.3",
"@types/node": "^20.6.0",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"chai": "^4.3.6",
"eslint": "^8.17.0",
"eslint-plugin-prettier": "^4.0.0",
"karma": "^4.3.0",
"mocha": "^10.0.0",
"nyc": "^14.1.1",
"prettier": "^2.6.2",
"ts-node": "^10.8.1",
"typescript": "^4.7.3"
"@biomejs/biome": "^2.3.14",
"@chainsafe/benchmark": "^2.0.1",
"@chainsafe/biomejs-config": "^1.0.0",
"@types/node": "^24.10.12",
"ts-node": "^10.9.2",
"typescript": "^5.9.3",
"vitest": "^4.0.18"
},
"packageManager": "pnpm@10.24.0"
}
5 changes: 0 additions & 5 deletions packages/discv5/.mocharc.yaml

This file was deleted.

27 changes: 14 additions & 13 deletions packages/discv5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Discovery V5",
"type": "module",
"files": [
"src",
"lib"
],
"exports": {
Expand Down Expand Up @@ -40,10 +41,10 @@
"check-types": "tsc --noEmit",
"build": "tsc -p tsconfig.build.json",
"prepublishOnly": "pnpm build",
"lint": "eslint --color --ext .ts src/ test/",
"lint": "biome check src test",
"test": "pnpm test:unit && pnpm test:e2e",
"test:unit": "mocha 'test/unit/**/*.test.ts'",
"test:e2e": "mocha 'test/e2e/**/*.test.ts'"
"test:unit": "vitest --run 'test/unit'",
"test:e2e": "vitest --run 'test/e2e'"
},
"pre-push": [
"lint"
Expand All @@ -67,16 +68,16 @@
"devDependencies": {},
"dependencies": {
"@chainsafe/enr": "workspace:^",
"@ethereumjs/rlp": "^5.0.2",
"@ethereumjs/rlp": "^10.1.1",
"@libp2p/crypto": "^5.0.1",
"@libp2p/interface": "^2.0.1",
"@libp2p/peer-id": "^5.0.1",
"@multiformats/multiaddr": "^12.1.10",
"@noble/hashes": "^1.7.0",
"@noble/secp256k1": "^2.2.2",
"debug": "^4.3.1",
"ethereum-cryptography": "^2.2.0",
"lru-cache": "^10.1.0",
"strict-event-emitter-types": "^2.0.0"
"@libp2p/interface": "^3.1.0",
"@libp2p/peer-id": "^6.0.4",
"@multiformats/multiaddr": "^13.0.1",
"@noble/hashes": "^2.0.1",
"@noble/secp256k1": "^3.0.0",
"ethereum-cryptography": "^3.2.0",
"lru-cache": "^11.2.5",
"strict-event-emitter-types": "^2.0.0",
"weald": "^1.1.1"
}
}
20 changes: 10 additions & 10 deletions packages/discv5/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ISessionConfig } from "../session/index.js";
import { ILookupConfig } from "../kademlia/index.js";
import type {ILookupConfig} from "../kademlia/index.js";
import type {ISessionConfig} from "../session/index.js";

export type IDiscv5Config = ISessionConfig &
ILookupConfig & {
Expand All @@ -20,16 +20,16 @@ export type IDiscv5Config = ISessionConfig &

export const defaultConfig: IDiscv5Config = {
addrVotesToUpdateEnr: 10,
requestTimeout: 1 * 1000,
requestRetries: 1,
sessionTimeout: 86400 * 1000, // 1 day
sessionEstablishTimeout: 15 * 1000,
sessionCacheCapacity: 2000,
allowUnverifiedSessions: false,
enrUpdate: true,
lookupNumResults: 16,
lookupParallelism: 3,
lookupRequestLimit: 3,
lookupNumResults: 16,
lookupTimeout: 60 * 1000,
pingInterval: 300 * 1000,
enrUpdate: true,
allowUnverifiedSessions: false,
requestRetries: 1,
requestTimeout: 1 * 1000,
sessionCacheCapacity: 2000,
sessionEstablishTimeout: 15 * 1000,
sessionTimeout: 86400 * 1000, // 1 day
};
77 changes: 40 additions & 37 deletions packages/discv5/src/kademlia/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { EventEmitter } from "events";
import { ENR, NodeId } from "@chainsafe/enr";

import { MAX_NODES_PER_BUCKET } from "./constants.js";
import { BucketEventEmitter, EntryStatus, IEntry, IEntryFull, InsertResult, UpdateResult } from "./types.js";

export class Bucket extends (EventEmitter as { new (): BucketEventEmitter }) {
import {EventEmitter} from "node:events";
import type {ENR, NodeId} from "@chainsafe/enr";

import {MAX_NODES_PER_BUCKET} from "./constants.js";
import {
type BucketEventEmitter,
EntryStatus,
type IEntry,
type IEntryFull,
InsertResult,
UpdateResult,
} from "./types.js";

export class Bucket extends (EventEmitter as {new (): BucketEventEmitter}) {
/**
* Entries ordered from least-recently connected to most-recently connected
*/
Expand Down Expand Up @@ -88,40 +95,37 @@ export class Bucket extends (EventEmitter as { new (): BucketEventEmitter }) {
case EntryStatus.Connected: {
if (this.nodes.length < MAX_NODES_PER_BUCKET) {
this.firstConnectedIndex = this.firstConnectedIndex ?? this.nodes.length;
this.nodes.push({ value, status });
this.nodes.push({status, value});
break;
} else {
// The bucket is full, attempt to add the node as pending
if (this.addPending(value, status)) {
return InsertResult.Pending;
} else {
return InsertResult.FailedBucketFull;
}
}
// The bucket is full, attempt to add the node as pending
if (this.addPending(value, status)) {
return InsertResult.Pending;
}
return InsertResult.FailedBucketFull;
}
case EntryStatus.Disconnected: {
if (this.nodes.length < MAX_NODES_PER_BUCKET) {
if (this.firstConnectedIndex === undefined) {
// No connected nodes, add to the end
this.nodes.push({ value, status });
this.nodes.push({status, value});
} else {
// add before the first connected node
this.nodes.splice(this.firstConnectedIndex, 0, { value, status });
this.nodes.splice(this.firstConnectedIndex, 0, {status, value});
this.firstConnectedIndex++;
}
break;
} else {
// The bucket is full
return InsertResult.FailedBucketFull;
}
// The bucket is full
return InsertResult.FailedBucketFull;
}
}

// If we inserted the node, make sure there is no pending node of the same key. This can
// happen when a pending node is inserted, a node gets removed from the bucket, freeing up
// space and then re-inserted here.
if (isPendingNode) {
delete this.pending;
this.pending = undefined;
}
return InsertResult.Inserted;
}
Expand All @@ -138,15 +142,14 @@ export class Bucket extends (EventEmitter as { new (): BucketEventEmitter }) {
if (value.seq > node.value.seq) {
node.value = value;
return UpdateResult.Updated;
} else {
return UpdateResult.NotModified;
}
} else if (this.pending?.value.nodeId === value.nodeId) {
return UpdateResult.NotModified;
}
if (this.pending?.value.nodeId === value.nodeId) {
this.pending.value = value;
return UpdateResult.UpdatedPending;
} else {
return UpdateResult.FailedKeyNonExistent;
}
return UpdateResult.FailedKeyNonExistent;
}

/**
Expand Down Expand Up @@ -175,29 +178,29 @@ export class Bucket extends (EventEmitter as { new (): BucketEventEmitter }) {
// If the least-recently connected node re-establishes its
// connected status, drop the pending node.
if (index === 0 && isConnected) {
delete this.pending;
this.pending = undefined;
}

// Reinsert the node with the desired status
switch (this.add(node.value, status)) {
case InsertResult.Inserted: {
if (notModified) {
return UpdateResult.NotModified;
} else if (!wasConnected && isConnected) {
}
if (!wasConnected && isConnected) {
return UpdateResult.UpdatedAndPromoted;
} else {
return UpdateResult.Updated;
}
return UpdateResult.Updated;
}
default:
throw new Error("Unreachable");
}
} else if (this.pending?.value.nodeId === id) {
}
if (this.pending?.value.nodeId === id) {
this.pending.status = status;
return UpdateResult.UpdatedPending;
} else {
return UpdateResult.FailedKeyNonExistent;
}
return UpdateResult.FailedKeyNonExistent;
}

/**
Expand All @@ -208,7 +211,7 @@ export class Bucket extends (EventEmitter as { new (): BucketEventEmitter }) {
*/
addPending(value: ENR, status: EntryStatus): boolean {
if (!this.pending && this.firstConnectedIndex !== 0) {
this.pending = { value, status };
this.pending = {status, value};
const first = this.nodes[0];
this.emit("pendingEviction", first.value);
this.pendingTimeoutId = setTimeout(this.applyPending, this.pendingTimeout);
Expand Down Expand Up @@ -253,10 +256,10 @@ export class Bucket extends (EventEmitter as { new (): BucketEventEmitter }) {
getWithPending(id: NodeId): IEntryFull<ENR> | undefined {
const bucketEntry = this.get(id);
if (bucketEntry) {
return { pending: false, ...bucketEntry };
return {pending: false, ...bucketEntry};
}
if (this.pending && this.pending.value.nodeId === id) {
return { pending: true, ...this.pending };
return {pending: true, ...this.pending};
}
return undefined;
}
Expand Down Expand Up @@ -297,7 +300,7 @@ export class Bucket extends (EventEmitter as { new (): BucketEventEmitter }) {
case EntryStatus.Connected: {
if (this.firstConnectedIndex === index && index === this.nodes.length - 1) {
// It was the last connected node.
delete this.firstConnectedIndex;
this.firstConnectedIndex = undefined;
}
break;
}
Expand Down
Loading