Skip to content

Commit aef4420

Browse files
refactor: make XXXDecisionRoot fns return js.String (#342)
extracted from #347 `IBeaconStateView` expects `RootHex` (66-char string) for these outputs. it also happens that it's cheaper to just create a string upfront than go through a typed array see: https://github.com/ChainSafe/lodestar/blob/35940ffd61ad7e29f5de376e13587d044b27b246/packages/state-transition/src/stateView/interface.ts#L78-L82
1 parent d8b9c06 commit aef4420

3 files changed

Lines changed: 23 additions & 20 deletions

File tree

bindings/napi/BeaconStateView.zig

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,37 +371,39 @@ pub fn proposerLookahead(self: *const BeaconStateView) !js.Uint32Array {
371371

372372
// pub fn BeaconStateView_getShufflingAtEpoch
373373

374-
pub fn previousDecisionRoot(self: *const BeaconStateView) !js.Uint8Array {
374+
fn rootToHexString(root: *const [32]u8) !js.String {
375375
const env = js.env();
376+
var hex_buf: [66]u8 = undefined;
377+
try @import("hex").rootIntoHex(&hex_buf, root);
378+
return js_types.wrap(js.String, try env.createStringUtf8(&hex_buf));
379+
}
380+
381+
pub fn previousDecisionRoot(self: *const BeaconStateView) !js.String {
376382
const cached_state = try self.requireState();
377383
const root = cached_state.previousDecisionRoot();
378-
return js_types.wrap(js.Uint8Array, try sszValueToNapiValue(env, ct.primitive.Root, &root));
384+
return rootToHexString(&root);
379385
}
380386

381-
pub fn currentDecisionRoot(self: *const BeaconStateView) !js.Uint8Array {
382-
const env = js.env();
387+
pub fn currentDecisionRoot(self: *const BeaconStateView) !js.String {
383388
const cached_state = try self.requireState();
384389
const root = cached_state.currentDecisionRoot();
385-
return js_types.wrap(js.Uint8Array, try sszValueToNapiValue(env, ct.primitive.Root, &root));
390+
return rootToHexString(&root);
386391
}
387392

388393
/// Get the next decision root for the state.
389-
pub fn nextDecisionRoot(self: *const BeaconStateView) !js.Uint8Array {
390-
const env = js.env();
394+
pub fn nextDecisionRoot(self: *const BeaconStateView) !js.String {
391395
const cached_state = try self.requireState();
392396
const root = cached_state.nextDecisionRoot();
393-
return js_types.wrap(js.Uint8Array, try sszValueToNapiValue(env, ct.primitive.Root, &root));
397+
return rootToHexString(&root);
394398
}
395399

396400
/// Get the shuffling decision root for a given epoch.
397-
pub fn getShufflingDecisionRoot(self: *const BeaconStateView, epoch_arg: js.Number) !js.Uint8Array {
398-
const env = js.env();
401+
pub fn getShufflingDecisionRoot(self: *const BeaconStateView, epoch_arg: js.Number) !js.String {
399402
const cached_state = try self.requireState();
400-
const epoch_value: u64 = @intCast(try epoch_arg.toI64());
401-
const root = st.calculateShufflingDecisionRoot(cached_state.state, epoch_value) catch {
402-
return throwNullAs(js.Uint8Array, "STATE_ERROR", "Failed to calculate shuffling decision root");
403+
const root = st.calculateShufflingDecisionRoot(cached_state.state, try epoch_arg.toU32()) catch {
404+
return throwNullAs(js.String, "STATE_ERROR", "Failed to calculate shuffling decision root");
403405
};
404-
return js_types.wrap(js.Uint8Array, try sszValueToNapiValue(env, ct.primitive.Root, &root));
406+
return rootToHexString(&root);
405407
}
406408

407409
pub fn previousProposers(self: *const BeaconStateView) !?js.Array {

bindings/test/beaconStateView.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,15 @@ describe("BeaconStateView", () => {
397397
expect(proposer).toBeLessThan(state.validatorCount);
398398
});
399399

400-
it("decision roots should be 32 bytes each", () => {
401-
expect(state.previousDecisionRoot.length).toBe(32);
402-
expect(state.currentDecisionRoot.length).toBe(32);
403-
expect(state.nextDecisionRoot.length).toBe(32);
400+
it("decision roots should be 66 bytes each", () => {
401+
expect(state.previousDecisionRoot.length).toBe(66);
402+
expect(state.currentDecisionRoot.length).toBe(66);
403+
expect(state.nextDecisionRoot.length).toBe(66);
404404
});
405405

406-
it("getShufflingDecisionRoot should return 32 bytes", () => {
406+
it("getShufflingDecisionRoot should return 66 bytes", () => {
407407
const decisionRoot = state.getShufflingDecisionRoot(state.epoch);
408-
expect(decisionRoot.length).toBe(32);
408+
expect(decisionRoot.length).toBe(66);
409409
});
410410
});
411411

build.zig.zon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
.imports = .{
299299
.bls,
300300
.bls_options,
301+
.hex,
301302
.persistent_merkle_tree,
302303
.ssz,
303304
.consensus_types,

0 commit comments

Comments
 (0)