Skip to content

Commit

Permalink
fix: spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Oct 3, 2024
1 parent b03b7c0 commit 133f805
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 418 deletions.
376 changes: 4 additions & 372 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 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: 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
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
9 changes: 6 additions & 3 deletions packages/relay/src/message_validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generateKeyPair } from "@libp2p/crypto/keys";
import { TopicValidatorResult } from "@libp2p/interface";
import type { UnsignedMessage } from "@libp2p/interface";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { peerIdFromPrivateKey } from "@libp2p/peer-id";
import { createEncoder } from "@waku/core";
import { determinePubsubTopic } from "@waku/utils";
import { expect } from "chai";
Expand All @@ -15,7 +16,8 @@ describe("Message Validator", () => {
it("Accepts a valid Waku Message", async () => {
await fc.assert(
fc.asyncProperty(fc.uint8Array({ minLength: 1 }), async (payload) => {
const peerId = await createSecp256k1PeerId();
const privateKey = await generateKeyPair("secp256k1");
const peerId = peerIdFromPrivateKey(privateKey);

const encoder = createEncoder({
contentTopic: TestContentTopic,
Expand All @@ -39,7 +41,8 @@ describe("Message Validator", () => {
it("Rejects garbage", async () => {
await fc.assert(
fc.asyncProperty(fc.uint8Array(), async (data) => {
const peerId = await createSecp256k1PeerId();
const peerId =
await generateKeyPair("secp256k1").then(peerIdFromPrivateKey);

const message: UnsignedMessage = {
type: "unsigned",
Expand Down

0 comments on commit 133f805

Please sign in to comment.