Report on-disk size in gettxoutsetinfo
[bitcoinplatinum.git] / test / functional / blockchain.py
blob4bfd3ee6770233c081ac2fd96046afcceffb0271
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2016 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.
7 Test the following RPCs:
8 - gettxoutsetinfo
9 - getdifficulty
10 - getbestblockhash
11 - getblockhash
12 - getblockheader
13 - getnetworkhashps
14 - verifychain
16 Tests correspond to code in rpc/blockchain.cpp.
17 """
19 from decimal import Decimal
21 from test_framework.test_framework import BitcoinTestFramework
22 from test_framework.util import (
23 assert_equal,
24 assert_raises_jsonrpc,
25 assert_is_hex_string,
26 assert_is_hash_string,
30 class BlockchainTest(BitcoinTestFramework):
32 def __init__(self):
33 super().__init__()
34 self.setup_clean_chain = False
35 self.num_nodes = 1
37 def run_test(self):
38 self._test_gettxoutsetinfo()
39 self._test_getblockheader()
40 self._test_getdifficulty()
41 self._test_getnetworkhashps()
42 self.nodes[0].verifychain(4, 0)
44 def _test_gettxoutsetinfo(self):
45 node = self.nodes[0]
46 res = node.gettxoutsetinfo()
48 assert_equal(res['total_amount'], Decimal('8725.00000000'))
49 assert_equal(res['transactions'], 200)
50 assert_equal(res['height'], 200)
51 assert_equal(res['txouts'], 200)
52 assert_equal(res['bestblock'], node.getblockhash(200))
53 size = res['disk_size']
54 assert size > 6400
55 assert size < 64000
56 assert_equal(len(res['bestblock']), 64)
57 assert_equal(len(res['hash_serialized_2']), 64)
59 self.log.info("Test that gettxoutsetinfo() works for blockchain with just the genesis block")
60 b1hash = node.getblockhash(1)
61 node.invalidateblock(b1hash)
63 res2 = node.gettxoutsetinfo()
64 assert_equal(res2['transactions'], 0)
65 assert_equal(res2['total_amount'], Decimal('0'))
66 assert_equal(res2['height'], 0)
67 assert_equal(res2['txouts'], 0)
68 assert_equal(res2['bestblock'], node.getblockhash(0))
69 assert_equal(len(res2['hash_serialized_2']), 64)
71 self.log.info("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block")
72 node.reconsiderblock(b1hash)
74 res3 = node.gettxoutsetinfo()
75 assert_equal(res['total_amount'], res3['total_amount'])
76 assert_equal(res['transactions'], res3['transactions'])
77 assert_equal(res['height'], res3['height'])
78 assert_equal(res['txouts'], res3['txouts'])
79 assert_equal(res['bestblock'], res3['bestblock'])
80 assert_equal(res['hash_serialized_2'], res3['hash_serialized_2'])
82 def _test_getblockheader(self):
83 node = self.nodes[0]
85 assert_raises_jsonrpc(-5, "Block not found",
86 node.getblockheader, "nonsense")
88 besthash = node.getbestblockhash()
89 secondbesthash = node.getblockhash(199)
90 header = node.getblockheader(besthash)
92 assert_equal(header['hash'], besthash)
93 assert_equal(header['height'], 200)
94 assert_equal(header['confirmations'], 1)
95 assert_equal(header['previousblockhash'], secondbesthash)
96 assert_is_hex_string(header['chainwork'])
97 assert_is_hash_string(header['hash'])
98 assert_is_hash_string(header['previousblockhash'])
99 assert_is_hash_string(header['merkleroot'])
100 assert_is_hash_string(header['bits'], length=None)
101 assert isinstance(header['time'], int)
102 assert isinstance(header['mediantime'], int)
103 assert isinstance(header['nonce'], int)
104 assert isinstance(header['version'], int)
105 assert isinstance(int(header['versionHex'], 16), int)
106 assert isinstance(header['difficulty'], Decimal)
108 def _test_getdifficulty(self):
109 difficulty = self.nodes[0].getdifficulty()
110 # 1 hash in 2 should be valid, so difficulty should be 1/2**31
111 # binary => decimal => binary math is why we do this check
112 assert abs(difficulty * 2**31 - 1) < 0.0001
114 def _test_getnetworkhashps(self):
115 hashes_per_second = self.nodes[0].getnetworkhashps()
116 # This should be 2 hashes every 10 minutes or 1/300
117 assert abs(hashes_per_second * 300 - 1) < 0.0001
119 if __name__ == '__main__':
120 BlockchainTest().main()