1#!/usr/bin/env python3
2# Copyright (c) 2014-2018 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 RPCs related to blockchainstate.
6
7Test the following RPCs:
8    - getblockchaininfo
9    - gettxoutsetinfo
10    - getdifficulty
11    - getbestblockhash
12    - getblockhash
13    - getblockheader
14    - getchaintxstats
15    - getnetworkhashps
16    - verifychain
17
18Tests correspond to code in rpc/blockchain.cpp.
19"""
20
21from decimal import Decimal
22import http.client
23import subprocess
24
25from test_framework.test_framework import BitcoinTestFramework
26from test_framework.util import (
27    assert_equal,
28    assert_greater_than,
29    assert_greater_than_or_equal,
30    assert_raises,
31    assert_raises_rpc_error,
32    assert_is_hex_string,
33    assert_is_hash_string,
34)
35from test_framework.blocktools import (
36    create_block,
37    create_coinbase,
38    TIME_GENESIS_BLOCK,
39)
40from test_framework.messages import (
41    msg_block,
42)
43from test_framework.mininode import (
44    P2PInterface,
45)
46
47
48class BlockchainTest(BitcoinTestFramework):
49    def set_test_params(self):
50        self.setup_clean_chain = True
51        self.num_nodes = 1
52
53    def run_test(self):
54        self.mine_chain()
55        self.restart_node(0, extra_args=['-stopatheight=207', '-prune=1'])  # Set extra args with pruning after rescan is complete
56
57        self._test_getblockchaininfo()
58        self._test_getchaintxstats()
59        self._test_gettxoutsetinfo()
60        self._test_getblockheader()
61        self._test_getdifficulty()
62        self._test_getnetworkhashps()
63        self._test_stopatheight()
64        self._test_waitforblockheight()
65        assert self.nodes[0].verifychain(4, 0)
66
67    def mine_chain(self):
68        self.log.info('Create some old blocks')
69        address = self.nodes[0].get_deterministic_priv_key().address
70        for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
71            # ten-minute steps from genesis block time
72            self.nodes[0].setmocktime(t)
73            self.nodes[0].generatetoaddress(1, address)
74        assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200)
75
76    def _test_getblockchaininfo(self):
77        self.log.info("Test getblockchaininfo")
78
79        keys = [
80            'bestblockhash',
81            'bip9_softforks',
82            'blocks',
83            'chain',
84            'chainwork',
85            'difficulty',
86            'headers',
87            'initialblockdownload',
88            'mediantime',
89            'pruned',
90            'size_on_disk',
91            'softforks',
92            'verificationprogress',
93            'warnings',
94        ]
95        res = self.nodes[0].getblockchaininfo()
96
97        # result should have these additional pruning keys if manual pruning is enabled
98        assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning'] + keys))
99
100        # size_on_disk should be > 0
101        assert_greater_than(res['size_on_disk'], 0)
102
103        # pruneheight should be greater or equal to 0
104        assert_greater_than_or_equal(res['pruneheight'], 0)
105
106        # check other pruning fields given that prune=1
107        assert res['pruned']
108        assert not res['automatic_pruning']
109
110        self.restart_node(0, ['-stopatheight=207'])
111        res = self.nodes[0].getblockchaininfo()
112        # should have exact keys
113        assert_equal(sorted(res.keys()), keys)
114
115        self.restart_node(0, ['-stopatheight=207', '-prune=550'])
116        res = self.nodes[0].getblockchaininfo()
117        # result should have these additional pruning keys if prune=550
118        assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning', 'prune_target_size'] + keys))
119
120        # check related fields
121        assert res['pruned']
122        assert_equal(res['pruneheight'], 0)
123        assert res['automatic_pruning']
124        assert_equal(res['prune_target_size'], 576716800)
125        assert_greater_than(res['size_on_disk'], 0)
126
127    def _test_getchaintxstats(self):
128        self.log.info("Test getchaintxstats")
129
130        # Test `getchaintxstats` invalid extra parameters
131        assert_raises_rpc_error(-1, 'getchaintxstats', self.nodes[0].getchaintxstats, 0, '', 0)
132
133        # Test `getchaintxstats` invalid `nblocks`
134        assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self.nodes[0].getchaintxstats, '')
135        assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, -1)
136        assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, self.nodes[0].getblockcount())
137
138        # Test `getchaintxstats` invalid `blockhash`
139        assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].getchaintxstats, blockhash=0)
140        assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 1, for '0')", self.nodes[0].getchaintxstats, blockhash='0')
141        assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].getchaintxstats, blockhash='ZZZ0000000000000000000000000000000000000000000000000000000000000')
142        assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getchaintxstats, blockhash='0000000000000000000000000000000000000000000000000000000000000000')
143        blockhash = self.nodes[0].getblockhash(200)
144        self.nodes[0].invalidateblock(blockhash)
145        assert_raises_rpc_error(-8, "Block is not in main chain", self.nodes[0].getchaintxstats, blockhash=blockhash)
146        self.nodes[0].reconsiderblock(blockhash)
147
148        chaintxstats = self.nodes[0].getchaintxstats(nblocks=1)
149        # 200 txs plus genesis tx
150        assert_equal(chaintxstats['txcount'], 201)
151        # tx rate should be 1 per 10 minutes, or 1/600
152        # we have to round because of binary math
153        assert_equal(round(chaintxstats['txrate'] * 600, 10), Decimal(1))
154
155        b1_hash = self.nodes[0].getblockhash(1)
156        b1 = self.nodes[0].getblock(b1_hash)
157        b200_hash = self.nodes[0].getblockhash(200)
158        b200 = self.nodes[0].getblock(b200_hash)
159        time_diff = b200['mediantime'] - b1['mediantime']
160
161        chaintxstats = self.nodes[0].getchaintxstats()
162        assert_equal(chaintxstats['time'], b200['time'])
163        assert_equal(chaintxstats['txcount'], 201)
164        assert_equal(chaintxstats['window_final_block_hash'], b200_hash)
165        assert_equal(chaintxstats['window_block_count'], 199)
166        assert_equal(chaintxstats['window_tx_count'], 199)
167        assert_equal(chaintxstats['window_interval'], time_diff)
168        assert_equal(round(chaintxstats['txrate'] * time_diff, 10), Decimal(199))
169
170        chaintxstats = self.nodes[0].getchaintxstats(blockhash=b1_hash)
171        assert_equal(chaintxstats['time'], b1['time'])
172        assert_equal(chaintxstats['txcount'], 2)
173        assert_equal(chaintxstats['window_final_block_hash'], b1_hash)
174        assert_equal(chaintxstats['window_block_count'], 0)
175        assert('window_tx_count' not in chaintxstats)
176        assert('window_interval' not in chaintxstats)
177        assert('txrate' not in chaintxstats)
178
179    def _test_gettxoutsetinfo(self):
180        node = self.nodes[0]
181        res = node.gettxoutsetinfo()
182
183        assert_equal(res['total_amount'], Decimal('8725.00000000'))
184        assert_equal(res['transactions'], 200)
185        assert_equal(res['height'], 200)
186        assert_equal(res['txouts'], 200)
187        assert_equal(res['bogosize'], 15000),
188        assert_equal(res['bestblock'], node.getblockhash(200))
189        size = res['disk_size']
190        assert size > 6400
191        assert size < 64000
192        assert_equal(len(res['bestblock']), 64)
193        assert_equal(len(res['hash_serialized_2']), 64)
194
195        self.log.info("Test that gettxoutsetinfo() works for blockchain with just the genesis block")
196        b1hash = node.getblockhash(1)
197        node.invalidateblock(b1hash)
198
199        res2 = node.gettxoutsetinfo()
200        assert_equal(res2['transactions'], 0)
201        assert_equal(res2['total_amount'], Decimal('0'))
202        assert_equal(res2['height'], 0)
203        assert_equal(res2['txouts'], 0)
204        assert_equal(res2['bogosize'], 0),
205        assert_equal(res2['bestblock'], node.getblockhash(0))
206        assert_equal(len(res2['hash_serialized_2']), 64)
207
208        self.log.info("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block")
209        node.reconsiderblock(b1hash)
210
211        res3 = node.gettxoutsetinfo()
212        # The field 'disk_size' is non-deterministic and can thus not be
213        # compared between res and res3.  Everything else should be the same.
214        del res['disk_size'], res3['disk_size']
215        assert_equal(res, res3)
216
217    def _test_getblockheader(self):
218        node = self.nodes[0]
219
220        assert_raises_rpc_error(-8, "hash must be of length 64 (not 8, for 'nonsense')", node.getblockheader, "nonsense")
221        assert_raises_rpc_error(-8, "hash must be hexadecimal string (not 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844')", node.getblockheader, "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
222        assert_raises_rpc_error(-5, "Block not found", node.getblockheader, "0cf7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
223
224        besthash = node.getbestblockhash()
225        secondbesthash = node.getblockhash(199)
226        header = node.getblockheader(blockhash=besthash)
227
228        assert_equal(header['hash'], besthash)
229        assert_equal(header['height'], 200)
230        assert_equal(header['confirmations'], 1)
231        assert_equal(header['previousblockhash'], secondbesthash)
232        assert_is_hex_string(header['chainwork'])
233        assert_equal(header['nTx'], 1)
234        assert_is_hash_string(header['hash'])
235        assert_is_hash_string(header['previousblockhash'])
236        assert_is_hash_string(header['merkleroot'])
237        assert_is_hash_string(header['bits'], length=None)
238        assert isinstance(header['time'], int)
239        assert isinstance(header['mediantime'], int)
240        assert isinstance(header['nonce'], int)
241        assert isinstance(header['version'], int)
242        assert isinstance(int(header['versionHex'], 16), int)
243        assert isinstance(header['difficulty'], Decimal)
244
245    def _test_getdifficulty(self):
246        difficulty = self.nodes[0].getdifficulty()
247        # 1 hash in 2 should be valid, so difficulty should be 1/2**31
248        # binary => decimal => binary math is why we do this check
249        assert abs(difficulty * 2**31 - 1) < 0.0001
250
251    def _test_getnetworkhashps(self):
252        hashes_per_second = self.nodes[0].getnetworkhashps()
253        # This should be 2 hashes every 10 minutes or 1/300
254        assert abs(hashes_per_second * 300 - 1) < 0.0001
255
256    def _test_stopatheight(self):
257        assert_equal(self.nodes[0].getblockcount(), 200)
258        self.nodes[0].generatetoaddress(6, self.nodes[0].get_deterministic_priv_key().address)
259        assert_equal(self.nodes[0].getblockcount(), 206)
260        self.log.debug('Node should not stop at this height')
261        assert_raises(subprocess.TimeoutExpired, lambda: self.nodes[0].process.wait(timeout=3))
262        try:
263            self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)
264        except (ConnectionError, http.client.BadStatusLine):
265            pass  # The node already shut down before response
266        self.log.debug('Node should stop at this height...')
267        self.nodes[0].wait_until_stopped()
268        self.start_node(0)
269        assert_equal(self.nodes[0].getblockcount(), 207)
270
271    def _test_waitforblockheight(self):
272        self.log.info("Test waitforblockheight")
273        node = self.nodes[0]
274        node.add_p2p_connection(P2PInterface())
275
276        current_height = node.getblock(node.getbestblockhash())['height']
277
278        # Create a fork somewhere below our current height, invalidate the tip
279        # of that fork, and then ensure that waitforblockheight still
280        # works as expected.
281        #
282        # (Previously this was broken based on setting
283        # `rpc/blockchain.cpp:latestblock` incorrectly.)
284        #
285        b20hash = node.getblockhash(20)
286        b20 = node.getblock(b20hash)
287
288        def solve_and_send_block(prevhash, height, time):
289            b = create_block(prevhash, create_coinbase(height), time)
290            b.nVersion = 0x20000000
291            b.solve()
292            node.p2p.send_message(msg_block(b))
293            node.p2p.sync_with_ping()
294            return b
295
296        b21f = solve_and_send_block(int(b20hash, 16), 21, b20['time'] + 1)
297        b22f = solve_and_send_block(b21f.sha256, 22, b21f.nTime + 1)
298
299        node.invalidateblock(b22f.hash)
300
301        def assert_waitforheight(height, timeout=2):
302            assert_equal(
303                node.waitforblockheight(height=height, timeout=timeout)['height'],
304                current_height)
305
306        assert_waitforheight(0)
307        assert_waitforheight(current_height - 1)
308        assert_waitforheight(current_height)
309        assert_waitforheight(current_height + 1)
310
311
312if __name__ == '__main__':
313    BlockchainTest().main()
314