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: upgrade to libp2p v2 #2143

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"Dscore",
"ecies",
"editorconfig",
"Encrypters",
"enr",
"enrs",
"enrtree",
Expand Down
436 changes: 258 additions & 178 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"node": ">=20"
},
"dependencies": {
"@libp2p/ping": "^1.1.2",
"@libp2p/ping": "2.0.1",
"@waku/enr": "^0.0.26",
"@waku/interfaces": "0.0.27",
"@waku/proto": "0.0.8",
Expand All @@ -82,6 +82,7 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@libp2p/peer-id": "^5.0.1",
"@multiformats/multiaddr": "^12.0.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
Expand All @@ -103,7 +104,7 @@
},
"peerDependencies": {
"@multiformats/multiaddr": "^12.0.0",
"libp2p": "^1.8.1"
"libp2p": "2.0.2"
},
"peerDependenciesMeta": {
"@multiformats/multiaddr": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/connection_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Peer, PeerId, PeerInfo, PeerStore } from "@libp2p/interface";
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
import { TypedEventEmitter } from "@libp2p/interface";
import {
ConnectionManagerOptions,
DiscoveryTrigger,
Expand Down Expand Up @@ -36,7 +36,7 @@
private options: ConnectionManagerOptions;
private libp2p: Libp2p;
private dialAttemptsForPeer: Map<string, number> = new Map();
private dialErrorsForPeer: Map<string, any> = new Map();

Check warning on line 39 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 39 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type

private currentActiveParallelDialCount = 0;
private pendingPeerDialQueue: Array<PeerId> = [];
Expand Down Expand Up @@ -253,7 +253,7 @@
// Handle generic error
log.error(
`Error dialing peer ${peerId.toString()} - ${
(error as any).message

Check warning on line 256 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 256 in packages/core/src/lib/connection_manager.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
}`
);
}
Expand Down
45 changes: 27 additions & 18 deletions packages/core/src/lib/filterPeers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import { Peer } from "@libp2p/interface";
import type { Tag } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { Tags } from "@waku/interfaces";
import { expect } from "chai";

import { filterPeersByDiscovery } from "./filterPeers.js";

describe("filterPeersByDiscovery function", function () {
it("should return all peers when numPeers is 0", async function () {
const peer1 = await createSecp256k1PeerId();
const peer2 = await createSecp256k1PeerId();
const peer3 = await createSecp256k1PeerId();
const [peer1, peer2, peer3] = await Promise.all([
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);

const mockPeers = [
{
Expand All @@ -32,10 +35,12 @@ describe("filterPeersByDiscovery function", function () {
});

it("should return all non-bootstrap peers and no bootstrap peer when numPeers is 0 and maxBootstrapPeers is 0", async function () {
const peer1 = await createSecp256k1PeerId();
const peer2 = await createSecp256k1PeerId();
const peer3 = await createSecp256k1PeerId();
const peer4 = await createSecp256k1PeerId();
const [peer1, peer2, peer3, peer4] = await Promise.all([
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);

const mockPeers = [
{
Expand Down Expand Up @@ -66,11 +71,13 @@ describe("filterPeersByDiscovery function", function () {
});

it("should return one bootstrap peer, and all non-boostrap peers, when numPeers is 0 & maxBootstrap is 1", async function () {
const peer1 = await createSecp256k1PeerId();
const peer2 = await createSecp256k1PeerId();
const peer3 = await createSecp256k1PeerId();
const peer4 = await createSecp256k1PeerId();
const peer5 = await createSecp256k1PeerId();
const [peer1, peer2, peer3, peer4, peer5] = await Promise.all([
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);

const mockPeers = [
{
Expand Down Expand Up @@ -105,11 +112,13 @@ describe("filterPeersByDiscovery function", function () {
});

it("should return only bootstrap peers up to maxBootstrapPeers", async function () {
const peer1 = await createSecp256k1PeerId();
const peer2 = await createSecp256k1PeerId();
const peer3 = await createSecp256k1PeerId();
const peer4 = await createSecp256k1PeerId();
const peer5 = await createSecp256k1PeerId();
const [peer1, peer2, peer3, peer4, peer5] = await Promise.all([
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
generateKeyPair("secp256k1").then(peerIdFromPrivateKey)
]);

const mockPeers = [
{
Expand Down
5 changes: 2 additions & 3 deletions packages/discovery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
"uint8arrays": "^5.0.1"
},
"devDependencies": {
"@libp2p/peer-id": "^4.2.1",
"@libp2p/peer-id-factory": "^4.2.1",
"@libp2p/peer-id": "5.0.1",
"@multiformats/multiaddr": "^12.3.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
Expand All @@ -81,7 +80,7 @@
"sinon": "^18.0.0"
},
"peerDependencies": {
"@libp2p/interface": "^1.6.3"
"@libp2p/interface": "2.0.1"
},
"peerDependenciesMeta": {
"@libp2p/interface": {
Expand Down
1 change: 0 additions & 1 deletion packages/discovery/src/dns/dns_discovery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
CustomEvent,
PeerDiscovery,
PeerDiscoveryEvents,
TypedEventEmitter
Expand Down
5 changes: 3 additions & 2 deletions packages/discovery/src/dns/fetch_nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { generateKeyPair } from "@libp2p/crypto/keys";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import { ENR } from "@waku/enr";
import { EnrCreator } from "@waku/enr";
Expand All @@ -8,7 +9,7 @@ import { expect } from "chai";
import { fetchNodesUntilCapabilitiesFulfilled } from "./fetch_nodes.js";

async function createEnr(waku2: Waku2): Promise<ENR> {
const peerId = await createSecp256k1PeerId();
const peerId = await generateKeyPair("secp256k1").then(peerIdFromPrivateKey);
const enr = await EnrCreator.fromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.multiaddrs = [
Expand Down
10 changes: 4 additions & 6 deletions packages/discovery/src/local-peer-cache/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { IdentifyResult } from "@libp2p/interface";
import { TypedEventEmitter } from "@libp2p/interface";
import tests from "@libp2p/interface-compliance-tests/peer-discovery";
import { prefixLogger } from "@libp2p/logger";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { createFromJSON } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey, peerIdFromString } from "@libp2p/peer-id";
import { PersistentPeerStore } from "@libp2p/peer-store";
import { multiaddr } from "@multiformats/multiaddr";
import { Libp2pComponents } from "@waku/interfaces";
Expand Down Expand Up @@ -55,7 +55,7 @@ describe("Local Storage Discovery", function () {
components = {
peerStore: new PersistentPeerStore({
events: new TypedEventEmitter(),
peerId: await createSecp256k1PeerId(),
peerId: await generateKeyPair("secp256k1").then(peerIdFromPrivateKey),
datastore: new MemoryDatastore(),
logger: prefixLogger("local_discovery.spec.ts")
}),
Expand Down Expand Up @@ -103,9 +103,7 @@ describe("Local Storage Discovery", function () {
it("should update peers in local storage on 'peer:identify' event", async () => {
const newPeerIdentifyEvent = {
detail: {
peerId: await createFromJSON({
id: mockPeers[1].id
}),
peerId: peerIdFromString(mockPeers[1].id.toString()),
listenAddrs: [multiaddr(mockPeers[1].address)]
}
} as CustomEvent<IdentifyResult>;
Expand Down
5 changes: 2 additions & 3 deletions packages/discovery/src/local-peer-cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { TypedEventEmitter } from "@libp2p/interface";
import {
CustomEvent,
IdentifyResult,
PeerDiscovery,
PeerDiscoveryEvents,
PeerInfo,
Startable
} from "@libp2p/interface";
import { createFromJSON } from "@libp2p/peer-id-factory";
import { peerIdFromString } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import {
type Libp2pComponents,
Expand Down Expand Up @@ -58,7 +57,7 @@
);

for (const { id: idStr, address } of this.peers) {
const peerId = await createFromJSON({ id: idStr });
const peerId = peerIdFromString(idStr);
if (await this.components.peerStore.has(peerId)) continue;

await this.components.peerStore.save(peerId, {
Expand Down Expand Up @@ -144,7 +143,7 @@
}
}

function isValidStoredPeer(peer: any): peer is LocalStoragePeerInfo {

Check warning on line 146 in packages/discovery/src/local-peer-cache/index.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type

Check warning on line 146 in packages/discovery/src/local-peer-cache/index.ts

View workflow job for this annotation

GitHub Actions / proto

Unexpected any. Specify a different type
return (
peer &&
typeof peer === "object" &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
import { TypedEventEmitter } from "@libp2p/interface";
import { peerDiscoverySymbol as symbol } from "@libp2p/interface";
import type {
IdentifyResult,
Expand Down
5 changes: 2 additions & 3 deletions packages/enr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@
},
"dependencies": {
"@ethersproject/rlp": "^5.7.0",
"@libp2p/crypto": "^4.1.6",
"@libp2p/peer-id": "^4.2.1",
"@libp2p/crypto": "^5.0.1",
"@libp2p/peer-id": "^5.0.1",
"@multiformats/multiaddr": "^12.0.0",
"@noble/secp256k1": "^1.7.1",
"@waku/utils": "0.0.20",
"debug": "^4.3.4",
"js-sha3": "^0.9.2"
},
"devDependencies": {
"@libp2p/peer-id-factory": "^4.2.1",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.3",
Expand Down
5 changes: 2 additions & 3 deletions packages/enr/src/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { utf8ToBytes } from "@waku/utils/bytes";

import { compressPublicKey } from "./crypto.js";
import { ENR } from "./enr.js";
import { getPublicKeyFromPeerId } from "./peer_id.js";

export class EnrCreator {
public static fromPublicKey(
publicKey: Uint8Array,
kvs: Record<ENRKey, ENRValue> = {}
): Promise<ENR> {
): ENR {
// EIP-778 specifies that the key must be in compressed format, 33 bytes
if (publicKey.length !== 33) {
publicKey = compressPublicKey(publicKey);
Expand All @@ -28,7 +27,7 @@ export class EnrCreator {
): Promise<ENR> {
switch (peerId.type) {
case "secp256k1":
return EnrCreator.fromPublicKey(getPublicKeyFromPeerId(peerId), kvs);
return EnrCreator.fromPublicKey(peerId.publicKey.raw, kvs);
default:
throw new Error();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/enr/src/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function fromValues(values: Uint8Array[]): Promise<ENR> {
}
const _seq = decodeSeq(seq);

const enr = await ENR.create(obj, _seq, signature);
const enr = ENR.create(obj, _seq, signature);
checkSignature(seq, kvs, enr, signature);
return enr;
}
Expand Down
33 changes: 17 additions & 16 deletions packages/enr/src/enr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import type { PeerId } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { multiaddr } from "@multiformats/multiaddr";
import * as secp from "@noble/secp256k1";
import type { Waku2 } from "@waku/interfaces";
Expand All @@ -16,14 +17,13 @@ import {
TransportProtocol,
TransportProtocolPerIpVersion
} from "./enr.js";
import { getPrivateKeyFromPeerId } from "./peer_id.js";

describe("ENR", function () {
describe("Txt codec", () => {
it("should encodeTxt and decodeTxt", async () => {
const peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
const enr = await EnrCreator.fromPeerId(peerId);
const privateKey = await getPrivateKeyFromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.multiaddrs = [
multiaddr("/dns4/node-01.do-ams3.waku.test.status.im/tcp/443/wss"),
Expand All @@ -42,7 +42,7 @@ describe("ENR", function () {
lightPush: false
};

const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKey.raw);
const enr2 = await EnrDecoder.fromString(txt);

if (!enr.signature) throw "enr.signature is undefined";
Expand Down Expand Up @@ -115,13 +115,13 @@ describe("ENR", function () {

it("should throw error - no id", async () => {
try {
const peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);
const enr = await EnrCreator.fromPeerId(peerId);
const privateKey = await getPrivateKeyFromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));

enr.set("id", new Uint8Array([0]));
const txt = await EnrEncoder.toString(enr, privateKey);
const txt = await EnrEncoder.toString(enr, privateKey.raw);

await EnrDecoder.fromString(txt);
assert.fail("Expect error here");
Expand All @@ -147,7 +147,7 @@ describe("ENR", function () {
describe("Verify", () => {
it("should throw error - no id", async () => {
try {
const enr = await ENR.create({}, BigInt(0), new Uint8Array());
const enr = ENR.create({}, BigInt(0), new Uint8Array());
enr.verify(new Uint8Array(), new Uint8Array());
assert.fail("Expect error here");
} catch (err: unknown) {
Expand All @@ -158,7 +158,7 @@ describe("ENR", function () {

it("should throw error - invalid id", async () => {
try {
const enr = await ENR.create(
const enr = ENR.create(
{ id: utf8ToBytes("v3") },
BigInt(0),
new Uint8Array()
Expand All @@ -173,7 +173,7 @@ describe("ENR", function () {

it("should throw error - no public key", async () => {
try {
const enr = await ENR.create(
const enr = ENR.create(
{ id: utf8ToBytes("v4") },
BigInt(0),
new Uint8Array()
Expand Down Expand Up @@ -204,7 +204,7 @@ describe("ENR", function () {
privateKey = hexToBytes(
"b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
);
record = await EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
record = EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
record.setLocationMultiaddr(multiaddr("/ip4/127.0.0.1/udp/30303"));
record.seq = seq;
await EnrEncoder.toString(record, privateKey);
Expand Down Expand Up @@ -249,7 +249,7 @@ describe("ENR", function () {
privateKey = hexToBytes(
"b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
);
record = await EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
record = EnrCreator.fromPublicKey(secp.getPublicKey(privateKey));
});

it("should get / set UDP multiaddr", () => {
Expand Down Expand Up @@ -320,7 +320,8 @@ describe("ENR", function () {
let enr: ENR;

before(async function () {
peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
peerId = peerIdFromPrivateKey(privateKey);
enr = await EnrCreator.fromPeerId(peerId);
enr.ip = ip4;
enr.ip6 = ip6;
Expand Down Expand Up @@ -422,9 +423,9 @@ describe("ENR", function () {
let privateKey: Uint8Array;

beforeEach(async function () {
peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
peerId = peerIdFromPrivateKey(privateKey);
enr = await EnrCreator.fromPeerId(peerId);
privateKey = await getPrivateKeyFromPeerId(peerId);
waku2Protocols = {
relay: false,
store: false,
Expand Down
Loading
Loading