Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions bindings/napi/pubkeys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ pub fn get(index: js.Number) !?blst_bindings.PublicKey {
return .{ .raw = public_key };
}

/// JS: pubkeys.getPubkeyBytes(index) → Uint8Array | undefined
pub fn getPubkeyBytes(index: js.Number) !?js.Uint8Array {
if (!state.initialized) return error.PubkeyIndexNotInitialized;

const idx = try index.toU32();
const io = js.io();
const pubkey_bytes = state.cache.getPubkeyBytes(io, idx) orelse return null;
return try js.Uint8Array.fromExternal(pubkey_bytes[0..]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

Suggested change
return try js.Uint8Array.fromExternal(pubkey_bytes[0..]);
return try js.Uint8Array.from(pubkey_bytes[0..]);

usually at lodestar side, the returned Uint8Array is a short-lived object that gets "gc" before we clone it via bls worker

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 5071c7c

}

/// Aggregate multiple `PublicKey`s by the given
/// validator `indices` into one.
///
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 @@ -5,6 +5,8 @@ export interface PubkeyCache {
get(index: number): PublicKey | undefined;
/** Same as get(), but throws if the index is not in the cache */
getOrThrow(index: number): PublicKey;
/** Get the cached 48-byte compressed pubkey bytes without materializing a PublicKey wrapper. */
getPubkeyBytes(index: number): Uint8Array | undefined;
/** Aggregate cached public keys by validator index */
aggregate(indices: number[]): PublicKey;
/** Get validator index by pubkey bytes */
Expand Down
4 changes: 4 additions & 0 deletions bindings/src/pubkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export const pubkeyCache = {
return pk;
},

getPubkeyBytes(index) {
return native.getPubkeyBytes(index);
},

aggregate(indices) {
if (indices.length === 1) return pubkeyCache.getOrThrow(indices[0]);
return native.aggregate(indices);
Expand Down
16 changes: 16 additions & 0 deletions bindings/test/pubkeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ describe("pubkeys", () => {
expect(pubkeyCache.get(0xffffffff)).toBeUndefined();
});

it("getPubkeyBytes returns the compressed pubkey bytes", () => {
for (const {index, pubkeyBytes} of keypairs) {
expect(pubkeyCache.getPubkeyBytes(index)).toEqual(pubkeyBytes);
}
});

it("getPubkeyBytes matches getOrThrow().toBytes() and returns copies", () => {
const bytes = pubkeyCache.getPubkeyBytes(0);
expect(bytes).toEqual(pubkeyCache.getOrThrow(0).toBytes());
expect(pubkeyCache.getPubkeyBytes(0)).not.toBe(bytes);
});

it("getPubkeyBytes returns undefined for out-of-range index", () => {
expect(pubkeyCache.getPubkeyBytes(0xffffffff)).toBeUndefined();
});

it("getIndex returns null for unknown pubkey", () => {
expect(pubkeyCache.getIndex(new Uint8Array(48))).toBeNull();
});
Expand Down
14 changes: 14 additions & 0 deletions src/state_transition/cache/pubkey_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ pub const PubkeyCache = struct {
return self.entries.values()[@intCast(index)];
}

/// Get the compressed pubkey bytes for a validator index. A value is
/// returned so no pointer into movable map storage escapes the shared
/// lock.
pub fn getPubkeyBytes(
self: *const PubkeyCache,
io: std.Io,
index: u64,
) ?[48]u8 {
self.lockShared(io);
defer self.unlockShared(io);
if (index >= self.entries.count()) return null;
return self.entries.keys()[@intCast(index)];
}

/// Copy affine pubkeys for a batch of validator indices while holding one
/// shared lock. The output is left unchanged when an index is invalid.
pub fn getPubkeys(
Expand Down
Loading