Skip to content

Commit

Permalink
Merge 8dfad5d into 09a4e53
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths authored Nov 15, 2021
2 parents 09a4e53 + 8dfad5d commit 18759b4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 2 deletions.
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
12 changes: 12 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,12 @@ 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
// See https://github.com/ChainSafe/lodestar/issues/3423
if (Date.now() - this.discv5StartMs <= this.discv5FirstQueryDelayMs) {
return;
}

// 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

0 comments on commit 18759b4

Please sign in to comment.