Skip to content

Commit

Permalink
Categorize peer by agent version (#3472)
Browse files Browse the repository at this point in the history
* Parse client from agent version

* Only show client name in req/resp log

* Only parse client kind
  • Loading branch information
twoeths authored Dec 6, 2021
1 parent 48bff72 commit 59bd4c7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
27 changes: 27 additions & 0 deletions packages/lodestar/src/network/peers/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export enum ClientKind {
Lighthouse = "Lighthouse",
Nimbus = "Nimbus",
Teku = "Teku",
Prysm = "Prysm",
Lodestar = "Lodestar",
Unknown = "Unknown",
}

export function clientFromAgentVersion(agentVersion: string): ClientKind {
const slashIndex = agentVersion.indexOf("/");
const agent = slashIndex >= 0 ? agentVersion.slice(0, slashIndex) : agentVersion;
switch (agent.toLowerCase()) {
case "lighthouse":
return ClientKind.Lighthouse;
case "teku":
return ClientKind.Teku;
case "prysm":
return ClientKind.Prysm;
case "nimbus":
return ClientKind.Nimbus;
case "js-libp2p":
return ClientKind.Lodestar;
default:
return ClientKind.Unknown;
}
}
6 changes: 3 additions & 3 deletions packages/lodestar/src/network/reqresp/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Libp2p} from "libp2p/src/connection-manager";
import {IForkDigestContext} from "@chainsafe/lodestar-config";
import {ErrorAborted, ILogger, Context, withTimeout, TimeoutError} from "@chainsafe/lodestar-utils";
import {timeoutOptions} from "../../../constants";
import {getAgentVersionFromPeerStore, prettyPrintPeerId} from "../../util";
import {getClientFromPeerStore, prettyPrintPeerId} from "../../util";
import {Method, Encoding, Protocol, Version, IncomingResponseBody, RequestBody} from "../types";
import {formatProtocolId} from "../utils";
import {ResponseError} from "../response";
Expand Down Expand Up @@ -55,8 +55,8 @@ export async function sendRequest<T extends IncomingResponseBody | IncomingRespo
): Promise<T> {
const {REQUEST_TIMEOUT, DIAL_TIMEOUT} = {...timeoutOptions, ...options};
const peer = prettyPrintPeerId(peerId);
const agentVersion = getAgentVersionFromPeerStore(peerId, libp2p.peerStore.metadataBook);
const logCtx = {method, encoding, agentVersion, peer, requestId};
const client = getClientFromPeerStore(peerId, libp2p.peerStore.metadataBook);
const logCtx = {method, encoding, client, peer, requestId};

if (signal?.aborted) {
throw new ErrorAborted("sendRequest");
Expand Down
6 changes: 3 additions & 3 deletions packages/lodestar/src/network/reqresp/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Libp2p} from "libp2p/src/connection-manager";
import {Context, ILogger, TimeoutError, withTimeout} from "@chainsafe/lodestar-utils";
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import {REQUEST_TIMEOUT, RespStatus} from "../../../constants";
import {getAgentVersionFromPeerStore, prettyPrintPeerId} from "../../util";
import {getClientFromPeerStore, prettyPrintPeerId} from "../../util";
import {Protocol, RequestBody, OutgoingResponseBody} from "../types";
import {onChunk} from "../utils";
import {Libp2pStream} from "../interface";
Expand Down Expand Up @@ -46,8 +46,8 @@ export async function handleRequest(
signal?: AbortSignal,
requestId = 0
): Promise<void> {
const agentVersion = getAgentVersionFromPeerStore(peerId, libp2p.peerStore.metadataBook);
const logCtx = {method: protocol.method, agentVersion, peer: prettyPrintPeerId(peerId), requestId};
const client = getClientFromPeerStore(peerId, libp2p.peerStore.metadataBook);
const logCtx = {method: protocol.method, client, peer: prettyPrintPeerId(peerId), requestId};

let responseError: Error | null = null;
await pipe(
Expand Down
6 changes: 6 additions & 0 deletions packages/lodestar/src/network/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Multiaddr} from "multiaddr";
import {networkInterfaces} from "os";
import {ENR} from "@chainsafe/discv5";
import MetadataBook from "libp2p/src/peer-store/metadata-book";
import {clientFromAgentVersion, ClientKind} from "./peers/client";

// peers

Expand Down Expand Up @@ -69,6 +70,11 @@ export function prettyPrintPeerId(peerId: PeerId): string {
return `${id.substr(0, 2)}...${id.substr(id.length - 6, id.length)}`;
}

export function getClientFromPeerStore(peerId: PeerId, metadataBook: MetadataBook): ClientKind {
const agentVersion = getAgentVersionFromPeerStore(peerId, metadataBook);
return clientFromAgentVersion(agentVersion);
}

export function getAgentVersionFromPeerStore(peerId: PeerId, metadataBook: MetadataBook): string {
return new TextDecoder().decode(metadataBook.getValue(peerId, "AgentVersion")) || "N/A";
}
38 changes: 38 additions & 0 deletions packages/lodestar/test/unit/network/peers/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {expect} from "chai";
import {clientFromAgentVersion, ClientKind} from "../../../../src/network/peers/client";

describe("clientFromAgentVersion", () => {
const testCases: {name: string; agentVersion: string; client: ClientKind}[] = [
{
name: "lighthouse",
agentVersion: "Lighthouse/v2.0.1-fff01b2/x86_64-linux",
client: ClientKind.Lighthouse,
},
{
name: "teku",
agentVersion: "teku/teku/v21.11.0+62-g501ffa7/linux-x86_64/corretto-java-17",
client: ClientKind.Teku,
},
{
name: "nimbus",
agentVersion: "nimbus",
client: ClientKind.Nimbus,
},
{
name: "prysm",
agentVersion: "Prysm/v2.0.2/a80b1c252a9b4773493b41999769bf3134ac373f",
client: ClientKind.Prysm,
},
{
name: "lodestar",
agentVersion: "js-libp2p/0.32.4",
client: ClientKind.Lodestar,
},
];

for (const {name, agentVersion, client} of testCases) {
it(name, () => {
expect(clientFromAgentVersion(agentVersion)).to.be.equal(client, `cannot parse ${name} agent version`);
});
}
});

0 comments on commit 59bd4c7

Please sign in to comment.