Skip to content

Commit

Permalink
feat: log agent version when client is not known (ChainSafe#6314)
Browse files Browse the repository at this point in the history
* feat: log agent version when client is not known

* chore: add test case for getKnownClientFromAgentVersion
  • Loading branch information
twoeths authored and ensi321 committed Jan 22, 2024
1 parent e31a1ea commit 8003862
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
11 changes: 9 additions & 2 deletions packages/beacon-node/src/network/peers/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ export enum ClientKind {
Unknown = "Unknown",
}

export function clientFromAgentVersion(agentVersion: string): ClientKind {
/**
* Get known client from agent version.
* If client is not known, don't return ClientKind.Unknown here.
* For metrics it'll have fallback logic to use ClientKind.Unknown
* For logs, we want to print out agentVersion instead for debugging purposes.
*/
export function getKnownClientFromAgentVersion(agentVersion: string): ClientKind | null {
const slashIndex = agentVersion.indexOf("/");
const agent = slashIndex >= 0 ? agentVersion.slice(0, slashIndex) : agentVersion;
const agentLC = agent.toLowerCase();
Expand All @@ -16,5 +22,6 @@ export function clientFromAgentVersion(agentVersion: string): ClientKind {
if (agentLC === "prysm") return ClientKind.Prysm;
if (agentLC === "nimbus") return ClientKind.Nimbus;
if (agentLC === "lodestar" || agentLC === "js-libp2p") return ClientKind.Lodestar;
return ClientKind.Unknown;

return null;
}
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {NetworkCoreMetrics} from "../core/metrics.js";
import {LodestarDiscv5Opts} from "../discv5/types.js";
import {PeerDiscovery, SubnetDiscvQueryMs} from "./discover.js";
import {PeersData, PeerData} from "./peersData.js";
import {clientFromAgentVersion, ClientKind} from "./client.js";
import {getKnownClientFromAgentVersion, ClientKind} from "./client.js";
import {
getConnectedPeerIds,
hasSomeConnectedPeer,
Expand Down Expand Up @@ -615,7 +615,7 @@ export class PeerManager {
if (agentVersionBytes) {
const agentVersion = new TextDecoder().decode(agentVersionBytes) || "N/A";
peerData.agentVersion = agentVersion;
peerData.agentClient = clientFromAgentVersion(agentVersion);
peerData.agentClient = getKnownClientFromAgentVersion(agentVersion);
}
},
{retries: 3, retryDelay: 1000}
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/peers/peersData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class PeersData {
return this.connectedPeers.get(peerIdStr)?.agentVersion ?? "NA";
}

getPeerKind(peerIdStr: string): ClientKind {
return this.connectedPeers.get(peerIdStr)?.agentClient ?? ClientKind.Unknown;
getPeerKind(peerIdStr: string): ClientKind | null {
return this.connectedPeers.get(peerIdStr)?.agentClient ?? null;
}

getEncodingPreference(peerIdStr: string): Encoding | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class ReqRespBeaconNode extends ReqResp {
metrics?.reqResp.rateLimitErrors.inc({method});
},
getPeerLogMetadata(peerId) {
return peersData.getPeerKind(peerId);
// this logs the whole agent version for unknown client which is good for debugging
return peersData.getPeerKind(peerId) ?? peersData.getAgentVersion(peerId);
},
}
);
Expand Down
11 changes: 8 additions & 3 deletions packages/beacon-node/test/unit/network/peers/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {describe, it, expect} from "vitest";
import {clientFromAgentVersion, ClientKind} from "../../../../src/network/peers/client.js";
import {getKnownClientFromAgentVersion, ClientKind} from "../../../../src/network/peers/client.js";

describe("clientFromAgentVersion", () => {
const testCases: {name: string; agentVersion: string; client: ClientKind}[] = [
const testCases: {name: string; agentVersion: string; client: ClientKind | null}[] = [
{
name: "lighthouse",
agentVersion: "Lighthouse/v2.0.1-fff01b2/x86_64-linux",
Expand All @@ -28,11 +28,16 @@ describe("clientFromAgentVersion", () => {
agentVersion: "js-libp2p/0.32.4",
client: ClientKind.Lodestar,
},
{
name: "unknown client",
agentVersion: "strange-client-agent-version",
client: null,
},
];

for (const {name, agentVersion, client} of testCases) {
it(name, () => {
expect(clientFromAgentVersion(agentVersion)).toBe(client);
expect(getKnownClientFromAgentVersion(agentVersion)).toBe(client);
});
}
});

0 comments on commit 8003862

Please sign in to comment.