Skip to content

Commit

Permalink
Merge eb78f23 into ce33cb5
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain authored Jan 3, 2023
2 parents ce33cb5 + eb78f23 commit 9683b84
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 551 deletions.
24 changes: 12 additions & 12 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@
"@chainsafe/as-sha256": "^0.3.1",
"@chainsafe/bls": "7.1.1",
"@chainsafe/discv5": "^1.4.0",
"@chainsafe/libp2p-gossipsub": "^4.1.1",
"@chainsafe/libp2p-gossipsub": "^5.3.0",
"@chainsafe/libp2p-noise": "^10.2.0",
"@chainsafe/persistent-merkle-tree": "^0.4.2",
"@chainsafe/snappy-stream": "5.1.1",
"@chainsafe/ssz": "^0.9.2",
"@chainsafe/threads": "^1.10.0",
"@ethersproject/abi": "^5.0.0",
"@libp2p/bootstrap": "^2.0.0",
"@libp2p/bootstrap": "^5.0.0",
"@libp2p/interface-connection": "^3.0.2",
"@libp2p/interface-connection-manager": "^1.1.1",
"@libp2p/interface-peer-id": "^1.0.4",
"@libp2p/interface-peer-info": "^1.0.3",
"@libp2p/interface-pubsub": "^2.1.0",
"@libp2p/mdns": "^3.0.1",
"@libp2p/mplex": "^5.2.4",
"@libp2p/peer-id-factory": "^1.0.18",
"@libp2p/tcp": "^3.1.2",
"@libp2p/interface-connection-manager": "^1.3.0",
"@libp2p/interface-peer-id": "^1.0.5",
"@libp2p/interface-pubsub": "^3.0.0",
"@libp2p/mdns": "^5.1.0",
"@libp2p/mplex": "^7.1.0",
"@libp2p/peer-id-factory": "^1.0.19",
"@libp2p/prometheus-metrics": "^1.1.2",
"@libp2p/tcp": "^6.0.4",
"@lodestar/api": "^1.3.0",
"@lodestar/config": "^1.3.0",
"@lodestar/db": "^1.3.0",
Expand All @@ -137,9 +137,9 @@
"gc-stats": "^1.4.0",
"interface-datastore": "^7.0.0",
"it-all": "^2.0.0",
"it-pipe": "^2.0.4",
"it-pipe": "^2.0.5",
"jwt-simple": "0.5.6",
"libp2p": "0.39.2",
"libp2p": "0.41.0",
"prom-client": "^14.1.0",
"prometheus-gc-stats": "^0.6.3",
"snappyjs": "^0.7.0",
Expand Down
84 changes: 0 additions & 84 deletions packages/beacon-node/src/metrics/metrics/libp2p.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/beacon-node/src/network/gossip/gossipsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class Eth2Gossipsub extends GossipSub {

// Gossipsub parameters defined here:
// https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/p2p-interface.md#the-gossip-domain-gossipsub
super({
super(modules.libp2p, {
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
allowPublishToZeroPeers: allowPublishToZeroPeers,
D: gossipsubD ?? GOSSIP_D,
Expand Down
6 changes: 3 additions & 3 deletions packages/beacon-node/src/network/gossip/interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {EventEmitter} from "events";
import {Libp2p} from "libp2p";
import {Message} from "@libp2p/interface-pubsub";
import {Message, TopicValidatorResult} from "@libp2p/interface-pubsub";
import StrictEventEmitter from "strict-event-emitter-types";
import {MessageAcceptance, PeerIdStr} from "@chainsafe/libp2p-gossipsub/types";
import {PeerIdStr} from "@chainsafe/libp2p-gossipsub/types";
import {ForkName} from "@lodestar/params";
import {allForks, altair, capella, eip4844, phase0} from "@lodestar/types";
import {IBeaconConfig} from "@lodestar/config";
Expand Down Expand Up @@ -138,7 +138,7 @@ export type GossipValidatorFn = (
msg: Message,
propagationSource: PeerIdStr,
seenTimestampSec: number
) => Promise<MessageAcceptance>;
) => Promise<TopicValidatorResult>;

export type ValidatorFnsByType = {[K in GossipType]: GossipValidatorFn};

Expand Down
12 changes: 6 additions & 6 deletions packages/beacon-node/src/network/gossip/validation/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MessageAcceptance} from "@chainsafe/libp2p-gossipsub/types";
import {TopicValidatorResult} from "@libp2p/interface-pubsub";
import {IChainForkConfig} from "@lodestar/config";
import {ILogger, mapValues} from "@lodestar/utils";
import {IMetrics} from "../../../metrics/index.js";
Expand Down Expand Up @@ -78,19 +78,19 @@ function getGossipValidatorFn<K extends GossipType>(
gossipObject = sszType.deserialize(msg.data);
} catch (e) {
// TODO: Log the error or do something better with it
return MessageAcceptance.Reject;
return TopicValidatorResult.Reject;
}

await (gossipHandler as GossipHandlerFn)(gossipObject, topic, propagationSource, seenTimestampSec);

metrics?.gossipValidationAccept.inc({topic: type});

return MessageAcceptance.Accept;
return TopicValidatorResult.Accept;
} catch (e) {
if (!(e instanceof GossipActionError)) {
// not deserve to log error here, it looks too dangerous to users
logger.debug(`Gossip validation ${type} threw a non-GossipActionError`, {}, e as Error);
return MessageAcceptance.Ignore;
return TopicValidatorResult.Ignore;
}

// Metrics on specific error reason
Expand All @@ -100,11 +100,11 @@ function getGossipValidatorFn<K extends GossipType>(
switch (e.action) {
case GossipAction.IGNORE:
metrics?.gossipValidationIgnore.inc({topic: type});
return MessageAcceptance.Ignore;
return TopicValidatorResult.Ignore;

case GossipAction.REJECT:
metrics?.gossipValidationReject.inc({topic: type});
return MessageAcceptance.Reject;
return TopicValidatorResult.Reject;
}
}
};
Expand Down
2 changes: 0 additions & 2 deletions packages/beacon-node/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ export class Network implements INetwork {
},
peersData: this.peersData,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
void this.gossip.init((libp2p as any).components).catch((e) => this.logger.error(e));

this.attnetsService = new AttnetsService(config, chain, this.gossip, metadata, logger, metrics, opts);
this.syncnetsService = new SyncnetsService(config, chain, this.gossip, metadata, logger, metrics, opts);
Expand Down
35 changes: 21 additions & 14 deletions packages/beacon-node/src/network/nodejs/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {createLibp2p, Libp2p} from "libp2p";
import {TCP} from "@libp2p/tcp";
import {Mplex} from "@libp2p/mplex";
import {Bootstrap} from "@libp2p/bootstrap";
import {MulticastDNS} from "@libp2p/mdns";
import {tcp} from "@libp2p/tcp";
import {mplex} from "@libp2p/mplex";
import {bootstrap} from "@libp2p/bootstrap";
import {mdns} from "@libp2p/mdns";
import {PeerId} from "@libp2p/interface-peer-id";
import {Datastore} from "interface-datastore";
import type {PeerDiscovery} from "@libp2p/interface-peer-discovery";
import type {Components} from "libp2p/components";
import {prometheusMetrics} from "@libp2p/prometheus-metrics";
import {Registry} from "prom-client";
import {createNoise} from "./noise.js";

export interface ILibp2pOptions {
Expand All @@ -14,11 +18,12 @@ export interface ILibp2pOptions {
announce?: string[];
};
datastore?: Datastore;
peerDiscovery?: (Bootstrap | MulticastDNS)[];
peerDiscovery?: ((components: Components) => PeerDiscovery)[];
bootMultiaddrs?: string[];
maxConnections?: number;
minConnections?: number;
metrics?: boolean;
metricsRegistry?: Registry;
lodestarVersion?: string;
mdns?: boolean;
}
Expand All @@ -29,10 +34,10 @@ export async function createNodejsLibp2p(options: ILibp2pOptions): Promise<Libp2
peerDiscovery.push(...options.peerDiscovery);
} else {
if ((options.bootMultiaddrs?.length ?? 0) > 0) {
peerDiscovery.push(new Bootstrap({interval: 2000, list: options.bootMultiaddrs ?? []}));
peerDiscovery.push(bootstrap({list: options.bootMultiaddrs ?? []}));
}
if (options.mdns) {
peerDiscovery.push(new MulticastDNS());
peerDiscovery.push(mdns());
}
}
return await createLibp2p({
Expand All @@ -42,14 +47,16 @@ export async function createNodejsLibp2p(options: ILibp2pOptions): Promise<Libp2
announce: options.addresses.announce || [],
},
connectionEncryption: [createNoise()],
transports: [new TCP()],
streamMuxers: [new Mplex({maxInboundStreams: 256})],
transports: [tcp()],
streamMuxers: [mplex({maxInboundStreams: 256})],
peerDiscovery,
metrics: {
// temporarily disable since there is a performance issue with it
// see https://github.com/ChainSafe/lodestar/issues/4698
enabled: false,
},
metrics: options.metrics
? prometheusMetrics({
collectDefaultMetrics: false,
preserveExistingMetrics: true,
registry: options.metricsRegistry,
})
: undefined,
connectionManager: {
// dialer config
maxParallelDials: 100,
Expand Down
5 changes: 2 additions & 3 deletions packages/beacon-node/src/network/nodejs/noise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const lodestarCrypto: ICryptoInterface = {
},
};

export function createNoise(): ConnectionEncrypter {
const factory = noise({crypto: lodestarCrypto});
return factory() as ConnectionEncrypter;
export function createNoise(): () => ConnectionEncrypter {
return noise({crypto: lodestarCrypto});
}
3 changes: 3 additions & 0 deletions packages/beacon-node/src/network/nodejs/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {Libp2p} from "libp2p";
import {Registry} from "prom-client";
import {ENR} from "@chainsafe/discv5";
import {Eth2PeerDataStore} from "../peers/datastore.js";
import {defaultDiscv5Options, defaultNetworkOptions, INetworkOptions} from "../options.js";
Expand All @@ -10,6 +11,7 @@ export type NodeJsLibp2pOpts = {
peerStoreDir?: string;
disablePeerDiscovery?: boolean;
metrics?: boolean;
metricsRegistry?: Registry;
};

/**
Expand Down Expand Up @@ -73,6 +75,7 @@ export async function createNodeJsLibp2p(
// If peer discovery is enabled let the default in NodejsNode
peerDiscovery: disablePeerDiscovery ? [] : undefined,
metrics: nodeJsLibp2pOpts.metrics,
metricsRegistry: nodeJsLibp2pOpts.metricsRegistry,
lodestarVersion: networkOpts.version,
mdns: networkOpts.mdns,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {Libp2p} from "libp2p";
import {Connection} from "@libp2p/interface-connection";
import {PeerId} from "@libp2p/interface-peer-id";
import type {ConnectionManager} from "@libp2p/interface-connection-manager";
import {getConnectionsMap} from "../../util.js";

/**
* Return peers with at least one connection in status "open"
*/
export function getConnectedPeerIds(libp2p: Libp2p): PeerId[] {
const peerIds: PeerId[] = [];
for (const connections of getConnectionsMap(libp2p.connectionManager).values()) {
for (const connections of getConnectionsMap(libp2p.connectionManager as ConnectionManager).values()) {
const openConnection = connections.find(isConnectionOpen);
if (openConnection) {
peerIds.push(openConnection.remotePeer);
Expand All @@ -21,7 +22,7 @@ export function getConnectedPeerIds(libp2p: Libp2p): PeerId[] {
* Efficiently check if there is at least one peer connected
*/
export function hasSomeConnectedPeer(libp2p: Libp2p): boolean {
for (const connections of getConnectionsMap(libp2p.connectionManager).values()) {
for (const connections of getConnectionsMap(libp2p.connectionManager as ConnectionManager).values()) {
if (connections.some(isConnectionOpen)) {
return true;
}
Expand Down
2 changes: 0 additions & 2 deletions packages/beacon-node/src/node/nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {getApi, BeaconRestApiServer} from "../api/index.js";
import {initializeExecutionEngine, initializeExecutionBuilder} from "../execution/index.js";
import {initializeEth1ForBlockProduction} from "../eth1/index.js";
import {initCKZG, loadEthereumTrustedSetup} from "../util/kzg.js";
import {createLibp2pMetrics} from "../metrics/metrics/libp2p.js";
import {IBeaconNodeOptions} from "./options.js";
import {runNodeNotifier} from "./notifier.js";

Expand Down Expand Up @@ -168,7 +167,6 @@ export class BeaconNode {
initBeaconMetrics(metrics, anchorState);
// Since the db is instantiated before this, metrics must be injected manually afterwards
db.setMetrics(metrics.db);
createLibp2pMetrics(libp2p, metrics.register);
}

const chain = new BeaconChain(opts.chain, {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@chainsafe/blst": "^0.2.8",
"@chainsafe/discv5": "^1.4.0",
"@chainsafe/ssz": "^0.9.2",
"@libp2p/peer-id-factory": "^1.0.18",
"@libp2p/peer-id-factory": "^1.0.19",
"@lodestar/api": "^1.3.0",
"@lodestar/beacon-node": "^1.3.0",
"@lodestar/config": "^1.3.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/cmds/beacon/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise<vo

// additional metrics registries
const metricsRegistries: Registry[] = [];
let networkRegistry: Registry | undefined;
if (options.metrics.enabled) {
networkRegistry = new Registry();
metricsRegistries.push(networkRegistry);
}
const db = new BeaconDb({
config,
controller: new LevelDbController(options.db, {metrics: null}),
Expand Down Expand Up @@ -86,6 +91,7 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise<vo
libp2p: await createNodeJsLibp2p(peerId, options.network, {
peerStoreDir: beaconPaths.peerStoreDir,
metrics: options.metrics.enabled,
metricsRegistry: networkRegistry,
}),
anchorState,
wsCheckpoint,
Expand Down
2 changes: 1 addition & 1 deletion packages/reqresp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"varint": "^6.0.0"
},
"peerDependencies": {
"libp2p": "0.39.2"
"libp2p": "0.41.0"
},
"keywords": [
"ethereum",
Expand Down
Loading

0 comments on commit 9683b84

Please sign in to comment.