Skip to content

Commit

Permalink
feat: log peer disconnect info (#7231)
Browse files Browse the repository at this point in the history
* feat: log disconnect reason

* feat: log peerScore update

* fix: pretty print peerId

* fix: use prettyPrintPeerId
  • Loading branch information
matthewkeil authored Nov 25, 2024
1 parent 8689c76 commit c8075d0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/core/networkCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/peers/score/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions packages/beacon-node/src/network/peers/score/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -139,7 +140,9 @@ export class MaxScore implements IPeerScore {
return DEFAULT_SCORE;
}

add(): void {}
add(): number {
return DEFAULT_SCORE;
}

update(): number {
return MAX_SCORE;
Expand Down
11 changes: 8 additions & 3 deletions packages/beacon-node/src/network/peers/score/store.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -29,11 +30,13 @@ const peerActionScore: Record<PeerAction, number> = {
export class PeerRpcScoreStore implements IPeerRpcScoreStore {
private readonly scores: MapDef<PeerIdStr, IPeerScore>;
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());
}

Expand All @@ -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});
}

Expand Down

0 comments on commit c8075d0

Please sign in to comment.