Skip to content

Commit 4388a04

Browse files
committed
fix: init binding in test files
1 parent 91b6c77 commit 4388a04

File tree

8 files changed

+37
-13
lines changed

8 files changed

+37
-13
lines changed

bun/src/binding.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const fns = {
124124
},
125125
};
126126

127-
let binding : ConvertFns<typeof fns> = null;
127+
let binding : ConvertFns<typeof fns> | null = null;
128128

129129
// Load the compiled Zig shared library
130130
const libPromise = await openLibrary(import.meta.dirname, fns);

bun/src/committeeIndices.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { binding } from "./binding.js";
1+
import { getBinding } from "./binding.js";
22
import { validateShufflingParams } from "./shuffle.js";
33

44
// this is to sync the constant from zig to Bun which is 0xffffffff
5-
const ERROR_INDEX = binding.getErrorIndex();
5+
const ERROR_INDEX = 0xffffffff;
66

77
/** Pre-electra, byte count for random value is 1, post-electra, byte count for random value is 2 */
88
export declare enum ByteCount {
@@ -20,6 +20,7 @@ export function computeProposerIndex(
2020
rounds: number,
2121
): number {
2222
validateShufflingParams(activeIndices, seed, rounds);
23+
const binding = getBinding();
2324

2425
const result = binding.computeProposerIndex(
2526
seed,
@@ -48,6 +49,7 @@ export function computeProposerIndexElectra(
4849
rounds: number,
4950
): number {
5051
validateShufflingParams(activeIndices, seed, rounds);
52+
const binding = getBinding();
5153

5254
const result = binding.computeProposerIndexElectra(
5355
seed,
@@ -77,6 +79,7 @@ export function computeSyncCommitteeIndices(
7779
rounds: number,
7880
): Uint32Array {
7981
validateShufflingParams(activeIndices, seed, rounds);
82+
const binding = getBinding();
8083

8184
const out = new Uint32Array(syncCommitteeSize);
8285
const result = binding.computeSyncCommitteeIndices(
@@ -111,6 +114,7 @@ export function computeSyncCommitteeIndicesElectra(
111114
rounds: number,
112115
): Uint32Array {
113116
validateShufflingParams(activeIndices, seed, rounds);
117+
const binding = getBinding();
114118

115119
const out = new Uint32Array(syncCommitteeSize);
116120
const result = binding.computeSyncCommitteeIndicesElectra(

bun/src/index.ts

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

bun/src/pubkeyIndexMap.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { binding } from "./binding.js";
1+
import { getBinding } from "./binding.js";
22

33
// this is to sync the constant from zig to Bun which is 0xffffffff
4-
const NOT_FOUND_INDEX = binding.getNotFoundIndex();
4+
const NOT_FOUND_INDEX = 0xffffffff;
55

66
const registry = new FinalizationRegistry((ptr) => {
7+
const binding = getBinding();
78
binding.destroyPubkeyIndexMap(ptr);
89
});
910

@@ -16,6 +17,7 @@ export class PubkeyIndexMap {
1617
// see https://bun.sh/docs/api/ffi#pointers
1718
private native_ptr: number;
1819
constructor() {
20+
const binding = getBinding();
1921
const pointer = binding.createPubkeyIndexMap();
2022
if (pointer == null) {
2123
throw new Error("Failed to create PubkeyIndexMap");
@@ -25,6 +27,7 @@ export class PubkeyIndexMap {
2527
}
2628

2729
get(key: Uint8Array): number | null {
30+
const binding = getBinding();
2831
const index = binding.pubkeyIndexMapGet(this.native_ptr, key, key.length);
2932
if (index === NOT_FOUND_INDEX) {
3033
return null;
@@ -33,6 +36,7 @@ export class PubkeyIndexMap {
3336
}
3437

3538
set(key: Uint8Array, value: number): void {
39+
const binding = getBinding();
3640
const res = binding.pubkeyIndexMapSet(
3741
this.native_ptr,
3842
key,
@@ -45,18 +49,22 @@ export class PubkeyIndexMap {
4549
}
4650

4751
has(key: Uint8Array): boolean {
52+
const binding = getBinding();
4853
return binding.pubkeyIndexMapHas(this.native_ptr, key, key.length);
4954
}
5055

5156
size(): number {
57+
const binding = getBinding();
5258
return binding.pubkeyIndexMapSize(this.native_ptr);
5359
}
5460

5561
delete(key: Uint8Array): boolean {
62+
const binding = getBinding();
5663
return binding.pubkeyIndexMapDelete(this.native_ptr, key, key.length);
5764
}
5865

5966
clear(): void {
67+
const binding = getBinding();
6068
binding.pubkeyIndexMapClear(this.native_ptr);
6169
}
6270
}

bun/src/shuffle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getBinding } from "./binding.js";
22

3-
const binding = getBinding();
43
/**
54
* shuffle a list of indices in place
65
* activeIndices is modified, we return it for convenience and make it the same to @chainsafe/swap-or-not-shuffle
@@ -12,6 +11,7 @@ export function shuffleList(
1211
): Uint32Array {
1312
validateShufflingParams(activeIndices, seed, rounds);
1413

14+
const binding = getBinding();
1515
const clonedActiveIndices = activeIndices.slice();
1616
const result = binding.shuffleList(
1717
clonedActiveIndices,
@@ -38,6 +38,7 @@ export function unshuffleList(
3838
rounds: number,
3939
): Uint32Array {
4040
validateShufflingParams(activeIndices, seed, rounds);
41+
const binding = getBinding();
4142

4243
const clonedActiveIndices = activeIndices.slice();
4344
const result = binding.unshuffleList(
@@ -76,6 +77,7 @@ export function withPollingParams(
7677
if (timeToWaitMs < 0 || pollEveryMs <= 0 || timeoutMs <= 0) {
7778
throw new Error("Invalid parameter");
7879
}
80+
const binding = getBinding();
7981

8082
async function doShuffleList(
8183
activeIndices: Uint32Array,

bun/test/unit/committeeIndices.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "bun:test";
1+
import { describe, expect, it, beforeAll } from "bun:test";
22
import { randomBytes } from "node:crypto";
33
import {
44
EFFECTIVE_BALANCE_INCREMENT,
@@ -12,10 +12,15 @@ import { computeProposerIndex } from "@lodestar/state-transition";
1212
import {
1313
computeProposerIndexElectra,
1414
computeSyncCommitteeIndicesElectra,
15+
initBinding,
1516
} from "../../src/index.js";
1617
import { naiveComputeSyncCommitteeIndicesElectra } from "../referenceImplementation.js";
1718

1819
describe("computeProposerIndex", () => {
20+
beforeAll(async () => {
21+
await initBinding();
22+
});
23+
1924
it("should compute the same index as reference implementation", async () => {
2025
const seed = randomBytes(32);
2126
const vc = 1000;

bun/test/unit/pubkeyIndexMap.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { describe, expect, it } from "bun:test";
2-
import { PubkeyIndexMap } from "../../src/pubkeyIndexMap.js";
1+
import { describe, expect, it, beforeAll } from "bun:test";
2+
import { PubkeyIndexMap, initBinding } from "../../src/index.js";
33

44
describe("PubkeyIndexMap", () => {
5+
beforeAll(async () => {
6+
await initBinding();
7+
});
8+
59
it("should init/populate/get/set/remove/clear", () => {
610
const map = new PubkeyIndexMap();
711
expect(map.size()).toBe(0);

bun/test/unit/shuffle.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { describe, expect, it, before } from "bun:test";
1+
import { describe, expect, it, beforeAll } from "bun:test";
22
import { randomBytes } from "node:crypto";
33
import {
44
shuffleList,
55
unshuffleList,
66
withPollingParams,
7-
} from "../../src/shuffle.js";
7+
initBinding,
8+
} from "../../src/index.js";
89
import * as referenceImplementation from "../referenceImplementation.js";
9-
import { initBinding } from "../../src/binding.js";
1010

1111
// start polling right after the call for every 1ms, throw error if after 100ms
1212
const { asyncShuffleList, asyncUnshuffleList } = withPollingParams(0, 1, 100);
@@ -28,7 +28,7 @@ describe("unshuffleList", () => {
2828
},
2929
];
3030

31-
before(async () => {
31+
beforeAll(async () => {
3232
await initBinding();
3333
});
3434

0 commit comments

Comments
 (0)