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
7 changes: 7 additions & 0 deletions bindings/napi/BeaconStateView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,13 @@ pub fn getAllValidators(self: *const BeaconStateView) !js.Array {
return js_types.wrap(js.Array, result);
}

/// Get the number of builders in the registry (Gloas+). Throws on pre-Gloas states.
pub fn getBuildersLength(self: *const BeaconStateView) !js.Number {
const cached_state = try self.requireState();
const count = try cached_state.state.buildersLength();
return js.Number.from(count);
}

/// Get all balances in the registry.
pub fn getAllBalances(self: *const BeaconStateView) !js.Array {
const env = js.env();
Expand Down
1 change: 1 addition & 0 deletions bindings/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ export declare class BeaconStateView {
getBalance(index: number): number;
getValidator(index: number): Validator;
getAllValidators(): Validator[];
getBuildersLength(): number;
getAllBalances(): number[];
getValidatorsByStatus(statuses: Set<string>, currentEpoch: number): Validator[];
// TODO wrong function
Expand Down
6 changes: 6 additions & 0 deletions bindings/test/beaconStateView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@ describe("BeaconStateView", () => {
});
});

describe("gloas+ fields", () => {
it("getBuildersLength should throw on a pre-Gloas state", () => {
expect(() => state.getBuildersLength()).toThrow();
});
});

describe("block and state roots", () => {
it("getBlockRoot should return 32 bytes", () => {
const blockRoot = state.getBlockRoot(state.epoch - 1);
Expand Down
10 changes: 10 additions & 0 deletions src/fork_types/any_beacon_state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ pub const AnyBeaconState = union(ForkSeq) {
try self.setEth1DepositIndex(try self.eth1DepositIndex() + 1);
}

pub fn buildersLength(self: *AnyBeaconState) !usize {
return switch (self.*) {
.phase0, .altair, .bellatrix, .capella, .deneb, .electra, .fulu => error.InvalidAtFork,
inline else => |state| {
var builders_view = try state.getReadonly("builders");
return builders_view.length();
},
};
}

pub fn validators(self: *AnyBeaconState) !*ct.phase0.Validators.TreeView {
return switch (self.*) {
inline else => |state| try state.get("validators"),
Expand Down
Loading