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

Discv5: Do not issue FINDNODES right after the start #3429

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions packages/cli/src/options/beaconNodeOptions/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface INetworkArgs {
"network.localMultiaddrs": string[];
"network.subscribeAllSubnets": boolean;
"network.connectToDiscv5Bootnodes": boolean;
"network.discv5FirstQueryDelayMs": number;
}

export function parseArgs(args: INetworkArgs): IBeaconNodeOptions["network"] {
Expand All @@ -28,6 +29,7 @@ export function parseArgs(args: INetworkArgs): IBeaconNodeOptions["network"] {
localMultiaddrs: args["network.localMultiaddrs"],
subscribeAllSubnets: args["network.subscribeAllSubnets"],
connectToDiscv5Bootnodes: args["network.connectToDiscv5Bootnodes"],
discv5FirstQueryDelayMs: args["network.discv5FirstQueryDelayMs"],
};
}

Expand Down Expand Up @@ -95,4 +97,11 @@ export const options: ICliCommandOptions<INetworkArgs> = {
defaultDescription: String(defaultOptions.network.connectToDiscv5Bootnodes === true),
group: "network",
},

"network.discv5FirstQueryDelayMs": {
type: "number",
description: "Delay the 1st heart beat of Peer Manager after starting Discv5",
defaultDescription: String(defaultOptions.network.discv5FirstQueryDelayMs),
group: "network",
},
};
2 changes: 2 additions & 0 deletions packages/cli/test/unit/options/beaconNodeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe("options / beaconNodeOptions", () => {
"network.localMultiaddrs": [],
"network.subscribeAllSubnets": true,
"network.connectToDiscv5Bootnodes": true,
"network.discv5FirstQueryDelayMs": 1000,

"sync.isSingleNode": true,
"sync.disableProcessAsChainSegment": true,
Expand Down Expand Up @@ -100,6 +101,7 @@ describe("options / beaconNodeOptions", () => {
localMultiaddrs: [],
subscribeAllSubnets: true,
connectToDiscv5Bootnodes: true,
discv5FirstQueryDelayMs: 1000,
},
sync: {
isSingleNode: true,
Expand Down
1 change: 1 addition & 0 deletions packages/lodestar/src/network/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const defaultDiscv5Options: IDiscv5DiscoveryInputOptions = {
export const defaultNetworkOptions: INetworkOptions = {
maxPeers: 30, // Allow some room above targetPeers for new inbound peers
targetPeers: 25,
discv5FirstQueryDelayMs: 1000,
localMultiaddrs: ["/ip4/0.0.0.0/tcp/9000"],
bootMultiaddrs: [],
discv5: defaultDiscv5Options,
Expand Down
10 changes: 10 additions & 0 deletions packages/lodestar/src/network/peers/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const MAX_CACHED_ENR_AGE_MS = 5 * 60 * 1000;

export type PeerDiscoveryOpts = {
maxPeers: number;
discv5FirstQueryDelayMs: number;
discv5: Omit<IDiscv5DiscoveryInputOptions, "metrics" | "searchInterval" | "enabled">;
};

Expand Down Expand Up @@ -86,6 +87,8 @@ export class PeerDiscovery {

/** The maximum number of peers we allow (exceptions for subnet peers) */
private maxPeers: number;
private discv5StartMs: number;
private discv5FirstQueryDelayMs: number;

constructor(modules: PeerDiscoveryModules, opts: PeerDiscoveryOpts) {
const {libp2p, peerRpcScores, metrics, logger, config} = modules;
Expand All @@ -95,6 +98,8 @@ export class PeerDiscovery {
this.logger = logger;
this.config = config;
this.maxPeers = opts.maxPeers;
this.discv5StartMs = 0;
this.discv5FirstQueryDelayMs = opts.discv5FirstQueryDelayMs;

this.discv5 = Discv5.create({
enr: opts.discv5.enr,
Expand All @@ -119,6 +124,7 @@ export class PeerDiscovery {

async start(): Promise<void> {
await this.discv5.start();
this.discv5StartMs = Date.now();
this.discv5.on("discovered", this.onDiscovered);
}

Expand Down Expand Up @@ -196,6 +202,10 @@ export class PeerDiscovery {
* Request to find peers. First, looked at cached peers in peerStore
*/
private async runFindRandomNodeQuery(): Promise<void> {
// Delay the 1st query after starting discv5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to the Github issue that explain why this is necessary

if (Date.now() - this.discv5StartMs <= this.discv5FirstQueryDelayMs) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace after this if please 🙏

// Run a general discv5 query if one is not already in progress
if (this.randomNodeQuery.code === QueryStatusCode.Active) {
this.metrics?.discovery.findNodeQueryRequests.inc({action: "ignore"});
Expand Down
13 changes: 12 additions & 1 deletion packages/lodestar/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export type PeerManagerOpts = {
targetPeers: number;
/** The maximum number of peers we allow (exceptions for subnet peers) */
maxPeers: number;
/**
* Delay the 1st query after starting discv5
* See https://github.com/ChainSafe/lodestar/issues/3423
*/
discv5FirstQueryDelayMs: number;
/**
* If null, Don't run discv5 queries, nor connect to cached peers in the peerStore
*/
Expand Down Expand Up @@ -128,7 +133,13 @@ export class PeerManager {
this.opts = opts;

// opts.discv5 === null, discovery is disabled
this.discovery = opts.discv5 && new PeerDiscovery(modules, {maxPeers: opts.maxPeers, discv5: opts.discv5});
this.discovery =
opts.discv5 &&
new PeerDiscovery(modules, {
maxPeers: opts.maxPeers,
discv5FirstQueryDelayMs: opts.discv5FirstQueryDelayMs,
discv5: opts.discv5,
});

const {metrics} = modules;
if (metrics) {
Expand Down
1 change: 1 addition & 0 deletions packages/lodestar/test/e2e/network/gossipsub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const opts: INetworkOptions = {
targetPeers: 1,
bootMultiaddrs: [],
localMultiaddrs: [],
discv5FirstQueryDelayMs: 0,
discv5: null,
};

Expand Down
1 change: 1 addition & 0 deletions packages/lodestar/test/e2e/network/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe("network", function () {
targetPeers: 1,
bootMultiaddrs: [],
localMultiaddrs: [],
discv5FirstQueryDelayMs: 0,
discv5: {
enr,
bindAddr: bindAddrUdp,
Expand Down
7 changes: 6 additions & 1 deletion packages/lodestar/test/e2e/network/peers/peerManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ describe("network / peers / PeerManager", function () {
attnetsService: mockSubnetsService,
syncnetsService: mockSubnetsService,
},
{targetPeers: 30, maxPeers: 50, discv5: null}
{
targetPeers: 30,
maxPeers: 50,
discv5: null,
discv5FirstQueryDelayMs: 0,
}
);
await peerManager.start();

Expand Down
1 change: 1 addition & 0 deletions packages/lodestar/test/e2e/network/reqresp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("network / ReqResp", function () {
targetPeers: 1,
bootMultiaddrs: [],
localMultiaddrs: [],
discv5FirstQueryDelayMs: 0,
discv5: null,
};
const state = generateState();
Expand Down