wallet_fundrawtransaction.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2014-2022 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_approx, 22 assert_equal, 23 assert_fee_amount, 24 assert_greater_than, 25 assert_greater_than_or_equal, 26 assert_raises_rpc_error, 27 count_bytes, 28 get_fee, 29 ) 30 from test_framework.wallet_util import generate_keypair, WalletUnlock 31 32 ERR_NOT_ENOUGH_PRESET_INPUTS = "The preselected coins total amount does not cover the transaction target. " \ 33 "Please allow other inputs to be automatically selected or include more coins manually" 34 35 def get_unspent(listunspent, amount): 36 for utx in listunspent: 37 if utx['amount'] == amount: 38 return utx 39 raise AssertionError('Could not find unspent with amount={}'.format(amount)) 40 41 class RawTransactionsTest(BitcoinTestFramework): 42 def add_options(self, parser): 43 self.add_wallet_options(parser) 44 45 def set_test_params(self): 46 self.num_nodes = 4 47 self.setup_clean_chain = True 48 # whitelist peers to speed up tx relay / mempool sync 49 self.noban_tx_relay = True 50 self.rpc_timeout = 90 # to prevent timeouts in `test_transaction_too_large` 51 52 def skip_test_if_missing_module(self): 53 self.skip_if_no_wallet() 54 55 def setup_network(self): 56 self.setup_nodes() 57 58 self.connect_nodes(0, 1) 59 self.connect_nodes(1, 2) 60 self.connect_nodes(0, 2) 61 self.connect_nodes(0, 3) 62 63 def lock_outputs_type(self, wallet, outputtype): 64 """ 65 Only allow UTXOs of the given type 66 """ 67 if outputtype in ["legacy", "p2pkh", "pkh"]: 68 prefixes = ["pkh(", "sh(multi("] 69 elif outputtype in ["p2sh-segwit", "sh_wpkh"]: 70 prefixes = ["sh(wpkh(", "sh(wsh("] 71 elif outputtype in ["bech32", "wpkh"]: 72 prefixes = ["wpkh(", "wsh("] 73 else: 74 assert False, f"Unknown output type {outputtype}" 75 76 to_lock = [] 77 for utxo in wallet.listunspent(): 78 if "desc" in utxo: 79 for prefix in prefixes: 80 if utxo["desc"].startswith(prefix): 81 to_lock.append({"txid": utxo["txid"], "vout": utxo["vout"]}) 82 wallet.lockunspent(False, to_lock) 83 84 def unlock_utxos(self, wallet): 85 """ 86 Unlock all UTXOs except the watchonly one 87 """ 88 to_keep = [] 89 if self.watchonly_utxo is not None: 90 to_keep.append(self.watchonly_utxo) 91 wallet.lockunspent(True) 92 wallet.lockunspent(False, to_keep) 93 94 def run_test(self): 95 self.watchonly_utxo = None 96 self.log.info("Connect nodes, set fees, generate blocks, and sync") 97 self.min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee'] 98 # This test is not meant to test fee estimation and we'd like 99 # to be sure all txs are sent at a consistent desired feerate 100 for node in self.nodes: 101 node.settxfee(self.min_relay_tx_fee) 102 103 # if the fee's positive delta is higher than this value tests will fail, 104 # neg. delta always fail the tests. 105 # The size of the signature of every input may be at most 2 bytes larger 106 # than a minimum sized signature. 107 108 # = 2 bytes * minRelayTxFeePerByte 109 self.fee_tolerance = 2 * self.min_relay_tx_fee / 1000 110 111 self.generate(self.nodes[2], 1) 112 self.generate(self.nodes[0], 121) 113 114 self.test_add_inputs_default_value() 115 self.test_preset_inputs_selection() 116 self.test_weight_calculation() 117 self.test_change_position() 118 self.test_simple() 119 self.test_simple_two_coins() 120 self.test_simple_two_outputs() 121 self.test_change() 122 self.test_no_change() 123 self.test_invalid_option() 124 self.test_invalid_change_address() 125 self.test_valid_change_address() 126 self.test_change_type() 127 self.test_coin_selection() 128 self.test_two_vin() 129 self.test_two_vin_two_vout() 130 self.test_invalid_input() 131 self.test_fee_p2pkh() 132 self.test_fee_p2pkh_multi_out() 133 self.test_fee_p2sh() 134 self.test_fee_4of5() 135 self.test_spend_2of2() 136 self.test_locked_wallet() 137 self.test_many_inputs_fee() 138 self.test_many_inputs_send() 139 self.test_op_return() 140 self.test_watchonly() 141 self.test_all_watched_funds() 142 self.test_option_feerate() 143 self.test_address_reuse() 144 self.test_option_subtract_fee_from_outputs() 145 self.test_subtract_fee_with_presets() 146 self.test_transaction_too_large() 147 self.test_include_unsafe() 148 self.test_external_inputs() 149 self.test_22670() 150 self.test_feerate_rounding() 151 self.test_input_confs_control() 152 self.test_duplicate_outputs() 153 154 def test_duplicate_outputs(self): 155 self.log.info("Test deserializing and funding a transaction with duplicate outputs") 156 self.nodes[1].createwallet("fundtx_duplicate_outputs") 157 w = self.nodes[1].get_wallet_rpc("fundtx_duplicate_outputs") 158 159 addr = w.getnewaddress(address_type="bech32") 160 self.nodes[0].sendtoaddress(addr, 5) 161 self.generate(self.nodes[0], 1) 162 163 address = self.nodes[0].getnewaddress("bech32") 164 tx = CTransaction() 165 tx.vin = [] 166 tx.vout = [CTxOut(1 * COIN, bytearray(address_to_scriptpubkey(address)))] * 2 167 tx.nLockTime = 0 168 tx_hex = tx.serialize().hex() 169 res = w.fundrawtransaction(tx_hex, add_inputs=True) 170 signed_res = w.signrawtransactionwithwallet(res["hex"]) 171 txid = w.sendrawtransaction(signed_res["hex"]) 172 assert self.nodes[1].getrawtransaction(txid) 173 174 self.log.info("Test SFFO with duplicate outputs") 175 176 res_sffo = w.fundrawtransaction(tx_hex, add_inputs=True, subtractFeeFromOutputs=[0,1]) 177 signed_res_sffo = w.signrawtransactionwithwallet(res_sffo["hex"]) 178 txid_sffo = w.sendrawtransaction(signed_res_sffo["hex"]) 179 assert self.nodes[1].getrawtransaction(txid_sffo) 180 181 def test_change_position(self): 182 """Ensure setting changePosition in fundraw with an exact match is handled properly.""" 183 self.log.info("Test fundrawtxn changePosition option") 184 rawmatch = self.nodes[2].createrawtransaction([], {self.nodes[2].getnewaddress():50}) 185 rawmatch = self.nodes[2].fundrawtransaction(rawmatch, changePosition=1, subtractFeeFromOutputs=[0]) 186 assert_equal(rawmatch["changepos"], -1) 187 188 self.nodes[3].createwallet(wallet_name="wwatch", disable_private_keys=True) 189 wwatch = self.nodes[3].get_wallet_rpc('wwatch') 190 watchonly_address = self.nodes[0].getnewaddress() 191 watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)["pubkey"] 192 self.watchonly_amount = Decimal(200) 193 wwatch.importpubkey(watchonly_pubkey, "", True) 194 self.watchonly_utxo = self.create_outpoints(self.nodes[0], outputs=[{watchonly_address: self.watchonly_amount}])[0] 195 196 # Lock UTXO so nodes[0] doesn't accidentally spend it 197 self.nodes[0].lockunspent(False, [self.watchonly_utxo]) 198 199 self.nodes[0].sendtoaddress(self.nodes[3].get_wallet_rpc(self.default_wallet_name).getnewaddress(), self.watchonly_amount / 10) 200 201 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5) 202 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0) 203 self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0) 204 205 self.generate(self.nodes[0], 1) 206 207 wwatch.unloadwallet() 208 209 def test_simple(self): 210 self.log.info("Test fundrawtxn") 211 inputs = [ ] 212 outputs = { self.nodes[0].getnewaddress() : 1.0 } 213 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 214 rawtxfund = self.nodes[2].fundrawtransaction(rawtx) 215 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 216 assert len(dec_tx['vin']) > 0 #test that we have enough inputs 217 218 def test_simple_two_coins(self): 219 self.log.info("Test fundrawtxn with 2 coins") 220 inputs = [ ] 221 outputs = { self.nodes[0].getnewaddress() : 2.2 } 222 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 223 rawtxfund = self.nodes[2].fundrawtransaction(rawtx) 224 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 225 assert len(dec_tx['vin']) > 0 #test if we have enough inputs 226 assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') 227 228 def test_simple_two_outputs(self): 229 self.log.info("Test fundrawtxn with 2 outputs") 230 231 inputs = [ ] 232 outputs = { self.nodes[0].getnewaddress() : 2.6, self.nodes[1].getnewaddress() : 2.5 } 233 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 234 235 rawtxfund = self.nodes[2].fundrawtransaction(rawtx) 236 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 237 238 assert len(dec_tx['vin']) > 0 239 assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') 240 241 def test_change(self): 242 self.log.info("Test fundrawtxn with a vin > required amount") 243 utx = get_unspent(self.nodes[2].listunspent(), 5) 244 245 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}] 246 outputs = { self.nodes[0].getnewaddress() : 1.0 } 247 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 248 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 249 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 250 251 rawtxfund = self.nodes[2].fundrawtransaction(rawtx) 252 fee = rawtxfund['fee'] 253 self.test_no_change_fee = fee # Use the same fee for the next tx 254 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 255 totalOut = 0 256 for out in dec_tx['vout']: 257 totalOut += out['value'] 258 259 assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee 260 261 def test_no_change(self): 262 self.log.info("Test fundrawtxn not having a change output") 263 utx = get_unspent(self.nodes[2].listunspent(), 5) 264 265 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}] 266 outputs = {self.nodes[0].getnewaddress(): Decimal(5.0) - self.test_no_change_fee - self.fee_tolerance} 267 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 268 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 269 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 270 271 rawtxfund = self.nodes[2].fundrawtransaction(rawtx) 272 fee = rawtxfund['fee'] 273 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 274 totalOut = 0 275 for out in dec_tx['vout']: 276 totalOut += out['value'] 277 278 assert_equal(rawtxfund['changepos'], -1) 279 assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee 280 281 def test_invalid_option(self): 282 self.log.info("Test fundrawtxn with an invalid option") 283 utx = get_unspent(self.nodes[2].listunspent(), 5) 284 285 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ] 286 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) } 287 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 288 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 289 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 290 291 assert_raises_rpc_error(-8, "Unknown named parameter foo", self.nodes[2].fundrawtransaction, rawtx, foo='bar') 292 293 # reserveChangeKey was deprecated and is now removed 294 assert_raises_rpc_error(-8, "Unknown named parameter reserveChangeKey", lambda: self.nodes[2].fundrawtransaction(hexstring=rawtx, reserveChangeKey=True)) 295 296 def test_invalid_change_address(self): 297 self.log.info("Test fundrawtxn with an invalid change address") 298 utx = get_unspent(self.nodes[2].listunspent(), 5) 299 300 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ] 301 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) } 302 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 303 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 304 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 305 306 assert_raises_rpc_error(-5, "Change address must be a valid bitcoin address", self.nodes[2].fundrawtransaction, rawtx, changeAddress='foobar') 307 308 def test_valid_change_address(self): 309 self.log.info("Test fundrawtxn with a provided change address") 310 utx = get_unspent(self.nodes[2].listunspent(), 5) 311 312 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ] 313 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) } 314 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 315 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 316 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 317 318 change = self.nodes[2].getnewaddress() 319 assert_raises_rpc_error(-8, "changePosition out of bounds", self.nodes[2].fundrawtransaction, rawtx, changeAddress=change, changePosition=2) 320 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, changeAddress=change, changePosition=0) 321 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 322 out = dec_tx['vout'][0] 323 assert_equal(change, out['scriptPubKey']['address']) 324 325 def test_change_type(self): 326 self.log.info("Test fundrawtxn with a provided change type") 327 utx = get_unspent(self.nodes[2].listunspent(), 5) 328 329 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ] 330 outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) } 331 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 332 assert_raises_rpc_error(-3, "JSON value of type null is not of expected type string", self.nodes[2].fundrawtransaction, rawtx, change_type=None) 333 assert_raises_rpc_error(-5, "Unknown change type ''", self.nodes[2].fundrawtransaction, rawtx, change_type='') 334 rawtx = self.nodes[2].fundrawtransaction(rawtx, change_type='bech32') 335 dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex']) 336 assert_equal('witness_v0_keyhash', dec_tx['vout'][rawtx['changepos']]['scriptPubKey']['type']) 337 338 def test_coin_selection(self): 339 self.log.info("Test fundrawtxn with a vin < required amount") 340 utx = get_unspent(self.nodes[2].listunspent(), 1) 341 342 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}] 343 outputs = { self.nodes[0].getnewaddress() : 1.0 } 344 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 345 346 # 4-byte version + 1-byte vin count + 36-byte prevout then script_len 347 rawtx = rawtx[:82] + "0100" + rawtx[84:] 348 349 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 350 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 351 assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex']) 352 353 # Should fail without add_inputs: 354 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, self.nodes[2].fundrawtransaction, rawtx, add_inputs=False) 355 # add_inputs is enabled by default 356 rawtxfund = self.nodes[2].fundrawtransaction(rawtx) 357 358 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 359 matchingOuts = 0 360 for i, out in enumerate(dec_tx['vout']): 361 if out['scriptPubKey']['address'] in outputs: 362 matchingOuts+=1 363 else: 364 assert_equal(i, rawtxfund['changepos']) 365 366 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 367 assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex']) 368 369 assert_equal(matchingOuts, 1) 370 assert_equal(len(dec_tx['vout']), 2) 371 372 def test_two_vin(self): 373 self.log.info("Test fundrawtxn with 2 vins") 374 utx = get_unspent(self.nodes[2].listunspent(), 1) 375 utx2 = get_unspent(self.nodes[2].listunspent(), 5) 376 377 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ] 378 outputs = { self.nodes[0].getnewaddress() : 6.0 } 379 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 380 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 381 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 382 383 # Should fail without add_inputs: 384 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, self.nodes[2].fundrawtransaction, rawtx, add_inputs=False) 385 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, add_inputs=True) 386 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 387 matchingOuts = 0 388 for out in dec_tx['vout']: 389 if out['scriptPubKey']['address'] in outputs: 390 matchingOuts+=1 391 392 assert_equal(matchingOuts, 1) 393 assert_equal(len(dec_tx['vout']), 2) 394 395 matchingIns = 0 396 for vinOut in dec_tx['vin']: 397 for vinIn in inputs: 398 if vinIn['txid'] == vinOut['txid']: 399 matchingIns+=1 400 401 assert_equal(matchingIns, 2) #we now must see two vins identical to vins given as params 402 403 def test_two_vin_two_vout(self): 404 self.log.info("Test fundrawtxn with 2 vins and 2 vouts") 405 utx = get_unspent(self.nodes[2].listunspent(), 1) 406 utx2 = get_unspent(self.nodes[2].listunspent(), 5) 407 408 inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ] 409 outputs = { self.nodes[0].getnewaddress() : 6.0, self.nodes[0].getnewaddress() : 1.0 } 410 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 411 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 412 assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) 413 414 # Should fail without add_inputs: 415 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, self.nodes[2].fundrawtransaction, rawtx, add_inputs=False) 416 rawtxfund = self.nodes[2].fundrawtransaction(rawtx, add_inputs=True) 417 418 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 419 matchingOuts = 0 420 for out in dec_tx['vout']: 421 if out['scriptPubKey']['address'] in outputs: 422 matchingOuts+=1 423 424 assert_equal(matchingOuts, 2) 425 assert_equal(len(dec_tx['vout']), 3) 426 427 def test_invalid_input(self): 428 self.log.info("Test fundrawtxn with an invalid vin") 429 txid = "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1" 430 vout = 0 431 inputs = [ {'txid' : txid, 'vout' : vout} ] #invalid vin! 432 outputs = { self.nodes[0].getnewaddress() : 1.0} 433 rawtx = self.nodes[2].createrawtransaction(inputs, outputs) 434 assert_raises_rpc_error(-4, "Unable to find UTXO for external input", self.nodes[2].fundrawtransaction, rawtx) 435 436 def test_fee_p2pkh(self): 437 """Compare fee of a standard pubkeyhash transaction.""" 438 self.log.info("Test fundrawtxn p2pkh fee") 439 self.lock_outputs_type(self.nodes[0], "p2pkh") 440 inputs = [] 441 outputs = {self.nodes[1].getnewaddress():1.1} 442 rawtx = self.nodes[0].createrawtransaction(inputs, outputs) 443 fundedTx = self.nodes[0].fundrawtransaction(rawtx) 444 445 # Create same transaction over sendtoaddress. 446 txId = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1.1) 447 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base'] 448 449 # Compare fee. 450 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) 451 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance 452 453 self.unlock_utxos(self.nodes[0]) 454 455 def test_fee_p2pkh_multi_out(self): 456 """Compare fee of a standard pubkeyhash transaction with multiple outputs.""" 457 self.log.info("Test fundrawtxn p2pkh fee with multiple outputs") 458 self.lock_outputs_type(self.nodes[0], "p2pkh") 459 inputs = [] 460 outputs = { 461 self.nodes[1].getnewaddress():1.1, 462 self.nodes[1].getnewaddress():1.2, 463 self.nodes[1].getnewaddress():0.1, 464 self.nodes[1].getnewaddress():1.3, 465 self.nodes[1].getnewaddress():0.2, 466 self.nodes[1].getnewaddress():0.3, 467 } 468 rawtx = self.nodes[0].createrawtransaction(inputs, outputs) 469 fundedTx = self.nodes[0].fundrawtransaction(rawtx) 470 471 # Create same transaction over sendtoaddress. 472 txId = self.nodes[0].sendmany("", outputs) 473 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base'] 474 475 # Compare fee. 476 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) 477 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance 478 479 self.unlock_utxos(self.nodes[0]) 480 481 def test_fee_p2sh(self): 482 """Compare fee of a 2-of-2 multisig p2sh transaction.""" 483 self.lock_outputs_type(self.nodes[0], "p2pkh") 484 # Create 2-of-2 addr. 485 addr1 = self.nodes[1].getnewaddress() 486 addr2 = self.nodes[1].getnewaddress() 487 488 addr1Obj = self.nodes[1].getaddressinfo(addr1) 489 addr2Obj = self.nodes[1].getaddressinfo(addr2) 490 491 mSigObj = self.nodes[3].createmultisig(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] 492 493 inputs = [] 494 outputs = {mSigObj:1.1} 495 rawtx = self.nodes[0].createrawtransaction(inputs, outputs) 496 fundedTx = self.nodes[0].fundrawtransaction(rawtx) 497 498 # Create same transaction over sendtoaddress. 499 txId = self.nodes[0].sendtoaddress(mSigObj, 1.1) 500 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base'] 501 502 # Compare fee. 503 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) 504 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance 505 506 self.unlock_utxos(self.nodes[0]) 507 508 def test_fee_4of5(self): 509 """Compare fee of a standard pubkeyhash transaction.""" 510 self.log.info("Test fundrawtxn fee with 4-of-5 addresses") 511 self.lock_outputs_type(self.nodes[0], "p2pkh") 512 513 # Create 4-of-5 addr. 514 addr1 = self.nodes[1].getnewaddress() 515 addr2 = self.nodes[1].getnewaddress() 516 addr3 = self.nodes[1].getnewaddress() 517 addr4 = self.nodes[1].getnewaddress() 518 addr5 = self.nodes[1].getnewaddress() 519 520 addr1Obj = self.nodes[1].getaddressinfo(addr1) 521 addr2Obj = self.nodes[1].getaddressinfo(addr2) 522 addr3Obj = self.nodes[1].getaddressinfo(addr3) 523 addr4Obj = self.nodes[1].getaddressinfo(addr4) 524 addr5Obj = self.nodes[1].getaddressinfo(addr5) 525 526 mSigObj = self.nodes[1].createmultisig( 527 4, 528 [ 529 addr1Obj['pubkey'], 530 addr2Obj['pubkey'], 531 addr3Obj['pubkey'], 532 addr4Obj['pubkey'], 533 addr5Obj['pubkey'], 534 ] 535 )['address'] 536 537 inputs = [] 538 outputs = {mSigObj:1.1} 539 rawtx = self.nodes[0].createrawtransaction(inputs, outputs) 540 fundedTx = self.nodes[0].fundrawtransaction(rawtx) 541 542 # Create same transaction over sendtoaddress. 543 txId = self.nodes[0].sendtoaddress(mSigObj, 1.1) 544 signedFee = self.nodes[0].getmempoolentry(txId)['fees']['base'] 545 546 # Compare fee. 547 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) 548 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance 549 550 self.unlock_utxos(self.nodes[0]) 551 552 def test_spend_2of2(self): 553 """Spend a 2-of-2 multisig transaction over fundraw.""" 554 self.log.info("Test fundpsbt spending 2-of-2 multisig") 555 556 # Create 2-of-2 addr. 557 addr1 = self.nodes[2].getnewaddress() 558 addr2 = self.nodes[2].getnewaddress() 559 560 addr1Obj = self.nodes[2].getaddressinfo(addr1) 561 addr2Obj = self.nodes[2].getaddressinfo(addr2) 562 563 self.nodes[2].createwallet(wallet_name='wmulti', disable_private_keys=True) 564 wmulti = self.nodes[2].get_wallet_rpc('wmulti') 565 w2 = self.nodes[2].get_wallet_rpc(self.default_wallet_name) 566 mSigObj = wmulti.addmultisigaddress( 567 2, 568 [ 569 addr1Obj['pubkey'], 570 addr2Obj['pubkey'], 571 ] 572 )['address'] 573 if not self.options.descriptors: 574 wmulti.importaddress(mSigObj) 575 576 # Send 1.2 BTC to msig addr. 577 self.nodes[0].sendtoaddress(mSigObj, 1.2) 578 self.generate(self.nodes[0], 1) 579 580 oldBalance = self.nodes[1].getbalance() 581 inputs = [] 582 outputs = {self.nodes[1].getnewaddress():1.1} 583 funded_psbt = wmulti.walletcreatefundedpsbt(inputs=inputs, outputs=outputs, changeAddress=w2.getrawchangeaddress())['psbt'] 584 585 signed_psbt = w2.walletprocesspsbt(funded_psbt) 586 self.nodes[2].sendrawtransaction(signed_psbt['hex']) 587 self.generate(self.nodes[2], 1) 588 589 # Make sure funds are received at node1. 590 assert_equal(oldBalance+Decimal('1.10000000'), self.nodes[1].getbalance()) 591 592 wmulti.unloadwallet() 593 594 def test_locked_wallet(self): 595 self.log.info("Test fundrawtxn with locked wallet and hardened derivation") 596 597 df_wallet = self.nodes[1].get_wallet_rpc(self.default_wallet_name) 598 self.nodes[1].createwallet(wallet_name="locked_wallet", descriptors=self.options.descriptors) 599 wallet = self.nodes[1].get_wallet_rpc("locked_wallet") 600 # This test is not meant to exercise fee estimation. Making sure all txs are sent at a consistent fee rate. 601 wallet.settxfee(self.min_relay_tx_fee) 602 603 # Add some balance to the wallet (this will be reverted at the end of the test) 604 df_wallet.sendall(recipients=[wallet.getnewaddress()]) 605 self.generate(self.nodes[1], 1) 606 607 # Encrypt wallet and import descriptors 608 wallet.encryptwallet("test") 609 610 if self.options.descriptors: 611 with WalletUnlock(wallet, "test"): 612 wallet.importdescriptors([{ 613 'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPdYeeZbPSKd2KYLmeVKtcFA7kqCxDvDR13MQ6us8HopUR2wLcS2ZKPhLyKsqpDL2FtL73LMHcgoCL7DXsciA8eX8nbjCR2eG/0h/*h)'), 614 'timestamp': 'now', 615 'active': True 616 }, 617 { 618 'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPdYeeZbPSKd2KYLmeVKtcFA7kqCxDvDR13MQ6us8HopUR2wLcS2ZKPhLyKsqpDL2FtL73LMHcgoCL7DXsciA8eX8nbjCR2eG/1h/*h)'), 619 'timestamp': 'now', 620 'active': True, 621 'internal': True 622 }]) 623 624 # Drain the keypool. 625 wallet.getnewaddress() 626 wallet.getrawchangeaddress() 627 628 # Choose input 629 inputs = wallet.listunspent() 630 631 # Deduce exact fee to produce a changeless transaction 632 tx_size = 110 # Total tx size: 110 vbytes, p2wpkh -> p2wpkh. Input 68 vbytes + rest of tx is 42 vbytes. 633 value = inputs[0]["amount"] - get_fee(tx_size, self.min_relay_tx_fee) 634 635 outputs = {self.nodes[0].getnewaddress():value} 636 rawtx = wallet.createrawtransaction(inputs, outputs) 637 # fund a transaction that does not require a new key for the change output 638 funded_tx = wallet.fundrawtransaction(rawtx) 639 assert_equal(funded_tx["changepos"], -1) 640 641 # fund a transaction that requires a new key for the change output 642 # creating the key must be impossible because the wallet is locked 643 outputs = {self.nodes[0].getnewaddress():value - Decimal("0.1")} 644 rawtx = wallet.createrawtransaction(inputs, outputs) 645 assert_raises_rpc_error(-4, "Transaction needs a change address, but we can't generate it.", wallet.fundrawtransaction, rawtx) 646 647 # Refill the keypool. 648 with WalletUnlock(wallet, "test"): 649 wallet.keypoolrefill(8) #need to refill the keypool to get an internal change address 650 651 assert_raises_rpc_error(-13, "walletpassphrase", wallet.sendtoaddress, self.nodes[0].getnewaddress(), 1.2) 652 653 oldBalance = self.nodes[0].getbalance() 654 655 inputs = [] 656 outputs = {self.nodes[0].getnewaddress():1.1} 657 rawtx = wallet.createrawtransaction(inputs, outputs) 658 fundedTx = wallet.fundrawtransaction(rawtx) 659 assert fundedTx["changepos"] != -1 660 661 # Now we need to unlock. 662 with WalletUnlock(wallet, "test"): 663 signedTx = wallet.signrawtransactionwithwallet(fundedTx['hex']) 664 wallet.sendrawtransaction(signedTx['hex']) 665 self.generate(self.nodes[1], 1) 666 667 # Make sure funds are received at node1. 668 assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance()) 669 670 # Restore pre-test wallet state 671 wallet.sendall(recipients=[df_wallet.getnewaddress(), df_wallet.getnewaddress(), df_wallet.getnewaddress()]) 672 wallet.unloadwallet() 673 self.generate(self.nodes[1], 1) 674 675 def test_many_inputs_fee(self): 676 """Multiple (~19) inputs tx test | Compare fee.""" 677 self.log.info("Test fundrawtxn fee with many inputs") 678 679 # Empty node1, send some small coins from node0 to node1. 680 self.nodes[1].sendall(recipients=[self.nodes[0].getnewaddress()]) 681 self.generate(self.nodes[1], 1) 682 683 for _ in range(20): 684 self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01) 685 self.generate(self.nodes[0], 1) 686 687 # Fund a tx with ~20 small inputs. 688 inputs = [] 689 outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04} 690 rawtx = self.nodes[1].createrawtransaction(inputs, outputs) 691 fundedTx = self.nodes[1].fundrawtransaction(rawtx) 692 693 # Create same transaction over sendtoaddress. 694 txId = self.nodes[1].sendmany("", outputs) 695 signedFee = self.nodes[1].getmempoolentry(txId)['fees']['base'] 696 697 # Compare fee. 698 feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) 699 assert feeDelta >= 0 and feeDelta <= self.fee_tolerance * 19 #~19 inputs 700 701 def test_many_inputs_send(self): 702 """Multiple (~19) inputs tx test | sign/send.""" 703 self.log.info("Test fundrawtxn sign+send with many inputs") 704 705 # Again, empty node1, send some small coins from node0 to node1. 706 self.nodes[1].sendall(recipients=[self.nodes[0].getnewaddress()]) 707 self.generate(self.nodes[1], 1) 708 709 for _ in range(20): 710 self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01) 711 self.generate(self.nodes[0], 1) 712 713 # Fund a tx with ~20 small inputs. 714 oldBalance = self.nodes[0].getbalance() 715 716 inputs = [] 717 outputs = {self.nodes[0].getnewaddress():0.15,self.nodes[0].getnewaddress():0.04} 718 rawtx = self.nodes[1].createrawtransaction(inputs, outputs) 719 fundedTx = self.nodes[1].fundrawtransaction(rawtx) 720 fundedAndSignedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex']) 721 self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex']) 722 self.generate(self.nodes[1], 1) 723 assert_equal(oldBalance+Decimal('50.19000000'), self.nodes[0].getbalance()) #0.19+block reward 724 725 def test_op_return(self): 726 self.log.info("Test fundrawtxn with OP_RETURN and no vin") 727 728 rawtx = "0100000000010000000000000000066a047465737400000000" 729 dec_tx = self.nodes[2].decoderawtransaction(rawtx) 730 731 assert_equal(len(dec_tx['vin']), 0) 732 assert_equal(len(dec_tx['vout']), 1) 733 734 rawtxfund = self.nodes[2].fundrawtransaction(rawtx) 735 dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) 736 737 assert_greater_than(len(dec_tx['vin']), 0) # at least one vin 738 assert_equal(len(dec_tx['vout']), 2) # one change output added 739 740 def test_watchonly(self): 741 self.log.info("Test fundrawtxn using only watchonly") 742 743 inputs = [] 744 outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount / 2} 745 rawtx = self.nodes[3].createrawtransaction(inputs, outputs) 746 747 self.nodes[3].loadwallet('wwatch') 748 wwatch = self.nodes[3].get_wallet_rpc('wwatch') 749 # Setup change addresses for the watchonly wallet 750 desc_import = [{ 751 "desc": descsum_create("wpkh(tpubD6NzVbkrYhZ4YNXVQbNhMK1WqguFsUXceaVJKbmno2aZ3B6QfbMeraaYvnBSGpV3vxLyTTK9DYT1yoEck4XUScMzXoQ2U2oSmE2JyMedq3H/1/*)"), 752 "timestamp": "now", 753 "internal": True, 754 "active": True, 755 "keypool": True, 756 "range": [0, 100], 757 "watchonly": True, 758 }] 759 if self.options.descriptors: 760 wwatch.importdescriptors(desc_import) 761 else: 762 wwatch.importmulti(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, includeWatching=True, changeAddress=w3.getrawchangeaddress(), subtractFeeFromOutputs=[0]) 786 res_dec = self.nodes[0].decoderawtransaction(result["hex"]) 787 assert_equal(len(res_dec["vin"]), 1) 788 assert 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) # uses self.min_relay_tx_fee (set by settxfee) 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"], 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.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 changeaddress != "" 903 nextaddr = self.nodes[3].getnewaddress() 904 # Now the change address key should be removed from the keypool. 905 assert 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), # uses self.min_relay_tx_fee (set by settxfee) 919 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[]), # empty subtraction list 920 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[0]), # uses self.min_relay_tx_fee (set by settxfee) 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), # uses self.min_relay_tx_fee (set by settxfee) 939 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[]), # empty subtraction list 940 self.nodes[3].fundrawtransaction(rawtx, subtractFeeFromOutputs=[0]), # uses self.min_relay_tx_fee (set by settxfee) 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]) 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) 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 if self.options.descriptors: 1040 res = self.nodes[0].importdescriptors([{"desc": desc, "timestamp": "now"}]) 1041 else: 1042 res = self.nodes[0].importmulti([{"desc": desc, "timestamp": "now"}]) 1043 assert res[0]["success"] 1044 addr = self.nodes[0].deriveaddresses(desc)[0] 1045 addr_info = self.nodes[0].getaddressinfo(addr) 1046 1047 self.nodes[0].sendtoaddress(addr, 10) 1048 self.nodes[0].sendtoaddress(wallet.getnewaddress(), 10) 1049 self.generate(self.nodes[0], 6) 1050 ext_utxo = self.nodes[0].listunspent(addresses=[addr])[0] 1051 1052 # An external input without solving data should result in an error 1053 raw_tx = wallet.createrawtransaction([ext_utxo], {self.nodes[0].getnewaddress(): ext_utxo["amount"] / 2}) 1054 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) 1055 1056 # Error conditions 1057 assert_raises_rpc_error(-5, "'not a pubkey' is not hex", wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["not a pubkey"]}) 1058 assert_raises_rpc_error(-5, "'01234567890a0b0c0d0e0f' is not a valid public key", wallet.fundrawtransaction, raw_tx, solving_data={"pubkeys":["01234567890a0b0c0d0e0f"]}) 1059 assert_raises_rpc_error(-5, "'not a script' is not hex", wallet.fundrawtransaction, raw_tx, solving_data={"scripts":["not a script"]}) 1060 assert_raises_rpc_error(-8, "Unable to parse descriptor 'not a descriptor'", wallet.fundrawtransaction, raw_tx, solving_data={"descriptors":["not a descriptor"]}) 1061 assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"]}]) 1062 assert_raises_rpc_error(-8, "Invalid parameter, vout cannot be negative", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": -1}]) 1063 assert_raises_rpc_error(-8, "Invalid parameter, missing weight key", wallet.fundrawtransaction, raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"]}]) 1064 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}]) 1065 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}]) 1066 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}]) 1067 1068 # But funding should work when the solving data is provided 1069 funded_tx = wallet.fundrawtransaction(raw_tx, solving_data={"pubkeys": [addr_info['pubkey']], "scripts": [addr_info["embedded"]["scriptPubKey"]]}) 1070 signed_tx = wallet.signrawtransactionwithwallet(funded_tx['hex']) 1071 assert not signed_tx['complete'] 1072 signed_tx = self.nodes[0].signrawtransactionwithwallet(signed_tx['hex']) 1073 assert signed_tx['complete'] 1074 1075 funded_tx = wallet.fundrawtransaction(raw_tx, solving_data={"descriptors": [desc]}) 1076 signed_tx1 = wallet.signrawtransactionwithwallet(funded_tx['hex']) 1077 assert not signed_tx1['complete'] 1078 signed_tx2 = self.nodes[0].signrawtransactionwithwallet(signed_tx1['hex']) 1079 assert signed_tx2['complete'] 1080 1081 unsigned_weight = self.nodes[0].decoderawtransaction(signed_tx1["hex"])["weight"] 1082 signed_weight = self.nodes[0].decoderawtransaction(signed_tx2["hex"])["weight"] 1083 # Input's weight is difference between weight of signed and unsigned, 1084 # and the weight of stuff that didn't change (prevout, sequence, 1 byte of scriptSig) 1085 input_weight = signed_weight - unsigned_weight + (41 * 4) 1086 low_input_weight = input_weight // 2 1087 high_input_weight = input_weight * 2 1088 1089 # Funding should also work if the input weight is provided 1090 funded_tx = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": input_weight}], fee_rate=2) 1091 signed_tx = wallet.signrawtransactionwithwallet(funded_tx["hex"]) 1092 signed_tx = self.nodes[0].signrawtransactionwithwallet(signed_tx["hex"]) 1093 assert_equal(self.nodes[0].testmempoolaccept([signed_tx["hex"]])[0]["allowed"], True) 1094 assert_equal(signed_tx["complete"], True) 1095 # Reducing the weight should have a lower fee 1096 funded_tx2 = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": low_input_weight}], fee_rate=2) 1097 assert_greater_than(funded_tx["fee"], funded_tx2["fee"]) 1098 # Increasing the weight should have a higher fee 1099 funded_tx2 = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": high_input_weight}], fee_rate=2) 1100 assert_greater_than(funded_tx2["fee"], funded_tx["fee"]) 1101 # The provided weight should override the calculated weight when solving data is provided 1102 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) 1103 assert_equal(funded_tx2["fee"], funded_tx3["fee"]) 1104 # The feerate should be met 1105 funded_tx4 = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": high_input_weight}], fee_rate=10) 1106 input_add_weight = high_input_weight - (41 * 4) 1107 tx4_weight = wallet.decoderawtransaction(funded_tx4["hex"])["weight"] + input_add_weight 1108 tx4_vsize = int(ceil(tx4_weight / 4)) 1109 assert_fee_amount(funded_tx4["fee"], tx4_vsize, Decimal(0.0001)) 1110 1111 # Funding with weight at csuint boundaries should not cause problems 1112 funded_tx = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": 255}], fee_rate=2) 1113 funded_tx = wallet.fundrawtransaction(raw_tx, input_weights=[{"txid": ext_utxo["txid"], "vout": ext_utxo["vout"], "weight": 65539}], fee_rate=2) 1114 1115 self.nodes[2].unloadwallet("extfund") 1116 1117 def test_add_inputs_default_value(self): 1118 self.log.info("Test 'add_inputs' default value") 1119 1120 # Create and fund the wallet with 5 BTC 1121 self.nodes[2].createwallet("test_preset_inputs") 1122 wallet = self.nodes[2].get_wallet_rpc("test_preset_inputs") 1123 addr1 = wallet.getnewaddress(address_type="bech32") 1124 self.nodes[0].sendtoaddress(addr1, 5) 1125 self.generate(self.nodes[0], 1) 1126 1127 # Covered cases: 1128 # 1. Default add_inputs value with no preset inputs (add_inputs=true): 1129 # Expect: automatically add coins from the wallet to the tx. 1130 # 2. Default add_inputs value with preset inputs (add_inputs=false): 1131 # Expect: disallow automatic coin selection. 1132 # 3. Explicit add_inputs=true and preset inputs (with preset inputs not-covering the target amount). 1133 # Expect: include inputs from the wallet. 1134 # 4. Explicit add_inputs=true and preset inputs (with preset inputs covering the target amount). 1135 # Expect: only preset inputs are used. 1136 # 5. Explicit add_inputs=true, no preset inputs (same as (1) but with an explicit set): 1137 # Expect: include inputs from the wallet. 1138 # 6. Explicit add_inputs=false, no preset inputs: 1139 # Expect: failure as we did not provide inputs and the process cannot automatically select coins. 1140 1141 # Case (1), 'send' command 1142 # 'add_inputs' value is true unless "inputs" are specified, in such case, add_inputs=false. 1143 # So, the wallet will automatically select coins and create the transaction if only the outputs are provided. 1144 tx = wallet.send(outputs=[{addr1: 3}]) 1145 assert tx["complete"] 1146 1147 # Case (2), 'send' command 1148 # Select an input manually, which doesn't cover the entire output amount and 1149 # verify that the dynamically set 'add_inputs=false' value works. 1150 1151 # Fund wallet with 2 outputs, 5 BTC each. 1152 addr2 = wallet.getnewaddress(address_type="bech32") 1153 source_tx = self.nodes[0].send(outputs=[{addr1: 5}, {addr2: 5}], change_position=0) 1154 self.generate(self.nodes[0], 1) 1155 1156 # Select only one input. 1157 options = { 1158 "inputs": [ 1159 { 1160 "txid": source_tx["txid"], 1161 "vout": 1 # change position was hardcoded to index 0 1162 } 1163 ] 1164 } 1165 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.send, outputs=[{addr1: 8}], **options) 1166 1167 # Case (3), Explicit add_inputs=true and preset inputs (with preset inputs not-covering the target amount) 1168 options["add_inputs"] = True 1169 options["add_to_wallet"] = False 1170 tx = wallet.send(outputs=[{addr1: 8}], **options) 1171 assert tx["complete"] 1172 1173 # Case (4), Explicit add_inputs=true and preset inputs (with preset inputs covering the target amount) 1174 options["inputs"].append({ 1175 "txid": source_tx["txid"], 1176 "vout": 2 # change position was hardcoded to index 0 1177 }) 1178 tx = wallet.send(outputs=[{addr1: 8}], **options) 1179 assert tx["complete"] 1180 # Check that only the preset inputs were added to the tx 1181 decoded_psbt_inputs = self.nodes[0].decodepsbt(tx["psbt"])['tx']['vin'] 1182 assert_equal(len(decoded_psbt_inputs), 2) 1183 for input in decoded_psbt_inputs: 1184 assert_equal(input["txid"], source_tx["txid"]) 1185 1186 # Case (5), assert that inputs are added to the tx by explicitly setting add_inputs=true 1187 options = {"add_inputs": True, "add_to_wallet": True} 1188 tx = wallet.send(outputs=[{addr1: 8}], **options) 1189 assert tx["complete"] 1190 1191 # 6. Explicit add_inputs=false, no preset inputs: 1192 options = {"add_inputs": False} 1193 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.send, outputs=[{addr1: 3}], **options) 1194 1195 ################################################ 1196 1197 # Case (1), 'walletcreatefundedpsbt' command 1198 # Default add_inputs value with no preset inputs (add_inputs=true) 1199 inputs = [] 1200 outputs = {self.nodes[1].getnewaddress(): 8} 1201 assert "psbt" in wallet.walletcreatefundedpsbt(inputs=inputs, outputs=outputs) 1202 1203 # Case (2), 'walletcreatefundedpsbt' command 1204 # Default add_inputs value with preset inputs (add_inputs=false). 1205 inputs = [{ 1206 "txid": source_tx["txid"], 1207 "vout": 1 # change position was hardcoded to index 0 1208 }] 1209 outputs = {self.nodes[1].getnewaddress(): 8} 1210 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.walletcreatefundedpsbt, inputs=inputs, outputs=outputs) 1211 1212 # Case (3), Explicit add_inputs=true and preset inputs (with preset inputs not-covering the target amount) 1213 options["add_inputs"] = True 1214 assert "psbt" in wallet.walletcreatefundedpsbt(outputs=[{addr1: 8}], inputs=inputs, **options) 1215 1216 # Case (4), Explicit add_inputs=true and preset inputs (with preset inputs covering the target amount) 1217 inputs.append({ 1218 "txid": source_tx["txid"], 1219 "vout": 2 # change position was hardcoded to index 0 1220 }) 1221 psbt_tx = wallet.walletcreatefundedpsbt(outputs=[{addr1: 8}], inputs=inputs, **options) 1222 # Check that only the preset inputs were added to the tx 1223 decoded_psbt_inputs = self.nodes[0].decodepsbt(psbt_tx["psbt"])['tx']['vin'] 1224 assert_equal(len(decoded_psbt_inputs), 2) 1225 for input in decoded_psbt_inputs: 1226 assert_equal(input["txid"], source_tx["txid"]) 1227 1228 # Case (5), 'walletcreatefundedpsbt' command 1229 # Explicit add_inputs=true, no preset inputs 1230 options = { 1231 "add_inputs": True 1232 } 1233 assert "psbt" in wallet.walletcreatefundedpsbt(inputs=[], outputs=outputs, **options) 1234 1235 # Case (6). Explicit add_inputs=false, no preset inputs: 1236 options = {"add_inputs": False} 1237 assert_raises_rpc_error(-4, ERR_NOT_ENOUGH_PRESET_INPUTS, wallet.walletcreatefundedpsbt, inputs=[], outputs=outputs, **options) 1238 1239 self.nodes[2].unloadwallet("test_preset_inputs") 1240 1241 def test_preset_inputs_selection(self): 1242 self.log.info('Test wallet preset inputs are not double-counted or reused in coin selection') 1243 1244 # Create and fund the wallet with 4 UTXO of 5 BTC each (20 BTC total) 1245 self.nodes[2].createwallet("test_preset_inputs_selection") 1246 wallet = self.nodes[2].get_wallet_rpc("test_preset_inputs_selection") 1247 outputs = {} 1248 for _ in range(4): 1249 outputs[wallet.getnewaddress(address_type="bech32")] = 5 1250 self.nodes[0].sendmany("", outputs) 1251 self.generate(self.nodes[0], 1) 1252 1253 # Select the preset inputs 1254 coins = wallet.listunspent() 1255 preset_inputs = [coins[0], coins[1], coins[2]] 1256 1257 # Now let's create the tx creation options 1258 options = { 1259 "inputs": preset_inputs, 1260 "add_inputs": True, # automatically add coins from the wallet to fulfill the target 1261 "subtract_fee_from_outputs": [0], # deduct fee from first output 1262 "add_to_wallet": False 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) 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_include_unsafe(self): 1316 self.log.info("Test fundrawtxn with unsafe inputs") 1317 1318 self.nodes[0].createwallet("unsafe") 1319 wallet = self.nodes[0].get_wallet_rpc("unsafe") 1320 1321 # We receive unconfirmed funds from external keys (unsafe outputs). 1322 addr = wallet.getnewaddress() 1323 inputs = [] 1324 for i in range(0, 2): 1325 utxo = self.create_outpoints(self.nodes[2], outputs=[{addr: 5}])[0] 1326 inputs.append((utxo['txid'], utxo['vout'])) 1327 self.sync_mempools() 1328 1329 # Unsafe inputs are ignored by default. 1330 rawtx = wallet.createrawtransaction([], [{self.nodes[2].getnewaddress(): 7.5}]) 1331 assert_raises_rpc_error(-4, "Insufficient funds", wallet.fundrawtransaction, rawtx) 1332 1333 # But we can opt-in to use them for funding. 1334 fundedtx = wallet.fundrawtransaction(rawtx, include_unsafe=True) 1335 tx_dec = wallet.decoderawtransaction(fundedtx['hex']) 1336 assert all((txin["txid"], txin["vout"]) in inputs for txin in tx_dec["vin"]) 1337 signedtx = wallet.signrawtransactionwithwallet(fundedtx['hex']) 1338 assert wallet.testmempoolaccept([signedtx['hex']])[0]["allowed"] 1339 1340 # And we can also use them once they're confirmed. 1341 self.generate(self.nodes[0], 1) 1342 fundedtx = wallet.fundrawtransaction(rawtx, include_unsafe=False) 1343 tx_dec = wallet.decoderawtransaction(fundedtx['hex']) 1344 assert all((txin["txid"], txin["vout"]) in inputs for txin in tx_dec["vin"]) 1345 signedtx = wallet.signrawtransactionwithwallet(fundedtx['hex']) 1346 assert wallet.testmempoolaccept([signedtx['hex']])[0]["allowed"] 1347 self.nodes[0].unloadwallet("unsafe") 1348 1349 def test_22670(self): 1350 # In issue #22670, it was observed that ApproximateBestSubset may 1351 # choose enough value to cover the target amount but not enough to cover the transaction fees. 1352 # This leads to a transaction whose actual transaction feerate is lower than expected. 1353 # However at normal feerates, the difference between the effective value and the real value 1354 # that this bug is not detected because the transaction fee must be at least 0.01 BTC (the minimum change value). 1355 # Otherwise the targeted minimum change value will be enough to cover the transaction fees that were not 1356 # being accounted for. So the minimum relay fee is set to 0.1 BTC/kvB in this test. 1357 self.log.info("Test issue 22670 ApproximateBestSubset bug") 1358 # Make sure the default wallet will not be loaded when restarted with a high minrelaytxfee 1359 self.nodes[0].unloadwallet(self.default_wallet_name, False) 1360 feerate = Decimal("0.1") 1361 self.restart_node(0, [f"-minrelaytxfee={feerate}", "-discardfee=0"]) # Set high minrelayfee, set discardfee to 0 for easier calculation 1362 1363 self.nodes[0].loadwallet(self.default_wallet_name, True) 1364 funds = self.nodes[0].get_wallet_rpc(self.default_wallet_name) 1365 self.nodes[0].createwallet(wallet_name="tester") 1366 tester = self.nodes[0].get_wallet_rpc("tester") 1367 1368 # Because this test is specifically for ApproximateBestSubset, the target value must be greater 1369 # than any single input available, and require more than 1 input. So we make 3 outputs 1370 for i in range(0, 3): 1371 funds.sendtoaddress(tester.getnewaddress(address_type="bech32"), 1) 1372 self.generate(self.nodes[0], 1, sync_fun=self.no_op) 1373 1374 # Create transactions in order to calculate fees for the target bounds that can trigger this bug 1375 change_tx = tester.fundrawtransaction(tester.createrawtransaction([], [{funds.getnewaddress(): 1.5}])) 1376 tx = tester.createrawtransaction([], [{funds.getnewaddress(): 2}]) 1377 no_change_tx = tester.fundrawtransaction(tx, subtractFeeFromOutputs=[0]) 1378 1379 overhead_fees = feerate * len(tx) / 2 / 1000 1380 cost_of_change = change_tx["fee"] - no_change_tx["fee"] 1381 fees = no_change_tx["fee"] 1382 assert_greater_than(fees, 0.01) 1383 1384 def do_fund_send(target): 1385 create_tx = tester.createrawtransaction([], [{funds.getnewaddress(): target}]) 1386 funded_tx = tester.fundrawtransaction(create_tx) 1387 signed_tx = tester.signrawtransactionwithwallet(funded_tx["hex"]) 1388 assert signed_tx["complete"] 1389 decoded_tx = tester.decoderawtransaction(signed_tx["hex"]) 1390 assert_equal(len(decoded_tx["vin"]), 3) 1391 assert tester.testmempoolaccept([signed_tx["hex"]])[0]["allowed"] 1392 1393 # We want to choose more value than is available in 2 inputs when considering the fee, 1394 # but not enough to need 3 inputs when not considering the fee. 1395 # So the target value must be at least 2.00000001 - fee. 1396 lower_bound = Decimal("2.00000001") - fees 1397 # The target value must be at most 2 - cost_of_change - not_input_fees - min_change (these are all 1398 # included in the target before ApproximateBestSubset). 1399 upper_bound = Decimal("2.0") - cost_of_change - overhead_fees - Decimal("0.01") 1400 assert_greater_than_or_equal(upper_bound, lower_bound) 1401 do_fund_send(lower_bound) 1402 do_fund_send(upper_bound) 1403 1404 self.restart_node(0) 1405 self.connect_nodes(0, 1) 1406 self.connect_nodes(0, 2) 1407 self.connect_nodes(0, 3) 1408 1409 def test_feerate_rounding(self): 1410 self.log.info("Test that rounding of GetFee does not result in an assertion") 1411 1412 self.nodes[1].createwallet("roundtest") 1413 w = self.nodes[1].get_wallet_rpc("roundtest") 1414 1415 addr = w.getnewaddress(address_type="bech32") 1416 self.nodes[0].sendtoaddress(addr, 1) 1417 self.generate(self.nodes[0], 1) 1418 1419 # 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. 1420 # At a feerate of 1.85 sat/vb, the input will need a fee of 125.8 sats and the rest 77.7 sats 1421 # The entire tx fee should be 203.5 sats. 1422 # Coin selection rounds the fee individually instead of at the end (due to how CFeeRate::GetFee works). 1423 # If rounding down (which is the incorrect behavior), then the calculated fee will be 125 + 77 = 202. 1424 # If rounding up, then the calculated fee will be 126 + 78 = 204. 1425 # In the former case, the calculated needed fee is higher than the actual fee being paid, so an assertion is reached 1426 # To test this does not happen, we subtract 202 sats from the input value. If working correctly, this should 1427 # fail with insufficient funds rather than bitcoind asserting. 1428 rawtx = w.createrawtransaction(inputs=[], outputs=[{self.nodes[0].getnewaddress(address_type="bech32"): 1 - 0.00000202}]) 1429 assert_raises_rpc_error(-4, "Insufficient funds", w.fundrawtransaction, rawtx, fee_rate=1.85) 1430 1431 def test_input_confs_control(self): 1432 self.nodes[0].createwallet("minconf") 1433 wallet = self.nodes[0].get_wallet_rpc("minconf") 1434 1435 # Fund the wallet with different chain heights 1436 for _ in range(2): 1437 self.nodes[2].sendmany("", {wallet.getnewaddress():1, wallet.getnewaddress():1}) 1438 self.generate(self.nodes[2], 1) 1439 1440 unconfirmed_txid = wallet.sendtoaddress(wallet.getnewaddress(), 0.5) 1441 1442 self.log.info("Crafting TX using an unconfirmed input") 1443 target_address = self.nodes[2].getnewaddress() 1444 raw_tx1 = wallet.createrawtransaction([], {target_address: 0.1}, 0, True) 1445 funded_tx1 = wallet.fundrawtransaction(raw_tx1, {'fee_rate': 1, 'maxconf': 0})['hex'] 1446 1447 # Make sure we only had the one input 1448 tx1_inputs = self.nodes[0].decoderawtransaction(funded_tx1)['vin'] 1449 assert_equal(len(tx1_inputs), 1) 1450 1451 utxo1 = tx1_inputs[0] 1452 assert unconfirmed_txid == utxo1['txid'] 1453 1454 final_tx1 = wallet.signrawtransactionwithwallet(funded_tx1)['hex'] 1455 txid1 = self.nodes[0].sendrawtransaction(final_tx1) 1456 1457 mempool = self.nodes[0].getrawmempool() 1458 assert txid1 in mempool 1459 1460 self.log.info("Fail to craft a new TX with minconf above highest one") 1461 # Create a replacement tx to 'final_tx1' that has 1 BTC target instead of 0.1. 1462 raw_tx2 = wallet.createrawtransaction([{'txid': utxo1['txid'], 'vout': utxo1['vout']}], {target_address: 1}) 1463 assert_raises_rpc_error(-4, "Insufficient funds", wallet.fundrawtransaction, raw_tx2, {'add_inputs': True, 'minconf': 3, 'fee_rate': 10}) 1464 1465 self.log.info("Fail to broadcast a new TX with maxconf 0 due to BIP125 rules to verify it actually chose unconfirmed outputs") 1466 # Now fund 'raw_tx2' to fulfill the total target (1 BTC) by using all the wallet unconfirmed outputs. 1467 # As it was created with the first unconfirmed output, 'raw_tx2' only has 0.1 BTC covered (need to fund 0.9 BTC more). 1468 # 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 1469 # new tx is replacing!. So, once we send it to the mempool, it will return a "bad-txns-spends-conflicting-tx" 1470 # because the input will no longer exist once the first tx gets replaced by this new one). 1471 funded_invalid = wallet.fundrawtransaction(raw_tx2, {'add_inputs': True, 'maxconf': 0, 'fee_rate': 10})['hex'] 1472 final_invalid = wallet.signrawtransactionwithwallet(funded_invalid)['hex'] 1473 assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, final_invalid) 1474 1475 self.log.info("Craft a replacement adding inputs with highest depth possible") 1476 funded_tx2 = wallet.fundrawtransaction(raw_tx2, {'add_inputs': True, 'minconf': 2, 'fee_rate': 10})['hex'] 1477 tx2_inputs = self.nodes[0].decoderawtransaction(funded_tx2)['vin'] 1478 assert_greater_than_or_equal(len(tx2_inputs), 2) 1479 for vin in tx2_inputs: 1480 if vin['txid'] != unconfirmed_txid: 1481 assert_greater_than_or_equal(self.nodes[0].gettxout(vin['txid'], vin['vout'])['confirmations'], 2) 1482 1483 final_tx2 = wallet.signrawtransactionwithwallet(funded_tx2)['hex'] 1484 txid2 = self.nodes[0].sendrawtransaction(final_tx2) 1485 1486 mempool = self.nodes[0].getrawmempool() 1487 assert txid1 not in mempool 1488 assert txid2 in mempool 1489 1490 wallet.unloadwallet() 1491 1492 if __name__ == '__main__': 1493 RawTransactionsTest().main()