Skip to content

Commit e0a7e19

Browse files
committed
Merge #10225: [test] Add aborttrescan tests
ed60970 [test] Test abortrescan command. (Karl-Johan Alm) Tree-SHA512: 7f617adba65a6df8fdc4b01432992926a06c4a05da4e657653436f7716301fa5d6249d77894a097737e7fb9e118925883f2425c639058b8973680339bb8e61b6
2 parents c29a0d4 + ed60970 commit e0a7e19

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test wallet import RPCs.
6+
7+
Test rescan behavior of importprivkey when aborted. The test ensures that:
8+
1. The abortrescan command indeed stops the rescan process.
9+
2. Subsequent rescan catches the aborted address UTXO
10+
"""
11+
12+
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import (assert_equal, get_rpc_proxy)
14+
from decimal import Decimal
15+
import threading # for bg importprivkey
16+
import time # for sleep
17+
18+
class ImportAbortRescanTest(BitcoinTestFramework):
19+
def __init__(self):
20+
super().__init__()
21+
self.setup_clean_chain = True
22+
23+
def run_test(self):
24+
# Generate for BTC
25+
assert_equal(self.nodes[0].getbalance(), 0)
26+
assert_equal(self.nodes[1].getbalance(), 0)
27+
self.nodes[0].generate(300)
28+
assert_equal(self.nodes[1].getbalance(), 0)
29+
# Make blocks with spam to cause rescan delay
30+
for i in range(5):
31+
for j in range(5):
32+
self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 0.1)
33+
self.nodes[0].generate(10)
34+
addr = self.nodes[0].getnewaddress()
35+
privkey = self.nodes[0].dumpprivkey(addr)
36+
self.nodes[0].sendtoaddress(addr, 0.123)
37+
self.nodes[0].generate(10) # mature tx
38+
self.sync_all()
39+
40+
# Import this address in the background ...
41+
node1ref = get_rpc_proxy(self.nodes[1].url, 1, timeout=600)
42+
importthread = threading.Thread(target=node1ref.importprivkey, args=[privkey])
43+
importthread.start()
44+
# ... then abort rescan; try a bunch until abortres becomes true,
45+
# because we will start checking before above thread starts processing
46+
for i in range(2000):
47+
time.sleep(0.001)
48+
abortres = self.nodes[1].abortrescan()
49+
if abortres: break
50+
assert abortres # if false, we failed to abort
51+
# import should die soon
52+
for i in range(10):
53+
time.sleep(0.1)
54+
deadres = not importthread.isAlive()
55+
if deadres: break
56+
57+
assert deadres # if false, importthread did not die soon enough
58+
assert_equal(self.nodes[1].getbalance(), 0.0)
59+
60+
# Import a different address and let it run
61+
self.nodes[1].importprivkey(self.nodes[0].dumpprivkey(self.nodes[0].getnewaddress()))
62+
# Expect original privkey to now also be discovered and added to balance
63+
assert_equal(self.nodes[1].getbalance(), Decimal("0.123"))
64+
65+
if __name__ == "__main__":
66+
ImportAbortRescanTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
'rpcnamedargs.py',
110110
'listsinceblock.py',
111111
'p2p-leaktests.py',
112+
'import-abort-rescan.py',
112113
]
113114

114115
EXTENDED_SCRIPTS = [

0 commit comments

Comments
 (0)