diff --git a/packages/beacon-node/src/network/core/networkCore.ts b/packages/beacon-node/src/network/core/networkCore.ts index 34a7e8430723..79af952b8b23 100644 --- a/packages/beacon-node/src/network/core/networkCore.ts +++ b/packages/beacon-node/src/network/core/networkCore.ts @@ -143,7 +143,7 @@ export class NetworkCore implements INetworkCore { const metrics = metricsRegistry ? createNetworkCoreMetrics(metricsRegistry) : null; const peersData = new PeersData(); - const peerRpcScores = new PeerRpcScoreStore(opts, metrics); + const peerRpcScores = new PeerRpcScoreStore(opts, metrics, logger); const statusCache = new LocalStatusCache(initialStatus); // Bind discv5's ENR to local metadata diff --git a/packages/beacon-node/src/network/peers/peerManager.ts b/packages/beacon-node/src/network/peers/peerManager.ts index 4a8d179cc749..1bb1b36ab1e5 100644 --- a/packages/beacon-node/src/network/peers/peerManager.ts +++ b/packages/beacon-node/src/network/peers/peerManager.ts @@ -736,6 +736,7 @@ export class PeerManager { try { const reason = GOODBYE_KNOWN_CODES[goodbye.toString()] || ""; this.metrics?.peerGoodbyeSent.inc({reason}); + this.logger.debug("disconnected peer", {reason, peerId: prettyPrintPeerId(peer)}); const conn = getConnection(this.libp2p, peer.toString()); if (conn && Date.now() - conn.timeline.open > LONG_PEER_CONNECTION_MS) { diff --git a/packages/beacon-node/src/network/peers/score/interface.ts b/packages/beacon-node/src/network/peers/score/interface.ts index f6e2dc88013a..8bdcbb7e1788 100644 --- a/packages/beacon-node/src/network/peers/score/interface.ts +++ b/packages/beacon-node/src/network/peers/score/interface.ts @@ -20,7 +20,7 @@ export interface IPeerRpcScoreStore { export interface IPeerScore { getScore(): number; getGossipScore(): number; - add(scoreDelta: number): void; + add(scoreDelta: number): number; update(): number; updateGossipsubScore(newScore: number, ignore: boolean): void; getStat(): PeerScoreStat; diff --git a/packages/beacon-node/src/network/peers/score/score.ts b/packages/beacon-node/src/network/peers/score/score.ts index a949bf5d43d1..9c4d23355906 100644 --- a/packages/beacon-node/src/network/peers/score/score.ts +++ b/packages/beacon-node/src/network/peers/score/score.ts @@ -38,12 +38,13 @@ export class RealScore implements IPeerScore { return this.gossipScore; } - add(scoreDelta: number): void { + add(scoreDelta: number): number { let newScore = this.lodestarScore + scoreDelta; if (newScore > MAX_SCORE) newScore = MAX_SCORE; if (newScore < MIN_SCORE) newScore = MIN_SCORE; this.setLodestarScore(newScore); + return newScore; } /** @@ -139,7 +140,9 @@ export class MaxScore implements IPeerScore { return DEFAULT_SCORE; } - add(): void {} + add(): number { + return DEFAULT_SCORE; + } update(): number { return MAX_SCORE; diff --git a/packages/beacon-node/src/network/peers/score/store.ts b/packages/beacon-node/src/network/peers/score/store.ts index 9b1ef5a297e6..21e30c260d57 100644 --- a/packages/beacon-node/src/network/peers/score/store.ts +++ b/packages/beacon-node/src/network/peers/score/store.ts @@ -1,6 +1,7 @@ import {PeerId} from "@libp2p/interface"; -import {MapDef, pruneSetToMax} from "@lodestar/utils"; +import {Logger, MapDef, pruneSetToMax} from "@lodestar/utils"; import {NetworkCoreMetrics} from "../../core/metrics.js"; +import {prettyPrintPeerId} from "../../util.js"; import {DEFAULT_SCORE, MAX_ENTRIES, MAX_SCORE, MIN_SCORE, SCORE_THRESHOLD} from "./constants.js"; import { IPeerRpcScoreStore, @@ -29,11 +30,13 @@ const peerActionScore: Record = { export class PeerRpcScoreStore implements IPeerRpcScoreStore { private readonly scores: MapDef; private readonly metrics: NetworkCoreMetrics | null; + private readonly logger: Logger | null; // TODO: Persist scores, at least BANNED status to disk - constructor(opts: PeerRpcScoreOpts = {}, metrics: NetworkCoreMetrics | null = null) { + constructor(opts: PeerRpcScoreOpts = {}, metrics: NetworkCoreMetrics | null = null, logger: Logger | null = null) { this.metrics = metrics; + this.logger = logger; this.scores = opts.disablePeerScoring ? new MapDef(() => new MaxScore()) : new MapDef(() => new RealScore()); } @@ -55,8 +58,10 @@ export class PeerRpcScoreStore implements IPeerRpcScoreStore { applyAction(peer: PeerId, action: PeerAction, actionName: string): void { const peerScore = this.scores.getOrDefault(peer.toString()); - peerScore.add(peerActionScore[action]); + const scoreChange = peerActionScore[action]; + const newScore = peerScore.add(scoreChange); + this.logger?.debug("peer score adjusted", {scoreChange, newScore, peerId: prettyPrintPeerId(peer), actionName}); this.metrics?.peersReportPeerCount.inc({reason: actionName}); }