Skip to content

Commit 7349d57

Browse files
committed
Merge bitcoin/bitcoin#30701: Use MiniWallet in functional test rpc_signrawtransactionwithkey.
a563f41 Remove second node since only 1 is needed for the test (Martin Saposnic) 1f4cdb3 Replace custom funding tx creation with MiniWallet. (Martin Saposnic) Pull request description: In response to issue bitcoin/bitcoin#30600, optimizations have been implemented to enhance test efficiency and readability: This PR refactors the `rpc_signrawtransactionwithkey.py` functional test to use MiniWallet for creating funding transactions. This simplifies the test code and improves performance by eliminating the need to mine new blocks for each funding transaction. Key changes: - Replaced custom `send_to_address` method with MiniWallet's `send_to` method - Removed unnecessary setup of a clean chain and second node - Simplified transaction creation and signing process ACKs for top commit: glozow: ACK a563f41 ismaelsadeeq: code review ACK a563f41 theStack: ACK a563f41 Tree-SHA512: 318959f89702b169453d537dafb822f5ef1921db1088941d8bbdb3171dd7a6ecad590e57a3802bc37bcf8992267ed6ffa7f156b229d9817ebf812bd35df509b5
2 parents 5c7d4b9 + a563f41 commit 7349d57

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

test/functional/rpc_signrawtransactionwithkey.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test transaction signing using the signrawtransactionwithkey RPC."""
66

7-
from test_framework.blocktools import (
8-
COINBASE_MATURITY,
7+
from test_framework.messages import (
8+
COIN,
99
)
1010
from test_framework.address import (
1111
address_to_scriptpubkey,
@@ -16,7 +16,6 @@
1616
from test_framework.util import (
1717
assert_equal,
1818
assert_raises_rpc_error,
19-
find_vout_for_address,
2019
)
2120
from test_framework.script_util import (
2221
key_to_p2pk_script,
@@ -26,6 +25,7 @@
2625
)
2726
from test_framework.wallet import (
2827
getnewdestination,
28+
MiniWallet,
2929
)
3030
from test_framework.wallet_util import (
3131
generate_keypair,
@@ -46,16 +46,12 @@
4646

4747
class SignRawTransactionWithKeyTest(BitcoinTestFramework):
4848
def set_test_params(self):
49-
self.setup_clean_chain = True
50-
self.num_nodes = 2
49+
self.num_nodes = 1
5150

5251
def send_to_address(self, addr, amount):
53-
input = {"txid": self.nodes[0].getblock(self.block_hash[self.blk_idx])["tx"][0], "vout": 0}
54-
output = {addr: amount}
55-
self.blk_idx += 1
56-
rawtx = self.nodes[0].createrawtransaction([input], output)
57-
txid = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithkey(rawtx, [self.nodes[0].get_deterministic_priv_key().key])["hex"], 0)
58-
return txid
52+
script_pub_key = address_to_scriptpubkey(addr)
53+
tx = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=script_pub_key, amount=int(amount * COIN))
54+
return tx["txid"], tx["sent_vout"]
5955

6056
def assert_signing_completed_successfully(self, signed_tx):
6157
assert 'errors' not in signed_tx
@@ -80,14 +76,12 @@ def witness_script_test(self):
8076
self.log.info("Test signing transaction to P2SH-P2WSH addresses without wallet")
8177
# Create a new P2SH-P2WSH 1-of-1 multisig address:
8278
embedded_privkey, embedded_pubkey = generate_keypair(wif=True)
83-
p2sh_p2wsh_address = self.nodes[1].createmultisig(1, [embedded_pubkey.hex()], "p2sh-segwit")
79+
p2sh_p2wsh_address = self.nodes[0].createmultisig(1, [embedded_pubkey.hex()], "p2sh-segwit")
8480
# send transaction to P2SH-P2WSH 1-of-1 multisig address
85-
self.block_hash = self.generate(self.nodes[0], COINBASE_MATURITY + 1)
86-
self.blk_idx = 0
8781
self.send_to_address(p2sh_p2wsh_address["address"], 49.999)
8882
self.generate(self.nodes[0], 1)
8983
# Get the UTXO info from scantxoutset
90-
unspent_output = self.nodes[1].scantxoutset('start', [p2sh_p2wsh_address['descriptor']])['unspents'][0]
84+
unspent_output = self.nodes[0].scantxoutset('start', [p2sh_p2wsh_address['descriptor']])['unspents'][0]
9185
spk = script_to_p2sh_p2wsh_script(p2sh_p2wsh_address['redeemScript']).hex()
9286
unspent_output['witnessScript'] = p2sh_p2wsh_address['redeemScript']
9387
unspent_output['redeemScript'] = script_to_p2wsh_script(unspent_output['witnessScript']).hex()
@@ -103,9 +97,9 @@ def witness_script_test(self):
10397

10498
def keyless_signing_test(self):
10599
self.log.info("Test that keyless 'signing' of pay-to-anchor input succeeds")
106-
funding_txid = self.send_to_address(p2a(), 49.999)
100+
[txid, vout] = self.send_to_address(p2a(), 49.999)
107101
spending_tx = self.nodes[0].createrawtransaction(
108-
[{"txid": funding_txid, "vout": 0}],
102+
[{"txid": txid, "vout": vout}],
109103
[{getnewdestination()[2]: Decimal("49.998")}])
110104
spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [], [])
111105
self.assert_signing_completed_successfully(spending_tx_signed)
@@ -124,9 +118,7 @@ def verify_txn_with_witness_script(self, tx_type):
124118
addr = script_to_p2sh(redeem_script)
125119
script_pub_key = address_to_scriptpubkey(addr).hex()
126120
# Fund that address
127-
txid = self.send_to_address(addr, 10)
128-
vout = find_vout_for_address(self.nodes[0], txid, addr)
129-
self.generate(self.nodes[0], 1)
121+
[txid, vout] = self.send_to_address(addr, 10)
130122
# Now create and sign a transaction spending that output on node[0], which doesn't know the scripts or keys
131123
spending_tx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], {getnewdestination()[2]: Decimal("9.999")})
132124
spending_tx_signed = self.nodes[0].signrawtransactionwithkey(spending_tx, [embedded_privkey], [{'txid': txid, 'vout': vout, 'scriptPubKey': script_pub_key, 'redeemScript': redeem_script, 'witnessScript': witness_script, 'amount': 10}])
@@ -149,6 +141,7 @@ def invalid_private_key_and_tx(self):
149141
assert_raises_rpc_error(-22, "TX decode failed. Make sure the tx has at least one input.", self.nodes[0].signrawtransactionwithkey, tx + "00", privkeys)
150142

151143
def run_test(self):
144+
self.wallet = MiniWallet(self.nodes[0])
152145
self.successful_signing_test()
153146
self.witness_script_test()
154147
self.keyless_signing_test()

0 commit comments

Comments
 (0)