Skip to content

refactor: simplify mempool to sorted queue and fix tx removal semantics#60

Merged
Zahnentferner merged 3 commits intoStabilityNexus:mainfrom
SIDDHANTCOOKIE:complex-architecture-cleanup
Mar 15, 2026
Merged

refactor: simplify mempool to sorted queue and fix tx removal semantics#60
Zahnentferner merged 3 commits intoStabilityNexus:mainfrom
SIDDHANTCOOKIE:complex-architecture-cleanup

Conversation

@SIDDHANTCOOKIE
Copy link
Copy Markdown
Contributor

@SIDDHANTCOOKIE SIDDHANTCOOKIE commented Mar 14, 2026

Addressed Issues:

Fixes

  • Replace mempool nonce-bucket structure with a simple sorted queue.
  • Allow same (sender, nonce) transaction replacement instead of hard rejection.
  • Keep get_transactions_for_block() read-only and enforce per-block transaction cap.
  • Remove mempool transactions only after successful chain.add_block(...).
  • Remove confirmed transactions by both tx_id and (sender, nonce); rebuild seen-set for consistency.
  • Update protocol-hardening tests for sorted/capped selection, nonce replacement, and removal correctness.

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:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: TODO

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions
  • If applicable, I have made corresponding changes or additions to the documentation
  • If applicable, I have made corresponding changes or additions to tests
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

  • New Features

    • CLI now validates recipient format and positive amounts with clear error messages.
    • Validator utility added for receiver address checks.
  • Bug Fixes

    • Mempool now uses deterministic global ordering and enforces a per-block transaction cap.
    • Mining only includes transactions validated as mineable; stale transactions are removed and mined transactions purged.
    • New incoming transaction with same sender+nonce replaces existing pending one.
    • Nonce handling synchronized with chain state (removed mutable counter).
  • Tests

    • Tests updated to reflect selection, replacement, capping, and removal behaviors.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 14, 2026

Walkthrough

Refactors the mempool to a flat, timestamp-sorted transaction queue with a capped selection API; updates CLI/mining to use new mempool API and chain-based nonces; tightens receiver/amount validation in p2p/CLI; adds Mempool.remove_transactions; tests adapted to new behavior.

Changes

Cohort / File(s) Summary
Mempool Core Refactoring
minichain/mempool.py
Replaced per-sender nonce queues with a flat _pending_txs list and TRANSACTIONS_PER_BLOCK; constructor gained transactions_per_block; add_transaction replaces same-sender/nonce or appends; get_transactions_for_block() is read-only and returns a deterministically sorted, capped subset; added remove_transactions and updated __len__. Removed per-sender helpers.
CLI / Main Integration
main.py
Removed nonce_counter from cli_loop signature; CLI reads nonce from chain.state; send flow validates receiver and amount; mining uses mempool.get_transactions_for_block() (mine only mineable txs), removes mined txs via mempool.remove_transactions(...); switched block message parsing to dict.get.
P2P Validation
minichain/p2p.py, minichain/validators.py
Added is_valid_receiver validator (regex for 40- or 64-hex) and enforced positive amount and receiver format in _validate_transaction_payload.
Tests
tests/test_protocol_hardening.py
Renamed test class and tests to reflect new mempool semantics; updated helpers and assertions to use get_transactions_for_block() (no state arg); added tests for replacement-by-same-nonce and removal-by-(sender,nonce) when tx_id differs; validate deterministic sorting and block capping behavior.

Sequence Diagram(s)

sequenceDiagram
  participant CLI as CLI
  participant Mempool as Mempool
  participant Miner as Miner
  participant Chain as Chain
  participant Network as Network

  CLI->>Mempool: submit transaction (validate receiver/amount)
  CLI->>Chain: read sender nonce (chain.state)
  CLI->>Network: broadcast tx
  CLI->>Miner: request mine
  Miner->>Mempool: get_transactions_for_block()
  Mempool-->>Miner: selected txs (sorted, capped)
  Miner->>Chain: propose block(with txs)
  Chain-->>Miner: block accepted
  Miner->>Mempool: remove_transactions(selected txs)
  Miner->>Network: broadcast new block
  Network->>Chain: peers receive & validate block
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

Python Lang

Suggested reviewers

  • Zahnentferner

Poem

🐰 I hopped through queues where nonces used to brawl,
Flattened timestamps, capped the lot—no more stall.
I nibble stale txs, keep the freshest in sight,
Blocks crunch like carrots, then vanish from night. 🥕

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the two main changes: refactoring the mempool to use a sorted queue and fixing transaction removal semantics.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

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

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

Caution

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

⚠️ Outside diff range comments (1)
main.py (1)

52-67: ⚠️ Potential issue | 🟠 Major

Mining can get stuck retrying stale-nonce transactions.

Line 52 takes a capped queue without state-aware filtering; if any selected tx has an outdated nonce, Line 65 rejects the block, and because Line 67 only removes on success, the same stale set can be retried indefinitely.

Suggested mitigation in this function
     if chain.add_block(mined_block):
         logger.info("✅ Block #%d mined and added (%d txs)", mined_block.index, len(pending_txs))
         mempool.remove_transactions(pending_txs)
         chain.state.credit_mining_reward(miner_pk)
         return mined_block
     else:
         logger.error("❌ Block rejected by chain")
+        # Drop transactions that are definitely stale against current chain nonce.
+        stale = [
+            tx
+            for tx in pending_txs
+            if tx.nonce < chain.state.get_account(tx.sender).get("nonce", 0)
+        ]
+        if stale:
+            mempool.remove_transactions(stale)
         return None
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@main.py`:
- Around line 151-153: The _is_valid_receiver function duplicates regex logic
elsewhere; extract its regex into a shared validator utility (e.g., create or
add to a validators module) and export a single function name like
is_valid_receiver; replace the local _is_valid_receiver definition in main.py
and the duplicate in minichain/p2p.py to import and call that shared
is_valid_receiver function, and update any references to use the exported name
so both modules rely on the same implementation and regex constant.
🪄 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: 876a8b6c-6178-4d2d-ba07-46d5abee7b0c

📥 Commits

Reviewing files that changed from the base of the PR and between cf4c09a and a8e8718.

📒 Files selected for processing (2)
  • main.py
  • minichain/p2p.py

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)
main.py (1)

170-267: 🧹 Nitpick | 🔵 Trivial

Refactor cli_loop into command handlers to reduce complexity.

Line 170 still carries high branch/statement complexity; splitting each command path into focused handlers will improve readability and testability.

♻️ Minimal refactor direction
+async def _handle_send(parts, sk, pk, chain, mempool, network):
+    ...
+
+async def _handle_mine(pk, chain, mempool, network):
+    ...
+
+COMMAND_HANDLERS = {
+    "send": _handle_send,
+    "mine": _handle_mine,
+    ...
+}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@main.py` around lines 170 - 267, The cli_loop function is too complex;
extract each command branch into small focused handler functions (e.g.,
handle_balance(chain, pk), handle_send(sk, pk, chain, mempool, network, parts),
handle_mine(chain, mempool, pk, network), handle_peers(network),
handle_connect(network, parts), handle_address(pk), handle_chain(chain),
handle_help()) and update cli_loop to just parse input and dispatch to these
handlers; ensure handlers preserve current behavior and return any control
signals needed (e.g., whether to break the loop), keep async for handlers that
await (send, mine, connect), and reference the existing symbols Transaction,
is_valid_receiver, mine_and_process_block, chain.state, mempool.add_transaction,
network.broadcast_transaction, and network.broadcast_block so logic and
side-effects remain identical.
🤖 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 `@main.py`:
- Around line 170-267: The cli_loop function is too complex; extract each
command branch into small focused handler functions (e.g., handle_balance(chain,
pk), handle_send(sk, pk, chain, mempool, network, parts), handle_mine(chain,
mempool, pk, network), handle_peers(network), handle_connect(network, parts),
handle_address(pk), handle_chain(chain), handle_help()) and update cli_loop to
just parse input and dispatch to these handlers; ensure handlers preserve
current behavior and return any control signals needed (e.g., whether to break
the loop), keep async for handlers that await (send, mine, connect), and
reference the existing symbols Transaction, is_valid_receiver,
mine_and_process_block, chain.state, mempool.add_transaction,
network.broadcast_transaction, and network.broadcast_block so logic and
side-effects remain identical.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 36e82b5f-d1b2-428d-bc63-9ec412793dcd

📥 Commits

Reviewing files that changed from the base of the PR and between a8e8718 and 38bbde7.

📒 Files selected for processing (3)
  • main.py
  • minichain/p2p.py
  • minichain/validators.py

@Zahnentferner Zahnentferner merged commit 715a904 into StabilityNexus:main Mar 15, 2026
1 check passed
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.

2 participants