Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
092e2e9
feat: remove test/bun
twoeths May 9, 2025
39e84cd
feat: add bun binding at cwd
twoeths May 9, 2025
91b6c77
fix: skip Bun in CI
twoeths May 9, 2025
4388a04
fix: init binding in test files
twoeths May 9, 2025
57e2d8a
fix: add optimize config
twoeths May 9, 2025
5107ffb
fix: bun folder path
twoeths May 9, 2025
1f25498
fix: bun-ffi-z dynamic-lib-name
twoeths May 12, 2025
a0fe350
fix: lazy load binding in shuffle.ts
twoeths May 12, 2025
57b3578
feat: use bun-ffi-z github url
twoeths May 12, 2025
b9f68a8
fix: lint package.json
twoeths May 12, 2025
2d00352
fix: add benchmark resource
twoeths May 12, 2025
9ce52b7
fix: unset binding in closeBinding()
twoeths May 12, 2025
98326e3
feat: upload artifacts in CI
twoeths May 12, 2025
c23db2f
feat: add test-linux-x64-gnu-downloaded-artifacts on CI
twoeths May 12, 2025
e864fb6
fix: only run benchmark on linux x86_64
twoeths May 12, 2025
d6f431f
feat: add test-linux-arm64-gnu-downloaded-artifacts on CI
twoeths May 13, 2025
203cc6e
fix: remove blank line in test.yml
twoeths May 13, 2025
60b9185
fix: name of download artifact for linux arm64 gnu
twoeths May 13, 2025
66db603
Revert "fix: only run benchmark on linux x86_64"
twoeths May 13, 2025
69bc587
fix: move benchmark to a separate job
twoeths May 13, 2025
8b62d06
refactor: specify zig target tripple
twoeths May 13, 2025
4fdefd3
fix: only run bun unit tests on target platforms
twoeths May 13, 2025
e3c06cf
feat: test bun-linux-x64-musl on CI
twoeths May 13, 2025
88f95d0
fix: only test pubkeyIndexMap on x86_64-linux-musl
twoeths May 13, 2025
a19e76b
fix: run shuffle.test.ts for linux-x64-musl
twoeths May 13, 2025
3dbf4b8
fix: only run pubkeyIndexMap.test.ts for linux-x64-musl
twoeths May 13, 2025
688a1ad
feat: add bun-linux-arm64-musl test
twoeths May 13, 2025
2b85b4c
chore: reorder matrix.settings
twoeths May 13, 2025
0090229
feat: add macos tests for x64 and arm64
twoeths May 13, 2025
e21fc0f
feat: build windows-latest
twoeths May 13, 2025
979d75d
fix: skip check binary for windows
twoeths May 13, 2025
4376d6f
fix: generalize dynamic lib output file name for windows
twoeths May 13, 2025
1b68060
feat: add test for windows-latest platform
twoeths May 13, 2025
0408d30
fix: use top level await
twoeths May 14, 2025
416ba82
fix: use oven-sh/setup-bun
twoeths May 14, 2025
93e6c6d
fix: run bun:lint check once
twoeths May 14, 2025
71c27dc
fix: switch to the official bun-ffi-z repo
twoeths May 15, 2025
e7b7a33
fix: unsupport windows
twoeths May 15, 2025
18ea709
fix: simplify binding
twoeths May 15, 2025
86ca3e4
fix: remove redundant build cli params
twoeths May 15, 2025
fe3f0a6
feat: prepublish step in workflow (#11)
twoeths May 16, 2025
f28257c
feat: add release-please action
twoeths May 17, 2025
cc279c6
fix: only trigger release-please on a push to main branch
twoeths May 17, 2025
9bdc193
fix: add bun/CHANGELOG.md
twoeths May 17, 2025
fa6da59
fix: set initial version to v0.0.0
twoeths May 17, 2025
7a8b7ed
fix: single CI workflow
twoeths May 17, 2025
d848013
fix: simplify openLibrary bunCwd param
twoeths May 17, 2025
880c03c
feat: add publish step
twoeths May 18, 2025
9dd10a9
fix: switch to import.meta.dirname
twoeths May 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,21 @@ jobs:
echo "$HOME/.bun/bin" >> $GITHUB_PATH
bun --version

# (Optional) Install dependencies if you use bun install.
- name: Bun - Install Dependencies
run: bun install
working-directory: ./test/bun
working-directory: ./bun

- name: Bun - Lint Code
run: bun lint
working-directory: ./test/bun
working-directory: ./bun

- name: Bun - Unit Tests
run: bun test:unit
working-directory: ./test/bun
working-directory: ./bun

- name: Benchmark
run: bun benchmark
working-directory: ./test/bun
working-directory: ./bun
env:
# To write to PRs and commits
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ zig-out/

test/bun/node_modules/

test/bun/benchmark_data/
test/bun/benchmark_data/

bun/node_modules/

bun/benchmark_data/
File renamed without changes.
6 changes: 6 additions & 0 deletions bun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# state-transition-utils-bun
State transition utilities for Bun, which is Bun binding using [state-transition-z](https://github.com/ChainSafe/state-transition-z)

To consume this libraries, application needs to call these apis:
- `initBinding()`: at application startup, before calling any apis of this library
- `closeBinding()`: at application close()
37 changes: 21 additions & 16 deletions test/bun/bun.lock → bun/bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion test/bun/package.json → bun/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "state-transition-bun",
"name": "@chainsafe/state-transition-bun",
"module": "src/index.ts",
"type": "module",
"dependencies": {
"@chainsafe/bun-ffi-z": "https://github.com/twoeths/bun-ffi-z.git#te/fix_open_library"
},
"devDependencies": {
"@types/bun": "latest",
"@types/js-yaml": "^4.0.9",
Expand All @@ -18,10 +21,24 @@
"typescript": "^5.0.0"
},
"scripts": {
"build": "bun ./node_modules/.bin/bun-ffi-z build --zig-cwd '..'",
"test:unit": "bun test test/unit",
"lint": "biome check",
"benchmark": "bun benchmark:files 'test/perf/*.test.ts'",
"benchmark:files": "bun ./node_modules/.bin/benchmark --config .benchrc.yaml --defaultBranch main",
"lint:fix": "yarn lint --write"
},
"bun-ffi-z": {
"name": "state-transition-utils",
"targets": [
"linux-x64-gnu",
"linux-arm64-gnu",
"linux-x64-musl",
"linux-arm64-musl",
"darwin-x64",
"darwin-arm64"
],
"optimize": "ReleaseSafe",
"zigCwd": ".."
}
}
43 changes: 33 additions & 10 deletions test/bun/src/binding.ts → bun/src/binding.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { dlopen } from "bun:ffi";
import { getBinaryName, getPrebuiltBinaryPath } from "../utils/index.js";
import type { ConvertFns } from "bun:ffi";
import path from "node:path";
import { openLibrary } from "@chainsafe/bun-ffi-z";

const binaryName = getBinaryName();
const binaryPath = getPrebuiltBinaryPath(binaryName);

// Load the compiled Zig shared library
const lib = dlopen(binaryPath, {
const fns = {
createPubkeyIndexMap: {
args: [],
returns: "ptr",
Expand Down Expand Up @@ -116,10 +113,36 @@ const lib = dlopen(binaryPath, {
],
returns: "u32",
},
});
};

let binding: ConvertFns<typeof fns> | null = null;

// Load the compiled Zig shared library
const libPromise = await openLibrary(path.resolve("../bun"), fns);

/**
* Call this api to initialize the binding.
*/
export async function initBinding(): Promise<void> {
const lib = await libPromise;
binding = lib.symbols;
}

export const binding = lib.symbols;
/**
* Get the binding.
*/
export function getBinding(): ConvertFns<typeof fns> {
if (binding == null) {
throw new Error("Binding not initialized. Call init() first.");
}
return binding;
}

Comment thread
twoeths marked this conversation as resolved.
Outdated
export function closeBinding(): void {
/**
* Call this api to close the binding.
*/
export async function closeBinding(): Promise<void> {
Comment thread
twoeths marked this conversation as resolved.
Outdated
const lib = await libPromise;
lib.close();
binding = null;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { binding } from "./binding.js";
import { getBinding } from "./binding.js";
import { validateShufflingParams } from "./shuffle.js";

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

/** Pre-electra, byte count for random value is 1, post-electra, byte count for random value is 2 */
export declare enum ByteCount {
Expand All @@ -20,6 +20,7 @@ export function computeProposerIndex(
rounds: number,
): number {
validateShufflingParams(activeIndices, seed, rounds);
const binding = getBinding();
Comment thread
twoeths marked this conversation as resolved.
Outdated

const result = binding.computeProposerIndex(
seed,
Expand Down Expand Up @@ -48,6 +49,7 @@ export function computeProposerIndexElectra(
rounds: number,
): number {
validateShufflingParams(activeIndices, seed, rounds);
const binding = getBinding();

const result = binding.computeProposerIndexElectra(
seed,
Expand Down Expand Up @@ -77,6 +79,7 @@ export function computeSyncCommitteeIndices(
rounds: number,
): Uint32Array {
validateShufflingParams(activeIndices, seed, rounds);
const binding = getBinding();

const out = new Uint32Array(syncCommitteeSize);
const result = binding.computeSyncCommitteeIndices(
Expand Down Expand Up @@ -111,6 +114,7 @@ export function computeSyncCommitteeIndicesElectra(
rounds: number,
): Uint32Array {
validateShufflingParams(activeIndices, seed, rounds);
const binding = getBinding();

const out = new Uint32Array(syncCommitteeSize);
const result = binding.computeSyncCommitteeIndicesElectra(
Expand Down
1 change: 1 addition & 0 deletions test/bun/src/index.ts → bun/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./pubkeyIndexMap.js";
export * from "./shuffle.js";
export * from "./committeeIndices.js";
export * from "./binding.js";
Loading