wallet_transactiontime_rescan.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2018-present 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 transaction time during old block rescanning
6 """
7
8 import time
9
10 from test_framework.blocktools import COINBASE_MATURITY
11 from test_framework.test_framework import BitcoinTestFramework
12 from test_framework.util import (
13 assert_equal,
14 assert_raises_rpc_error,
15 set_node_times,
16 )
17
18
19 class TransactionTimeRescanTest(BitcoinTestFramework):
20 def set_test_params(self):
21 self.setup_clean_chain = False
22 self.num_nodes = 3
23 self.extra_args = [["-keypool=400"],
24 ["-keypool=400"],
25 []
26 ]
27
28 def skip_test_if_missing_module(self):
29 self.skip_if_no_wallet()
30
31 def run_test(self):
32 self.log.info('Prepare nodes and wallet')
33
34 minernode = self.nodes[0] # node used to mine BTC and create transactions
35 usernode = self.nodes[1] # user node with correct time
36 restorenode = self.nodes[2] # node used to restore user wallet and check time determination in ComputeSmartTime (wallet.cpp)
37
38 # time constant
39 cur_time = int(time.time())
40 ten_days = 10 * 24 * 60 * 60
41
42 # synchronize nodes and time
43 self.sync_all()
44 set_node_times(self.nodes, cur_time)
45
46 # prepare miner wallet
47 minernode.createwallet(wallet_name='default')
48 miner_wallet = minernode.get_wallet_rpc('default')
49 m1 = miner_wallet.getnewaddress()
50
51 # prepare the user wallet with 3 watch only addresses
52 wo1 = usernode.getnewaddress()
53 wo1_desc = usernode.getaddressinfo(wo1)["desc"]
54 wo2 = usernode.getnewaddress()
55 wo2_desc = usernode.getaddressinfo(wo2)["desc"]
56 wo3 = usernode.getnewaddress()
57 wo3_desc = usernode.getaddressinfo(wo3)["desc"]
58
59 usernode.createwallet(wallet_name='wo', disable_private_keys=True)
60 wo_wallet = usernode.get_wallet_rpc('wo')
61
62 import_res = wo_wallet.importdescriptors(
63 [
64 {"desc": wo1_desc, "timestamp": "now"},
65 {"desc": wo2_desc, "timestamp": "now"},
66 {"desc": wo3_desc, "timestamp": "now"},
67 ]
68 )
69 assert_equal(all([r["success"] for r in import_res]), True)
70
71 self.log.info('Start transactions')
72
73 # check blockcount
74 assert_equal(minernode.getblockcount(), 200)
75
76 # generate some btc to create transactions and check blockcount
77 initial_mine = COINBASE_MATURITY + 1
78 self.generatetoaddress(minernode, initial_mine, m1)
79 assert_equal(minernode.getblockcount(), initial_mine + 200)
80
81 # synchronize nodes and time
82 self.sync_all()
83 set_node_times(self.nodes, cur_time + ten_days)
84 # send 10 btc to user's first watch-only address
85 self.log.info('Send 10 btc to user')
86 miner_wallet.sendtoaddress(wo1, 10)
87
88 # generate blocks and check blockcount
89 self.generatetoaddress(minernode, COINBASE_MATURITY, m1)
90 assert_equal(minernode.getblockcount(), initial_mine + 300)
91
92 # synchronize nodes and time
93 self.sync_all()
94 set_node_times(self.nodes, cur_time + ten_days + ten_days)
95 # send 5 btc to our second watch-only address
96 self.log.info('Send 5 btc to user')
97 miner_wallet.sendtoaddress(wo2, 5)
98
99 # generate blocks and check blockcount
100 self.generatetoaddress(minernode, COINBASE_MATURITY, m1)
101 assert_equal(minernode.getblockcount(), initial_mine + 400)
102
103 # synchronize nodes and time
104 self.sync_all()
105 set_node_times(self.nodes, cur_time + ten_days + ten_days + ten_days)
106 # send 1 btc to our third watch-only address
107 self.log.info('Send 1 btc to user')
108 miner_wallet.sendtoaddress(wo3, 1)
109
110 # generate more blocks and check blockcount
111 self.generatetoaddress(minernode, COINBASE_MATURITY, m1)
112 assert_equal(minernode.getblockcount(), initial_mine + 500)
113
114 self.log.info('Check user\'s final balance and transaction count')
115 assert_equal(wo_wallet.getbalance(), 16)
116 assert_equal(len(wo_wallet.listtransactions()), 3)
117
118 self.log.info('Check transaction times')
119 for tx in wo_wallet.listtransactions():
120 if tx['address'] == wo1:
121 assert_equal(tx['blocktime'], cur_time + ten_days)
122 assert_equal(tx['time'], cur_time + ten_days)
123 elif tx['address'] == wo2:
124 assert_equal(tx['blocktime'], cur_time + ten_days + ten_days)
125 assert_equal(tx['time'], cur_time + ten_days + ten_days)
126 elif tx['address'] == wo3:
127 assert_equal(tx['blocktime'], cur_time + ten_days + ten_days + ten_days)
128 assert_equal(tx['time'], cur_time + ten_days + ten_days + ten_days)
129
130 # restore user wallet without rescan
131 self.log.info('Restore user wallet on another node without rescan')
132 restorenode.createwallet(wallet_name='wo', disable_private_keys=True)
133 restorewo_wallet = restorenode.get_wallet_rpc('wo')
134
135 # importdescriptors with "timestamp": "now" always rescans
136 # blocks of the past 2 hours, based on the current MTP timestamp; in order to avoid
137 # importing the last address (wo3), we advance the time further and generate 10 blocks
138 set_node_times(self.nodes, cur_time + ten_days + ten_days + ten_days + ten_days)
139 self.generatetoaddress(minernode, 10, m1)
140
141 import_res = restorewo_wallet.importdescriptors(
142 [
143 {"desc": wo1_desc, "timestamp": "now"},
144 {"desc": wo2_desc, "timestamp": "now"},
145 {"desc": wo3_desc, "timestamp": "now"},
146 ]
147 )
148 assert_equal(all([r["success"] for r in import_res]), True)
149
150 self.log.info('Testing abortrescan when no rescan is in progress')
151 assert_equal(restorewo_wallet.getwalletinfo()['scanning'], False)
152 assert_equal(restorewo_wallet.abortrescan(), False)
153
154 # check user has 0 balance and no transactions
155 assert_equal(restorewo_wallet.getbalance(), 0)
156 assert_equal(len(restorewo_wallet.listtransactions()), 0)
157
158 # proceed to rescan, first with an incomplete one, then with a full rescan
159 self.log.info('Rescan last history part')
160 restorewo_wallet.rescanblockchain(initial_mine + 350)
161 self.log.info('Rescan all history')
162 restorewo_wallet.rescanblockchain()
163
164 self.log.info('Check user\'s final balance and transaction count after restoration')
165 assert_equal(restorewo_wallet.getbalance(), 16)
166 assert_equal(len(restorewo_wallet.listtransactions()), 3)
167
168 self.log.info('Check transaction times after restoration')
169 for tx in restorewo_wallet.listtransactions():
170 if tx['address'] == wo1:
171 assert_equal(tx['blocktime'], cur_time + ten_days)
172 assert_equal(tx['time'], cur_time + ten_days)
173 elif tx['address'] == wo2:
174 assert_equal(tx['blocktime'], cur_time + ten_days + ten_days)
175 assert_equal(tx['time'], cur_time + ten_days + ten_days)
176 elif tx['address'] == wo3:
177 assert_equal(tx['blocktime'], cur_time + ten_days + ten_days + ten_days)
178 assert_equal(tx['time'], cur_time + ten_days + ten_days + ten_days)
179
180
181 self.log.info('Test handling of invalid parameters for rescanblockchain')
182 assert_raises_rpc_error(-8, "Invalid start_height", restorewo_wallet.rescanblockchain, -1, 10)
183 assert_raises_rpc_error(-8, "Invalid stop_height", restorewo_wallet.rescanblockchain, 1, -1)
184 assert_raises_rpc_error(-8, "stop_height must be greater than start_height", restorewo_wallet.rescanblockchain, 20, 10)
185
186 self.log.info("Test `rescanblockchain` fails when wallet is encrypted and locked")
187 usernode.createwallet(wallet_name="enc_wallet", passphrase="passphrase")
188 enc_wallet = usernode.get_wallet_rpc("enc_wallet")
189 assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", enc_wallet.rescanblockchain)
190
191
192 if __name__ == '__main__':
193 TransactionTimeRescanTest(__file__).main()
194