Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 3 additions & 4 deletions bindings/napi/BeaconStateView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const pubkey = @import("./pubkeys.zig");
const js_types = @import("./js_types.zig");
const sszValueToNapiValue = @import("./to_napi_value.zig").sszValueToNapiValue;
const numberSliceToNapiValue = @import("./to_napi_value.zig").numberSliceToNapiValue;
const napi_io = @import("./io.zig");

/// Allocator used for all BeaconStateView instances.
var gpa: std.heap.DebugAllocator(.{}) = .init;
Expand Down Expand Up @@ -1044,7 +1043,7 @@ pub fn createMultiProof(self: *const BeaconStateView, descriptor: js.Uint8Array)
pub fn computeUnrealizedCheckpoints(self: *const BeaconStateView) !js_types.UnrealizedCheckpoints {
const env = js.env();
const cached_state = try self.requireState();
const result = try st.computeUnrealizedCheckpoints(allocator, napi_io.get(), cached_state);
const result = try st.computeUnrealizedCheckpoints(allocator, js.io(), cached_state);

const obj = try env.createObject();
try obj.setNamedProperty(
Expand Down Expand Up @@ -1289,7 +1288,7 @@ pub fn processSlots(self: *const BeaconStateView, slot_arg: js.Number, options:
allocator.destroy(post_state);
}

try st.processSlots(allocator, napi_io.get(), post_state, slot_value, .{});
try st.processSlots(allocator, js.io(), post_state, slot_value, .{});
return .{
.cached_state = post_state,
.pool_rc = pool.state.poolRc().ref(),
Expand Down Expand Up @@ -1318,7 +1317,7 @@ pub fn stateTransition(self: *const BeaconStateView, signed_block_bytes: js.Uint
const signed_block = try AnySignedBeaconBlock.deserialize(allocator, .full, fork_seq, bytes);
defer signed_block.deinit(allocator);

const post_state = try st.stateTransition(allocator, napi_io.get(), cached_state, signed_block, opts);
const post_state = try st.stateTransition(allocator, js.io(), cached_state, signed_block, opts);
return .{
.cached_state = post_state,
.pool_rc = pool.state.poolRc().ref(),
Expand Down
17 changes: 8 additions & 9 deletions bindings/napi/blst.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const zapi = @import("zapi:zapi");
const js = zapi.js;
const napi = zapi.napi;
const bls = @import("bls");
const napi_io = @import("./io.zig");

const NativePublicKey = bls.PublicKey;
const NativeSignature = bls.Signature;
Expand Down Expand Up @@ -43,7 +42,7 @@ const State = struct {

pub fn init(self: *State, n_workers: u16) !void {
if (self.thread_pool != null) return error.PoolExists;
self.thread_pool = try ThreadPool.init(std.heap.page_allocator, napi_io.get(), .{ .n_workers = n_workers });
self.thread_pool = try ThreadPool.init(std.heap.page_allocator, js.io(), .{ .n_workers = n_workers });
}

/// Closes the `ThreadPool` used for blst operations.
Expand All @@ -56,7 +55,7 @@ const State = struct {
/// Same goes for any other long-lived processes.
pub fn deinit(self: *State) void {
if (self.thread_pool) |p| {
p.deinit(napi_io.get());
p.deinit(js.io());
self.thread_pool = null;
}
}
Expand Down Expand Up @@ -391,7 +390,7 @@ pub fn aggregateVerify(msgs: js.Array, pks: js.Array, sig: Signature, pks_valida

const pool = state.thread_pool orelse return error.ThreadPoolNotInitialized;
const result = pool.aggregateVerify(
napi_io.get(),
js.io(),
&sig.raw,
try boolOrDefault(sig_groupcheck, false),
msg_bufs,
Expand Down Expand Up @@ -489,7 +488,7 @@ pub fn verifyMultipleAggregateSignatures(sets: js.Array, pks_validate: ?js.Boole
};

var seed_bytes: [8]u8 = undefined;
const io = napi_io.get();
const io = js.io();
io.random(&seed_bytes);
var prng = std.Random.DefaultPrng.init(std.mem.readInt(u64, &seed_bytes, .little));
const rand = prng.random();
Expand Down Expand Up @@ -518,7 +517,7 @@ pub fn verifyMultipleAggregateSignatures(sets: js.Array, pks_validate: ?js.Boole

const pool = state.thread_pool orelse return error.ThreadPoolNotInitialized;
const result = pool.verifyMultipleAggregateSignatures(
napi_io.get(),
js.io(),
n_elems,
msgs,
DST,
Expand Down Expand Up @@ -628,7 +627,7 @@ pub fn aggregateWithRandomness(sets: js.Array) !js.Value {
var sig_ptrs: [MAX_AGGREGATE_PER_JOB]*const NativeSignature = undefined;

var seed_bytes: [8]u8 = undefined;
const io = napi_io.get();
const io = js.io();
io.random(&seed_bytes);
var prng = std.Random.DefaultPrng.init(std.mem.readInt(u64, &seed_bytes, .little));
const rand = prng.random();
Expand Down Expand Up @@ -737,7 +736,7 @@ fn asyncAggRand_execute(_: napi.Env, data: *AsyncAggRandData) void {
return;
};
pool.aggregateWithRandomness(
napi_io.get(),
js.io(),
data.pk_ptrs[0..data.n],
data.sig_ptrs[0..data.n],
data.randomness[0 .. data.n * 32],
Expand Down Expand Up @@ -833,7 +832,7 @@ pub fn asyncAggregateWithRandomness(sets: js.Array) !js.Value {
data.err = null;
data.deferred = undefined;
data.work = undefined;
napi_io.get().random(data.randomness[0 .. n * 32]);
js.io().random(data.randomness[0 .. n * 32]);

for (0..n) |i| {
const set = (try sets.get(@intCast(i))).toValue();
Expand Down
48 changes: 0 additions & 48 deletions bindings/napi/io.zig

This file was deleted.

3 changes: 1 addition & 2 deletions bindings/napi/metrics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const std = @import("std");
const builtin = @import("builtin");
const js = @import("zapi:zapi").js;
const state_transition = @import("state_transition");
const napi_io = @import("./io.zig");

var gpa: std.heap.DebugAllocator(.{}) = .init;
const allocator = if (builtin.mode == .Debug)
Expand All @@ -15,7 +14,7 @@ var initialized: bool = false;
/// JS: metrics.init() → void
pub fn init() !void {
if (initialized) return;
try state_transition.metrics.init(allocator, napi_io.get(), .{});
try state_transition.metrics.init(allocator, js.io(), .{});
initialized = true;
}

Expand Down
5 changes: 2 additions & 3 deletions bindings/napi/pubkeys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const bls = @import("bls");
const blst_bindings = @import("./blst.zig");
const PubkeyIndexMap = @import("state_transition").PubkeyIndexMap;
const Index2PubkeyCache = @import("state_transition").Index2PubkeyCache;
const napi_io = @import("./io.zig");
const preset = @import("preset").preset;

/// Owns the long-lived pubkey cache allocations.
Expand Down Expand Up @@ -89,7 +88,7 @@ fn pubkey2indexWrittenSize() usize {
pub fn save(file_path: js.String) !void {
var file_path_buf: [1024]u8 = undefined;
const path = try file_path.toSlice(&file_path_buf);
const io = napi_io.get();
const io = js.io();
const file = try std.Io.Dir.createFile(.cwd(), io, path, .{});
defer file.close(io);

Expand Down Expand Up @@ -118,7 +117,7 @@ pub fn save(file_path: js.String) !void {
pub fn load(file_path: js.String) !void {
var file_path_buf: [1024]u8 = undefined;
const path = try file_path.toSlice(&file_path_buf);
const io = napi_io.get();
const io = js.io();
const file = try std.Io.Dir.openFile(.cwd(), io, path, .{});
defer file.close(io);

Expand Down
7 changes: 1 addition & 6 deletions bindings/napi/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ pub const blst = @import("./blst.zig");
pub const pubkeys = @import("./pubkeys.zig");

const options = @import("bls_options");
const napi_io = @import("./io.zig");

var gpa: std.heap.DebugAllocator(.{}) = .init;
const allocator = if (builtin.mode == .Debug) gpa.allocator() else std.heap.c_allocator;

fn init(old_ref_count: u32) !void {
if (old_ref_count == 0) {
// First environment — initialize shared state in your threadpool init.
try napi_io.init();
errdefer napi_io.deinit();

var cpu_count: u64 = options.thread_count;
if (options.thread_count == 0) {
cpu_count = @max(try detectCpuCount(), 2) - 1;
Expand All @@ -45,7 +41,7 @@ fn init(old_ref_count: u32) !void {
/// not prevent the module from loading: warn and fall back to the affinity
/// count (what `std.Thread.getCpuCount()` reports).
fn detectCpuCount() !usize {
return @import("cpu_count").getNumCpus(allocator, napi_io.get()) catch |err| {
return @import("cpu_count").getNumCpus(allocator, js.io()) catch |err| {
std.log.debug(
"Warning: cgroup CPU detection failed ({s}), using affinity count\n",
.{@errorName(err)},
Expand All @@ -62,7 +58,6 @@ fn cleanup(new_ref_count: u32) void {
pubkeys.state.deinit();
pool.state.deinit();
metrics.deinit();
napi_io.deinit();
}
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/napi/stateTransition.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const napi_io = @import("./io.zig");
const js = @import("zapi:zapi").js;
const st = @import("state_transition");

pub fn deinitReusedEpochTransitionCache() void {
st.deinitReusedEpochTransitionCache(napi_io.get());
st.deinitReusedEpochTransitionCache(js.io());
}
Loading