Skip to content

Commit

Permalink
chore: fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Sep 12, 2024
1 parent f3c4117 commit 7515ff2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"got": "^11.8.6",
"inquirer": "^9.1.5",
"js-yaml": "^4.1.0",
"multiformats": "^13.2.2",
"prom-client": "^15.1.0",
"proper-lockfile": "^4.1.2",
"rimraf": "^4.4.1",
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/config/peerId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {PrivateKey} from "@libp2p/interface";
import {peerIdFromCID, peerIdFromPrivateKey} from "@libp2p/peer-id";
import {peerIdFromPrivateKey, peerIdFromString} from "@libp2p/peer-id";
import {
privateKeyFromProtobuf,
privateKeyToProtobuf,
Expand All @@ -8,7 +8,6 @@ import {
} from "@libp2p/crypto/keys";
import {fromString as uint8ArrayFromString} from "uint8arrays/from-string";
import {toString as uint8ArrayToString} from "uint8arrays/to-string";
import {CID} from "multiformats";
import {writeFile600Perm, readFile} from "../util/index.js";

// Peer id to / from JSON taken from peer-id-factory
Expand All @@ -32,7 +31,7 @@ export function exportToJSON(privateKey: PrivateKey): PeerIdJSON {
export function createFromJSON(obj: PeerIdJSON): PrivateKey {
const privateKey = privateKeyFromProtobuf(uint8ArrayFromString(obj.privKey, "base64pad"));
const publicKey = publicKeyFromProtobuf(uint8ArrayFromString(obj.pubKey, "base64pad"));
const peerId = peerIdFromCID(CID.parse(obj.id));
const peerId = peerIdFromString(obj.id);
if (!publicKey.equals(privateKey.publicKey)) {
throw new Error("Public key does not match private key");
}
Expand Down
13 changes: 7 additions & 6 deletions packages/cli/test/unit/cmds/beacon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {describe, it, expect} from "vitest";
import {multiaddr} from "@multiformats/multiaddr";
import {ENR, SignableENR} from "@chainsafe/enr";
import {generateKeyPair} from "@libp2p/crypto/keys";
import {peerIdFromPrivateKey} from "@libp2p/peer-id";
import {chainConfig} from "@lodestar/config/default";
import {chainConfigToJson} from "@lodestar/config";
import {LogLevel} from "@lodestar/utils";
Expand Down Expand Up @@ -58,7 +59,7 @@ describe("cmds / beacon / args handler", () => {
const {privateKey: pk1} = await runBeaconHandlerInit({});
const {privateKey: pk2} = await runBeaconHandlerInit({});

expect(pk1.toString()).not.toBe(pk2.toString());
expect(pk1.equals(pk2)).toBe(false);
});

it("Re-use existing peer", async () => {
Expand All @@ -74,7 +75,7 @@ describe("cmds / beacon / args handler", () => {
persistNetworkIdentity: true,
});

expect(privateKey.toString()).toBe(prevPk.toString());
expect(privateKey.equals(prevPk)).toBe(true);
});

it("Set known deposit contract", async () => {
Expand Down Expand Up @@ -128,7 +129,7 @@ describe("initPeerIdAndEnr", () => {
testLogger()
);

expect(pk1.toString()).not.toBe(pk2.toString());
expect(pk1.equals(pk2)).toBe(false);
});

it("should reuse peer id, persistNetworkIdentity=true", async () => {
Expand All @@ -143,7 +144,7 @@ describe("initPeerIdAndEnr", () => {
testLogger()
);

expect(pk1.toString()).toBe(pk2.toString());
expect(pk1.equals(pk2)).toBe(true);
});

it("should overwrite invalid peer id", async () => {
Expand All @@ -157,8 +158,8 @@ describe("initPeerIdAndEnr", () => {
);
const filePk = createFromJSON(JSON.parse(fs.readFileSync(peerIdFile, "utf-8")));

expect(pk1Str).not.toBe(pk2.toString());
expect(filePk.toString()).toBe(pk2.toString());
expect(pk1Str).not.toBe(peerIdFromPrivateKey(pk2).toString());
expect(filePk.equals(pk2)).toBe(true);
});

it("should overwrite invalid enr", async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/unit/cmds/initPeerIdAndEnr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("initPeerIdAndEnr", () => {
true
);

expect(run1.privateKey.toString()).toBe(run2.privateKey.toString());
expect(run1.privateKey.equals(run2.privateKey)).toBe(true);
expect(run1.enr.encodeTxt()).toBe(run2.enr.encodeTxt());
});
});

0 comments on commit 7515ff2

Please sign in to comment.