Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2023-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.6"""7Test that wallet correctly tracks transactions that have been conflicted by blocks, particularly during reorgs.8"""10from decimal import Decimal12from test_framework.test_framework import BitcoinTestFramework13from test_framework.util import (14assert_equal,15)17class TxConflicts(BitcoinTestFramework):18def set_test_params(self):19self.num_nodes = 321def skip_test_if_missing_module(self):22self.skip_if_no_wallet()24def get_utxo_of_value(self, from_tx_id, search_value):25return next(tx_out["vout"] for tx_out in self.nodes[0].gettransaction(from_tx_id)["details"] if tx_out["amount"] == Decimal(f"{search_value}"))27def run_test(self):28"""29The following tests check the behavior of the wallet when30transaction conflicts are created. These conflicts are created31using raw transaction RPCs that double-spend UTXOs and have more32fees, replacing the original transaction.33"""35self.test_block_conflicts()36self.test_mempool_conflict()37self.test_mempool_and_block_conflicts()38self.test_descendants_with_mempool_conflicts()40def test_block_conflicts(self):41self.log.info("Send tx from which to conflict outputs later")42txid_conflict_from_1 = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))43txid_conflict_from_2 = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), Decimal("10"))44self.generate(self.nodes[0], 1)45self.sync_blocks()47self.log.info("Disconnect nodes to broadcast conflicts on their respective chains")48self.disconnect_nodes(0, 1)49self.disconnect_nodes(2, 1)51self.log.info("Create transactions that conflict with each other")52output_A = self.get_utxo_of_value(from_tx_id=txid_conflict_from_1, search_value=10)53output_B = self.get_utxo_of_value(from_tx_id=txid_conflict_from_2, search_value=10)55# First create a transaction that consumes both A and B outputs.56#57# | tx1 | -----> | | | |58# | AB_parent_tx | ----> | Child_Tx |59# | tx2 | -----> | | | |60#61inputs_tx_AB_parent = [{"txid": txid_conflict_from_1, "vout": output_A}, {"txid": txid_conflict_from_2, "vout": output_B}]62tx_AB_parent = self.nodes[0].signrawtransactionwithwallet(self.nodes[0].createrawtransaction(inputs_tx_AB_parent, {self.nodes[0].getnewaddress(): Decimal("19.99998")}))64# Secondly, create two transactions: One consuming output_A, and another one consuming output_B65#66# | tx1 | -----> | Tx_A_1 |67# ----------------68# | tx2 | -----> | Tx_B_1 |69#70inputs_tx_A_1 = [{"txid": txid_conflict_from_1, "vout": output_A}]71inputs_tx_B_1 = [{"txid": txid_conflict_from_2, "vout": output_B}]72tx_A_1 = self.nodes[0].signrawtransactionwithwallet(self.nodes[0].createrawtransaction(inputs_tx_A_1, {self.nodes[0].getnewaddress(): Decimal("9.99998")}))73tx_B_1 = self.nodes[0].signrawtransactionwithwallet(self.nodes[0].createrawtransaction(inputs_tx_B_1, {self.nodes[0].getnewaddress(): Decimal("9.99998")}))75self.log.info("Broadcast conflicted transaction")76txid_AB_parent = self.nodes[0].sendrawtransaction(tx_AB_parent["hex"])77self.generate(self.nodes[0], 1, sync_fun=self.no_op)79# Now that 'AB_parent_tx' was broadcast, build 'Child_Tx'80output_c = self.get_utxo_of_value(from_tx_id=txid_AB_parent, search_value=19.99998)81inputs_tx_C_child = [({"txid": txid_AB_parent, "vout": output_c})]83tx_C_child = self.nodes[0].signrawtransactionwithwallet(self.nodes[0].createrawtransaction(inputs_tx_C_child, {self.nodes[0].getnewaddress() : Decimal("19.99996")}))84tx_C_child_txid = self.nodes[0].sendrawtransaction(tx_C_child["hex"])85self.generate(self.nodes[0], 1, sync_fun=self.no_op)87self.log.info("Broadcast conflicting tx to node 1 and generate a longer chain")88conflicting_txid_A = self.nodes[1].sendrawtransaction(tx_A_1["hex"])89self.generate(self.nodes[1], 4, sync_fun=self.no_op)90conflicting_txid_B = self.nodes[1].sendrawtransaction(tx_B_1["hex"])91self.generate(self.nodes[1], 4, sync_fun=self.no_op)93self.log.info("Connect nodes 0 and 1, trigger reorg and ensure that the tx is effectively conflicted")94self.connect_nodes(0, 1)95self.sync_blocks([self.nodes[0], self.nodes[1]])96conflicted_AB_tx = self.nodes[0].gettransaction(txid_AB_parent)97tx_C_child = self.nodes[0].gettransaction(tx_C_child_txid)98conflicted_A_tx = self.nodes[0].gettransaction(conflicting_txid_A)100self.log.info("Verify, after the reorg, that Tx_A was accepted, and tx_AB and its Child_Tx are conflicting now")101# Tx A was accepted, Tx AB was not.102assert conflicted_AB_tx["confirmations"] < 0103assert conflicted_A_tx["confirmations"] > 0105# Conflicted tx should have confirmations set to the confirmations of the most conflicting tx106assert_equal(-conflicted_AB_tx["confirmations"], conflicted_A_tx["confirmations"])107# Child should inherit conflicted state from parent108assert_equal(-tx_C_child["confirmations"], conflicted_A_tx["confirmations"])109# Check the confirmations of the conflicting transactions110assert_equal(conflicted_A_tx["confirmations"], 8)111assert_equal(self.nodes[0].gettransaction(conflicting_txid_B)["confirmations"], 4)113self.log.info("Now generate a longer chain that does not contain any tx")114# Node2 chain without conflicts115self.generate(self.nodes[2], 15, sync_fun=self.no_op)117# Connect node0 and node2 and wait reorg118self.connect_nodes(0, 2)119self.sync_blocks()120conflicted = self.nodes[0].gettransaction(txid_AB_parent)121tx_C_child = self.nodes[0].gettransaction(tx_C_child_txid)123self.log.info("Test that formerly conflicted transaction are inactive after reorg")124# Former conflicted tx should be unconfirmed as it hasn't been yet rebroadcast125assert_equal(conflicted["confirmations"], 0)126# Former conflicted child tx should be unconfirmed as it hasn't been rebroadcast127assert_equal(tx_C_child["confirmations"], 0)128# Rebroadcast former conflicted tx and check it confirms smoothly129self.nodes[2].sendrawtransaction(conflicted["hex"])130self.generate(self.nodes[2], 1)131self.sync_blocks()132former_conflicted = self.nodes[0].gettransaction(txid_AB_parent)133assert_equal(former_conflicted["confirmations"], 1)134assert_equal(former_conflicted["blockheight"], 217)136def test_mempool_conflict(self):137self.nodes[0].createwallet("alice")138alice = self.nodes[0].get_wallet_rpc("alice")140bob = self.nodes[1]142self.nodes[2].send(outputs=[{alice.getnewaddress() : 25} for _ in range(3)])143self.generate(self.nodes[2], 1)145self.log.info("Test a scenario where a transaction has a mempool conflict")147unspents = alice.listunspent()148assert_equal(len(unspents), 3)149for tx in unspents:150assert_equal(tx["amount"], 25)152# tx1 spends unspent[0] and unspent[1]153raw_tx = alice.createrawtransaction(inputs=[unspents[0], unspents[1]], outputs=[{bob.getnewaddress() : 49.9999}])154tx1 = alice.signrawtransactionwithwallet(raw_tx)['hex']156# tx2 spends unspent[1] and unspent[2], conflicts with tx1157raw_tx = alice.createrawtransaction(inputs=[unspents[1], unspents[2]], outputs=[{bob.getnewaddress() : 49.99}])158tx2 = alice.signrawtransactionwithwallet(raw_tx)['hex']160# tx3 spends unspent[2], conflicts with tx2161raw_tx = alice.createrawtransaction(inputs=[unspents[2]], outputs=[{bob.getnewaddress() : 24.9899}])162tx3 = alice.signrawtransactionwithwallet(raw_tx)['hex']164# broadcast tx1165tx1_txid = alice.sendrawtransaction(tx1)167assert_equal(alice.listunspent(), [unspents[2]])168assert_equal(alice.getbalance(), 25)170# broadcast tx2, replaces tx1 in mempool171tx2_txid = alice.sendrawtransaction(tx2)173# Check that unspent[0] is now available because the transaction spending it has been replaced in the mempool174assert_equal(alice.listunspent(), [unspents[0]])175assert_equal(alice.getbalance(), 25)177assert_equal(alice.gettransaction(tx1_txid)["mempoolconflicts"], [tx2_txid])179self.log.info("Test scenario where a mempool conflict is removed")181# broadcast tx3, replaces tx2 in mempool182# Now that tx1's conflict has been removed, tx1 is now183# not conflicted, and instead is inactive until it is184# rebroadcasted. Now unspent[0] is not available, because185# tx1 is no longer conflicted.186alice.sendrawtransaction(tx3)188assert_equal(alice.gettransaction(tx1_txid)["mempoolconflicts"], [])189assert tx1_txid not in self.nodes[0].getrawmempool()191# now all of alice's outputs should be considered spent192# unspent[0]: spent by inactive tx1193# unspent[1]: spent by inactive tx1194# unspent[2]: spent by active tx3195assert_equal(alice.listunspent(), [])196assert_equal(alice.getbalance(), 0)198# Clean up for next test199bob.sendall([self.nodes[2].getnewaddress()])200self.generate(self.nodes[2], 1)202alice.unloadwallet()204def test_mempool_and_block_conflicts(self):205self.nodes[0].createwallet("alice_2")206alice = self.nodes[0].get_wallet_rpc("alice_2")207bob = self.nodes[1]209self.nodes[2].send(outputs=[{alice.getnewaddress() : 25} for _ in range(3)])210self.generate(self.nodes[2], 1)212self.log.info("Test a scenario where a transaction has both a block conflict and a mempool conflict")213unspents = [{"txid" : element["txid"], "vout" : element["vout"]} for element in alice.listunspent()]215assert_equal(bob.getbalances()["mine"]["untrusted_pending"], 0)217# alice and bob nodes are disconnected so that transactions can be218# created by alice, but broadcasted from bob so that alice's wallet219# doesn't know about them220self.disconnect_nodes(0, 1)222# Sends funds to bob223raw_tx = alice.createrawtransaction(inputs=[unspents[0]], outputs=[{bob.getnewaddress() : 24.99999}])224raw_tx1 = alice.signrawtransactionwithwallet(raw_tx)['hex']225tx1_txid = bob.sendrawtransaction(raw_tx1) # broadcast original tx spending unspents[0] only to bob227# create a conflict to previous tx (also spends unspents[0]), but don't broadcast, sends funds back to alice228raw_tx = alice.createrawtransaction(inputs=[unspents[0], unspents[2]], outputs=[{alice.getnewaddress() : 49.999}])229tx1_conflict = alice.signrawtransactionwithwallet(raw_tx)['hex']231# Sends funds to bob232raw_tx = alice.createrawtransaction(inputs=[unspents[1]], outputs=[{bob.getnewaddress() : 24.9999}])233raw_tx2 = alice.signrawtransactionwithwallet(raw_tx)['hex']234tx2_txid = bob.sendrawtransaction(raw_tx2) # broadcast another original tx spending unspents[1] only to bob236# create a conflict to previous tx (also spends unspents[1]), but don't broadcast, sends funds to alice237raw_tx = alice.createrawtransaction(inputs=[unspents[1]], outputs=[{alice.getnewaddress() : 24.9999}])238tx2_conflict = alice.signrawtransactionwithwallet(raw_tx)['hex']240bob_unspents = [{"txid" : element, "vout" : 0} for element in [tx1_txid, tx2_txid]]242# tx1 and tx2 are now in bob's mempool, and they are unconflicted, so bob has these funds243assert_equal(bob.getbalances()["mine"]["untrusted_pending"], Decimal("49.99989000"))245# spend both of bob's unspents, child tx of tx1 and tx2246raw_tx = bob.createrawtransaction(inputs=[bob_unspents[0], bob_unspents[1]], outputs=[{bob.getnewaddress() : 49.999}])247raw_tx3 = bob.signrawtransactionwithwallet(raw_tx)['hex']248tx3_txid = bob.sendrawtransaction(raw_tx3) # broadcast tx only to bob250# alice knows about 0 txs, bob knows about 3
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.