Skip to content

refactor: msg pool to make more structured#6965

Merged
akaladarshi merged 8 commits intomainfrom
akaladarshi/msgpool-refactor
May 6, 2026
Merged

refactor: msg pool to make more structured#6965
akaladarshi merged 8 commits intomainfrom
akaladarshi/msgpool-refactor

Conversation

@akaladarshi
Copy link
Copy Markdown
Collaborator

@akaladarshi akaladarshi commented Apr 23, 2026

Summary of changes

Changes introduced in this pull request:

  • Part 1 one of the refactor of the msg_pool, this mostly contains the structural refactor such as combining methods and creating new files etc.:
    • Introduced a new pending_store that holds all the pending related data for the mempool
    • Moved the mset related changes into it's own file to reduce the bloat on msgpool

Reference issue to close (if applicable)

Part of #7010

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

  • New Features

    • Subscribe to real-time pending-message add/remove events.
  • Improvements

    • Pending-message state moved to a snapshot-capable store for safer concurrency and more efficient reads.
    • Stricter, clearer nonce-gap and replace-by-fee rules to improve transaction ordering and capacity enforcement.
    • Message selection and head-change flows now use snapshot-based reads.
  • Tests

    • Updated and expanded tests for snapshots, event emission, capacity/RBF, and nonce-gap scenarios.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 23, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

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: 3f9d1289-6a7f-4f23-aea5-d854e4ced1f7

📥 Commits

Reviewing files that changed from the base of the PR and between 4ba6458 and 10d8a08.

📒 Files selected for processing (3)
  • src/message_pool/msgpool/msg_pool.rs
  • src/message_pool/msgpool/pending_store.rs
  • src/utils/shallow_clone.rs
💤 Files with no reviewable changes (1)
  • src/utils/shallow_clone.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/message_pool/msgpool/pending_store.rs
  • src/message_pool/msgpool/msg_pool.rs

Walkthrough

Replaces the per-address in-memory pending HashMap with a crate-scoped PendingStore, extracts per-sender logic into MsgSet, and adds MpoolUpdate broadcast events; threads PendingStore through selection/republish/head-change, updates MessagePool APIs, and adjusts tests to use snapshot/subscribe APIs.

Changes

Pending store + MsgSet + wiring

Layer / File(s) Summary
Data Shape / Events
src/message_pool/msgpool/msg_set.rs, src/message_pool/msgpool/events.rs
Adds MsgSet, MsgSetLimits, StrictnessPolicy, MAX_NONCE_GAP; adds MpoolUpdate enum (Add, Remove) and MPOOL_UPDATE_CHANNEL_CAPACITY = 256.
Core Implementation
src/message_pool/msgpool/pending_store.rs
Introduces PendingStore owning the per-sender pending map, broadcast channel, and limits; implements new, insert, remove, snapshot, snapshot_for, subscribe; emits MpoolUpdate only on real mutations.
MessagePool Integration
src/message_pool/msgpool/msg_pool.rs, src/message_pool/msgpool/mod.rs
Replaces pending: SyncRwLock<HashMap<...>> with pending_store: PendingStore; routes insert/remove through pending_store; adds MessagePool::subscribe_to_updates(); threads pending_store into MpoolCtx; tightens head_change visibility.
Selection & Republish Wiring
src/message_pool/msgpool/selection.rs, src/message_pool/msgpool/mod.rs
Selection and republish use pending_store.snapshot() / snapshot_for(...); run_head_change/selection accept &PendingStore; removal fallbacks call pending_store.remove.
Tests / Exports
src/message_pool/msgpool/...
Adds submodules (events, msg_set, pending_store), re-exports MpoolUpdate, updates tests to construct/use PendingStore, assert via snapshots, and remove direct MsgSet-internal assumptions.

Sequence Diagram(s)

sequenceDiagram
    participant Client as "MessagePool API"
    participant Store as "PendingStore"
    participant MsgSet as "MsgSet (per-sender)"
    participant Sub as "Subscriber (broadcast::Receiver)"

    Client->>Store: insert(resolved_from, msg, sequence, trust, strictness)
    Store->>MsgSet: get_or_create(resolved_from)
    MsgSet-->>Store: add(msg, strictness, trust, limits) -> Result
    alt insert succeeded
        Store->>Store: persist into map
        Store->>Sub: broadcast MpoolUpdate::Add(msg)
    else insert failed
        Store-->>Client: return Error
    end

    Client->>Store: remove(from, sequence, applied)
    Store->>MsgSet: rm(sequence, applied) -> Option<msg>
    alt removal occurred
        Store->>Sub: broadcast MpoolUpdate::Remove(msg)
    else no-op
        Store-->>Client: None
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • sudo-shashank
  • LesnyRumcajs
  • hanabi1224
🚥 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 'refactor: msg pool to make more structured' accurately describes the main focus of the PR: structural reorganization of the message pool code through extraction of new modules and consolidation of related logic.
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 akaladarshi/msgpool-refactor
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch akaladarshi/msgpool-refactor

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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

@akaladarshi akaladarshi changed the title refactor: msg pool to make more structured [skip ci] refactor: msg pool to make more structured Apr 27, 2026
@akaladarshi akaladarshi added the RPC requires calibnet RPC checks to run on CI label Apr 27, 2026
@akaladarshi akaladarshi marked this pull request as ready for review April 27, 2026 15:07
@akaladarshi akaladarshi requested a review from a team as a code owner April 27, 2026 15:07
@akaladarshi akaladarshi requested review from LesnyRumcajs and sudo-shashank and removed request for a team April 27, 2026 15:07
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

🤖 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/message_pool/msgpool/msg_pool.rs`:
- Around line 449-453: The public method subscribe_to_updates currently returns
broadcast::Receiver<MpoolUpdate> but MpoolUpdate is crate-private (declared
pub(in crate::message_pool) in events.rs), so callers outside
crate::message_pool cannot name the type; either re-export MpoolUpdate from a
public module (add a pub use crate::message_pool::msgpool::events::MpoolUpdate
in a public mod) so the type is publicly reachable, or reduce
subscribe_to_updates() visibility to match MpoolUpdate (make
subscribe_to_updates pub(crate) or pub(in crate::message_pool)) so the declared
signature and type visibility align; update the signature or re-export
accordingly and adjust any callers.

In `@src/message_pool/msgpool/msg_set.rs`:
- Around line 183-188: The applied=true branch in msg_set.rs only sets
self.next_sequence = sequence + 1 which can leave already-buffered higher nonces
unaccounted for; change the branch to advance next_sequence past any buffered
successors the same way the unknown-message branch does: set next_sequence =
sequence + 1 and then loop while self.msgs.contains_key(&self.next_sequence) (or
the equivalent buffered map/set used in this struct) incrementing next_sequence
until no buffered message exists, so callers are not told they can reuse a
still-pending nonce.

In `@src/message_pool/msgpool/selection.rs`:
- Around line 853-857: The simulated path run_head_change() is mutating the live
PendingStore because MpoolCtx is constructed with the real pending_store; change
the helper to be side-effect free by giving MpoolCtx a simulation-only context
or snapshot: either construct MpoolCtx with a read-only/snapshot PendingStore
clone or introduce a SimulationPendingStore (or a flag on MpoolCtx) that makes
remove_from_selected_msgs() operate only on the local rmsgs/result map rather
than calling pending_store.remove(...). Update the MpoolCtx construction in
run_head_change() and ensure MpoolCtx::remove_from_selected_msgs() checks the
simulation mode or uses the snapshot store so select_messages() against
non-current tipsets cannot delete entries from the live mempool.
🪄 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: 3914d600-4744-47e8-8b77-311223afc188

📥 Commits

Reviewing files that changed from the base of the PR and between 65c39a0 and c515b49.

📒 Files selected for processing (6)
  • src/message_pool/msgpool/events.rs
  • src/message_pool/msgpool/mod.rs
  • src/message_pool/msgpool/msg_pool.rs
  • src/message_pool/msgpool/msg_set.rs
  • src/message_pool/msgpool/pending_store.rs
  • src/message_pool/msgpool/selection.rs

Comment thread src/message_pool/msgpool/msg_pool.rs
Comment thread src/message_pool/msgpool/msg_set.rs
Comment thread src/message_pool/msgpool/selection.rs
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

❌ Patch coverage is 95.84775% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.23%. Comparing base (420a5ab) to head (10d8a08).
⚠️ Report is 3 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/message_pool/msgpool/msg_set.rs 95.48% 9 Missing and 5 partials ⚠️
src/message_pool/msgpool/pending_store.rs 97.08% 2 Missing and 4 partials ⚠️
src/message_pool/msgpool/msg_pool.rs 92.50% 3 Missing ⚠️
src/message_pool/msgpool/selection.rs 90.90% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
src/message_pool/msgpool/mod.rs 91.30% <100.00%> (+0.08%) ⬆️
src/utils/shallow_clone.rs 100.00% <ø> (ø)
src/message_pool/msgpool/selection.rs 86.82% <90.90%> (-0.11%) ⬇️
src/message_pool/msgpool/msg_pool.rs 87.63% <92.50%> (-1.78%) ⬇️
src/message_pool/msgpool/pending_store.rs 97.08% <97.08%> (ø)
src/message_pool/msgpool/msg_set.rs 95.48% <95.48%> (ø)

... and 8 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 420a5ab...10d8a08. 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.

Comment thread src/message_pool/msgpool/events.rs
@LesnyRumcajs
Copy link
Copy Markdown
Member

@akaladarshi Anything with 🐰 comments to address? If not, mark them as resolved.

@sudo-shashank
Copy link
Copy Markdown
Contributor

Coverage check is failing dues to TODO lint error

@LesnyRumcajs
Copy link
Copy Markdown
Member

Coverage check is failing dues to TODO lint error

The lint was removed in #6977.

Comment thread src/message_pool/msgpool/events.rs Outdated
Comment thread src/message_pool/msgpool/pending_store.rs Outdated
LesnyRumcajs
LesnyRumcajs previously approved these changes May 4, 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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/message_pool/msgpool/mod.rs`:
- Around line 14-16: Remove the "TODO" marker in the comment above the pub use
to avoid failing the TODO lint: either delete the entire comment line "// TODO:
This will be used in https://github.com/ChainSafe/forest/pull/6941" or rewrite
it as a non-TODO remark (e.g., "// Will be used in PR ...") so the file still
exposes events::MpoolUpdate but no TODO token remains in
src/message_pool/msgpool/mod.rs.
🪄 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: 78cd95d6-c294-4f15-8004-e891c682cf26

📥 Commits

Reviewing files that changed from the base of the PR and between 0d66af3 and 4ba6458.

📒 Files selected for processing (6)
  • src/message_pool/msgpool/events.rs
  • src/message_pool/msgpool/mod.rs
  • src/message_pool/msgpool/msg_pool.rs
  • src/message_pool/msgpool/msg_set.rs
  • src/message_pool/msgpool/pending_store.rs
  • src/message_pool/msgpool/selection.rs
✅ Files skipped from review due to trivial changes (2)
  • src/message_pool/msgpool/pending_store.rs
  • src/message_pool/msgpool/msg_set.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/message_pool/msgpool/events.rs
  • src/message_pool/msgpool/msg_pool.rs

Comment thread src/message_pool/msgpool/mod.rs
@akaladarshi akaladarshi enabled auto-merge May 5, 2026 07:45
@akaladarshi akaladarshi requested a review from LesnyRumcajs May 5, 2026 11:37
Comment thread src/message_pool/msgpool/pending_store.rs Outdated
Comment thread src/message_pool/msgpool/pending_store.rs Outdated
@akaladarshi akaladarshi added this pull request to the merge queue May 6, 2026
Merged via the queue into main with commit 868b7c2 May 6, 2026
34 checks passed
@akaladarshi akaladarshi deleted the akaladarshi/msgpool-refactor branch May 6, 2026 15:47
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.

3 participants