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
2 changes: 1 addition & 1 deletion bindings/napi/BeaconStateView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub const js_meta = js.class(.{ .properties = .{
} });

cached_state: ?*CachedBeaconState = null,
pool_rc: ?*pool.PoolRc = null,
pool_rc: @TypeOf(pool.state.pool_rc) = null,
const BeaconStateView = @This();

pub fn init() BeaconStateView {
Expand Down
40 changes: 23 additions & 17 deletions bindings/napi/blst.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,31 @@ const BATCH_VERIFY_SIZE = 32;
/// Initialized lazily on first use, torn down via `deinitThreadPool`.
var thread_pool: ?*ThreadPool = null;

pub fn initThreadPool(n_workers: u16) !void {
if (thread_pool != null) return error.PoolExists;
thread_pool = try ThreadPool.init(std.heap.page_allocator, napi_io.get(), .{ .n_workers = n_workers });
}
/// Native-only thread pool lifecycle, reached from `root.zig` through the
/// pub `lifecycle` var so it is not part of the JS module surface.
const Lifecycle = struct {
pub fn initThreadPool(_: *Lifecycle, n_workers: u16) !void {
if (thread_pool != null) return error.PoolExists;
thread_pool = try ThreadPool.init(std.heap.page_allocator, napi_io.get(), .{ .n_workers = n_workers });
}

/// Closes the `ThreadPool` used for blst operations.
///
/// Note: this can invalidate any inflight verification requests. Consumer is responsible
/// for the lifecycle of their program and should only call this when all work is done.
///
/// This note is however application dependent. For the use case of lodestar,
/// it's likely that this would not be called at all.
/// Same goes for any other long-lived processes.
pub fn deinitThreadPool() void {
if (thread_pool) |p| {
p.deinit(napi_io.get());
thread_pool = null;
/// Closes the `ThreadPool` used for blst operations.
///
/// Note: this can invalidate any inflight verification requests. Consumer is responsible
/// for the lifecycle of their program and should only call this when all work is done.
///
/// This note is however application dependent. For the use case of lodestar,
/// it's likely that this would not be called at all.
/// Same goes for any other long-lived processes.
pub fn deinitThreadPool(_: *Lifecycle) void {
if (thread_pool) |p| {
p.deinit(napi_io.get());
thread_pool = null;
}
}
}
};

pub var lifecycle: Lifecycle = .{};

var gpa: std.heap.DebugAllocator(.{}) = .init;
const allocator = if (builtin.mode == .Debug)
Expand Down
4 changes: 2 additions & 2 deletions bindings/napi/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Preset = @import("preset").Preset;

const max_blob_schedule_entries = 16;

pub const State = struct {
const State = struct {
config: BeaconConfig = undefined,
initialized: bool = false,
config_name: [64]u8 = undefined,
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn set(object: js.Value, genesis_root: js.Uint8Array) !void {
state.initialized = true;
}

pub fn chainConfigFromObject(env: napi.Env, obj: napi.Value) !ChainConfig {
fn chainConfigFromObject(env: napi.Env, obj: napi.Value) !ChainConfig {
var chain_config: ChainConfig = undefined;

inline for (std.meta.fields(ChainConfig)) |field| {
Expand Down
4 changes: 2 additions & 2 deletions bindings/napi/pool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const allocator = std.heap.page_allocator;

const default_pool_size: u32 = 0;

pub const PoolRc = RefCount(Node.Pool);
const PoolRc = RefCount(Node.Pool);

/// Pool is wrapped in `RefCount` so binding objects holding pool refs at
/// process exit keep the pool alive until their JS finalizer runs. NAPI
/// env cleanup hook fires before module-level JS holders are finalized,
/// so an unconditional `pool.deinit()` there would free memory that
/// `pool.unref()` calls in those finalizers still need.
pub const State = struct {
const State = struct {
pool_rc: ?*PoolRc = null,

pub fn init(self: *State) !void {
Expand Down
2 changes: 1 addition & 1 deletion bindings/napi/pubkeys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const max_stack_aggregate_pubkeys = 512;
/// per epoch at 12s slots), so growth stays proportionate at any network scale.
const growth_step: u32 = preset.MAX_PENDING_DEPOSITS_PER_EPOCH * ((90 * 24 * 60 * 60) / (12 * preset.SLOTS_PER_EPOCH));

pub const State = struct {
const State = struct {
pubkey2index: PubkeyIndexMap = undefined,
index2pubkey: Index2PubkeyCache = undefined,
initialized: bool = false,
Expand Down
4 changes: 2 additions & 2 deletions bindings/napi/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn init(old_ref_count: u32) !void {
}

const n_workers = @min(cpu_count, @import("bls").ThreadPool.MAX_WORKERS);
try blst.initThreadPool(@intCast(n_workers));
try blst.lifecycle.initThreadPool(@intCast(n_workers));
try pool.state.init();
try pubkeys.state.init();
config.state.init();
Expand All @@ -55,7 +55,7 @@ fn detectCpuCount() !usize {
fn cleanup(new_ref_count: u32) void {
if (new_ref_count == 0) {
// Last environment — tear down shared state.
blst.deinitThreadPool();
blst.lifecycle.deinitThreadPool();
config.state.deinit();
pubkeys.state.deinit();
pool.state.deinit();
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
.hash = "zig_yaml-0.1.0-C1161kFWAwDxjKAFmklKwWVDvz2mmq0Q__bDhGGjeyd3",
},
.zapi = .{
.url = "git+https://github.com/chainsafe/zapi.git?ref=zapi-v2.x.0#92fe4babe4022ceb94a5cb24022a18fe27417d69",
.hash = "zapi-2.2.0-rIqzUXBaBADUsvvvB2N5kOBdLEO0rGPhowTqmIXe3qVh",
.url = "https://github.com/ChainSafe/zapi/archive/refs/tags/zapi-v3.1.0.tar.gz",
.hash = "zapi-3.1.0-rIqzUQJsBADCFUrmxoTZyOhSTkcW6m3t_OgOHc6oxnm9",
},
.zbench = .{
.url = "git+https://github.com/hendriknielaender/zBench#b2b89c475e3ef1bb2bd71255c80478a82d3e0ca8",
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@
]
},
"dependencies": {
"@chainsafe/zapi": "github:ChainSafe/zapi#6649cef91b19bcd9854aa1ffa4d0c1aed7270f55"
"@chainsafe/zapi": "3.1.0"
},
"bundleDependencies": [
"@chainsafe/zapi"
],
"devDependencies": {
"@biomejs/biome": "^2.3.11",
"@chainsafe/benchmark": "^2.0.1",
Expand Down
11 changes: 5 additions & 6 deletions pnpm-lock.yaml

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

Loading