Skip to content

Commit 714267d

Browse files
committed
feat: bun binding for committeeIndices utils
1 parent e7d8801 commit 714267d

8 files changed

Lines changed: 695 additions & 1 deletion

File tree

test/bun/bun.lock

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/bun/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"@types/js-yaml": "^4.0.9",
88
"@biomejs/biome": "^1.9.3",
99
"@chainsafe/benchmark": "^1.2.3",
10+
"@chainsafe/as-sha256": "^0.5.0",
11+
"@lodestar/params": "^1.27.0",
12+
"@lodestar/state-transition": "^1.27.0",
13+
"@lodestar/utils": "^1.27.0",
1014
"tar": "^7.4.0",
1115
"js-yaml": "^4.1.0"
1216
},

test/bun/src/binding.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dlopen, ptr } from "bun:ffi";
1+
import { dlopen } from "bun:ffi";
22
import { getBinaryName, getPrebuiltBinaryPath } from "../utils/index.js";
33

44
const binaryName = getBinaryName();
@@ -18,6 +18,10 @@ const lib = dlopen(binaryPath, {
1818
args: [],
1919
returns: "u32",
2020
},
21+
getErrorIndex: {
22+
args: [],
23+
returns: "u32",
24+
},
2125
pubkeyIndexMapSet: {
2226
args: ["ptr", "ptr", "u32", "u32"],
2327
returns: "u32",
@@ -71,6 +75,47 @@ const lib = dlopen(binaryPath, {
7175
args: ["u32"],
7276
returns: "void",
7377
},
78+
computeProposerIndexElectra: {
79+
args: ["ptr", "u32", "ptr", "u32", "ptr", "u32", "u64", "u32", "u32"],
80+
returns: "u32",
81+
},
82+
computeProposerIndex: {
83+
args: ["ptr", "u32", "ptr", "u32", "ptr", "u32", "u8", "u64", "u32", "u32"],
84+
returns: "u32",
85+
},
86+
computeSyncCommitteeIndicesElectra: {
87+
args: [
88+
"ptr",
89+
"u32",
90+
"ptr",
91+
"u32",
92+
"ptr",
93+
"u32",
94+
"u64",
95+
"u32",
96+
"u32",
97+
"ptr",
98+
"u32",
99+
],
100+
returns: "u32",
101+
},
102+
computeSyncCommitteeIndices: {
103+
arts: [
104+
"ptr",
105+
"u32",
106+
"ptr",
107+
"u32",
108+
"ptr",
109+
"u32",
110+
"u8",
111+
"u64",
112+
"u32",
113+
"u32",
114+
"ptr",
115+
"u32",
116+
],
117+
returns: "u32",
118+
},
74119
});
75120

76121
export const binding = lib.symbols;

test/bun/src/committeeIndices.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { binding } from "./binding.js";
2+
3+
// this is to sync the constant from zig to Bun which is 0xffffffff
4+
const ERROR_INDEX = binding.getErrorIndex();
5+
6+
/** Pre-electra, byte count for random value is 1, post-electra, byte count for random value is 2 */
7+
export declare enum ByteCount {
8+
One = 1,
9+
Two = 2,
10+
}
11+
12+
export function computeProposerIndex(
13+
seed: Uint8Array,
14+
activeIndices: Uint32Array,
15+
effectiveBalanceIncrements: Uint16Array,
16+
randByteCount: ByteCount,
17+
maxEffectiveBalanceElectra: number,
18+
effectiveBalanceIncrement: number,
19+
rounds: number,
20+
): number {
21+
const result = binding.computeProposerIndex(
22+
seed,
23+
seed.length,
24+
activeIndices,
25+
activeIndices.length,
26+
effectiveBalanceIncrements,
27+
effectiveBalanceIncrements.length,
28+
randByteCount,
29+
maxEffectiveBalanceElectra,
30+
effectiveBalanceIncrement,
31+
rounds,
32+
);
33+
if (result === ERROR_INDEX) {
34+
throw new Error("Failed to compute proposer index");
35+
}
36+
return result;
37+
}
38+
39+
export function computeProposerIndexElectra(
40+
seed: Uint8Array,
41+
activeIndices: Uint32Array,
42+
effectiveBalanceIncrements: Uint16Array,
43+
maxEffectiveBalanceElectra: number,
44+
effectiveBalanceIncrement: number,
45+
rounds: number,
46+
): number {
47+
const result = binding.computeProposerIndexElectra(
48+
seed,
49+
seed.length,
50+
activeIndices,
51+
activeIndices.length,
52+
effectiveBalanceIncrements,
53+
effectiveBalanceIncrements.length,
54+
maxEffectiveBalanceElectra,
55+
effectiveBalanceIncrement,
56+
rounds,
57+
);
58+
if (result === ERROR_INDEX) {
59+
throw new Error("Failed to compute proposer index");
60+
}
61+
return result;
62+
}
63+
64+
export function computeSyncCommitteeIndices(
65+
seed: Uint8Array,
66+
activeIndices: Uint32Array,
67+
effectiveBalanceIncrements: Uint16Array,
68+
randByteCount: ByteCount,
69+
syncCommitteeSize: number,
70+
maxEffectiveBalanceElectra: number,
71+
effectiveBalanceIncrement: number,
72+
rounds: number,
73+
): Uint32Array {
74+
const out = new Uint32Array(syncCommitteeSize);
75+
const result = binding.computeSyncCommitteeIndices(
76+
seed,
77+
seed.length,
78+
activeIndices,
79+
activeIndices.length,
80+
effectiveBalanceIncrements,
81+
effectiveBalanceIncrements.length,
82+
randByteCount,
83+
maxEffectiveBalanceElectra,
84+
effectiveBalanceIncrement,
85+
rounds,
86+
out,
87+
out.length,
88+
);
89+
if (result !== 0) {
90+
throw new Error(
91+
`Failed to compute sync committee indices, result = ${result}`,
92+
);
93+
}
94+
return out;
95+
}
96+
97+
export function computeSyncCommitteeIndicesElectra(
98+
seed: Uint8Array,
99+
activeIndices: Uint32Array,
100+
effectiveBalanceIncrements: Uint16Array,
101+
syncCommitteeSize: number,
102+
maxEffectiveBalanceElectra: number,
103+
effectiveBalanceIncrement: number,
104+
rounds: number,
105+
): Uint32Array {
106+
const out = new Uint32Array(syncCommitteeSize);
107+
const result = binding.computeSyncCommitteeIndicesElectra(
108+
seed,
109+
seed.length,
110+
activeIndices,
111+
activeIndices.length,
112+
effectiveBalanceIncrements,
113+
effectiveBalanceIncrements.length,
114+
maxEffectiveBalanceElectra,
115+
effectiveBalanceIncrement,
116+
rounds,
117+
out,
118+
out.length,
119+
);
120+
if (result !== 0) {
121+
throw new Error(
122+
`Failed to compute sync committee indices electra, result = ${result}`,
123+
);
124+
}
125+
return out;
126+
}

test/bun/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export * from "./pubkeyIndexMap.js";
2+
export * from "./shuffle.js";
3+
export * from "./committeeIndices.js";

0 commit comments

Comments
 (0)