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) 2014-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.5"""Test the wallet accounts properly when there is a double-spend conflict."""6from decimal import Decimal8from test_framework.test_framework import BitcoinTestFramework9from test_framework.util import (10assert_equal,11)14class TxnMallTest(BitcoinTestFramework):15def set_test_params(self):16self.num_nodes = 318def skip_test_if_missing_module(self):19self.skip_if_no_wallet()21def add_options(self, parser):22parser.add_argument("--mineblock", dest="mine_block", default=False, action="store_true",23help="Test double-spend of 1-confirmed transaction")25def setup_network(self):26# Start with split network:27super().setup_network()28self.disconnect_nodes(1, 2)30def spend_utxo(self, utxo, outputs):31inputs = [utxo]32tx = self.nodes[0].createrawtransaction(inputs, outputs)33tx = self.nodes[0].fundrawtransaction(tx)34tx = self.nodes[0].signrawtransactionwithwallet(tx['hex'])35return self.nodes[0].sendrawtransaction(tx['hex'])37def run_test(self):38# All nodes should start with 1,250 BTC:39starting_balance = 125041# All nodes should be out of IBD.42# If the nodes are not all out of IBD, that can interfere with43# blockchain sync later in the test when nodes are connected, due to44# timing issues.45for n in self.nodes:46assert_equal(n.getblockchaininfo()["initialblockdownload"], False)48for i in range(3):49assert_equal(self.nodes[i].getbalance(), starting_balance)51# Assign coins to foo and bar addresses:52node0_address_foo = self.nodes[0].getnewaddress()53fund_foo_utxo = self.create_outpoints(self.nodes[0], outputs=[{node0_address_foo: 1219}])[0]54fund_foo_tx = self.nodes[0].gettransaction(fund_foo_utxo['txid'])55self.nodes[0].lockunspent(False, [fund_foo_utxo])57node0_address_bar = self.nodes[0].getnewaddress()58fund_bar_utxo = self.create_outpoints(node=self.nodes[0], outputs=[{node0_address_bar: 29}])[0]59fund_bar_tx = self.nodes[0].gettransaction(fund_bar_utxo['txid'])61assert_equal(self.nodes[0].getbalance(),62starting_balance + fund_foo_tx["fee"] + fund_bar_tx["fee"])64# Coins are sent to node1_address65node1_address = self.nodes[1].getnewaddress()67# First: use raw transaction API to send 1240 BTC to node1_address,68# but don't broadcast:69doublespend_fee = Decimal('-.02')70inputs = [fund_foo_utxo, fund_bar_utxo]71change_address = self.nodes[0].getnewaddress()72outputs = {}73outputs[node1_address] = 124074outputs[change_address] = 1248 - 1240 + doublespend_fee75rawtx = self.nodes[0].createrawtransaction(inputs, outputs)76doublespend = self.nodes[0].signrawtransactionwithwallet(rawtx)77assert_equal(doublespend["complete"], True)79# Create two spends using 1 50 BTC coin each80txid1 = self.spend_utxo(fund_foo_utxo, {node1_address: 40})81txid2 = self.spend_utxo(fund_bar_utxo, {node1_address: 20})83# Have node0 mine a block:84if (self.options.mine_block):85self.generate(self.nodes[0], 1, sync_fun=lambda: self.sync_blocks(self.nodes[0:2]))87tx1 = self.nodes[0].gettransaction(txid1)88tx2 = self.nodes[0].gettransaction(txid2)90# Node0's balance should be starting balance, plus 50BTC for another91# matured block, minus 40, minus 20, and minus transaction fees:92expected = starting_balance + fund_foo_tx["fee"] + fund_bar_tx["fee"]93if self.options.mine_block:94expected += 5095expected += tx1["amount"] + tx1["fee"]96expected += tx2["amount"] + tx2["fee"]97assert_equal(self.nodes[0].getbalance(), expected)99if self.options.mine_block:100assert_equal(tx1["confirmations"], 1)101assert_equal(tx2["confirmations"], 1)102# Node1's balance should be both transaction amounts:103assert_equal(self.nodes[1].getbalance(), starting_balance - tx1["amount"] - tx2["amount"])104else:105assert_equal(tx1["confirmations"], 0)106assert_equal(tx2["confirmations"], 0)108# Now give doublespend and its parents to miner:109self.nodes[2].sendrawtransaction(fund_foo_tx["hex"])110self.nodes[2].sendrawtransaction(fund_bar_tx["hex"])111doublespend_txid = self.nodes[2].sendrawtransaction(doublespend["hex"])112# ... mine a block...113self.generate(self.nodes[2], 1, sync_fun=self.no_op)115# Reconnect the split network, and sync chain:116self.connect_nodes(1, 2)117self.generate(self.nodes[2], 1) # Mine another block to make sure we sync118assert_equal(self.nodes[0].gettransaction(doublespend_txid)["confirmations"], 2)120# Re-fetch transaction info:121tx1 = self.nodes[0].gettransaction(txid1)122tx2 = self.nodes[0].gettransaction(txid2)124# Both transactions should be conflicted125assert_equal(tx1["confirmations"], -2)126assert_equal(tx2["confirmations"], -2)128# Node0's total balance should be starting balance, plus 100BTC for129# two more matured blocks, minus 1240 for the double-spend, plus fees (which are130# negative):131expected = starting_balance + 100 - 1240 + fund_foo_tx["fee"] + fund_bar_tx["fee"] + doublespend_fee132assert_equal(self.nodes[0].getbalance(), expected)134# Node1's balance should be its initial balance (1250 for 25 block rewards) plus the doublespend:135assert_equal(self.nodes[1].getbalance(), 1250 + 1240)138if __name__ == '__main__':139TxnMallTest(__file__).main()
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.