Skip to content

Commit d3582f2

Browse files
author
MarcoFalke
committed
Merge bitcoin#23866: test: use MiniWallet for rpc_scantxoutset.py
e844115 test: use MiniWallet for rpc_scantxoutset.py (Sebastian Falbesoner) 983ca04 test: introduce `address_to_scriptpubkey` helper (Sebastian Falbesoner) e704d4d test: introduce `getnewdestination` helper for generating various address types (Sebastian Falbesoner) Pull request description: This PR enables one more of the non-wallet functional tests (rpc_scantxoutset.py) to be run even with the Bitcoin Core wallet disabled by using the MiniWallet instead, as proposed in bitcoin#20078 and bitcoin#23858 (comment) more recently. Reviewer's guide: * [commit 1/3] For replacing the getnewaddress/getaddressinfo RPC calls a helper `getnewdestination` is introduced which allows to create addresses with the common address format types ('legacy', 'p2sh-segwit' and 'bech32'), but also additionally returns the corresponding pubkey and output script. * [commit 2/3] In order to send to addresses with MiniWallet, a helper `address_to_scriptpubkey` is introduced. It only supports legacy addresses (Base58Check) so far, which is sufficient for the scantxoutset test. * [commit 3/3] With those helpers, the use of MiniWallet in the test is quite straight-forward. To avoid repeatedly specifying parameters like `from_node` to MiniWallet's `send_to` method, another test-internal helper `sendtodestination` is introduced which supports specifying the destination both as outputscript or as address. ACKs for top commit: w0xlt: reACK [e844115](bitcoin@e844115) Tree-SHA512: e4823dc507019b2b8e479602963c5dddc4c78211e1d934218ee0f0e32c068ab7e44e9793307f549127946364f57d684c4ea1d70f25865ea70d30d4f3855836d0
2 parents a213bd6 + e844115 commit d3582f2

File tree

3 files changed

+86
-55
lines changed

3 files changed

+86
-55
lines changed

test/functional/p2p_filter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from test_framework.test_framework import BitcoinTestFramework
3232
from test_framework.wallet import (
3333
MiniWallet,
34-
random_p2wpkh,
34+
getnewdestination,
3535
)
3636

3737

@@ -169,14 +169,14 @@ def test_filter(self, filter_peer):
169169

170170
self.log.info('Check that we only receive a merkleblock if the filter does not match a tx in a block')
171171
filter_peer.tx_received = False
172-
block_hash = self.generatetoscriptpubkey(random_p2wpkh())
172+
block_hash = self.generatetoscriptpubkey(getnewdestination()[1])
173173
filter_peer.wait_for_merkleblock(block_hash)
174174
assert not filter_peer.tx_received
175175

176176
self.log.info('Check that we not receive a tx if the filter does not match a mempool tx')
177177
filter_peer.merkleblock_received = False
178178
filter_peer.tx_received = False
179-
self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=random_p2wpkh(), amount=7 * COIN)
179+
self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=7 * COIN)
180180
filter_peer.sync_send_with_ping()
181181
assert not filter_peer.merkleblock_received
182182
assert not filter_peer.tx_received
@@ -190,14 +190,14 @@ def test_filter(self, filter_peer):
190190
self.log.info('Check that after deleting filter all txs get relayed again')
191191
filter_peer.send_and_ping(msg_filterclear())
192192
for _ in range(5):
193-
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=random_p2wpkh(), amount=7 * COIN)
193+
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=7 * COIN)
194194
filter_peer.wait_for_tx(txid)
195195

196196
self.log.info('Check that request for filtered blocks is ignored if no filter is set')
197197
filter_peer.merkleblock_received = False
198198
filter_peer.tx_received = False
199199
with self.nodes[0].assert_debug_log(expected_msgs=['received getdata']):
200-
block_hash = self.generatetoscriptpubkey(random_p2wpkh())
200+
block_hash = self.generatetoscriptpubkey(getnewdestination()[1])
201201
filter_peer.wait_for_inv([CInv(MSG_BLOCK, int(block_hash, 16))])
202202
filter_peer.sync_with_ping()
203203
assert not filter_peer.merkleblock_received

test/functional/rpc_scantxoutset.py

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the scantxoutset rpc call."""
6+
from test_framework.messages import COIN
67
from test_framework.test_framework import BitcoinTestFramework
78
from test_framework.util import assert_equal, assert_raises_rpc_error
9+
from test_framework.wallet import (
10+
MiniWallet,
11+
address_to_scriptpubkey,
12+
getnewdestination,
13+
)
814

915
from decimal import Decimal
10-
import shutil
11-
import os
1216

1317

1418
def descriptors(out):
@@ -18,66 +22,55 @@ def descriptors(out):
1822
class ScantxoutsetTest(BitcoinTestFramework):
1923
def set_test_params(self):
2024
self.num_nodes = 1
21-
self.setup_clean_chain = True
2225

23-
def skip_test_if_missing_module(self):
24-
self.skip_if_no_wallet()
26+
def sendtodestination(self, destination, amount):
27+
# interpret strings as addresses, assume scriptPubKey otherwise
28+
if isinstance(destination, str):
29+
destination = address_to_scriptpubkey(destination)
30+
self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=destination, amount=int(COIN * amount))
2531

2632
def run_test(self):
27-
self.log.info("Mining blocks...")
28-
self.generate(self.nodes[0], 110)
29-
30-
addr_P2SH_SEGWIT = self.nodes[0].getnewaddress("", "p2sh-segwit")
31-
pubk1 = self.nodes[0].getaddressinfo(addr_P2SH_SEGWIT)['pubkey']
32-
addr_LEGACY = self.nodes[0].getnewaddress("", "legacy")
33-
pubk2 = self.nodes[0].getaddressinfo(addr_LEGACY)['pubkey']
34-
addr_BECH32 = self.nodes[0].getnewaddress("", "bech32")
35-
pubk3 = self.nodes[0].getaddressinfo(addr_BECH32)['pubkey']
36-
txid = self.nodes[0].sendtoaddress(addr_P2SH_SEGWIT, 0.001)
37-
self.nodes[0].lockunspent(unlock=False, transactions=[{"txid": txid, "vout": 0}, {"txid": txid, "vout": 1}])
38-
txid = self.nodes[0].sendtoaddress(addr_LEGACY, 0.002)
39-
self.nodes[0].lockunspent(unlock=False, transactions=[{"txid": txid, "vout": 0}, {"txid": txid, "vout": 1}])
40-
txid = self.nodes[0].sendtoaddress(addr_BECH32, 0.004)
41-
self.nodes[0].lockunspent(unlock=False, transactions=[{"txid": txid, "vout": 0}, {"txid": txid, "vout": 1}])
33+
self.wallet = MiniWallet(self.nodes[0])
34+
self.wallet.rescan_utxos()
35+
36+
self.log.info("Create UTXOs...")
37+
pubk1, spk_P2SH_SEGWIT, addr_P2SH_SEGWIT = getnewdestination("p2sh-segwit")
38+
pubk2, spk_LEGACY, addr_LEGACY = getnewdestination("legacy")
39+
pubk3, spk_BECH32, addr_BECH32 = getnewdestination("bech32")
40+
self.sendtodestination(spk_P2SH_SEGWIT, 0.001)
41+
self.sendtodestination(spk_LEGACY, 0.002)
42+
self.sendtodestination(spk_BECH32, 0.004)
4243

4344
#send to child keys of tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK
44-
self.nodes[0].sendtoaddress("mkHV1C6JLheLoUSSZYk7x3FH5tnx9bu7yc", 0.008) # (m/0'/0'/0')
45-
self.nodes[0].sendtoaddress("mipUSRmJAj2KrjSvsPQtnP8ynUon7FhpCR", 0.016) # (m/0'/0'/1')
46-
self.nodes[0].sendtoaddress("n37dAGe6Mq1HGM9t4b6rFEEsDGq7Fcgfqg", 0.032) # (m/0'/0'/1500')
47-
self.nodes[0].sendtoaddress("mqS9Rpg8nNLAzxFExsgFLCnzHBsoQ3PRM6", 0.064) # (m/0'/0'/0)
48-
self.nodes[0].sendtoaddress("mnTg5gVWr3rbhHaKjJv7EEEc76ZqHgSj4S", 0.128) # (m/0'/0'/1)
49-
self.nodes[0].sendtoaddress("mketCd6B9U9Uee1iCsppDJJBHfvi6U6ukC", 0.256) # (m/0'/0'/1500)
50-
self.nodes[0].sendtoaddress("mj8zFzrbBcdaWXowCQ1oPZ4qioBVzLzAp7", 0.512) # (m/1/1/0')
51-
self.nodes[0].sendtoaddress("mfnKpKQEftniaoE1iXuMMePQU3PUpcNisA", 1.024) # (m/1/1/1')
52-
self.nodes[0].sendtoaddress("mou6cB1kaP1nNJM1sryW6YRwnd4shTbXYQ", 2.048) # (m/1/1/1500')
53-
self.nodes[0].sendtoaddress("mtfUoUax9L4tzXARpw1oTGxWyoogp52KhJ", 4.096) # (m/1/1/0)
54-
self.nodes[0].sendtoaddress("mxp7w7j8S1Aq6L8StS2PqVvtt4HGxXEvdy", 8.192) # (m/1/1/1)
55-
self.nodes[0].sendtoaddress("mpQ8rokAhp1TAtJQR6F6TaUmjAWkAWYYBq", 16.384) # (m/1/1/1500)
45+
self.sendtodestination("mkHV1C6JLheLoUSSZYk7x3FH5tnx9bu7yc", 0.008) # (m/0'/0'/0')
46+
self.sendtodestination("mipUSRmJAj2KrjSvsPQtnP8ynUon7FhpCR", 0.016) # (m/0'/0'/1')
47+
self.sendtodestination("n37dAGe6Mq1HGM9t4b6rFEEsDGq7Fcgfqg", 0.032) # (m/0'/0'/1500')
48+
self.sendtodestination("mqS9Rpg8nNLAzxFExsgFLCnzHBsoQ3PRM6", 0.064) # (m/0'/0'/0)
49+
self.sendtodestination("mnTg5gVWr3rbhHaKjJv7EEEc76ZqHgSj4S", 0.128) # (m/0'/0'/1)
50+
self.sendtodestination("mketCd6B9U9Uee1iCsppDJJBHfvi6U6ukC", 0.256) # (m/0'/0'/1500)
51+
self.sendtodestination("mj8zFzrbBcdaWXowCQ1oPZ4qioBVzLzAp7", 0.512) # (m/1/1/0')
52+
self.sendtodestination("mfnKpKQEftniaoE1iXuMMePQU3PUpcNisA", 1.024) # (m/1/1/1')
53+
self.sendtodestination("mou6cB1kaP1nNJM1sryW6YRwnd4shTbXYQ", 2.048) # (m/1/1/1500')
54+
self.sendtodestination("mtfUoUax9L4tzXARpw1oTGxWyoogp52KhJ", 4.096) # (m/1/1/0)
55+
self.sendtodestination("mxp7w7j8S1Aq6L8StS2PqVvtt4HGxXEvdy", 8.192) # (m/1/1/1)
56+
self.sendtodestination("mpQ8rokAhp1TAtJQR6F6TaUmjAWkAWYYBq", 16.384) # (m/1/1/1500)
5657

5758
self.generate(self.nodes[0], 1)
5859

59-
self.log.info("Stop node, remove wallet, mine again some blocks...")
60-
self.stop_node(0)
61-
shutil.rmtree(os.path.join(self.nodes[0].datadir, self.chain, 'wallets'))
62-
self.start_node(0, ['-nowallet'])
63-
self.import_deterministic_coinbase_privkeys()
64-
self.generate(self.nodes[0], 110)
65-
6660
scan = self.nodes[0].scantxoutset("start", [])
6761
info = self.nodes[0].gettxoutsetinfo()
6862
assert_equal(scan['success'], True)
6963
assert_equal(scan['height'], info['height'])
7064
assert_equal(scan['txouts'], info['txouts'])
7165
assert_equal(scan['bestblock'], info['bestblock'])
7266

73-
self.restart_node(0, ['-nowallet'])
7467
self.log.info("Test if we have found the non HD unspent outputs.")
75-
assert_equal(self.nodes[0].scantxoutset("start", ["pkh(" + pubk1 + ")", "pkh(" + pubk2 + ")", "pkh(" + pubk3 + ")"])['total_amount'], Decimal("0.002"))
76-
assert_equal(self.nodes[0].scantxoutset("start", ["wpkh(" + pubk1 + ")", "wpkh(" + pubk2 + ")", "wpkh(" + pubk3 + ")"])['total_amount'], Decimal("0.004"))
77-
assert_equal(self.nodes[0].scantxoutset("start", ["sh(wpkh(" + pubk1 + "))", "sh(wpkh(" + pubk2 + "))", "sh(wpkh(" + pubk3 + "))"])['total_amount'], Decimal("0.001"))
78-
assert_equal(self.nodes[0].scantxoutset("start", ["combo(" + pubk1 + ")", "combo(" + pubk2 + ")", "combo(" + pubk3 + ")"])['total_amount'], Decimal("0.007"))
68+
assert_equal(self.nodes[0].scantxoutset("start", ["pkh(" + pubk1.hex() + ")", "pkh(" + pubk2.hex() + ")", "pkh(" + pubk3.hex() + ")"])['total_amount'], Decimal("0.002"))
69+
assert_equal(self.nodes[0].scantxoutset("start", ["wpkh(" + pubk1.hex() + ")", "wpkh(" + pubk2.hex() + ")", "wpkh(" + pubk3.hex() + ")"])['total_amount'], Decimal("0.004"))
70+
assert_equal(self.nodes[0].scantxoutset("start", ["sh(wpkh(" + pubk1.hex() + "))", "sh(wpkh(" + pubk2.hex() + "))", "sh(wpkh(" + pubk3.hex() + "))"])['total_amount'], Decimal("0.001"))
71+
assert_equal(self.nodes[0].scantxoutset("start", ["combo(" + pubk1.hex() + ")", "combo(" + pubk2.hex() + ")", "combo(" + pubk3.hex() + ")"])['total_amount'], Decimal("0.007"))
7972
assert_equal(self.nodes[0].scantxoutset("start", ["addr(" + addr_P2SH_SEGWIT + ")", "addr(" + addr_LEGACY + ")", "addr(" + addr_BECH32 + ")"])['total_amount'], Decimal("0.007"))
80-
assert_equal(self.nodes[0].scantxoutset("start", ["addr(" + addr_P2SH_SEGWIT + ")", "addr(" + addr_LEGACY + ")", "combo(" + pubk3 + ")"])['total_amount'], Decimal("0.007"))
73+
assert_equal(self.nodes[0].scantxoutset("start", ["addr(" + addr_P2SH_SEGWIT + ")", "addr(" + addr_LEGACY + ")", "combo(" + pubk3.hex() + ")"])['total_amount'], Decimal("0.007"))
8174

8275
self.log.info("Test range validation.")
8376
assert_raises_rpc_error(-8, "End of range is too high", self.nodes[0].scantxoutset, "start", [{"desc": "desc", "range": -1}])

test/functional/test_framework/wallet.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
from enum import Enum
1010
from random import choice
1111
from typing import Optional
12-
from test_framework.address import create_deterministic_address_bcrt1_p2tr_op_true
12+
from test_framework.address import (
13+
base58_to_byte,
14+
create_deterministic_address_bcrt1_p2tr_op_true,
15+
key_to_p2pkh,
16+
key_to_p2sh_p2wpkh,
17+
key_to_p2wpkh,
18+
)
1319
from test_framework.descriptors import descsum_create
1420
from test_framework.key import ECKey
1521
from test_framework.messages import (
@@ -31,7 +37,11 @@
3137
)
3238
from test_framework.script_util import (
3339
key_to_p2pk_script,
40+
key_to_p2pkh_script,
41+
key_to_p2sh_p2wpkh_script,
3442
key_to_p2wpkh_script,
43+
keyhash_to_p2pkh_script,
44+
scripthash_to_p2sh_script,
3545
)
3646
from test_framework.util import (
3747
assert_equal,
@@ -209,12 +219,40 @@ def sendrawtransaction(self, *, from_node, tx_hex):
209219
return txid
210220

211221

212-
def random_p2wpkh():
213-
"""Generate a random P2WPKH scriptPubKey. Can be used when a random destination is needed,
214-
but no compiled wallet is available (e.g. as replacement to the getnewaddress RPC)."""
222+
def getnewdestination(address_type='bech32'):
223+
"""Generate a random destination of the specified type and return the
224+
corresponding public key, scriptPubKey and address. Supported types are
225+
'legacy', 'p2sh-segwit' and 'bech32'. Can be used when a random
226+
destination is needed, but no compiled wallet is available (e.g. as
227+
replacement to the getnewaddress/getaddressinfo RPCs)."""
215228
key = ECKey()
216229
key.generate()
217-
return key_to_p2wpkh_script(key.get_pubkey().get_bytes())
230+
pubkey = key.get_pubkey().get_bytes()
231+
if address_type == 'legacy':
232+
scriptpubkey = key_to_p2pkh_script(pubkey)
233+
address = key_to_p2pkh(pubkey)
234+
elif address_type == 'p2sh-segwit':
235+
scriptpubkey = key_to_p2sh_p2wpkh_script(pubkey)
236+
address = key_to_p2sh_p2wpkh(pubkey)
237+
elif address_type == 'bech32':
238+
scriptpubkey = key_to_p2wpkh_script(pubkey)
239+
address = key_to_p2wpkh(pubkey)
240+
# TODO: also support bech32m (need to generate x-only-pubkey)
241+
else:
242+
assert False
243+
return pubkey, scriptpubkey, address
244+
245+
246+
def address_to_scriptpubkey(address):
247+
"""Converts a given address to the corresponding output script (scriptPubKey)."""
248+
payload, version = base58_to_byte(address)
249+
if version == 111: # testnet pubkey hash
250+
return keyhash_to_p2pkh_script(payload)
251+
elif version == 196: # testnet script hash
252+
return scripthash_to_p2sh_script(payload)
253+
# TODO: also support other address formats
254+
else:
255+
assert False
218256

219257

220258
def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE):

0 commit comments

Comments
 (0)