1#!/usr/bin/env python3
2# Copyright (c) 2014-2020 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.blocktools import (
26    create_block,
27    create_coinbase,
28    TIME_GENESIS_BLOCK,
29)
30from test_framework.messages import (
31    CBlockHeader,
32    FromHex,
33    msg_block,
34)
35from test_framework.p2p import P2PInterface
36from test_framework.test_framework import BitcoinTestFramework
37from test_framework.util import (
38    assert_equal,
39    assert_greater_than,
40    assert_greater_than_or_equal,
41    assert_raises,
42    assert_raises_rpc_error,
43    assert_is_hex_string,
44    assert_is_hash_string,
45)
46
47
48class BlockchainTest(BitcoinTestFramework):
49    def set_test_params(self):
50        self.setup_clean_chain = True
51        self.num_nodes = 1
52        self.supports_cli = False
53
54    def run_test(self):
55        self.mine_chain()
56        self.restart_node(0, extra_args=['-stopatheight=207', '-prune=1'])  # Set extra args with pruning after rescan is complete
57
58        self._test_getblockchaininfo()
59        self._test_getchaintxstats()
60        self._test_gettxoutsetinfo()
61        self._test_getblockheader()
62        self._test_getdifficulty()
63        self._test_getnetworkhashps()
64        self._test_stopatheight()
65        self._test_waitforblockheight()
66        assert self.nodes[0].verifychain(4, 0)
67
68    def mine_chain(self):
69        self.log.info('Create some old blocks')
70        address = self.nodes[0].get_deterministic_priv_key().address
71        for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
72            # ten-minute steps from genesis block time
73            self.nodes[0].setmocktime(t)
74            self.nodes[0].generatetoaddress(1, address)
75        assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200)
76
77    def _test_getblockchaininfo(self):
78        self.log.info("Test getblockchaininfo")
79
80        keys = [
81            'bestblockhash',
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        assert_equal(res['softforks'], {
128            'bip16': {'type': 'buried', 'active': True, 'height': 0},
129            'bip34': {'type': 'buried', 'active': False, 'height': 500},
130            'bip66': {'type': 'buried', 'active': False, 'height': 1251},
131            'bip65': {'type': 'buried', 'active': False, 'height': 1351},
132            'csv': {'type': 'buried', 'active': False, 'height': 432},
133            'segwit': {'type': 'buried', 'active': True, 'height': 0},
134        })
135
136    def _test_getchaintxstats(self):
137        self.log.info("Test getchaintxstats")
138
139        # Test `getchaintxstats` invalid extra parameters
140        assert_raises_rpc_error(-1, 'getchaintxstats', self.nodes[0].getchaintxstats, 0, '', 0)
141
142        # Test `getchaintxstats` invalid `nblocks`
143        assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self.nodes[0].getchaintxstats, '')
144        assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, -1)
145        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())
146
147        # Test `getchaintxstats` invalid `blockhash`
148        assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].getchaintxstats, blockhash=0)
149        assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 1, for '0')", self.nodes[0].getchaintxstats, blockhash='0')
150        assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].getchaintxstats, blockhash='ZZZ0000000000000000000000000000000000000000000000000000000000000')
151        assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getchaintxstats, blockhash='0000000000000000000000000000000000000000000000000000000000000000')
152        blockhash = self.nodes[0].getblockhash(200)
153        self.nodes[0].invalidateblock(blockhash)
154        assert_raises_rpc_error(-8, "Block is not in main chain", self.nodes[0].getchaintxstats, blockhash=blockhash)
155        self.nodes[0].reconsiderblock(blockhash)
156
157        chaintxstats = self.nodes[0].getchaintxstats(nblocks=1)
158        # 200 txs plus genesis tx
159        assert_equal(chaintxstats['txcount'], 201)
160        # tx rate should be 1 per 10 minutes, or 1/600
161        # we have to round because of binary math
162        assert_equal(round(chaintxstats['txrate'] * 600, 10), Decimal(1))
163
164        b1_hash = self.nodes[0].getblockhash(1)
165        b1 = self.nodes[0].getblock(b1_hash)
166        b200_hash = self.nodes[0].getblockhash(200)
167        b200 = self.nodes[0].getblock(b200_hash)
168        time_diff = b200['mediantime'] - b1['mediantime']
169
170        chaintxstats = self.nodes[0].getchaintxstats()
171        assert_equal(chaintxstats['time'], b200['time'])
172        assert_equal(chaintxstats['txcount'], 201)
173        assert_equal(chaintxstats['window_final_block_hash'], b200_hash)
174        assert_equal(chaintxstats['window_final_block_height'], 200)
175        assert_equal(chaintxstats['window_block_count'], 199)
176        assert_equal(chaintxstats['window_tx_count'], 199)
177        assert_equal(chaintxstats['window_interval'], time_diff)
178        assert_equal(round(chaintxstats['txrate'] * time_diff, 10), Decimal(199))
179
180        chaintxstats = self.nodes[0].getchaintxstats(blockhash=b1_hash)
181        assert_equal(chaintxstats['time'], b1['time'])
182        assert_equal(chaintxstats['txcount'], 2)
183        assert_equal(chaintxstats['window_final_block_hash'], b1_hash)
184        assert_equal(chaintxstats['window_final_block_height'], 1)
185        assert_equal(chaintxstats['window_block_count'], 0)
186        assert 'window_tx_count' not in chaintxstats
187        assert 'window_interval' not in chaintxstats
188        assert 'txrate' not in chaintxstats
189
190    def _test_gettxoutsetinfo(self):
191        node = self.nodes[0]
192        res = node.gettxoutsetinfo()
193
194        assert_equal(res['amount']['total'], Decimal('8725.00000000'))
195        assert_equal(res['transactions'], 200)
196        assert_equal(res['height'], 200)
197        assert_equal(res['txouts'], 200)
198        assert_equal(res['bogosize'], 15000),
199        assert_equal(res['bestblock'], node.getblockhash(200))
200        size = res['disk_size']
201        assert size > 6400
202        assert size < 64000
203        assert_equal(len(res['bestblock']), 64)
204        assert_equal(len(res['hash_serialized_2']), 64)
205
206        self.log.info("Test that gettxoutsetinfo() works for blockchain with just the genesis block")
207        b1hash = node.getblockhash(1)
208        node.invalidateblock(b1hash)
209
210        res2 = node.gettxoutsetinfo()
211        assert_equal(res2['transactions'], 0)
212        assert_equal(res2['amount']['total'], Decimal('0'))
213        assert_equal(res2['height'], 0)
214        assert_equal(res2['txouts'], 0)
215        assert_equal(res2['bogosize'], 0),
216        assert_equal(res2['bestblock'], node.getblockhash(0))
217        assert_equal(len(res2['hash_serialized_2']), 64)
218
219        self.log.info("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block")
220        node.reconsiderblock(b1hash)
221
222        res3 = node.gettxoutsetinfo()
223        # The field 'disk_size' is non-deterministic and can thus not be
224        # compared between res and res3.  Everything else should be the same.
225        del res['disk_size'], res3['disk_size']
226        assert_equal(res, res3)
227
228        self.log.info("Test hash_type option for gettxoutsetinfo()")
229        # Adding hash_type 'hash_serialized_2', which is the default, should
230        # not change the result.
231        res4 = node.gettxoutsetinfo(hash_type='hash_serialized_2')
232        del res4['disk_size']
233        assert_equal(res, res4)
234
235        # hash_type none should not return a UTXO set hash.
236        res5 = node.gettxoutsetinfo(hash_type='none')
237        assert 'hash_serialized_2' not in res5
238
239    def _test_getblockheader(self):
240        node = self.nodes[0]
241
242        assert_raises_rpc_error(-8, "hash must be of length 64 (not 8, for 'nonsense')", node.getblockheader, "nonsense")
243        assert_raises_rpc_error(-8, "hash must be hexadecimal string (not 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844')", node.getblockheader, "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
244        assert_raises_rpc_error(-5, "Block not found", node.getblockheader, "0cf7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
245
246        besthash = node.getbestblockhash()
247        secondbesthash = node.getblockhash(199)
248        header = node.getblockheader(blockhash=besthash)
249
250        assert_equal(header['hash'], besthash)
251        assert_equal(header['height'], 200)
252        assert_equal(header['confirmations'], 1)
253        assert_equal(header['previousblockhash'], secondbesthash)
254        assert_is_hex_string(header['chainwork'])
255        assert_equal(header['nTx'], 1)
256        assert_is_hash_string(header['hash'])
257        assert_is_hash_string(header['previousblockhash'])
258        assert_is_hash_string(header['merkleroot'])
259        assert_is_hash_string(header['bits'], length=None)
260        assert isinstance(header['time'], int)
261        assert isinstance(header['mediantime'], int)
262        assert isinstance(header['nonce'], int)
263        assert isinstance(header['version'], int)
264        assert isinstance(int(header['versionHex'], 16), int)
265        assert isinstance(header['difficulty'], Decimal)
266
267        # Test with verbose=False, which should return the header as hex.
268        header_hex = node.getblockheader(blockhash=besthash, verbose=False)
269        assert_is_hex_string(header_hex)
270
271        header = FromHex(CBlockHeader(), header_hex)
272        header.calc_sha256()
273        assert_equal(header.hash, besthash)
274
275    def _test_getdifficulty(self):
276        difficulty = self.nodes[0].getdifficulty()
277        # 1 hash in 2 should be valid, so difficulty should be 1/2**31
278        # binary => decimal => binary math is why we do this check
279        assert abs(difficulty * 2**31 - 1) < 0.0001
280
281    def _test_getnetworkhashps(self):
282        hashes_per_second = self.nodes[0].getnetworkhashps()
283        # This should be 2 hashes every 10 minutes or 1/300
284        assert abs(hashes_per_second * 300 - 1) < 0.0001
285
286    def _test_stopatheight(self):
287        assert_equal(self.nodes[0].getblockcount(), 200)
288        self.nodes[0].generatetoaddress(6, self.nodes[0].get_deterministic_priv_key().address)
289        assert_equal(self.nodes[0].getblockcount(), 206)
290        self.log.debug('Node should not stop at this height')
291        assert_raises(subprocess.TimeoutExpired, lambda: self.nodes[0].process.wait(timeout=3))
292        try:
293            self.nodes[0].generatetoaddress(1, self.nodes[0].get_deterministic_priv_key().address)
294        except (ConnectionError, http.client.BadStatusLine):
295            pass  # The node already shut down before response
296        self.log.debug('Node should stop at this height...')
297        self.nodes[0].wait_until_stopped()
298        self.start_node(0)
299        assert_equal(self.nodes[0].getblockcount(), 207)
300
301    def _test_waitforblockheight(self):
302        self.log.info("Test waitforblockheight")
303        node = self.nodes[0]
304        peer = node.add_p2p_connection(P2PInterface())
305
306        current_height = node.getblock(node.getbestblockhash())['height']
307
308        # Create a fork somewhere below our current height, invalidate the tip
309        # of that fork, and then ensure that waitforblockheight still
310        # works as expected.
311        #
312        # (Previously this was broken based on setting
313        # `rpc/blockchain.cpp:latestblock` incorrectly.)
314        #
315        b20hash = node.getblockhash(20)
316        b20 = node.getblock(b20hash)
317
318        def solve_and_send_block(prevhash, height, time):
319            b = create_block(prevhash, create_coinbase(height), time)
320            b.solve()
321            peer.send_and_ping(msg_block(b))
322            return b
323
324        b21f = solve_and_send_block(int(b20hash, 16), 21, b20['time'] + 1)
325        b22f = solve_and_send_block(b21f.sha256, 22, b21f.nTime + 1)
326
327        node.invalidateblock(b22f.hash)
328
329        def assert_waitforheight(height, timeout=2):
330            assert_equal(
331                node.waitforblockheight(height=height, timeout=timeout)['height'],
332                current_height)
333
334        assert_waitforheight(0)
335        assert_waitforheight(current_height - 1)
336        assert_waitforheight(current_height)
337        assert_waitforheight(current_height + 1)
338
339
340if __name__ == '__main__':
341    BlockchainTest().main()
342