|
| 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() |
0 commit comments