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

feat: add disablePeerScoring flag #5478

Merged
merged 2 commits into from
May 13, 2023
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
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class Network implements INetwork {
const peersData = new PeersData();
const networkEventBus = new NetworkEventBus();
const metadata = new MetadataController(config);
const peerRpcScores = new PeerRpcScoreStore(metricsCore);
const peerRpcScores = new PeerRpcScoreStore(opts, metricsCore);

const libp2p = await createNodeJsLibp2p(peerId, opts, {
peerStoreDir: peerStoreDir,
Expand Down
3 changes: 2 additions & 1 deletion packages/beacon-node/src/network/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Eth2GossipsubOpts} from "./gossip/gossipsub.js";
import {defaultGossipHandlerOpts} from "./processor/gossipHandlers.js";
import {PeerManagerOpts} from "./peers/index.js";
import {PeerManagerOpts, PeerRpcScoreOpts} from "./peers/index.js";
import {ReqRespBeaconNodeOpts} from "./reqresp/ReqRespBeaconNode.js";
import {NetworkProcessorOpts} from "./processor/index.js";

Expand All @@ -10,6 +10,7 @@ export interface NetworkOptions
// remove all Functions
Omit<ReqRespBeaconNodeOpts, "getPeerLogMetadata" | "onRateLimit">,
NetworkProcessorOpts,
PeerRpcScoreOpts,
Eth2GossipsubOpts {
localMultiaddrs: string[];
bootMultiaddrs?: string[];
Expand Down
4 changes: 4 additions & 0 deletions packages/beacon-node/src/network/peers/score/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {NetworkCoreMetrics} from "../../core/metrics.js";

export type PeerIdStr = string;

export type PeerRpcScoreOpts = {
disablePeerScoring?: boolean;
};

export interface IPeerRpcScoreStore {
getScore(peer: PeerId): number;
getGossipScore(peer: PeerId): number;
Expand Down
31 changes: 31 additions & 0 deletions packages/beacon-node/src/network/peers/score/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,34 @@ export class RealScore implements IPeerScore {
}
}
}

/** An implementation of IPeerScore for testing */
export class MaxScore implements IPeerScore {
getScore(): number {
return MAX_SCORE;
}

getGossipScore(): number {
return DEFAULT_SCORE;
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
add(): void {}

update(): number {
return MAX_SCORE;
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
updateGossipsubScore(): void {}

getStat(): PeerScoreStat {
return {
lodestarScore: MAX_SCORE,
gossipScore: DEFAULT_SCORE,
ignoreNegativeGossipScore: false,
score: MAX_SCORE,
lastUpdate: Date.now(),
};
}
}
17 changes: 13 additions & 4 deletions packages/beacon-node/src/network/peers/score/store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {MapDef, pruneSetToMax} from "@lodestar/utils";
import {NetworkCoreMetrics} from "../../core/metrics.js";
import {IPeerRpcScoreStore, IPeerScore, PeerAction, PeerIdStr, PeerScoreStats, ScoreState} from "./interface.js";
import {DEFAULT_SCORE, MAX_ENTRIES, MAX_SCORE, MIN_SCORE, SCORE_THRESHOLD} from "./constants.js";
import {RealScore} from "./score.js";
import {
IPeerRpcScoreStore,
IPeerScore,
PeerAction,
PeerIdStr,
PeerRpcScoreOpts,
PeerScoreStats,
ScoreState,
} from "./interface.js";
import {MaxScore, RealScore} from "./score.js";
import {scoreToState} from "./utils.js";

const peerActionScore: Record<PeerAction, number> = {
Expand All @@ -19,13 +27,14 @@ const peerActionScore: Record<PeerAction, number> = {
* The decay rate applies equally to positive and negative scores.
*/
export class PeerRpcScoreStore implements IPeerRpcScoreStore {
private readonly scores = new MapDef<PeerIdStr, IPeerScore>(() => new RealScore());
private readonly scores: MapDef<PeerIdStr, IPeerScore>;
private readonly metrics: NetworkCoreMetrics | null;

// TODO: Persist scores, at least BANNED status to disk

constructor(metrics: NetworkCoreMetrics | null = null) {
constructor(opts: PeerRpcScoreOpts = {}, metrics: NetworkCoreMetrics | null = null) {
this.metrics = metrics;
this.scores = opts.disablePeerScoring ? new MapDef(() => new MaxScore()) : new MapDef(() => new RealScore());
}

getScore(peer: PeerId): number {
Expand Down
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 @@ -12,6 +12,7 @@ export type NetworkArgs = {
bootnodes?: string[];
targetPeers: number;
subscribeAllSubnets: boolean;
disablePeerScoring: boolean;
mdns: boolean;
"network.maxPeers": number;
"network.connectToDiscv5Bootnodes": boolean;
Expand Down Expand Up @@ -58,6 +59,7 @@ export function parseArgs(args: NetworkArgs): IBeaconNodeOptions["network"] {
targetPeers,
localMultiaddrs: [`/ip4/${listenAddress}/tcp/${tcpPort}`],
subscribeAllSubnets: args["subscribeAllSubnets"],
disablePeerScoring: args["disablePeerScoring"],
connectToDiscv5Bootnodes: args["network.connectToDiscv5Bootnodes"],
discv5FirstQueryDelayMs: args["network.discv5FirstQueryDelayMs"],
dontSendGossipAttestationsToForkchoice: args["network.dontSendGossipAttestationsToForkchoice"],
Expand Down Expand Up @@ -128,6 +130,13 @@ export const options: CliCommandOptions<NetworkArgs> = {
group: "network",
},

disablePeerScoring: {
type: "boolean",
description: "Disable peer scoring, used for testing on devnets",
defaultDescription: String(defaultOptions.network.disablePeerScoring === true),
group: "network",
},

mdns: {
type: "boolean",
description: "Enable mdns local peer discovery",
Expand Down
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 @@ -71,6 +71,7 @@ describe("options / beaconNodeOptions", () => {
bootnodes: ["enr:-somedata"],
targetPeers: 25,
subscribeAllSubnets: true,
disablePeerScoring: true,
mdns: false,
"network.maxPeers": 30,
"network.connectToDiscv5Bootnodes": true,
Expand Down Expand Up @@ -168,6 +169,7 @@ describe("options / beaconNodeOptions", () => {
targetPeers: 25,
localMultiaddrs: ["/ip4/127.0.0.1/tcp/9001"],
subscribeAllSubnets: true,
disablePeerScoring: true,
connectToDiscv5Bootnodes: true,
discv5FirstQueryDelayMs: 1000,
dontSendGossipAttestationsToForkchoice: true,
Expand Down