test: Replace usage of importaddress

This commit is contained in:
Ava Chow 2025-05-08 11:48:38 -07:00
parent fcc457573f
commit d314207779
7 changed files with 34 additions and 48 deletions

View File

@ -112,7 +112,8 @@ class PSBTTest(BitcoinTestFramework):
# Mine a transaction that credits the offline address # Mine a transaction that credits the offline address
offline_addr = offline_node.getnewaddress(address_type="bech32m") offline_addr = offline_node.getnewaddress(address_type="bech32m")
online_addr = w2.getnewaddress(address_type="bech32m") online_addr = w2.getnewaddress(address_type="bech32m")
wonline.importaddress(offline_addr, label="", rescan=False) import_res = wonline.importdescriptors([{"desc": offline_node.getaddressinfo(offline_addr)["desc"], "timestamp": "now"}])
assert_equal(import_res[0]["success"], True)
mining_wallet = mining_node.get_wallet_rpc(self.default_wallet_name) mining_wallet = mining_node.get_wallet_rpc(self.default_wallet_name)
mining_wallet.sendtoaddress(address=offline_addr, amount=1.0) mining_wallet.sendtoaddress(address=offline_addr, amount=1.0)
self.generate(mining_node, nblocks=1, sync_fun=lambda: self.sync_all([online_node, mining_node])) self.generate(mining_node, nblocks=1, sync_fun=lambda: self.sync_all([online_node, mining_node]))

View File

@ -959,30 +959,3 @@ class RPCOverloadWrapper():
import_res = self.importdescriptors(req) import_res = self.importdescriptors(req)
if not import_res[0]['success']: if not import_res[0]['success']:
raise JSONRPCException(import_res[0]['error']) raise JSONRPCException(import_res[0]['error'])
def importaddress(self, address, *, label=None, rescan=None, p2sh=None):
wallet_info = self.getwalletinfo()
if 'descriptors' not in wallet_info or ('descriptors' in wallet_info and not wallet_info['descriptors']):
return self.__getattr__('importaddress')(address, label, rescan, p2sh)
is_hex = False
try:
int(address ,16)
is_hex = True
desc = descsum_create('raw(' + address + ')')
except Exception:
desc = descsum_create('addr(' + address + ')')
reqs = [{
'desc': desc,
'timestamp': 0 if rescan else 'now',
'label': label if label else '',
}]
if is_hex and p2sh:
reqs.append({
'desc': descsum_create('p2sh(raw(' + address + '))'),
'timestamp': 0 if rescan else 'now',
'label': label if label else '',
})
import_res = self.importdescriptors(reqs)
for res in import_res:
if not res['success']:
raise JSONRPCException(res['error'])

View File

@ -673,7 +673,8 @@ class WalletTest(BitcoinTestFramework):
self.generate(self.wallet, 1, sync_fun=self.no_op) self.generate(self.wallet, 1, sync_fun=self.no_op)
self.nodes[0].createwallet("watch_wallet", disable_private_keys=True) self.nodes[0].createwallet("watch_wallet", disable_private_keys=True)
watch_wallet = self.nodes[0].get_wallet_rpc("watch_wallet") watch_wallet = self.nodes[0].get_wallet_rpc("watch_wallet")
watch_wallet.importaddress(self.wallet.get_address()) import_res = watch_wallet.importdescriptors([{"desc": self.wallet.get_descriptor(), "timestamp": "now"}])
assert_equal(import_res[0]["success"], True)
# DEFAULT_ANCESTOR_LIMIT transactions off a confirmed tx should be fine # DEFAULT_ANCESTOR_LIMIT transactions off a confirmed tx should be fine
chain = self.wallet.create_self_transfer_chain(chain_length=DEFAULT_ANCESTOR_LIMIT) chain = self.wallet.create_self_transfer_chain(chain_length=DEFAULT_ANCESTOR_LIMIT)

View File

@ -90,7 +90,7 @@ class ImportPrunedFundsTest(BitcoinTestFramework):
# Import with affiliated address with no rescan # Import with affiliated address with no rescan
self.nodes[1].createwallet('wwatch', disable_private_keys=True) self.nodes[1].createwallet('wwatch', disable_private_keys=True)
wwatch = self.nodes[1].get_wallet_rpc('wwatch') wwatch = self.nodes[1].get_wallet_rpc('wwatch')
wwatch.importaddress(address=address2, rescan=False) wwatch.importdescriptors([{"desc": self.nodes[0].getaddressinfo(address2)["desc"], "timestamp": "now"}])
wwatch.importprunedfunds(rawtransaction=rawtxn2, txoutproof=proof2) wwatch.importprunedfunds(rawtransaction=rawtxn2, txoutproof=proof2)
assert [tx for tx in wwatch.listtransactions(include_watchonly=True) if tx['txid'] == txnid2] assert [tx for tx in wwatch.listtransactions(include_watchonly=True) if tx['txid'] == txnid2]

View File

@ -12,6 +12,7 @@ RPCs tested are:
from collections import defaultdict from collections import defaultdict
from test_framework.blocktools import COINBASE_MATURITY from test_framework.blocktools import COINBASE_MATURITY
from test_framework.descriptors import descsum_create
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error from test_framework.util import assert_equal, assert_raises_rpc_error
from test_framework.wallet_util import test_address from test_framework.wallet_util import test_address
@ -176,17 +177,17 @@ class WalletLabelsTest(BitcoinTestFramework):
} }
for l in BECH32_VALID: for l in BECH32_VALID:
ad = BECH32_VALID[l] ad = BECH32_VALID[l]
wallet_watch_only.importaddress(label=l, rescan=False, address=ad) import_res = wallet_watch_only.importdescriptors([{"desc": descsum_create(f"addr({ad})"), "timestamp": "now", "label": l}])
assert_equal(import_res[0]["success"], True)
self.generatetoaddress(node, 1, ad) self.generatetoaddress(node, 1, ad)
assert_equal(wallet_watch_only.getaddressesbylabel(label=l), {ad: {'purpose': 'receive'}}) assert_equal(wallet_watch_only.getaddressesbylabel(label=l), {ad: {'purpose': 'receive'}})
assert_equal(wallet_watch_only.getreceivedbylabel(label=l), 0) assert_equal(wallet_watch_only.getreceivedbylabel(label=l), 0)
for l in BECH32_INVALID: for l in BECH32_INVALID:
ad = BECH32_INVALID[l] ad = BECH32_INVALID[l]
assert_raises_rpc_error( import_res = wallet_watch_only.importdescriptors([{"desc": descsum_create(f"addr({ad})"), "timestamp": "now", "label": l}])
-5, assert_equal(import_res[0]["success"], False)
"Address is not valid", assert_equal(import_res[0]["error"]["code"], -5)
lambda: wallet_watch_only.importaddress(label=l, rescan=False, address=ad), assert_equal(import_res[0]["error"]["message"], "Address is not valid")
)
class Label: class Label:

View File

@ -46,10 +46,8 @@ class WalletReindexTest(BitcoinTestFramework):
# Blank wallets don't have a birth time # Blank wallets don't have a birth time
assert 'birthtime' not in wallet_watch_only.getwalletinfo() assert 'birthtime' not in wallet_watch_only.getwalletinfo()
# For a descriptors wallet: Import address with timestamp=now. # Import address with timestamp=now.
# For legacy wallet: There is no way of importing a script/address with a custom time. The wallet always imports it with birthtime=1. wallet_watch_only.importdescriptors([{"desc": miner_wallet.getaddressinfo(wallet_addr)["desc"], "timestamp": "now"}])
# In both cases, disable rescan to not detect the transaction.
wallet_watch_only.importaddress(wallet_addr, rescan=False)
assert_equal(len(wallet_watch_only.listtransactions()), 0) assert_equal(len(wallet_watch_only.listtransactions()), 0)
# Depending on the wallet type, the birth time changes. # Depending on the wallet type, the birth time changes.

View File

@ -50,15 +50,23 @@ class TransactionTimeRescanTest(BitcoinTestFramework):
# prepare the user wallet with 3 watch only addresses # prepare the user wallet with 3 watch only addresses
wo1 = usernode.getnewaddress() wo1 = usernode.getnewaddress()
wo1_desc = usernode.getaddressinfo(wo1)["desc"]
wo2 = usernode.getnewaddress() wo2 = usernode.getnewaddress()
wo2_desc = usernode.getaddressinfo(wo2)["desc"]
wo3 = usernode.getnewaddress() wo3 = usernode.getnewaddress()
wo3_desc = usernode.getaddressinfo(wo3)["desc"]
usernode.createwallet(wallet_name='wo', disable_private_keys=True) usernode.createwallet(wallet_name='wo', disable_private_keys=True)
wo_wallet = usernode.get_wallet_rpc('wo') wo_wallet = usernode.get_wallet_rpc('wo')
wo_wallet.importaddress(wo1) import_res = wo_wallet.importdescriptors(
wo_wallet.importaddress(wo2) [
wo_wallet.importaddress(wo3) {"desc": wo1_desc, "timestamp": "now"},
{"desc": wo2_desc, "timestamp": "now"},
{"desc": wo3_desc, "timestamp": "now"},
]
)
assert_equal(all([r["success"] for r in import_res]), True)
self.log.info('Start transactions') self.log.info('Start transactions')
@ -124,16 +132,20 @@ class TransactionTimeRescanTest(BitcoinTestFramework):
restorenode.createwallet(wallet_name='wo', disable_private_keys=True) restorenode.createwallet(wallet_name='wo', disable_private_keys=True)
restorewo_wallet = restorenode.get_wallet_rpc('wo') restorewo_wallet = restorenode.get_wallet_rpc('wo')
# for descriptor wallets, the test framework maps the importaddress RPC to the # importdescriptors with "timestamp": "now" always rescans
# importdescriptors RPC (with argument 'timestamp'='now'), which always rescans
# blocks of the past 2 hours, based on the current MTP timestamp; in order to avoid # blocks of the past 2 hours, based on the current MTP timestamp; in order to avoid
# importing the last address (wo3), we advance the time further and generate 10 blocks # importing the last address (wo3), we advance the time further and generate 10 blocks
set_node_times(self.nodes, cur_time + ten_days + ten_days + ten_days + ten_days) set_node_times(self.nodes, cur_time + ten_days + ten_days + ten_days + ten_days)
self.generatetoaddress(minernode, 10, m1) self.generatetoaddress(minernode, 10, m1)
restorewo_wallet.importaddress(wo1, rescan=False) import_res = restorewo_wallet.importdescriptors(
restorewo_wallet.importaddress(wo2, rescan=False) [
restorewo_wallet.importaddress(wo3, rescan=False) {"desc": wo1_desc, "timestamp": "now"},
{"desc": wo2_desc, "timestamp": "now"},
{"desc": wo3_desc, "timestamp": "now"},
]
)
assert_equal(all([r["success"] for r in import_res]), True)
# check user has 0 balance and no transactions # check user has 0 balance and no transactions
assert_equal(restorewo_wallet.getbalance(), 0) assert_equal(restorewo_wallet.getbalance(), 0)