Skip to content

test: unit tests for state migrations#6486

Merged
hanabi1224 merged 11 commits intomainfrom
hm/ut-state-migration
Jan 28, 2026
Merged

test: unit tests for state migrations#6486
hanabi1224 merged 11 commits intomainfrom
hm/ut-state-migration

Conversation

@hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Jan 27, 2026

Summary of changes

Changes introduced in this pull request:

  • unit tests for state migrations

Reference issue to close (if applicable)

Closes

Other information and links

Change checklist

  • I have performed a self-review of my own code,
  • I have made corresponding changes to the documentation. All new code adheres to the team's documentation standards,
  • I have added tests that prove my fix is effective or that my feature works (if possible),
  • I have made sure the CHANGELOG is up-to-date. All user-facing changes should be reflected in this document.

Summary by CodeRabbit

  • New Features

    • Configurable state lookup policy (Enabled/Disabled) for controlled/faster state & receipt reads.
    • Tipset accessor exposing parent message receipts.
    • Async DB loading with optional actor-bundle support during snapshot generation.
  • Bug Fixes

    • Runtime verifications ensuring computed state and receipt roots match expected values.
  • Refactor

    • Centralized prepare/compute/validate workflow now returns next-tipset and propagates lookup policy across RPC, miner, and gas paths.
  • Tests

    • Updated calibnet/mainnet benchmarks and test flows.
  • Chores / Docs

    • Renamed fetch command to fetch-test-snapshots; CI/workflow updated.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 27, 2026

Walkthrough

Threads a new StateLookupPolicy through state resolution and execution paths; makes DB loading async with an optional chain/actor-bundle parameter; refactors state compute/validate helpers to return/use a next-tipset for runtime verification; updates call sites, tooling, CI, docs, and tests.

Changes

Cohort / File(s) Summary
State Manager Core
src/state_manager/mod.rs
Adds pub enum StateLookupPolicy { Enabled, Disabled }; tipset_state and tipset_state_output now accept state_lookup: StateLookupPolicy and propagate it through call_with_gas, apply_on_state_with_gas, and related paths.
State Manager Utilities & Tests
src/state_manager/utils.rs
prepare_state_compute now returns (Arc<StateManager<ManyCar>>, ts, ts_next); new prepare_state_validate returns (Arc<StateManager<ManyCar>>, FullTipset); state_compute accepts ts_next and asserts computed state_root/receipt_root against expected values; snapshot caching moved inside functions; tests and benchmarks updated.
Replay / State Compute Flows
src/dev/subcommands/state_cmd.rs, src/state_manager/utils.rs
ReplayCompute/ReplayValidate reworked to use prepare_state_compute/prepare_state_validate and shared state_compute/validate_tipset helpers; removes ad-hoc in-memory/car DB creation and manual CAR snapshot handling.
RPC / Gas / Miner Call Sites
src/rpc/methods/eth.rs, src/rpc/methods/gas.rs, src/rpc/methods/miner.rs
Import StateLookupPolicy; replace calls to tipset_state/call_with_gas/apply_on_state_with_gas to pass explicit policy (generally Enabled for RPC/gas reads, Disabled for miner/validation parent lookups).
Chain Sync Validation
src/chain_sync/tipset_syncer.rs
validate_block and check_block_messages now call tipset_state(..., StateLookupPolicy::Disabled) to disable next-tipset fast-path during sync validations.
Dev Subcommands / Test Snapshot Tooling
src/dev/subcommands/mod.rs, docs/docs/users/reference/cli.sh, .github/workflows/*
Renames FetchRpcTestsFetchTestSnapshots { actor_bundle: Option<PathBuf> }; adds fetch_test_snapshots(actor_bundle) wrapper that can pre-generate actor bundles; CI/workflows and docs updated to new command and env var.
Tooling / DB Loader
src/tool/subcommands/api_cmd.rs, src/tool/subcommands/api_cmd/generate_test_snapshot.rs
load_db becomes async pub async fn load_db(db_root: &Path, chain: Option<&NetworkChain>) and callers now await it; conditional actor-bundle loading when chain provided.
Tipset API
src/blocks/tipset.rs
Adds pub fn parent_message_receipts(&self) -> &Cid accessor.
Benchmarks
src/benchmark_private/tipset_validation.rs
Benchmarks updated to obtain and pass ts_next into state_compute; epoch constants adjusted.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Caller (RPC / Dev / Sync)
    participant Validator as Validation / Command Layer
    participant StateMgr as State Manager
    participant Snapshot as Snapshot / ActorBundle Loader

    Caller->>Validator: request state/read/validation (tipset)
    Validator->>StateMgr: tipset_state(tipset, state_lookup)
    alt StateLookupPolicy::Enabled
        StateMgr->>StateMgr: attempt fast-path using next-tipset (ts_next)
        StateMgr-->>Validator: return state_root / receipts from next-tipset
    else StateLookupPolicy::Disabled
        StateMgr->>Snapshot: ensure snapshots / actor bundles loaded
        Snapshot-->>StateMgr: snapshots and bundles ready
        StateMgr->>StateMgr: compute state for tipset (apply messages, use ts and ts_next)
        StateMgr-->>Validator: return computed state_root / receipts
    end
    Validator-->>Caller: result (state / validation)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

Suggested Reviewers

  • LesnyRumcajs
  • akaladarshi
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'test: unit tests for state migrations' accurately reflects the primary objective of adding unit tests for state migrations, as stated in the PR description and commit message.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

@hanabi1224 hanabi1224 marked this pull request as ready for review January 27, 2026 14:16
@hanabi1224 hanabi1224 requested a review from a team as a code owner January 27, 2026 14:16
@hanabi1224 hanabi1224 requested review from LesnyRumcajs and akaladarshi and removed request for a team January 27, 2026 14:16
@LesnyRumcajs
Copy link
Member

no green checkmark, no review!

@hanabi1224 hanabi1224 force-pushed the hm/ut-state-migration branch from 6344888 to bb4f6a8 Compare January 27, 2026 15:14
@hanabi1224 hanabi1224 force-pushed the hm/ut-state-migration branch from bb4f6a8 to fecb541 Compare January 27, 2026 15:19
@codecov
Copy link

codecov bot commented Jan 27, 2026

Codecov Report

❌ Patch coverage is 67.66169% with 65 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.75%. Comparing base (7f2f0db) to head (d63915b).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/dev/subcommands/state_cmd.rs 0.00% 31 Missing ⚠️
src/dev/subcommands/mod.rs 0.00% 9 Missing ⚠️
...tool/subcommands/api_cmd/generate_test_snapshot.rs 0.00% 7 Missing ⚠️
src/rpc/methods/eth.rs 72.22% 1 Missing and 4 partials ⚠️
src/state_manager/utils.rs 95.45% 0 Missing and 5 partials ⚠️
src/rpc/methods/miner.rs 0.00% 4 Missing ⚠️
src/state_manager/mod.rs 80.00% 1 Missing and 2 partials ⚠️
src/tool/subcommands/api_cmd.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/blocks/tipset.rs 88.05% <100.00%> (+0.07%) ⬆️
src/chain_sync/tipset_syncer.rs 62.00% <100.00%> (ø)
src/rpc/methods/gas.rs 86.84% <100.00%> (+0.03%) ⬆️
src/tool/subcommands/api_cmd.rs 0.00% <0.00%> (ø)
src/state_manager/mod.rs 69.55% <80.00%> (+0.48%) ⬆️
src/rpc/methods/miner.rs 7.29% <0.00%> (-0.17%) ⬇️
src/rpc/methods/eth.rs 68.96% <72.22%> (+0.11%) ⬆️
src/state_manager/utils.rs 82.12% <95.45%> (+2.49%) ⬆️
...tool/subcommands/api_cmd/generate_test_snapshot.rs 0.00% <0.00%> (ø)
src/dev/subcommands/mod.rs 31.37% <0.00%> (-5.00%) ⬇️
... and 1 more

... and 56 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f2f0db...d63915b. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@src/dev/subcommands/state_cmd.rs`:
- Around line 125-134: The second anyhow::ensure! currently uses the wrong error
text ("state root mismatch") for the receipt check; update the ensure! that
compares receipt_root to *ts_next.parent_message_receipts() so its error message
reads something like "receipt root mismatch, receipt_root: {receipt_root},
expected_receipt_root: {expected_receipt_root}" to accurately reflect the
compared variables (receipt_root and expected_receipt_root).
- Around line 84-107: After calling chain_store.chain_index().tipset_by_height
for ts and ts_next, add an explicit guard that detects when ts_next resolves to
the same tipset as ts (compare ts_next.key() to ts.key() or equivalent equality
on the loaded Tipset) and return a clear, descriptive error (or early return)
instead of proceeding; locate the calls to tipset_by_height and the later usage
of ts_next (symbols: tipset_by_height, ts_next, ts,
chain_store.heaviest_tipset(), ResolveNullTipset::TakeNewer,
SettingsStoreExt::write_obj) and ensure you perform the equality check before
writing HEAD_KEY or doing root checks so you avoid misleading mismatch errors
when epoch is already the chain head.

In `@src/state_manager/utils.rs`:
- Around line 299-305: The second anyhow::ensure call currently uses the wrong
error text ("state root mismatch") while checking receipt_root vs
expected_receipt_root; update the error message in the ensure that references
receipt_root and expected_receipt_root to say "receipt root mismatch" (or
similar) and include both receipt_root and expected_receipt_root in the
formatted message to match the variables being validated.
🧹 Nitpick comments (1)
src/state_manager/utils.rs (1)

204-277: Add rustdoc for the public helpers in state_compute.
These are public APIs and should be documented for intent and usage.

As per coding guidelines, document all public functions and structs with rustdoc comments.

Also applies to: 279-283

@hanabi1224
Copy link
Contributor Author

no green checkmark, no review!

@LesnyRumcajs all green now!

@hanabi1224 hanabi1224 enabled auto-merge January 28, 2026 15:07
@hanabi1224 hanabi1224 added this pull request to the merge queue Jan 28, 2026
Merged via the queue into main with commit 323e119 Jan 28, 2026
45 of 46 checks passed
@hanabi1224 hanabi1224 deleted the hm/ut-state-migration branch January 28, 2026 15:49
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

Comments