Skip to content

Commit

Permalink
feat: manually use identify protocol (#6400)
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain authored Feb 10, 2024
1 parent b1848ec commit 10ac136
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/beacon-node/src/network/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "@libp2p/interface";
import type {AddressManager, ConnectionManager, Registrar, TransportManager} from "@libp2p/interface-internal";
import type {Datastore} from "interface-datastore";
import {Identify} from "@chainsafe/libp2p-identify";
import {Slot, SlotRootHex, allForks, altair, capella, deneb, phase0} from "@lodestar/types";
import {PeerIdStr} from "../util/peerId.js";
import {INetworkEventBus} from "./events.js";
Expand Down Expand Up @@ -98,4 +99,4 @@ export type LodestarComponents = {
metrics?: Metrics;
};

export type Libp2p = ILibp2p<{components: LodestarComponents}>;
export type Libp2p = ILibp2p<{components: LodestarComponents; identify: Identify}>;
1 change: 1 addition & 0 deletions packages/beacon-node/src/network/libp2p/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export async function createNodeJsLibp2p(
services: {
identify: identify({
agentVersion: networkOpts.private ? "" : networkOpts.version ? `lodestar/${networkOpts.version}` : "lodestar",
runOnConnectionOpen: false,
}),
// individual components are specified because the components object is a Proxy
// and passing it here directly causes problems downstream, not to mention is slowwww
Expand Down
24 changes: 10 additions & 14 deletions packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {BitArray} from "@chainsafe/ssz";
import {SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params";
import {BeaconConfig} from "@lodestar/config";
import {allForks, altair, phase0} from "@lodestar/types";
import {retry, withTimeout} from "@lodestar/utils";
import {withTimeout} from "@lodestar/utils";
import {LoggerNode} from "@lodestar/logger/node";
import {GoodByeReasonCode, GOODBYE_KNOWN_CODES, Libp2pEvent} from "../../constants/index.js";
import {IClock} from "../../util/clock.js";
Expand Down Expand Up @@ -606,22 +606,18 @@ export class PeerManager {
void this.requestStatus(remotePeer, this.statusCache.get());
}

// AgentVersion was set in libp2p IdentifyService, 'peer:connect' event handler
// since it's not possible to handle it async, we have to wait for a while to set AgentVersion
// See https://github.com/libp2p/js-libp2p/pull/1168
retry(
async () => {
const agentVersionBytes = (await this.libp2p.peerStore.get(peerData.peerId)).metadata.get("AgentVersion");
if (agentVersionBytes) {
const agentVersion = new TextDecoder().decode(agentVersionBytes) || "N/A";
this.libp2p.services.identify
.identify(evt.detail)
.then((result) => {
const agentVersion = result.agentVersion;
if (agentVersion) {
peerData.agentVersion = agentVersion;
peerData.agentClient = getKnownClientFromAgentVersion(agentVersion);
}
},
{retries: 3, retryDelay: 1000}
).catch((err) => {
this.logger.debug("Error setting agentVersion for the peer", {peerId: peerData.peerId.toString()}, err);
});
})
.catch((err) => {
this.logger.debug("Error setting agentVersion for the peer", {peerId: peerData.peerId.toString()}, err);
});
};

/**
Expand Down

0 comments on commit 10ac136

Please sign in to comment.