Skip to content

refactor state manager mod into multiple sub mods#6864

Open
akaladarshi wants to merge 2 commits intomainfrom
akaladarshi/refactor-stmgr
Open

refactor state manager mod into multiple sub mods#6864
akaladarshi wants to merge 2 commits intomainfrom
akaladarshi/refactor-stmgr

Conversation

@akaladarshi
Copy link
Copy Markdown
Collaborator

@akaladarshi akaladarshi commented Apr 7, 2026

Summary of changes

Changes introduced in this pull request:

  • Divides the state_manager mod.rs into saperate file so all the modules are self contained
  • This is first step in the larger refactor of the statemanager to allow it use the traits instead of concrete type

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.

Outside contributions

  • I have read and agree to the CONTRIBUTING document.
  • I have read and agree to the AI Policy document. I understand that failure to comply with the guidelines will lead to rejection of the pull request.

Summary by CodeRabbit

Release Notes

  • New Features
    • Added message execution replay and inspection capabilities to inspect historical transaction execution.
    • Added message receipt lookup with confirmation tracking over chain history.
    • Added message simulation utilities to preview transaction execution without modifying state.
    • Added miner eligibility verification and mining base information retrieval.
    • Added on-chain state query methods for market data, account balances, and miner information.
    • Added address resolution and BLS public key retrieval utilities.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 7, 2026

Walkthrough

This PR refactors the StateManager<DB> implementation by extracting methods from mod.rs into seven dedicated submodules: actor_queries, address_resolution, execution, message_search, message_simulation, mining, and state_computation. Approximately 1,800 lines of code are reorganized without functional API changes, and a new public VMFlush enum is introduced.

Changes

Cohort / File(s) Summary
Actor & Mining Query Methods
src/state_manager/actor_queries.rs, src/state_manager/mining.rs
New public methods for retrieving market state/balances, miner info/faults/recoveries/power, verified registry claims/allocations, and verified client datacap; mining eligibility and base info methods for consensus validation.
Address Resolution Helpers
src/state_manager/address_resolution.rs
New public methods for BLS key extraction, ID/key address lookups, and async address-to-key/deterministic address resolution with fallback tipset-state computation.
Message Execution & Simulation
src/state_manager/execution.rs, src/state_manager/message_simulation.rs
New public methods for replaying messages with tracing/bailout, pre-state tracking, tipset validation, and execution-trace collection; message simulation on arbitrary state roots with optional gas metering and state flushing.
Message Search & Confirmation
src/state_manager/message_search.rs
New public methods for receipt lookup with backward search, async wait-for-message with reorg/confidence tracking, and message search from optional tipsets with bounded lookback.
State Computation Pipeline
src/state_manager/state_computation.rs
Core tipset state computation, executed-tipset construction, async/blocking entrypoints with VM callbacks and tracing, tipset validation, and internal TipsetExecutor for parent state preparation (cron, migrations).
Module Reorganization
src/state_manager/mod.rs
Extracted large impl<DB> StateManager<DB> block into submodules; introduced pub enum VMFlush; added restricted-access accessors (engine(), tipset_state_cache()); re-exported apply_block_messages and validate_tipsets from state_computation; reduced EVENTS_AMT_BITWIDTH visibility to pub(crate).

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related PRs

Suggested reviewers

  • hanabi1224
  • LesnyRumcajs
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title accurately describes the main change: refactoring the state_manager mod.rs into multiple sub-modules. This is the primary objective reflected in the file summaries.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch akaladarshi/refactor-stmgr
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch akaladarshi/refactor-stmgr

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

@akaladarshi akaladarshi force-pushed the akaladarshi/refactor-stmgr branch from a40c2d9 to 2c9f42a Compare April 14, 2026 05:28
Comment thread src/state_manager/state_computation.rs Outdated
@akaladarshi akaladarshi force-pushed the akaladarshi/refactor-stmgr branch from 259a36f to 7e82e1d Compare April 15, 2026 07:47
@akaladarshi akaladarshi marked this pull request as ready for review April 15, 2026 07:49
@akaladarshi akaladarshi requested a review from a team as a code owner April 15, 2026 07:49
@akaladarshi akaladarshi requested review from LesnyRumcajs and hanabi1224 and removed request for a team April 15, 2026 07:49
@akaladarshi akaladarshi added the RPC requires calibnet RPC checks to run on CI label Apr 15, 2026
Copy link
Copy Markdown
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

🧹 Nitpick comments (1)
src/state_manager/message_search.rs (1)

99-102: Remove the unwrap() from this boundary check.

The short-circuit makes this safe today, but Line 101 is still a production unwrap() on a hot path. A small rewrite with is_some_and or match keeps the condition panic-free if this branch gets rearranged later.

♻️ Suggested change
             if parent_actor_state.is_none()
                 || (current_actor_state.sequence > message_sequence
-                    && parent_actor_state.as_ref().unwrap().sequence <= message_sequence)
+                    && parent_actor_state
+                        .as_ref()
+                        .is_some_and(|state| state.sequence <= message_sequence))
             {
As per coding guidelines "Avoid `unwrap()` in production code; use `?` or `expect()` with descriptive messages instead".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/state_manager/message_search.rs` around lines 99 - 102, The condition
using parent_actor_state.unwrap() is unsafe; replace the unwrap by checking the
Option directly (e.g., use Option::is_some_and(|p| p.sequence <=
message_sequence) or a match on parent_actor_state) so the expression becomes
panic-free while keeping the same logic comparing current_actor_state.sequence
and message_sequence; update the conditional that currently references
parent_actor_state.is_none() and parent_actor_state.as_ref().unwrap().sequence
to use a safe Option check involving parent_actor_state and its sequence field
(variables: parent_actor_state, current_actor_state.sequence, message_sequence).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/state_manager/actor_queries.rs`:
- Around line 171-174: The error message for the branch that loads
Address::DATACAP_TOKEN_ACTOR is wrong; update the Error::state call in the
get_actor result handling (where get_actor(&Address::DATACAP_TOKEN_ACTOR,
*ts.parent_state()) is used) to return a message indicating the datacap actor is
missing (e.g., "Datacap actor not found") instead of "Miner actor not found" so
logs and RPC responses correctly reflect the failing actor.

In `@src/state_manager/message_search.rs`:
- Around line 208-230: The code is recording applied tipsets into block_revert
instead of actual reverts, so search_back_poll never marks the backward-search
tipset as reverted; update the logic so that when observing head_changes.reverts
you insert the reverted tipset key into block_revert (use
tipset.key().to_owned()), and remove the insertion from the head_changes.applies
loop; adjust around candidate_tipset/candidate_receipt checks in the applies
loop so only applies drive acceptance while reverts populate block_revert; refer
to head_changes.reverts, head_changes.applies, candidate_tipset,
candidate_receipt, block_revert, search_back_poll, and wait_for_message to
locate the change.
- Around line 301-307: The code incorrectly continues the backward walk from
self.heaviest_tipset(), allowing a receipt that happened after the caller's
starting tipset; instead resume the search from the caller's provided starting
tipset (`from`). Update the call on the last line to continue from `from` (e.g.,
call `self.search_back_for_message(from, &message, look_back_limit,
allow_replaced)` or pass `Some(from)` into `search_for_message` as appropriate)
so the backward walk does not advance to `self.heaviest_tipset()`; keep the
existing fast-path `self.tipset_executed_message(&from, &message, ...)` check
intact.

---

Nitpick comments:
In `@src/state_manager/message_search.rs`:
- Around line 99-102: The condition using parent_actor_state.unwrap() is unsafe;
replace the unwrap by checking the Option directly (e.g., use
Option::is_some_and(|p| p.sequence <= message_sequence) or a match on
parent_actor_state) so the expression becomes panic-free while keeping the same
logic comparing current_actor_state.sequence and message_sequence; update the
conditional that currently references parent_actor_state.is_none() and
parent_actor_state.as_ref().unwrap().sequence to use a safe Option check
involving parent_actor_state and its sequence field (variables:
parent_actor_state, current_actor_state.sequence, message_sequence).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0b62694d-597a-46eb-926a-41c3e75a3007

📥 Commits

Reviewing files that changed from the base of the PR and between 227b6e5 and 7e82e1d.

📒 Files selected for processing (8)
  • src/state_manager/actor_queries.rs
  • src/state_manager/address_resolution.rs
  • src/state_manager/execution.rs
  • src/state_manager/message_search.rs
  • src/state_manager/message_simulation.rs
  • src/state_manager/mining.rs
  • src/state_manager/mod.rs
  • src/state_manager/state_computation.rs

Comment on lines +171 to +174
let act = self
.get_actor(&Address::DATACAP_TOKEN_ACTOR, *ts.parent_state())
.map_err(Error::state)?
.ok_or_else(|| Error::state("Miner actor not found"))?;
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot Apr 15, 2026

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix the actor name in this error path.

Line 174 reports "Miner actor not found", but this branch is loading Address::DATACAP_TOKEN_ACTOR. That makes missing-datacap failures harder to diagnose in RPC logs and responses.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/state_manager/actor_queries.rs` around lines 171 - 174, The error message
for the branch that loads Address::DATACAP_TOKEN_ACTOR is wrong; update the
Error::state call in the get_actor result handling (where
get_actor(&Address::DATACAP_TOKEN_ACTOR, *ts.parent_state()) is used) to return
a message indicating the datacap actor is missing (e.g., "Datacap actor not
found") instead of "Miner actor not found" so logs and RPC responses correctly
reflect the failing actor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@akaladarshi is this valid?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Copy Markdown
Contributor

@hanabi1224 hanabi1224 Apr 22, 2026

Choose a reason for hiding this comment

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

I see this is from the original code, but maybe we should correct it here

Comment on lines +208 to +230
for tipset in head_changes.reverts {
if candidate_tipset
.as_ref()
.is_some_and(|candidate| candidate.key() == tipset.key())
{
candidate_tipset = None;
candidate_receipt = None;
}
}
for tipset in head_changes.applies {
if candidate_tipset
.as_ref()
.map(|s| tipset.epoch() >= s.epoch() + confidence)
.unwrap_or_default()
{
return Ok((candidate_tipset, candidate_receipt));
}
let poll_receiver = receiver.try_recv();
if let Ok(Some(_)) = poll_receiver {
block_revert
.write()
.await
.insert(tipset.key().to_owned(), true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Track actual reverts before accepting the back-search result.

search_back_poll later interprets this map as “the backward-search tipset was reverted”. Lines 227-230 currently insert keys from head_changes.applies, so a real revert of back_tipset is never recorded and wait_for_message can return a receipt from a non-canonical chain during a reorg race.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/state_manager/message_search.rs` around lines 208 - 230, The code is
recording applied tipsets into block_revert instead of actual reverts, so
search_back_poll never marks the backward-search tipset as reverted; update the
logic so that when observing head_changes.reverts you insert the reverted tipset
key into block_revert (use tipset.key().to_owned()), and remove the insertion
from the head_changes.applies loop; adjust around
candidate_tipset/candidate_receipt checks in the applies loop so only applies
drive acceptance while reverts populate block_revert; refer to
head_changes.reverts, head_changes.applies, candidate_tipset, candidate_receipt,
block_revert, search_back_poll, and wait_for_message to locate the change.

Comment on lines +301 to +307
let current_tipset = self.heaviest_tipset();
let maybe_message_receipt =
self.tipset_executed_message(&from, &message, allow_replaced.unwrap_or(true))?;
if let Some(r) = maybe_message_receipt {
Ok(Some((from, r)))
} else {
self.search_back_for_message(current_tipset, &message, look_back_limit, allow_replaced)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Continue the backward walk from from, not the current head.

After the fast-path check against from, Line 307 switches to self.heaviest_tipset(). That lets search_for_message(Some(from), ...) return a receipt that only landed after the caller’s starting tipset.

🐛 Suggested change
-        let current_tipset = self.heaviest_tipset();
         let maybe_message_receipt =
             self.tipset_executed_message(&from, &message, allow_replaced.unwrap_or(true))?;
         if let Some(r) = maybe_message_receipt {
             Ok(Some((from, r)))
         } else {
-            self.search_back_for_message(current_tipset, &message, look_back_limit, allow_replaced)
+            self.search_back_for_message(from, &message, look_back_limit, allow_replaced)
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let current_tipset = self.heaviest_tipset();
let maybe_message_receipt =
self.tipset_executed_message(&from, &message, allow_replaced.unwrap_or(true))?;
if let Some(r) = maybe_message_receipt {
Ok(Some((from, r)))
} else {
self.search_back_for_message(current_tipset, &message, look_back_limit, allow_replaced)
let maybe_message_receipt =
self.tipset_executed_message(&from, &message, allow_replaced.unwrap_or(true))?;
if let Some(r) = maybe_message_receipt {
Ok(Some((from, r)))
} else {
self.search_back_for_message(from, &message, look_back_limit, allow_replaced)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/state_manager/message_search.rs` around lines 301 - 307, The code
incorrectly continues the backward walk from self.heaviest_tipset(), allowing a
receipt that happened after the caller's starting tipset; instead resume the
search from the caller's provided starting tipset (`from`). Update the call on
the last line to continue from `from` (e.g., call
`self.search_back_for_message(from, &message, look_back_limit, allow_replaced)`
or pass `Some(from)` into `search_for_message` as appropriate) so the backward
walk does not advance to `self.heaviest_tipset()`; keep the existing fast-path
`self.tipset_executed_message(&from, &message, ...)` check intact.

@akaladarshi akaladarshi changed the title refactor state manager mod into multiple sub mods [skip ci] refactor state manager mod into multiple sub mods Apr 15, 2026
pub fn miner_info(&self, addr: &Address, ts: &Tipset) -> Result<MinerInfo, Error> {
let actor = self
.get_actor(addr, *ts.parent_state())?
.ok_or_else(|| Error::state("Miner actor not found"))?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could add addr to the info to make it more useful

) -> Result<BitField, Error> {
let actor = self
.get_actor(addr, *ts.parent_state())?
.ok_or_else(|| Error::state("Miner actor not found"))?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
.ok_or_else(|| Error::state("Miner actor not found"))?;
.ok_or_else(|| Error::state(format!("Actor {addr} not found at epoch {}", ts.epoch())))?;

pub fn miner_info(&self, addr: &Address, ts: &Tipset) -> Result<MinerInfo, Error> {
let actor = self
.get_actor(addr, *ts.parent_state())?
.ok_or_else(|| Error::state("Miner actor not found"))?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
.ok_or_else(|| Error::state("Miner actor not found"))?;
.ok_or_else(|| Error::state(format!("Miner actor {addr} not found")))?;

Copy link
Copy Markdown
Contributor

@hanabi1224 hanabi1224 left a comment

Choose a reason for hiding this comment

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

LGTM with some NITs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

RPC requires calibnet RPC checks to run on CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants