Skip to content

refactor: optimize cache key type for resolve_to_key#6961

Merged
hanabi1224 merged 3 commits intomainfrom
hm/cache-key-for-resolve_to_key
Apr 23, 2026
Merged

refactor: optimize cache key type for resolve_to_key#6961
hanabi1224 merged 3 commits intomainfrom
hm/cache-key-for-resolve_to_key

Conversation

@hanabi1224
Copy link
Copy Markdown
Contributor

@hanabi1224 hanabi1224 commented Apr 23, 2026

Summary of changes

Follow up on https://github.com/ChainSafe/forest/pull/6910/changes#r3109534232

Changes introduced in this pull request:

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

  • Refactor

    • Improved message-pool address-resolution and caching: swapped to an ID-keyed cache and updated resolution flow for more efficient lookups and reduced overhead, improving stability during chain reorgs.
    • Introduced a dedicated numeric address identifier type to standardize cache keys.
  • Documentation

    • Clarified deterministic address-resolution semantics and finality/reorg-stability expectations.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 3f0b61e7-b294-431d-bd72-e4af4842db1c

📥 Commits

Reviewing files that changed from the base of the PR and between 0660f70 and 5299819.

📒 Files selected for processing (5)
  • src/message_pool/msgpool/mod.rs
  • src/message_pool/msgpool/msg_pool.rs
  • src/message_pool/msgpool/selection.rs
  • src/shim/address.rs
  • src/state_manager/mod.rs
✅ Files skipped from review due to trivial changes (2)
  • src/shim/address.rs
  • src/message_pool/msgpool/mod.rs

Walkthrough

Message-pool address-resolution now uses finality-stable deterministic resolution and an ID-keyed cache: cache keys changed from Address to AddressId (u64), Provider::resolve_to_key was renamed to resolve_to_deterministic_address_at_finality, and related call sites/impls updated.

Changes

Cohort / File(s) Summary
Message Pool Core
src/message_pool/msgpool/mod.rs, src/message_pool/msgpool/msg_pool.rs, src/message_pool/msgpool/selection.rs
Replaced Address-keyed cache with IdToAddressCache (AddressId/u64 keys). Updated resolve helpers to extract numeric ID, consult/insert ID-keyed cache, and call finality-aware resolution API. Adjusted callers to pass references to non-Arc caches.
Provider Trait & Impl
src/message_pool/msgpool/provider.rs, src/message_pool/msgpool/test_provider.rs
Renamed Provider::resolve_to_keyresolve_to_deterministic_address_at_finality and updated ChainStore/TestApi implementations to perform finality-lookback deterministic resolution using the lookback tipset state.
State Manager / Types
src/state_manager/mod.rs, src/shim/address.rs
Added pub type IdToAddressCache = SizeTrackingLruCache<AddressId, Address>; and pub type AddressId = u64. Updated StateManager cache field to use IdToAddressCache and clarified docs for deterministic resolution.
Tests / Minor Call Sites
src/message_pool/msgpool/test_provider.rs, related tests
Updated tests to call the renamed provider method and assert behavior via finality-aware resolution where applicable.

Sequence Diagram(s)

sequenceDiagram
    participant MP as MessagePool
    participant Cache as KeyCache (AddressId -> Address)
    participant Prov as Provider
    participant SM as StateManager / ChainStore

    MP->>Cache: lookup(addr.id() -> AddressId)
    alt cache hit
        Cache-->>MP: cached Address
    else cache miss
        MP->>Prov: resolve_to_deterministic_address_at_finality(addr, tipset)
        Prov->>SM: compute lookback state & resolve deterministically
        SM-->>Prov: deterministic Address
        Prov-->>MP: deterministic Address
        MP->>Cache: insert(AddressId, Address)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • sudo-shashank
  • LesnyRumcajs
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: refactoring to optimize the cache key type for resolve_to_key functionality, which is consistent with the PR's introduction of IdToAddressCache and related cache key optimizations across multiple modules.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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 hm/cache-key-for-resolve_to_key
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch hm/cache-key-for-resolve_to_key

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

@hanabi1224 hanabi1224 added the RPC requires calibnet RPC checks to run on CI label Apr 23, 2026
@hanabi1224 hanabi1224 marked this pull request as ready for review April 23, 2026 08:34
@hanabi1224 hanabi1224 requested a review from a team as a code owner April 23, 2026 08:34
@hanabi1224 hanabi1224 requested review from LesnyRumcajs and sudo-shashank and removed request for a team April 23, 2026 08:34
Comment thread src/message_pool/msgpool/msg_pool.rs
Comment thread src/message_pool/msgpool/msg_pool.rs
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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/message_pool/msgpool/provider.rs (1)

122-132: ⚠️ Potential issue | 🟠 Major

Return an error until a finality lookback tipset exists.

This branch currently resolves against ts when the chain is younger than chain_finality, which contradicts the contract on Lines 51-52 and lets the mpool cache mappings that are not finality-stable yet. Please fail fast here instead of treating the current head as final.

Suggested fix
-                let lookback_ts = if ts.epoch() > self.chain_config().policy.chain_finality {
+                let lookback_ts = if ts.epoch() > self.chain_config().policy.chain_finality {
                     self.chain_index()
                         .tipset_by_height(
                             ts.epoch() - self.chain_config().policy.chain_finality,
                             ts.clone(),
                             ResolveNullTipset::TakeOlder,
                         )
                         .map_err(|e| Error::Other(e.to_string()))?
                 } else {
-                    ts.clone()
+                    return Err(Error::Other(
+                        "cannot resolve ID address to a finality-stable deterministic address yet"
+                            .into(),
+                    ));
                 };
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/message_pool/msgpool/provider.rs` around lines 122 - 132, The branch that
sets lookback_ts currently uses ts.clone() when ts.epoch() <=
self.chain_config().policy.chain_finality; instead, fail fast by returning an
error so we don't cache non-finality-stable mappings: in the function that
computes lookback_ts (referencing lookback_ts,
self.chain_config().policy.chain_finality, and
self.chain_index().tipset_by_height with ResolveNullTipset), replace the else
arm with an early Err(...) return (e.g., using Error::Other with a clear
message) indicating that a finality lookback tipset does not yet exist rather
than using ts.clone().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/message_pool/msgpool/provider.rs`:
- Around line 122-132: The branch that sets lookback_ts currently uses
ts.clone() when ts.epoch() <= self.chain_config().policy.chain_finality;
instead, fail fast by returning an error so we don't cache non-finality-stable
mappings: in the function that computes lookback_ts (referencing lookback_ts,
self.chain_config().policy.chain_finality, and
self.chain_index().tipset_by_height with ResolveNullTipset), replace the else
arm with an early Err(...) return (e.g., using Error::Other with a clear
message) indicating that a finality lookback tipset does not yet exist rather
than using ts.clone().

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6d390790-a546-431e-a2da-70b4fa4adcfa

📥 Commits

Reviewing files that changed from the base of the PR and between 25ef5e2 and c498ce7.

📒 Files selected for processing (6)
  • src/message_pool/msgpool/mod.rs
  • src/message_pool/msgpool/msg_pool.rs
  • src/message_pool/msgpool/provider.rs
  • src/message_pool/msgpool/selection.rs
  • src/message_pool/msgpool/test_provider.rs
  • src/state_manager/mod.rs

@hanabi1224 hanabi1224 force-pushed the hm/cache-key-for-resolve_to_key branch from c498ce7 to 0660f70 Compare April 23, 2026 08:47
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.

🧹 Nitpick comments (1)
src/message_pool/msgpool/provider.rs (1)

122-140: Add operation-specific context to error mapping.

At Line 129, Line 137, and Line 140, converting errors with e.to_string() drops step-level context, which makes runtime diagnosis harder.

Suggested patch
-                let lookback_ts = if ts.epoch() > self.chain_config().policy.chain_finality {
+                let finality = self.chain_config().policy.chain_finality;
+                let lookback_ts = if ts.epoch() > finality {
                     self.chain_index()
                         .tipset_by_height(
-                            ts.epoch() - self.chain_config().policy.chain_finality,
+                            ts.epoch() - finality,
                             ts.clone(),
                             ResolveNullTipset::TakeOlder,
                         )
-                        .map_err(|e| Error::Other(e.to_string()))?
+                        .map_err(|e| {
+                            Error::Other(format!(
+                                "failed loading lookback tipset at epoch {}: {e}",
+                                ts.epoch() - finality
+                            ))
+                        })?
                 } else {
                     // Matches the logic at <https://github.com/filecoin-project/lotus/blob/v1.35.1/chain/stmgr/stmgr.go#L361>
                     ts.clone()
                 };
 
                 let state =
                     StateTree::new_from_root(self.blockstore().clone(), lookback_ts.parent_state())
-                        .map_err(|e| Error::Other(e.to_string()))?;
+                        .map_err(|e| Error::Other(format!("failed building state tree: {e}")))?;
                 state
                     .resolve_to_deterministic_addr(self.blockstore(), *addr)
-                    .map_err(|e| Error::Other(e.to_string()))
+                    .map_err(|e| {
+                        Error::Other(format!(
+                            "failed resolving deterministic address at finality for {addr}: {e}"
+                        ))
+                    })

As per coding guidelines, “Use anyhow::Result<T> for most operations and add context with .context() when errors occur”.

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

In `@src/message_pool/msgpool/provider.rs` around lines 122 - 140, The error
mappings around the lookback/tipset and state resolution drop useful context;
update the calls that currently use map_err(|e| Error::Other(e.to_string())) for
tipset_by_height, StateTree::new_from_root, and
state.resolve_to_deterministic_addr to add operation-specific context (e.g.,
using anyhow::Context and .context("...") or otherwise wrapping the error with a
message) so failures include which step failed (computing lookback_ts via
tipset_by_height, constructing StateTree from parent_state, or resolving
deterministic address) and relevant values (epoch/chain_finality, parent_state,
addr); adjust the function signature to return anyhow::Result if needed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/message_pool/msgpool/provider.rs`:
- Around line 122-140: The error mappings around the lookback/tipset and state
resolution drop useful context; update the calls that currently use map_err(|e|
Error::Other(e.to_string())) for tipset_by_height, StateTree::new_from_root, and
state.resolve_to_deterministic_addr to add operation-specific context (e.g.,
using anyhow::Context and .context("...") or otherwise wrapping the error with a
message) so failures include which step failed (computing lookback_ts via
tipset_by_height, constructing StateTree from parent_state, or resolving
deterministic address) and relevant values (epoch/chain_finality, parent_state,
addr); adjust the function signature to return anyhow::Result if needed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bfa04351-eada-43f5-82a3-cbbe759a7519

📥 Commits

Reviewing files that changed from the base of the PR and between c498ce7 and 0660f70.

📒 Files selected for processing (6)
  • src/message_pool/msgpool/mod.rs
  • src/message_pool/msgpool/msg_pool.rs
  • src/message_pool/msgpool/provider.rs
  • src/message_pool/msgpool/selection.rs
  • src/message_pool/msgpool/test_provider.rs
  • src/state_manager/mod.rs
✅ Files skipped from review due to trivial changes (1)
  • src/state_manager/mod.rs
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/message_pool/msgpool/test_provider.rs
  • src/message_pool/msgpool/mod.rs
  • src/message_pool/msgpool/msg_pool.rs
  • src/message_pool/msgpool/selection.rs

LesnyRumcajs
LesnyRumcajs previously approved these changes Apr 23, 2026
Comment thread src/message_pool/msgpool/mod.rs Outdated
Co-authored-by: Copilot <copilot@github.com>
@hanabi1224 hanabi1224 enabled auto-merge April 23, 2026 11:20
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

❌ Patch coverage is 98.18182% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 64.09%. Comparing base (9eca031) to head (5299819).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/message_pool/msgpool/msg_pool.rs 97.50% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/message_pool/msgpool/mod.rs 91.48% <100.00%> (+0.13%) ⬆️
src/message_pool/msgpool/provider.rs 61.19% <100.00%> (+5.63%) ⬆️
src/message_pool/msgpool/selection.rs 86.93% <100.00%> (ø)
src/message_pool/msgpool/test_provider.rs 99.04% <100.00%> (+0.01%) ⬆️
src/shim/address.rs 85.50% <ø> (ø)
src/state_manager/mod.rs 66.57% <ø> (+0.13%) ⬆️
src/message_pool/msgpool/msg_pool.rs 89.51% <97.50%> (+0.04%) ⬆️

... and 12 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 9eca031...5299819. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hanabi1224 hanabi1224 added this pull request to the merge queue Apr 23, 2026
Merged via the queue into main with commit 65c39a0 Apr 23, 2026
36 of 37 checks passed
@hanabi1224 hanabi1224 deleted the hm/cache-key-for-resolve_to_key branch April 23, 2026 12:56
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