diff --git a/test/functional/rpc_psbt.py b/test/functional/rpc_psbt.py index 73a4922054..48879caf99 100755 --- a/test/functional/rpc_psbt.py +++ b/test/functional/rpc_psbt.py @@ -38,7 +38,6 @@ from test_framework.util import ( assert_greater_than, assert_greater_than_or_equal, assert_raises_rpc_error, - find_output, find_vout_for_address, # random_bytes, ) @@ -523,43 +522,24 @@ class PSBTTest(BitcoinTestFramework): self.nodes[0].converttopsbt(hexstring=signedtx['hex'], permitsigdata=True) # Create outputs to nodes 1 and 2 - # We do a whole song-and-dance here (instead of calling sendtoaddress) to get access to the unblinded transaction data to find our outputs - node1_addr = self.get_address(confidential, 1) - node1_unconf_addr = self.to_unconf_addr(1, node1_addr) - node2_addr = self.get_address(confidential, 2) - node2_unconf_addr = self.to_unconf_addr(2, node2_addr) - rt1 = self.nodes[0].createrawtransaction([], [{node1_addr:13}]) - rt1 = self.nodes[0].fundrawtransaction(rt1) - rt1 = self.nodes[0].blindrawtransaction(rt1['hex']) - rt1 = self.nodes[0].signrawtransactionwithwallet(rt1) - txid1 = self.nodes[0].sendrawtransaction(rt1['hex']) - rt1 = self.nodes[0].decoderawtransaction(rt1['hex']) + # (note that we intentionally create two different txs here, as we want + # to check that each node is missing prevout data for one of the two + # utxos, see "should only have data for one input" test below) + node1_addr = self.nodes[1].getnewaddress() + node2_addr = self.nodes[2].getnewaddress() + utxo1 = self.create_outpoints(self.nodes[0], outputs=[{node1_addr: 13}], blind=True)[0] + utxo2 = self.create_outpoints(self.nodes[0], outputs=[{node2_addr: 13}], blind=True)[0] + self.generate(self.nodes[0], 6)[0] - rt2 = self.nodes[0].createrawtransaction([], [{node2_addr:13}]) - rt2 = self.nodes[0].fundrawtransaction(rt2) - rt2 = self.nodes[0].blindrawtransaction(rt2['hex']) - rt2 = self.nodes[0].signrawtransactionwithwallet(rt2) - txid2 = self.nodes[0].sendrawtransaction(rt2['hex']) - rt2 = self.nodes[0].decoderawtransaction(rt2['hex']) + # Create a psbt spending outputs from nodes 1 and 2 + psbt_orig = self.nodes[0].createpsbt([utxo1, utxo2], [{self.nodes[0].getnewaddress():25.999}]) self.generate(self.nodes[0], 6) - for out in rt1['vout']: - if out['scriptPubKey']['type'] == "fee": - pass - elif out['scriptPubKey']['address'] == node1_unconf_addr: - vout1 = out['n'] - - for out in rt2['vout']: - if out['scriptPubKey']['type'] == "fee": - pass - elif out['scriptPubKey']['address'] == node2_unconf_addr: - vout2 = out['n'] - # This test doesn't work with Confidential Assets yet. if not confidential: # Create a psbt spending outputs from nodes 1 and 2 - psbt_orig = self.nodes[0].createpsbt([{"txid":txid1, "vout":vout1}, {"txid":txid2, "vout":vout2}], [{self.get_address(confidential, 0):25.999}, {"fee":0.001}]) + psbt_orig = self.nodes[0].createpsbt([utxo1, utxo2], [{self.get_address(confidential, 0):25.999}, {"fee":0.001}]) # Update psbts, should only have data for one input and not the other psbt1 = self.nodes[1].walletprocesspsbt(psbt_orig, False, "ALL")['psbt'] @@ -1079,14 +1059,9 @@ class PSBTTest(BitcoinTestFramework): # Send to all types of addresses addr1 = self.nodes[1].getnewaddress("", "bech32") - txid1 = self.nodes[0].sendtoaddress(addr1, 11) - vout1 = find_output(self.nodes[0], txid1, 11) addr2 = self.nodes[1].getnewaddress("", "legacy") - txid2 = self.nodes[0].sendtoaddress(addr2, 11) - vout2 = find_output(self.nodes[0], txid2, 11) addr3 = self.nodes[1].getnewaddress("", "p2sh-segwit") - txid3 = self.nodes[0].sendtoaddress(addr3, 11) - vout3 = find_output(self.nodes[0], txid3, 11) + utxo1, utxo2, utxo3 = self.create_outpoints(self.nodes[1], outputs=[{addr1: 11}, {addr2: 11}, {addr3: 11}], blind=True) self.sync_all() psbt_v2_required_keys = ["previous_vout", "sequence", "previous_txid"] @@ -1096,7 +1071,7 @@ class PSBTTest(BitcoinTestFramework): assert_equal(set(keys), set(psbt_input.keys())) # Create a PSBT. None of the inputs are filled initially - psbt = self.nodes[1].createpsbt([{"txid":txid1, "vout":vout1},{"txid":txid2, "vout":vout2},{"txid":txid3, "vout":vout3}], [{self.nodes[0].getnewaddress():32.999}]) + psbt = self.nodes[1].createpsbt([utxo1, utxo2, utxo3], [{self.nodes[0].getnewaddress():32.999}]) decoded = self.nodes[1].decodepsbt(psbt) test_psbt_input_keys(decoded['inputs'][0], psbt_v2_required_keys) test_psbt_input_keys(decoded['inputs'][1], psbt_v2_required_keys) @@ -1119,7 +1094,7 @@ class PSBTTest(BitcoinTestFramework): # test_psbt_input_keys(decoded['inputs'][2], psbt_v2_required_keys + ['non_witness_utxo']) # Cannot create PSBTv0 - assert_raises_rpc_error(-8, "The PSBT version can only be 2", self.nodes[0].createpsbt, [{"txid":txid1, "vout":vout1}], [{self.nodes[0].getnewaddress():Decimal('10.999')}], 0, True, 0) + assert_raises_rpc_error(-8, "The PSBT version can only be 2", self.nodes[0].createpsbt, [utxo1], [{self.nodes[0].getnewaddress():Decimal('10.999')}], 0, True, 0) """ # TODO: joinpsbts is disabled for PSBTv2s @@ -1130,16 +1105,15 @@ class PSBTTest(BitcoinTestFramework): assert_raises_rpc_error(-8, "joinpsbts only operates on version 0 PSBTs", self.nodes[1].joinpsbts, [psbt1, psbt2]) # Two PSBTs with a common input should not be joinable - psbt2 = self.nodes[1].createpsbt(inputs=[{"txid":txid1, "vout":vout1}], outputs=[{self.nodes[0].getnewaddress():Decimal('10.999')}], psbt_version=0) - assert_raises_rpc_error(-8, "exists in multiple PSBTs", self.nodes[1].joinpsbts, [psbt1, psbt2]) + psbt1 = self.nodes[1].createpsbt([utxo1], {self.nodes[0].getnewaddress():Decimal('10.999')}) + assert_raises_rpc_error(-8, "exists in multiple PSBTs", self.nodes[1].joinpsbts, [psbt1, updated]) # Join two distinct PSBTs psbt1 = self.nodes[1].createpsbt(inputs=[{"txid":txid1, "vout":vout1},{"txid":txid2, "vout":vout2},{"txid":txid3, "vout":vout3}], outputs=[{self.nodes[0].getnewaddress():32.999}], psbt_version=0) addr4 = self.nodes[1].getnewaddress("", "p2sh-segwit") - txid4 = self.nodes[0].sendtoaddress(addr4, 5) - vout4 = find_output(self.nodes[0], txid4, 5) + utxo4 = self.create_outpoints(self.nodes[0], outputs=[{addr4: 5}])[0] self.generate(self.nodes[0], 6) - psbt2 = self.nodes[1].createpsbt(inputs=[{"txid":txid4, "vout":vout4}], outputs=[{self.nodes[0].getnewaddress():Decimal('4.999')}], psbt_version=0) + psbt2 = self.nodes[1].createpsbt([utxo4], {self.nodes[0].getnewaddress():Decimal('4.999')}) psbt2 = self.nodes[1].walletprocesspsbt(psbt2)['psbt'] psbt2_decoded = self.nodes[0].decodepsbt(psbt2) assert "final_scriptwitness" in psbt2_decoded['inputs'][0] and "final_scriptSig" in psbt2_decoded['inputs'][0] @@ -1163,11 +1137,10 @@ class PSBTTest(BitcoinTestFramework): # Newly created PSBT needs UTXOs and updating addr = self.nodes[1].getnewaddress("", "p2sh-segwit") - txid = self.nodes[0].sendtoaddress(addr, 7) + utxo = self.create_outpoints(self.nodes[0], outputs=[{addr: 7}], blind=True)[0] addrinfo = self.nodes[1].getaddressinfo(addr) - blockhash = self.generate(self.nodes[0], 6)[0] - vout = find_output(self.nodes[0], txid, 7, blockhash=blockhash) - psbt = self.nodes[1].createpsbt([{"txid":txid, "vout":vout}], [{self.nodes[0].getnewaddress("", "p2sh-segwit"):Decimal('6.999')}, {"fee": 0.001}]) + self.generate(self.nodes[0], 6)[0] + psbt = self.nodes[1].createpsbt([utxo], [{self.nodes[0].getnewaddress("", "p2sh-segwit"):Decimal('6.999')}, {"fee": 0.001}]) analyzed = self.nodes[0].analyzepsbt(psbt) assert not analyzed['inputs'][0]['has_utxo'] and not analyzed['inputs'][0]['is_final'] and analyzed['inputs'][0]['next'] == 'updater' and analyzed['next'] == 'updater' @@ -1375,9 +1348,8 @@ class PSBTTest(BitcoinTestFramework): self.log.info("Test that walletprocesspsbt both updates and signs a non-updated psbt containing Taproot inputs") addr = self.nodes[0].getnewaddress("", "bech32m") - txid = self.nodes[0].sendtoaddress(addr, 1) - vout = find_vout_for_address(self.nodes[0], txid, addr) - psbt = self.nodes[0].createpsbt([{"txid": txid, "vout": vout}], [{self.nodes[0].getnewaddress(): 0.9999}, {"fee": 0.0001}]) + utxo = self.create_outpoints(self.nodes[0], outputs=[{addr: 1}], blind=True)[0] + psbt = self.nodes[0].createpsbt([utxo], [{self.nodes[0].getnewaddress(): 0.9999}, {"fee": 0.0001}]) signed = self.nodes[0].walletprocesspsbt(psbt) rawtx = signed["hex"] self.nodes[0].sendrawtransaction(rawtx) @@ -1470,11 +1442,10 @@ class PSBTTest(BitcoinTestFramework): descriptor = descsum_create(f"wpkh({key})") if self.options.descriptors: - txid = self.nodes[0].sendtoaddress(address, 1) + utxo = self.create_outpoints(self.nodes[0], outputs=[{address: 1}], blind=True)[0] self.sync_all() - vout = find_output(self.nodes[0], txid, 1) - psbt = self.nodes[2].createpsbt([{"txid": txid, "vout": vout}], [{self.nodes[0].getnewaddress(): 0.99999},{"fee": 0.00001}]) + psbt = self.nodes[2].createpsbt([utxo], [{self.nodes[0].getnewaddress(): 0.99999},{"fee": 0.00001}]) decoded = self.nodes[2].decodepsbt(psbt) test_psbt_input_keys(decoded['inputs'][0], psbt_v2_required_keys) diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 3761ae1129..55b18d831d 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -30,6 +30,7 @@ from .util import ( PortSeed, assert_equal, check_json_precision, + find_vout_for_address, get_datadir_path, initialize_datadir, p2p_port, @@ -702,6 +703,33 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass): sync_fun() if sync_fun else self.sync_all() return blocks + def create_outpoints(self, node, *, outputs, blind=False): + """Send funds to a given list of `{address: amount}` targets using the bitcoind + wallet and return the corresponding outpoints as a list of dictionaries + `[{"txid": txid, "vout": vout1}, {"txid": txid, "vout": vout2}, ...]`. + The result can be used to specify inputs for RPCs like `createrawtransaction`, + `createpsbt`, `lockunspent` etc.""" + assert all(len(output.keys()) == 1 for output in outputs) + if blind: + # ELEMENTS FIXME: use createrawtransaction until `send` can send to blinded addresses + raw_tx = node.createrawtransaction([], outputs) + funded_tx = node.fundrawtransaction(raw_tx) + blinded_tx = node.blindrawtransaction(funded_tx["hex"]) + signed_tx = node.signrawtransactionwithwallet(blinded_tx) + assert signed_tx["complete"] + assert node.testmempoolaccept([signed_tx["hex"]])[0]["allowed"] + txid = node.sendrawtransaction(signed_tx["hex"]) + send_res = {"txid": txid} + else: + send_res = node.send(outputs) + assert send_res["complete"] + utxos = [] + for output in outputs: + address = list(output.keys())[0] + vout = find_vout_for_address(node, send_res["txid"], address) + utxos.append({"txid": send_res["txid"], "vout": vout}) + return utxos + def sync_blocks(self, nodes=None, wait=1, timeout=60, expect_disconnected=False): """ Wait until everybody has the same tip. diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index 54462a4b60..c5234f6f16 100755 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -518,18 +518,6 @@ def check_node_connections(*, node, num_in, num_out): ############################# -def find_output(node, txid, amount, *, blockhash=None): - """ - Return index to output of txid with value amount - Raises exception if there is none. - """ - txdata = node.getrawtransaction(txid, 1, blockhash) - for i in range(len(txdata["vout"])): - if txdata["vout"][i].get("value") == amount: - return i - raise RuntimeError("find_output txid %s : %s not found" % (txid, str(amount))) - - # Create large OP_RETURN txouts that can be appended to a transaction # to make it large (helper for constructing large transactions). The # total serialized size of the txouts is about 66k vbytes. diff --git a/test/functional/wallet_basic.py b/test/functional/wallet_basic.py index 54e9748bfc..7a69e59d5f 100755 --- a/test/functional/wallet_basic.py +++ b/test/functional/wallet_basic.py @@ -17,7 +17,6 @@ from test_framework.util import ( assert_equal, assert_fee_amount, assert_raises_rpc_error, - find_vout_for_address, ) from test_framework.wallet_util import test_address from test_framework.wallet import MiniWallet @@ -471,10 +470,9 @@ class WalletTest(BitcoinTestFramework): # Import address and private key to check correct behavior of spendable unspents # 1. Send some coins to generate new UTXO address_to_import = self.nodes[2].getaddressinfo(self.nodes[2].getnewaddress())["confidential"] - txid = self.nodes[0].sendtoaddress(address_to_import, 1) + utxo = self.create_outpoints(self.nodes[0], outputs=[{address_to_import: 1}], blind=True)[0] self.sync_mempools(self.nodes[0:3]) - vout = find_vout_for_address(self.nodes[2], txid, address_to_import) - self.nodes[2].lockunspent(False, [{"txid": txid, "vout": vout}]) + self.nodes[2].lockunspent(False, [utxo]) self.generate(self.nodes[0], 1, sync_fun=lambda: self.sync_all(self.nodes[0:3])) self.log.info("Test sendtoaddress with fee_rate param (explicit fee rate in sat/vB)") diff --git a/test/functional/wallet_fundrawtransaction.py b/test/functional/wallet_fundrawtransaction.py index ac13909035..1a249a7a8f 100755 --- a/test/functional/wallet_fundrawtransaction.py +++ b/test/functional/wallet_fundrawtransaction.py @@ -22,7 +22,6 @@ from test_framework.util import ( assert_greater_than_or_equal, assert_raises_rpc_error, count_bytes, - find_vout_for_address, get_fee, ) from test_framework.wallet_util import generate_keypair, WalletUnlock @@ -85,14 +84,13 @@ class RawTransactionsTest(BitcoinTestFramework): Unlock all UTXOs except the watchonly one """ to_keep = [] - if self.watchonly_txid is not None and self.watchonly_vout is not None: - to_keep.append({"txid": self.watchonly_txid, "vout": self.watchonly_vout}) + if self.watchonly_utxo is not None: + to_keep.append(self.watchonly_utxo) wallet.lockunspent(True) wallet.lockunspent(False, to_keep) def run_test(self): - self.watchonly_txid = None - self.watchonly_vout = None + self.watchonly_utxo = None self.log.info("Connect nodes, set fees, generate blocks, and sync") self.min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee'] # This test is not meant to test fee estimation and we'd like @@ -167,11 +165,10 @@ class RawTransactionsTest(BitcoinTestFramework): self.watchonly_amount = Decimal(200) wwatch.importpubkey(watchonly_pubkey, "", True) wwatch.importblindingkey(watchonly_address, watchonly_blindingkey) - self.watchonly_txid = self.nodes[0].sendtoaddress(watchonly_address, self.watchonly_amount) + self.watchonly_utxo = self.create_outpoints(self.nodes[0], outputs=[{watchonly_address: self.watchonly_amount}], blind=True)[0] # Lock UTXO so nodes[0] doesn't accidentally spend it - self.watchonly_vout = find_vout_for_address(self.nodes[0], self.watchonly_txid, watchonly_address) - self.nodes[0].lockunspent(False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}]) + self.nodes[0].lockunspent(False, [self.watchonly_utxo]) self.nodes[0].sendtoaddress(self.nodes[3].get_wallet_rpc(self.default_wallet_name).getnewaddress(), self.watchonly_amount / 10) @@ -795,7 +792,7 @@ class RawTransactionsTest(BitcoinTestFramework): result = wwatch.fundrawtransaction(rawtx, True) res_dec = self.nodes[0].decoderawtransaction(result["hex"]) assert_equal(len(res_dec["vin"]), 1) - assert_equal(res_dec["vin"][0]["txid"], self.watchonly_txid) + assert_equal(res_dec["vin"][0]["txid"], self.watchonly_utxo['txid']) assert "fee" in result.keys() assert_greater_than(result["changepos"], -1) @@ -817,7 +814,7 @@ class RawTransactionsTest(BitcoinTestFramework): # unblinded_result = self.nodes[2].unblindrawtransaction(blinded_result) res_dec = self.nodes[0].decoderawtransaction(result["hex"]) assert_equal(len(res_dec["vin"]), 1) - assert res_dec["vin"][0]["txid"] == self.watchonly_txid + assert res_dec["vin"][0]["txid"] == self.watchonly_utxo['txid'] assert_greater_than(result["fee"], 0) assert_equal(result["changepos"], -1) @@ -1028,14 +1025,12 @@ class RawTransactionsTest(BitcoinTestFramework): n0_blind_addr = self.nodes[0].getnewaddress() addr_info = self.nodes[0].getaddressinfo(n0_blind_addr) - txid = self.nodes[2].sendtoaddress(addr_info['unconfidential'], 10) + utxo = self.create_outpoints(self.nodes[2], outputs=[{addr_info['unconfidential']: 10}])[0] self.sync_all() - vout = find_vout_for_address(self.nodes[0], txid, n0_blind_addr) self.generate(self.nodes[0], 1) - self.sync_all() # An external input without solving data should result in an error - raw_tx = self.nodes[2].createrawtransaction([{"txid": txid, "vout": vout}], [{addr_info['unconfidential']: 20}]) + raw_tx = self.nodes[2].createrawtransaction([utxo], [{addr_info['unconfidential']: 20}]) # But funding should work when the solving data is provided (as part of options) funded_tx = self.nodes[2].fundrawtransaction(raw_tx, {"solving_data": {"pubkeys": [addr_info['pubkey']]}}, False) @@ -1050,10 +1045,9 @@ class RawTransactionsTest(BitcoinTestFramework): self.log.info("Test fundrawtxn subtract fee from outputs with preset inputs that are sufficient") addr = self.nodes[0].getnewaddress() - txid = self.nodes[0].sendtoaddress(addr, 10) - vout = find_vout_for_address(self.nodes[0], txid, addr) + utxo = self.create_outpoints(self.nodes[0], outputs=[{addr: 10}], blind=True)[0] - rawtx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], [{self.nodes[0].getnewaddress(): 5}]) + rawtx = self.nodes[0].createrawtransaction([utxo], [{self.nodes[0].getnewaddress(): 5}]) fundedtx = self.nodes[0].fundrawtransaction(rawtx, subtractFeeFromOutputs=[0]) blindedtx = self.nodes[0].blindrawtransaction(fundedtx['hex']) signedtx = self.nodes[0].signrawtransactionwithwallet(blindedtx) @@ -1353,14 +1347,12 @@ class RawTransactionsTest(BitcoinTestFramework): addr = wallet.getnewaddress(address_type="bech32") ext_addr = self.nodes[0].getnewaddress(address_type="bech32") - txid = self.nodes[0].send([{addr: 5}, {ext_addr: 5}])["txid"] - vout = find_vout_for_address(self.nodes[0], txid, addr) - ext_vout = find_vout_for_address(self.nodes[0], txid, ext_addr) + utxo, ext_utxo = self.create_outpoints(self.nodes[0], outputs=[{addr: 5}, {ext_addr: 5}]) self.nodes[0].sendtoaddress(wallet.getnewaddress(address_type="bech32"), 5) self.generate(self.nodes[0], 1) - rawtx = wallet.createrawtransaction([{'txid': txid, 'vout': vout}], [{self.nodes[0].getnewaddress(address_type="bech32"): 8}]) + rawtx = wallet.createrawtransaction([utxo], [{self.nodes[0].getnewaddress(address_type="bech32"): 8}]) fundedtx = wallet.fundrawtransaction(rawtx, fee_rate=10, change_type="bech32") # with 71-byte signatures we should expect following tx size # 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) @@ -1368,7 +1360,7 @@ class RawTransactionsTest(BitcoinTestFramework): assert_equal(fundedtx['fee'] * COIN, 3260) # ELEMENTS # Using the other output should have 72 byte sigs - rawtx = wallet.createrawtransaction([{'txid': txid, 'vout': ext_vout}], [{self.nodes[0].getnewaddress(): 13}]) + rawtx = wallet.createrawtransaction([ext_utxo], [{self.nodes[0].getnewaddress(): 13}]) ext_desc = self.nodes[0].getaddressinfo(ext_addr)["desc"] fundedtx = wallet.fundrawtransaction(rawtx, fee_rate=10, change_type="bech32", solving_data={"descriptors": [ext_desc]}) # 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) @@ -1388,10 +1380,9 @@ class RawTransactionsTest(BitcoinTestFramework): addr = wallet.getnewaddress() inputs = [] for i in range(0, 2): - txid = self.nodes[2].sendtoaddress(addr, 5) - self.sync_mempools() - vout = find_vout_for_address(wallet, txid, addr) - inputs.append((txid, vout)) + utxo = self.create_outpoints(self.nodes[2], outputs=[{addr: 5}], blind=True)[0] + inputs.append((utxo['txid'], utxo['vout'])) + self.sync_mempools() # Unsafe inputs are ignored by default. rawtx = wallet.createrawtransaction([], [{self.nodes[2].getnewaddress(): 7.5}]) diff --git a/test/functional/wallet_importdescriptors.py b/test/functional/wallet_importdescriptors.py index a6b90b897a..840727f670 100755 --- a/test/functional/wallet_importdescriptors.py +++ b/test/functional/wallet_importdescriptors.py @@ -24,7 +24,6 @@ from test_framework.descriptors import descsum_create from test_framework.util import ( assert_equal, assert_raises_rpc_error, - find_vout_for_address, ) from test_framework.wallet_util import ( get_generate_key, @@ -493,12 +492,10 @@ class ImportDescriptorsTest(BitcoinTestFramework): assert_equal(wmulti_pub.getwalletinfo()['keypoolsize'], 999) # generate some utxos for next tests - txid = w0.sendtoaddress(addr, 10) - vout = find_vout_for_address(self.nodes[0], txid, addr) + utxo = self.create_outpoints(w0, outputs=[{addr: 10}])[0] addr2 = wmulti_pub.getnewaddress('', 'bech32') - txid2 = w0.sendtoaddress(addr2, 10) - vout2 = find_vout_for_address(self.nodes[0], txid2, addr2) + utxo2 = self.create_outpoints(w0, outputs=[{addr2: 10}])[0] self.generate(self.nodes[0], 6) assert_equal(wmulti_pub.getbalance(), wmulti_priv.getbalance()) @@ -554,7 +551,7 @@ class ImportDescriptorsTest(BitcoinTestFramework): assert_equal(res[1]['success'], True) assert_equal(res[1]['warnings'][0], 'Not all private keys provided. Some wallet functionality may return unexpected errors') - rawtx = self.nodes[1].createrawtransaction([{'txid': txid, 'vout': vout}], [{w0.getnewaddress(): 9.999}, {"fee": 0.001}]) + rawtx = self.nodes[1].createrawtransaction([utxo], [{w0.getnewaddress(): 9.999}, {"fee": 0.001}]) tx_signed_1 = wmulti_priv1.signrawtransactionwithwallet(rawtx) assert_equal(tx_signed_1['complete'], False) tx_signed_2 = wmulti_priv2.signrawtransactionwithwallet(tx_signed_1['hex']) @@ -648,7 +645,7 @@ class ImportDescriptorsTest(BitcoinTestFramework): }]) assert_equal(res[0]['success'], True) - rawtx = self.nodes[1].createrawtransaction([{'txid': txid2, 'vout': vout2}], [{w0.getnewaddress(): 9.999}, {"fee": 0.001}]) + rawtx = self.nodes[1].createrawtransaction([utxo2], [{w0.getnewaddress(): 9.999}, {"fee": 0.001}]) tx = wmulti_priv3.signrawtransactionwithwallet(rawtx) assert_equal(tx['complete'], True) self.nodes[1].sendrawtransaction(tx['hex']) diff --git a/test/functional/wallet_migration.py b/test/functional/wallet_migration.py index f2eafb7c2f..9dd6b6ac36 100755 --- a/test/functional/wallet_migration.py +++ b/test/functional/wallet_migration.py @@ -24,7 +24,6 @@ from test_framework.script_util import key_to_p2pkh_script, script_to_p2sh_scrip from test_framework.util import ( assert_equal, assert_raises_rpc_error, - find_vout_for_address, sha256sum_file, ) from test_framework.wallet_util import ( @@ -311,14 +310,14 @@ class WalletMigrationTest(BitcoinTestFramework): # Received watchonly tx that is then spent import_sent_addr = default.getnewaddress() imports0.importaddress(import_sent_addr) - received_sent_watchonly_txid = default.sendtoaddress(import_sent_addr, 10) - received_sent_watchonly_vout = find_vout_for_address(self.nodes[0], received_sent_watchonly_txid, import_sent_addr) - send = default.sendall(recipients=[default.getnewaddress()], inputs=[{"txid": received_sent_watchonly_txid, "vout": received_sent_watchonly_vout}]) + received_sent_watchonly_utxo = self.create_outpoints(node=default, outputs=[{import_sent_addr: 10}])[0] + + send = default.sendall(recipients=[default.getnewaddress()], inputs=[received_sent_watchonly_utxo]) sent_watchonly_txid = send["txid"] self.generate(self.nodes[0], 1) received_watchonly_tx_info = imports0.gettransaction(received_watchonly_txid, True) - received_sent_watchonly_tx_info = imports0.gettransaction(received_sent_watchonly_txid, True) + received_sent_watchonly_tx_info = imports0.gettransaction(received_sent_watchonly_utxo["txid"], True) balances = imports0.getbalances() spendable_bal = balances["mine"]["trusted"] @@ -333,7 +332,7 @@ class WalletMigrationTest(BitcoinTestFramework): assert_equal(imports0.getwalletinfo()["descriptors"], True) self.assert_is_sqlite("imports0") assert_raises_rpc_error(-5, "Invalid or non-wallet transaction id", imports0.gettransaction, received_watchonly_txid) - assert_raises_rpc_error(-5, "Invalid or non-wallet transaction id", imports0.gettransaction, received_sent_watchonly_txid) + assert_raises_rpc_error(-5, "Invalid or non-wallet transaction id", imports0.gettransaction, received_sent_watchonly_utxo['txid']) assert_raises_rpc_error(-5, "Invalid or non-wallet transaction id", imports0.gettransaction, sent_watchonly_txid) assert_equal(len(imports0.listtransactions(include_watchonly=True)), 1) imports0.gettransaction(received_txid) @@ -348,7 +347,7 @@ class WalletMigrationTest(BitcoinTestFramework): received_migrated_watchonly_tx_info = watchonly.gettransaction(received_watchonly_txid) assert_equal(received_watchonly_tx_info["time"], received_migrated_watchonly_tx_info["time"]) assert_equal(received_watchonly_tx_info["timereceived"], received_migrated_watchonly_tx_info["timereceived"]) - received_sent_migrated_watchonly_tx_info = watchonly.gettransaction(received_sent_watchonly_txid) + received_sent_migrated_watchonly_tx_info = watchonly.gettransaction(received_sent_watchonly_utxo["txid"]) assert_equal(received_sent_watchonly_tx_info["time"], received_sent_migrated_watchonly_tx_info["time"]) assert_equal(received_sent_watchonly_tx_info["timereceived"], received_sent_migrated_watchonly_tx_info["timereceived"]) watchonly.gettransaction(sent_watchonly_txid) diff --git a/test/functional/wallet_signrawtransactionwithwallet.py b/test/functional/wallet_signrawtransactionwithwallet.py index a4ea09f2ba..34e27a3dca 100755 --- a/test/functional/wallet_signrawtransactionwithwallet.py +++ b/test/functional/wallet_signrawtransactionwithwallet.py @@ -14,7 +14,6 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, assert_raises_rpc_error, - find_vout_for_address, ) from test_framework.messages import ( CTxInWitness, @@ -206,13 +205,12 @@ class SignRawTransactionWithWalletTest(BitcoinTestFramework): address = script_to_p2wsh(script) # Fund that address and make the spend - txid = self.nodes[0].sendtoaddress(address, 1) - vout = find_vout_for_address(self.nodes[0], txid, address) + utxo1 = self.create_outpoints(self.nodes[0], outputs=[{address: 1}])[0] self.generate(self.nodes[0], 1, sync_fun=self.no_op) - utxo = self.nodes[0].listunspent()[0] - amt = Decimal(1) + utxo["amount"] - Decimal(0.00001) + utxo2 = self.nodes[0].listunspent()[0] + amt = Decimal(1) + utxo2["amount"] - Decimal(0.00001) tx = self.nodes[0].createrawtransaction( - [{"txid": txid, "vout": vout, "sequence": 1},{"txid": utxo["txid"], "vout": utxo["vout"]}], + [{**utxo1, "sequence": 1},{"txid": utxo2["txid"], "vout": utxo2["vout"]}], [{self.nodes[0].getnewaddress(): amt}, {"fee": 0.00001}], self.nodes[0].getblockcount() ) @@ -241,10 +239,9 @@ class SignRawTransactionWithWalletTest(BitcoinTestFramework): address = script_to_p2wsh(script) # Fund that address and make the spend - txid = self.nodes[0].sendtoaddress(address, 1) - vout = find_vout_for_address(self.nodes[0], txid, address) + utxo1 = self.create_outpoints(self.nodes[0], outputs=[{address: 1}])[0] self.generate(self.nodes[0], 1, sync_fun=self.no_op) - utxo = self.nodes[0].listunspent()[0] + utxo2 = self.nodes[0].listunspent()[0] # ELEMENTS: # use increased Decimal precision # when utxo['amount'] has many decimal places (eg: 50.00005640) @@ -253,9 +250,9 @@ class SignRawTransactionWithWalletTest(BitcoinTestFramework): # precision 10: 1 + 50.00005640 - 0.00001 = 51.00004640 # which causes the inputs/outputs to not balance getcontext().prec = 10 - amt = Decimal(1) + utxo["amount"] - Decimal(0.00001) + amt = Decimal(1) + utxo2["amount"] - Decimal(0.00001) tx = self.nodes[0].createrawtransaction( - [{"txid": txid, "vout": vout},{"txid": utxo["txid"], "vout": utxo["vout"]}], + [utxo1, {"txid": utxo2["txid"], "vout": utxo2["vout"]}], [{self.nodes[0].getnewaddress(): amt}, {"fee": 0.00001}], self.nodes[0].getblockcount() ) diff --git a/test/functional/wallet_txn_clone.py b/test/functional/wallet_txn_clone.py index 7775816cd0..d59d4ad772 100755 --- a/test/functional/wallet_txn_clone.py +++ b/test/functional/wallet_txn_clone.py @@ -7,7 +7,6 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, - find_vout_for_address ) from test_framework.messages import ( COIN, @@ -35,8 +34,8 @@ class TxnMallTest(BitcoinTestFramework): super().setup_network() self.disconnect_nodes(1, 2) - def spend_txid(self, txid, vout, outputs): - inputs = [{"txid": txid, "vout": vout}] + def spend_utxo(self, utxo, outputs): + inputs = [utxo] tx = self.nodes[0].createrawtransaction(inputs, outputs) tx = self.nodes[0].fundrawtransaction(tx) tx = self.nodes[0].signrawtransactionwithwallet(tx['hex']) @@ -56,13 +55,13 @@ class TxnMallTest(BitcoinTestFramework): self.nodes[0].settxfee(.001) node0_address1 = self.nodes[0].getnewaddress(address_type=output_type) - node0_txid1 = self.nodes[0].sendtoaddress(node0_address1, 1219) - node0_tx1 = self.nodes[0].gettransaction(node0_txid1) - self.nodes[0].lockunspent(False, [{"txid":node0_txid1, "vout": find_vout_for_address(self.nodes[0], node0_txid1, node0_address1)}]) + node0_utxo1 = self.create_outpoints(self.nodes[0], outputs=[{node0_address1: 1219}])[0] + node0_tx1 = self.nodes[0].gettransaction(node0_utxo1['txid']) + self.nodes[0].lockunspent(False, [node0_utxo1]) node0_address2 = self.nodes[0].getnewaddress(address_type=output_type) - node0_txid2 = self.nodes[0].sendtoaddress(node0_address2, 29) - node0_tx2 = self.nodes[0].gettransaction(node0_txid2) + node0_utxo2 = self.create_outpoints(self.nodes[0], outputs=[{node0_address2: 29}])[0] + node0_tx2 = self.nodes[0].gettransaction(node0_utxo2['txid']) assert_equal(self.nodes[0].getbalance()['bitcoin'], starting_balance + node0_tx1["fee"]['bitcoin'] + node0_tx2["fee"]['bitcoin']) @@ -71,8 +70,8 @@ class TxnMallTest(BitcoinTestFramework): node1_address = self.nodes[1].getnewaddress() # Send tx1, and another transaction tx2 that won't be cloned - txid1 = self.spend_txid(node0_txid1, find_vout_for_address(self.nodes[0], node0_txid1, node0_address1), [{node1_address: 40}]) - txid2 = self.spend_txid(node0_txid2, find_vout_for_address(self.nodes[0], node0_txid2, node0_address2), [{node1_address: 20}]) + txid1 = self.spend_utxo(node0_utxo1, [{node1_address: 40}]) + txid2 = self.spend_utxo(node0_utxo2, [{node1_address: 20}]) # Construct a clone of tx1, to be malleated rawtx1 = self.nodes[0].getrawtransaction(txid1, 1) diff --git a/test/functional/wallet_txn_doublespend.py b/test/functional/wallet_txn_doublespend.py index d118d2f018..30295666fc 100755 --- a/test/functional/wallet_txn_doublespend.py +++ b/test/functional/wallet_txn_doublespend.py @@ -8,8 +8,6 @@ from decimal import Decimal from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, - find_output, - find_vout_for_address ) @@ -31,8 +29,8 @@ class TxnMallTest(BitcoinTestFramework): super().setup_network() self.disconnect_nodes(1, 2) - def spend_txid(self, txid, vout, outputs): - inputs = [{"txid": txid, "vout": vout}] + def spend_utxo(self, utxo, outputs): + inputs = [utxo] tx = self.nodes[0].createrawtransaction(inputs, outputs) tx = self.nodes[0].fundrawtransaction(tx) tx = self.nodes[0].signrawtransactionwithwallet(tx['hex']) @@ -54,13 +52,13 @@ class TxnMallTest(BitcoinTestFramework): # Assign coins to foo and bar addresses: node0_address_foo = self.nodes[0].getnewaddress() - fund_foo_txid = self.nodes[0].sendtoaddress(node0_address_foo, 1219) - fund_foo_tx = self.nodes[0].gettransaction(fund_foo_txid) - self.nodes[0].lockunspent(False, [{"txid":fund_foo_txid, "vout": find_vout_for_address(self.nodes[0], fund_foo_txid, node0_address_foo)}]) + fund_foo_utxo = self.create_outpoints(self.nodes[0], outputs=[{node0_address_foo: 1219}])[0] + fund_foo_tx = self.nodes[0].gettransaction(fund_foo_utxo['txid']) + self.nodes[0].lockunspent(False, [fund_foo_utxo]) node0_address_bar = self.nodes[0].getnewaddress() - fund_bar_txid = self.nodes[0].sendtoaddress(node0_address_bar, 29) - fund_bar_tx = self.nodes[0].gettransaction(fund_bar_txid) + fund_bar_utxo = self.create_outpoints(node=self.nodes[0], outputs=[{node0_address_bar: 29}])[0] + fund_bar_tx = self.nodes[0].gettransaction(fund_bar_utxo['txid']) assert_equal(self.nodes[0].getbalance(), {"bitcoin": starting_balance + fund_foo_tx["fee"]['bitcoin'] + fund_bar_tx["fee"]['bitcoin']}) @@ -71,13 +69,7 @@ class TxnMallTest(BitcoinTestFramework): # First: use raw transaction API to send 1240 BTC to node1_address, # but don't broadcast: doublespend_fee = Decimal('-.02') - rawtx_input_0 = {} - rawtx_input_0["txid"] = fund_foo_txid - rawtx_input_0["vout"] = find_output(self.nodes[0], fund_foo_txid, 1219) - rawtx_input_1 = {} - rawtx_input_1["txid"] = fund_bar_txid - rawtx_input_1["vout"] = find_output(self.nodes[0], fund_bar_txid, 29) - inputs = [rawtx_input_0, rawtx_input_1] + inputs = [fund_foo_utxo, fund_bar_utxo] change_address = self.nodes[0].getnewaddress() outputs = [{node1_address: 1240}, {change_address: 1248 - 1240 + doublespend_fee}, {"fee": (1219+29) - (1240+1248 - 1240 + doublespend_fee)}] rawtx = self.nodes[0].createrawtransaction(inputs, outputs) @@ -85,8 +77,8 @@ class TxnMallTest(BitcoinTestFramework): assert_equal(doublespend["complete"], True) # Create two spends using 1 50 BTC coin each - txid1 = self.spend_txid(fund_foo_txid, find_vout_for_address(self.nodes[0], fund_foo_txid, node0_address_foo), [{node1_address: 40}]) - txid2 = self.spend_txid(fund_bar_txid, find_vout_for_address(self.nodes[0], fund_bar_txid, node0_address_bar), [{node1_address: 20}]) + txid1 = self.spend_utxo(fund_foo_utxo, [{node1_address: 40}]) + txid2 = self.spend_utxo(fund_bar_utxo, [{node1_address: 20}]) # Have node0 mine a block: if (self.options.mine_block):