Skip to content

Commit 269fa66

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25044: test: Use MiniWallet in rpc_rawtransaction.py
e895900 test: Use MiniWallet in rpc_rawtransaction.py (Daniela Brozzoni) e93046c MOVEONLY: Move signrawtransactionwithwallet test (Daniela Brozzoni) Pull request description: This PR allows `rpc_rawtransaction.py` to be run even without the Core wallet by using the MiniWallet instead, as proposed in bitcoin/bitcoin#20078. This test was previously run twice, once with `--legacy-wallet` and once with `--descriptors`. Since this would have meant running the same test twice if the wallet wasn't compiled, now we run it just once with the legacy wallet. ACKs for top commit: jonatack: ACK e895900 Tree-SHA512: d1580570a54dad8e30a5df1ab7d03ecb3f824efe6843323e1f3aef63592045d823c7d54fc86321dc7c1d414854a253431a01a7baa9f30426ea9a09ef11ae3a04
2 parents ad9e5ea + e895900 commit 269fa66

File tree

3 files changed

+93
-119
lines changed

3 files changed

+93
-119
lines changed

test/functional/rpc_rawtransaction.py

Lines changed: 42 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
from test_framework.util import (
2525
assert_equal,
2626
assert_raises_rpc_error,
27-
find_vout_for_address,
27+
)
28+
from test_framework.wallet import (
29+
getnewdestination,
30+
MiniWallet,
2831
)
2932

3033

@@ -52,79 +55,67 @@ def items(self):
5255
class RawTransactionsTest(BitcoinTestFramework):
5356
def set_test_params(self):
5457
self.setup_clean_chain = True
55-
self.num_nodes = 4
58+
self.num_nodes = 3
5659
self.extra_args = [
57-
["-txindex"],
5860
["-txindex"],
5961
["-txindex"],
6062
[],
6163
]
6264
# whitelist all peers to speed up tx relay / mempool sync
6365
for args in self.extra_args:
6466
args.append("[email protected]")
67+
self.requires_wallet = self.is_specified_wallet_compiled()
6568

6669
self.supports_cli = False
6770

68-
def skip_test_if_missing_module(self):
69-
self.skip_if_no_wallet()
70-
7171
def setup_network(self):
7272
super().setup_network()
7373
self.connect_nodes(0, 2)
7474

7575
def run_test(self):
76+
self.wallet = MiniWallet(self.nodes[0])
7677
self.log.info("Prepare some coins for multiple *rawtransaction commands")
77-
self.generate(self.nodes[2], 1)
78+
self.generate(self.wallet, 10)
7879
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
79-
for amount in [1.5, 1.0, 5.0]:
80-
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), amount)
81-
self.sync_all()
82-
self.generate(self.nodes[0], 5)
8380

8481
self.getrawtransaction_tests()
8582
self.createrawtransaction_tests()
86-
self.signrawtransactionwithwallet_tests()
8783
self.sendrawtransaction_tests()
8884
self.sendrawtransaction_testmempoolaccept_tests()
8985
self.decoderawtransaction_tests()
9086
self.transaction_version_number_tests()
91-
if not self.options.descriptors:
87+
if self.requires_wallet and not self.options.descriptors:
9288
self.raw_multisig_transaction_legacy_tests()
9389

9490
def getrawtransaction_tests(self):
95-
addr = self.nodes[1].getnewaddress()
96-
txid = self.nodes[0].sendtoaddress(addr, 10)
91+
tx = self.wallet.send_self_transfer(from_node=self.nodes[0])
9792
self.generate(self.nodes[0], 1)
98-
vout = find_vout_for_address(self.nodes[1], txid, addr)
99-
rawTx = self.nodes[1].createrawtransaction([{'txid': txid, 'vout': vout}], {self.nodes[1].getnewaddress(): 9.999})
100-
rawTxSigned = self.nodes[1].signrawtransactionwithwallet(rawTx)
101-
txId = self.nodes[1].sendrawtransaction(rawTxSigned['hex'])
102-
self.generateblock(self.nodes[0], output=self.nodes[0].getnewaddress(), transactions=[rawTxSigned['hex']])
93+
txId = tx['txid']
10394
err_msg = (
10495
"No such mempool transaction. Use -txindex or provide a block hash to enable"
10596
" blockchain transaction queries. Use gettransaction for wallet transactions."
10697
)
10798

108-
for n in [0, 3]:
99+
for n in [0, 2]:
109100
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex")
110101

111102
if n == 0:
112103
# With -txindex.
113104
# 1. valid parameters - only supply txid
114-
assert_equal(self.nodes[n].getrawtransaction(txId), rawTxSigned['hex'])
105+
assert_equal(self.nodes[n].getrawtransaction(txId), tx['hex'])
115106

116107
# 2. valid parameters - supply txid and 0 for non-verbose
117-
assert_equal(self.nodes[n].getrawtransaction(txId, 0), rawTxSigned['hex'])
108+
assert_equal(self.nodes[n].getrawtransaction(txId, 0), tx['hex'])
118109

119110
# 3. valid parameters - supply txid and False for non-verbose
120-
assert_equal(self.nodes[n].getrawtransaction(txId, False), rawTxSigned['hex'])
111+
assert_equal(self.nodes[n].getrawtransaction(txId, False), tx['hex'])
121112

122113
# 4. valid parameters - supply txid and 1 for verbose.
123114
# We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
124-
assert_equal(self.nodes[n].getrawtransaction(txId, 1)["hex"], rawTxSigned['hex'])
115+
assert_equal(self.nodes[n].getrawtransaction(txId, 1)["hex"], tx['hex'])
125116

126117
# 5. valid parameters - supply txid and True for non-verbose
127-
assert_equal(self.nodes[n].getrawtransaction(txId, True)["hex"], rawTxSigned['hex'])
118+
assert_equal(self.nodes[n].getrawtransaction(txId, True)["hex"], tx['hex'])
128119
else:
129120
# Without -txindex, expect to raise.
130121
for verbose in [None, 0, False, 1, True]:
@@ -141,9 +132,9 @@ def getrawtransaction_tests(self):
141132
assert_raises_rpc_error(-1, "not a boolean", self.nodes[n].getrawtransaction, txId, {})
142133

143134
# Make a tx by sending, then generate 2 blocks; block1 has the tx in it
144-
tx = self.nodes[2].sendtoaddress(self.nodes[1].getnewaddress(), 1)
135+
tx = self.wallet.send_self_transfer(from_node=self.nodes[2])['txid']
145136
block1, block2 = self.generate(self.nodes[2], 2)
146-
for n in [0, 3]:
137+
for n in [0, 2]:
147138
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex, with blockhash")
148139
# We should be able to get the raw transaction by providing the correct block
149140
gottx = self.nodes[n].getrawtransaction(txid=tx, verbose=True, blockhash=block1)
@@ -200,20 +191,21 @@ def createrawtransaction_tests(self):
200191
# sequence number out of range
201192
for invalid_seq in [-1, 4294967296]:
202193
inputs = [{'txid': TXID, 'vout': 1, 'sequence': invalid_seq}]
203-
outputs = {self.nodes[0].getnewaddress(): 1}
194+
address = getnewdestination()[2]
195+
outputs = {address: 1}
204196
assert_raises_rpc_error(-8, 'Invalid parameter, sequence number is out of range',
205197
self.nodes[0].createrawtransaction, inputs, outputs)
206198
# with valid sequence number
207199
for valid_seq in [1000, 4294967294]:
208200
inputs = [{'txid': TXID, 'vout': 1, 'sequence': valid_seq}]
209-
outputs = {self.nodes[0].getnewaddress(): 1}
201+
address = getnewdestination()[2]
202+
outputs = {address: 1}
210203
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
211204
decrawtx = self.nodes[0].decoderawtransaction(rawtx)
212205
assert_equal(decrawtx['vin'][0]['sequence'], valid_seq)
213206

214207
# Test `createrawtransaction` invalid `outputs`
215-
address = self.nodes[0].getnewaddress()
216-
address2 = self.nodes[0].getnewaddress()
208+
address = getnewdestination()[2]
217209
assert_raises_rpc_error(-1, "JSON value is not an array as expected", self.nodes[0].createrawtransaction, [], 'foo')
218210
self.nodes[0].createrawtransaction(inputs=[], outputs={}) # Should not throw for backwards compatibility
219211
self.nodes[0].createrawtransaction(inputs=[], outputs=[])
@@ -245,6 +237,7 @@ def createrawtransaction_tests(self):
245237
self.nodes[2].createrawtransaction(inputs=[{'txid': TXID, 'vout': 9}], outputs=[{address: 99}]),
246238
)
247239
# Two outputs
240+
address2 = getnewdestination()[2]
248241
tx = tx_from_hex(self.nodes[2].createrawtransaction(inputs=[{'txid': TXID, 'vout': 9}], outputs=OrderedDict([(address, 99), (address2, 99)])))
249242
assert_equal(len(tx.vout), 2)
250243
assert_equal(
@@ -259,122 +252,53 @@ def createrawtransaction_tests(self):
259252
self.nodes[2].createrawtransaction(inputs=[{'txid': TXID, 'vout': 9}], outputs=[{address: 99}, {address2: 99}, {'data': '99'}]),
260253
)
261254

262-
def signrawtransactionwithwallet_tests(self):
263-
for type in ["bech32", "p2sh-segwit", "legacy"]:
264-
self.log.info(f"Test signrawtransactionwithwallet with missing prevtx info ({type})")
265-
addr = self.nodes[0].getnewaddress("", type)
266-
addrinfo = self.nodes[0].getaddressinfo(addr)
267-
pubkey = addrinfo["scriptPubKey"]
268-
inputs = [{'txid': TXID, 'vout': 3, 'sequence': 1000}]
269-
outputs = {self.nodes[0].getnewaddress(): 1}
270-
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
271-
272-
prevtx = dict(txid=TXID, scriptPubKey=pubkey, vout=3, amount=1)
273-
succ = self.nodes[0].signrawtransactionwithwallet(rawtx, [prevtx])
274-
assert succ["complete"]
275-
276-
if type == "legacy":
277-
del prevtx["amount"]
278-
succ = self.nodes[0].signrawtransactionwithwallet(rawtx, [prevtx])
279-
assert succ["complete"]
280-
else:
281-
assert_raises_rpc_error(-3, "Missing amount", self.nodes[0].signrawtransactionwithwallet, rawtx, [
282-
{
283-
"txid": TXID,
284-
"scriptPubKey": pubkey,
285-
"vout": 3,
286-
}
287-
])
288-
289-
assert_raises_rpc_error(-3, "Missing vout", self.nodes[0].signrawtransactionwithwallet, rawtx, [
290-
{
291-
"txid": TXID,
292-
"scriptPubKey": pubkey,
293-
"amount": 1,
294-
}
295-
])
296-
assert_raises_rpc_error(-3, "Missing txid", self.nodes[0].signrawtransactionwithwallet, rawtx, [
297-
{
298-
"scriptPubKey": pubkey,
299-
"vout": 3,
300-
"amount": 1,
301-
}
302-
])
303-
assert_raises_rpc_error(-3, "Missing scriptPubKey", self.nodes[0].signrawtransactionwithwallet, rawtx, [
304-
{
305-
"txid": TXID,
306-
"vout": 3,
307-
"amount": 1
308-
}
309-
])
310-
311255
def sendrawtransaction_tests(self):
312256
self.log.info("Test sendrawtransaction with missing input")
313257
inputs = [{'txid': TXID, 'vout': 1}] # won't exist
314-
outputs = {self.nodes[0].getnewaddress(): 4.998}
258+
address = getnewdestination()[2]
259+
outputs = {address: 4.998}
315260
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
316-
rawtx = self.nodes[2].signrawtransactionwithwallet(rawtx)
317-
assert_raises_rpc_error(-25, "bad-txns-inputs-missingorspent", self.nodes[2].sendrawtransaction, rawtx['hex'])
261+
assert_raises_rpc_error(-25, "bad-txns-inputs-missingorspent", self.nodes[2].sendrawtransaction, rawtx)
318262

319263
def sendrawtransaction_testmempoolaccept_tests(self):
320264
self.log.info("Test sendrawtransaction/testmempoolaccept with maxfeerate")
321265
fee_exceeds_max = "Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)"
322266

323267
# Test a transaction with a small fee.
324-
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
325-
rawTx = self.nodes[0].getrawtransaction(txId, True)
326-
vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('1.00000000'))
327-
328-
self.sync_all()
329-
inputs = [{"txid": txId, "vout": vout['n']}]
330-
# Fee 10,000 satoshis, (1 - (10000 sat * 0.00000001 BTC/sat)) = 0.9999
331-
outputs = {self.nodes[0].getnewaddress(): Decimal("0.99990000")}
332-
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
333-
rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx)
334-
assert_equal(rawTxSigned['complete'], True)
335-
# Fee 10,000 satoshis, ~100 b transaction, fee rate should land around 100 sat/byte = 0.00100000 BTC/kB
268+
# Fee rate is 0.00100000 BTC/kvB
269+
tx = self.wallet.create_self_transfer(fee_rate=Decimal('0.00100000'))
336270
# Thus, testmempoolaccept should reject
337-
testres = self.nodes[2].testmempoolaccept([rawTxSigned['hex']], 0.00001000)[0]
271+
testres = self.nodes[2].testmempoolaccept([tx['hex']], 0.00001000)[0]
338272
assert_equal(testres['allowed'], False)
339273
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
340274
# and sendrawtransaction should throw
341-
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, rawTxSigned['hex'], 0.00001000)
275+
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, tx['hex'], 0.00001000)
342276
# and the following calls should both succeed
343-
testres = self.nodes[2].testmempoolaccept(rawtxs=[rawTxSigned['hex']])[0]
277+
testres = self.nodes[2].testmempoolaccept(rawtxs=[tx['hex']])[0]
344278
assert_equal(testres['allowed'], True)
345-
self.nodes[2].sendrawtransaction(hexstring=rawTxSigned['hex'])
279+
self.nodes[2].sendrawtransaction(hexstring=tx['hex'])
346280

347281
# Test a transaction with a large fee.
348-
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
349-
rawTx = self.nodes[0].getrawtransaction(txId, True)
350-
vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('1.00000000'))
351-
352-
self.sync_all()
353-
inputs = [{"txid": txId, "vout": vout['n']}]
354-
# Fee 2,000,000 satoshis, (1 - (2000000 sat * 0.00000001 BTC/sat)) = 0.98
355-
outputs = {self.nodes[0].getnewaddress() : Decimal("0.98000000")}
356-
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
357-
rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx)
358-
assert_equal(rawTxSigned['complete'], True)
359-
# Fee 2,000,000 satoshis, ~100 b transaction, fee rate should land around 20,000 sat/byte = 0.20000000 BTC/kB
282+
# Fee rate is 0.20000000 BTC/kvB
283+
tx = self.wallet.create_self_transfer(mempool_valid=False, from_node=self.nodes[0], fee_rate=Decimal('0.20000000'))
360284
# Thus, testmempoolaccept should reject
361-
testres = self.nodes[2].testmempoolaccept([rawTxSigned['hex']])[0]
285+
testres = self.nodes[2].testmempoolaccept([tx['hex']])[0]
362286
assert_equal(testres['allowed'], False)
363287
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
364288
# and sendrawtransaction should throw
365-
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, rawTxSigned['hex'])
289+
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, tx['hex'])
366290
# and the following calls should both succeed
367-
testres = self.nodes[2].testmempoolaccept(rawtxs=[rawTxSigned['hex']], maxfeerate='0.20000000')[0]
291+
testres = self.nodes[2].testmempoolaccept(rawtxs=[tx['hex']], maxfeerate='0.20000000')[0]
368292
assert_equal(testres['allowed'], True)
369-
self.nodes[2].sendrawtransaction(hexstring=rawTxSigned['hex'], maxfeerate='0.20000000')
293+
self.nodes[2].sendrawtransaction(hexstring=tx['hex'], maxfeerate='0.20000000')
370294

371295
self.log.info("Test sendrawtransaction/testmempoolaccept with tx already in the chain")
372296
self.generate(self.nodes[2], 1)
373297
for node in self.nodes:
374-
testres = node.testmempoolaccept([rawTxSigned['hex']])[0]
298+
testres = node.testmempoolaccept([tx['hex']])[0]
375299
assert_equal(testres['allowed'], False)
376300
assert_equal(testres['reject-reason'], 'txn-already-known')
377-
assert_raises_rpc_error(-27, 'Transaction already in block chain', node.sendrawtransaction, rawTxSigned['hex'])
301+
assert_raises_rpc_error(-27, 'Transaction already in block chain', node.sendrawtransaction, tx['hex'])
378302

379303
def decoderawtransaction_tests(self):
380304
self.log.info("Test decoderawtransaction")

test/functional/rpc_signrawtransaction.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,56 @@ def test_signing_with_cltv(self):
334334
assert_equal(signed["complete"], True)
335335
self.nodes[0].sendrawtransaction(signed["hex"])
336336

337+
def test_signing_with_missing_prevtx_info(self):
338+
txid = "1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000"
339+
for type in ["bech32", "p2sh-segwit", "legacy"]:
340+
self.log.info(f"Test signing with missing prevtx info ({type})")
341+
addr = self.nodes[0].getnewaddress("", type)
342+
addrinfo = self.nodes[0].getaddressinfo(addr)
343+
pubkey = addrinfo["scriptPubKey"]
344+
inputs = [{'txid': txid, 'vout': 3, 'sequence': 1000}]
345+
outputs = {self.nodes[0].getnewaddress(): 1}
346+
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
347+
348+
prevtx = dict(txid=txid, scriptPubKey=pubkey, vout=3, amount=1)
349+
succ = self.nodes[0].signrawtransactionwithwallet(rawtx, [prevtx])
350+
assert succ["complete"]
351+
352+
if type == "legacy":
353+
del prevtx["amount"]
354+
succ = self.nodes[0].signrawtransactionwithwallet(rawtx, [prevtx])
355+
assert succ["complete"]
356+
else:
357+
assert_raises_rpc_error(-3, "Missing amount", self.nodes[0].signrawtransactionwithwallet, rawtx, [
358+
{
359+
"txid": txid,
360+
"scriptPubKey": pubkey,
361+
"vout": 3,
362+
}
363+
])
364+
365+
assert_raises_rpc_error(-3, "Missing vout", self.nodes[0].signrawtransactionwithwallet, rawtx, [
366+
{
367+
"txid": txid,
368+
"scriptPubKey": pubkey,
369+
"amount": 1,
370+
}
371+
])
372+
assert_raises_rpc_error(-3, "Missing txid", self.nodes[0].signrawtransactionwithwallet, rawtx, [
373+
{
374+
"scriptPubKey": pubkey,
375+
"vout": 3,
376+
"amount": 1,
377+
}
378+
])
379+
assert_raises_rpc_error(-3, "Missing scriptPubKey", self.nodes[0].signrawtransactionwithwallet, rawtx, [
380+
{
381+
"txid": txid,
382+
"vout": 3,
383+
"amount": 1
384+
}
385+
])
386+
337387
def run_test(self):
338388
self.successful_signing_test()
339389
self.script_verification_error_test()
@@ -343,6 +393,7 @@ def run_test(self):
343393
self.test_fully_signed_tx()
344394
self.test_signing_with_csv()
345395
self.test_signing_with_cltv()
396+
self.test_signing_with_missing_prevtx_info()
346397

347398

348399
if __name__ == '__main__':

test/functional/test_runner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@
183183
'rpc_signrawtransaction.py --legacy-wallet',
184184
'rpc_signrawtransaction.py --descriptors',
185185
'rpc_rawtransaction.py --legacy-wallet',
186-
'rpc_rawtransaction.py --descriptors',
187186
'wallet_groups.py --legacy-wallet',
188187
'wallet_transactiontime_rescan.py --descriptors',
189188
'wallet_transactiontime_rescan.py --legacy-wallet',

0 commit comments

Comments
 (0)