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