Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: revert "chore: upgrade to js-libp2p 2.0 (#7077)" #7202

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"https-browserify": "^1.0.0",
"jsdom": "^23.0.1",
"lerna": "^7.3.0",
"libp2p": "2.1.7",
"libp2p": "1.4.3",
"mocha": "^10.2.0",
"node-gyp": "^9.4.0",
"npm-run-all": "^4.1.5",
Expand Down
35 changes: 18 additions & 17 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@
"dependencies": {
"@chainsafe/as-sha256": "^0.5.0",
"@chainsafe/blst": "^2.1.0",
"@chainsafe/discv5": "^10.0.1",
"@chainsafe/enr": "^4.0.1",
"@chainsafe/libp2p-gossipsub": "^14.1.0",
"@chainsafe/libp2p-noise": "^16.0.0",
"@chainsafe/discv5": "^9.0.0",
"@chainsafe/enr": "^3.0.0",
"@chainsafe/libp2p-gossipsub": "^13.0.0",
"@chainsafe/libp2p-identify": "^1.0.0",
"@chainsafe/libp2p-noise": "^15.0.0",
"@chainsafe/persistent-merkle-tree": "^0.8.0",
"@chainsafe/prometheus-gc-stats": "^1.0.0",
"@chainsafe/ssz": "^0.18.0",
Expand All @@ -110,15 +111,15 @@
"@fastify/cors": "^10.0.1",
"@fastify/swagger": "^9.0.0",
"@fastify/swagger-ui": "^5.0.1",
"@libp2p/bootstrap": "^11.0.4",
"@libp2p/crypto": "^5.0.4",
"@libp2p/identify": "^3.0.4",
"@libp2p/interface": "^2.1.2",
"@libp2p/mdns": "^11.0.4",
"@libp2p/mplex": "^11.0.4",
"@libp2p/peer-id": "^5.0.4",
"@libp2p/prometheus-metrics": "^4.1.2",
"@libp2p/tcp": "10.0.4",
"@libp2p/bootstrap": "^10.0.21",
"@libp2p/identify": "^1.0.20",
"@libp2p/interface": "^1.3.0",
"@libp2p/mdns": "^10.0.21",
"@libp2p/mplex": "^10.0.21",
"@libp2p/peer-id": "^4.1.0",
"@libp2p/peer-id-factory": "^4.1.0",
"@libp2p/prometheus-metrics": "^3.0.21",
"@libp2p/tcp": "9.0.23",
"@lodestar/api": "^1.22.0",
"@lodestar/config": "^1.22.0",
"@lodestar/db": "^1.22.0",
Expand All @@ -133,15 +134,15 @@
"@lodestar/validator": "^1.22.0",
"@multiformats/multiaddr": "^12.1.3",
"c-kzg": "^2.1.2",
"datastore-core": "^10.0.0",
"datastore-level": "^11.0.0",
"datastore-core": "^9.1.1",
"datastore-level": "^10.1.1",
"deepmerge": "^4.3.1",
"fastify": "^5.0.0",
"interface-datastore": "^8.3.0",
"interface-datastore": "^8.2.7",
"it-all": "^3.0.4",
"it-pipe": "^3.0.1",
"jwt-simple": "0.5.6",
"libp2p": "2.1.7",
"libp2p": "1.4.3",
"multiformats": "^11.0.1",
"prom-client": "^15.1.0",
"qs": "^6.11.1",
Expand Down
17 changes: 6 additions & 11 deletions packages/beacon-node/src/network/core/networkCore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Connection, PrivateKey} from "@libp2p/interface";
import {Connection, PeerId} from "@libp2p/interface";
import {multiaddr} from "@multiformats/multiaddr";
import {PublishOpts} from "@chainsafe/libp2p-gossipsub/types";
import {PeerScoreStatsDump} from "@chainsafe/libp2p-gossipsub/dist/src/score/peer-score.js";
Expand Down Expand Up @@ -55,7 +55,7 @@ type Mods = {
export type BaseNetworkInit = {
opts: NetworkOptions;
config: BeaconConfig;
privateKey: PrivateKey;
peerId: PeerId;
peerStoreDir: string | undefined;
logger: LoggerNode;
metricsRegistry: RegistryMetricCreator | null;
Expand Down Expand Up @@ -126,7 +126,7 @@ export class NetworkCore implements INetworkCore {
static async init({
opts,
config,
privateKey,
peerId,
peerStoreDir,
logger,
metricsRegistry,
Expand All @@ -136,7 +136,7 @@ export class NetworkCore implements INetworkCore {
activeValidatorCount,
initialStatus,
}: BaseNetworkInit): Promise<NetworkCore> {
const libp2p = await createNodeJsLibp2p(privateKey, opts, {
const libp2p = await createNodeJsLibp2p(peerId, opts, {
peerStoreDir,
metrics: Boolean(metricsRegistry),
metricsRegistry: metricsRegistry ?? undefined,
Expand Down Expand Up @@ -200,9 +200,8 @@ export class NetworkCore implements INetworkCore {

const peerManager = await PeerManager.init(
{
privateKey,
libp2p,
gossip,
gossip: gossip,
reqResp,
attnetsService,
syncnetsService,
Expand Down Expand Up @@ -363,11 +362,7 @@ export class NetworkCore implements INetworkCore {
}

getConnectionsByPeer(): Map<string, Connection[]> {
const m = new Map<string, Connection[]>();
for (const [k, v] of getConnectionsMap(this.libp2p).entries()) {
m.set(k, v.value);
}
return m;
return getConnectionsMap(this.libp2p);
}

async getConnectedPeers(): Promise<PeerIdStr[]> {
Expand Down
8 changes: 3 additions & 5 deletions packages/beacon-node/src/network/core/networkCoreWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import path from "node:path";
import worker from "node:worker_threads";
import type {ModuleThread} from "@chainsafe/threads";
import {expose} from "@chainsafe/threads/worker";
import {privateKeyFromProtobuf} from "@libp2p/crypto/keys";
import {peerIdFromPrivateKey} from "@libp2p/peer-id";
import {createFromProtobuf} from "@libp2p/peer-id-factory";
import {chainConfigFromJson, createBeaconConfig} from "@lodestar/config";
import {getNodeLogger} from "@lodestar/logger/node";
import {RegistryMetricCreator, collectNodeJSMetrics} from "../../metrics/index.js";
Expand Down Expand Up @@ -33,8 +32,7 @@ if (!workerData) throw Error("workerData must be defined");
if (!parentPort) throw Error("parentPort must be defined");

const config = createBeaconConfig(chainConfigFromJson(workerData.chainConfigJson), workerData.genesisValidatorsRoot);
const privateKey = privateKeyFromProtobuf(workerData.privateKeyProto);
const peerId = peerIdFromPrivateKey(privateKey);
const peerId = await createFromProtobuf(workerData.peerIdProto);

// TODO: Pass options from main thread for logging
// TODO: Logging won't be visible in file loggers
Expand Down Expand Up @@ -94,7 +92,7 @@ if (networkCoreWorkerMetrics) {
const core = await NetworkCore.init({
opts: workerData.opts,
config,
privateKey,
peerId,
peerStoreDir: workerData.peerStoreDir,
logger,
metricsRegistry: metricsRegister,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import workerThreads from "node:worker_threads";
import {PeerScoreStatsDump} from "@chainsafe/libp2p-gossipsub/dist/src/score/peer-score.js";
import {PublishOpts} from "@chainsafe/libp2p-gossipsub/types";
import {ModuleThread, Thread, Worker, spawn} from "@chainsafe/threads";
import {PrivateKey} from "@libp2p/interface";
import {privateKeyToProtobuf} from "@libp2p/crypto/keys";
import {PeerId, Secp256k1PeerId} from "@libp2p/interface";
import {exportToProtobuf} from "@libp2p/peer-id-factory";
import {routes} from "@lodestar/api";
import {BeaconConfig, chainConfigToJson} from "@lodestar/config";
import type {LoggerNode} from "@lodestar/logger/node";
Expand Down Expand Up @@ -44,7 +44,7 @@ export type WorkerNetworkCoreInitModules = {
opts: WorkerNetworkCoreOpts;
config: BeaconConfig;
logger: LoggerNode;
privateKey: PrivateKey;
peerId: PeerId;
events: NetworkEventBus;
metrics: Metrics | null;
getReqRespHandler: GetReqRespHandlerFn;
Expand Down Expand Up @@ -103,14 +103,14 @@ export class WorkerNetworkCore implements INetworkCore {
}

static async init(modules: WorkerNetworkCoreInitModules): Promise<WorkerNetworkCore> {
const {opts, config, privateKey} = modules;
const {opts, config, peerId} = modules;
const {genesisTime, peerStoreDir, activeValidatorCount, localMultiaddrs, metricsEnabled, initialStatus} = opts;

const workerData: NetworkWorkerData = {
opts,
chainConfigJson: chainConfigToJson(config),
genesisValidatorsRoot: config.genesisValidatorsRoot,
privateKeyProto: privateKeyToProtobuf(privateKey),
peerIdProto: exportToProtobuf(peerId as Secp256k1PeerId),
localMultiaddrs,
metricsEnabled,
peerStoreDir,
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export type NetworkWorkerData = {
genesisTime: number;
activeValidatorCount: number;
initialStatus: phase0.Status;
privateKeyProto: Uint8Array;
peerIdProto: Uint8Array;
localMultiaddrs: string[];
metricsEnabled: boolean;
peerStoreDir?: string;
Expand Down
14 changes: 8 additions & 6 deletions packages/beacon-node/src/network/discv5/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import EventEmitter from "node:events";
import {privateKeyToProtobuf} from "@libp2p/crypto/keys";
import {PrivateKey} from "@libp2p/interface";
import {PeerId, Secp256k1PeerId} from "@libp2p/interface";
import {StrictEventEmitter} from "strict-event-emitter-types";
import {ENR, ENRData, SignableENR} from "@chainsafe/enr";
import {exportToProtobuf} from "@libp2p/peer-id-factory";
import {createPrivateKeyFromPeerId, ENR, ENRData, SignableENR} from "@chainsafe/enr";
import {spawn, Thread, Worker} from "@chainsafe/threads";
import {chainConfigFromJson, chainConfigToJson, BeaconConfig} from "@lodestar/config";
import {LoggerNode} from "@lodestar/logger/node";
import {NetworkCoreMetrics} from "../core/metrics.js";
import {Discv5WorkerApi, Discv5WorkerData, LodestarDiscv5Opts} from "./types.js";

export type Discv5Opts = {
privateKey: PrivateKey;
peerId: PeerId;
discv5: LodestarDiscv5Opts;
logger: LoggerNode;
config: BeaconConfig;
Expand All @@ -25,6 +25,7 @@ export type Discv5Events = {
* Wrapper class abstracting the details of discv5 worker instantiation and message-passing
*/
export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<EventEmitter, Discv5Events>}) {
private readonly keypair;
private readonly subscription: {unsubscribe: () => void};
private closed = false;

Expand All @@ -34,13 +35,14 @@ export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<E
) {
super();

this.keypair = createPrivateKeyFromPeerId(this.opts.peerId);
this.subscription = workerApi.discovered().subscribe((enrObj) => this.onDiscovered(enrObj));
}

static async init(opts: Discv5Opts): Promise<Discv5Worker> {
const workerData: Discv5WorkerData = {
enr: opts.discv5.enr,
privateKeyProto: privateKeyToProtobuf(opts.privateKey),
peerIdProto: exportToProtobuf(opts.peerId as Secp256k1PeerId),
bindAddrs: opts.discv5.bindAddrs,
config: opts.discv5.config ?? {},
bootEnrs: opts.discv5.bootEnrs,
Expand Down Expand Up @@ -78,7 +80,7 @@ export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<E

async enr(): Promise<SignableENR> {
const obj = await this.workerApi.enr();
return new SignableENR(obj.kvs, obj.seq, this.opts.privateKey.raw);
return new SignableENR(obj.kvs, obj.seq, this.keypair.privateKey);
}

setEnrValue(key: string, value: Uint8Array): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/discv5/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type LodestarDiscv5Opts = {
/** discv5 worker constructor data */
export interface Discv5WorkerData {
enr: string;
privateKeyProto: Uint8Array;
peerIdProto: Uint8Array;
bindAddrs: BindAddrs;
config: Discv5Config;
bootEnrs: string[];
Expand Down
13 changes: 6 additions & 7 deletions packages/beacon-node/src/network/discv5/worker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import worker from "node:worker_threads";
import path from "node:path";
import fs from "node:fs";
import {createFromProtobuf} from "@libp2p/peer-id-factory";
import {Multiaddr, multiaddr} from "@multiformats/multiaddr";
import {expose} from "@chainsafe/threads/worker";
import {Observable, Subject} from "@chainsafe/threads/observable";
import {Discv5} from "@chainsafe/discv5";
import {ENR, ENRData, SignableENR, SignableENRData} from "@chainsafe/enr";
import {privateKeyFromProtobuf} from "@libp2p/crypto/keys";
import {peerIdFromPrivateKey} from "@libp2p/peer-id";
import {createPrivateKeyFromPeerId, ENR, ENRData, SignableENR, SignableENRData} from "@chainsafe/enr";
import {createBeaconConfig} from "@lodestar/config";
import {getNodeLogger} from "@lodestar/logger/node";
import {Gauge} from "@lodestar/utils";
Expand Down Expand Up @@ -43,15 +42,15 @@ if (workerData.metrics) {
});
}

const privateKey = privateKeyFromProtobuf(workerData.privateKeyProto);
const peerId = peerIdFromPrivateKey(privateKey);
const peerId = await createFromProtobuf(workerData.peerIdProto);
const keypair = createPrivateKeyFromPeerId(peerId);

const config = createBeaconConfig(workerData.chainConfig, workerData.genesisValidatorsRoot);

// Initialize discv5
const discv5 = Discv5.create({
enr: SignableENR.decodeTxt(workerData.enr, privateKey.raw),
privateKey,
enr: SignableENR.decodeTxt(workerData.enr, keypair.privateKey),
peerId,
bindAddrs: {
ip4: (workerData.bindAddrs.ip4 ? multiaddr(workerData.bindAddrs.ip4) : undefined) as Multiaddr,
ip6: workerData.bindAddrs.ip6 ? multiaddr(workerData.bindAddrs.ip6) : undefined,
Expand Down
4 changes: 0 additions & 4 deletions packages/beacon-node/src/network/gossip/gossipsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ export class Eth2Gossipsub extends GossipSub {
// if this is false, only publish to mesh peers. If there is not enough GOSSIP_D mesh peers,
// publish to some more topic peers to make sure we always publish to at least GOSSIP_D peers
floodPublish: !opts?.disableFloodPublish,
// Only send IDONTWANT messages if the message size is larger than this
// This should be large enough to not send IDONTWANT for "small" messages
// See https://github.com/ChainSafe/lodestar/pull/7077#issuecomment-2383679472
idontwantMinDataSize: 16829,
});
this.scoreParams = scoreParams;
this.config = config;
Expand Down
4 changes: 1 addition & 3 deletions packages/beacon-node/src/network/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import {
PeerRouting,
PeerStore,
Upgrader,
PrivateKey,
} from "@libp2p/interface";
import type {AddressManager, ConnectionManager, Registrar, TransportManager} from "@libp2p/interface-internal";
import type {Datastore} from "interface-datastore";
import {Identify} from "@libp2p/identify";
import {Identify} from "@chainsafe/libp2p-identify";
import {
LightClientFinalityUpdate,
LightClientOptimisticUpdate,
Expand Down Expand Up @@ -94,7 +93,6 @@ export interface INetwork extends INetworkCorePublic {

export type LodestarComponents = {
peerId: PeerId;
privateKey: PrivateKey;
nodeInfo: NodeInfo;
logger: ComponentLogger;
events: TypedEventTarget<Libp2pEvents>;
Expand Down
21 changes: 11 additions & 10 deletions packages/beacon-node/src/network/libp2p/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {PrivateKey} from "@libp2p/interface";
import {PeerId} from "@libp2p/interface";
import {Registry} from "prom-client";
import {ENR} from "@chainsafe/enr";
import {identify} from "@libp2p/identify";
// TODO: We should use this fork until https://github.com/libp2p/js-libp2p/pull/2387
import {identify} from "@chainsafe/libp2p-identify";
import {bootstrap} from "@libp2p/bootstrap";
import {mdns} from "@libp2p/mdns";
import {createLibp2p} from "libp2p";
Expand Down Expand Up @@ -33,7 +34,7 @@ export async function getDiscv5Multiaddrs(bootEnrs: string[]): Promise<string[]>
}

export async function createNodeJsLibp2p(
privateKey: PrivateKey,
peerId: PeerId,
networkOpts: Partial<NetworkOptions> = {},
nodeJsLibp2pOpts: NodeJsLibp2pOpts = {}
): Promise<Libp2p> {
Expand Down Expand Up @@ -64,12 +65,12 @@ export async function createNodeJsLibp2p(
}

return createLibp2p({
privateKey,
peerId,
addresses: {
listen: localMultiaddrs,
announce: [],
},
connectionEncrypters: [noise()],
connectionEncryption: [noise()],
// Reject connections when the server's connection count gets high
transports: [
tcp({
Expand Down Expand Up @@ -98,14 +99,15 @@ export async function createNodeJsLibp2p(
maxParallelDials: 100,
maxPeerAddrsToDial: 4,
dialTimeout: 30_000,

// Rely entirely on lodestar's peer manager to prune connections
//maxConnections: options.maxConnections,
// DOCS: There is no way to turn off autodial other than setting minConnections to 0
minConnections: 0,
// the maximum number of pending connections libp2p will accept before it starts rejecting incoming connections.
// make it the same to backlog option above
maxIncomingPendingConnections: 5,
},
// rely on lodestar's peer manager to ping peers
connectionMonitor: {
enabled: false,
},
datastore,
services: {
identify: identify({
Expand All @@ -116,7 +118,6 @@ export async function createNodeJsLibp2p(
// and passing it here directly causes problems downstream, not to mention is slowwww
components: (components: LodestarComponents) => ({
peerId: components.peerId,
privateKey: components.privateKey,
nodeInfo: components.nodeInfo,
logger: components.logger,
events: components.events,
Expand Down
Loading
Loading