wallet: Only broadcast resubmitted transactions that were added to the mempool #36482

pull achow101 wants to merge 5 commits into bitcoin:master from achow101:broadcasttx-if-new changing 5 files +163 −65
  1. achow101 commented at 9:48 PM on October 8, 2026: member

    As discussed in #36403, one of the issues with current rebroadcast behavior is that the rebroadcast begins an unconditional relay of a transaction to peers. While this is broadcast is limited to peers that have not previously been inv'd the transaction, there is still a privacy leak in this double relay of a transaction, especially so for transactions that are still in nodes' mempools.

    This PR changes the rebroadcast to only rebroadcast transactions if the transaction was not already present in our mempool. This provides some additional privacy while retaining intended rebroadcast behavior as transactions that have likely fallen out of nodes' mempools are the ones that actually need to be rebroadcast.

    The wallet rebroadcast tests are additionally refactored for isolation so that their behaviors can still be tested after this change. Additionally, a test for the loading resubmission was added, as well as a test for the new behavior.

  2. test: Refactor wallet_resendwallettransactions for test case isolation 2121d61323
  3. test: Test wallet unconfirmed resubmission on load 858be1c447
  4. node: Add option to relay txs only if they are added to mempool
    BroadcastTransaction takes different modes of broadcasting. Add one
    which will attempt to add a transaction to the mempool, and if that
    succeeds, broadcast the transaction to peers. If the transaction already
    exists in the mempool, it will not be rebroadcast.
    4446226ea9
  5. wallet: Only rebroadcast transactions that were re-added to the mempool 8aed98fef4
  6. test: Check that wallet txs already in the mempool are not rebroadcast 4dbd27f37b
  7. DrahtBot added the label Wallet on Oct 8, 2026
  8. DrahtBot commented at 9:48 PM on October 8, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK vasild
    Concept ACK polespinasa, ismaelsadeeq

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36540 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36540.svg"></sub> (mempool: remove time-based transaction expiry by ismaelsadeeq)
    • #36481 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36481.svg"></sub> (wallet, rpc, gui: Allow users to manually rebroadcast a wallet transaction by achow101)
    • #36398 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36398.svg"></sub> (wallet: only rebroadcast transactions originating from the wallet by mzumsande)
    • #36330 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36330.svg"></sub> (wallet: don't resubmit transactions under -privatebroadcast by instagibbs)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  9. polespinasa commented at 10:24 PM on October 8, 2026: member

    concept ACK

  10. in test/functional/wallet_resendwallettransactions.py:45 in 8aed98fef4
      41 | @@ -42,6 +42,19 @@ def mine_empty_block(self, node):
      42 |          block.solve()
      43 |          node.submitblock(block.serialize().hex())
      44 |  
      45 | +    def evict_mempool_transaction(self, conflict_utxo, node, wallet, indep_wallet):
    


    mzumsande commented at 3:32 PM on October 9, 2026:

    This replacement pattern with indep_utxo is just the mechanism that an attacker could do to probe for which nodes a given address belong to, so this doesn't appear to completely solve the receiver issue from #3828.

  11. murchandamus commented at 6:25 PM on October 21, 2026: member

    This PR introduces the new MEMPOOL_AND_BROADCAST_ADDED TxBroadcast case, and uses it when the wallet resubmits transactions to the node’s mempool instead of the transaction being rebroadcast to all peers that it hadn’t been inventoried. The tests check that the rebroadcast happens when the transaction is added to the mempool and doesn’t happen when the transaction was already present. This is an obvious privacy improvement.

    The only open question I have at the moment is whether the combination of using MEMPOOL_NO_BROADCAST on wallet loading and then MEMPOOL_AND_BROADCAST_ADDED on the wallet’s rebroadcast could prevent a transaction from being resubmitted to the network. E.g., if a wallet submits a low-feerate transaction, then the node is turned off for some period during which a feerate spike causes most of the network to drop the transaction, then the node is turned back on, wouldn’t this mean that the wallet’s transaction is not rebroadcast at all?

    Also, I’m not sure whether using MEMPOOL_AND_BROADCAST_ADDED during wallet loading would fix that, if the node still had the transaction in its mempool from before going offline.

  12. fametrano commented at 7:43 AM on October 25, 2026: none

    After a restart, this branch does not announce a wallet transaction that peers already received while our own mempool keeps it, so the answer to murch's question is yes.

    I built 4dbd27f37b and its base f739c7876b and ran a regtest script that connects a fresh P2PInterface peer, moves mocktime 36 hours ahead so that MaybeResendWalletTxs runs, and sends an unrelated transaction as a barrier. A wallet transaction still in the mempool is announced on base and not on the branch, as intended. One that expired and was accepted again is announced on both. One rejected as non-final is announced on neither.

    With -persistmempool=0, the load-time resubmission puts the transaction back without broadcast. With mempool.dat, it comes back already out of the unbroadcast set, because a peer requested it before shutdown. The branch announced nothing in either case, and with -persistmempool=0 nothing over two periodic resubmissions and six ReattemptInitialBroadcast windows; base announced it at each resubmission. The same holds whenever our mempool keeps a transaction that peers dropped, until it expires from our mempool (two weeks by default). A node offline for days now takes its own mempool as a proxy for everyone else's. test_resubmit_on_load stops before the first periodic resubmission; extending it would pin whichever behaviour is intended.

    Smaller: the comment above ResubmitWalletTransactions still names MEMPOOL_AND_BROADCAST_TO_ALL for the periodic path, and "resubmit N unconfirmed transactions" now counts transactions not relayed. I can share the script as a gist.

  13. in src/node/transaction.cpp:65 in 4446226ea9


    polespinasa commented at 5:17 PM on November 13, 2026:

    in 4446226ea9f835396c16fdfd388dd5e69c6174bc node: Add option to relay txs only if they are added to mempool

    $ git diff
    diff --git a/src/node/transaction.cpp b/src/node/transaction.cpp
    index 3be8919b16..23d394c7aa 100644
    --- a/src/node/transaction.cpp
    +++ b/src/node/transaction.cpp
    @@ -62,7 +62,8 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
                 // There's already a transaction in the mempool with this txid. Don't
                 // try to submit this transaction to the mempool (since it'll be
                 // rejected as a TX_CONFLICT), but do attempt to reannounce the mempool
    -            // transaction if broadcast_method is not TxBroadcast::MEMPOOL_NO_BROADCAST.
    +            // transaction if broadcast_method is not TxBroadcast::MEMPOOL_NO_BROADCAST
    +            // or TxBroadcast::MEMPOOL_AND_BROADCAST_ADDED.
                 //
                 // The mempool transaction may have the same or different witness (and
                 // wtxid) as this transaction. Use the mempool's wtxid for reannouncement.
    
    
  14. in test/functional/wallet_resendwallettransactions.py:195 in 858be1c447
     190 | +        broadcast_txid = self.default_wallet.sendtoaddress(self.default_wallet.getnewaddress(), 1)
     191 | +        broadcast_wtxid = self.default_wallet.gettransaction(broadcast_txid)["wtxid"]
     192 | +        peer.wait_for_broadcast([broadcast_wtxid])
     193 | +        invs = peer.get_invs()
     194 | +        assert int(txid1, 16) not in invs
     195 | +        assert int(txid2, 16) not in invs
    


    polespinasa commented at 6:20 PM on November 13, 2026:

    in 858be1c4477978af54d8db2d7f5f5f615d76b318 test: Test wallet unconfirmed resubmission on load

    This asserts are wrong, in invs we have wtxid so they will never fail. Easy way to test it is changing txid1 or txid2 with broadcast_txid it will pass anyway, but if you use broadcast_wtxid the test will fail.


    vasild commented at 8:25 AM on November 20, 2026:

    For me it fails with both broadcast_txid and broadcast_wtxid. They are equal. Here is some debug output:

             invs = peer.get_invs()
             assert int(txid1, 16) not in invs
             assert int(txid2, 16) not in invs
    +        print(f"txid1:  {int(txid1, 16)}")
    +        print(f"wtxid1: {int(self.nodes[0].get_wallet_rpc("loading_resubmit").gettransaction(txid1)["wtxid"], 16)}")
    +        print(f"txid2:  {int(txid2, 16)}")
    +        print(f"wtxid2: {int(self.default_wallet.gettransaction(txid2)["wtxid"], 16)}")
    +        print(f"broadcast_txid:  {int(broadcast_txid, 16)}")
    +        print(f"broadcast_wtxid: {int(broadcast_wtxid, 16)}")
    +        print(f"invs[]: {invs}")
    

    output:

    txid1:  50556715902934661947412272260573146809491106239685953740317537168349755949630
    wtxid1: 46464095935464499678726971305872763512167833087509151534719235003950002890391
    txid2:  66017971935110931380568664541653393034732726126770766062901131324024597221773
    wtxid2: 30565878341147032154661473719272837167420234698891461705525763184751019784320
    broadcast_txid:  29166901737146472027258190114347233193678720285132270983418691067319012104802
    broadcast_wtxid: 29166901737146472027258190114347233193678720285132270983418691067319012104802
    invs[]: [29166901737146472027258190114347233193678720285132270983418691067319012104802]
    

    I think it is more robust and easier to reason about if we test that neither one of the transaction id or witness transaction id are in the invs[].

  15. in test/functional/wallet_resendwallettransactions.py:111 in 4dbd27f37b
     133 | +        with node.assert_debug_log(expected_msgs=[], unexpected_msgs=["resubmit 1 unconfirmed transactions"]):
     134 | +            twelve_hrs = 12 * 60 * 60
     135 | +            two_min = 2 * 60
     136 | +            node.bumpmocktime(twelve_hrs - two_min)
     137 | +            node.mockscheduler(60)  # Tell scheduler to call MaybeResendWalletTxs now
     138 | +            assert_equal(int(wtxid, 16) in peer_second.get_invs(), False)
    


    vasild commented at 6:11 AM on November 20, 2026:

    Consider also adding:

    assert_equal(int(txid, 16) in peer_second.get_invs(), False)
    

    because neither txid nor wtxid should be in peer_second and both txid wtxid could end in the result of get_invs():

    https://github.com/bitcoin/bitcoin/blob/079437cca7a8e2997a6dcd83ad38350014352544/test/functional/test_framework/p2p.py#L991-L994

    (hint from #36482 (review))

  16. in test/functional/wallet_resendwallettransactions.py:48 in 4dbd27f37b
      44 | +        node.submitblock(block.serialize().hex())
      45 | +
      46 | +    def evict_mempool_transaction(self, conflict_utxo, node, wallet, indep_wallet):
      47 | +        # Evict a transaction spending conflict_utxo by creating a conflict with it that pays a higher feerate
      48 | +        indep_utxo = indep_wallet.listunspent()[0]
      49 | +        psbt = indep_wallet.send(outputs=[{indep_wallet.getnewaddress(): 0.00001}], psbt=True, fee_rate=30, inputs=[conflict_utxo, indep_utxo], solving_data={"descriptors":[conflict_utxo["desc"]]})["psbt"]
    


    vasild commented at 10:05 AM on November 20, 2026:

    This assumes that the transaction to be evicted, the one that spends conflict_utxo, has a fee rate less than 30. That is a bit obscure and fragile.

    Consider getting the feerate of the transaction to be evicted and using a higher value than that (plus two more asserts - that the to-be-evicted transaction is in the mempool when the function starts and is absent at the end):

    --- i/test/functional/wallet_resendwallettransactions.py
    +++ w/test/functional/wallet_resendwallettransactions.py
    @@ -9,5 +9,8 @@ from test_framework.blocktools import (
         create_block,
     )
    -from test_framework.messages import DEFAULT_MEMPOOL_EXPIRY_HOURS
    +from test_framework.messages import (
    +    COIN,
    +    DEFAULT_MEMPOOL_EXPIRY_HOURS,
    +)
     from test_framework.p2p import P2PTxInvStore
     from test_framework.test_framework import BitcoinTestFramework
    @@ -45,12 +48,20 @@ class ResendWalletTransactionsTest(BitcoinTestFramework):
         def evict_mempool_transaction(self, conflict_utxo, node, wallet, indep_wallet):
             # Evict a transaction spending conflict_utxo by creating a conflict with it that pays a higher feerate
    +        evict_txid = node.gettxspendingprevout([{"txid": conflict_utxo["txid"], "vout": conflict_utxo["vout"]}])[0]["spendingtxid"]
    +        assert evict_txid in node.getrawmempool()
    +        evict_tx_mempool_entry = node.getmempoolentry(evict_txid)
    +        evict_tx_feerate = evict_tx_mempool_entry["fees"]["base"] * COIN / evict_tx_mempool_entry["vsize"]
    +
             indep_utxo = indep_wallet.listunspent()[0]
    -        psbt = indep_wallet.send(outputs=[{indep_wallet.getnewaddress(): 0.00001}], psbt=True, fee_rate=30, inputs=[conflict_utxo, indep_utxo], solving_data={"descriptors":[conflict_utxo["desc"]]})["psbt"]
    +        tx1_feerate = evict_tx_feerate * 2
    +        psbt = indep_wallet.send(outputs=[{indep_wallet.getnewaddress(): 0.00001}], psbt=True, fee_rate=tx1_feerate, inputs=[conflict_utxo, indep_utxo], solving_data={"descriptors":[conflict_utxo["desc"]]})["psbt"]
             psbt = wallet.walletprocesspsbt(psbt)["psbt"]
             txid1 = node.sendrawtransaction(node.finalizepsbt(psbt)["hex"])
             # Create a conflict with just indep_utxo to evict the previous transaction so that the original can be rebroadcast
    -        r = indep_wallet.send(outputs=[{indep_wallet.getnewaddress(): 0.00001}], fee_rate=60, inputs=[indep_utxo], add_to_wallet=False)
    +        tx2_feerate = tx1_feerate * 2
    +        r = indep_wallet.send(outputs=[{indep_wallet.getnewaddress(): 0.00001}], fee_rate=tx2_feerate, inputs=[indep_utxo], add_to_wallet=False)
             txid2 = node.sendrawtransaction(r["hex"])
             mempool = node.getrawmempool()
    +        assert evict_txid not in node.getrawmempool()
             assert txid1 not in mempool
             assert txid2 in mempool
    
  17. in test/functional/test_framework/p2p.py:1006 in 4dbd27f37b
     998 | @@ -999,6 +999,13 @@ def wait_for_broadcast(self, txns, *, timeout=60):
     999 |          # Flush messages and wait for the getdatas to be processed
    1000 |          self.sync_with_ping()
    1001 |  
    1002 | +    def wait_for_single_broadcast(self, tx, *, timeout=60):
    1003 | +        # Wait until invs have been received (and getdatas sent) for each txid.
    1004 | +        self.wait_until(lambda: int(tx, 16) in self.tx_invs_received.keys(), timeout=timeout)
    1005 | +        # Flush messages and wait for the getdatas to be processed
    1006 | +        self.sync_with_ping()
    


    vasild commented at 11:23 AM on November 20, 2026:

    The newly added wait_for_single_broadcast() is almost identical to the existent wait_for_broadcast(). I wonder if the existent function can be modified to use >= instead of == when comparing the sets. This would allow the input list of txids to be a subset of the tx_invs_received.

    Or if that is going to break something then take a parameter to control whether to use == or >=.

  18. vasild approved
  19. vasild commented at 11:59 AM on November 20, 2026: contributor

    ACK 4dbd27f37b52e49a8d392bd2ee6713ae7dcf23df modulo #36482 (review)

    As for the @murchandamus scenario:

    • a transaction is submitted, ends up in the mempool and in the network
    • the node is shut down
    • fee pressure causes the network to forget about the transaction
    • the fee pressure eases on the network
    • the node is started
      • at wallet load it will not broadcast the transaction
      • 12-36h later it will not broadcast either because the transaction is in its mempool

    #36481 would help in that case to manually rebroadcast the transaction.

    Some non-blocker comments below.

    <details> <summary>Show Signature</summary>

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA256
    
    ACK 4dbd27f37b52e49a8d392bd2ee6713ae7dcf23df modulo [#36482 (review)](/bitcoin-bitcoin/36482/#discussion_r4567017959)
    
    As for the [@murchandamus](/bitcoin-bitcoin/contributor/murchandamus/) scenario:
    
    * a transaction is submitted, ends up in the mempool and in the network
    * the node is shut down
    * fee pressure causes the network to forget about the transaction
    * the fee pressure eases on the network
    * the node is started
      * at wallet load it will not broadcast the transaction
      * 12-36h later it will not broadcast either because the transaction is in its mempool
    
    [#36481](/bitcoin-bitcoin/36481/) would help in that case to manually rebroadcast the transaction.
    
    Some non-blocker comments below.
    -----BEGIN PGP SIGNATURE-----
    
    iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmsANeEbFIAAAAAABAAO
    bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/hYEf/AydYNVqVYASuY8u3fQ9
    yKZvpNBJvdKWWz5oN0eRZPsH7HQ1nDS/P77bAWyLA6PmwJcoTFh/QEzVl9Oq0O57
    9kscEvduOYL1UW4hoOBb9WEdqoUTLywn3OxS5UqcwFGIHs63ErFYn9PRD2/HzO6X
    jOmTLlpI1dUS7gQbnrO1Cqy8jLBP0CFPqS/R9tdsi/O1bJ8dwAxGxLIqEeqmFpLD
    0lD+m0ufj5A6LfFCyeJhhJ7BrlxpREqQwH5vsUAiExk15vSQ2cQH7AOrY4FXIDEk
    Zxb5co8+xxFdYAGo3c0PkaMRwGsUJ5+tobT9qX+FPeskX7fFl0rlO/HnXA5mMjp7
    Lfg+uAkOWLBiN+NYnQBMNP+9peArHEAtVR8oTRp5W7BCQJi6QF2vEqXDdiEFFF0h
    BzEA+7unMHgxW/6X/Fz9YXCOdrLBDCfhnyyCP4g9hZIxxanA7aG5MIX3x9UlGF3K
    HaOyxlzLUTrNPM/O+qrVUQY5pdHD/9B+kt+eXcmDV4CEYGcLl14Hj3ZL+VMsuD4T
    fwQoxSwTmlkE59I0i4RMbuR6axwUiApHFpnONqGhpuY+vquYRfdAmW2Kq8NCKkb5
    8sTE6dhFVp8xt0+38g5XjppkZRQVhBRbZYcV4Rb3ZAihg65FZz5/p/IXYMPNixzA
    8J83rgRSg6y10HG0wTjFUWNvwQOmrOL5PqSlb8x+o3D2/XSRiB9Q5334PBc/UO29
    4P121/Q+ZCEpVnOclJUkDj19AJQRYicpoRGWslCUEfJKOi41Fvw2kk7Y5T2jr/3m
    TlMz97FEKanhAQgk5opclA6bI+8Lz8CIxokylWV9T8Yy21V17KeeAziTtsaut/Bv
    tq1BWqF66OnQTScS4e/cmEjRFY2zmVkslBpO0iT1UTlkHlThmEYzuKQQ6aSwhQJk
    W9ddYGKAr7kH7OBPj5KpkL4lMln6Pur83joGpfq8P4jP9ACxnnel0VkySJnkLDcN
    zGWVuDNiboSshE+CMNYf3tTS707C3arcWcVDNFwpviby1DvWWMJQsPgx4+9Y+aSE
    8ao88/766Hhs7THwZLgkSfgHh0pcqYdl3Xa8UyKK2T2akqdm/F6GrNYOVew/i+cZ
    6lZaaMWb5XzrXPmNFY8GY6iCvKyzi6mzPYbBg1RUnfN5aI3LwM7ZyecYptdq2ZDX
    6lWCmc2uELhDtGGYUeeK2o9GdSlTN2lOTdG1GnfMRGN3IpAW9pGQRFGCJLx/Jn16
    nPbwgS8hhXkapBxM3QhS3l78bZjNOFRnTcDxWKRgDRZabHmNFJ6xM5Z0LbnGwCXC
    ige6oUhDsF+csP/ZQ1De6bAtaPcv1OGesPLjss92kV8aDX79/TnhYuuOS3LNmja6
    zlE=
    =jYfY
    -----END PGP SIGNATURE-----
    

    vasild's public key is on openpgp.org

    </details>

  20. DrahtBot requested review from polespinasa on Nov 20, 2026
  21. ismaelsadeeq commented at 4:32 PM on November 25, 2026: member

    Concept ACK. Worth noting that a node with a small mempool can still resubmit and rebroadcast a tx while others have it and won't relay it, so it still has the same leak even with this change. Adding a separate option to disable rebroadcast, ideally off by default, would be useful. That way, it's opt-in for advanced users who need it.

  22. ismaelsadeeq commented at 4:33 PM on November 25, 2026: member

    Post-Coredev, I discussed the issue with a few others in BRW, and made a post about what I understand thus far. https://delvingbitcoin.org/t/exploiting-wallet-rebroadcast-to-link-addresses-to-node-ips-and-more/3123


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-28 05:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me