Skip to content

wallet: mempool replay can deliver a child before its parent and miss the spend #8208

Closed

Description

@PastaPastaPasta

Problem

When the wallet reads the mempool, it can record an unconfirmed payment to itself but miss the unconfirmed transaction that spends it. The spent output then shows as unspent until the spend is mined.

ChainImpl::requestMempoolTransactions() in src/node/interfaces.cpp walks mapTx through its default index, which is hashed by txid, so a child can be delivered before its parent:

for (const CTxMemPoolEntry& entry : m_node.mempool->mapTx) {
    notifications.transactionAddedToMempool(entry.GetSharedTx(), /*nAcceptTime=*/0);
}

If the child pays only external addresses, the wallet does not recognise it when it arrives first: none of its outputs are ours, and its input is not known to be ours until the parent is in mapWallet. Nothing revisits the child after the parent is added.

This is reached on every path that replays the mempool:

  • wallet load (CWallet::postInitProcess());
  • any rescan without an end height (ScanForWalletTransactions(), used by rescanblockchain, importdescriptors and the wallet interface's rescans).

How to reproduce

  1. Have two wallets from the same seed, or one wallet that is unloaded for step 2.
  2. With wallet A not loaded, receive an unconfirmed payment to one of its addresses, and spend that output with wallet B to an external address. Keep both transactions in the mempool.
  3. Load wallet A (or rescan it).

Depending on the two txids, wallet A lists the payment as an unspent output although the mempool already spends it.

Fix

Bitcoin Core fixed the order in bitcoin#28391: requestMempoolTransactions() iterates CTxMemPool::entryAll(), which returns entries sorted by ancestor count, parents first. bitcoin#29179 added the regression tests (wallet_rescan_unconfirmed.py and a case in wallet_import_rescan.py: a parent with an IsFromMe child in the mempool), and bitcoin#29019, which would have removed the sorting, was closed for this reason.

Dash has neither bitcoin#28391 nor bitcoin#29179. Backporting both fixes this for every caller.

Found while reviewing #8068, whose new rescanFromTime() uses the same replay.

Activity

  1. added a commit that references this issue on Nov 20, 2026
    35032ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions