Skip to content

refactor: order Checkpoint by slot to drop max() lambda noise#684

Merged
tcoratger merged 1 commit intoleanEthereum:mainfrom
tcoratger:refactor/checkpoint-ordering
Apr 26, 2026
Merged

refactor: order Checkpoint by slot to drop max() lambda noise#684
tcoratger merged 1 commit intoleanEthereum:mainfrom
tcoratger:refactor/checkpoint-ordering

Conversation

@tcoratger
Copy link
Copy Markdown
Collaborator

Summary

Adds Checkpoint.__lt__ ordered by slot so the four max(...) calls in forkchoice/store.py can drop their key=lambda c: c.slot argument.

Tie-break semantics preserved

Equal-slot checkpoints are intentionally incomparable (a < b and b < a both return False). Python's max() keeps its first argument when neither is strictly less, which matches what each call site already relied on:

  • on_block: max(self.X, post_state.X) — store wins ties (the documented "anchor-pinned, authoritative on ties" invariant).
  • produce_block_with_signatures: max(final_post_state.X, store.X) — post-state wins ties (existing behavior; not changed in this PR).

No behavioral change at any call site. Just the lambda noise removed.

Tests

tests/lean_spec/subspecs/containers/test_checkpoint.py — 8 tests, no duplicate scenarios:

  • Strict-less branch returns True for lower slot
  • Strict-less branch returns False for higher slot
  • Equal slots with different roots are incomparable both directions (verifies root is ignored)
  • Anti-reflexivity: a checkpoint is not less than itself
  • Direct dunder call returns the NotImplemented sentinel for foreign types
  • < operator raises TypeError after Python's reflected fallback fails
  • max() picks the higher-slot checkpoint regardless of argument order
  • max() keeps the first argument on slot ties (the property the forkchoice store relies on)

Test plan

  • uvx tox -e all-checks passes (ruff, format, ty, codespell, mdformat).
  • uv run pytest tests/lean_spec/subspecs/containers/test_checkpoint.py — 8 passed.
  • Reviewer to spot-check tie-break preservation at each call site.

🤖 Generated with Claude Code

Add Checkpoint.__lt__ ordered by slot so the four max(...) calls in the
forkchoice store can drop their `key=lambda c: c.slot` argument. Tie-break
behavior is preserved: equal-slot checkpoints are incomparable, so max()
keeps its first argument — same semantics each call site already relied on.

Adds unit tests covering strict/reverse/equal ordering, anti-reflexivity,
the NotImplemented sentinel for foreign types, the resulting TypeError on
the < operator, and the two max() integration cases (different slots,
equal slots).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@tcoratger tcoratger merged commit 16d1374 into leanEthereum:main Apr 26, 2026
13 checks passed
tcoratger added a commit to tcoratger/leanSpec that referenced this pull request Apr 27, 2026
Absorbs 14 commits from main and resolves two textual conflicts in
genesis/config.py and the matching test (PR leanEthereum#683 deleted the dead
GenesisConfig.create_state helper that this branch had refactored to
take a fork argument; production code goes through fork.generate_genesis
directly, so the wrapper is genuinely dead).

Reroutes two stale subspecs.containers imports left behind by the
auto-merge: a new test file from PR leanEthereum#684 (Checkpoint __lt__) and a
sync test fixture from PR leanEthereum#672. Both now resolve through
forks.devnet4.containers.

Tightens the docstring and inline comment introduced for
update_safe_target by PR leanEthereum#680 to follow the project's documentation
rules (short sentences, bullets over paragraphs).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant