Skip to content

Conversation

@Roasbeef
Copy link
Member

@Roasbeef Roasbeef commented Oct 4, 2025

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.

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.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 18237964546

Details

  • 140 of 184 (76.09%) changed or added relevant lines in 3 files are covered.
  • 13 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.07%) to 56.189%

Changes Missing Coverage Covered Lines Changed/Added Lines %
mempool/mempool_v2.go 117 161 72.67%
Files with Coverage Reduction New Missed Lines %
btcutil/gcs/gcs.go 1 81.25%
database/ffldb/blockio.go 4 88.81%
mempool/mempool_v2.go 8 80.24%
Totals Coverage Status
Change from base Build 18237957498: 0.07%
Covered Lines: 33389
Relevant Lines: 59423

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants