wallet_fundrawtransaction.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2022 The Limenka developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test the fundrawtransaction RPC."""
6
7
8 from decimal import Decimal
9 from itertools import product
10 from math import ceil
11 from test_framework.address import address_to_scriptpubkey
12 from test_framework.blocktools import COINBASE_MATURITY
13
14 from test_framework.descriptors import descsum_create
15 from test_framework.messages import (
16 COIN,
17 CTransaction,
18 CTxOut,
19 )
20 from test_framework.test_framework import LimenkaTestFramework
21 from test_framework.util import (
22 assert_approx,
23 assert_equal,
24 assert_fee_amount,
25 assert_greater_than,
26 assert_greater_than_or_equal,
27 assert_raises_rpc_error,
28 count_bytes,
29 get_fee,
30 )
31 from test_framework.wallet_util import generate_keypair, WalletUnlock
32
33 ERR_NOT_ENOUGH_PRESET_INPUTS = "The preselected coins total amount does not cover the transaction target. " \
34 "Please allow other inputs to be automatically selected or include more coins manually"
35
36 def get_unspent(listunspent, amount):
37 for utx in listunspent:
38 if utx['amount'] == amount:
39 return utx
40 raise AssertionError('Could not find unspent with amount={}'.format(amount))
41
42 class RawTransactionsTest(LimenkaTestFramework):
43 def add_options(self, parser):
44 self.add_wallet_options(parser)
45
46 def set_test_params(self):
47 self.num_nodes = 4
48 self.extra_args = [[
49 "-deprecatedrpc=settxfee",
50 "-minrelaytxfee=0.00001000",
51 ] for i in range(self.num_nodes)]
52 self.setup_clean_chain = True
53 # whitelist peers to speed up tx relay / mempool sync
54 self.noban_tx_relay = True
55 self.rpc_timeout = 90 # to prevent timeouts in `test_transaction_too_large`
56
57 def skip_test_if_missing_module(self):
58 self.skip_if_no_wallet()
59
60 def setup_network(self):
61 self.setup_nodes()
62
63 self.connect_nodes(0, 1)
64 self.connect_nodes(1, 2)
65 self.connect_nodes(0, 2)
66 self.connect_nodes(0, 3)
67
68 def lock_outputs_type(self, wallet, outputtype):
69 """
70 Only allow UTXOs of the given type
71 """
72 if outputtype in ["legacy", "p2pkh", "pkh"]:
73 prefixes = ["pkh(", "sh(multi("]
74 elif outputtype in ["p2sh-segwit", "sh_wpkh"]:
75 prefixes = ["sh(wpkh(", "sh(wsh("]
76 elif outputtype in ["bech32", "wpkh"]:
77 prefixes = ["wpkh(", "wsh("]
78 else:
79 assert False, f"Unknown output type {outputtype}"
80
81 to_lock = []
82 for utxo in wallet.listunspent():
83 if "desc" in utxo:
84 for prefix in prefixes:
85 if utxo["desc"].startswith(prefix):
86 to_lock.append({"txid": utxo["txid"], "vout": utxo["vout"]})
87 wallet.lockunspent(False, to_lock)
88
89 def unlock_utxos(self, wallet):
90 """
91 Unlock all UTXOs except the watchonly one
92 """
93 to_keep = []
94 if self.watchonly_utxo is not None:
95 to_keep.append(self.watchonly_utxo)
96 wallet.lockunspent(True)
97 wallet.lockunspent(False, to_keep)
98
99 def run_test(self):
100 self.watchonly_utxo = None
101 self.log.info("Connect nodes, set fees, generate blocks, and sync")
102 self.min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee']
103 # This test is not meant to test fee estimation and we'd like
104 # to be sure all txs are sent at a consistent desired feerate
105 for node in self.nodes:
106 node.settxfee(self.min_relay_tx_fee)
107
108 # if the fee's positive delta is higher than this value tests will fail,
109 # neg. delta always fail the tests.
110 # The size of the signature of every input may be at most 2 bytes larger
111 # than a minimum sized signature.
112
113 # = 2 bytes * minRelayTxFeePerByte
114 self.fee_tolerance = 2 * self.min_relay_tx_fee / 1000
115
116 self.generate(self.nodes[2], 1)
117 self.generate(self.nodes[0], 121)
118
119 self.test_add_inputs_default_value()
120 self.test_preset_inputs_selection()
121 self.test_weight_calculation()
122 self.test_weight_limits()
123 self.test_change_position()
124 self.test_simple()
125 self.test_simple_two_coins()
126 self.test_simple_two_outputs()
127 self.test_change()
128 self.test_no_change()
129 self.test_invalid_option()
130 self.test_invalid_change_address()
131 self.test_valid_change_address()
132 self.test_change_type()
133 self.test_coin_selection()
134 self.test_two_vin()
135 self.test_two_vin_two_vout()
136 self.test_invalid_input()
137 self.test_fee_p2pkh()
138 self.test_fee_p2pkh_multi_out()
139 self.test_fee_p2sh()
140 self.test_fee_4of5()
141 self.test_spend_2of2()
142 self.test_locked_wallet()
143 self.test_many_inputs_fee()
144 self.test_many_inputs_send()
145 self.test_witness_only()
146 self.test_op_return()
147 self.test_watchonly()
148 self.test_all_watched_funds()
149 self.test_option_feerate()
150 self.test_address_reuse()
151 self.test_option_subtract_fee_from_outputs()
152 self.test_subtract_fee_with_presets()
153 self.test_transaction_too_large()
154 self.test_include_unsafe()
155 self.test_external_inputs()
156 self.test_22670()
157 self.test_feerate_rounding()
158 self.test_input_confs_control()
159 self.test_duplicate_outputs()
160
161 def test_duplicate_outputs(self):
162 self.log.info("Test deserializing and funding a transaction with duplicate outputs")
163 self.nodes[1].createwallet("fundtx_duplicate_outputs")
164 w = self.nodes[1].get_wallet_rpc("fundtx_duplicate_outputs")
165
166 addr = w.getnewaddress(address_type="bech32")
167 self.nodes[0].sendtoaddress(addr, 5)
168 self.generate(self.nodes[0], 1)
169
170 address = self.nodes[0].getnewaddress("bech32")
171 tx = CTransaction()
172 tx.vin = []
173 tx.vout = [CTxOut(1 * COIN, bytearray(address_to_scriptpubkey(address)))] * 2
174 tx.nLockTime = 0
175 tx_hex = tx.serialize().hex()
176 res = w.fundrawtransaction(tx_hex, add_inputs=True)
177 signed_res = w.signrawtransactionwithwallet(res["hex"])
178 txid = w.sendrawtransaction(signed_res["hex"])
179 assert self.nodes[1].getrawtransaction(txid)
180
181 self.log.info("Test SFFO with duplicate outputs")
182
183 res_sffo = w.fundrawtransaction(tx_hex, add_inputs=True, subtractFeeFromOutputs=[0,1])
184 signed_res_sffo = w.signrawtransactionwithwallet(res_sffo["hex"])
185 txid_sffo = w.sendrawtransaction(signed_res_sffo["hex"])
186 assert self.nodes[1].getrawtransaction(txid_sffo)
187
188 def test_change_position(self):
189 """Ensure setting changePosition in fundraw with an exact match is handled properly."""
190 self.log.info("Test fundrawtxn changePosition option")
191 rawmatch = self.nodes[2].createrawtransaction([], {self.nodes[2].getnewaddress():50})
192 rawmatch = self.nodes[2].fundrawtransaction(rawmatch, changePosition=1, subtractFeeFromOutputs=[0])
193 assert_equal(rawmatch["changepos"], -1)
194
195 self.nodes[3].createwallet(wallet_name="wwatch", disable_private_keys=True)
196 wwatch = self.nodes[3].get_wallet_rpc('wwatch')
197 watchonly_address = self.nodes[0].getnewaddress()
198 watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)["pubkey"]
199 self.watchonly_amount = Decimal(200)
200 wwatch.importpubkey(watchonly_pubkey, "", True)
201 self.watchonly_utxo = self.create_outpoints(self.nodes[0], outputs=[{watchonly_address: self.watchonly_amount}])[0]
202
203 # Lock UTXO so nodes[0] doesn't accidentally spend it
204 self.nodes[0].lockunspent(False, [self.watchonly_utxo])
205
206 self.nodes[0].sendtoaddress(self.nodes[3].get_wallet_rpc(self.default_wallet_name).getnewaddress(), self.watchonly_amount / 10)
207
208 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5)
209 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
210 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0)
211
212 self.generate(self.nodes[0], 1)
213
214 wwatch.unloadwallet()
215
216 def test_simple(self):
217 self.log.info("Test fundrawtxn")
218 inputs = [ ]
219 outputs = { self.nodes[0].getnewaddress() : 1.0 }
220 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
221 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
222 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
223 assert len(dec_tx['vin']) > 0 #test that we have enough inputs
224
225 def check_witness_inputs(self, vins):
226 for vin in vins:
227 # check vin is a segwit input
228 utxo = self.nodes[2].gettxout(vin['txid'], vin['vout'])
229 info = self.nodes[2].getaddressinfo(utxo['scriptPubKey']['address'])
230 if not (info['iswitness'] or info['embedded']['iswitness']):
231 return False
232
233 return True
234
235 def test_witness_only(self):
236 self.log.info("Testing fundrawtxn with witness inputs only")
237
238 self.generate(self.nodes[0], COINBASE_MATURITY + 10)
239 self.nodes[2].sendall(recipients=[self.nodes[0].getnewaddress()])
240
241 output_types = ['legacy', 'p2sh-segwit', 'bech32']
242 if self.options.descriptors:
243 output_types.append('bech32m')
244 # Create coins
245 for _ in range(10):
246 for output_type in output_types:
247 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(address_type=output_type), 1)
248
249 self.generate(self.nodes[0], 1)
250
251 inputs = [ ]
252 target_addr = self.nodes[2].getnewaddress()
253 segwit_balance = (len(output_types) - 1) * 10
254
255 # make sure legacy inputs are not accepted in witness only mode if no witness inputs are found
256 # trying to spend more than segwit total should fail
257 outputs = { target_addr : segwit_balance + Decimal('0.00000001') }
258 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
259 assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawtx, {'segwit_inputs_only': True, 'subtractFeeFromOutputs': [0]})
260
261 # make sure all inputs are of type witness
262 outputs = { target_addr : segwit_balance }
263 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
264 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, {'segwit_inputs_only': True, 'subtractFeeFromOutputs': [0]})
265 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
266
267 assert len(dec_tx['vin']) > 0
268 assert(self.check_witness_inputs(dec_tx['vin']))
269
270
271 def test_simple_two_coins(self):
272 self.log.info("Test fundrawtxn with 2 coins")
273 inputs = [ ]
274 outputs = { self.nodes[0].getnewaddress() : 2.2 }
275 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
276 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
277 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
278 assert len(dec_tx['vin']) > 0 #test if we have enough inputs
279 assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
280
281 def test_simple_two_outputs(self):
282 self.log.info("Test fundrawtxn with 2 outputs")
283
284 inputs = [ ]
285 outputs = { self.nodes[0].getnewaddress() : 2.6, self.nodes[1].getnewaddress() : 2.5 }
286 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
287
288 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
289 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
290
291 assert len(dec_tx['vin']) > 0
292 assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
293
294 def test_change(self):
295 self.log.info("Test fundrawtxn with a vin > required amount")
296 utx = get_unspent(self.nodes[2].listunspent(), 5)
297
298 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
299 outputs = { self.nodes[0].getnewaddress() : 1.0 }
300 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
301 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
302 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
303
304 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
305 fee = rawtxfund['fee']
306 self.test_no_change_fee = fee # Use the same fee for the next tx
307 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
308 totalOut = 0
309 for out in dec_tx['vout']:
310 totalOut += out['value']
311
312 assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
313
314 def test_no_change(self):
315 self.log.info("Test fundrawtxn not having a change output")
316 utx = get_unspent(self.nodes[2].listunspent(), 5)
317
318 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
319 outputs = {self.nodes[0].getnewaddress(): Decimal(5.0) - self.test_no_change_fee - self.fee_tolerance}
320 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
321 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
322 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
323
324 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
325 fee = rawtxfund['fee']
326 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
327 totalOut = 0
328 for out in dec_tx['vout']:
329 totalOut += out['value']
330
331 assert_equal(rawtxfund['changepos'], -1)
332 assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
333
334 def test_invalid_option(self):
335 self.log.info("Test fundrawtxn with an invalid option")
336 utx = get_unspent(self.nodes[2].listunspent(), 5)
337
338 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
339 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
340 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
341 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
342 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
343
344 assert_raises_rpc_error(-8, "Unknown named parameter foo", self.nodes[2].fundrawtransaction, rawtx, foo='bar')
345
346 # reserveChangeKey was deprecated and is now removed
347 assert_raises_rpc_error(-8, "Unknown named parameter reserveChangeKey", lambda: self.nodes[2].fundrawtransaction(hexstring=rawtx, reserveChangeKey=True))
348
349 def test_invalid_change_address(self):
350 self.log.info("Test fundrawtxn with an invalid change address")
351 utx = get_unspent(self.nodes[2].listunspent(), 5)
352
353 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
354 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
355 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
356 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
357 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
358
359 assert_raises_rpc_error(-5, "Change address must be a valid limenka address", self.nodes[2].fundrawtransaction, rawtx, changeAddress='foobar')
360
361 def test_valid_change_address(self):
362 self.log.info("Test fundrawtxn with a provided change address")
363 utx = get_unspent(self.nodes[2].listunspent(), 5)
364
365 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
366 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
367 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
368 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
369 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
370
371 change = self.nodes[2].getnewaddress()
372 assert_raises_rpc_error(-8, "changePosition out of bounds", self.nodes[2].fundrawtransaction, rawtx, changeAddress=change, changePosition=2)
373 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, changeAddress=change, changePosition=0)
374 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
375 out = dec_tx['vout'][0]
376 assert_equal(change, out['scriptPubKey']['address'])
377
378 def test_change_type(self):
379 self.log.info("Test fundrawtxn with a provided change type")
380 utx = get_unspent(self.nodes[2].listunspent(), 5)
381
382 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
383 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
384 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
385 assert_raises_rpc_error(-3, "JSON value of type null is not of expected type string", self.nodes[2].fundrawtransaction, rawtx, change_type=None)
386 assert_raises_rpc_error(-5, "Unknown change type ''", self.nodes[2].fundrawtransaction, rawtx, change_type='')
387 rawtx = self.nodes[2].fundrawtransaction(rawtx, change_type='bech32')
388 dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex'])
389 assert_equal('witness_v0_keyhash', dec_tx['vout'][rawtx['changepos']]['scriptPubKey']['type'])
390
391 def test_coin_selection(self):
392 self.log.info("Test fundrawtxn with a vin < required amount")
393 utx = get_unspent(self.nodes[2].listunspent(), 1)
394
395 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
396 outputs = { self.nodes[0].getnewaddress() : 1.0 }
397 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
398
399 # 4-byte version + 1-byte vin count + 36-byte prevout then script_len
400 rawtx = rawtx[:82] + "0100" + rawtx[84:]
401
402 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
403 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
404 assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
405
406 # Should fail without add_inputs:
407 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, self.nodes[2].fundrawtransaction, rawtx, add_inputs=False)
408 # add_inputs is enabled by default
409 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
410
411 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
412 matchingOuts = 0
413 for i, out in enumerate(dec_tx['vout']):
414 if out['scriptPubKey']['address'] in outputs:
415 matchingOuts+=1
416 else:
417 assert_equal(i, rawtxfund['changepos'])
418
419 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
420 assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
421
422 assert_equal(matchingOuts, 1)
423 assert_equal(len(dec_tx['vout']), 2)
424
425 def test_two_vin(self):
426 self.log.info("Test fundrawtxn with 2 vins")
427 utx = get_unspent(self.nodes[2].listunspent(), 1)
428 utx2 = get_unspent(self.nodes[2].listunspent(), 5)
429
430 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
431 outputs = { self.nodes[0].getnewaddress() : 6.0 }
432 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
433 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
434 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
435
436 # Should fail without add_inputs:
437 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, self.nodes[2].fundrawtransaction, rawtx, add_inputs=False)
438 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, add_inputs=True)
439 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
440 matchingOuts = 0
441 for out in dec_tx['vout']:
442 if out['scriptPubKey']['address'] in outputs:
443 matchingOuts+=1
444
445 assert_equal(matchingOuts, 1)
446 assert_equal(len(dec_tx['vout']), 2)
447
448 matchingIns = 0
449 for vinOut in dec_tx['vin']:
450 for vinIn in inputs:
451 if vinIn['txid'] == vinOut['txid']:
452 matchingIns+=1
453
454 assert_equal(matchingIns, 2) #we now must see two vins identical to vins given as params
455
456 def test_two_vin_two_vout(self):
457 self.log.info("Test fundrawtxn with 2 vins and 2 vouts")
458 utx = get_unspent(self.nodes[2].listunspent(), 1)
459 utx2 = get_unspent(self.nodes[2].listunspent(), 5)
460
461 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
462 outputs = { self.nodes[0].getnewaddress() : 6.0, self.nodes[0].getnewaddress() : 1.0 }
463 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
464 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
465 assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
466
467 # Should fail without add_inputs:
468 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, self.nodes[2].fundrawtransaction, rawtx, add_inputs=False)
469 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, add_inputs=True)
470
471 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
472 matchingOuts = 0
473 for out in dec_tx['vout']:
474 if out['scriptPubKey']['address'] in outputs:
475 matchingOuts+=1
476
477 assert_equal(matchingOuts, 2)
478 assert_equal(len(dec_tx['vout']), 3)
479
480 def test_invalid_input(self):
481 self.log.info("Test fundrawtxn with an invalid vin")
482 txid = "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1"
483 vout = 0
484 inputs = [ {'txid' : txid, 'vout' : vout} ] #invalid vin!
485 outputs = { self.nodes[0].getnewaddress() : 1.0}
486 rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
487 assert_raises_rpc_error(-4, "Unable to find UTXO for external input", self.nodes[2].fundrawtransaction, rawtx)
488
489 def test_fee_p2pkh(self):
490 """Compare fee of a standard pubkeyhash transaction."""
491 self.log.info("Test fundrawtxn p2pkh fee")
492 self.lock_outputs_type(self.nodes[0], "p2pkh")
493 inputs = []
494 outputs = {self.nodes[1].getnewaddress():1.1}
495 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
496 fundedTx = self.nodes[0].fundrawtransaction(rawtx)
497
498 # Create same transaction over sendtoaddress.
499 txId = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1.1)
500 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base']
501
502 # Compare fee.
503 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
504 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
505
506 self.unlock_utxos(self.nodes[0])
507
508 def test_fee_p2pkh_multi_out(self):
509 """Compare fee of a standard pubkeyhash transaction with multiple outputs."""
510 self.log.info("Test fundrawtxn p2pkh fee with multiple outputs")
511 self.lock_outputs_type(self.nodes[0], "p2pkh")
512 inputs = []
513 outputs = {
514 self.nodes[1].getnewaddress():1.1,
515 self.nodes[1].getnewaddress():1.2,
516 self.nodes[1].getnewaddress():0.1,
517 self.nodes[1].getnewaddress():1.3,
518 self.nodes[1].getnewaddress():0.2,
519 self.nodes[1].getnewaddress():0.3,
520 }
521 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
522 fundedTx = self.nodes[0].fundrawtransaction(rawtx)
523
524 # Create same transaction over sendtoaddress.
525 txId = self.nodes[0].sendmany("", outputs)
526 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base']
527
528 # Compare fee.
529 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
530 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
531
532 self.unlock_utxos(self.nodes[0])
533
534 def test_fee_p2sh(self):
535 """Compare fee of a 2-of-2 multisig p2sh transaction."""
536 self.lock_outputs_type(self.nodes[0], "p2pkh")
537 # Create 2-of-2 addr.
538 addr1 = self.nodes[1].getnewaddress()
539 addr2 = self.nodes[1].getnewaddress()
540
541 addr1Obj = self.nodes[1].getaddressinfo(addr1)
542 addr2Obj = self.nodes[1].getaddressinfo(addr2)
543
544 mSigObj = self.nodes[3].createmultisig(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address']
545
546 inputs = []
547 outputs = {mSigObj:1.1}
548 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
549 fundedTx = self.nodes[0].fundrawtransaction(rawtx)
550
551 # Create same transaction over sendtoaddress.
552 txId = self.nodes[0].sendtoaddress(mSigObj, 1.1)
553 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base']
554
555 # Compare fee.
556 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
557 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
558
559 self.unlock_utxos(self.nodes[0])
560
561 def test_fee_4of5(self):
562 """Compare fee of a standard pubkeyhash transaction."""
563 self.log.info("Test fundrawtxn fee with 4-of-5 addresses")
564 self.lock_outputs_type(self.nodes[0], "p2pkh")
565
566 # Create 4-of-5 addr.
567 addr1 = self.nodes[1].getnewaddress()
568 addr2 = self.nodes[1].getnewaddress()
569 addr3 = self.nodes[1].getnewaddress()
570 addr4 = self.nodes[1].getnewaddress()
571 addr5 = self.nodes[1].getnewaddress()
572
573 addr1Obj = self.nodes[1].getaddressinfo(addr1)
574 addr2Obj = self.nodes[1].getaddressinfo(addr2)
575 addr3Obj = self.nodes[1].getaddressinfo(addr3)
576 addr4Obj = self.nodes[1].getaddressinfo(addr4)
577 addr5Obj = self.nodes[1].getaddressinfo(addr5)
578
579 mSigObj = self.nodes[1].createmultisig(
580 4,
581 [
582 addr1Obj['pubkey'],
583 addr2Obj['pubkey'],
584 addr3Obj['pubkey'],
585 addr4Obj['pubkey'],
586 addr5Obj['pubkey'],
587 ]
588 )['address']
589
590 inputs = []
591 outputs = {mSigObj:1.1}
592 rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
593 fundedTx = self.nodes[0].fundrawtransaction(rawtx)
594
595 # Create same transaction over sendtoaddress.
596 txId = self.nodes[0].sendtoaddress(mSigObj, 1.1)
597 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base']
598
599 # Compare fee.
600 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
601 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
602
603 self.unlock_utxos(self.nodes[0])
604
605 def test_spend_2of2(self):
606 """Spend a 2-of-2 multisig transaction over fundraw."""
607 self.log.info("Test fundpsbt spending 2-of-2 multisig")
608
609 # Create 2-of-2 addr.
610 addr1 = self.nodes[2].getnewaddress()
611 addr2 = self.nodes[2].getnewaddress()
612
613 addr1Obj = self.nodes[2].getaddressinfo(addr1)
614 addr2Obj = self.nodes[2].getaddressinfo(addr2)
615
616 self.nodes[2].createwallet(wallet_name='wmulti', disable_private_keys=True)
617 wmulti = self.nodes[2].get_wallet_rpc('wmulti')
618 w2 = self.nodes[2].get_wallet_rpc(self.default_wallet_name)
619 mSigObj = wmulti.addmultisigaddress(
620 2,
621 [
622 addr1Obj['pubkey'],
623 addr2Obj['pubkey'],
624 ]
625 )['address']
626 if not self.options.descriptors:
627 wmulti.importaddress(mSigObj)
628
629 # Send 1.2 BTC to msig addr.
630 self.nodes[0].sendtoaddress(mSigObj, 1.2)
631 self.generate(self.nodes[0], 1)
632
633 oldBalance = self.nodes[1].getbalance()
634 inputs = []
635 outputs = {self.nodes[1].getnewaddress():1.1}
636 funded_psbt = wmulti.walletcreatefundedpsbt(inputs=inputs, outputs=outputs, changeAddress=w2.getrawchangeaddress())['psbt']
637
638 signed_psbt = w2.walletprocesspsbt(funded_psbt)
639 self.nodes[2].sendrawtransaction(signed_psbt['hex'])
640 self.generate(self.nodes[2], 1)
641
642 # Make sure funds are received at node1.
643 assert_equal(oldBalance+Decimal('1.10000000'), self.nodes[1].getbalance())
644
645 wmulti.unloadwallet()
646
647 def test_locked_wallet(self):
648 self.log.info("Test fundrawtxn with locked wallet and hardened derivation")
649
650 df_wallet = self.nodes[1].get_wallet_rpc(self.default_wallet_name)
651 self.nodes[1].createwallet(wallet_name="locked_wallet", descriptors=self.options.descriptors)
652 wallet = self.nodes[1].get_wallet_rpc("locked_wallet")
653 # This test is not meant to exercise fee estimation. Making sure all txs are sent at a consistent fee rate.
654 wallet.settxfee(self.min_relay_tx_fee)
655
656 # Add some balance to the wallet (this will be reverted at the end of the test)
657 df_wallet.sendall(recipients=[wallet.getnewaddress()])
658 self.generate(self.nodes[1], 1)
659
660 # Encrypt wallet and import descriptors
661 wallet.encryptwallet("test")
662
663 if self.options.descriptors:
664 with WalletUnlock(wallet, "test"):
665 wallet.importdescriptors([{
666 'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPdYeeZbPSKd2KYLmeVKtcFA7kqCxDvDR13MQ6us8HopUR2wLcS2ZKPhLyKsqpDL2FtL73LMHcgoCL7DXsciA8eX8nbjCR2eG/0h/*h)'),
667 'timestamp': 'now',
668 'active': True
669 },
670 {
671 'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPdYeeZbPSKd2KYLmeVKtcFA7kqCxDvDR13MQ6us8HopUR2wLcS2ZKPhLyKsqpDL2FtL73LMHcgoCL7DXsciA8eX8nbjCR2eG/1h/*h)'),
672 'timestamp': 'now',
673 'active': True,
674 'internal': True
675 }])
676
677 # Drain the keypool.
678 wallet.getnewaddress()
679 wallet.getrawchangeaddress()
680
681 # Choose input
682 inputs = wallet.listunspent()
683
684 # Deduce exact fee to produce a changeless transaction
685 tx_size = 110 # Total tx size: 110 vbytes, p2wpkh -> p2wpkh. Input 68 vbytes + rest of tx is 42 vbytes.
686 value = inputs[0]["amount"] - get_fee(tx_size, self.min_relay_tx_fee)
687
688 outputs = {self.nodes[0].getnewaddress():value}
689 rawtx = wallet.createrawtransaction(inputs, outputs)
690 # fund a transaction that does not require a new key for the change output
691 funded_tx = wallet.fundrawtransaction(rawtx)
692 assert_equal(funded_tx["changepos"], -1)
693
694 # fund a transaction that requires a new key for the change output
695 # creating the key must be impossible because the wallet is locked
696 outputs = {self.nodes[0].getnewaddress():value - Decimal("0.1")}
697 rawtx = wallet.createrawtransaction(inputs, outputs)
698 assert_raises_rpc_error(-4, "Transaction needs a change address, but we can't generate it.", wallet.fundrawtransaction, rawtx)
699
700 # Refill the keypool.
701 with WalletUnlock(wallet, "test"):
702 wallet.keypoolrefill(8) #need to refill the keypool to get an internal change address
703
704 assert_raises_rpc_error(-13, "walletpassphrase", wallet.sendtoaddress, self.nodes[0].getnewaddress(), 1.2)
705
706 oldBalance = self.nodes[0].getbalance()
707
708 inputs = []
709 outputs = {self.nodes[0].getnewaddress():1.1}
710 rawtx = wallet.createrawtransaction(inputs, outputs)
711 fundedTx = wallet.fundrawtransaction(rawtx)
712 assert fundedTx["changepos"] != -1
713
714 # Now we need to unlock.
715 with WalletUnlock(wallet, "test"):
716 signedTx = wallet.signrawtransactionwithwallet(fundedTx['hex'])
717 wallet.sendrawtransaction(signedTx['hex'])
718 self.generate(self.nodes[1], 1)
719
720 # Make sure funds are received at node1.
721 assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance())
722
723 # Restore pre-test wallet state
724 wallet.sendall(recipients=[df_wallet.getnewaddress(), df_wallet.getnewaddress(), df_wallet.getnewaddress()])
725 wallet.unloadwallet()
726 self.generate(self.nodes[1], 1)
727
728 def test_many_inputs_fee(self):
729 """Multiple (~19) inputs tx test | Compare fee."""
730 self.log.info("Test fundrawtxn fee with many inputs")
731
732 # Empty node1, send some small coins from node0 to node1.
733 self.nodes[1].sendall(recipients=[self.nodes[0].getnewaddress()])
734 self.generate(self.nodes[1], 1)
735
736 for _ in range(20):
737 self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
738 self.generate(self.nodes[0], 1)
739
740 # Fund a tx with ~20 small inputs.
741 inputs = []
742 outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04}
743 rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
744 fundedTx = self.nodes[1].fundrawtransaction(rawtx)
745
746 # Create same transaction over sendtoaddress.
747 txId = self.nodes[1].sendmany("", outputs)
748 signedFee = self.nodes[1].getmempoolentry(txId)['fees']['base']
749
750 # Compare fee.
751 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
752 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance * 19 #~19 inputs
753
754 def test_many_inputs_send(self):
755 """Multiple (~19) inputs tx test | sign/send."""
756 self.log.info("Test fundrawtxn sign+send with many inputs")
757
758 # Again, empty node1, send some small coins from node0 to node1.
759 self.nodes[1].sendall(recipients=[self.nodes[0].getnewaddress()])
760 self.generate(self.nodes[1], 1)
761
762 for _ in range(20):
763 self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01)
764 self.generate(self.nodes[0], 1)
765
766 # Fund a tx with ~20 small inputs.
767 oldBalance = self.nodes[0].getbalance()
768
769 inputs = []
770 outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04}
771 rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
772 fundedTx = self.nodes[1].fundrawtransaction(rawtx)
773 fundedAndSignedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex'])
774 self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex'])
775 self.generate(self.nodes[1], 1)
776 assert_equal(oldBalance+Decimal('50.19000000'), self.nodes[0].getbalance()) #0.19+block reward
777
778 def test_op_return(self):
779 self.log.info("Test fundrawtxn with OP_RETURN and no vin")
780
781 rawtx = "0100000000010000000000000000066a047465737400000000"
782 dec_tx = self.nodes[2].decoderawtransaction(rawtx)
783
784 assert_equal(len(dec_tx['vin']), 0)
785 assert_equal(len(dec_tx['vout']), 1)
786
787 rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
788 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
789
790 assert_greater_than(len(dec_tx['vin']), 0) # at least one vin
791 assert_equal(len(dec_tx['vout']), 2) # one change output added
792
793 def test_watchonly(self):
794 self.log.info("Test fundrawtxn using only watchonly")
795
796 inputs = []
797 outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount / 2}
798 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
799
800 self.nodes[3].loadwallet('wwatch')
801 wwatch = self.nodes[3].get_wallet_rpc('wwatch')
802 # Setup change addresses for the watchonly wallet
803 desc_import = [{
804 "desc": descsum_create("wpkh(tpubD6NzVbkrYhZ4YNXVQbNhMK1WqguFsUXceaVJKbmno2aZ3B6QfbMeraaYvnBSGpV3vxLyTTK9DYT1yoEck4XUScMzXoQ2U2oSmE2JyMedq3H/1/*)"),
805 "timestamp": "now",
806 "internal": True,
807 "active": True,
808 "keypool": True,
809 "range": [0, 100],
810 "watchonly": True,
811 }]
812 if self.options.descriptors:
813 wwatch.importdescriptors(desc_import)
814 else:
815 wwatch.importmulti(desc_import)
816
817 # Backward compatibility test (2nd params is includeWatching)
818 result = wwatch.fundrawtransaction(rawtx, True)
819 res_dec = self.nodes[0].decoderawtransaction(result["hex"])
820 assert_equal(len(res_dec["vin"]), 1)
821 assert_equal(res_dec["vin"][0]["txid"], self.watchonly_utxo['txid'])
822
823 assert "fee" in result.keys()
824 assert_greater_than(result["changepos"], -1)
825
826 wwatch.unloadwallet()
827
828 def test_all_watched_funds(self):
829 self.log.info("Test fundrawtxn using entirety of watched funds")
830
831 inputs = []
832 outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount}
833 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
834
835 self.nodes[3].loadwallet('wwatch')
836 wwatch = self.nodes[3].get_wallet_rpc('wwatch')
837 w3 = self.nodes[3].get_wallet_rpc(self.default_wallet_name)
838 result = wwatch.fundrawtransaction(rawtx, includeWatching=True, changeAddress=w3.getrawchangeaddress(), subtractFeeFromOutputs=[0])
839 res_dec = self.nodes[0].decoderawtransaction(result["hex"])
840 assert_equal(len(res_dec["vin"]), 1)
841 assert res_dec["vin"][0]["txid"] == self.watchonly_utxo['txid']
842
843 assert_greater_than(result["fee"], 0)
844 assert_equal(result["changepos"], -1)
845 assert_equal(result["fee"] + res_dec["vout"][0]["value"], self.watchonly_amount)
846
847 signedtx = wwatch.signrawtransactionwithwallet(result["hex"])
848 assert not signedtx["complete"]
849 signedtx = self.nodes[0].signrawtransactionwithwallet(signedtx["hex"])
850 assert signedtx["complete"]
851 self.nodes[0].sendrawtransaction(signedtx["hex"])
852 self.generate(self.nodes[0], 1)
853
854 wwatch.unloadwallet()
855
856 def test_option_feerate(self):
857 self.log.info("Test fundrawtxn with explicit fee rates (fee_rate sat/vB and feeRate BTC/kvB)")
858 node = self.nodes[3]
859 # Make sure there is exactly one input so coin selection can't skew the result.
860 assert_equal(len(self.nodes[3].listunspent(1)), 1)
861 inputs = []
862 outputs = {node.getnewaddress() : 1}
863 rawtx = node.createrawtransaction(inputs, outputs)
864
865 result = node.fundrawtransaction(rawtx) # uses self.min_relay_tx_fee (set by settxfee)
866 btc_kvb_to_sat_vb = 100000 # (1e5)
867 result1 = node.fundrawtransaction(rawtx, fee_rate=str(2 * btc_kvb_to_sat_vb * self.min_relay_tx_fee))
868 result2 = node.fundrawtransaction(rawtx, feeRate=2 * self.min_relay_tx_fee)
869 result3 = node.fundrawtransaction(rawtx, fee_rate=10 * btc_kvb_to_sat_vb * self.min_relay_tx_fee)
870 result4 = node.fundrawtransaction(rawtx, feeRate=str(10 * self.min_relay_tx_fee))
871
872 result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
873 assert_fee_amount(result1['fee'], count_bytes(result1['hex']), 2 * result_fee_rate)
874 assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
875 assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)
876 assert_fee_amount(result4['fee'], count_bytes(result4['hex']), 10 * result_fee_rate)
877
878 # Test that funding non-standard "zero-fee" transactions is valid.
879 for param, zero_value in product(["fee_rate", "feeRate"], [0, 0.000, 0.00000000, "0", "0.000", "0.00000000"]):
880 assert_equal(self.nodes[3].fundrawtransaction(rawtx, {param: zero_value})["fee"], 0)
881
882 # With no arguments passed, expect fee of 141 satoshis.
883 assert_approx(node.fundrawtransaction(rawtx)["fee"], vexp=0.00000141, vspan=0.00000001)
884 # Expect fee to be 10,000x higher when an explicit fee rate 10,000x greater is specified.
885 result = node.fundrawtransaction(rawtx, fee_rate=10000)
886 assert_approx(result["fee"], vexp=0.0141, vspan=0.0001)
887
888 self.log.info("Test fundrawtxn with invalid estimate_mode settings")
889 for k, v in {"number": 42, "object": {"foo": "bar"}}.items():
890 assert_raises_rpc_error(-3, f"JSON value of type {k} for field estimate_mode is not of expected type string",
891 node.fundrawtransaction, rawtx, estimate_mode=v, conf_target=0.1, add_inputs=True)
892 for mode in ["", "foo", Decimal("3.141592")]:
893 assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
894 node.fundrawtransaction, rawtx, estimate_mode=mode, conf_target=0.1, add_inputs=True)
895
896 self.log.info("Test fundrawtxn with invalid conf_target settings")
897 for mode in ["unset", "economical", "conservative"]:
898 self.log.debug("{}".format(mode))
899 for k, v in {"string": "", "object": {"foo": "bar"}}.items():
900 assert_raises_rpc_error(-3, f"JSON value of type {k} for field conf_target is not of expected type number",
901 node.fundrawtransaction, rawtx, estimate_mode=mode, conf_target=v, add_inputs=True)
902 for n in [-1, 0, 1009]:
903 assert_raises_rpc_error(-8, "Invalid conf_target, must be between 1 and 1008", # max value of 1008 per src/policy/fees.h
904 node.fundrawtransaction, rawtx, estimate_mode=mode, conf_target=n, add_inputs=True)
905
906 self.log.info("Test invalid fee rate settings")
907 for param, value in {("fee_rate", 100000), ("feeRate", 1.000)}:
908 assert_raises_rpc_error(-4, "Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)",
909 node.fundrawtransaction, rawtx, add_inputs=True, **{param: value})
910 assert_raises_rpc_error(-3, "Amount out of range",
911 node.fundrawtransaction, rawtx, add_inputs=True, **{param: -1})
912 assert_raises_rpc_error(-3, "Amount is not a number or string",
913 node.fundrawtransaction, rawtx, add_inputs=True, **{param: {"foo": "bar"}})
914 # Test fee rate values that don't pass fixed-point parsing checks.
915 for invalid_value in ["", 0.000000001, 1e-09, 1.111111111, 1111111111111111, "31.999999999999999999999"]:
916 assert_raises_rpc_error(-3, "Invalid amount", node.fundrawtransaction, rawtx, add_inputs=True, **{param: invalid_value})
917 # Test fee_rate values that cannot be represented in sat/vB.
918 for invalid_value in [0.0001, 0.00000001, 0.00099999, 31.99999999]:
919 assert_raises_rpc_error(-3, "Invalid amount",
920 node.fundrawtransaction, rawtx, fee_rate=invalid_value, add_inputs=True)
921
922 self.log.info("Test min fee rate checks are bypassed with fundrawtxn, e.g. a fee_rate under 1 sat/vB is allowed")
923 node.fundrawtransaction(rawtx, fee_rate=0.999, add_inputs=True)
924 node.fundrawtransaction(rawtx, feeRate=0.00000999, add_inputs=True)
925
926 self.log.info("- raises RPC error if both feeRate and fee_rate are passed")
927 assert_raises_rpc_error(-8, "Cannot specify both fee_rate (sat/vB) and feeRate (BTC/kvB)",
928 node.fundrawtransaction, rawtx, fee_rate=0.1, feeRate=0.1, add_inputs=True)
929
930 self.log.info("- raises RPC error if both feeRate and estimate_mode passed")
931 assert_raises_rpc_error(-8, "Cannot specify both estimate_mode and feeRate",
932 node.fundrawtransaction, rawtx, estimate_mode="economical", feeRate=0.1, add_inputs=True)
933
934 for param in ["feeRate", "fee_rate"]:
935 self.log.info("- raises RPC error if both {} and conf_target are passed".format(param))
936 assert_raises_rpc_error(-8, "Cannot specify both conf_target and {}. Please provide either a confirmation "
937 "target in blocks for automatic fee estimation, or an explicit fee rate.".format(param),
938 node.fundrawtransaction, rawtx, {param: 1, "conf_target": 1, "add_inputs": True})
939
940 self.log.info("- raises RPC error if both fee_rate and estimate_mode are passed")
941 assert_raises_rpc_error(-8, "Cannot specify both estimate_mode and fee_rate",
942 node.fundrawtransaction, rawtx, fee_rate=1, estimate_mode="economical", add_inputs=True)
943
944 def test_address_reuse(self):
945 """Test no address reuse occurs."""
946 self.log.info("Test fundrawtxn does not reuse addresses")
947
948 rawtx = self.nodes[3].createrawtransaction(inputs=[], outputs={self.nodes[3].getnewaddress(): 1})
949 result3 = self.nodes[3].fundrawtransaction(rawtx)
950 res_dec = self.nodes[0].decoderawtransaction(result3["hex"])
951 changeaddress = ""
952 for out in res_dec['vout']:
953 if out['value'] > 1.0:
954 changeaddress += out['scriptPubKey']['address']
955 assert changeaddress != ""
956 nextaddr = self.nodes[3].getnewaddress()
957 # Now the change address key should be removed from the keypool.
958 assert changeaddress != nextaddr
959
960 def test_option_subtract_fee_from_outputs(self):
961 self.log.info("Test fundrawtxn subtractFeeFromOutputs option")
962
963 # Make sure there is exactly one input so coin selection can't skew the result.
964 assert_equal(len(self.nodes[3].listunspent(1)), 1)
965
966 inputs = []
967 outputs = {self.nodes[2].getnewaddress(): 1}
968 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
969
970 # Test subtract fee from outputs with feeRate (BTC/kvB)
971 result = [self.nodes[3].fundrawtransaction(rawtx), # uses self.min_relay_tx_fee (set by settxfee)
972 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[]), # empty subtraction list
973 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[0]), # uses self.min_relay_tx_fee (set by settxfee)
974 self.nodes[3].fundrawtransaction(rawtx, feeRate=2 * self.min_relay_tx_fee),
975 self.nodes[3].fundrawtransaction(rawtx, feeRate=2 * self.min_relay_tx_fee, subtractFeeFromOutputs=[0]),]
976 dec_tx = [self.nodes[3].decoderawtransaction(tx_['hex']) for tx_ in result]
977 output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)]
978 change = [d['vout'][r['changepos']]['value'] for d, r in zip(dec_tx, result)]
979
980 assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee'])
981 assert_equal(result[3]['fee'], result[4]['fee'])
982 assert_equal(change[0], change[1])
983 assert_equal(output[0], output[1])
984 assert_equal(output[0], output[2] + result[2]['fee'])
985 assert_equal(change[0] + result[0]['fee'], change[2])
986 assert_equal(output[3], output[4] + result[4]['fee'])
987 assert_equal(change[3] + result[3]['fee'], change[4])
988
989 # Test subtract fee from outputs with fee_rate (sat/vB)
990 btc_kvb_to_sat_vb = 100000 # (1e5)
991 result = [self.nodes[3].fundrawtransaction(rawtx), # uses self.min_relay_tx_fee (set by settxfee)
992 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[]), # empty subtraction list
993 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[0]), # uses self.min_relay_tx_fee (set by settxfee)
994 self.nodes[3].fundrawtransaction(rawtx, fee_rate=2 * btc_kvb_to_sat_vb * self.min_relay_tx_fee),
995 self.nodes[3].fundrawtransaction(rawtx, fee_rate=2 * btc_kvb_to_sat_vb * self.min_relay_tx_fee, subtractFeeFromOutputs=[0]),]
996 dec_tx = [self.nodes[3].decoderawtransaction(tx_['hex']) for tx_ in result]
997 output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)]
998 change = [d['vout'][r['changepos']]['value'] for d, r in zip(dec_tx, result)]
999
1000 assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee'])
1001 assert_equal(result[3]['fee'], result[4]['fee'])
1002 assert_equal(change[0], change[1])
1003 assert_equal(output[0], output[1])
1004 assert_equal(output[0], output[2] + result[2]['fee'])
1005 assert_equal(change[0] + result[0]['fee'], change[2])
1006 assert_equal(output[3], output[4] + result[4]['fee'])
1007 assert_equal(change[3] + result[3]['fee'], change[4])
1008
1009 inputs = []
1010 outputs = {self.nodes[2].getnewaddress(): value for value in (1.0, 1.1, 1.2, 1.3)}
1011 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
1012
1013 result = [self.nodes[3].fundrawtransaction(rawtx),
1014 # Split the fee between outputs 0, 2, and 3, but not output 1.
1015 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[0, 2, 3])]
1016
1017 dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']),
1018 self.nodes[3].decoderawtransaction(result[1]['hex'])]
1019
1020 # Nested list of non-change output amounts for each transaction.
1021 output = [[out['value'] for i, out in enumerate(d['vout']) if i != r['changepos']]
1022 for d, r in zip(dec_tx, result)]
1023
1024 # List of differences in output amounts between normal and subtractFee transactions.
1025 share = [o0 - o1 for o0, o1 in zip(output[0], output[1])]
1026
1027 # Output 1 is the same in both transactions.
1028 assert_equal(share[1], 0)
1029
1030 # The other 3 outputs are smaller as a result of subtractFeeFromOutputs.
1031 assert_greater_than(share[0], 0)
1032 assert_greater_than(share[2], 0)
1033 assert_greater_than(share[3], 0)
1034
1035 # Outputs 2 and 3 take the same share of the fee.
1036 assert_equal(share[2], share[3])
1037
1038 # Output 0 takes at least as much share of the fee, and no more than 2
1039 # satoshis more, than outputs 2 and 3.
1040 assert_greater_than_or_equal(share[0], share[2])
1041 assert_greater_than_or_equal(share[2] + Decimal(2e-8), share[0])
1042
1043 # The fee is the same in both transactions.
1044 assert_equal(result[0]['fee'], result[1]['fee'])
1045
1046 # The total subtracted from the outputs is equal to the fee.
1047 assert_equal(share[0] + share[2] + share[3], result[0]['fee'])
1048
1049 # test funding with custom min_conf
1050 inputs = []
1051 outputs = {self.nodes[2].getnewaddress(): 1}
1052 rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
1053 unspent = self.nodes[3].listunspent()
1054 assert len(unspent) == 1
1055 input_confs = unspent[0]['confirmations']
1056 assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[3].fundrawtransaction, rawtx, {'min_conf': input_confs + 1})
1057 result = self.nodes[3].fundrawtransaction(rawtx, {'min_conf': input_confs})
1058
1059 def test_subtract_fee_with_presets(self):
1060 self.log.info("Test fundrawtxn subtract fee from outputs with preset inputs that are sufficient")
1061
1062 addr = self.nodes[0].getnewaddress()
1063 utxo = self.create_outpoints(self.nodes[0], outputs=[{addr: 10}])[0]
1064
1065 rawtx = self.nodes[0].createrawtransaction([utxo], [{self.nodes[0].getnewaddress(): 5}])
1066 fundedtx = self.nodes[0].fundrawtransaction(rawtx, subtractFeeFromOutputs=[0])
1067 signedtx = self.nodes[0].signrawtransactionwithwallet(fundedtx['hex'])
1068 self.nodes[0].sendrawtransaction(signedtx['hex'])
1069
1070 def test_transaction_too_large(self):
1071 self.log.info("Test fundrawtx where BnB solution would result in a too large transaction, but Knapsack would not")
1072 self.nodes[0].createwallet("large")
1073 wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
1074 recipient = self.nodes[0].get_wallet_rpc("large")
1075 outputs = {}
1076 rawtx = recipient.createrawtransaction([], {wallet.getnewaddress(): 147.99899260})
1077
1078 # Make 1500 0.1 BTC outputs. The amount that we target for funding is in
1079 # the BnB range when these outputs are used. However if these outputs
1080 # are selected, the transaction will end up being too large, so it
1081 # shouldn't use BnB and instead fall back to Knapsack but that behavior
1082 # is not implemented yet. For now we just check that we get an error.
1083 # First, force the wallet to bulk-generate the addresses we'll need.
1084 recipient.keypoolrefill(1500)
1085 for _ in range(1500):
1086 outputs[recipient.getnewaddress()] = 0.1
1087 wallet.sendmany("", outputs)
1088 self.generate(self.nodes[0], 10)
1089 assert_raises_rpc_error(-4, "The inputs size exceeds the maximum weight. "
1090 "Please try sending a smaller amount or manually consolidating your wallet's UTXOs",
1091 recipient.fundrawtransaction, rawtx)
1092 self.nodes[0].unloadwallet("large")
1093
1094 def test_external_inputs(self):
1095 self.log.info("Test funding with external inputs")
1096 privkey, _ = generate_keypair(wif=True)
1097 self.nodes[2].createwallet("extfund")
1098 wallet = self.nodes[2].get_wallet_rpc("extfund")
1099
1100 # Make a weird but signable script. sh(pkh()) descriptor accomplishes this
1101 desc = descsum_create("sh(pkh({}))".format(privkey))
1102 if self.options.descriptors:
1103 res = self.nodes[0].importdescriptors([{"desc": desc, "timestamp": "now"}])
1104 else:
1105 res = self.nodes[0].importmulti([{"desc": desc, "timestamp": "now"}])
1106 assert res[0]["success"]
1107 addr = self.nodes[0].deriveaddresses(desc)[0]
1108 addr_info = self.nodes[0].getaddressinfo(addr)
1109
1110 self.nodes[0].sendtoaddress(addr, 10)
1111 self.nodes[0].sendtoaddress(wallet.getnewaddress(), 10)
1112 self.generate(self.nodes[0], 6)
1113 ext_utxo = self.nodes[0].listunspent(addresses=[addr])[0]
1114
1115 # An external input without solving data should result in an error
1116 raw_tx = wallet.createrawtransaction([ext_utxo], {self.nodes[0].getnewaddress(): ext_utxo["amount"] / 2})
1117 assert_raises_rpc_error(-4, "Not solvable pre-selected input COutPoint(%s, %s)" % (ext_utxo["txid"][0:10], ext_utxo["vout"]), wallet.fundrawtransaction, raw_tx)
1118
1119 # Error conditions
1120 assert_raises_rpc_error(-5, 'Pubkey "not a pubkey" must be a hex string', wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["not a pubkey"]})
1121 assert_raises_rpc_error(-5, 'Pubkey "01234567890a0b0c0d0e0f" must have a length of either 33 or 65 bytes', wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["01234567890a0b0c0d0e0f"]})
1122 assert_raises_rpc_error(-5, "'not a script' is not hex", wallet.fundrawtransaction, raw_tx, solving_data={"scripts":["not a script"]})
1123 assert_raises_rpc_error(-8, "Unable to parse descriptor 'not a descriptor'", wallet.fundrawtransaction, raw_tx, solving_data={"descriptors":["not a descriptor"]})
1124 assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"]}])
1125 assert_raises_rpc_error(-8, "Invalid parameter, vout cannot be negative", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": -1}])
1126 assert_raises_rpc_error(-8, "Invalid parameter, missing weight key", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"]}])
1127 assert_raises_rpc_error(-8, "Invalid parameter, weight cannot be less than 165", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": 164}])
1128 assert_raises_rpc_error(-8, "Invalid parameter, weight cannot be less than 165", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": -1}])
1129 assert_raises_rpc_error(-8, "Invalid parameter, weight cannot be greater than", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": 400001}])
1130
1131 # But funding should work when the solving data is provided
1132 funded_tx = wallet.fundrawtransaction(raw_tx, solving_data={"pubkeys": [addr_info['pubkey']], "scripts": [addr_info["embedded"]["scriptPubKey"]]})
1133 signed_tx = wallet.signrawtransactionwithwallet(funded_tx['hex'])
1134 assert not signed_tx['complete']
1135 signed_tx = self.nodes[0].signrawtransactionwithwallet(signed_tx['hex'])
1136 assert signed_tx['complete']
1137
1138 funded_tx = wallet.fundrawtransaction(raw_tx, solving_data={"descriptors": [desc]})
1139 signed_tx1 = wallet.signrawtransactionwithwallet(funded_tx['hex'])
1140 assert not signed_tx1['complete']
1141 signed_tx2 = self.nodes[0].signrawtransactionwithwallet(signed_tx1['hex'])
1142 assert signed_tx2['complete']
1143
1144 unsigned_weight = self.nodes[0].decoderawtransaction(signed_tx1["hex"])["weight"]
1145 signed_weight = self.nodes[0].decoderawtransaction(signed_tx2["hex"])["weight"]
1146 # Input's weight is difference between weight of signed and unsigned,
1147 # and the weight of stuff that didn't change (prevout, sequence, 1 byte of scriptSig)
1148 input_weight = signed_weight - unsigned_weight + (41 * 4)
1149 low_input_weight = input_weight // 2
1150 high_input_weight = input_weight * 2
1151
1152 # Funding should also work if the input weight is provided
1153 funded_tx = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": input_weight}], fee_rate=2)
1154 signed_tx = wallet.signrawtransactionwithwallet(funded_tx["hex"])
1155 signed_tx = self.nodes[0].signrawtransactionwithwallet(signed_tx["hex"])
1156 assert_equal(self.nodes[0].testmempoolaccept([signed_tx["hex"]])[0]["allowed"], True)
1157 assert_equal(signed_tx["complete"], True)
1158 # Reducing the weight should have a lower fee
1159 funded_tx2 = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": low_input_weight}], fee_rate=2)
1160 assert_greater_than(funded_tx["fee"], funded_tx2["fee"])
1161 # Increasing the weight should have a higher fee
1162 funded_tx2 = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": high_input_weight}], fee_rate=2)
1163 assert_greater_than(funded_tx2["fee"], funded_tx["fee"])
1164 # The provided weight should override the calculated weight when solving data is provided
1165 funded_tx3 = wallet.fundrawtransaction(raw_tx, solving_data={"descriptors": [desc]}, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": high_input_weight}], fee_rate=2)
1166 assert_equal(funded_tx2["fee"], funded_tx3["fee"])
1167 # The feerate should be met
1168 funded_tx4 = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": high_input_weight}], fee_rate=10)
1169 input_add_weight = high_input_weight - (41 * 4)
1170 tx4_weight = wallet.decoderawtransaction(funded_tx4["hex"])["weight"] + input_add_weight
1171 tx4_vsize = int(ceil(tx4_weight / 4))
1172 assert_fee_amount(funded_tx4["fee"], tx4_vsize, Decimal(0.0001))
1173
1174 # Funding with weight at csuint boundaries should not cause problems
1175 funded_tx = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": 255}], fee_rate=2)
1176 funded_tx = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": 65539}], fee_rate=2)
1177
1178 self.nodes[2].unloadwallet("extfund")
1179
1180 def test_add_inputs_default_value(self):
1181 self.log.info("Test 'add_inputs' default value")
1182
1183 # Create and fund the wallet with 5 BTC
1184 self.nodes[2].createwallet("test_preset_inputs")
1185 wallet = self.nodes[2].get_wallet_rpc("test_preset_inputs")
1186 addr1 = wallet.getnewaddress(address_type="bech32")
1187 self.nodes[0].sendtoaddress(addr1, 5)
1188 self.generate(self.nodes[0], 1)
1189
1190 # Covered cases:
1191 # 1. Default add_inputs value with no preset inputs (add_inputs=true):
1192 # Expect: automatically add coins from the wallet to the tx.
1193 # 2. Default add_inputs value with preset inputs (add_inputs=false):
1194 # Expect: disallow automatic coin selection.
1195 # 3. Explicit add_inputs=true and preset inputs (with preset inputs not-covering the target amount).
1196 # Expect: include inputs from the wallet.
1197 # 4. Explicit add_inputs=true and preset inputs (with preset inputs covering the target amount).
1198 # Expect: only preset inputs are used.
1199 # 5. Explicit add_inputs=true, no preset inputs (same as (1) but with an explicit set):
1200 # Expect: include inputs from the wallet.
1201 # 6. Explicit add_inputs=false, no preset inputs:
1202 # Expect: failure as we did not provide inputs and the process cannot automatically select coins.
1203
1204 # Case (1), 'send' command
1205 # 'add_inputs' value is true unless "inputs" are specified, in such case, add_inputs=false.
1206 # So, the wallet will automatically select coins and create the transaction if only the outputs are provided.
1207 tx = wallet.send(outputs=[{addr1: 3}])
1208 assert tx["complete"]
1209
1210 # Case (2), 'send' command
1211 # Select an input manually, which doesn't cover the entire output amount and
1212 # verify that the dynamically set 'add_inputs=false' value works.
1213
1214 # Fund wallet with 2 outputs, 5 BTC each.
1215 addr2 = wallet.getnewaddress(address_type="bech32")
1216 source_tx = self.nodes[0].send(outputs=[{addr1: 5}, {addr2: 5}], change_position=0)
1217 self.generate(self.nodes[0], 1)
1218
1219 # Select only one input.
1220 options = {
1221 "inputs": [
1222 {
1223 "txid": source_tx["txid"],
1224 "vout": 1 # change position was hardcoded to index 0
1225 }
1226 ]
1227 }
1228 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.send, outputs=[{addr1: 8}], **options)
1229
1230 # Case (3), Explicit add_inputs=true and preset inputs (with preset inputs not-covering the target amount)
1231 options["add_inputs"] = True
1232 options["add_to_wallet"] = False
1233 tx = wallet.send(outputs=[{addr1: 8}], **options)
1234 assert tx["complete"]
1235
1236 # Case (4), Explicit add_inputs=true and preset inputs (with preset inputs covering the target amount)
1237 options["inputs"].append({
1238 "txid": source_tx["txid"],
1239 "vout": 2 # change position was hardcoded to index 0
1240 })
1241 tx = wallet.send(outputs=[{addr1: 8}], **options)
1242 assert tx["complete"]
1243 # Check that only the preset inputs were added to the tx
1244 decoded_psbt_inputs = self.nodes[0].decodepsbt(tx["psbt"])['tx']['vin']
1245 assert_equal(len(decoded_psbt_inputs), 2)
1246 for input in decoded_psbt_inputs:
1247 assert_equal(input["txid"], source_tx["txid"])
1248
1249 # Case (5), assert that inputs are added to the tx by explicitly setting add_inputs=true
1250 options = {"add_inputs": True, "add_to_wallet": True}
1251 tx = wallet.send(outputs=[{addr1: 8}], **options)
1252 assert tx["complete"]
1253
1254 # 6. Explicit add_inputs=false, no preset inputs:
1255 options = {"add_inputs": False}
1256 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.send, outputs=[{addr1: 3}], **options)
1257
1258 ################################################
1259
1260 # Case (1), 'walletcreatefundedpsbt' command
1261 # Default add_inputs value with no preset inputs (add_inputs=true)
1262 inputs = []
1263 outputs = {self.nodes[1].getnewaddress(): 8}
1264 assert "psbt" in wallet.walletcreatefundedpsbt(inputs=inputs, outputs=outputs)
1265
1266 # Case (2), 'walletcreatefundedpsbt' command
1267 # Default add_inputs value with preset inputs (add_inputs=false).
1268 inputs = [{
1269 "txid": source_tx["txid"],
1270 "vout": 1 # change position was hardcoded to index 0
1271 }]
1272 outputs = {self.nodes[1].getnewaddress(): 8}
1273 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.walletcreatefundedpsbt, inputs=inputs, outputs=outputs)
1274
1275 # Case (3), Explicit add_inputs=true and preset inputs (with preset inputs not-covering the target amount)
1276 options["add_inputs"] = True
1277 assert "psbt" in wallet.walletcreatefundedpsbt(outputs=[{addr1: 8}], inputs=inputs, **options)
1278
1279 # Case (4), Explicit add_inputs=true and preset inputs (with preset inputs covering the target amount)
1280 inputs.append({
1281 "txid": source_tx["txid"],
1282 "vout": 2 # change position was hardcoded to index 0
1283 })
1284 psbt_tx = wallet.walletcreatefundedpsbt(outputs=[{addr1: 8}], inputs=inputs, **options)
1285 # Check that only the preset inputs were added to the tx
1286 decoded_psbt_inputs = self.nodes[0].decodepsbt(psbt_tx["psbt"])['tx']['vin']
1287 assert_equal(len(decoded_psbt_inputs), 2)
1288 for input in decoded_psbt_inputs:
1289 assert_equal(input["txid"], source_tx["txid"])
1290
1291 # Case (5), 'walletcreatefundedpsbt' command
1292 # Explicit add_inputs=true, no preset inputs
1293 options = {
1294 "add_inputs": True
1295 }
1296 assert "psbt" in wallet.walletcreatefundedpsbt(inputs=[], outputs=outputs, **options)
1297
1298 # Case (6). Explicit add_inputs=false, no preset inputs:
1299 options = {"add_inputs": False}
1300 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.walletcreatefundedpsbt, inputs=[], outputs=outputs, **options)
1301
1302 self.nodes[2].unloadwallet("test_preset_inputs")
1303
1304 def test_preset_inputs_selection(self):
1305 self.log.info('Test wallet preset inputs are not double-counted or reused in coin selection')
1306
1307 # Create and fund the wallet with 4 UTXO of 5 BTC each (20 BTC total)
1308 self.nodes[2].createwallet("test_preset_inputs_selection")
1309 wallet = self.nodes[2].get_wallet_rpc("test_preset_inputs_selection")
1310 outputs = {}
1311 for _ in range(4):
1312 outputs[wallet.getnewaddress(address_type="bech32")] = 5
1313 self.nodes[0].sendmany("", outputs)
1314 self.generate(self.nodes[0], 1)
1315
1316 # Select the preset inputs
1317 coins = wallet.listunspent()
1318 preset_inputs = [coins[0], coins[1], coins[2]]
1319
1320 # Now let's create the tx creation options
1321 options = {
1322 "inputs": preset_inputs,
1323 "add_inputs": True, # automatically add coins from the wallet to fulfill the target
1324 "subtract_fee_from_outputs": [0], # deduct fee from first output
1325 "add_to_wallet": False
1326 }
1327
1328 # Attempt to send 29 BTC from a wallet that only has 20 BTC. The wallet should exclude
1329 # the preset inputs from the pool of available coins, realize that there is not enough
1330 # money to fund the 29 BTC payment, and fail with "Insufficient funds".
1331 #
1332 # Even with SFFO, the wallet can only afford to send 20 BTC.
1333 # If the wallet does not properly exclude preset inputs from the pool of available coins
1334 # prior to coin selection, it may create a transaction that does not fund the full payment
1335 # amount or, through SFFO, incorrectly reduce the recipient's amount by the difference
1336 # between the original target and the wrongly counted inputs (in this case 9 BTC)
1337 # so that the recipient's amount is no longer equal to the user's selected target of 29 BTC.
1338
1339 # First case, use 'subtract_fee_from_outputs = true'
1340 assert_raises_rpc_error(-4, "Insufficient funds", wallet.send, outputs=[{wallet.getnewaddress(address_type="bech32"): 29}], options=options)
1341
1342 # Second case, don't use 'subtract_fee_from_outputs'
1343 del options["subtract_fee_from_outputs"]
1344 assert_raises_rpc_error(-4, "Insufficient funds", wallet.send, outputs=[{wallet.getnewaddress(address_type="bech32"): 29}], options=options)
1345
1346 self.nodes[2].unloadwallet("test_preset_inputs_selection")
1347
1348 def test_weight_calculation(self):
1349 self.log.info("Test weight calculation with external inputs")
1350
1351 self.nodes[2].createwallet("test_weight_calculation")
1352 wallet = self.nodes[2].get_wallet_rpc("test_weight_calculation")
1353
1354 addr = wallet.getnewaddress(address_type="bech32")
1355 ext_addr = self.nodes[0].getnewaddress(address_type="bech32")
1356 utxo, ext_utxo = self.create_outpoints(self.nodes[0], outputs=[{addr: 5}, {ext_addr: 5}])
1357
1358 self.nodes[0].sendtoaddress(wallet.getnewaddress(address_type="bech32"), 5)
1359 self.generate(self.nodes[0], 1)
1360
1361 rawtx = wallet.createrawtransaction([utxo], [{self.nodes[0].getnewaddress(address_type="bech32"): 8}])
1362 fundedtx = wallet.fundrawtransaction(rawtx, fee_rate=10, change_type="bech32")
1363 # with 71-byte signatures we should expect following tx size
1364 # tx overhead (10) + 2 inputs (41 each) + 2 p2wpkh (31 each) + (segwit marker and flag (2) + 2 p2wpkh 71 byte sig witnesses (107 each)) / witness scaling factor (4)
1365 tx_size = ceil(10 + 41*2 + 31*2 + (2 + 107*2)/4)
1366 assert_equal(fundedtx['fee'] * COIN, tx_size * 10)
1367
1368 # Using the other output should have 72 byte sigs
1369 rawtx = wallet.createrawtransaction([ext_utxo], [{self.nodes[0].getnewaddress(): 13}])
1370 ext_desc = self.nodes[0].getaddressinfo(ext_addr)["desc"]
1371 fundedtx = wallet.fundrawtransaction(rawtx, fee_rate=10, change_type="bech32", solving_data={"descriptors": [ext_desc]})
1372 # tx overhead (10) + 3 inputs (41 each) + 2 p2wpkh(31 each) + (segwit marker and flag (2) + 2 p2wpkh 71 bytes sig witnesses (107 each) + p2wpkh 72 byte sig witness (108)) / witness scaling factor (4)
1373 tx_size = ceil(10 + 41*3 + 31*2 + (2 + 107*2 + 108)/4)
1374 assert_equal(fundedtx['fee'] * COIN, tx_size * 10)
1375
1376 self.nodes[2].unloadwallet("test_weight_calculation")
1377
1378 def test_weight_limits(self):
1379 self.log.info("Test weight limits")
1380
1381 self.nodes[2].createwallet("test_weight_limits")
1382 wallet = self.nodes[2].get_wallet_rpc("test_weight_limits")
1383
1384 outputs = []
1385 for _ in range(1472):
1386 outputs.append({wallet.getnewaddress(address_type="legacy"): 0.1})
1387 txid = self.nodes[0].send(outputs=outputs, change_position=0)["txid"]
1388 self.generate(self.nodes[0], 1)
1389
1390 # 272 WU per input (273 when high-s); picking 1471 inputs will exceed the max standard tx weight.
1391 rawtx = wallet.createrawtransaction([], [{wallet.getnewaddress(): 0.1 * 1471}])
1392
1393 # 1) Try to fund transaction only using the preset inputs (pick all 1472 inputs to cover the fee)
1394 input_weights = []
1395 for i in range(1, 1473): # skip first output as it is the parent tx change output
1396 input_weights.append({"txid": txid, "vout": i, "weight": 273})
1397 assert_raises_rpc_error(-4, "Transaction too large", wallet.fundrawtransaction, hexstring=rawtx, input_weights=input_weights)
1398
1399 # 2) Let the wallet fund the transaction
1400 assert_raises_rpc_error(-4, "The inputs size exceeds the maximum weight. Please try sending a smaller amount or manually consolidating your wallet's UTXOs",
1401 wallet.fundrawtransaction, hexstring=rawtx)
1402
1403 # 3) Pre-select some inputs and let the wallet fill-up the remaining amount
1404 inputs = input_weights[0:1000]
1405 assert_raises_rpc_error(-4, "The combination of the pre-selected inputs and the wallet automatic inputs selection exceeds the transaction maximum weight. Please try sending a smaller amount or manually consolidating your wallet's UTXOs",
1406 wallet.fundrawtransaction, hexstring=rawtx, input_weights=inputs)
1407
1408 self.nodes[2].unloadwallet("test_weight_limits")
1409
1410 def test_include_unsafe(self):
1411 self.log.info("Test fundrawtxn with unsafe inputs")
1412
1413 self.nodes[0].createwallet("unsafe")
1414 wallet = self.nodes[0].get_wallet_rpc("unsafe")
1415
1416 # We receive unconfirmed funds from external keys (unsafe outputs).
1417 addr = wallet.getnewaddress()
1418 inputs = []
1419 for i in range(0, 2):
1420 utxo = self.create_outpoints(self.nodes[2], outputs=[{addr: 5}])[0]
1421 inputs.append((utxo['txid'], utxo['vout']))
1422 self.sync_mempools()
1423
1424 # Unsafe inputs are ignored by default.
1425 rawtx = wallet.createrawtransaction([], [{self.nodes[2].getnewaddress(): 7.5}])
1426 assert_raises_rpc_error(-4, "Insufficient funds", wallet.fundrawtransaction, rawtx)
1427
1428 # But we can opt-in to use them for funding.
1429 fundedtx = wallet.fundrawtransaction(rawtx, include_unsafe=True)
1430 tx_dec = wallet.decoderawtransaction(fundedtx['hex'])
1431 assert all((txin["txid"], txin["vout"]) in inputs for txin in tx_dec["vin"])
1432 signedtx = wallet.signrawtransactionwithwallet(fundedtx['hex'])
1433 assert wallet.testmempoolaccept([signedtx['hex']])[0]["allowed"]
1434
1435 # And we can also use them once they're confirmed.
1436 self.generate(self.nodes[0], 1)
1437 fundedtx = wallet.fundrawtransaction(rawtx, include_unsafe=False)
1438 tx_dec = wallet.decoderawtransaction(fundedtx['hex'])
1439 assert all((txin["txid"], txin["vout"]) in inputs for txin in tx_dec["vin"])
1440 signedtx = wallet.signrawtransactionwithwallet(fundedtx['hex'])
1441 assert wallet.testmempoolaccept([signedtx['hex']])[0]["allowed"]
1442 self.nodes[0].unloadwallet("unsafe")
1443
1444 def test_22670(self):
1445 # In issue #22670, it was observed that ApproximateBestSubset may
1446 # choose enough value to cover the target amount but not enough to cover the transaction fees.
1447 # This leads to a transaction whose actual transaction feerate is lower than expected.
1448 # However at normal feerates, the difference between the effective value and the real value
1449 # that this bug is not detected because the transaction fee must be at least 0.01 BTC (the minimum change value).
1450 # Otherwise the targeted minimum change value will be enough to cover the transaction fees that were not
1451 # being accounted for. So the minimum relay fee is set to 0.1 BTC/kvB in this test.
1452 self.log.info("Test issue 22670 ApproximateBestSubset bug")
1453 # Make sure the default wallet will not be loaded when restarted with a high minrelaytxfee
1454 self.nodes[0].unloadwallet(self.default_wallet_name, False)
1455 feerate = Decimal("0.1")
1456 self.restart_node(0, [f"-minrelaytxfee={feerate}", "-discardfee=0"]) # Set high minrelayfee, set discardfee to 0 for easier calculation
1457
1458 self.nodes[0].loadwallet(self.default_wallet_name, True)
1459 funds = self.nodes[0].get_wallet_rpc(self.default_wallet_name)
1460 self.nodes[0].createwallet(wallet_name="tester")
1461 tester = self.nodes[0].get_wallet_rpc("tester")
1462
1463 # Because this test is specifically for ApproximateBestSubset, the target value must be greater
1464 # than any single input available, and require more than 1 input. So we make 3 outputs
1465 for i in range(0, 3):
1466 funds.sendtoaddress(tester.getnewaddress(address_type="bech32"), 1)
1467 self.generate(self.nodes[0], 1, sync_fun=self.no_op)
1468
1469 # Create transactions in order to calculate fees for the target bounds that can trigger this bug
1470 change_tx = tester.fundrawtransaction(tester.createrawtransaction([], [{funds.getnewaddress(): 1.5}]))
1471 tx = tester.createrawtransaction([], [{funds.getnewaddress(): 2}])
1472 no_change_tx = tester.fundrawtransaction(tx, subtractFeeFromOutputs=[0])
1473
1474 overhead_fees = feerate * len(tx) / 2 / 1000
1475 cost_of_change = change_tx["fee"] - no_change_tx["fee"]
1476 fees = no_change_tx["fee"]
1477 assert_greater_than(fees, 0.01)
1478
1479 def do_fund_send(target):
1480 create_tx = tester.createrawtransaction([], [{funds.getnewaddress(): target}])
1481 funded_tx = tester.fundrawtransaction(create_tx)
1482 signed_tx = tester.signrawtransactionwithwallet(funded_tx["hex"])
1483 assert signed_tx["complete"]
1484 decoded_tx = tester.decoderawtransaction(signed_tx["hex"])
1485 assert_equal(len(decoded_tx["vin"]), 3)
1486 assert tester.testmempoolaccept([signed_tx["hex"]])[0]["allowed"]
1487
1488 # We want to choose more value than is available in 2 inputs when considering the fee,
1489 # but not enough to need 3 inputs when not considering the fee.
1490 # So the target value must be at least 2.00000001 - fee.
1491 lower_bound = Decimal("2.00000001") - fees
1492 # The target value must be at most 2 - cost_of_change - not_input_fees - min_change (these are all
1493 # included in the target before ApproximateBestSubset).
1494 upper_bound = Decimal("2.0") - cost_of_change - overhead_fees - Decimal("0.01")
1495 assert_greater_than_or_equal(upper_bound, lower_bound)
1496 do_fund_send(lower_bound)
1497 do_fund_send(upper_bound)
1498
1499 self.restart_node(0)
1500 self.connect_nodes(0, 1)
1501 self.connect_nodes(0, 2)
1502 self.connect_nodes(0, 3)
1503
1504 def test_feerate_rounding(self):
1505 self.log.info("Test that rounding of GetFee does not result in an assertion")
1506
1507 self.nodes[1].createwallet("roundtest")
1508 w = self.nodes[1].get_wallet_rpc("roundtest")
1509
1510 addr = w.getnewaddress(address_type="bech32")
1511 self.nodes[0].sendtoaddress(addr, 1)
1512 self.generate(self.nodes[0], 1)
1513
1514 # A P2WPKH input costs 68 vbytes; With a single P2WPKH output, the rest of the tx is 42 vbytes for a total of 110 vbytes.
1515 # At a feerate of 1.85 sat/vb, the input will need a fee of 125.8 sats and the rest 77.7 sats
1516 # The entire tx fee should be 203.5 sats.
1517 # Coin selection rounds the fee individually instead of at the end (due to how CFeeRate::GetFee works).
1518 # If rounding down (which is the incorrect behavior), then the calculated fee will be 125 + 77 = 202.
1519 # If rounding up, then the calculated fee will be 126 + 78 = 204.
1520 # In the former case, the calculated needed fee is higher than the actual fee being paid, so an assertion is reached
1521 # To test this does not happen, we subtract 202 sats from the input value. If working correctly, this should
1522 # fail with insufficient funds rather than limenkad asserting.
1523 rawtx = w.createrawtransaction(inputs=[], outputs=[{self.nodes[0].getnewaddress(address_type="bech32"): 1 - 0.00000202}])
1524 assert_raises_rpc_error(-4, "Insufficient funds", w.fundrawtransaction, rawtx, fee_rate=1.85)
1525
1526 def test_input_confs_control(self):
1527 self.nodes[0].createwallet("minconf")
1528 wallet = self.nodes[0].get_wallet_rpc("minconf")
1529
1530 # Fund the wallet with different chain heights
1531 for _ in range(2):
1532 self.nodes[2].sendmany("", {wallet.getnewaddress():1, wallet.getnewaddress():1})
1533 self.generate(self.nodes[2], 1)
1534
1535 unconfirmed_txid = wallet.sendtoaddress(wallet.getnewaddress(), 0.5)
1536
1537 self.log.info("Crafting TX using an unconfirmed input")
1538 target_address = self.nodes[2].getnewaddress()
1539 raw_tx1 = wallet.createrawtransaction([], {target_address: 0.1}, 0, True)
1540 funded_tx1 = wallet.fundrawtransaction(raw_tx1, {'fee_rate': 1, 'maxconf': 0})['hex']
1541
1542 # Make sure we only had the one input
1543 tx1_inputs = self.nodes[0].decoderawtransaction(funded_tx1)['vin']
1544 assert_equal(len(tx1_inputs), 1)
1545
1546 utxo1 = tx1_inputs[0]
1547 assert unconfirmed_txid == utxo1['txid']
1548
1549 final_tx1 = wallet.signrawtransactionwithwallet(funded_tx1)['hex']
1550 txid1 = self.nodes[0].sendrawtransaction(final_tx1)
1551
1552 mempool = self.nodes[0].getrawmempool()
1553 assert txid1 in mempool
1554
1555 self.log.info("Fail to craft a new TX with minconf above highest one")
1556 # Create a replacement tx to 'final_tx1' that has 1 BTC target instead of 0.1.
1557 raw_tx2 = wallet.createrawtransaction([{'txid': utxo1['txid'], 'vout': utxo1['vout']}], {target_address: 1})
1558 assert_raises_rpc_error(-4, "Insufficient funds", wallet.fundrawtransaction, raw_tx2, {'add_inputs': True, 'minconf': 3, 'fee_rate': 10})
1559
1560 self.log.info("Fail to broadcast a new TX with maxconf 0 due to BIP125 rules to verify it actually chose unconfirmed outputs")
1561 # Now fund 'raw_tx2' to fulfill the total target (1 BTC) by using all the wallet unconfirmed outputs.
1562 # As it was created with the first unconfirmed output, 'raw_tx2' only has 0.1 BTC covered (need to fund 0.9 BTC more).
1563 # So, the selection process, to cover the amount, will pick up the 'final_tx1' output as well, which is an output of the tx that this
1564 # new tx is replacing!. So, once we send it to the mempool, it will return a "bad-txns-spends-conflicting-tx"
1565 # because the input will no longer exist once the first tx gets replaced by this new one).
1566 funded_invalid = wallet.fundrawtransaction(raw_tx2, {'add_inputs': True, 'maxconf': 0, 'fee_rate': 10})['hex']
1567 final_invalid = wallet.signrawtransactionwithwallet(funded_invalid)['hex']
1568 assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, final_invalid)
1569
1570 self.log.info("Craft a replacement adding inputs with highest depth possible")
1571 funded_tx2 = wallet.fundrawtransaction(raw_tx2, {'add_inputs': True, 'minconf': 2, 'fee_rate': 10})['hex']
1572 tx2_inputs = self.nodes[0].decoderawtransaction(funded_tx2)['vin']
1573 assert_greater_than_or_equal(len(tx2_inputs), 2)
1574 for vin in tx2_inputs:
1575 if vin['txid'] != unconfirmed_txid:
1576 assert_greater_than_or_equal(self.nodes[0].gettxout(vin['txid'], vin['vout'])['confirmations'], 2)
1577
1578 final_tx2 = wallet.signrawtransactionwithwallet(funded_tx2)['hex']
1579 txid2 = self.nodes[0].sendrawtransaction(final_tx2)
1580
1581 mempool = self.nodes[0].getrawmempool()
1582 assert txid1 not in mempool
1583 assert txid2 in mempool
1584
1585 wallet.unloadwallet()
1586
1587 if __name__ == '__main__':
1588 RawTransactionsTest(__file__).main()
1589