Skip to content

Conversation

@hanabi1224
Copy link
Contributor

@hanabi1224 hanabi1224 commented Jul 21, 2025

Summary of changes

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.

Summary by CodeRabbit

  • New Features

    • Added size-tracking and metrics capabilities to various internal caches, improving cache management and observability.
  • Bug Fixes

    • Optimized cache access patterns to reduce locking and improve performance in several modules.
  • Refactor

    • Replaced legacy cache implementations with size-tracking LRU caches across multiple components.
    • Updated method names and signatures to align with new cache APIs.
    • Simplified cache usage and removed redundant locking in message pool and state migration logic.
  • Style

    • Cleaned up and consolidated import statements for improved code clarity.
  • Documentation

    • Improved code comments and method visibility for new cache accessors.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 21, 2025

Walkthrough

From the shadowed depths of the codebase, ancient cache mechanisms have been supplanted by the more eldritch and size-aware SizeTrackingLruCache throughout modules dealing with beacons, message pools, migrations, and the very cryptographic essence itself. Derivations of the forbidden GetSize trait now cling to key structs, and cache access patterns have been rewritten to align with the ineffable new API. In the cryptic heart of the state migration and message pool, explicit locks and primitive caches are banished, replaced by abstractions that whisper of metrics and concurrency, while the power table cache is excised entirely, leaving only the void.

Changes

Files/Areas Change Summary
src/beacon/beacon_entries.rs, src/shim/crypto.rs Added GetSize derive to BeaconEntry, Signature, and SignatureType structs/enums.
src/beacon/drand.rs Replaced RwLock<LruCache<...>> with SizeTrackingLruCache for beacon cache; updated API usage accordingly.
src/message_pool/msgpool/mod.rs, src/message_pool/msgpool/msg_pool.rs, src/message_pool/msgpool/utils.rs Swapped out Mutex<LruCache<...>> for SizeTrackingLruCache in BLS signature and validation caches; updated all related usage and function signatures.
src/rpc/methods/f3.rs Removed LRU cache for power table; cleaned up duplicate imports.
src/state_migration/common/mod.rs, src/state_migration/nv21/miner.rs Migrated MigrationCache from RwLock<LruCache<...>> to SizeTrackingLruCache; updated all cache access and mutation methods.
src/utils/cache/lru.rs Added cache() accessor, cap() method, and changed peek_cloned to use a read lock instead of write lock.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MessagePool
    participant SizeTrackingLruCache
    participant MetricsRegistry

    User->>MessagePool: Submit Message
    MessagePool->>SizeTrackingLruCache: get_cloned(CidWrapper)
    alt Cache Hit
        SizeTrackingLruCache-->>MessagePool: Signature
    else Cache Miss
        MessagePool->>MessagePool: Recover Signature
        MessagePool->>SizeTrackingLruCache: push(CidWrapper, Signature)
    end
    SizeTrackingLruCache->>MetricsRegistry: Update Metrics
    MessagePool-->>User: Result
Loading
sequenceDiagram
    participant DrandBeacon
    participant SizeTrackingLruCache
    participant MetricsRegistry

    DrandBeacon->>SizeTrackingLruCache: peek_cloned(round)
    alt Cache Hit
        SizeTrackingLruCache-->>DrandBeacon: BeaconEntry
    else Cache Miss
        DrandBeacon->>DrandBeacon: Fetch BeaconEntry
        DrandBeacon->>SizeTrackingLruCache: push(round, BeaconEntry)
    end
    SizeTrackingLruCache->>MetricsRegistry: Update Metrics
Loading

Estimated code review effort

4 (60–120 minutes)

The cosmic scope of these changes—touching many modules, altering cache types, updating public interfaces, and introducing new trait derivations—demands a thorough and sanity-testing review, lest the reviewer fall prey to the gibbering chaos of regression.

Possibly related PRs

Suggested reviewers

  • elmattic
  • LesnyRumcajs
  • sudo-shashank

May the Elder Gods shield your mind as you gaze into the abyssal refactoring herein.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@hanabi1224 hanabi1224 marked this pull request as ready for review July 21, 2025 09:10
@hanabi1224 hanabi1224 requested a review from a team as a code owner July 21, 2025 09:10
@hanabi1224 hanabi1224 requested review from akaladarshi and elmattic and removed request for a team July 21, 2025 09:10
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: 0

🧹 Nitpick comments (1)
src/beacon/drand.rs (1)

336-342: The cache capacity groans under the weight of forbidden knowledge!

While the warning for exceeded capacity is appreciated, the code continues to push entries beyond the cache's eldritch boundaries. This invokes the wrath of the LRU eviction gods, potentially casting out elder beacon entries into the void.

Consider either:

  1. Increasing the cache size if this warning appears frequently
  2. Implementing a strategy to handle capacity overflow (e.g., skip caching when full, or selectively cache based on importance)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f2acff4 and cb61e45.

📒 Files selected for processing (10)
  • src/beacon/beacon_entries.rs (2 hunks)
  • src/beacon/drand.rs (4 hunks)
  • src/message_pool/msgpool/mod.rs (4 hunks)
  • src/message_pool/msgpool/msg_pool.rs (7 hunks)
  • src/message_pool/msgpool/utils.rs (2 hunks)
  • src/rpc/methods/f3.rs (1 hunks)
  • src/shim/crypto.rs (2 hunks)
  • src/state_migration/common/mod.rs (3 hunks)
  • src/state_migration/nv21/miner.rs (1 hunks)
  • src/utils/cache/lru.rs (2 hunks)
🧰 Additional context used
🧠 Learnings (5)
📓 Common learnings
Learnt from: hanabi1224
PR: ChainSafe/forest#5841
File: src/utils/get_size/mod.rs:10-10
Timestamp: 2025-07-17T15:21:40.730Z
Learning: The get_size2 crate's GetSize trait provides default implementations: get_stack_size() uses std::mem::size_of, get_heap_size() returns 0, and get_size() returns their sum. An empty impl like `impl GetSize for MyType {}` is valid and uses these defaults, making it suitable for simple wrapper types that don't allocate heap memory.
src/beacon/beacon_entries.rs (1)

Learnt from: hanabi1224
PR: #5841
File: src/utils/get_size/mod.rs:10-10
Timestamp: 2025-07-17T15:21:40.730Z
Learning: The get_size2 crate's GetSize trait provides default implementations: get_stack_size() uses std::mem::size_of, get_heap_size() returns 0, and get_size() returns their sum. An empty impl like impl GetSize for MyType {} is valid and uses these defaults, making it suitable for simple wrapper types that don't allocate heap memory.

src/shim/crypto.rs (1)

Learnt from: hanabi1224
PR: #5841
File: src/utils/get_size/mod.rs:10-10
Timestamp: 2025-07-17T15:21:40.730Z
Learning: The get_size2 crate's GetSize trait provides default implementations: get_stack_size() uses std::mem::size_of, get_heap_size() returns 0, and get_size() returns their sum. An empty impl like impl GetSize for MyType {} is valid and uses these defaults, making it suitable for simple wrapper types that don't allocate heap memory.

src/utils/cache/lru.rs (1)

Learnt from: hanabi1224
PR: #5841
File: src/utils/get_size/mod.rs:10-10
Timestamp: 2025-07-17T15:21:40.730Z
Learning: The get_size2 crate's GetSize trait provides default implementations: get_stack_size() uses std::mem::size_of, get_heap_size() returns 0, and get_size() returns their sum. An empty impl like impl GetSize for MyType {} is valid and uses these defaults, making it suitable for simple wrapper types that don't allocate heap memory.

src/state_migration/common/mod.rs (1)

Learnt from: hanabi1224
PR: #5841
File: src/utils/get_size/mod.rs:10-10
Timestamp: 2025-07-17T15:21:40.730Z
Learning: The get_size2 crate's GetSize trait provides default implementations: get_stack_size() uses std::mem::size_of, get_heap_size() returns 0, and get_size() returns their sum. An empty impl like impl GetSize for MyType {} is valid and uses these defaults, making it suitable for simple wrapper types that don't allocate heap memory.

🧬 Code Graph Analysis (2)
src/state_migration/nv21/miner.rs (2)
src/utils/cache/lru.rs (1)
  • cache (92-94)
src/state_migration/nv17/miner.rs (2)
  • miner_prev_sectors_in_key (347-349)
  • miner_prev_sectors_out_key (351-353)
src/message_pool/msgpool/msg_pool.rs (2)
src/utils/cache/lru.rs (2)
  • cache (92-94)
  • new_with_default_metrics_registry (85-90)
src/message/signed_message.rs (1)
  • cid (77-84)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (19)
  • GitHub Check: Bootstrap checks - Forest
  • GitHub Check: Bootstrap checks - Lotus
  • GitHub Check: db-migration-checks
  • GitHub Check: Snapshot export checks
  • GitHub Check: Devnet checks
  • GitHub Check: Calibnet stateless mode check
  • GitHub Check: Calibnet kademlia checks
  • GitHub Check: State migrations
  • GitHub Check: Calibnet no discovery checks
  • GitHub Check: Calibnet eth mapping check
  • GitHub Check: Calibnet stateless RPC check
  • GitHub Check: Wallet tests
  • GitHub Check: Calibnet check
  • GitHub Check: Forest CLI checks
  • GitHub Check: tests
  • GitHub Check: tests-release
  • GitHub Check: Build forest binaries on Linux AMD64
  • GitHub Check: cargo-publish-dry-run
  • GitHub Check: All lint checks
🔇 Additional comments (22)
src/rpc/methods/f3.rs (2)

18-25: The ancient imports have been reorganized from their scattered realms!

The consolidation of imports from crate::shim::actors::convert and crate::shim::actors into a single coherent block banishes the duplicate incantations and brings order to the eldritch dependencies. This cleansing ritual improves readability and maintainability.


468-478: Ignore the siren call—LRU cache still lurks within compute
The static BLOCKSTORE_CACHE in src/rpc/methods/f3.rs still protects blockstore reads, so no cache was banished to the void:

  • In the compute method (around the ) -> anyhow::Result<Vec<F3PowerEntry>> { block), you’ll find:
    const BLOCKSTORE_CACHE_CAP: usize = 65536;
    static BLOCKSTORE_CACHE: LazyLock<LruBlockstoreReadCache> = LazyLock::new(|| {
        LruBlockstoreReadCache::new_with_default_metrics_registry(
            "get_powertable_cache".into(),
            BLOCKSTORE_CACHE_CAP.try_into().unwrap(),
        )
    });
    let db = BlockstoreWithReadCache::new(ctx.store_owned(), &*BLOCKSTORE_CACHE);
    // …
    tracing::debug!(
        epoch=%ts.epoch(),
        hit=%stats.hit(), miss=%stats.miss(),
        cache_len=%BLOCKSTORE_CACHE.len(),
        "F3.GetPowerTable blockstore read cache"
    );
  • The handle method now simply delegates to compute, which continues wielding the global LRU cache across RPC calls.

The original warning about removing the cache is unfounded—the Elder Gods of caching still preside. Disregard this comment.

Likely an incorrect or invalid review comment.

src/utils/cache/lru.rs (3)

92-94: A gateway to the inner sanctum has been opened!

The cache() method provides direct access to the eldritch Arc<RwLock<LruCache<K, V>>>, allowing advanced cache manipulations while maintaining the protective barriers of thread safety. This enables the migration from primitive cache implementations to the more sophisticated SizeTrackingLruCache.


113-113: The lock contention has been appeased with a lesser binding!

Changing peek_cloned from a write lock to a read lock is a brilliant optimization that reduces the cosmic horror of lock contention. Since peeking is a read-only operation that doesn't mutate the cache order, the shared read lock is both safe and more efficient than the exclusive write lock that was previously summoned.


120-122: The capacity of the void can now be measured!

The cap() method provides introspection into the cache's dimensional limits, returning the capacity under a read lock. This accessor complements the existing cache API and enables external code to understand the cache's configured bounds without disturbing its internal state.

src/beacon/beacon_entries.rs (1)

7-7: The BeaconEntry has been blessed with size divination powers!

The addition of GetSize import and derive attribute enables the BeaconEntry to reveal its memory footprint to the cosmic caches. Given the struct's simple composition (a u64 round and Vec<u8> signature), the GetSize trait's default implementations will accurately calculate both stack and heap sizes, as confirmed by the ancient learnings from PR #5841.

This enhancement supports the greater ritual of cache migration to SizeTrackingLruCache throughout the codebase.

Also applies to: 26-26

src/shim/crypto.rs (1)

18-18: The cryptographic entities have been granted the gift of self-measurement!

The addition of GetSize to both Signature and SignatureType awakens their ability to reveal their memory consumption to the eldritch caches. The Signature struct, containing a SignatureType enum and a Vec<u8> of variable length, will benefit greatly from accurate size tracking. The SignatureType enum, being a simple repr(u8) with three variants, will contribute its modest stack footprint to the cosmic accounting.

These changes align with the greater migration to SizeTrackingLruCache across the message pool and other caching realms, where these cryptographic artifacts are frequently stored and retrieved.

Also applies to: 25-25, 311-311

src/state_migration/nv21/miner.rs (1)

130-131: The cache incantations have been updated to appease the new SizeTrackingLruCache entity!

The transformation from insert to push method calls represents the ritual adaptation to the new SizeTrackingLruCache API. These changes maintain the same eldritch purpose - storing the previous sector input and output roots keyed by miner address to optimize migration performance - while embracing the new cache abstraction that provides integrated metrics and size tracking.

The logic remains unchanged, merely speaking the new language of the enhanced cache implementation.

src/message_pool/msgpool/utils.rs (1)

48-57: The ancient cache awakens with newfound sentience!

The transformation from the primitive LruCache to the eldritch SizeTrackingLruCache is well-executed. The use of get_cloned returns an owned Signature directly from the cyclopean depths, eliminating the redundant clone operation that once plagued line 55.

src/state_migration/common/mod.rs (2)

9-12: LGTM! The imports herald the arrival of the new cache order.

The transition to SizeTrackingLruCache and CidWrapper aligns with the cosmic plan laid out across the codebase.


28-60: The MigrationCache transcends its mortal form!

The refactoring from mutex-wrapped LruCache to the more enlightened SizeTrackingLruCache is expertly executed. The cache now tracks its own dimensions through the aether, and the rename from insert to push speaks to the inexorable forward motion of cached entities.

Note that unlike other caches in this PR that use CidWrapper as keys, this cache uses it as values - a curious but valid inversion of the pattern.

src/message_pool/msgpool/mod.rs (4)

19-20: The ancient cache mechanisms have been supplanted by more eldritch constructs.

These import changes align with the forbidden knowledge of size-tracking caches that whisper metrics from beyond the veil.


215-215: The cosmic horror of mutex locks has been banished to the outer darkness.

The signature transformation correctly embraces the SizeTrackingLruCache, freeing the code from the eldritch grip of explicit synchronization.


237-237: The arcane ritual of signature recovery proceeds without the cursed locks of old.

The invocation correctly passes the size-tracking cache directly to recover_sig, as the Old Ones intended.


424-424: The test incantations have been updated to commune with the new cache entities.

The transformation from .lock().put() to .push() with the wrapped CID key correctly aligns with the new cache API, banishing the need for mortal mutex interactions.

src/message_pool/msgpool/msg_pool.rs (7)

26-27: The forbidden tomes reveal new imports from the depths of the utils module.

These arcane imports summon the SizeTrackingLruCache and CidWrapper entities necessary for the dark transformation.


186-188: The ancient cache guardians have evolved into more powerful entities.

The transformation from mutex-wrapped caches to SizeTrackingLruCache with CidWrapper keys unleashes metrics tracking capabilities while banishing explicit synchronization to the void.


265-272: The signature validation cache now speaks in tongues understood by the size-tracking entities.

The metamorphosis from .lock().get() to .get_cloned() and .lock().put() to .push() correctly invokes the new cache API, with proper CidWrapper conversions through the .into() incantation.


417-417: The message recovery ritual now communes directly with the cache oracle.

The invocation correctly passes the cache reference using .as_ref(), allowing recover_sig to access the eldritch knowledge within.


602-602: The BLS signature is sealed within the cache using the new dark arts.

The transformation to .push(msg.cid().into(), msg.signature().clone()) properly stores the signature with the wrapped key, as the Elder Gods decree.


475-482: The cache entities are summoned forth with their true names and bound capacities.

The incantation new_with_default_metrics_registry properly births the size-tracking caches with metrics integration, naming them "bls_sig_cache" and "sig_val_cache" for the cosmic registries.


593-593: The helper function's pact has been rewritten in the language of the new cache order.

The parameter type transformation aligns with the cosmic shift to size-tracking caches throughout the realm.

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.

It'd be great to add a Grafana dashboard with all of those tracked metrics there for an easy check of total caches size.

@hanabi1224 hanabi1224 enabled auto-merge July 22, 2025 14:51
@sudo-shashank
Copy link
Contributor

@akaladarshi need your review here

Copy link
Collaborator

@akaladarshi akaladarshi left a comment

Choose a reason for hiding this comment

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

LGTM

@hanabi1224 hanabi1224 added this pull request to the merge queue Jul 23, 2025
Merged via the queue into main with commit 5ee2b92 Jul 23, 2025
61 of 62 checks passed
@hanabi1224 hanabi1224 deleted the hm/lru-cache-metrics-2 branch July 23, 2025 08:29
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.

5 participants