Skip to content

Commit e7796a5

Browse files
Eunovorkrux
andcommitted
test/wallet: ensure FastWalletRescanFilter is updated during scanning
The fixed non-range descriptor address ensured that the FastWalletRescanFilter would match all Blocks even if the filter wasn't properly updated. This commit moves the non-range descriptor tx to a different block, so that the filters must be updated after each TopUp for the test to pass. Co-authored-by: rkrux <rkrux.connect@gmail.com>
1 parent 4035231 commit e7796a5

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

test/functional/wallet_fast_rescan.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
KEYPOOL_SIZE = 100 # smaller than default size to speed-up test
17-
NUM_DESCRIPTORS = 9 # number of descriptors (8 default ranged ones + 1 fixed non-ranged one)
1817
NUM_BLOCKS = 6 # number of blocks to mine
1918

2019

@@ -33,41 +32,55 @@ def get_wallet_txids(self, node: TestNode, wallet_name: str) -> list[str]:
3332

3433
def run_test(self):
3534
node = self.nodes[0]
36-
wallet = MiniWallet(node)
35+
funding_wallet = MiniWallet(node)
3736

3837
self.log.info("Create descriptor wallet with backup")
3938
WALLET_BACKUP_FILENAME = node.datadir_path / 'wallet.bak'
4039
node.createwallet(wallet_name='topup_test')
4140
w = node.get_wallet_rpc('topup_test')
4241
fixed_key = get_generate_key()
43-
print(w.importdescriptors([{"desc": descsum_create(f"wpkh({fixed_key.privkey})"), "timestamp": "now"}]))
42+
w.importdescriptors([{"desc": descsum_create(f"wpkh({fixed_key.privkey})"), "timestamp": "now"}])
4443
descriptors = w.listdescriptors()['descriptors']
45-
assert_equal(len(descriptors), NUM_DESCRIPTORS)
4644
w.backupwallet(WALLET_BACKUP_FILENAME)
4745

46+
num_txs = 0
47+
48+
fast_rescan_messages = []
49+
def append_fast_rescan_message():
50+
chain_info = self.nodes[0].getblockchaininfo()
51+
fast_rescan_messages.append(f"Fast rescan: inspect block {chain_info['blocks']} [{chain_info['bestblockhash']}] (filter matched)")
52+
53+
self.log.info("Create tx sending to non-ranged descriptors")
54+
self.log.info(f"Block 1/{NUM_BLOCKS}")
55+
spk = bytes.fromhex(fixed_key.p2wpkh_script)
56+
self.log.info(f"-> fixed non-range descriptor address {fixed_key.p2wpkh_addr}")
57+
funding_wallet.send_to(from_node=node, scriptPubKey=spk, amount=10000)
58+
num_txs += 1
59+
self.generate(node, 1)
60+
append_fast_rescan_message()
61+
4862
self.log.info("Create txs sending to end range address of each descriptor, triggering top-ups")
49-
for i in range(NUM_BLOCKS):
63+
for i in range(1, NUM_BLOCKS):
5064
self.log.info(f"Block {i+1}/{NUM_BLOCKS}")
51-
for desc_info in w.listdescriptors()['descriptors']:
52-
if 'range' in desc_info:
53-
start_range, end_range = desc_info['range']
54-
addr = w.deriveaddresses(desc_info['desc'], [end_range, end_range])[0]
55-
spk = address_to_scriptpubkey(addr)
56-
self.log.info(f"-> range [{start_range},{end_range}], last address {addr}")
57-
else:
58-
spk = bytes.fromhex(fixed_key.p2wpkh_script)
59-
self.log.info(f"-> fixed non-range descriptor address {fixed_key.p2wpkh_addr}")
60-
wallet.send_to(from_node=node, scriptPubKey=spk, amount=10000)
65+
ranged_descs = [desc for desc in descriptors if 'range' in desc]
66+
for desc_info in ranged_descs:
67+
start_range, end_range = desc_info['range']
68+
addr = w.deriveaddresses(desc_info['desc'], [end_range, end_range])[0]
69+
spk = address_to_scriptpubkey(addr)
70+
self.log.info(f"-> range [{start_range},{end_range}], last address {addr}")
71+
funding_wallet.send_to(from_node=node, scriptPubKey=spk, amount=10000)
72+
num_txs += 1
6173
self.generate(node, 1)
74+
append_fast_rescan_message()
6275

6376
self.log.info("Import wallet backup with block filter index")
64-
with node.assert_debug_log(['fast variant using block filters']):
77+
with node.assert_debug_log(['fast variant using block filters', *fast_rescan_messages]):
6578
node.restorewallet('rescan_fast', WALLET_BACKUP_FILENAME)
6679
txids_fast = self.get_wallet_txids(node, 'rescan_fast')
6780

6881
self.log.info("Import non-active descriptors with block filter index")
6982
node.createwallet(wallet_name='rescan_fast_nonactive', disable_private_keys=True, blank=True)
70-
with node.assert_debug_log(['fast variant using block filters']):
83+
with node.assert_debug_log(['fast variant using block filters', *fast_rescan_messages]):
7184
w = node.get_wallet_rpc('rescan_fast_nonactive')
7285
w.importdescriptors([{"desc": descriptor['desc'], "timestamp": 0} for descriptor in descriptors])
7386
txids_fast_nonactive = self.get_wallet_txids(node, 'rescan_fast_nonactive')
@@ -86,10 +99,10 @@ def run_test(self):
8699
txids_slow_nonactive = self.get_wallet_txids(node, 'rescan_slow_nonactive')
87100

88101
self.log.info("Verify that all rescans found the same txs in slow and fast variants")
89-
assert_equal(len(txids_slow), NUM_DESCRIPTORS * NUM_BLOCKS)
90-
assert_equal(len(txids_fast), NUM_DESCRIPTORS * NUM_BLOCKS)
91-
assert_equal(len(txids_slow_nonactive), NUM_DESCRIPTORS * NUM_BLOCKS)
92-
assert_equal(len(txids_fast_nonactive), NUM_DESCRIPTORS * NUM_BLOCKS)
102+
assert_equal(len(txids_slow), num_txs)
103+
assert_equal(len(txids_fast), num_txs)
104+
assert_equal(len(txids_slow_nonactive), num_txs)
105+
assert_equal(len(txids_fast_nonactive), num_txs)
93106
assert_equal(sorted(txids_slow), sorted(txids_fast))
94107
assert_equal(sorted(txids_slow_nonactive), sorted(txids_fast_nonactive))
95108

0 commit comments

Comments
 (0)