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 os
24import subprocess
25
26from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
27from test_framework.blocktools import (
28    create_block,
29    create_coinbase,
30    TIME_GENESIS_BLOCK,
31)
32from test_framework.messages import (
33    CBlockHeader,
34    from_hex,
35    msg_block,
36)
37from test_framework.p2p import P2PInterface
38from test_framework.test_framework import BitcoinTestFramework
39from test_framework.util import (
40    assert_equal,
41    assert_greater_than,
42    assert_greater_than_or_equal,
43    assert_raises,
44    assert_raises_rpc_error,
45    assert_is_hex_string,
46    assert_is_hash_string,
47    get_datadir_path,
48)
49from test_framework.wallet import MiniWallet
50
51
52class BlockchainTest(BitcoinTestFramework):
53    def set_test_params(self):
54        self.setup_clean_chain = True
55        self.num_nodes = 1
56        self.supports_cli = False
57
58    def run_test(self):
59        self.mine_chain()
60        self.restart_node(0, extra_args=['-stopatheight=207', '-prune=1'])  # Set extra args with pruning after rescan is complete
61
62        self._test_getblockchaininfo()
63        self._test_getchaintxstats()
64        self._test_gettxoutsetinfo()
65        self._test_getblockheader()
66        self._test_getdifficulty()
67        self._test_getnetworkhashps()
68        self._test_stopatheight()
69        self._test_waitforblockheight()
70        self._test_getblock()
71        assert self.nodes[0].verifychain(4, 0)
72
73    def mine_chain(self):
74        self.log.info('Create some old blocks')
75        for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
76            # ten-minute steps from genesis block time
77            self.nodes[0].setmocktime(t)
78            self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_P2WSH_OP_TRUE)
79        assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200)
80
81    def _test_getblockchaininfo(self):
82        self.log.info("Test getblockchaininfo")
83
84        keys = [
85            'bestblockhash',
86            'blocks',
87            'chain',
88            'chainwork',
89            'difficulty',
90            'headers',
91            'initialblockdownload',
92            'mediantime',
93            'pruned',
94            'size_on_disk',
95            'softforks',
96            'verificationprogress',
97            'warnings',
98        ]
99        res = self.nodes[0].getblockchaininfo()
100
101        # result should have these additional pruning keys if manual pruning is enabled
102        assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning'] + keys))
103
104        # size_on_disk should be > 0
105        assert_greater_than(res['size_on_disk'], 0)
106
107        # pruneheight should be greater or equal to 0
108        assert_greater_than_or_equal(res['pruneheight'], 0)
109
110        # check other pruning fields given that prune=1
111        assert res['pruned']
112        assert not res['automatic_pruning']
113
114        self.restart_node(0, ['-stopatheight=207'])
115        res = self.nodes[0].getblockchaininfo()
116        # should have exact keys
117        assert_equal(sorted(res.keys()), keys)
118
119        self.restart_node(0, ['-stopatheight=207', '-prune=550'])
120        res = self.nodes[0].getblockchaininfo()
121        # result should have these additional pruning keys if prune=550
122        assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning', 'prune_target_size'] + keys))
123
124        # check related fields
125        assert res['pruned']
126        assert_equal(res['pruneheight'], 0)
127        assert res['automatic_pruning']
128        assert_equal(res['prune_target_size'], 576716800)
129        assert_greater_than(res['size_on_disk'], 0)
130
131        assert_equal(res['softforks'], {
132            'bip34': {'type': 'buried', 'active': False, 'height': 500},
133            'bip66': {'type': 'buried', 'active': False, 'height': 1251},
134            'bip65': {'type': 'buried', 'active': False, 'height': 1351},
135            'csv': {'type': 'buried', 'active': False, 'height': 432},
136            'segwit': {'type': 'buried', 'active': True, 'height': 0},
137            'testdummy': {
138                'type': 'bip9',
139                'bip9': {
140                    'status': 'started',
141                    'bit': 28,
142                    'start_time': 0,
143                    'timeout': 0x7fffffffffffffff,  # testdummy does not have a timeout so is set to the max int64 value
144                    'since': 144,
145                    'statistics': {
146                        'period': 144,
147                        'threshold': 108,
148                        'elapsed': 57,
149                        'count': 57,
150                        'possible': True,
151                    },
152                    'min_activation_height': 0,
153                },
154                'active': False
155            },
156            'taproot': {
157                'type': 'bip9',
158                'bip9': {
159                    'status': 'active',
160                    'start_time': -1,
161                    'timeout': 9223372036854775807,
162                    'since': 0,
163                    'min_activation_height': 0,
164                },
165                'height': 0,
166                'active': True
167            }
168        })
169
170    def _test_getchaintxstats(self):
171        self.log.info("Test getchaintxstats")
172
173        # Test `getchaintxstats` invalid extra parameters
174        assert_raises_rpc_error(-1, 'getchaintxstats', self.nodes[0].getchaintxstats, 0, '', 0)
175
176        # Test `getchaintxstats` invalid `nblocks`
177        assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self.nodes[0].getchaintxstats, '')
178        assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, -1)
179        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())
180
181        # Test `getchaintxstats` invalid `blockhash`
182        assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].getchaintxstats, blockhash=0)
183        assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 1, for '0')", self.nodes[0].getchaintxstats, blockhash='0')
184        assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].getchaintxstats, blockhash='ZZZ0000000000000000000000000000000000000000000000000000000000000')
185        assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getchaintxstats, blockhash='0000000000000000000000000000000000000000000000000000000000000000')
186        blockhash = self.nodes[0].getblockhash(200)
187        self.nodes[0].invalidateblock(blockhash)
188        assert_raises_rpc_error(-8, "Block is not in main chain", self.nodes[0].getchaintxstats, blockhash=blockhash)
189        self.nodes[0].reconsiderblock(blockhash)
190
191        chaintxstats = self.nodes[0].getchaintxstats(nblocks=1)
192        # 200 txs plus genesis tx
193        assert_equal(chaintxstats['txcount'], 201)
194        # tx rate should be 1 per 10 minutes, or 1/600
195        # we have to round because of binary math
196        assert_equal(round(chaintxstats['txrate'] * 600, 10), Decimal(1))
197
198        b1_hash = self.nodes[0].getblockhash(1)
199        b1 = self.nodes[0].getblock(b1_hash)
200        b200_hash = self.nodes[0].getblockhash(200)
201        b200 = self.nodes[0].getblock(b200_hash)
202        time_diff = b200['mediantime'] - b1['mediantime']
203
204        chaintxstats = self.nodes[0].getchaintxstats()
205        assert_equal(chaintxstats['time'], b200['time'])
206        assert_equal(chaintxstats['txcount'], 201)
207        assert_equal(chaintxstats['window_final_block_hash'], b200_hash)
208        assert_equal(chaintxstats['window_final_block_height'], 200)
209        assert_equal(chaintxstats['window_block_count'], 199)
210        assert_equal(chaintxstats['window_tx_count'], 199)
211        assert_equal(chaintxstats['window_interval'], time_diff)
212        assert_equal(round(chaintxstats['txrate'] * time_diff, 10), Decimal(199))
213
214        chaintxstats = self.nodes[0].getchaintxstats(blockhash=b1_hash)
215        assert_equal(chaintxstats['time'], b1['time'])
216        assert_equal(chaintxstats['txcount'], 2)
217        assert_equal(chaintxstats['window_final_block_hash'], b1_hash)
218        assert_equal(chaintxstats['window_final_block_height'], 1)
219        assert_equal(chaintxstats['window_block_count'], 0)
220        assert 'window_tx_count' not in chaintxstats
221        assert 'window_interval' not in chaintxstats
222        assert 'txrate' not in chaintxstats
223
224    def _test_gettxoutsetinfo(self):
225        node = self.nodes[0]
226        res = node.gettxoutsetinfo()
227
228        assert_equal(res['total_amount'], Decimal('8725.00000000'))
229        assert_equal(res['transactions'], 200)
230        assert_equal(res['height'], 200)
231        assert_equal(res['txouts'], 200)
232        assert_equal(res['bogosize'], 16800),
233        assert_equal(res['bestblock'], node.getblockhash(200))
234        size = res['disk_size']
235        assert size > 6400
236        assert size < 64000
237        assert_equal(len(res['bestblock']), 64)
238        assert_equal(len(res['hash_serialized_2']), 64)
239
240        self.log.info("Test that gettxoutsetinfo() works for blockchain with just the genesis block")
241        b1hash = node.getblockhash(1)
242        node.invalidateblock(b1hash)
243
244        res2 = node.gettxoutsetinfo()
245        assert_equal(res2['transactions'], 0)
246        assert_equal(res2['total_amount'], Decimal('0'))
247        assert_equal(res2['height'], 0)
248        assert_equal(res2['txouts'], 0)
249        assert_equal(res2['bogosize'], 0),
250        assert_equal(res2['bestblock'], node.getblockhash(0))
251        assert_equal(len(res2['hash_serialized_2']), 64)
252
253        self.log.info("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block")
254        node.reconsiderblock(b1hash)
255
256        res3 = node.gettxoutsetinfo()
257        # The field 'disk_size' is non-deterministic and can thus not be
258        # compared between res and res3.  Everything else should be the same.
259        del res['disk_size'], res3['disk_size']
260        assert_equal(res, res3)
261
262        self.log.info("Test hash_type option for gettxoutsetinfo()")
263        # Adding hash_type 'hash_serialized_2', which is the default, should
264        # not change the result.
265        res4 = node.gettxoutsetinfo(hash_type='hash_serialized_2')
266        del res4['disk_size']
267        assert_equal(res, res4)
268
269        # hash_type none should not return a UTXO set hash.
270        res5 = node.gettxoutsetinfo(hash_type='none')
271        assert 'hash_serialized_2' not in res5
272
273        # hash_type muhash should return a different UTXO set hash.
274        res6 = node.gettxoutsetinfo(hash_type='muhash')
275        assert 'muhash' in res6
276        assert(res['hash_serialized_2'] != res6['muhash'])
277
278        # muhash should not be returned unless requested.
279        for r in [res, res2, res3, res4, res5]:
280            assert 'muhash' not in r
281
282        # Unknown hash_type raises an error
283        assert_raises_rpc_error(-8, "foohash is not a valid hash_type", node.gettxoutsetinfo, "foohash")
284
285    def _test_getblockheader(self):
286        node = self.nodes[0]
287
288        assert_raises_rpc_error(-8, "hash must be of length 64 (not 8, for 'nonsense')", node.getblockheader, "nonsense")
289        assert_raises_rpc_error(-8, "hash must be hexadecimal string (not 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844')", node.getblockheader, "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
290        assert_raises_rpc_error(-5, "Block not found", node.getblockheader, "0cf7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
291
292        besthash = node.getbestblockhash()
293        secondbesthash = node.getblockhash(199)
294        header = node.getblockheader(blockhash=besthash)
295
296        assert_equal(header['hash'], besthash)
297        assert_equal(header['height'], 200)
298        assert_equal(header['confirmations'], 1)
299        assert_equal(header['previousblockhash'], secondbesthash)
300        assert_is_hex_string(header['chainwork'])
301        assert_equal(header['nTx'], 1)
302        assert_is_hash_string(header['hash'])
303        assert_is_hash_string(header['previousblockhash'])
304        assert_is_hash_string(header['merkleroot'])
305        assert_is_hash_string(header['bits'], length=None)
306        assert isinstance(header['time'], int)
307        assert isinstance(header['mediantime'], int)
308        assert isinstance(header['nonce'], int)
309        assert isinstance(header['version'], int)
310        assert isinstance(int(header['versionHex'], 16), int)
311        assert isinstance(header['difficulty'], Decimal)
312
313        # Test with verbose=False, which should return the header as hex.
314        header_hex = node.getblockheader(blockhash=besthash, verbose=False)
315        assert_is_hex_string(header_hex)
316
317        header = from_hex(CBlockHeader(), header_hex)
318        header.calc_sha256()
319        assert_equal(header.hash, besthash)
320
321        assert 'previousblockhash' not in node.getblockheader(node.getblockhash(0))
322        assert 'nextblockhash' not in node.getblockheader(node.getbestblockhash())
323
324    def _test_getdifficulty(self):
325        difficulty = self.nodes[0].getdifficulty()
326        # 1 hash in 2 should be valid, so difficulty should be 1/2**31
327        # binary => decimal => binary math is why we do this check
328        assert abs(difficulty * 2**31 - 1) < 0.0001
329
330    def _test_getnetworkhashps(self):
331        hashes_per_second = self.nodes[0].getnetworkhashps()
332        # This should be 2 hashes every 10 minutes or 1/300
333        assert abs(hashes_per_second * 300 - 1) < 0.0001
334
335    def _test_stopatheight(self):
336        assert_equal(self.nodes[0].getblockcount(), 200)
337        self.nodes[0].generatetoaddress(6, ADDRESS_BCRT1_P2WSH_OP_TRUE)
338        assert_equal(self.nodes[0].getblockcount(), 206)
339        self.log.debug('Node should not stop at this height')
340        assert_raises(subprocess.TimeoutExpired, lambda: self.nodes[0].process.wait(timeout=3))
341        try:
342            self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_P2WSH_OP_TRUE)
343        except (ConnectionError, http.client.BadStatusLine):
344            pass  # The node already shut down before response
345        self.log.debug('Node should stop at this height...')
346        self.nodes[0].wait_until_stopped()
347        self.start_node(0)
348        assert_equal(self.nodes[0].getblockcount(), 207)
349
350    def _test_waitforblockheight(self):
351        self.log.info("Test waitforblockheight")
352        node = self.nodes[0]
353        peer = node.add_p2p_connection(P2PInterface())
354
355        current_height = node.getblock(node.getbestblockhash())['height']
356
357        # Create a fork somewhere below our current height, invalidate the tip
358        # of that fork, and then ensure that waitforblockheight still
359        # works as expected.
360        #
361        # (Previously this was broken based on setting
362        # `rpc/blockchain.cpp:latestblock` incorrectly.)
363        #
364        b20hash = node.getblockhash(20)
365        b20 = node.getblock(b20hash)
366
367        def solve_and_send_block(prevhash, height, time):
368            b = create_block(prevhash, create_coinbase(height), time)
369            b.solve()
370            peer.send_and_ping(msg_block(b))
371            return b
372
373        b21f = solve_and_send_block(int(b20hash, 16), 21, b20['time'] + 1)
374        b22f = solve_and_send_block(b21f.sha256, 22, b21f.nTime + 1)
375
376        node.invalidateblock(b22f.hash)
377
378        def assert_waitforheight(height, timeout=2):
379            assert_equal(
380                node.waitforblockheight(height=height, timeout=timeout)['height'],
381                current_height)
382
383        assert_waitforheight(0)
384        assert_waitforheight(current_height - 1)
385        assert_waitforheight(current_height)
386        assert_waitforheight(current_height + 1)
387
388    def _test_getblock(self):
389        node = self.nodes[0]
390
391        miniwallet = MiniWallet(node)
392        miniwallet.scan_blocks(num=5)
393
394        fee_per_byte = Decimal('0.00000010')
395        fee_per_kb = 1000 * fee_per_byte
396
397        miniwallet.send_self_transfer(fee_rate=fee_per_kb, from_node=node)
398        blockhash = node.generate(1)[0]
399
400        self.log.info("Test that getblock with verbosity 1 doesn't include fee")
401        block = node.getblock(blockhash, 1)
402        assert 'fee' not in block['tx'][1]
403
404        self.log.info('Test that getblock with verbosity 2 includes expected fee')
405        block = node.getblock(blockhash, 2)
406        tx = block['tx'][1]
407        assert 'fee' in tx
408        assert_equal(tx['fee'], tx['vsize'] * fee_per_byte)
409
410        self.log.info("Test that getblock with verbosity 2 still works with pruned Undo data")
411        datadir = get_datadir_path(self.options.tmpdir, 0)
412
413        self.log.info("Test that getblock with invalid verbosity type returns proper error message")
414        assert_raises_rpc_error(-1, "JSON value is not an integer as expected", node.getblock, blockhash, "2")
415
416        def move_block_file(old, new):
417            old_path = os.path.join(datadir, self.chain, 'blocks', old)
418            new_path = os.path.join(datadir, self.chain, 'blocks', new)
419            os.rename(old_path, new_path)
420
421        # Move instead of deleting so we can restore chain state afterwards
422        move_block_file('rev00000.dat', 'rev_wrong')
423
424        block = node.getblock(blockhash, 2)
425        assert 'fee' not in block['tx'][1]
426
427        # Restore chain state
428        move_block_file('rev_wrong', 'rev00000.dat')
429
430        assert 'previousblockhash' not in node.getblock(node.getblockhash(0))
431        assert 'nextblockhash' not in node.getblock(node.getbestblockhash())
432
433
434if __name__ == '__main__':
435    BlockchainTest().main()
436