Skip to content

Commit f2d9934

Browse files
committed
Merge #19922: test: Run rpc_txoutproof.py even with wallet disabled
faf251d test: gettxoutproof duplicate txid (João Barbosa) faf5eb4 test: Test empty array in gettxoutproof (MarcoFalke) fa56e86 test: Run rpc_txoutproof.py even with wallet disabled (MarcoFalke) faba790 test: MiniWallet: Default fee_rate in send_self_transfer, Pass in utxo_to_spend (MarcoFalke) fa65a11 test: bugfix: Actually pick largest utxo (MarcoFalke) Pull request description: Run the consensus test even when the wallet was not compiled. Also: * Minor bugfix in MiniWallet * Two new test cases (one cherry-picked from #19847) ACKs for top commit: jnewbery: utACK faf251d. Thanks Marco! kristapsk: ACK faf251d Tree-SHA512: a5ab33695c88cfb3c369021d4506069c08ce298e24e891db55159130693ed3817444c72f6aad3f472235aa4597b2c601010af714411c2ec8ad9c2d2e0b00ecbc
2 parents 9366a73 + faf251d commit f2d9934

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

test/functional/rpc_txoutproof.py

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,31 @@
66

77
from test_framework.messages import CMerkleBlock, FromHex, ToHex
88
from test_framework.test_framework import BitcoinTestFramework
9-
from test_framework.util import assert_equal, assert_raises_rpc_error, connect_nodes
9+
from test_framework.util import assert_equal, assert_raises_rpc_error
10+
from test_framework.wallet import MiniWallet
11+
1012

1113
class MerkleBlockTest(BitcoinTestFramework):
1214
def set_test_params(self):
13-
self.num_nodes = 4
15+
self.num_nodes = 2
1416
self.setup_clean_chain = True
15-
# Nodes 0/1 are "wallet" nodes, Nodes 2/3 are used for testing
16-
self.extra_args = [[], [], [], ["-txindex"]]
17-
18-
def skip_test_if_missing_module(self):
19-
self.skip_if_no_wallet()
20-
21-
def setup_network(self):
22-
self.setup_nodes()
23-
connect_nodes(self.nodes[0], 1)
24-
connect_nodes(self.nodes[0], 2)
25-
connect_nodes(self.nodes[0], 3)
26-
27-
self.sync_all()
17+
self.extra_args = [
18+
[],
19+
["-txindex"],
20+
]
2821

2922
def run_test(self):
30-
self.log.info("Mining blocks...")
31-
self.nodes[0].generate(105)
23+
miniwallet = MiniWallet(self.nodes[0])
24+
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
25+
miniwallet.generate(5)
26+
self.nodes[0].generate(100)
3227
self.sync_all()
3328

3429
chain_height = self.nodes[1].getblockcount()
3530
assert_equal(chain_height, 105)
36-
assert_equal(self.nodes[1].getbalance(), 0)
37-
assert_equal(self.nodes[2].getbalance(), 0)
38-
39-
node0utxos = self.nodes[0].listunspent(1)
40-
tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
41-
txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithwallet(tx1)["hex"])
42-
tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
43-
txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithwallet(tx2)["hex"])
31+
32+
txid1 = miniwallet.send_self_transfer(from_node=self.nodes[0])['txid']
33+
txid2 = miniwallet.send_self_transfer(from_node=self.nodes[0])['txid']
4434
# This will raise an exception because the transaction is not yet in a block
4535
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid1])
4636

@@ -53,50 +43,54 @@ def run_test(self):
5343
txlist.append(blocktxn[1])
5444
txlist.append(blocktxn[2])
5545

56-
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1])), [txid1])
57-
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2])), txlist)
58-
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2], blockhash)), txlist)
46+
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1])), [txid1])
47+
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2])), txlist)
48+
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2], blockhash)), txlist)
5949

60-
txin_spent = self.nodes[1].listunspent(1).pop()
61-
tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 49.98})
62-
txid3 = self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransactionwithwallet(tx3)["hex"])
50+
txin_spent = miniwallet.get_utxo() # Get the change from txid2
51+
tx3 = miniwallet.send_self_transfer(from_node=self.nodes[0], utxo_to_spend=txin_spent)
52+
txid3 = tx3['txid']
6353
self.nodes[0].generate(1)
6454
self.sync_all()
6555

6656
txid_spent = txin_spent["txid"]
67-
txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2
57+
txid_unspent = txid1 # Input was change from txid2, so txid1 should be unspent
6858

6959
# Invalid txids
70-
assert_raises_rpc_error(-8, "txid must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[2].gettxoutproof, ["00000000000000000000000000000000"], blockhash)
71-
assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[2].gettxoutproof, ["ZZZ0000000000000000000000000000000000000000000000000000000000000"], blockhash)
60+
assert_raises_rpc_error(-8, "txid must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["00000000000000000000000000000000"], blockhash)
61+
assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, ["ZZZ0000000000000000000000000000000000000000000000000000000000000"], blockhash)
7262
# Invalid blockhashes
73-
assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[2].gettxoutproof, [txid_spent], "00000000000000000000000000000000")
74-
assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[2].gettxoutproof, [txid_spent], "ZZZ0000000000000000000000000000000000000000000000000000000000000")
63+
assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 32, for '00000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "00000000000000000000000000000000")
64+
assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].gettxoutproof, [txid_spent], "ZZZ0000000000000000000000000000000000000000000000000000000000000")
7565
# We can't find the block from a fully-spent tx
76-
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[2].gettxoutproof, [txid_spent])
66+
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid_spent])
7767
# We can get the proof if we specify the block
78-
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent])
68+
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_spent], blockhash)), [txid_spent])
7969
# We can't get the proof if we specify a non-existent block
80-
assert_raises_rpc_error(-5, "Block not found", self.nodes[2].gettxoutproof, [txid_spent], "0000000000000000000000000000000000000000000000000000000000000000")
70+
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].gettxoutproof, [txid_spent], "0000000000000000000000000000000000000000000000000000000000000000")
8171
# We can get the proof if the transaction is unspent
82-
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent])
72+
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid_unspent])), [txid_unspent])
8373
# We can get the proof if we provide a list of transactions and one of them is unspent. The ordering of the list should not matter.
84-
assert_equal(sorted(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2]))), sorted(txlist))
85-
assert_equal(sorted(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid2, txid1]))), sorted(txlist))
74+
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid1, txid2]))), sorted(txlist))
75+
assert_equal(sorted(self.nodes[0].verifytxoutproof(self.nodes[0].gettxoutproof([txid2, txid1]))), sorted(txlist))
8676
# We can always get a proof if we have a -txindex
87-
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent])
77+
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].gettxoutproof([txid_spent])), [txid_spent])
8878
# We can't get a proof if we specify transactions from different blocks
89-
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[2].gettxoutproof, [txid1, txid3])
79+
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[0].gettxoutproof, [txid1, txid3])
80+
# Test empty list
81+
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [])
82+
# Test duplicate txid
83+
assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1])
9084

9185
# Now we'll try tweaking a proof.
92-
proof = self.nodes[3].gettxoutproof([txid1, txid2])
86+
proof = self.nodes[1].gettxoutproof([txid1, txid2])
9387
assert txid1 in self.nodes[0].verifytxoutproof(proof)
9488
assert txid2 in self.nodes[1].verifytxoutproof(proof)
9589

9690
tweaked_proof = FromHex(CMerkleBlock(), proof)
9791

9892
# Make sure that our serialization/deserialization is working
99-
assert txid1 in self.nodes[2].verifytxoutproof(ToHex(tweaked_proof))
93+
assert txid1 in self.nodes[0].verifytxoutproof(ToHex(tweaked_proof))
10094

10195
# Check to see if we can go up the merkle tree and pass this off as a
10296
# single-transaction block

test/functional/test_framework/wallet.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,21 @@ def generate(self, num_blocks):
4040
self._utxos.append({'txid': cb_tx['txid'], 'vout': 0, 'value': cb_tx['vout'][0]['value']})
4141
return blocks
4242

43-
def send_self_transfer(self, *, fee_rate, from_node):
43+
def get_utxo(self):
44+
"""Return the last utxo. Can be used to get the change output immediately after a send_self_transfer"""
45+
return self._utxos.pop()
46+
47+
def send_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_spend=None):
4448
"""Create and send a tx with the specified fee_rate. Fee may be exact or at most one satoshi higher than needed."""
45-
self._utxos = sorted(self._utxos, key=lambda k: -k['value'])
46-
largest_utxo = self._utxos.pop() # Pick the largest utxo and hope it covers the fee
49+
self._utxos = sorted(self._utxos, key=lambda k: k['value'])
50+
utxo_to_spend = utxo_to_spend or self._utxos.pop() # Pick the largest utxo (if none provided) and hope it covers the fee
4751
vsize = Decimal(96)
48-
send_value = satoshi_round(largest_utxo['value'] - fee_rate * (vsize / 1000))
49-
fee = largest_utxo['value'] - send_value
50-
assert (send_value > 0)
52+
send_value = satoshi_round(utxo_to_spend['value'] - fee_rate * (vsize / 1000))
53+
fee = utxo_to_spend['value'] - send_value
54+
assert send_value > 0
5155

5256
tx = CTransaction()
53-
tx.vin = [CTxIn(COutPoint(int(largest_utxo['txid'], 16), largest_utxo['vout']))]
57+
tx.vin = [CTxIn(COutPoint(int(utxo_to_spend['txid'], 16), utxo_to_spend['vout']))]
5458
tx.vout = [CTxOut(int(send_value * COIN), self._scriptPubKey)]
5559
tx.wit.vtxinwit = [CTxInWitness()]
5660
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]

0 commit comments

Comments
 (0)