Skip to content

Commit b3a6f46

Browse files
authored
fix: default to as-sha256 if no chip support for AVX (#7887)
**Motivation** A crash was reported by several parties running Lodestar on a VPS. The problem was tracked down to no AVX support on the host which causes OffchainLabs/hashtree to crash. The go binding do a similar check, at the bindings level, and revert to a non-assembly SHA256 lib. This PR does the same. Checks the hardware for the supported AVX types and returns `as-sha256` hasher from the `hashtree` hasher so there is no crash. https://github.com/OffchainLabs/hashtree/blob/67979dccfc99b02b488ed8f5fddee9f09aea411e/src/hashtree.c#L53-L67
1 parent ad63e51 commit b3a6f46

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

packages/cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"@lodestar/utils": "^1.30.0",
7575
"@lodestar/validator": "^1.30.0",
7676
"@multiformats/multiaddr": "^12.1.3",
77+
"cpu-features": "^0.0.10",
7778
"deepmerge": "^4.3.1",
7879
"ethers": "^6.7.0",
7980
"find-up": "^6.3.0",
@@ -88,6 +89,7 @@
8889
},
8990
"devDependencies": {
9091
"@lodestar/test-utils": "^1.30.0",
92+
"@types/cpu-features": "^0.0.3",
9193
"@types/debug": "^4.1.7",
9294
"@types/inquirer": "^9.0.3",
9395
"@types/proper-lockfile": "^4.1.4",

packages/cli/src/applyPreset.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
// MUST import this file first before anything and not import any Lodestar code.
22
import {setHasher} from "@chainsafe/persistent-merkle-tree";
3-
import {hasher} from "@chainsafe/persistent-merkle-tree/hasher/hashtree";
3+
import {hasher as asSha256Hasher} from "@chainsafe/persistent-merkle-tree/hasher/as-sha256";
4+
import {hasher as hashtreeHasher} from "@chainsafe/persistent-merkle-tree/hasher/hashtree";
5+
import CpuFeatures from "cpu-features";
46

57
// without setting this first, persistent-merkle-tree will use noble instead
6-
setHasher(hasher);
8+
const cpuFeatures = CpuFeatures();
9+
if (
10+
cpuFeatures.arch === "x86" &&
11+
!(cpuFeatures.flags.avx || cpuFeatures.flags.avx2 || cpuFeatures.flags.avx512f || cpuFeatures.flags.avx512vl)
12+
) {
13+
console.log(
14+
"Hashtree hasher is preferred but the CPU architecture does not support AVX, AVX2 or the correct AVX512 instruction sets. Falling back to as-sha256 hasher instead."
15+
);
16+
setHasher(asSha256Hasher);
17+
} else {
18+
setHasher(hashtreeHasher);
19+
}
720

821
//
922
// ## Rationale

packages/prover/src/cli/applyPreset.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
// MUST import this file first before anything and not import any Lodestar code.
2-
32
import {setHasher} from "@chainsafe/persistent-merkle-tree";
4-
import {hasher} from "@chainsafe/persistent-merkle-tree/hasher/hashtree";
3+
import {hasher as asSha256Hasher} from "@chainsafe/persistent-merkle-tree/hasher/as-sha256";
4+
import {hasher as hashtreeHasher} from "@chainsafe/persistent-merkle-tree/hasher/hashtree";
5+
import CpuFeatures from "cpu-features";
56

67
// without setting this first, persistent-merkle-tree will use noble instead
7-
setHasher(hasher);
8+
const cpuFeatures = CpuFeatures();
9+
if (
10+
cpuFeatures.arch === "x86" &&
11+
!(cpuFeatures.flags.avx || cpuFeatures.flags.avx2 || cpuFeatures.flags.avx512f || cpuFeatures.flags.avx512vl)
12+
) {
13+
// biome-ignore lint/suspicious/noConsole: let consumer know that the default hasher is not supported
14+
console.log(
15+
"Hashtree hasher is preferred but the CPU architecture does not support AVX, AVX2 or the correct AVX512 instruction sets. Falling back to as-sha256 hasher instead."
16+
);
17+
setHasher(asSha256Hasher);
18+
} else {
19+
setHasher(hashtreeHasher);
20+
}
821

922
//
1023
// ## Rationale

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,6 +3357,11 @@
33573357
resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.6.0.tgz#eac397f28bf1d6ae0ae081363eca2f425bedf0d5"
33583358
integrity sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA==
33593359

3360+
"@types/cpu-features@^0.0.3":
3361+
version "0.0.3"
3362+
resolved "https://registry.yarnpkg.com/@types/cpu-features/-/cpu-features-0.0.3.tgz#a2bec076eb5dc95e0a6c23d1f8d389be4109b309"
3363+
integrity sha512-W/Ep+LDZoxMbCcH7LHRB3RN+TY4gbHl3u4uRq4XsxOh1gnpf5Lkwy5xWTBKSaJYQuMLW2XPAmRWA5Ucsy2EGVQ==
3364+
33603365
"@types/datastore-level@^3.0.0":
33613366
version "3.0.0"
33623367
resolved "https://registry.npmjs.org/@types/datastore-level/-/datastore-level-3.0.0.tgz"
@@ -5380,6 +5385,14 @@ cosmiconfig@^8.2.0:
53805385
parse-json "^5.2.0"
53815386
path-type "^4.0.0"
53825387

5388+
cpu-features@^0.0.10:
5389+
version "0.0.10"
5390+
resolved "https://registry.yarnpkg.com/cpu-features/-/cpu-features-0.0.10.tgz#9aae536db2710c7254d7ed67cb3cbc7d29ad79c5"
5391+
integrity sha512-9IkYqtX3YHPCzoVg1Py+o9057a3i0fp7S530UWokCSaFVTc7CwXPRiOjRjBQQ18ZCNafx78YfnG+HALxtVmOGA==
5392+
dependencies:
5393+
buildcheck "~0.0.6"
5394+
nan "^2.19.0"
5395+
53835396
cpu-features@~0.0.4:
53845397
version "0.0.8"
53855398
resolved "https://registry.yarnpkg.com/cpu-features/-/cpu-features-0.0.8.tgz#a2d464b023b8ad09004c8cdca23b33f192f63546"

0 commit comments

Comments
 (0)