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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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
2,387 changes: 1,186 additions & 1,201 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/browser-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "npx playwright test"
},
"devDependencies": {
"@playwright/test": "^1.40.1",
"@playwright/test": "^1.48.1",
"@waku/create-app": "^0.1.1-504bcd4",
"dotenv-flow": "^4.1.0",
"serve": "^14.2.3"
Expand Down
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 @@ -81,6 +81,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.1.8"
},
"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
14 changes: 6 additions & 8 deletions packages/discovery/src/local-peer-cache/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 { PersistentPeerStore } from "@libp2p/peer-store";
import { peerIdFromPrivateKey, peerIdFromString } from "@libp2p/peer-id";
import { persistentPeerStore } from "@libp2p/peer-store";
import { multiaddr } from "@multiformats/multiaddr";
import { Libp2pComponents } from "@waku/interfaces";
import { LocalStoragePeerInfo } from "@waku/interfaces";
Expand Down Expand Up @@ -53,9 +53,9 @@ describe("Local Storage Discovery", function () {
beforeEach(async function () {
localStorage.clear();
components = {
peerStore: new PersistentPeerStore({
peerStore: 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
Loading
Loading