Skip to content

Commit fa79a78

Browse files
author
MarcoFalke
committed
test: Add reorg test to wallet_balance
1 parent fad03cd commit fa79a78

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

test/functional/wallet_balance.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,39 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the wallet balance RPC methods."""
66
from decimal import Decimal
7+
import struct
78

89
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE as ADDRESS_WATCHONLY
910
from test_framework.test_framework import BitcoinTestFramework
1011
from test_framework.util import (
1112
assert_equal,
1213
assert_raises_rpc_error,
14+
connect_nodes_bi,
15+
sync_blocks,
1316
)
1417

1518

1619
def create_transactions(node, address, amt, fees):
1720
# Create and sign raw transactions from node to address for amt.
1821
# Creates a transaction for each fee and returns an array
1922
# of the raw transactions.
20-
utxos = node.listunspent(0)
23+
utxos = [u for u in node.listunspent(0) if u['spendable']]
2124

2225
# Create transactions
2326
inputs = []
2427
ins_total = 0
2528
for utxo in utxos:
2629
inputs.append({"txid": utxo["txid"], "vout": utxo["vout"]})
2730
ins_total += utxo['amount']
28-
if ins_total > amt:
31+
if ins_total + max(fees) > amt:
2932
break
3033

3134
txs = []
3235
for fee in fees:
3336
outputs = {address: amt, node.getrawchangeaddress(): ins_total - amt - fee}
3437
raw_tx = node.createrawtransaction(inputs, outputs, 0, True)
3538
raw_tx = node.signrawtransactionwithwallet(raw_tx)
39+
assert_equal(raw_tx['complete'], True)
3640
txs.append(raw_tx)
3741

3842
return txs
@@ -152,9 +156,47 @@ def test_balances(*, fee_node_1=0):
152156
# Create 3 more wallet txs, where the last is not accepted to the
153157
# mempool because it is the third descendant of the tx above
154158
for _ in range(3):
159+
# Set amount high enough such that all coins are spent by each tx
155160
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 99)
161+
162+
self.log.info('Check that wallet txs not in the mempool are untrusted')
156163
assert txid not in self.nodes[0].getrawmempool()
164+
assert_equal(self.nodes[0].gettransaction(txid)['trusted'], False)
165+
assert_equal(self.nodes[0].getbalance(minconf=0), 0)
166+
167+
self.log.info("Test replacement and reorg of non-mempool tx")
168+
tx_orig = self.nodes[0].gettransaction(txid)['hex']
169+
# Increase fee by 1 coin
170+
tx_replace = tx_orig.replace(
171+
struct.pack("<q", 99 * 10**8).hex(),
172+
struct.pack("<q", 98 * 10**8).hex(),
173+
)
174+
tx_replace = self.nodes[0].signrawtransactionwithwallet(tx_replace)['hex']
175+
# Total balance is given by the sum of outputs of the tx
176+
total_amount = sum([o['value'] for o in self.nodes[0].decoderawtransaction(tx_replace)['vout']])
177+
self.sync_all()
178+
self.nodes[1].sendrawtransaction(hexstring=tx_replace, maxfeerate=0)
179+
180+
# Now confirm tx_replace
181+
block_reorg = self.nodes[1].generatetoaddress(1, ADDRESS_WATCHONLY)[0]
182+
self.sync_all()
183+
assert_equal(self.nodes[0].getbalance(minconf=0), total_amount)
184+
185+
self.log.info('Put txs back into mempool of node 1 (not node 0)')
186+
self.nodes[0].invalidateblock(block_reorg)
187+
self.nodes[1].invalidateblock(block_reorg)
157188
assert_equal(self.nodes[0].getbalance(minconf=0), 0) # wallet txs not in the mempool are untrusted
189+
self.nodes[0].generatetoaddress(1, ADDRESS_WATCHONLY)
190+
assert_equal(self.nodes[0].getbalance(minconf=0), 0) # wallet txs not in the mempool are untrusted
191+
192+
# Now confirm tx_orig
193+
self.restart_node(1, ['-persistmempool=0'])
194+
connect_nodes_bi(self.nodes, 0, 1)
195+
sync_blocks(self.nodes)
196+
self.nodes[1].sendrawtransaction(tx_orig)
197+
self.nodes[1].generatetoaddress(1, ADDRESS_WATCHONLY)
198+
self.sync_all()
199+
assert_equal(self.nodes[0].getbalance(minconf=0), total_amount + 1) # The reorg recovered our fee of 1 coin
158200

159201

160202
if __name__ == '__main__':

0 commit comments

Comments
 (0)