From cf71c299349dfdaa659d1cfc126e6bce58e0c724 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Tue, 19 Sep 2023 16:06:47 +0700 Subject: [PATCH 1/5] feat: migrate @chainsafe/persistent-merkle-tree to 0.6.1 --- packages/api/package.json | 2 +- packages/beacon-node/package.json | 2 +- packages/cli/package.json | 1 + packages/cli/src/cmds/index.ts | 21 ++++++++++--------- packages/light-client/package.json | 2 +- .../src/utils/verifyMerkleBranch.ts | 6 +++--- packages/prover/src/cli/cli.ts | 7 +++++++ packages/state-transition/package.json | 2 +- yarn.lock | 7 ------- 9 files changed, 26 insertions(+), 24 deletions(-) diff --git a/packages/api/package.json b/packages/api/package.json index f87de28f0882..25aaf3a231b6 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -69,7 +69,7 @@ "check-readme": "typescript-docs-verifier" }, "dependencies": { - "@chainsafe/persistent-merkle-tree": "^0.5.0", + "@chainsafe/persistent-merkle-tree": "^0.6.1", "@chainsafe/ssz": "^0.13.0", "@lodestar/config": "^1.11.1", "@lodestar/params": "^1.11.1", diff --git a/packages/beacon-node/package.json b/packages/beacon-node/package.json index 44759696641a..907e7f0aab74 100644 --- a/packages/beacon-node/package.json +++ b/packages/beacon-node/package.json @@ -102,7 +102,7 @@ "@chainsafe/discv5": "^5.1.0", "@chainsafe/libp2p-gossipsub": "^10.1.0", "@chainsafe/libp2p-noise": "^13.0.0", - "@chainsafe/persistent-merkle-tree": "^0.5.0", + "@chainsafe/persistent-merkle-tree": "^0.6.1", "@chainsafe/prometheus-gc-stats": "^1.0.0", "@chainsafe/ssz": "^0.13.0", "@chainsafe/threads": "^1.11.1", diff --git a/packages/cli/package.json b/packages/cli/package.json index a4a387fc670b..107d225e4133 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -61,6 +61,7 @@ "@chainsafe/discv5": "^5.1.0", "@chainsafe/ssz": "^0.13.0", "@chainsafe/threads": "^1.11.1", + "@chainsafe/persistent-merkle-tree": "^0.6.1", "@libp2p/crypto": "^2.0.2", "@libp2p/peer-id": "^3.0.1", "@libp2p/peer-id-factory": "^3.0.2", diff --git a/packages/cli/src/cmds/index.ts b/packages/cli/src/cmds/index.ts index 849cb23d9af7..26d5f3b02b24 100644 --- a/packages/cli/src/cmds/index.ts +++ b/packages/cli/src/cmds/index.ts @@ -1,15 +1,16 @@ +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; import {CliCommand} from "../util/index.js"; import {GlobalArgs} from "../options/index.js"; -import {beacon} from "./beacon/index.js"; -import {dev} from "./dev/index.js"; -import {validator} from "./validator/index.js"; -import {lightclient} from "./lightclient/index.js"; -import {bootnode} from "./bootnode/index.js"; +// without setting this first, persistent-merkle-tree will use noble instead +setHasher(hasher); export const cmds: Required>>["subcommands"] = [ - beacon, - validator, - lightclient, - dev, - bootnode, + (await import("./beacon/index.js")).beacon, + (await import("./validator/index.js")).validator, + (await import("./lightclient/index.js")).lightclient, + (await import("./dev/index.js")).dev, + (await import("./bootnode/index.js")).bootnode, ]; diff --git a/packages/light-client/package.json b/packages/light-client/package.json index 95bca9e36b29..f2bac8225e59 100644 --- a/packages/light-client/package.json +++ b/packages/light-client/package.json @@ -65,7 +65,7 @@ }, "dependencies": { "@chainsafe/bls": "7.1.1", - "@chainsafe/persistent-merkle-tree": "^0.5.0", + "@chainsafe/persistent-merkle-tree": "^0.6.1", "@chainsafe/ssz": "^0.13.0", "@lodestar/api": "^1.11.1", "@lodestar/config": "^1.11.1", diff --git a/packages/light-client/src/utils/verifyMerkleBranch.ts b/packages/light-client/src/utils/verifyMerkleBranch.ts index 7cdf673aaf87..87b1d660eb32 100644 --- a/packages/light-client/src/utils/verifyMerkleBranch.ts +++ b/packages/light-client/src/utils/verifyMerkleBranch.ts @@ -1,5 +1,5 @@ import {byteArrayEquals} from "@chainsafe/ssz"; -import {hash} from "@chainsafe/persistent-merkle-tree"; +import {hasher} from "@chainsafe/persistent-merkle-tree"; export const SYNC_COMMITTEES_DEPTH = 4; export const SYNC_COMMITTEES_INDEX = 11; @@ -20,9 +20,9 @@ export function isValidMerkleBranch( let value = leaf; for (let i = 0; i < depth; i++) { if (Math.floor(index / 2 ** i) % 2) { - value = hash(proof[i], value); + value = hasher.digest64(proof[i], value); } else { - value = hash(value, proof[i]); + value = hasher.digest64(value, proof[i]); } } return byteArrayEquals(value, root); diff --git a/packages/prover/src/cli/cli.ts b/packages/prover/src/cli/cli.ts index 8ef53e781a40..9860bbf4fe9c 100644 --- a/packages/prover/src/cli/cli.ts +++ b/packages/prover/src/cli/cli.ts @@ -1,3 +1,7 @@ +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; // Must not use `* as yargs`, see https://github.com/yargs/yargs/issues/1131 import yargs from "yargs"; import {hideBin} from "yargs/helpers"; @@ -6,6 +10,9 @@ import {getVersionData} from "../utils/version.js"; import {cmds} from "./cmds/index.js"; import {globalOptions} from "./options.js"; +// without setting this first, persistent-merkle-tree will use noble instead +setHasher(hasher); + const {version} = getVersionData(); const topBanner = `🌟 Lodestar Prover Proxy: Ethereum RPC proxy for RPC responses, verified against the trusted block hashes. * Version: ${version} diff --git a/packages/state-transition/package.json b/packages/state-transition/package.json index 133e149188b7..653c7c6b68fa 100644 --- a/packages/state-transition/package.json +++ b/packages/state-transition/package.json @@ -59,7 +59,7 @@ "dependencies": { "@chainsafe/as-sha256": "^0.3.1", "@chainsafe/bls": "7.1.1", - "@chainsafe/persistent-merkle-tree": "^0.5.0", + "@chainsafe/persistent-merkle-tree": "^0.6.1", "@chainsafe/persistent-ts": "^0.19.1", "@chainsafe/ssz": "^0.13.0", "@lodestar/config": "^1.11.1", diff --git a/yarn.lock b/yarn.lock index 8d6155b15fec..f8decbcf93ea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -611,13 +611,6 @@ dependencies: "@chainsafe/is-ip" "^2.0.1" -"@chainsafe/persistent-merkle-tree@^0.5.0": - version "0.5.0" - resolved "https://registry.yarnpkg.com/@chainsafe/persistent-merkle-tree/-/persistent-merkle-tree-0.5.0.tgz#2b4a62c9489a5739dedd197250d8d2f5427e9f63" - integrity sha512-l0V1b5clxA3iwQLXP40zYjyZYospQLZXzBVIhhr9kDg/1qHZfzzHw0jj4VPBijfYCArZDlPkRi1wZaV2POKeuw== - dependencies: - "@chainsafe/as-sha256" "^0.3.1" - "@chainsafe/persistent-merkle-tree@^0.6.1": version "0.6.1" resolved "https://registry.yarnpkg.com/@chainsafe/persistent-merkle-tree/-/persistent-merkle-tree-0.6.1.tgz#37bde25cf6cbe1660ad84311aa73157dc86ec7f2" From 319b71b0aec2ff5d548d54bd086baec90c5d3d55 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Tue, 19 Sep 2023 17:43:45 +0700 Subject: [PATCH 2/5] fix: setHasher first --- packages/cli/src/cmds/index.ts | 21 ++++++++++----------- packages/cli/src/index.ts | 15 +++++++++++---- packages/prover/src/cli/cli.ts | 7 ------- packages/prover/src/cli/index.ts | 14 ++++++++++---- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/packages/cli/src/cmds/index.ts b/packages/cli/src/cmds/index.ts index 26d5f3b02b24..849cb23d9af7 100644 --- a/packages/cli/src/cmds/index.ts +++ b/packages/cli/src/cmds/index.ts @@ -1,16 +1,15 @@ -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; import {CliCommand} from "../util/index.js"; import {GlobalArgs} from "../options/index.js"; +import {beacon} from "./beacon/index.js"; +import {dev} from "./dev/index.js"; +import {validator} from "./validator/index.js"; +import {lightclient} from "./lightclient/index.js"; +import {bootnode} from "./bootnode/index.js"; -// without setting this first, persistent-merkle-tree will use noble instead -setHasher(hasher); export const cmds: Required>>["subcommands"] = [ - (await import("./beacon/index.js")).beacon, - (await import("./validator/index.js")).validator, - (await import("./lightclient/index.js")).lightclient, - (await import("./dev/index.js")).dev, - (await import("./bootnode/index.js")).bootnode, + beacon, + validator, + lightclient, + dev, + bootnode, ]; diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index add49804c9be..294211426ed5 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,11 +1,18 @@ #!/usr/bin/env node -// MUST import first to apply preset from args -import "./applyPreset.js"; -import {YargsError} from "./util/index.js"; -import {getLodestarCli, yarg} from "./cli.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; import "source-map-support/register.js"; +// without setting this first, persistent-merkle-tree will use noble instead +setHasher(hasher); + +// MUST import second to apply preset from args +await import("./applyPreset.js"); +const {YargsError} = await import("./util/index.js"); +const {getLodestarCli, yarg} = await import("./cli.js"); const lodestar = getLodestarCli(); void lodestar diff --git a/packages/prover/src/cli/cli.ts b/packages/prover/src/cli/cli.ts index 9860bbf4fe9c..8ef53e781a40 100644 --- a/packages/prover/src/cli/cli.ts +++ b/packages/prover/src/cli/cli.ts @@ -1,7 +1,3 @@ -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; // Must not use `* as yargs`, see https://github.com/yargs/yargs/issues/1131 import yargs from "yargs"; import {hideBin} from "yargs/helpers"; @@ -10,9 +6,6 @@ import {getVersionData} from "../utils/version.js"; import {cmds} from "./cmds/index.js"; import {globalOptions} from "./options.js"; -// without setting this first, persistent-merkle-tree will use noble instead -setHasher(hasher); - const {version} = getVersionData(); const topBanner = `🌟 Lodestar Prover Proxy: Ethereum RPC proxy for RPC responses, verified against the trusted block hashes. * Version: ${version} diff --git a/packages/prover/src/cli/index.ts b/packages/prover/src/cli/index.ts index 53a32a02eb87..f40bdff2e353 100644 --- a/packages/prover/src/cli/index.ts +++ b/packages/prover/src/cli/index.ts @@ -1,11 +1,17 @@ #!/usr/bin/env node -// MUST import first to apply preset from args -import "./applyPreset.js"; -import {YargsError} from "../utils/errors.js"; -import {getLodestarProverCli, yarg} from "./cli.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; import "source-map-support/register.js"; +// without setting this first, persistent-merkle-tree will use noble instead +setHasher(hasher); +// MUST import second to apply preset from args +await import("./applyPreset.js"); +const {getLodestarProverCli, yarg} = await import("./cli.js"); +const {YargsError} = await import("../utils/errors.js"); const prover = getLodestarProverCli(); void prover From ff2fb5c60784d959dd4c1c2e53b46183fe2e3ec4 Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 2 Oct 2023 11:06:03 -0400 Subject: [PATCH 3/5] chore: move setHasher to applyPreset.ts --- packages/cli/src/applyPreset.ts | 9 +++++++++ packages/cli/src/index.ts | 15 ++++----------- packages/prover/src/cli/applyPreset.ts | 9 +++++++++ packages/prover/src/cli/index.ts | 14 ++++---------- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/applyPreset.ts b/packages/cli/src/applyPreset.ts index f0f784f8ae61..760c18dbbcd7 100644 --- a/packages/cli/src/applyPreset.ts +++ b/packages/cli/src/applyPreset.ts @@ -1,4 +1,13 @@ // MUST import this file first before anything and not import any Lodestar code. + +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; + +// without setting this first, persistent-merkle-tree will use noble instead +setHasher(hasher); + // // ## Rationale // diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 294211426ed5..25e968c1c72c 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,18 +1,11 @@ #!/usr/bin/env node -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; +// MUST import first to apply preset from args and set ssz hasher +import("./applyPreset.js"); +import {YargsError} from "./util/index.js"; +import {getLodestarCli, yarg} from "./cli.js"; import "source-map-support/register.js"; -// without setting this first, persistent-merkle-tree will use noble instead -setHasher(hasher); - -// MUST import second to apply preset from args -await import("./applyPreset.js"); -const {YargsError} = await import("./util/index.js"); -const {getLodestarCli, yarg} = await import("./cli.js"); const lodestar = getLodestarCli(); void lodestar diff --git a/packages/prover/src/cli/applyPreset.ts b/packages/prover/src/cli/applyPreset.ts index a6a3568c5f91..158e05243ec7 100644 --- a/packages/prover/src/cli/applyPreset.ts +++ b/packages/prover/src/cli/applyPreset.ts @@ -1,4 +1,13 @@ // MUST import this file first before anything and not import any Lodestar code. + +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; +// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies +import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; + +// without setting this first, persistent-merkle-tree will use noble instead +setHasher(hasher); + // // ## Rationale // diff --git a/packages/prover/src/cli/index.ts b/packages/prover/src/cli/index.ts index f40bdff2e353..f5644f02f8f1 100644 --- a/packages/prover/src/cli/index.ts +++ b/packages/prover/src/cli/index.ts @@ -1,17 +1,11 @@ #!/usr/bin/env node -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {hasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/as-sha256.js"; -// eslint-disable-next-line no-restricted-imports, import/no-extraneous-dependencies -import {setHasher} from "@chainsafe/persistent-merkle-tree/lib/hasher/index.js"; +// MUST import first to apply preset from args and set ssz hasher +import("./applyPreset.js"); +import {YargsError} from "../utils/errors.js"; +import {getLodestarProverCli, yarg} from "./cli.js"; import "source-map-support/register.js"; -// without setting this first, persistent-merkle-tree will use noble instead -setHasher(hasher); -// MUST import second to apply preset from args -await import("./applyPreset.js"); -const {getLodestarProverCli, yarg} = await import("./cli.js"); -const {YargsError} = await import("../utils/errors.js"); const prover = getLodestarProverCli(); void prover From 3596ab86eab2cb79365ed2ab0a88db0329aa165b Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 2 Oct 2023 11:07:18 -0400 Subject: [PATCH 4/5] chore: fix applyPreset import --- packages/cli/src/index.ts | 2 +- packages/prover/src/cli/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 25e968c1c72c..5cdccbacfeec 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node // MUST import first to apply preset from args and set ssz hasher -import("./applyPreset.js"); +import "./applyPreset.js"; import {YargsError} from "./util/index.js"; import {getLodestarCli, yarg} from "./cli.js"; import "source-map-support/register.js"; diff --git a/packages/prover/src/cli/index.ts b/packages/prover/src/cli/index.ts index f5644f02f8f1..845831b32cb0 100644 --- a/packages/prover/src/cli/index.ts +++ b/packages/prover/src/cli/index.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node // MUST import first to apply preset from args and set ssz hasher -import("./applyPreset.js"); +import "./applyPreset.js"; import {YargsError} from "../utils/errors.js"; import {getLodestarProverCli, yarg} from "./cli.js"; import "source-map-support/register.js"; From cf8bd0d58a5c38b3f180362d6a3b5ecd1cec8eb9 Mon Sep 17 00:00:00 2001 From: Cayman Date: Thu, 12 Oct 2023 15:38:11 -0400 Subject: [PATCH 5/5] chore: fix test utility --- packages/light-client/test/utils/utils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/light-client/test/utils/utils.ts b/packages/light-client/test/utils/utils.ts index c5f5b78afe42..df9bd4170dcc 100644 --- a/packages/light-client/test/utils/utils.ts +++ b/packages/light-client/test/utils/utils.ts @@ -1,6 +1,6 @@ import bls from "@chainsafe/bls/switchable"; import {PointFormat, PublicKey, SecretKey} from "@chainsafe/bls/types"; -import {hash, Tree} from "@chainsafe/persistent-merkle-tree"; +import {hasher, Tree} from "@chainsafe/persistent-merkle-tree"; import {BitArray, fromHexString} from "@chainsafe/ssz"; import {BeaconConfig} from "@lodestar/config"; import { @@ -235,9 +235,9 @@ export function computeMerkleBranch( for (let i = 0; i < depth; i++) { proof[i] = Buffer.alloc(32, i); if (Math.floor(index / 2 ** i) % 2) { - value = hash(proof[i], value); + value = hasher.digest64(proof[i], value); } else { - value = hash(value, proof[i]); + value = hasher.digest64(value, proof[i]); } } return {root: value, proof};