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