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.
251# listunspent should show 1 single, unused 5 btc output252assert_unspent(self.nodes[1], total_count=1, total_sum=5, reused_supported=True, reused_count=0)253# getbalances should show no used, 5 btc trusted254assert_balances(self.nodes[1], mine={"used": 0, "trusted": 5})256def test_getbalances_used(self):257'''258getbalances and listunspent should pick up on reused addresses259immediately, even for address reusing outputs created before the first260transaction was spending from that address261'''262self.log.info("Test getbalances used category")264# node under test should be completely empty265assert_equal(self.nodes[1].getbalance(avoid_reuse=False), 0)267new_addr = self.nodes[1].getnewaddress()268ret_addr = self.nodes[0].getnewaddress()270# send multiple transactions, reusing one address271for _ in range(101):272self.nodes[0].sendtoaddress(new_addr, 1)274self.generate(self.nodes[0], 1)276# send transaction that should not use all the available outputs277# per the current coin selection algorithm278self.nodes[1].sendtoaddress(ret_addr, 5)280# getbalances and listunspent should show the remaining outputs281# in the reused address as used/reused282assert_unspent(self.nodes[1], total_count=2, total_sum=96, reused_count=1, reused_sum=1, margin=0.01)283assert_balances(self.nodes[1], mine={"used": 1, "trusted": 95}, margin=0.01)285def test_full_destination_group_is_preferred(self):286'''287Test the case where [1] only has 101 outputs of 1 BTC in the same reused288address and tries to send a small payment of 0.5 BTC. The wallet289should use 100 outputs from the reused address as inputs and not a290single 1 BTC input, in order to join several outputs from the reused291address.292'''293self.log.info("Test that full destination groups are preferred in coin selection")295# Node under test should be empty296assert_equal(self.nodes[1].getbalance(avoid_reuse=False), 0)298new_addr = self.nodes[1].getnewaddress()299ret_addr = self.nodes[0].getnewaddress()301# Send 101 outputs of 1 BTC to the same, reused address in the wallet302for _ in range(101):303self.nodes[0].sendtoaddress(new_addr, 1)305self.generate(self.nodes[0], 1)307# Sending a transaction that is smaller than each one of the308# available outputs309txid = self.nodes[1].sendtoaddress(address=ret_addr, amount=0.5)310inputs = self.nodes[1].getrawtransaction(txid, 1)["vin"]312# The transaction should use 100 inputs exactly313assert_equal(len(inputs), 100)315def test_all_destination_groups_are_used(self):316'''317Test the case where [1] only has 202 outputs of 1 BTC in the same reused318address and tries to send a payment of 200.5 BTC. The wallet319should use all 202 outputs from the reused address as inputs.320'''321self.log.info("Test that all destination groups are used")323# Node under test should be empty324assert_equal(self.nodes[1].getbalance(avoid_reuse=False), 0)326new_addr = self.nodes[1].getnewaddress()327ret_addr = self.nodes[0].getnewaddress()329# Send 202 outputs of 1 BTC to the same, reused address in the wallet330for _ in range(202):331self.nodes[0].sendtoaddress(new_addr, 1)333self.generate(self.nodes[0], 1)335# Sending a transaction that needs to use the full groups336# of 100 inputs but also the incomplete group of 2 inputs.337txid = self.nodes[1].sendtoaddress(address=ret_addr, amount=200.5)338inputs = self.nodes[1].getrawtransaction(txid, 1)["vin"]340# The transaction should use 202 inputs exactly341assert_equal(len(inputs), 202)344if __name__ == '__main__':345AvoidReuseTest(__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.