Skip to content

test: unit test for validate_tipset#6471

Merged
hanabi1224 merged 2 commits intomainfrom
hm/ut-validate-tipset
Jan 23, 2026
Merged

test: unit test for validate_tipset#6471
hanabi1224 merged 2 commits intomainfrom
hm/ut-validate-tipset

Conversation

@hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Jan 23, 2026

Summary of changes

(Part of codecov improvement effort)

Changes introduced in this pull request:

  • unit test for validate_tipset
  • move forest-tool state compute to forest-dev
  • add forest-dev state validate

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

    • Added dev CLI state subcommands: Compute, ReplayCompute, Validate, ReplayValidate for computing/validating chain state and replaying snapshots.
  • Documentation

    • Reorganized CLI docs: removed legacy forest-tool sections and introduced a forest-dev context with state-related docs.
  • Refactor

    • Simplified state validation and snapshot handling for more consistent workflows.
  • Chores

    • Removed legacy state_compute CLI command from the main tool.
  • Tests

    • Added tests and helpers for state compute/validate snapshot flows.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 23, 2026

Walkthrough

Adds a developer-focused state CLI under forest-dev (compute/replay/validate), removes those commands from forest-tool, refactors tipset validation to derive ChainStore/genesis from StateManager, parameterizes state snapshot helpers, and adds pause/resume read-ops tracking.

Changes

Cohort / File(s) Summary
Docs
docs/docs/users/reference/cli.sh
Removed forest-tool state docs and added forest-dev state sections (fetch-rpc-tests, state, state compute, state replay-compute, state validate, state replay-validate).
forest-dev CLI (new)
src/dev/subcommands/mod.rs, src/dev/subcommands/state_cmd.rs
Added State subcommand and state_cmd.rs implementing Compute / ReplayCompute / Validate / ReplayValidate flows (ChainStore/StateManager usage, tipset load, compute/validate, CAR export, replay loops).
forest-tool CLI removals & visibility
src/tool/subcommands/mod.rs, src/tool/subcommands/state_compute_cmd.rs, src/tool/main.rs
Removed state_compute_cmd module and State variant/dispatch from forest-tool; deleted command implementation; adjusted api_cmd visibility to pub(crate).
Chain sync: validation refactor
src/chain_sync/mod.rs, src/chain_sync/chain_follower.rs, src/chain_sync/tipset_syncer.rs
tipset_syncer made pub(crate); validate_tipset signature changed to remove chainstore/genesis params; callers now use state_manager.chain_store() and derive genesis from it; minor logging and call-site updates.
State manager utils
src/state_manager/utils.rs
Added parameterized get_state_snapshot(chain, bucket, epoch) plus get_state_compute_snapshot and test-only get_state_validate_snapshot / prepare_state_validate helpers; updated tests to use new helpers.
Read-ops tracking control
src/tool/subcommands/api_cmd/generate_test_snapshot.rs
Added AtomicBool tracking to ReadOpsTrackingStore<T> with resume_tracking()/pause_tracking() and gated tracking writes across blockstore/settings/bitswap/eth-mapping read paths.
Misc / Changelog
CHANGELOG.md
Added entry noting move of forest-tool state subcommand to forest-dev.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant CLI as CLI (forest-dev)
participant StateMgr as StateManager
participant ChainStore as ChainStore
participant Tipset as Tipset Loader
participant Export as CAR Export
CLI->>StateMgr: init (db path or snapshot)
StateMgr->>ChainStore: chain_store() / genesis access
CLI->>Tipset: request target tipset (epoch or snapshot)
Tipset-->>StateMgr: provide FullTipset
StateMgr->>StateMgr: compute or validate state
StateMgr->>Export: optionally export DB snapshot / CAR
Export-->>CLI: write snapshot / print results

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~35 minutes

Possibly related PRs

Suggested reviewers

  • akaladarshi
  • LesnyRumcajs
🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title claims to add a unit test for validate_tipset, but the changeset shows a much broader refactoring including moving commands between CLI tools and refactoring validate_tipset's signature. Update the title to reflect the primary changes, such as 'refactor: move forest-tool state commands to forest-dev and simplify validate_tipset signature' or similar.
Docstring Coverage ⚠️ Warning Docstring coverage is 27.59% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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


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

@hanabi1224 hanabi1224 marked this pull request as ready for review January 23, 2026 10:45
@hanabi1224 hanabi1224 requested a review from a team as a code owner January 23, 2026 10:45
@hanabi1224 hanabi1224 requested review from LesnyRumcajs and akaladarshi and removed request for a team January 23, 2026 10:45
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: 2

🤖 Fix all issues with AI agents
In `@src/dev/subcommands/state_cmd.rs`:
- Around line 256-258: Update the doc comment on the ReplayValidateCommand's
snapshot field to correctly reference the producing command; replace "generated
by `forest-tool state compute`" with the correct generator "produced by
`forest-dev state validate`" (or an accurate description like "snapshot produced
by the forest-dev state validate command") so the comment on the snapshot:
PathBuf field correctly documents expected input for ReplayValidateCommand.
- Around line 127-129: Update the doc comment on the snapshot field of the
ReplayComputeCommand struct to reference the new command name; change the text
that currently says "forest-tool state compute" to "forest-dev state compute"
(or the correct new command name) so the doc comment for
ReplayComputeCommand::snapshot accurately reflects the moved compute command.
🧹 Nitpick comments (2)
src/state_manager/utils.rs (1)

213-231: Add rustdoc for the new public snapshot helpers.

These new pub functions should have brief /// docs describing purpose, inputs, and outputs to satisfy the public-API documentation rule. As per coding guidelines, document all public functions and structs with rustdoc comments.

Also applies to: 253-260

src/dev/subcommands/state_cmd.rs (1)

300-301: Consider adding a safety comment for the unsafe block.

The use of unsafe { std::env::set_var(...) } is necessary since Rust 1.80+ deprecated safe set_var due to thread-safety concerns. However, a brief safety comment explaining why this is acceptable here would improve maintainability.

💡 Suggested improvement
 fn disable_tipset_cache() {
+    // SAFETY: This is called at the start of single-threaded CLI commands before
+    // any concurrent work begins, so there are no data races with other threads
+    // reading environment variables.
     unsafe { std::env::set_var("FOREST_TIPSET_CACHE_DISABLED", "1") };
 }

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: 1

🤖 Fix all issues with AI agents
In `@src/dev/subcommands/state_cmd.rs`:
- Around line 300-302: The unsafe call to std::env::set_var inside
disable_tipset_cache needs an explanatory safety comment: describe why using
unsafe here is safe (e.g., this function is invoked only during single-threaded
startup before any threads are spawned, the environment variable is
process-local and race is acceptable in our usage, or we ensure synchronization
elsewhere), and document any preconditions or alternatives (like moving the call
to early initialization or guarding with a Once). Add this comment immediately
above or inside the unsafe block in disable_tipset_cache so reviewers can verify
the assumptions and consider using synchronization (std::sync::Once) if those
preconditions cannot be guaranteed.
🧹 Nitpick comments (4)
src/tool/subcommands/api_cmd/generate_test_snapshot.rs (1)

161-173: Missing rustdoc comments for public methods.

The resume_tracking and pause_tracking methods are public but lack documentation. As per coding guidelines, all public functions should have rustdoc comments explaining their purpose.

📝 Suggested documentation
 impl<T> ReadOpsTrackingStore<T> {
+    /// Resumes recording of read operations to the in-memory tracker.
+    ///
+    /// After calling this method, subsequent read operations will be
+    /// written to the tracker for snapshot export.
     pub fn resume_tracking(&self) {
         self.tracking.store(true, Ordering::Relaxed);
     }

+    /// Pauses recording of read operations to the in-memory tracker.
+    ///
+    /// While paused, read operations will still be performed on the
+    /// inner store but will not be recorded for snapshot export.
     pub fn pause_tracking(&self) {
         self.tracking.store(false, Ordering::Relaxed);
     }

     fn tracking(&self) -> bool {
         self.tracking.load(Ordering::Relaxed)
     }
 }
src/state_manager/utils.rs (1)

228-252: Consider adding validation for the bucket parameter.

The bucket parameter is interpolated directly into the URL without validation. While currently only called with known values ("state_compute", "state_validate"), a malformed bucket string could produce invalid URLs.

💡 Optional: Add bucket validation
 pub async fn get_state_snapshot(
     chain: &NetworkChain,
     bucket: &str,
     epoch: i64,
 ) -> anyhow::Result<PathBuf> {
+    anyhow::ensure!(
+        bucket.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'),
+        "Invalid bucket name: {bucket}"
+    );
     let url = Url::parse(&format!(
         "https://forest-snapshots.fra1.cdn.digitaloceanspaces.com/{bucket}/{chain}_{epoch}.forest.car.zst"
     ))?;
src/dev/subcommands/state_cmd.rs (2)

61-121: Significant code duplication between ComputeCommand::run and ValidateCommand::run.

Both methods share nearly identical setup logic (lines 69-102 vs 204-238):

  • Disable tipset cache
  • Resolve DB root path
  • Load DB and chain config
  • Create genesis header
  • Create chain store
  • Pause/resume tracking and load tipset
  • Export snapshot

Consider extracting shared logic into a helper function.

♻️ Suggested refactor approach

Extract common setup into a helper:

struct StateCommandSetup {
    db: Arc<ReadOpsTrackingStore<ManyCar<ParityDb>>>,
    chain_store: Arc<ChainStore<...>>,
    ts: Tipset,
}

async fn setup_state_command(
    epoch: ChainEpoch,
    chain: &NetworkChain,
    db_path: Option<PathBuf>,
) -> anyhow::Result<StateCommandSetup> {
    disable_tipset_cache();
    let db_root_path = if let Some(db) = db_path {
        db
    } else {
        let (_, config) = read_config(None, Some(chain.clone()))?;
        db_root(&chain_path(&config))?
    };
    // ... shared setup logic
}

Also applies to: 196-250


138-176: Similar duplication pattern in replay commands.

ReplayComputeCommand::run and ReplayValidateCommand::run also share substantial setup logic for loading snapshots and initializing the chain store. The same refactoring opportunity applies here.

Also applies to: 267-297

@codecov
Copy link

codecov bot commented Jan 23, 2026

Codecov Report

❌ Patch coverage is 14.52991% with 200 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.66%. Comparing base (26757f5) to head (fbf9dd2).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/dev/subcommands/state_cmd.rs 0.00% 165 Missing ⚠️
...tool/subcommands/api_cmd/generate_test_snapshot.rs 0.00% 23 Missing ⚠️
src/chain_sync/chain_follower.rs 0.00% 9 Missing ⚠️
src/state_manager/utils.rs 93.33% 0 Missing and 2 partials ⚠️
src/dev/subcommands/mod.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/chain_sync/tipset_syncer.rs 62.00% <100.00%> (+62.00%) ⬆️
src/tool/main.rs 59.09% <ø> (+2.56%) ⬆️
src/tool/subcommands/api_cmd.rs 0.00% <ø> (ø)
src/dev/subcommands/mod.rs 36.36% <0.00%> (-0.85%) ⬇️
src/state_manager/utils.rs 79.63% <93.33%> (+0.98%) ⬆️
src/chain_sync/chain_follower.rs 39.66% <0.00%> (+0.47%) ⬆️
...tool/subcommands/api_cmd/generate_test_snapshot.rs 0.00% <0.00%> (ø)
src/dev/subcommands/state_cmd.rs 0.00% <0.00%> (ø)

... and 27 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 26757f5...fbf9dd2. 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
Member

@LesnyRumcajs LesnyRumcajs left a comment

Choose a reason for hiding this comment

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

LGTM. Let's have a changelog entry for this breaking change. I don't think anyone was actively using forest-tool state compute but in case they were, it'd be helpful to put an info.

@hanabi1224
Copy link
Contributor Author

LGTM. Let's have a changelog entry for this breaking change. I don't think anyone was actively using forest-tool state compute but in case they were, it'd be helpful to put an info.

@LesnyRumcajs changelog added.

@hanabi1224 hanabi1224 enabled auto-merge January 23, 2026 12:17
@hanabi1224 hanabi1224 added this pull request to the merge queue Jan 23, 2026
Merged via the queue into main with commit 6b48085 Jan 23, 2026
45 checks passed
@hanabi1224 hanabi1224 deleted the hm/ut-validate-tipset branch January 23, 2026 13:37
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