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
12 changes: 12 additions & 0 deletions bindings/napi/pubkeys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ pub const State = struct {
self.index2pubkey.deinit(allocator);
self.initialized = false;
}

pub fn reset(self: *State) !void {
if (!self.initialized) return;

self.pubkey2index.clearRetainingCapacity();
self.index2pubkey.shrinkRetainingCapacity(0);
}
};

pub var state: State = .{};
Expand Down Expand Up @@ -152,6 +159,11 @@ pub fn load(file_path: js.String) !void {
state.initialized = true;
}

/// JS: pubkeys.reset()
pub fn reset() !void {
try state.reset();
}

/// JS: pubkeys.getIndex(pubkeyBytes) → number | null
pub fn getIndex(pubkey: js.Uint8Array) !js.Value {
if (!state.initialized) return error.PubkeyIndexNotInitialized;
Expand Down
2 changes: 2 additions & 0 deletions bindings/src/pubkeys.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface PubkeyCache {
readonly size: number;
/** Load cache from a PKIX file (clears JS-level cache) */
load(filepath: string): void;
/** Clear native and JS-level cache contents */
reset(): void;
/** Save cache to a PKIX file */
save(filepath: string): void;
/** Pre-allocate native capacity */
Expand Down
5 changes: 5 additions & 0 deletions bindings/src/pubkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export const pubkeyCache = {
native.load(filepath);
},

reset() {
pkCache.clear();
native.reset();
},

save(filepath) {
native.save(filepath);
},
Expand Down
12 changes: 12 additions & 0 deletions bindings/test/pubkeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,16 @@ describe("pubkeys", () => {
const after = pubkeyCache.get(0);
expect(before).not.toBe(after);
});

it("reset clears native and JS-level cache", () => {
const before = pubkeyCache.get(0);
expect(before).toBeDefined();
expect(pubkeyCache.getIndex(keypairs[0].pubkeyBytes)).toBeDefined();

pubkeyCache.reset();

expect(pubkeyCache.size).toBe(0);
expect(pubkeyCache.get(0)).toBeUndefined();
expect(pubkeyCache.getIndex(keypairs[0].pubkeyBytes)).toBeNull();
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chainsafe/lodestar-z",
"version": "0.1.2-rc.9",
"version": "0.1.2-rc.10",
"description": "Lodestar-z NAPI bindings",
"files": [
"bindings/src/",
Expand Down
Loading