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
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 js.Uint8Array.from(pubkey_bytes[0..]);
}

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

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

getPubkeyBytesOrThrow(index) {
const pubkey = native.getPubkeyBytes(index);
if (pubkey === undefined) {
throw new Error(`pubkeyCache: index ${index} not found`);
}

return pubkey;
},
Comment on lines +32 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, but if we can add a test specific for failure case of this function that will be very helpful.


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