Skip to content

feat(napi): add latestBlockHash and payloadBlockNumber accessors#297

Open
lodekeeper-z wants to merge 1 commit intoChainSafe:mainfrom
lodekeeper-z:feat/napi-trivial-accessors
Open

feat(napi): add latestBlockHash and payloadBlockNumber accessors#297
lodekeeper-z wants to merge 1 commit intoChainSafe:mainfrom
lodekeeper-z:feat/napi-trivial-accessors

Conversation

@lodekeeper-z
Copy link
Copy Markdown
Contributor

Summary

Close 2 of the remaining gaps between our NAPI bindings and the upstream IBeaconStateView interface (lodestar PR #9067).

New methods

Method Type Description
latestBlockHash getter → Uint8Array Block hash from latest execution payload header
payloadBlockNumber getter → number Block number from latest execution payload header

Both throw INVALID_FORK for pre-bellatrix states, matching the upstream interface semantics.

Changes

  • bindings/napi/BeaconStateView.zig: Two new NAPI getter functions
  • src/fork_types/any_beacon_state.zig: latestExecutionPayloadHeaderBlockNumber accessor
  • bindings/src/index.d.ts: TypeScript type declarations

Context

With this PR + PR #276, only 3 non-Gloas methods remain unimplemented: getValidatorsByStatus, toValue, and computeAttestationsRewards (naming mismatch).

🤖 Generated with AI assistance

Add two missing IBeaconStateView methods to NAPI bindings:
- latestBlockHash: returns block_hash from latest execution payload header
- payloadBlockNumber: returns block_number from latest execution payload header

Both throw INVALID_FORK for pre-bellatrix states.

Also adds latestExecutionPayloadHeaderBlockNumber to AnyBeaconState
as the underlying accessor.

🤖 Generated with AI assistance
@lodekeeper-z lodekeeper-z requested a review from a team as a code owner March 29, 2026 13:14
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request bridges gaps between the existing NAPI bindings and the upstream IBeaconStateView interface by exposing block hash and block number information from the latest execution payload header. These changes ensure that the NAPI layer correctly handles fork-specific constraints, providing a more complete and robust API surface for consumers.

Highlights

  • New NAPI Accessors: Added latestBlockHash and payloadBlockNumber getters to the BeaconStateView NAPI bindings.
  • Fork Safety: Implemented INVALID_FORK error handling for pre-bellatrix states to ensure consistency with the upstream interface.
  • Type Definitions: Updated bindings/src/index.d.ts to include the new accessors for TypeScript support.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements getters for the latest execution payload header's block hash and block number across BeaconStateView and AnyBeaconState, including the necessary N-API bindings and TypeScript definitions. A review comment points out that the new function in any_beacon_state.zig violates the repository's style guide by failing to meet the required minimum assertion density for verifying invariants and preconditions.

Comment on lines +643 to +651
pub fn latestExecutionPayloadHeaderBlockNumber(self: *AnyBeaconState) !u64 {
return switch (self.*) {
.phase0, .altair => error.InvalidAtFork,
inline else => |state| {
var header = try state.get("latest_execution_payload_header");
return try header.get("block_number");
},
};
}
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.

medium

This function is missing assertions, which is a violation of the repository's style guide. The style guide requires an average of two assertions per function to ensure code safety and document invariants (lines 51-55).

Consider adding assertions for preconditions, such as checking that self is not null, and for critical invariants. For example, you could assert that the fork is indeed Bellatrix or newer before accessing fork-specific fields. The style guide encourages such "blatantly true assertions" as a form of documentation (line 62).

    pub fn latestExecutionPayloadHeaderBlockNumber(self: *AnyBeaconState) !u64 {
        std.debug.assert(self != null);
        return switch (self.*) {
            .phase0, .altair => error.InvalidAtFork,
            inline else => |state| {
                std.debug.assert(self.forkSeq().gte(.bellatrix));
                var header = try state.get("latest_execution_payload_header");
                return try header.get("block_number");
            },
        };
    }
References
  1. Functions should contain assertions to check for pre/postconditions and invariants. The style guide mandates an average of at least two assertions per function to improve code safety and act as documentation. (link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants