Skip to content

Commit

Permalink
builds
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Jun 28, 2022
1 parent 305a43b commit 687b0ae
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 183 deletions.
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@chainsafe/lodestar-utils": "^0.38.1",
"@chainsafe/lodestar-validator": "^0.38.1",
"@chainsafe/ssz": "^0.9.2",
"@libp2p/peer-id-factory": "^1.0.15",
"@multiformats/multiaddr": "^10.2.0",
"@types/lockfile": "^1.0.1",
"bip39": "^3.0.2",
Expand All @@ -84,6 +85,7 @@
"prom-client": "^14.0.1",
"rimraf": "^3.0.0",
"source-map-support": "^0.5.19",
"uint8arrays": "^3.0.0",
"uuidv4": "^6.1.1",
"yargs": "^16.1.0"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/cmds/dev/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "node:fs";
import {promisify} from "node:util";
import path from "node:path";
import rimraf from "rimraf";
import {createSecp256k1PeerId} from "@libp2p/peer-id-factory";
import {fromHexString} from "@chainsafe/ssz";
import {GENESIS_SLOT} from "@chainsafe/lodestar-params";
import {BeaconNode, BeaconDb, initStateFromAnchorState, createNodeJsLibp2p, nodeUtils} from "@chainsafe/lodestar";
Expand All @@ -11,7 +12,7 @@ import {interopSecretKey} from "@chainsafe/lodestar-beacon-state-transition";
import {createIBeaconConfig} from "@chainsafe/lodestar-config";
import {ACTIVE_PRESET, PresetName} from "@chainsafe/lodestar-params";
import {onGracefulShutdown} from "../../util/process.js";
import {createEnr, createPeerId, overwriteEnrWithCliArgs} from "../../config/index.js";
import {createEnr, overwriteEnrWithCliArgs} from "../../config/index.js";
import {IGlobalArgs, parseEnrArgs} from "../../options/index.js";
import {initializeOptionsAndConfig} from "../init/handler.js";
import {mkdir, getCliLogger, parseRange} from "../../util/index.js";
Expand All @@ -27,7 +28,7 @@ export async function devHandler(args: IDevArgs & IGlobalArgs): Promise<void> {
const {beaconNodeOptions, config} = await initializeOptionsAndConfig(args);

// ENR setup
const peerId = await createPeerId();
const peerId = await createSecp256k1PeerId();
const enr = createEnr(peerId);
beaconNodeOptions.set({network: {discv5: {enr}}});
const enrArgs = parseEnrArgs(args);
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,5 +1,5 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {Multiaddr} from "multiaddr";
import {Multiaddr} from "@multiformats/multiaddr";
import {IBeaconNodeOptions} from "@chainsafe/lodestar";
import {ENR, createKeypairFromPeerId} from "@chainsafe/discv5";
import {writeFile, readFile} from "../util/index.js";
Expand Down
49 changes: 44 additions & 5 deletions packages/cli/src/config/peerId.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
import {PeerId} from "@libp2p/interface-peer-id";
import {peerIdFromBytes} from "@libp2p/peer-id";
import {createFromPrivKey, createFromPubKey, createSecp256k1PeerId} from "@libp2p/peer-id-factory";
import {unmarshalPrivateKey, unmarshalPublicKey} from "@libp2p/crypto/keys";
import {fromString as uint8ArrayFromString} from "uint8arrays/from-string";
import {toString as uint8ArrayToString} from "uint8arrays/to-string";
import {writeFile, readFile} from "../util/index.js";

export async function createPeerId(): Promise<PeerId> {
return await PeerId.create({keyType: "secp256k1"});
// Peer id to / from JSON taken from peer-id-factory
// See https://github.com/libp2p/js-libp2p-peer-id/pull/9 for more details

async function createFromParts(multihash: Uint8Array, privKey?: Uint8Array, pubKey?: Uint8Array): Promise<PeerId> {
if (privKey != null) {
const key = await unmarshalPrivateKey(privKey);

return await createFromPrivKey(key);
} else if (pubKey != null) {
const key = unmarshalPublicKey(pubKey);

return await createFromPubKey(key);
}

return peerIdFromBytes(multihash);
}

export type PeerIdJSON = {id: string; pubKey?: string; privKey?: string};

export function exportToJSON(peerId: PeerId, excludePrivateKey?: boolean): PeerIdJSON {
return {
id: uint8ArrayToString(peerId.toBytes(), "base58btc"),
pubKey: peerId.publicKey != null ? uint8ArrayToString(peerId.publicKey, "base64pad") : undefined,
privKey:
excludePrivateKey === true || peerId.privateKey == null
? undefined
: uint8ArrayToString(peerId.privateKey, "base64pad"),
};
}

export async function createFromJSON(obj: PeerIdJSON): Promise<PeerId> {
return await createFromParts(
uint8ArrayFromString(obj.id, "base58btc"),
obj.privKey != null ? uint8ArrayFromString(obj.privKey, "base64pad") : undefined,
obj.pubKey != null ? uint8ArrayFromString(obj.pubKey, "base64pad") : undefined
);
}

export function writePeerId(filepath: string, peerId: PeerId): void {
writeFile(filepath, peerId.toJSON());
writeFile(filepath, exportToJSON(peerId));
}

export async function readPeerId(filepath: string): Promise<PeerId> {
return await PeerId.createFromJSON(readFile(filepath));
return await createFromJSON(readFile(filepath));
}

export async function initPeerId(filepath: string): Promise<void> {
writePeerId(filepath, await createPeerId());
writePeerId(filepath, await createSecp256k1PeerId());
}
4 changes: 2 additions & 2 deletions packages/cli/test/unit/config/enr.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {expect} from "chai";
import {getTestdirPath} from "../../utils.js";
import {createPeerId, createEnr, writeEnr, readEnr} from "../../../src/config/index.js";
import {createSecp256k1PeerId, createEnr, writeEnr, readEnr} from "../../../src/config/index.js";

describe("config / enr", () => {
const enrFilepath = getTestdirPath("./test-enr.json");

it("create, write and read ENR", async () => {
const peerId = await createPeerId();
const peerId = await createSecp256k1PeerId();
const enr = createEnr(peerId);
writeEnr(enrFilepath, enr, peerId);
const enrRead = readEnr(enrFilepath);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/test/unit/config/peerId.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {expect} from "chai";
import {getTestdirPath} from "../../utils.js";
import {createPeerId, writePeerId, readPeerId} from "../../../src/config/index.js";
import {createSecp256k1PeerId, writePeerId, readPeerId} from "../../../src/config/index.js";

describe("config / peerId", () => {
const peerIdFilepath = getTestdirPath("./test-peer-id.json");

it("create, write and read PeerId", async () => {
const peerId = await createPeerId();
const peerId = await createSecp256k1PeerId();
writePeerId(peerIdFilepath, peerId);
const peerIdRead = await readPeerId(peerIdFilepath);

Expand Down
28 changes: 0 additions & 28 deletions packages/cli/types/libp2p-bootstrap/index.d.ts

This file was deleted.

Empty file.
16 changes: 0 additions & 16 deletions packages/cli/types/libp2p-bootstrap/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/cli/types/libp2p-bootstrap/tslint.json

This file was deleted.

28 changes: 0 additions & 28 deletions packages/cli/types/libp2p-mdns/index.d.ts

This file was deleted.

Empty file.
16 changes: 0 additions & 16 deletions packages/cli/types/libp2p-mdns/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/cli/types/libp2p-mdns/tslint.json

This file was deleted.

9 changes: 4 additions & 5 deletions packages/lodestar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@
"@ethersproject/abi": "^5.0.0",
"@fastify/cors": "^8.0.0",
"@libp2p/bootstrap": "^2.0.0",
"@libp2p/interface-connection": "^1.0.1",
"@libp2p/interface-connection": "^2.1.0",
"@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/mdns": "^2.0.1",
"@libp2p/mplex": "^3.0.0",
"@libp2p/peer-id-factory": "^1.0.13",
"@libp2p/tcp": "^2.0.0",
"@libp2p/peer-id-factory": "^1.0.15",
"@libp2p/tcp": "^3.0.1",
"@multiformats/multiaddr": "^10.2.0",
"@types/datastore-level": "^3.0.0",
"bl": "^5.0.0",
Expand All @@ -153,7 +153,6 @@
"varint": "^6.0.0"
},
"devDependencies": {
"@libp2p/peer-id-factory": "^1.0.15",
"@types/bl": "^5.0.1",
"@types/eventsource": "^1.1.5",
"@types/it-all": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {writeEncodedPayload} from "../encodingStrategies/index.js";
export async function* requestEncode(
protocol: Pick<Protocol, "method" | "encoding">,
requestBody: RequestBody
): AsyncGenerator<Buffer> {
): AsyncGenerator<Uint8Array> {
const type = getRequestSzzTypeByMethod(protocol.method);

if (type && requestBody !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum StreamStatus {
export function responseDecode(
forkDigestContext: IForkDigestContext,
protocol: Protocol
): (source: AsyncIterable<Buffer>) => AsyncGenerator<IncomingResponseBody> {
): (source: AsyncIterable<Uint8Array>) => AsyncGenerator<IncomingResponseBody> {
return async function* responseDecodeSink(source) {
const contextBytesType = contextBytesTypeByProtocol(protocol);
const bufferedSource = new BufferedSource(source as AsyncGenerator<Buffer>);
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/network/reqresp/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export async function sendRequest<T extends IncomingResponseBody | IncomingRespo
const responses = await withTimeout(
() =>
pipe(
stream.source,
stream.source as AsyncIterable<Buffer>,
responseTimeoutsHandler(responseDecode(forkDigestContext, protocol), options),
collectResponses(method, maxResponses)
),
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/network/reqresp/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function handleRequest(
(async function* requestHandlerSource() {
try {
const requestBody = await withTimeout(
() => pipe(stream.source, requestDecode(protocol)),
() => pipe(stream.source as AsyncIterable<Buffer>, requestDecode(protocol)),
REQUEST_TIMEOUT,
signal
).catch((e: unknown) => {
Expand Down
Loading

0 comments on commit 687b0ae

Please sign in to comment.