Skip to content

Commit fa56e86

Browse files
author
MarcoFalke
committed
test: Run rpc_txoutproof.py even with wallet disabled
1 parent faba790 commit fa56e86

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

test/functional/rpc_txoutproof.py

+36-46
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,50 @@ 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])
9080

9181
# Now we'll try tweaking a proof.
92-
proof = self.nodes[3].gettxoutproof([txid1, txid2])
82+
proof = self.nodes[1].gettxoutproof([txid1, txid2])
9383
assert txid1 in self.nodes[0].verifytxoutproof(proof)
9484
assert txid2 in self.nodes[1].verifytxoutproof(proof)
9585

9686
tweaked_proof = FromHex(CMerkleBlock(), proof)
9787

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

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

test/functional/test_framework/wallet.py

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ 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 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+
4347
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."""
4549
self._utxos = sorted(self._utxos, key=lambda k: k['value'])

0 commit comments

Comments
 (0)