Skip to content

add /lean/v0/fork_choice endpoint#607

Open
ch4r10t33r wants to merge 8 commits intomainfrom
forkchoice-api
Open

add /lean/v0/fork_choice endpoint#607
ch4r10t33r wants to merge 8 commits intomainfrom
forkchoice-api

Conversation

@ch4r10t33r
Copy link
Contributor

@ch4r10t33r ch4r10t33r commented Feb 26, 2026

Summary

  • Add new JSON endpoint GET /lean/v0/fork_choice that returns the full fork choice state
  • Extends Snapshot struct with additional fields without breaking existing usages
  • Reuses existing ForkChoice.snapshot() method for thread-safe data access

Response format

{
  "head": {"slot": 100, "root": "0x..."},
  "justified": {"slot": 96, "root": "0x..."},
  "finalized": {"slot": 64, "root": "0x..."},
  "safe_target": {"root": "0x..."},
  "validator_count": 4,
  "nodes": [
    {"slot": 64, "root": "0x...", "parent_root": "0x...", "weight": 4},
    ...
  ]
}

Implements: leanEthereum/leanSpec#418

Test plan

  • Build succeeds (zig build)
  • Manual test: curl http://localhost:8084/lean/v0/fork_choice
  • Verify JSON response contains expected fields

Add new JSON endpoint that returns the full fork choice state including:
- Head block (slot and root)
- Justified checkpoint (slot and root)
- Finalized checkpoint (slot and root)
- Safe target root
- Validator count
- All proto nodes in the fork choice tree

Reuses existing ForkChoice.snapshot() method for thread-safe data access.
Extends Snapshot struct with additional fields (slots, safe_target, validator_count)
without breaking existing usages.
Zig's package manager can fail with HttpConnectionClosing when fetching
transitive dependencies (e.g., bitcoin-core/secp256k1) from GitHub.
This is a transient network issue, not a code problem.

Add retry logic to build steps (not just fetch) since Zig may lazily
fetch dependencies during build that weren't fetched during --fetch.
@ch4r10t33r ch4r10t33r marked this pull request as ready for review February 26, 2026 09:36
@ch4r10t33r ch4r10t33r requested a review from g11tech February 26, 2026 09:37
Fork choice rejects blocks whose slot is ahead of its clock (FutureSlot).
The test calls produceBlock(slot=4) without ticking the clock; advance
to slot 4 via onInterval before produceBlock so the block is accepted.
Reduce number of write calls by combining all top-level JSON fields
into a single print statement for better efficiency.
@gballet gballet changed the title feat(api): add /lean/v0/fork_choice endpoint add /lean/v0/fork_choice endpoint Feb 27, 2026
@gballet gballet added the enhancement New feature or request label Feb 27, 2026
pub const Snapshot = struct {
head: ProtoNode,
latest_justified_root: [32]u8,
latest_justified_slot: types.Slot,
Copy link
Member

Choose a reason for hiding this comment

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

@zclawz do a PR to refctor latest_justified_slot, latest_justified_root as a combined checkpoint object. similarly for latest_finalized_root, latest_finalized_slot

@ch4r10t33r ch4r10t33r requested a review from g11tech March 4, 2026 10:00
zclawz pushed a commit that referenced this pull request Mar 6, 2026
…rkChoice Snapshot

Replace separate latest_justified_root/latest_finalized_root [32]u8 fields in
ForkChoice.Snapshot with unified types.Checkpoint objects (latest_justified /
latest_finalized), mirroring the ForkChoiceStore fields they are derived from.

- forkchoice.zig: Snapshot struct updated; construction sites use fcStore checkpoints directly
- tree_visualizer.zig: updated callers to use .latest_justified.root / .latest_finalized.root

Requested by g11tech in PR #607 review comment (pkgs/node/src/forkchoice.zig).
zclawz pushed a commit that referenced this pull request Mar 6, 2026
…rkChoice Snapshot

Replace 4 separate latest_justified_root/slot, latest_finalized_root/slot fields in
ForkChoice.Snapshot with unified types.Checkpoint objects (latest_justified /
latest_finalized), mirroring the ForkChoiceStore fields they are derived from.

- forkchoice.zig: Snapshot struct updated; construction sites use fcStore checkpoints directly
- tree_visualizer.zig: updated all callers to use .latest_justified/.latest_finalized checkpoint fields

Requested by g11tech in PR #607 review comment (pkgs/node/src/forkchoice.zig).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants