Files
bitcoin/test/functional/wallet_orphanedreward.py
Ava Chow cd7865b0ce Merge bitcoin/bitcoin#33671: wallet: Add separate balance info for non-mempool wallet txs
32325d1777 tests: Add test for mempool-invalid wallet tx (Anthony Towns)
25e063d950 wallet: Add separate balance info for non-mempool wallet txs (Anthony Towns)
81e763f1e5 wallet: Have GetBalance report used amount directly without two calls (Anthony Towns)

Pull request description:

  Changes `getbalances` to report the sum of txos spent by transactions that aren't confirmed nor in the mempool (eg due to being part of too long a mempool chain, or spending non-standard outputs, or having a datacarrier output that exceeds `-datacarriersize`, etc). Those values are added to the trusted/untrusted_pending/immature/used fields as appropriate (where previously they were skipped), and subtracted from the new nonmempool field, so that the sum of all fields remains the same.

  For example:

  ```
  $ bitcoin-cli -regtest getbalances
  {
    "mine": {
      "trusted": 6049.99999220,
      "untrusted_pending": 0.00000000,
      "immature": 3200.00000780,
      "nonmempool": -100.00000000
    },
    "lastprocessedblock": {
      "hash": "3ab4582226d5e8ad76438db48d76e822c31bce2cdbc7ba82a5d974a277515d0d",
      "height": 221
    }
  }
  ```

  Closes #11887

ACKs for top commit:
  achow101:
    ACK 32325d1777
  w0xlt:
    lgtm ACK 32325d1777
  musaHaruna:
    Code Review ACK [32325d1](32325d1777)

Tree-SHA512: 142581944d1b3213067e219e3b8205f27b89007e545149c01b801bad38fe730c5b2bfdfe6a2064c3649889f66ec48ec7616982564d00e3d83837249e925d8f16
2026-04-22 16:19:55 -07:00

75 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright (c) 2020-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test orphaned block rewards in the wallet."""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
class OrphanedBlockRewardTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
def run_test(self):
# Generate some blocks and obtain some coins on node 0. We send
# some balance to node 1, which will hold it as a single coin.
self.generate(self.nodes[0], 150)
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 10)
self.generate(self.nodes[0], 1)
# Get a block reward with node 1 and remember the block so we can orphan
# it later.
self.sync_blocks()
blk = self.generate(self.nodes[1], 1)[0]
# Let the block reward mature and send coins including both
# the existing balance and the block reward.
self.generate(self.nodes[0], 150)
assert_equal(self.nodes[1].getbalance(), 10 + 25)
pre_reorg_conf_bals = self.nodes[1].getbalances()
txid = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 30)
orig_chain_tip = self.nodes[0].getbestblockhash()
self.sync_mempools()
# Orphan the block reward and make sure that the original coins
# from the wallet can still be spent.
self.nodes[0].invalidateblock(blk)
blocks = self.generate(self.nodes[0], 152)
conflict_block = blocks[0]
# We expect the descendants of orphaned rewards to no longer be considered
assert_equal(self.nodes[1].getbalances()["mine"], {
"trusted": 10,
"untrusted_pending": 0,
"immature": 0,
"nonmempool": 0,
})
# And the unconfirmed tx to be abandoned
assert_equal(self.nodes[1].gettransaction(txid)["details"][0]["abandoned"], True)
# The abandoning should persist through reloading
self.nodes[1].unloadwallet(self.default_wallet_name)
self.nodes[1].loadwallet(self.default_wallet_name)
assert_equal(self.nodes[1].gettransaction(txid)["details"][0]["abandoned"], True)
# If the orphaned reward is reorged back into the main chain, any unconfirmed
# descendant txs at the time of the original reorg remain abandoned.
self.nodes[0].invalidateblock(conflict_block)
self.nodes[0].reconsiderblock(blk)
assert_equal(self.nodes[0].getbestblockhash(), orig_chain_tip)
self.generate(self.nodes[0], 3)
balances = self.nodes[1].getbalances()
del balances["lastprocessedblock"]
del pre_reorg_conf_bals["lastprocessedblock"]
assert_equal(balances, pre_reorg_conf_bals)
assert_equal(self.nodes[1].gettransaction(txid)["details"][0]["abandoned"], True)
if __name__ == '__main__':
OrphanedBlockRewardTest(__file__).main()