Skip to content

Commit

Permalink
feat: update libp2p to 0.46.3 (#5869)
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain authored Aug 14, 2023
1 parent 4967d38 commit 4d5f777
Show file tree
Hide file tree
Showing 54 changed files with 504 additions and 712 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"karma-spec-reporter": "^0.0.36",
"karma-webpack": "^5.0.0",
"lerna": "^6.6.1",
"libp2p": "0.45.9",
"libp2p": "0.46.3",
"mocha": "^10.2.0",
"node-gyp": "^9.3.1",
"npm-run-all": "^4.1.5",
Expand Down
26 changes: 11 additions & 15 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,23 @@
"@chainsafe/bls": "7.1.1",
"@chainsafe/blst": "^0.2.9",
"@chainsafe/discv5": "^5.0.0",
"@chainsafe/libp2p-gossipsub": "^9.1.0",
"@chainsafe/libp2p-noise": "^12.0.1",
"@chainsafe/libp2p-gossipsub": "^10.0.0",
"@chainsafe/libp2p-noise": "^13.0.0",
"@chainsafe/persistent-merkle-tree": "^0.5.0",
"@chainsafe/prometheus-gc-stats": "^1.0.0",
"@chainsafe/ssz": "^0.10.2",
"@chainsafe/threads": "^1.11.1",
"@ethersproject/abi": "^5.7.0",
"@fastify/bearer-auth": "^9.0.0",
"@fastify/cors": "^8.2.1",
"@libp2p/bootstrap": "^8.0.0",
"@libp2p/interface-connection": "^5.1.0",
"@libp2p/interface-connection-manager": "^3.0.1",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-pubsub": "^4.0.1",
"@libp2p/interface-registrar": "^2.0.12",
"@libp2p/mdns": "^8.0.0",
"@libp2p/mplex": "^8.0.3",
"@libp2p/peer-id": "^2.0.3",
"@libp2p/peer-id-factory": "^2.0.3",
"@libp2p/prometheus-metrics": "^1.1.4",
"@libp2p/tcp": "7.0.1",
"@libp2p/bootstrap": "^9.0.2",
"@libp2p/interface": "^0.1.1",
"@libp2p/mdns": "^9.0.2",
"@libp2p/mplex": "^9.0.2",
"@libp2p/peer-id": "^3.0.1",
"@libp2p/peer-id-factory": "^3.0.2",
"@libp2p/prometheus-metrics": "^2.0.2",
"@libp2p/tcp": "8.0.2",
"@lodestar/api": "^1.10.0",
"@lodestar/config": "^1.10.0",
"@lodestar/db": "^1.10.0",
Expand All @@ -145,7 +141,7 @@
"it-all": "^3.0.2",
"it-pipe": "^3.0.1",
"jwt-simple": "0.5.6",
"libp2p": "0.45.9",
"libp2p": "0.46.3",
"multiformats": "^11.0.1",
"prom-client": "^14.2.0",
"qs": "^6.11.1",
Expand Down
23 changes: 11 additions & 12 deletions packages/beacon-node/src/api/impl/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Connection} from "@libp2p/interface-connection";
import {Connection, StreamStatus} from "@libp2p/interface/connection";
import {routes} from "@lodestar/api";
import {PeerStatus} from "../../../network/index.js";

/**
* Format a list of connections from libp2p connections manager into the API's format NodePeer
Expand All @@ -13,8 +12,8 @@ export function formatNodePeer(peerIdStr: string, connections: Connection[]): ro
// TODO: figure out how to get enr of peer
enr: "",
lastSeenP2pAddress: conn ? conn.remoteAddr.toString() : "",
direction: conn ? (conn.stat.direction as routes.node.PeerDirection) : null,
state: conn ? getPeerState(conn.stat.status) : "disconnected",
direction: conn ? (conn.direction as routes.node.PeerDirection) : null,
state: conn ? getPeerState(conn.status) : "disconnected",
};
}

Expand All @@ -25,26 +24,26 @@ export function formatNodePeer(peerIdStr: string, connections: Connection[]): ro
* - Otherwise, the first closed connection
*/
export function getRelevantConnection(connections: Connection[]): Connection | null {
const byStatus = new Map<PeerStatus, Connection>();
const byStatus = new Map<StreamStatus, Connection>();
for (const conn of connections) {
if (conn.stat.status === "OPEN") return conn;
if (!byStatus.has(conn.stat.status)) byStatus.set(conn.stat.status, conn);
if (conn.status === "open") return conn;
if (!byStatus.has(conn.status)) byStatus.set(conn.status, conn);
}

return byStatus.get("OPEN") || byStatus.get("CLOSING") || byStatus.get("CLOSED") || null;
return byStatus.get("open") || byStatus.get("closing") || byStatus.get("closed") || null;
}

/**
* Map libp2p connection status to the API's peer state notation
* @param status
*/
function getPeerState(status: PeerStatus): routes.node.PeerState {
function getPeerState(status: StreamStatus): routes.node.PeerState {
switch (status) {
case "OPEN":
case "open":
return "connected";
case "CLOSING":
case "closing":
return "disconnecting";
case "CLOSED":
case "closed":
default:
return "disconnected";
}
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/core/networkCore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {multiaddr} from "@multiformats/multiaddr";
import {Connection} from "@libp2p/interface-connection";
import {Connection} from "@libp2p/interface/connection";
import {PublishOpts} from "@chainsafe/libp2p-gossipsub/types";
import {PeerScoreStatsDump} from "@chainsafe/libp2p-gossipsub/dist/src/score/peer-score.js";
import {fromHexString} from "@chainsafe/ssz";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import worker_threads from "node:worker_threads";
import {exportToProtobuf} from "@libp2p/peer-id-factory";
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {PeerScoreStatsDump} from "@chainsafe/libp2p-gossipsub/dist/src/score/peer-score.js";
import {PublishOpts} from "@chainsafe/libp2p-gossipsub/types";
import {spawn, Thread, Worker} from "@chainsafe/threads";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/discv5/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EventEmitter from "events";
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import StrictEventEmitter from "strict-event-emitter-types";
import {exportToProtobuf} from "@libp2p/peer-id-factory";
import {createKeypairFromPeerId, ENR, ENRData, IKeypair, SignableENR} from "@chainsafe/discv5";
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EventEmitter} from "events";
import {PeerId} from "@libp2p/interface-peer-id";
import {TopicValidatorResult} from "@libp2p/interface-pubsub";
import {PeerId} from "@libp2p/interface/peer-id";
import {TopicValidatorResult} from "@libp2p/interface/pubsub";
import {phase0, RootHex} from "@lodestar/types";
import {BlockInput} from "../chain/blocks/types.js";
import {StrictEventEmitterSingleArg} from "../util/strictEvents.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/gossip/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {compress, uncompress} from "snappyjs";
import xxhashFactory from "xxhash-wasm";
import {Message} from "@libp2p/interface-pubsub";
import {Message} from "@libp2p/interface/pubsub";
import {digest} from "@chainsafe/as-sha256";
import {RPC} from "@chainsafe/libp2p-gossipsub/message";
import {intToBytes, toHex} from "@lodestar/utils";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/gossip/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Libp2p} from "libp2p";
import {Message, TopicValidatorResult} from "@libp2p/interface-pubsub";
import {Message, TopicValidatorResult} from "@libp2p/interface/pubsub";
import {PeerIdStr} from "@chainsafe/libp2p-gossipsub/types";
import {ForkName} from "@lodestar/params";
import {allForks, altair, capella, deneb, phase0, Slot} from "@lodestar/types";
Expand Down
4 changes: 0 additions & 4 deletions packages/beacon-node/src/network/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Libp2p as ILibp2p} from "libp2p";
import {Connection} from "@libp2p/interface-connection";
import {Components} from "libp2p/components";
import {Slot, SlotRootHex, allForks, altair, capella, deneb, phase0} from "@lodestar/types";
import {PeerIdStr} from "../util/peerId.js";
Expand Down Expand Up @@ -62,9 +61,6 @@ export interface INetwork extends INetworkCorePublic {
writeNetworkThreadProfile(durationMs?: number, dirpath?: string): Promise<string>;
}

export type PeerDirection = Connection["stat"]["direction"];
export type PeerStatus = Connection["stat"]["status"];

export type LodestarComponents = Pick<
Components,
| "peerId"
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/libp2p/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {Registry} from "prom-client";
import {ENR} from "@chainsafe/discv5";
import type {Components} from "libp2p/components";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/libp2p/noise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ConnectionEncrypter} from "@libp2p/interface-connection-encrypter";
import type {ConnectionEncrypter} from "@libp2p/interface/connection-encrypter";
import {newInstance, ChaCha20Poly1305} from "@chainsafe/as-chacha20poly1305";
import {ICryptoInterface, noise, pureJsCrypto} from "@chainsafe/libp2p-noise";
import {digest} from "@chainsafe/as-sha256";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {PublishOpts} from "@chainsafe/libp2p-gossipsub/types";
import {PeerScoreStatsDump} from "@chainsafe/libp2p-gossipsub/score";
import {BeaconConfig} from "@lodestar/config";
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/network/peers/discover.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {Multiaddr} from "@multiformats/multiaddr";
import type {PeerInfo} from "@libp2p/interface-peer-info";
import type {PeerInfo} from "@libp2p/interface/peer-info";
import {ENR} from "@chainsafe/discv5";
import {BeaconConfig} from "@lodestar/config";
import {pruneSetToMax, sleep} from "@lodestar/utils";
Expand Down Expand Up @@ -465,7 +465,7 @@ export class PeerDiscovery {
/** Check if there is 1+ open connection with this peer */
private isPeerConnected(peerIdStr: PeerIdStr): boolean {
const connections = getConnectionsMap(this.libp2p).get(peerIdStr);
return Boolean(connections && connections.some((connection) => connection.stat.status === "OPEN"));
return Boolean(connections && connections.some((connection) => connection.status === "open"));
}
}

Expand Down
44 changes: 20 additions & 24 deletions packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Connection} from "@libp2p/interface-connection";
import {PeerId} from "@libp2p/interface-peer-id";
import {Connection} from "@libp2p/interface/connection";
import {PeerId} from "@libp2p/interface/peer-id";
import {BitArray} from "@chainsafe/ssz";
import {SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params";
import {BeaconConfig} from "@lodestar/config";
Expand Down Expand Up @@ -324,7 +324,7 @@ export class PeerManager {
this.metrics?.peerGoodbyeReceived.inc({reason});

const conn = getConnection(this.libp2p, peer.toString());
if (conn && Date.now() - conn.stat.timeline.open > LONG_PEER_CONNECTION_MS) {
if (conn && Date.now() - conn.timeline.open > LONG_PEER_CONNECTION_MS) {
this.metrics?.peerLongConnectionDisconnect.inc({reason});
}

Expand Down Expand Up @@ -570,15 +570,13 @@ export class PeerManager {
* dialed or connecting to us.
*/
private onLibp2pPeerConnect = async (evt: CustomEvent<Connection>): Promise<void> => {
const libp2pConnection = evt.detail;
const {direction, status} = libp2pConnection.stat;
const peer = libp2pConnection.remotePeer;
this.logger.verbose("peer connected", {peer: prettyPrintPeerId(peer), direction, status});
const {direction, status, remotePeer} = evt.detail;
this.logger.verbose("peer connected", {peer: prettyPrintPeerId(remotePeer), direction, status});
// NOTE: The peerConnect event is not emitted here here, but after asserting peer relevance
this.metrics?.peerConnectedEvent.inc({direction, status});
// libp2p may emit closed connection, we don't want to handle it
// see https://github.com/libp2p/js-libp2p/issues/1565
if (this.connectedPeers.has(peer.toString()) || status !== "OPEN") {
if (this.connectedPeers.has(remotePeer.toString()) || status !== "open") {
return;
}

Expand All @@ -595,18 +593,18 @@ export class PeerManager {
connectedUnixTsMs: now,
relevantStatus: RelevantPeerStatus.Unknown,
direction,
peerId: peer,
peerId: remotePeer,
metadata: null,
agentVersion: null,
agentClient: null,
encodingPreference: null,
};
this.connectedPeers.set(peer.toString(), peerData);
this.connectedPeers.set(remotePeer.toString(), peerData);

if (direction === "outbound") {
//this.pingAndStatusTimeouts();
void this.requestPing(peer);
void this.requestStatus(peer, this.statusCache.get());
void this.requestPing(remotePeer);
void this.requestStatus(remotePeer, this.statusCache.get());
}

// AgentVersion was set in libp2p IdentifyService, 'peer:connect' event handler
Expand All @@ -626,19 +624,17 @@ export class PeerManager {
* The libp2p Upgrader has ended a connection
*/
private onLibp2pPeerDisconnect = (evt: CustomEvent<Connection>): void => {
const libp2pConnection = evt.detail;
const {direction, status} = libp2pConnection.stat;
const peer = libp2pConnection.remotePeer;
const {direction, status, remotePeer} = evt.detail;

// remove the ping and status timer for the peer
this.connectedPeers.delete(peer.toString());
this.connectedPeers.delete(remotePeer.toString());

this.logger.verbose("peer disconnected", {peer: prettyPrintPeerId(peer), direction, status});
this.networkEventBus.emit(NetworkEvent.peerDisconnected, {peer: peer.toString()});
this.logger.verbose("peer disconnected", {peer: prettyPrintPeerId(remotePeer), direction, status});
this.networkEventBus.emit(NetworkEvent.peerDisconnected, {peer: remotePeer.toString()});
this.metrics?.peerDisconnectedEvent.inc({direction});
this.libp2p.peerStore
.merge(peer, {tags: {[PEER_RELEVANT_TAG]: undefined}})
.catch((e) => this.logger.verbose("cannot untag peer", {peerId: peer.toString()}, e as Error));
.merge(remotePeer, {tags: {[PEER_RELEVANT_TAG]: undefined}})
.catch((e) => this.logger.verbose("cannot untag peer", {peerId: remotePeer.toString()}, e as Error));
};

private async disconnect(peer: PeerId): Promise<void> {
Expand All @@ -655,7 +651,7 @@ export class PeerManager {
this.metrics?.peerGoodbyeSent.inc({reason});

const conn = getConnection(this.libp2p, peer.toString());
if (conn && Date.now() - conn.stat.timeline.open > LONG_PEER_CONNECTION_MS) {
if (conn && Date.now() - conn.timeline.open > LONG_PEER_CONNECTION_MS) {
this.metrics?.peerLongConnectionDisconnect.inc({reason});
}

Expand Down Expand Up @@ -688,9 +684,9 @@ export class PeerManager {
}

for (const connections of getConnectionsMap(this.libp2p).values()) {
const openCnx = connections.find((cnx) => cnx.stat.status === "OPEN");
const openCnx = connections.find((cnx) => cnx.status === "open");
if (openCnx) {
const direction = openCnx.stat.direction;
const direction = openCnx.direction;
peersByDirection.set(direction, 1 + (peersByDirection.get(direction) ?? 0));
const peerId = openCnx.remotePeer;
const peerData = this.connectedPeers.get(peerId.toString());
Expand All @@ -703,7 +699,7 @@ export class PeerManager {
metrics.peerLongLivedAttnets.observe(attnets ? attnets.getTrueBitIndexes().length : 0);
metrics.peerScoreByClient.observe({client}, this.peerRpcScores.getScore(peerId));
metrics.peerGossipScoreByClient.observe({client}, this.peerRpcScores.getGossipScore(peerId));
metrics.peerConnectionLength.observe((now - openCnx.stat.timeline.open) / 1000);
metrics.peerConnectionLength.observe((now - openCnx.timeline.open) / 1000);
total++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/peers/peersData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {altair} from "@lodestar/types";
import {Encoding} from "@lodestar/reqresp";
import {ClientKind} from "./client.js";
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
@@ -1,4 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {NetworkCoreMetrics} from "../../core/metrics.js";

export type PeerIdStr = string;
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/peers/score/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {MapDef, pruneSetToMax} from "@lodestar/utils";
import {NetworkCoreMetrics} from "../../core/metrics.js";
import {DEFAULT_SCORE, MAX_ENTRIES, MAX_SCORE, MIN_SCORE, SCORE_THRESHOLD} from "./constants.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Connection} from "@libp2p/interface-connection";
import {PeerId} from "@libp2p/interface-peer-id";
import {Connection} from "@libp2p/interface/connection";
import {PeerId} from "@libp2p/interface/peer-id";
import {Libp2p} from "../../interface.js";
import {getConnectionsMap} from "../../util.js";

Expand Down Expand Up @@ -30,5 +30,5 @@ export function hasSomeConnectedPeer(libp2p: Libp2p): boolean {
}

function isConnectionOpen(connection: Connection): boolean {
return connection.stat.status === "OPEN";
return connection.status === "open";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {Direction} from "@libp2p/interface-connection";
import {PeerId} from "@libp2p/interface/peer-id";
import {Direction} from "@libp2p/interface/connection";
import {BitArray} from "@chainsafe/ssz";
import {altair, phase0} from "@lodestar/types";
import {ATTESTATION_SUBNET_COUNT, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TopicValidatorResult} from "@libp2p/interface-pubsub";
import {TopicValidatorResult} from "@libp2p/interface/pubsub";
import {ChainForkConfig} from "@lodestar/config";
import {Logger} from "@lodestar/utils";
import {Metrics} from "../../metrics/index.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/processor/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Message} from "@libp2p/interface-pubsub";
import {Message} from "@libp2p/interface/pubsub";
import {Slot, SlotOptionalRoot} from "@lodestar/types";
import {GossipTopic, GossipType} from "../gossip/index.js";
import {PeerIdStr} from "../../util/peerId.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";
import {Libp2p} from "libp2p";
import {BeaconConfig} from "@lodestar/config";
import {ForkName, ForkSeq} from "@lodestar/params";
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/reqresp/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {PeerId} from "@libp2p/interface/peer-id";

/**
* Rate limiter interface for inbound and outbound requests.
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {PeerId} from "@libp2p/interface-peer-id";
import type {Connection} from "@libp2p/interface-connection";
import type {PeerId} from "@libp2p/interface/peer-id";
import type {Connection} from "@libp2p/interface/connection";
import type {DefaultConnectionManager} from "libp2p/connection-manager/index.js";
import type {PeerIdStr} from "../util/peerId.js";
import type {Libp2p} from "./interface.js";
Expand Down
Loading

0 comments on commit 4d5f777

Please sign in to comment.