Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Jun 22, 2022
1 parent af4aca8 commit c0168a2
Show file tree
Hide file tree
Showing 82 changed files with 4,508 additions and 4,869 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"dependencies": {},
"resolutions": {
"libnpmpublish": "4.0.0"
"libnpmpublish": "4.0.0",
"@types/node": "14.14.43"
}
}
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@chainsafe/bls-keygen": "^0.3.0",
"@chainsafe/bls-keystore": "^2.0.0",
"@chainsafe/blst": "^0.2.4",
"@chainsafe/discv5": "^0.7.1",
"@chainsafe/discv5": "^1.0.2",
"@chainsafe/lodestar": "^0.38.1",
"@chainsafe/lodestar-api": "^0.38.1",
"@chainsafe/lodestar-beacon-state-transition": "^0.38.1",
Expand All @@ -68,6 +68,7 @@
"@chainsafe/lodestar-utils": "^0.38.1",
"@chainsafe/lodestar-validator": "^0.38.1",
"@chainsafe/ssz": "^0.9.2",
"@multiformats/multiaddr": "^10.2.0",
"@types/lockfile": "^1.0.1",
"bip39": "^3.0.2",
"deepmerge": "^4.2.2",
Expand All @@ -79,7 +80,6 @@
"js-yaml": "^3.14.0",
"lockfile": "^1.0.4",
"lodash": "^4.17.15",
"multiaddr": "^10.0.1",
"peer-id": "^0.15.3",
"prom-client": "^14.0.1",
"rimraf": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmds/init/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function persistOptionsAndConfig(args: IBeaconArgs & IGlobalArgs):
// Verify that the peerId matches the ENR
const enr = readEnr(beaconPaths.enrFile);
const peerIdPrev = await enr.peerId();
if (peerIdPrev.toB58String() !== peerId.toB58String()) {
if (peerIdPrev.toString() !== peerId.toString()) {
initEnr(beaconPaths.enrFile, peerId);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/config/enr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PeerId from "peer-id";
import {PeerId} from "@libp2p/interface-peer-id";
import {Multiaddr} from "multiaddr";
import {IBeaconNodeOptions} from "@chainsafe/lodestar";
import {ENR, createKeypairFromPeerId} from "@chainsafe/discv5";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/config/fileEnr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PeerId from "peer-id";
import {createKeypairFromPeerId, ENR, ENRKey, ENRValue} from "@chainsafe/discv5";
import {writeFile, readFile} from "../util/index.js";
import {PeerId} from "@libp2p/interface-peer-id";

/**
* `FileENR` is an `ENR` that saves the ENR contents to a file on every modification
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/config/peerId.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PeerId from "peer-id";
import {PeerId} from "@libp2p/interface-peer-id";
import {writeFile, readFile} from "../util/index.js";

export async function createPeerId(): Promise<PeerId> {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/unit/config/peerId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ describe("config / peerId", () => {
writePeerId(peerIdFilepath, peerId);
const peerIdRead = await readPeerId(peerIdFilepath);

expect(peerIdRead.toB58String()).to.equal(peerId.toB58String());
expect(peerIdRead.toString()).to.equal(peerId.toString());
});
});
4 changes: 2 additions & 2 deletions packages/keymanager-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
"@chainsafe/lodestar-utils": "^0.38.1",
"@chainsafe/lodestar-validator": "^0.38.1",
"@chainsafe/ssz": "^0.9.2",
"@fastify/bearer-auth": "8.0.0",
"@fastify/cors": "^8.0.0",
"fastify": "3.15.1",
"fastify-bearer-auth": "6.1.0",
"fastify-cors": "^6.0.1",
"lockfile": "^1.0.4"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/keymanager-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import crypto from "node:crypto";
import fs from "node:fs";
import {join} from "node:path";
import fastify, {FastifyError, FastifyInstance} from "fastify";
import fastifyCors from "fastify-cors";
import bearerAuthPlugin from "fastify-bearer-auth";
import fastifyCors from "@fastify/cors";
import bearerAuthPlugin from "@fastify/bearer-auth";
import {toHexString} from "@chainsafe/ssz";
import {Api} from "@chainsafe/lodestar-api/keymanager";
import {registerRoutes, RouteConfig} from "@chainsafe/lodestar-api/keymanager/server";
Expand Down
25 changes: 14 additions & 11 deletions packages/lodestar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@
"dependencies": {
"@chainsafe/as-sha256": "^0.3.1",
"@chainsafe/bls": "7.1.1",
"@chainsafe/discv5": "^0.7.1",
"@chainsafe/libp2p-noise": "5.0.3",
"@chainsafe/discv5": "^1.0.2",
"@chainsafe/libp2p-gossipsub": "^2.0.0",
"@chainsafe/libp2p-noise": "6.2.0",
"@chainsafe/lodestar-api": "^0.38.1",
"@chainsafe/lodestar-beacon-state-transition": "^0.38.1",
"@chainsafe/lodestar-config": "^0.38.1",
Expand All @@ -119,6 +120,16 @@
"@chainsafe/ssz": "^0.9.2",
"@chainsafe/threads": "^1.10.0",
"@ethersproject/abi": "^5.0.0",
"@libp2p/bootstrap": "^2.0.0",
"@libp2p/interface-connection": "^1.0.1",
"@libp2p/interface-connection-manager": "^1.0.0",
"@libp2p/interface-peer-id": "^1.0.2",
"@libp2p/interface-pubsub": "^1.0.1",
"@libp2p/mdns": "^2.0.0",
"@libp2p/mplex": "^2.0.0",
"@libp2p/peer-id-factory": "^1.0.13",
"@libp2p/tcp": "^2.0.0",
"@multiformats/multiaddr": "^10.2.0",
"@types/datastore-level": "^3.0.0",
"bl": "^5.0.0",
"buffer-xor": "^2.0.2",
Expand All @@ -133,14 +144,7 @@
"it-all": "^1.0.4",
"it-pipe": "^1.1.0",
"jwt-simple": "0.5.6",
"libp2p": "^0.36.2",
"libp2p-bootstrap": "^0.14.0",
"libp2p-gossipsub": "^0.14.0",
"libp2p-mdns": "^0.18.0",
"libp2p-mplex": "^0.10.5",
"libp2p-tcp": "^0.17.2",
"multiaddr": "^10.0.1",
"peer-id": "^0.16.0",
"libp2p": "0.37.3-d4dd664",
"prom-client": "^13.2.0",
"prometheus-gc-stats": "^0.6.3",
"snappyjs": "^0.6.0",
Expand All @@ -155,7 +159,6 @@
"@types/jwt-simple": "0.5.33",
"@types/leveldown": "^4.0.2",
"@types/prometheus-gc-stats": "^0.6.1",
"@types/supertest": "^2.0.8",
"@types/tmp": "^0.2.0",
"@types/varint": "^6.0.0",
"eventsource": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/api/impl/beacon/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function getBeaconBlockApi({

chain.processBlock(signedBlock).catch((e) => {
if (e instanceof BlockError && e.type.code === BlockErrorCode.PARENT_UNKNOWN) {
network.events.emit(NetworkEvent.unknownBlockParent, signedBlock, network.peerId.toB58String());
network.events.emit(NetworkEvent.unknownBlockParent, signedBlock, network.peerId.toString());
}
throw e;
}),
Expand Down
8 changes: 4 additions & 4 deletions packages/lodestar/src/api/impl/debug/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Multiaddr} from "multiaddr";
import {createFromB58String} from "peer-id";
import {Multiaddr} from "@multiformats/multiaddr";
import {peerIdFromString} from "@libp2p/peer-id";
import {routes} from "@chainsafe/lodestar-api";
import {resolveStateId} from "../beacon/state/utils.js";
import {ApiModules} from "../types.js";
Expand Down Expand Up @@ -41,13 +41,13 @@ export function getDebugApi({
},

async connectToPeer(peerIdStr, multiaddrStr) {
const peer = createFromB58String(peerIdStr);
const peer = peerIdFromString(peerIdStr);
const multiaddr = multiaddrStr.map((addr) => new Multiaddr(addr));
await network.connectToPeer(peer, multiaddr);
},

async disconnectPeer(peerIdStr) {
const peer = createFromB58String(peerIdStr);
const peer = peerIdFromString(peerIdStr);
await network.disconnectPeer(peer);
},
};
Expand Down
8 changes: 4 additions & 4 deletions packages/lodestar/src/api/impl/lodestar/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PeerId from "peer-id";
import {Multiaddr} from "multiaddr";
import {peerIdFromString} from "@libp2p/peer-id";
import {Multiaddr} from "@multiformats/multiaddr";
import {routes} from "@chainsafe/lodestar-api";
import {getLatestWeakSubjectivityCheckpointEpoch} from "@chainsafe/lodestar-beacon-state-transition";
import {toHexString} from "@chainsafe/ssz";
Expand Down Expand Up @@ -124,13 +124,13 @@ export function getLodestarApi({
},

async connectPeer(peerIdStr, multiaddrStrs) {
const peerId = PeerId.createFromB58String(peerIdStr);
const peerId = peerIdFromString(peerIdStr);
const multiaddrs = multiaddrStrs.map((multiaddrStr) => new Multiaddr(multiaddrStr));
await network.connectToPeer(peerId, multiaddrs);
},

async disconnectPeer(peerIdStr) {
const peerId = PeerId.createFromB58String(peerIdStr);
const peerId = peerIdFromString(peerIdStr);
await network.disconnectPeer(peerId);
},

Expand Down
8 changes: 4 additions & 4 deletions packages/lodestar/src/api/impl/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getNodeApi(opts: IApiOptions, {network, sync}: Pick<ApiModules,

return {
data: {
peerId: network.peerId.toB58String(),
peerId: network.peerId.toString(),
enr: enr?.encodeTxt(keypair.privateKey) || "",
discoveryAddresses,
p2pAddresses: network.localMultiaddrs.map((m) => m.toString()),
Expand Down Expand Up @@ -60,13 +60,13 @@ export function getNodeApi(opts: IApiOptions, {network, sync}: Pick<ApiModules,
for (const connections of network.getConnectionsByPeer().values()) {
const relevantConnection = getRevelantConnection(connections);
switch (relevantConnection?.stat.status) {
case "open":
case "OPEN":
connected++;
break;
case "closing":
case "CLOSING":
disconnecting++;
break;
case "closed":
case "CLOSED":
disconnected++;
break;
default:
Expand Down
14 changes: 7 additions & 7 deletions packages/lodestar/src/api/impl/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Connection} from "libp2p";
import {Connection} from "@libp2p/interface-connection";
import {routes} from "@chainsafe/lodestar-api";
import {PeerStatus} from "../../../network/index.js";

Expand All @@ -9,7 +9,7 @@ export function formatNodePeer(peerIdStr: string, connections: Connection[]): ro
const conn = getRevelantConnection(connections);

return {
peerId: conn ? conn.remotePeer.toB58String() : peerIdStr,
peerId: conn ? conn.remotePeer.toString() : peerIdStr,
// TODO: figure out how to get enr of peer
enr: "",
lastSeenP2pAddress: conn ? conn.remoteAddr.toString() : "",
Expand All @@ -27,11 +27,11 @@ export function formatNodePeer(peerIdStr: string, connections: Connection[]): ro
export function getRevelantConnection(connections: Connection[]): Connection | null {
const byStatus = new Map<PeerStatus, Connection>();
for (const conn of connections) {
if (conn.stat.status === "open") return conn;
if (conn.stat.status === "OPEN") return conn;
if (!byStatus.has(conn.stat.status)) byStatus.set(conn.stat.status, conn);
}

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

/**
Expand All @@ -40,11 +40,11 @@ export function getRevelantConnection(connections: Connection[]): Connection | n
*/
function getPeerState(status: PeerStatus): 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
2 changes: 1 addition & 1 deletion packages/lodestar/src/network/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {EventEmitter} from "events";
import PeerId from "peer-id";
import {PeerId} from "@libp2p/interface-peer-id";
import StrictEventEmitter from "strict-event-emitter-types";
import {allForks, phase0} from "@chainsafe/lodestar-types";
import {RequestTypedContainer} from "./reqresp/index.js";
Expand Down
8 changes: 4 additions & 4 deletions packages/lodestar/src/network/gossip/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {compress, uncompress} from "snappyjs";
import {RPC} from "libp2p-gossipsub/src/message/rpc";
import {GossipsubMessage} from "libp2p-gossipsub/src/types";
import {Message} from "@libp2p/interface-pubsub";
import {digest} from "@chainsafe/as-sha256";
import {intToBytes} from "@chainsafe/lodestar-utils";
import {ForkName} from "@chainsafe/lodestar-params";
import {RPC} from "@chainsafe/libp2p-gossipsub/message";
import {MESSAGE_DOMAIN_VALID_SNAPPY} from "./constants.js";
import {GossipTopicCache} from "./topic.js";

/**
* The function used to generate a gossipsub message id
* We use the first 8 bytes of SHA256(data) for content addressing
*/
export function fastMsgIdFn(rpcMsg: RPC.IMessage): string {
export function fastMsgIdFn(rpcMsg: RPC.Message): string {
if (rpcMsg.data) {
return Buffer.from(digest(rpcMsg.data)).slice(0, 8).toString("hex");
} else {
Expand All @@ -22,7 +22,7 @@ export function fastMsgIdFn(rpcMsg: RPC.IMessage): string {
/**
* Only valid msgId. Messages that fail to snappy_decompress() are not tracked
*/
export function msgIdFn(gossipTopicCache: GossipTopicCache, msg: GossipsubMessage): Uint8Array {
export function msgIdFn(gossipTopicCache: GossipTopicCache, msg: Message): Uint8Array {
const topic = gossipTopicCache.getTopic(msg.topic);

let vec: Uint8Array[];
Expand Down
33 changes: 16 additions & 17 deletions packages/lodestar/src/network/gossip/gossipsub.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/naming-convention */
import Libp2p from "libp2p";
import GossipsubDefault from "libp2p-gossipsub";
// TODO remove once Gossipsub goes ESM
const Gossipsub = ((GossipsubDefault as unknown) as {default: unknown}).default as typeof GossipsubDefault;
import {GossipsubMessage, SignaturePolicy, TopicStr} from "libp2p-gossipsub/src/types.js";
import {PeerScore, PeerScoreParams} from "libp2p-gossipsub/src/score/index.js";
import PeerId from "peer-id";
import {MetricsRegister, TopicLabel, TopicStrToLabel} from "libp2p-gossipsub/src/metrics";
import {Libp2p} from "libp2p";
import {Message} from "@libp2p/interface-pubsub";
import {PeerId} from "@libp2p/interface-peer-id";
import {GossipSub} from "@chainsafe/libp2p-gossipsub";
import {SignaturePolicy, TopicStr} from "@chainsafe/libp2p-gossipsub/types";
import {PeerScore, PeerScoreParams} from "@chainsafe/libp2p-gossipsub/score";
import {MetricsRegister, TopicLabel, TopicStrToLabel} from "@chainsafe/libp2p-gossipsub/metrics";
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import {ATTESTATION_SUBNET_COUNT, ForkName, SYNC_COMMITTEE_SUBNET_COUNT} from "@chainsafe/lodestar-params";
import {allForks, altair, phase0} from "@chainsafe/lodestar-types";
Expand Down Expand Up @@ -45,11 +44,11 @@ const GOSSIPSUB_HEARTBEAT_INTERVAL = 0.7 * 1000;

// TODO: Export this type
type GossipsubEvents = {
"gossipsub:message": {
"gossipsub:message": CustomEvent<{
propagationSource: PeerId;
msgId: string;
msg: GossipsubMessage;
};
msg: Message;
}>;
};

export type Eth2GossipsubModules = {
Expand Down Expand Up @@ -80,7 +79,7 @@ export type Eth2GossipsubOpts = {
*
* See https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/p2p-interface.md#the-gossip-domain-gossipsub
*/
export class Eth2Gossipsub extends Gossipsub {
export class Eth2Gossipsub extends GossipSub {
readonly jobQueues: GossipJobQueues;
readonly scoreParams: Partial<PeerScoreParams>;
private readonly config: IBeaconConfig;
Expand All @@ -99,8 +98,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(modules.libp2p, {
gossipIncoming: true,
super({
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
allowPublishToZeroPeers: opts.allowPublishToZeroPeers,
D: GOSSIP_D,
Expand Down Expand Up @@ -146,7 +144,7 @@ export class Eth2Gossipsub extends Gossipsub {
metrics.gossipMesh.peersByType.addCollect(() => this.onScrapeLodestarMetrics(metrics));
}

this.on("gossipsub:message", this.onGossipsubMessage.bind(this));
this.addEventListener("gossipsub:message", this.onGossipsubMessage.bind(this));

// Having access to this data is CRUCIAL for debugging. While this is a massive log, it must not be deleted.
// Scoring issues require this dump + current peer score stats to re-calculate scores.
Expand All @@ -160,7 +158,8 @@ export class Eth2Gossipsub extends Gossipsub {
const topicStr = this.getGossipTopicString(topic);
const sszType = getGossipSSZType(topic);
const messageData = (sszType.serialize as (object: GossipTypeMap[GossipType]) => Uint8Array)(object);
const sentPeers = await this.publish(topicStr, messageData);
const result = await this.publish(topicStr, messageData);
const sentPeers = result.recipients.length;
this.logger.verbose("Publish to topic", {topic: topicStr, sentPeers});
return sentPeers;
}
Expand Down Expand Up @@ -347,7 +346,7 @@ export class Eth2Gossipsub extends Gossipsub {
}

private onGossipsubMessage(event: GossipsubEvents["gossipsub:message"]): void {
const {propagationSource, msgId, msg} = event;
const {propagationSource, msgId, msg} = event.detail;

// TODO: validation GOSSIP_MAX_SIZE
// - Should be done here after inserting the message in the mcache?
Expand Down
Loading

0 comments on commit c0168a2

Please sign in to comment.