Skip to content

Commit

Permalink
Release v1.4.1 (#5088)
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain authored Feb 1, 2023
2 parents d82b681 + b033491 commit cbb7619
Show file tree
Hide file tree
Showing 34 changed files with 161 additions and 169 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "1.4.0",
"version": "1.4.1",
"stream": "true",
"command": {
"version": {
Expand Down
10 changes: 5 additions & 5 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"bugs": {
"url": "https://github.com/ChainSafe/lodestar/issues"
},
"version": "1.4.0",
"version": "1.4.1",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -70,10 +70,10 @@
"dependencies": {
"@chainsafe/persistent-merkle-tree": "^0.4.2",
"@chainsafe/ssz": "^0.9.2",
"@lodestar/config": "^1.4.0",
"@lodestar/params": "^1.4.0",
"@lodestar/types": "^1.4.0",
"@lodestar/utils": "^1.4.0",
"@lodestar/config": "^1.4.1",
"@lodestar/params": "^1.4.1",
"@lodestar/types": "^1.4.1",
"@lodestar/utils": "^1.4.1",
"cross-fetch": "^3.1.4",
"eventsource": "^2.0.2",
"qs": "^6.10.1"
Expand Down
26 changes: 13 additions & 13 deletions packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"bugs": {
"url": "https://github.com/ChainSafe/lodestar/issues"
},
"version": "1.4.0",
"version": "1.4.1",
"type": "module",
"exports": {
".": {
Expand Down Expand Up @@ -95,7 +95,7 @@
"@chainsafe/as-chacha20poly1305": "^0.1.0",
"@chainsafe/as-sha256": "^0.3.1",
"@chainsafe/bls": "7.1.1",
"@chainsafe/discv5": "^2.1.0",
"@chainsafe/discv5": "^3.0.0",
"@chainsafe/libp2p-gossipsub": "^6.1.0",
"@chainsafe/libp2p-noise": "^11.0.0",
"@chainsafe/persistent-merkle-tree": "^0.4.2",
Expand All @@ -114,17 +114,17 @@
"@libp2p/peer-id-factory": "^2.0.1",
"@libp2p/prometheus-metrics": "^1.1.2",
"@libp2p/tcp": "^6.1.0",
"@lodestar/api": "^1.4.0",
"@lodestar/config": "^1.4.0",
"@lodestar/db": "^1.4.0",
"@lodestar/fork-choice": "^1.4.0",
"@lodestar/light-client": "^1.4.0",
"@lodestar/params": "^1.4.0",
"@lodestar/reqresp": "^1.4.0",
"@lodestar/state-transition": "^1.4.0",
"@lodestar/types": "^1.4.0",
"@lodestar/utils": "^1.4.0",
"@lodestar/validator": "^1.4.0",
"@lodestar/api": "^1.4.1",
"@lodestar/config": "^1.4.1",
"@lodestar/db": "^1.4.1",
"@lodestar/fork-choice": "^1.4.1",
"@lodestar/light-client": "^1.4.1",
"@lodestar/params": "^1.4.1",
"@lodestar/reqresp": "^1.4.1",
"@lodestar/state-transition": "^1.4.1",
"@lodestar/types": "^1.4.1",
"@lodestar/utils": "^1.4.1",
"@lodestar/validator": "^1.4.1",
"@multiformats/multiaddr": "^11.0.0",
"@types/datastore-level": "^3.0.0",
"buffer-xor": "^2.0.2",
Expand Down
4 changes: 1 addition & 3 deletions packages/beacon-node/src/api/impl/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {routes, ServerApi} from "@lodestar/api";
import {createKeypairFromPeerId} from "@chainsafe/discv5";
import {ApiError} from "../errors.js";
import {ApiModules} from "../types.js";
import {IApiOptions} from "../../options.js";
Expand All @@ -12,7 +11,6 @@ export function getNodeApi(
return {
async getNetworkIdentity() {
const enr = await network.getEnr();
const keypair = createKeypairFromPeerId(network.peerId);
const discoveryAddresses = [
enr?.getLocationMultiaddr("tcp")?.toString() ?? null,
enr?.getLocationMultiaddr("udp")?.toString() ?? null,
Expand All @@ -21,7 +19,7 @@ export function getNodeApi(
return {
data: {
peerId: network.peerId.toString(),
enr: enr?.encodeTxt(keypair.privateKey) || "",
enr: enr?.encodeTxt() || "",
discoveryAddresses,
p2pAddresses: network.localMultiaddrs.map((m) => m.toString()),
metadata: network.metadata,
Expand Down
64 changes: 28 additions & 36 deletions packages/beacon-node/src/network/discv5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import EventEmitter from "events";
import {PeerId} from "@libp2p/interface-peer-id";
import StrictEventEmitter from "strict-event-emitter-types";
import {exportToProtobuf} from "@libp2p/peer-id-factory";
import {ENR, IDiscv5DiscoveryInputOptions} from "@chainsafe/discv5";
import {
createKeypairFromPeerId,
ENR,
ENRData,
IDiscv5DiscoveryInputOptions,
IKeypair,
SignableENR,
} from "@chainsafe/discv5";
import {spawn, Thread, Worker} from "@chainsafe/threads";
import {ILogger} from "@lodestar/utils";
import {IMetrics} from "../../metrics/metrics.js";
Expand All @@ -29,19 +36,21 @@ type Discv5WorkerStatus =
export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<EventEmitter, IDiscv5Events>}) {
private logger: ILogger;
private status: Discv5WorkerStatus;
private keypair: IKeypair;

constructor(private opts: Discv5Opts) {
super();

this.logger = opts.logger;
this.status = {status: "stopped"};
this.keypair = createKeypairFromPeerId(this.opts.peerId);
}

async start(): Promise<void> {
if (this.status.status === "started") return;

const workerData: Discv5WorkerData = {
enrStr: (this.opts.discv5.enr as ENR).encodeTxt(),
enr: (this.opts.discv5.enr as SignableENR).toObject(),
peerIdProto: exportToProtobuf(this.opts.peerId),
bindAddr: this.opts.discv5.bindAddr,
config: this.opts.discv5,
Expand All @@ -56,7 +65,7 @@ export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<E
timeout: 5 * 60 * 1000,
});

const subscription = workerApi.discoveredBuf().subscribe((enrStr) => this.onDiscoveredStr(enrStr));
const subscription = workerApi.discovered().subscribe((enrObj) => this.onDiscovered(enrObj));

this.status = {status: "started", workerApi, subscription};
}
Expand All @@ -71,20 +80,17 @@ export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<E
this.status = {status: "stopped"};
}

onDiscoveredStr(enrBuf: Uint8Array): void {
const enr = this.decodeEnr(enrBuf);
if (enr !== undefined) {
onDiscovered(obj: ENRData): void {
const enr = this.decodeEnr(obj);
if (enr) {
this.emit("discovered", enr);
}
}

async enr(): Promise<ENR> {
async enr(): Promise<SignableENR> {
if (this.status.status === "started") {
const enr = this.decodeEnr(await this.status.workerApi.enrBuf());
if (enr === undefined) {
throw new Error("unable to decode self ENR");
}
return enr;
const obj = await this.status.workerApi.enr();
return new SignableENR(obj.kvs, obj.seq, this.keypair);
} else {
throw new Error("Cannot get enr before module is started");
}
Expand All @@ -100,15 +106,15 @@ export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<E

async kadValues(): Promise<ENR[]> {
if (this.status.status === "started") {
return this.decodeEnrs(await this.status.workerApi.kadValuesBuf());
return this.decodeEnrs(await this.status.workerApi.kadValues());
} else {
return [];
}
}

async findRandomNode(): Promise<ENR[]> {
if (this.status.status === "started") {
return this.decodeEnrs(await this.status.workerApi.findRandomNodeBuf());
return this.decodeEnrs(await this.status.workerApi.findRandomNode());
} else {
return [];
}
Expand All @@ -122,33 +128,19 @@ export class Discv5Worker extends (EventEmitter as {new (): StrictEventEmitter<E
}
}

private decodeEnrs(enrBufs: Uint8Array[]): ENR[] {
let errors = 0;
private decodeEnrs(objs: ENRData[]): ENR[] {
const enrs: ENR[] = [];
for (const enrBuf of enrBufs) {
try {
enrs.push(ENR.decode(Buffer.from(enrBuf)));
} catch (e) {
this.logger.debug("ENR decode error", {enr: Buffer.from(enrBuf).toString("base64url")});
errors++;
for (const obj of objs) {
const enr = this.decodeEnr(obj);
if (enr) {
enrs.push(enr);
}
}

this.opts.metrics?.discv5.decodeEnrErrorCount.inc(errors);
this.opts.metrics?.discv5.decodeEnrAttemptCount.inc(enrBufs.length);

return enrs;
}

private decodeEnr(enrBuf: Uint8Array): ENR | undefined {
try {
return ENR.decode(Buffer.from(enrBuf));
} catch (e) {
this.opts.metrics?.discv5.decodeEnrErrorCount.inc(1);
} finally {
this.logger.debug("ENR decode error", {enr: Buffer.from(enrBuf).toString("base64url")});
this.opts.metrics?.discv5.decodeEnrAttemptCount.inc(1);
}
return undefined;
private decodeEnr(obj: ENRData): ENR | null {
this.opts.metrics?.discv5.decodeEnrAttemptCount.inc(1);
return new ENR(obj.kvs, obj.seq, obj.signature);
}
}
12 changes: 6 additions & 6 deletions packages/beacon-node/src/network/discv5/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Discv5} from "@chainsafe/discv5";
import {Discv5, ENRData, SignableENRData} from "@chainsafe/discv5";
import {Observable} from "@chainsafe/threads/observable";

// TODO export IDiscv5Config so we don't need this convoluted type
type Discv5Config = Parameters<typeof Discv5["create"]>[0]["config"];

/** discv5 worker constructor data */
export interface Discv5WorkerData {
enrStr: string;
enr: SignableENRData;
peerIdProto: Uint8Array;
bindAddr: string;
config: Discv5Config;
Expand All @@ -21,16 +21,16 @@ export interface Discv5WorkerData {
*/
export type Discv5WorkerApi = {
/** The current host ENR */
enrBuf(): Promise<Uint8Array>;
enr(): Promise<SignableENRData>;
/** Set a key-value of the current host ENR */
setEnrValue(key: string, value: Uint8Array): Promise<void>;

/** Return the ENRs currently in the kad table */
kadValuesBuf(): Promise<Uint8Array[]>;
kadValues(): Promise<ENRData[]>;
/** Begin a random search through the DHT, return discovered ENRs */
findRandomNodeBuf(): Promise<Uint8Array[]>;
findRandomNode(): Promise<ENRData[]>;
/** Stream of discovered ENRs */
discoveredBuf(): Observable<Uint8Array>;
discovered(): Observable<ENRData>;

/** Prometheus metrics string */
metrics(): Promise<string>;
Expand Down
22 changes: 11 additions & 11 deletions packages/beacon-node/src/network/discv5/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {createFromProtobuf} from "@libp2p/peer-id-factory";
import {multiaddr} from "@multiformats/multiaddr";
import {expose} from "@chainsafe/threads/worker";
import {Observable, Subject} from "@chainsafe/threads/observable";
import {createKeypairFromPeerId, Discv5, ENR} from "@chainsafe/discv5";
import {createKeypairFromPeerId, Discv5, ENR, ENRData, SignableENR, SignableENRData} from "@chainsafe/discv5";
import {RegistryMetricCreator} from "../../metrics/index.js";
import {collectNodeJSMetrics} from "../../metrics/nodeJsMetrics.js";
import {Discv5WorkerApi, Discv5WorkerData} from "./types.js";
Expand All @@ -29,7 +29,7 @@ const keypair = createKeypairFromPeerId(peerId);

// Initialize discv5
const discv5 = Discv5.create({
enr: workerData.enrStr,
enr: new SignableENR(workerData.enr.kvs, workerData.enr.seq, keypair),
peerId,
multiaddr: multiaddr(workerData.bindAddr),
config: workerData.config,
Expand All @@ -42,28 +42,28 @@ for (const bootEnr of workerData.bootEnrs) {
}

/** Used to push discovered ENRs */
const subject = new Subject<Uint8Array>();
const subject = new Subject<ENRData>();

const onDiscovered = (enr: ENR): void => subject.next(enr.encode());
const onDiscovered = (enr: ENR): void => subject.next(enr.toObject());
discv5.addListener("discovered", onDiscovered);

// Discv5 will now begin accepting request/responses
await discv5.start();

const module: Discv5WorkerApi = {
async enrBuf(): Promise<Uint8Array> {
return discv5.enr.encode(keypair.privateKey);
async enr(): Promise<SignableENRData> {
return discv5.enr.toObject();
},
async setEnrValue(key: string, value: Uint8Array): Promise<void> {
discv5.enr.set(key, value);
},
async kadValuesBuf(): Promise<Uint8Array[]> {
return discv5.kadValues().map((enr) => enr.encode());
async kadValues(): Promise<ENRData[]> {
return discv5.kadValues().map((enr) => enr.toObject());
},
async findRandomNodeBuf(): Promise<Uint8Array[]> {
return (await discv5.findRandomNode()).map((enr) => enr.encode());
async findRandomNode(): Promise<ENRData[]> {
return (await discv5.findRandomNode()).map((enr) => enr.toObject());
},
discoveredBuf() {
discovered() {
return Observable.from(subject);
},
async metrics(): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Registrar} from "@libp2p/interface-registrar";
import {Multiaddr} from "@multiformats/multiaddr";
import {PeerId} from "@libp2p/interface-peer-id";
import {ConnectionManager} from "@libp2p/interface-connection-manager";
import {ENR} from "@chainsafe/discv5";
import {SignableENR} from "@chainsafe/discv5";
import {phase0} from "@lodestar/types";
import {BlockInput} from "../chain/blocks/types.js";
import {INetworkEventBus} from "./events.js";
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface INetwork {
/** Our network identity */
peerId: PeerId;
localMultiaddrs: Multiaddr[];
getEnr(): Promise<ENR | undefined>;
getEnr(): Promise<SignableENR | undefined>;
getConnectionsByPeer(): Map<string, Connection[]>;
getConnectedPeers(): PeerId[];
hasSomeConnectedPeer(): boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Multiaddr} from "@multiformats/multiaddr";
import {IBeaconConfig} from "@lodestar/config";
import {ILogger, sleep} from "@lodestar/utils";
import {ATTESTATION_SUBNET_COUNT, ForkName, ForkSeq, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params";
import {ENR} from "@chainsafe/discv5";
import {SignableENR} from "@chainsafe/discv5";
import {computeEpochAtSlot, computeTimeAtSlot} from "@lodestar/state-transition";
import {altair, eip4844, Epoch, phase0} from "@lodestar/types";
import {routes} from "@lodestar/api";
Expand Down Expand Up @@ -194,7 +194,7 @@ export class Network implements INetwork {
return this.libp2p.peerId;
}

async getEnr(): Promise<ENR | undefined> {
async getEnr(): Promise<SignableENR | undefined> {
return await this.peerManager["discovery"]?.discv5.enr();
}

Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/nodejs/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {Registry} from "prom-client";
import {ENR} from "@chainsafe/discv5";
import {ENR, SignableENR} from "@chainsafe/discv5";
import {Libp2p} from "../interface.js";
import {Eth2PeerDataStore} from "../peers/datastore.js";
import {defaultDiscv5Options, defaultNetworkOptions, INetworkOptions} from "../options.js";
Expand Down Expand Up @@ -32,7 +32,7 @@ export async function createNodeJsLibp2p(
const {peerStoreDir, disablePeerDiscovery} = nodeJsLibp2pOpts;

if (enr !== undefined && typeof enr !== "string") {
if (enr instanceof ENR) {
if (enr instanceof SignableENR) {
if (enr.getLocationMultiaddr("udp") && !isLocalMultiAddr(enr.getLocationMultiaddr("udp"))) {
clearMultiaddrUDP(enr);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/beacon-node/src/network/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ENR, IDiscv5DiscoveryInputOptions} from "@chainsafe/discv5";
import {generateKeypair, IDiscv5DiscoveryInputOptions, KeypairType, SignableENR} from "@chainsafe/discv5";
import {Eth2GossipsubOpts} from "./gossip/gossipsub.js";
import {defaultGossipHandlerOpts, GossipHandlerOpts} from "./gossip/handlers/index.js";
import {PeerManagerOpts} from "./peers/index.js";
Expand All @@ -15,7 +15,7 @@ export interface INetworkOptions extends PeerManagerOpts, ReqRespBeaconNodeOpts,

export const defaultDiscv5Options: IDiscv5DiscoveryInputOptions = {
bindAddr: "/ip4/0.0.0.0/udp/9000",
enr: new ENR(),
enr: SignableENR.createV4(generateKeypair(KeypairType.Secp256k1)),
bootEnrs: [],
enrUpdate: true,
enabled: true,
Expand Down
Loading

0 comments on commit cbb7619

Please sign in to comment.