Conversation
WalkthroughReplace flat pending-tx list and global seen-ID set with a nested Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@minichain/mempool.py`:
- Around line 35-38: The current block-selection code holds self._lock while
sorting, which serializes add_transaction/remove_transactions; instead, inside
the critical section (under self._lock) capture a shallow snapshot of
transactions from self._pool into a local list and then release the lock, and
perform txs.sort(...) and slice (txs[: self.transactions_per_block]) outside the
lock; update the method that contains this code (referencing self._lock,
self._pool, and transactions_per_block) so only the snapshot collection is done
under the lock and the O(N log N) sort runs after the lock is released.
- Around line 20-31: Currently the code calls self._pool.setdefault(tx.sender,
{}) before checking capacity, which creates empty per-sender buckets even when
the mempool is full; change the logic to first probe the bucket with
self._pool.get(tx.sender) (or check existing = self._pool.get(tx.sender,
{}).get(tx.nonce)), perform the duplicate check (existing and existing.tx_id ==
tx.tx_id) and the capacity check (if not existing and self._size >=
self.max_size) before mutating self._pool, and only call setdefault or assign a
new sub-dict and increment self._size when you are actually accepting and
storing the transaction; keep references to self._pool, self._size,
self.max_size, tx.sender, tx.nonce, and tx.tx_id to locate the changes.
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: dd0d3d6c-6f70-4f6d-b26e-400508a08cb4
📒 Files selected for processing (1)
minichain/mempool.py
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@minichain/mempool.py`:
- Around line 20-31: When reinserting a tx into the mempool (the block shown
that reads existing = self._pool.get(tx.sender, {}).get(tx.nonce)), guard
against overwriting a newer replacement: only write pool[tx.nonce] = tx and
increment self._size when the slot is currently empty or contains the same
tx_id; if an existing entry with a different tx_id is present, skip the
overwrite and do not change self._size (also keep the max_size check when
inserting new slots). Update the logic around existing / self._size /
pool[tx.nonce] to enforce these conditions so a rejected-block restore cannot
revert a newer replacement.
- Around line 36-39: The current code flattens all mempool txs then
sorts/slices, which lets later nonces with earlier timestamps crowd out a
sender's lower nonce; fix by building the candidate window from per-sender
nonce-frontiers: iterate self._pool per sender, sort each sender's pool by nonce
(ascending), take that sender's lowest-nonce pending tx (and optionally
contiguous next nonces if you want deeper per-sender window), collect those
frontier txs into txs, then sort that candidate list by (timestamp, sender,
nonce) and return the first self.transactions_per_block entries; reference
self._pool, transactions_per_block, and the txs variable when making this
change.
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 7e44c830-db11-47bd-b09d-1bd4f36973b9
📒 Files selected for processing (1)
minichain/mempool.py
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@minichain/mempool.py`:
- Around line 47-64: The selection loop in the mempool (function handling
selected/transactions_per_block using snapshot, best_tx, best_sender) only
compares tx.timestamp so ties depend on dict traversal order; change the
comparison to use a deterministic tuple key such as (tx.timestamp, sender,
tx.nonce) or (tx.timestamp, sender_id, tx.nonce) when deciding
best_tx/best_sender so heads with equal timestamps break ties consistently
across runs; implement the tuple construction and compare tuples instead of just
timestamp when selecting txs from snapshot in the loop.
- Around line 20-37: The bug is that _size is incremented even when an existing
(sender, nonce) entry is replaced; change the logic in the block using variables
existing, self._pool, and self._size so that self._size is only incremented when
inserting a new slot (i.e., when existing is falsy). Concretely, move the
self._size += 1 so it runs only in the branch that executes
self._pool.setdefault(tx.sender, {})[tx.nonce] = tx when existing is None (do
not increment on replacements), ensuring __len__() reflects actual pool
cardinality.
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 5226d50b-7d42-4b88-8570-4ff2c64b3563
📒 Files selected for processing (2)
minichain/mempool.pytests/test_protocol_hardening.py
There was a problem hiding this comment.
♻️ Duplicate comments (1)
minichain/mempool.py (1)
44-64:⚠️ Potential issue | 🟠 MajorStop exposing higher nonces after a gap.
After Line 62 advances
best_senderunconditionally, a sender with pending nonces5and7can still occupy two capped candidate slots even though7is not executable yet. That lets nonce gaps crowd out other senders' runnable heads.🛠️ Proposed fix
- for txs in snapshot.values(): - txs.sort(key=lambda t: t.nonce) + for sender, txs in snapshot.items(): + txs.sort(key=lambda t: t.nonce) + contiguous = [] + next_nonce = None + for tx in txs: + if next_nonce is None or tx.nonce == next_nonce: + contiguous.append(tx) + next_nonce = tx.nonce + 1 + else: + break + snapshot[sender] = contiguous🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@minichain/mempool.py` around lines 44 - 64, The selection loop currently allows non-executable higher-nonce transactions to occupy slots; fix by only considering a sender's head tx (snapshot[sender][0]) if its nonce equals the sender's next executable nonce (e.g., compare txs[0].nonce to the sender's current/next nonce from whatever store you have, such as self.get_next_nonce(sender) or self.account_nonces[sender]); skip that sender entirely for this selection round if there is a gap so higher nonces cannot be chosen as best_tx and crowd out other senders when building selected up to self.transactions_per_block.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@minichain/mempool.py`:
- Around line 44-64: The selection loop currently allows non-executable
higher-nonce transactions to occupy slots; fix by only considering a sender's
head tx (snapshot[sender][0]) if its nonce equals the sender's next executable
nonce (e.g., compare txs[0].nonce to the sender's current/next nonce from
whatever store you have, such as self.get_next_nonce(sender) or
self.account_nonces[sender]); skip that sender entirely for this selection round
if there is a gap so higher nonces cannot be chosen as best_tx and crowd out
other senders when building selected up to self.transactions_per_block.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 6466410a-5e80-4a0f-9a88-3774ba96d507
📒 Files selected for processing (1)
minichain/mempool.py
Addressed Issues:
This PR refactors the [Mempool] data structure from a linear [list] to a nested dictionary (
dict[sender][nonce] = tx), optimizing transaction management fromO(N)toO(1)._seen_tx_idsset. The nested dictionary inherently guarantees uniqueness, serving as a single source of truth.O(N)implementation was a potential DoS vulnerability, as adding a new transaction required a linear scan of every item in the pool. By switching toO(1)dictionary assignment, the node's inbound networking performance remains securely flat regardless of mempool size.Screenshots/Recordings:
TODO: If applicable, add screenshots or recordings that demonstrate the interface before and after the changes.
Additional Notes:
AI Usage Disclosure:
We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.
Check one of the checkboxes below:
I have used the following AI models and tools: TODO
Checklist
Summary by CodeRabbit
Refactor
Tests