-
Notifications
You must be signed in to change notification settings - Fork 2.5k
mempool: add missing orphan handling for mempool v2 #2440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Roasbeef
wants to merge
5
commits into
btcsuite:truc-mempool-v2-new
Choose a base branch
from
Roasbeef:truc-mempool-v2-new-2
base: truc-mempool-v2-new
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
mempool: add missing orphan handling for mempool v2 #2440
Roasbeef
wants to merge
5
commits into
btcsuite:truc-mempool-v2-new
from
Roasbeef:truc-mempool-v2-new-2
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this commit, we fix the OrphanManager.ProcessOrphans method to accept the parent transaction object instead of just its hash. This change is necessary because orphans are indexed by the outpoints they spend, not by parent transaction hashes in the orphan graph. The previous implementation attempted to find the parent in the orphan graph, but this was incorrect because the parent transaction has just been accepted into the main mempool and is not itself an orphan. The correct approach is to iterate through each output of the parent transaction and check if any orphans spend those specific outpoints using the spentBy index. This fix enables proper orphan promotion when a parent transaction is accepted to the mempool, which is critical for the orphan handling workflow.
In this commit, we complete the implementation of all remaining stubbed methods in TxMempoolV2, removing the panic placeholders and providing full functionality for orphan management and mempool querying. The orphan management methods (RemoveOrphan, RemoveOrphansByTag, and ProcessOrphans) now properly delegate to the OrphanManager with appropriate locking for concurrent access safety. ProcessOrphans wraps the acceptance logic and correctly promotes orphans when their parents become available. The query methods (FetchTransaction, TxHashes, TxDescs, MiningDescs, RawMempoolVerbose, and CheckSpend) provide read-only access to mempool state. These methods use the transaction graph's iterator and indexes for efficient lookups, and all properly acquire read locks to ensure thread safety. CheckMempoolAcceptance is now implemented as a simple wrapper around the internal validation pipeline, using parameters appropriate for the testmempoolaccept RPC (isNew=true, rateLimit=false, rejectDupOrphans=true). We also update the OrphanTxManager interface to include the RemoveOrphan and RemoveOrphansByTag methods, and fix the processOrphansLocked internal method to properly handle missing parents by checking the return value correctly. A small helper method GetSpendingTx is added to TxGraph to support the CheckSpend implementation with O(1) lookup performance.
In this commit, we enhance the orphan processing tests by adding helper methods for common test scenarios and completing the orphan promotion test that was previously incomplete. The new helper methods include expectEarlyValidation for tests that only need the initial validation steps, and expectOrphanPromotion which sets up the complete mock chain for promoting an orphan transaction when its parent arrives. These helpers reduce duplication and make tests more readable. The TestProcessTransactionWithOrphans test now fully verifies the orphan promotion workflow. When a parent transaction is added to the mempool, orphan children are automatically promoted if they become valid. The test confirms both transactions end up in the main pool and the child is removed from the orphan pool. We also fix the TestDuplicateOrphan test to properly set up early validation mocks before the duplicate check occurs.
In this commit, we complete the test implementations for the basic mempool query methods that were previously marked with TODO comments. The completed tests verify Count, HaveTransaction, IsTransactionInPool, and IsOrphanInPool methods. Each test now exercises both the success path (finding transactions) and the distinction between main pool and orphan pool lookups. The tests use the createTestMempoolV2 harness with proper mock setup to create realistic transaction chains and orphan scenarios. Key testing patterns implemented include creating unique transactions by building parent-child chains, using ProcessTransaction to add orphans to the orphan pool (since MaybeAcceptTransaction only returns missing parents without actually storing orphans), and verifying the correct separation of concerns between main pool and orphan pool query methods. We also remove the TestMempoolStubbedMethodsPanic test since all methods are now fully implemented and no longer contain panic stubs.
In this commit, we add a new test file dedicated to testing the mempool query methods with realistic transaction scenarios. The tests cover FetchTransaction, TxHashes, TxDescs, MiningDescs, RawMempoolVerbose, and CheckSpend. Each test verifies both empty mempool behavior and populated mempool scenarios with parent-child transaction relationships. Key test patterns include verifying that TxHashes returns all transaction hashes with proper deduplication, that TxDescs and MiningDescs return correctly formatted descriptors with accurate fee information, and that RawMempoolVerbose properly tracks dependency relationships between transactions in the mempool. The CheckSpend test verifies the spent output detection works correctly, confirming that the mempool can identify which transaction spends a given outpoint while correctly returning nil for unspent outputs. These tests use the established createTestMempoolV2 harness pattern with mock setup, ensuring consistency with the existing test suite.
Pull Request Test Coverage Report for Build 18237964546Details
💛 - Coveralls |
kmk142789
approved these changes
Nov 5, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Complete TxMempoolV2 Implementation
This PR completes the core implementation of TxMempoolV2, the new transaction graph-based mempool that will eventually replace the existing TxPool. All previously stubbed methods are now fully implemented and tested, bringing the new mempool to feature parity with the original implementation.
What's Included
The work in this PR focuses on completing the remaining pieces of the TxMempoolV2 implementation. This includes the orphan management methods that handle transactions with missing parents, the query methods used by RPC commands to inspect mempool state, and the validation method used by the testmempoolaccept RPC.
The orphan handling implementation required fixing how orphans are promoted when their parent transactions arrive. The previous approach incorrectly assumed parent transactions would be in the orphan graph, but parents are actually in the main mempool when promotion happens. The fix indexes orphans by the outpoints they spend, allowing efficient lookup when a parent's outputs become available.
All query methods now properly use the transaction graph's iterator and indexes for efficient access. Methods like RawMempoolVerbose correctly track dependency relationships between transactions, while CheckSpend leverages the graph's spentBy index for O(1) lookup performance. Thread safety is ensured throughout with appropriate read/write locking.
The test suite has been substantially expanded to cover the new functionality. Tests verify orphan promotion workflows, transaction counting, pool distinction between main and orphan pools, and all query method behaviors with realistic transaction chains. The stubbed method panic tests have been removed since all methods are now fully implemented.
Next Steps
With this implementation complete, the next major milestone is creating an equivalence testing framework that runs identical operations on both the old TxPool and new TxMempoolV2, comparing results to prove behavioral compatibility. This testing is critical for validating the migration path and ensuring no regressions.
Following successful equivalence testing, we'll add a feature flag to allow running btcd with the new mempool implementation, enabling broader testing in live environments before making the switch permanent.