Skip to content

Commit db03248

Browse files
committed
Merge bitcoin/bitcoin#27199: test: fix race condition in encrypted wallet rescan tests
dbeca79 test: fix race condition in encrypted wallet rescan tests (ishaanam) Pull request description: This fixes bitcoin/bitcoin#26347 (comment) ACKs for top commit: MarcoFalke: nice re-ACK dbeca79 🚜 achow101: ACK dbeca79 Tree-SHA512: 7127254ac0274b5bc8ba0242736e77464acbf1f6e3f6af098b4e47742124c336cd67dffdb385e1e8dbd3a8ae74acd073c99e82fa35c44a615fd7d22b29a0daf7
2 parents 09e86d7 + dbeca79 commit db03248

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

test/functional/wallet_importdescriptors.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
- `test_address()` is called to call getaddressinfo for an address on node1
1616
and test the values returned."""
1717

18+
import threading
19+
20+
from test_framework.authproxy import JSONRPCException
1821
from test_framework.blocktools import COINBASE_MATURITY
1922
from test_framework.test_framework import BitcoinTestFramework
2023
from test_framework.descriptors import descsum_create
@@ -687,11 +690,26 @@ def run_test(self):
687690
descriptor["timestamp"] = 0
688691
descriptor["next_index"] = 0
689692

690-
batch = []
691-
batch.append(encrypted_wallet.walletpassphrase.get_request("passphrase", 3))
692-
batch.append(encrypted_wallet.importdescriptors.get_request([descriptor]))
693+
encrypted_wallet.walletpassphrase("passphrase", 99999)
694+
t = threading.Thread(target=encrypted_wallet.importdescriptors, args=([descriptor],))
695+
696+
with self.nodes[0].assert_debug_log(expected_msgs=[f'Rescan started from block 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206... (slow variant inspecting all blocks)'], timeout=5):
697+
t.start()
698+
699+
# Set the passphrase timeout to 1 to test that the wallet remains unlocked during the rescan
700+
self.nodes[0].cli("-rpcwallet=encrypted_wallet").walletpassphrase("passphrase", 1)
701+
702+
try:
703+
self.nodes[0].cli("-rpcwallet=encrypted_wallet").walletlock()
704+
except JSONRPCException as e:
705+
assert e.error['code'] == -4 and "Error: the wallet is currently being used to rescan the blockchain for related transactions. Please call `abortrescan` before locking the wallet." in e.error['message']
706+
707+
try:
708+
self.nodes[0].cli("-rpcwallet=encrypted_wallet").walletpassphrasechange("passphrase", "newpassphrase")
709+
except JSONRPCException as e:
710+
assert e.error['code'] == -4 and "Error: the wallet is currently being used to rescan the blockchain for related transactions. Please call `abortrescan` before changing the passphrase." in e.error['message']
693711

694-
encrypted_wallet.batch(batch)
712+
t.join()
695713

696714
assert_equal(temp_wallet.getbalance(), encrypted_wallet.getbalance())
697715

test/functional/wallet_transactiontime_rescan.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"""Test transaction time during old block rescanning
66
"""
77

8+
import threading
89
import time
910

11+
from test_framework.authproxy import JSONRPCException
1012
from test_framework.blocktools import COINBASE_MATURITY
1113
from test_framework.test_framework import BitcoinTestFramework
1214
from test_framework.util import (
@@ -196,14 +198,28 @@ def run_test(self):
196198
minernode.createwallet("encrypted_wallet", blank=True, passphrase="passphrase", descriptors=False)
197199
encrypted_wallet = minernode.get_wallet_rpc("encrypted_wallet")
198200

199-
encrypted_wallet.walletpassphrase("passphrase", 1)
201+
encrypted_wallet.walletpassphrase("passphrase", 99999)
200202
encrypted_wallet.sethdseed(seed=hd_seed)
201203

202-
batch = []
203-
batch.append(encrypted_wallet.walletpassphrase.get_request("passphrase", 3))
204-
batch.append(encrypted_wallet.rescanblockchain.get_request())
204+
t = threading.Thread(target=encrypted_wallet.rescanblockchain)
205205

206-
encrypted_wallet.batch(batch)
206+
with minernode.assert_debug_log(expected_msgs=[f'Rescan started from block 0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206... (slow variant inspecting all blocks)'], timeout=5):
207+
t.start()
208+
209+
# set the passphrase timeout to 1 to test that the wallet remains unlocked during the rescan
210+
minernode.cli("-rpcwallet=encrypted_wallet").walletpassphrase("passphrase", 1)
211+
212+
try:
213+
minernode.cli("-rpcwallet=encrypted_wallet").walletlock()
214+
except JSONRPCException as e:
215+
assert e.error['code'] == -4 and "Error: the wallet is currently being used to rescan the blockchain for related transactions. Please call `abortrescan` before locking the wallet." in e.error['message']
216+
217+
try:
218+
minernode.cli("-rpcwallet=encrypted_wallet").walletpassphrasechange("passphrase", "newpassphrase")
219+
except JSONRPCException as e:
220+
assert e.error['code'] == -4 and "Error: the wallet is currently being used to rescan the blockchain for related transactions. Please call `abortrescan` before changing the passphrase." in e.error['message']
221+
222+
t.join()
207223

208224
assert_equal(encrypted_wallet.getbalance(), temp_wallet.getbalance())
209225

0 commit comments

Comments
 (0)