Skip to content

Commit f9e7aa0

Browse files
authored
Merge branch 'napi' into bing/blst-extended
2 parents 723bc53 + f724240 commit f9e7aa0

43 files changed

Lines changed: 708 additions & 440 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Publish NAPI Bindings
2+
on:
3+
workflow_dispatch:
4+
5+
env:
6+
ZIG_VERSION: "0.14.1"
7+
NODE_VERSION: "24"
8+
9+
jobs:
10+
publish-bindings:
11+
name: publish bindings
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Repository
15+
uses: actions/checkout@v4
16+
- name: Setup pnpm
17+
uses: pnpm/action-setup@v4
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v6
20+
with:
21+
node-version: ${{ env.NODE_VERSION }}
22+
cache: pnpm
23+
- name: Install dependencies
24+
run: |
25+
pnpm install
26+
- name: Run bindings formatter
27+
run: |
28+
pnpm biome ci
29+
- name: Install Zig
30+
uses: mlugg/setup-zig@v2
31+
with:
32+
version: ${{ env.ZIG_VERSION }}
33+
cache-key: ubuntu-latest-${{ env.ZIG_VERSION }}
34+
- name: Build and publish to npm
35+
run: |
36+
pnpm zapi build-artifacts --optimize ReleaseSafe
37+
pnpm zapi prepublish
38+
pnpm zapi publish --access public
39+

bench/ssz/attestation.zig

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const std = @import("std");
2-
const Attestation = @import("consensus_types").deneb.Attestation;
2+
// TODO make this fork-agnostic
3+
const Attestation = @import("consensus_types").fulu.Attestation;
4+
const SignedBeaconBlock = @import("consensus_types").fulu.SignedBeaconBlock;
35
const ssz = @import("ssz");
46
const zbench = @import("zbench");
7+
const download_era_options = @import("download_era_options");
8+
const era = @import("era");
9+
const config = @import("config");
510

611
// printf "Date: %s\nKernel: %s\nCPU: %s\nCPUs: %s\nMemory: %s\n" "$(date)" "$(uname -r)" "$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs)" "$(lscpu | grep '^CPU(s):' | awk '{print $2}')" "$(free -h | grep Mem | awk '{print $2}')"
712
// Date: Fri Apr 25 10:07:24 AM EDT 2025
@@ -115,13 +120,28 @@ pub fn main() !void {
115120
var bench = zbench.Benchmark.init(allocator, .{});
116121
defer bench.deinit();
117122

118-
const attestation_file = try std.fs.cwd().openFile("bench/attestation.ssz", .{});
119-
defer attestation_file.close();
120-
const attestation_bytes = try attestation_file.readToEndAlloc(allocator, 1_000_000_000);
123+
const era_path = try std.fs.path.join(
124+
allocator,
125+
&[_][]const u8{ download_era_options.era_out_dir, download_era_options.era_files[1] },
126+
);
127+
defer allocator.free(era_path);
121128

122-
const attestation = allocator.create(Attestation.Type) catch unreachable;
123-
attestation.* = Attestation.default_value;
124-
Attestation.deserializeFromBytes(allocator, attestation_bytes, attestation) catch unreachable;
129+
var era_reader = try era.Reader.open(allocator, config.mainnet.config, era_path);
130+
defer era_reader.close(allocator);
131+
132+
const block_slot = try era.era.computeStartBlockSlotFromEraNumber(era_reader.era_number) + 1;
133+
134+
const block_bytes: []u8 = @constCast(try era_reader.readSerializedBlock(allocator, block_slot) orelse return error.InvalidEraFile);
135+
defer allocator.free(block_bytes);
136+
137+
var block = SignedBeaconBlock.default_value;
138+
defer block.deinit();
139+
try SignedBeaconBlock.deserializeFromBytes(allocator, block_bytes, &block);
140+
141+
const attestation = &block.message.body.attestations.items[0];
142+
const attestation_bytes = try allocator.alloc(u8, Attestation.serializedSize(attestation));
143+
defer allocator.free(attestation_bytes);
144+
_ = Attestation.serializeIntoBytes(attestation, attestation_bytes);
125145

126146
const serialize_attestation = SerializeAttestation{ .attestation = attestation };
127147
try bench.addParam("serialize attestation", &serialize_attestation, .{});

bench/ssz/block.zig

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const std = @import("std");
2-
const BeaconBlock = @import("consensus_types").deneb.SignedBeaconBlock;
2+
// TODO make this fork-agnostic
3+
const BeaconBlock = @import("consensus_types").fulu.SignedBeaconBlock;
34
const ssz = @import("ssz");
45
const zbench = @import("zbench");
6+
const download_era_options = @import("download_era_options");
7+
const era = @import("era");
8+
const config = @import("config");
59

610
// printf "Date: %s\nKernel: %s\nCPU: %s\nCPUs: %s\nMemory: %s\n" "$(date)" "$(uname -r)" "$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs)" "$(lscpu | grep '^CPU(s):' | awk '{print $2}')" "$(free -h | grep Mem | awk '{print $2}')"
711
// Date: Mon Apr 21 12:59:32 PM EDT 2025
@@ -107,9 +111,19 @@ pub fn main() !void {
107111
var bench = zbench.Benchmark.init(allocator, .{});
108112
defer bench.deinit();
109113

110-
const block_file = try std.fs.cwd().openFile("bench/block.ssz", .{});
111-
defer block_file.close();
112-
const block_bytes = try block_file.readToEndAlloc(allocator, 1_000_000_000);
114+
const era_path = try std.fs.path.join(
115+
allocator,
116+
&[_][]const u8{ download_era_options.era_out_dir, download_era_options.era_files[1] },
117+
);
118+
defer allocator.free(era_path);
119+
120+
var era_reader = try era.Reader.open(allocator, config.mainnet.config, era_path);
121+
defer era_reader.close(allocator);
122+
123+
const block_slot = try era.era.computeStartBlockSlotFromEraNumber(era_reader.era_number) + 1;
124+
125+
const block_bytes: []u8 = @constCast(try era_reader.readSerializedBlock(allocator, block_slot) orelse return error.InvalidEraFile);
126+
defer allocator.free(block_bytes);
113127

114128
const block = allocator.create(BeaconBlock.Type) catch unreachable;
115129
block.* = BeaconBlock.default_value;

bench/ssz/state.zig

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const std = @import("std");
2-
const BeaconState = @import("consensus_types").deneb.BeaconState;
2+
// TODO make this fork-agnostic
3+
const BeaconState = @import("consensus_types").fulu.BeaconState;
34
const ssz = @import("ssz");
45
const zbench = @import("zbench");
6+
const download_era_options = @import("download_era_options");
7+
const era = @import("era");
8+
const config = @import("config");
59

610
// printf "Date: %s\nKernel: %s\nCPU: %s\nCPUs: %s\nMemory: %s\n" "$(date)" "$(uname -r)" "$(lscpu | grep 'Model name' | awk -F: '{print $2}' | xargs)" "$(lscpu | grep '^CPU(s):' | awk '{print $2}')" "$(free -h | grep Mem | awk '{print $2}')"
711
// Date: Mon Apr 21 12:59:32 PM EDT 2025
@@ -107,9 +111,16 @@ pub fn main() !void {
107111
var bench = zbench.Benchmark.init(allocator, .{});
108112
defer bench.deinit();
109113

110-
const state_file = try std.fs.cwd().openFile("bench/state.ssz", .{});
111-
defer state_file.close();
112-
const state_bytes = try state_file.readToEndAlloc(allocator, 1_000_000_000);
114+
const era_path = try std.fs.path.join(
115+
allocator,
116+
&[_][]const u8{ download_era_options.era_out_dir, download_era_options.era_files[0] },
117+
);
118+
defer allocator.free(era_path);
119+
var era_reader = try era.Reader.open(allocator, config.mainnet.config, era_path);
120+
defer era_reader.close(allocator);
121+
122+
const state_bytes: []u8 = @constCast(try era_reader.readSerializedState(allocator, null));
123+
defer allocator.free(state_bytes);
113124

114125
const state = allocator.create(BeaconState.Type) catch unreachable;
115126
state.* = BeaconState.default_value;

bench/state_transition/process_block.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ fn ProcessOperationsBench(comptime fork: ForkSeq, comptime opts: BenchOpts) type
186186
cloned.config,
187187
cloned.getEpochCache(),
188188
cloned.state.castToFork(fork),
189+
&cloned.slashings_cache,
189190
.full,
190191
self.body,
191192
.{ .verify_signature = opts.verify_signature },
@@ -236,6 +237,7 @@ fn ProcessBlockBench(comptime fork: ForkSeq, comptime opts: BenchOpts) type {
236237
cloned.config,
237238
cloned.getEpochCache(),
238239
cloned.state.castToFork(fork),
240+
&cloned.slashings_cache,
239241
.full,
240242
self.block,
241243
external_data,
@@ -400,6 +402,7 @@ fn ProcessBlockSegmentedBench(comptime fork: ForkSeq) type {
400402
cloned.config,
401403
epoch_cache,
402404
state,
405+
&cloned.slashings_cache,
403406
.full,
404407
self.body,
405408
.{ .verify_signature = true },
@@ -492,7 +495,7 @@ fn runBenchmark(comptime fork: ForkSeq, allocator: std.mem.Allocator, pool: *Nod
492495
const validators = try beacon_state.validatorsSlice(allocator);
493496
defer allocator.free(validators);
494497

495-
try state_transition.syncPubkeys(validators, pubkey_index_map, index_pubkey_cache);
498+
try state_transition.syncPubkeys(validators, &pubkey_index_map, &index_pubkey_cache);
496499

497500
const cached_state = try CachedBeaconState.createCachedBeaconState(allocator, beacon_state, .{
498501
.config = &beacon_config,
@@ -507,6 +510,7 @@ fn runBenchmark(comptime fork: ForkSeq, allocator: std.mem.Allocator, pool: *Nod
507510
.{},
508511
);
509512
try cached_state.state.commit();
513+
try state_transition.buildSlashingsCacheFromStateIfNeeded(allocator, cached_state.state, &cached_state.slashings_cache);
510514
try stdout.print("State: slot={}, validators={}\n", .{ try cached_state.state.slot(), try beacon_state.validatorsCount() });
511515

512516
var bench = zbench.Benchmark.init(allocator, .{

bench/state_transition/process_epoch.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fn ProcessInactivityUpdatesBench(comptime fork: ForkSeq) type {
5353
defer cache.deinit();
5454
state_transition.processInactivityUpdates(
5555
fork,
56+
allocator,
5657
cloned.config,
5758
cloned.getEpochCache(),
5859
cloned.state.castToFork(fork),
@@ -81,6 +82,7 @@ fn ProcessRewardsAndPenaltiesBench(comptime fork: ForkSeq) type {
8182
cloned.getEpochCache(),
8283
cloned.state.castToFork(fork),
8384
&cache,
85+
null,
8486
) catch unreachable;
8587
}
8688
};
@@ -121,12 +123,13 @@ fn ProcessSlashingsBench(comptime fork: ForkSeq) type {
121123
}
122124
var cache = EpochTransitionCache.init(allocator, cloned.config, cloned.getEpochCache(), cloned.state) catch unreachable;
123125
defer cache.deinit();
124-
state_transition.processSlashings(
126+
_ = state_transition.processSlashings(
125127
fork,
126128
allocator,
127129
cloned.getEpochCache(),
128130
cloned.state.castToFork(fork),
129131
&cache,
132+
true,
130133
) catch unreachable;
131134
}
132135
};
@@ -462,6 +465,7 @@ fn ProcessEpochSegmentedBench(comptime fork: ForkSeq) type {
462465
const inactivity_start = std.time.nanoTimestamp();
463466
state_transition.processInactivityUpdates(
464467
fork,
468+
allocator,
465469
cloned.config,
466470
epoch_cache,
467471
fork_state,
@@ -481,12 +485,13 @@ fn ProcessEpochSegmentedBench(comptime fork: ForkSeq) type {
481485
recordSegment(.registry_updates, elapsedSince(registry_start));
482486

483487
const slashings_start = std.time.nanoTimestamp();
484-
state_transition.processSlashings(
488+
const slashing_penalties = state_transition.processSlashings(
485489
fork,
486490
allocator,
487491
epoch_cache,
488492
fork_state,
489493
&cache,
494+
false,
490495
) catch unreachable;
491496
recordSegment(.slashings, elapsedSince(slashings_start));
492497

@@ -498,6 +503,7 @@ fn ProcessEpochSegmentedBench(comptime fork: ForkSeq) type {
498503
epoch_cache,
499504
fork_state,
500505
&cache,
506+
slashing_penalties,
501507
) catch unreachable;
502508
recordSegment(.rewards_and_penalties, elapsedSince(rewards_start));
503509

@@ -659,7 +665,7 @@ fn runBenchmark(
659665
const validators = try beacon_state.validatorsSlice(allocator);
660666
defer allocator.free(validators);
661667

662-
try state_transition.syncPubkeys(validators, pubkey_index_map, index_pubkey_cache);
668+
try state_transition.syncPubkeys(validators, &pubkey_index_map, &index_pubkey_cache);
663669

664670
const immutable_data = state_transition.EpochCacheImmutableData{
665671
.config = &beacon_config,

0 commit comments

Comments
 (0)