Repository navigation
refactor(bindings): use zapi js.io() instead of local io module #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
f951042
690cb48
7417538
d24e9d5
1fd50dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -39,25 +38,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 { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern is safety hatch for above mentioned problem. Now we can access the lifecycle functions in zig but not in the js surface.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems reasonable and cleaner than #483 , but thinking about future modules we expose, might get confusing with mixed use in the same file, but this might be overthinking, we can reconsider again when/if we encounter this issue because the blast radius here is not big nit: is |
||
| pub fn initThreadPool(_: *Lifecycle, n_workers: u16) !void { | ||
| if (thread_pool != null) return error.PoolExists; | ||
| thread_pool = try ThreadPool.init(std.heap.page_allocator, js.io(), .{ .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(js.io()); | ||
| thread_pool = null; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| pub var lifecycle: Lifecycle = .{}; | ||
|
|
||
| var gpa: std.heap.DebugAllocator(.{}) = .init; | ||
| const allocator = if (builtin.mode == .Debug) | ||
|
|
@@ -375,7 +380,7 @@ pub fn aggregateVerify(msgs: js.Array, pks: js.Array, sig: Signature, pks_valida | |
|
|
||
| const pool = thread_pool orelse return error.ThreadPoolNotInitialized; | ||
| const result = pool.aggregateVerify( | ||
| napi_io.get(), | ||
| js.io(), | ||
| &sig.raw, | ||
| try boolOrDefault(sig_groupcheck, false), | ||
| msg_bufs, | ||
|
|
@@ -473,7 +478,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(); | ||
|
|
@@ -503,7 +508,7 @@ pub fn verifyMultipleAggregateSignatures(sets: js.Array, pks_validate: ?js.Boole | |
|
|
||
| const pool = thread_pool orelse return error.ThreadPoolNotInitialized; | ||
| const result = pool.verifyMultipleAggregateSignatures( | ||
| napi_io.get(), | ||
| js.io(), | ||
| n_elems, | ||
| msgs, | ||
| DST, | ||
|
|
@@ -613,7 +618,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(); | ||
|
|
@@ -720,7 +725,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], | ||
|
|
@@ -816,7 +821,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(); | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have kept the function as it is by using following signature.
But then it would have been expose to JS surface, which we don't want, as it's an internal life cycle function only used at root with the number of cpu count.