Skip to content

Conversation

@madisoncarter1234
Copy link

@madisoncarter1234 madisoncarter1234 commented Dec 13, 2025

Summary

  • Add test for StateNotReachable error in apply_fork_choice covering the real-world pruning scenario
  • Test builds a 130-block canonical chain, creates an early fork, simulates state pruning, then verifies reorg fails with StateNotReachable
  • Added clear_trie_cache_for_testing() helper to Store for simulating pruning in tests (InMemory backend uses 10k threshold)

Test plan

  • cargo test -p ethrex-blockchain test_state_not_reachable_on_reorg_beyond_pruning_range

Closes #5603

Add a unit test that verifies apply_fork_choice returns StateNotReachable
when attempting to fork choice to a block whose state root is not
available in the trie.

Closes lambdaclass#5603
@madisoncarter1234 madisoncarter1234 requested a review from a team as a code owner December 13, 2025 16:08
Comment on lines 323 to 329
let result = apply_fork_choice(&store, block_hash, genesis_hash, genesis_hash).await;

assert!(
matches!(result, Err(InvalidForkChoice::StateNotReachable)),
"Expected StateNotReachable, got {:?}",
result
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This case isn't that useful. On normal operation, if we have the state for a block, and we receive a child, we'll only insert the header into the database if we executed the block (we have the state), so this can't happen.

The issue refers to cases where we have a reorg, and the nearest chain link is beyond our pruning range (128 blocks, currently).

Copy link
Author

Choose a reason for hiding this comment

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

understood, rewrote the test to cover the actual reorg scenario you described. builds 130 blocks on canonical, forks early, then simulates the pruning by clearing the trie cache before attempting the reorg.

Had to add a test helper (clear_trie_cache_for_testing) to Store since the InMemory backend threshold is 10k and won't prune naturally in tests.

The previous test created an artificial scenario (fake state_root) that
can't occur in production. The new test simulates the actual case:

- Build canonical chain beyond 128-block pruning threshold
- Create fork early in chain
- Common ancestor's state gets pruned
- Reorg to fork fails with StateNotReachable

Added clear_trie_cache_for_testing() helper to Store.
Copy link
Contributor

@Peponks9 Peponks9 left a comment

Choose a reason for hiding this comment

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

hey @madisoncarter1234 looks like the lint check failing causes the other two tests to fail.

Copy link
Contributor

@Peponks9 Peponks9 left a comment

Choose a reason for hiding this comment

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

is this being blocked by another task?

@madisoncarter1234
Copy link
Author

is this being blocked by another task?

just waiting on approvals, ready for review

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.

Add test for newly added FCU cases

3 participants