[qa] TestNode: Add wait_until_stopped helper method
[bitcoinplatinum.git] / test / functional / blockchain.py
blob50be9262e4f9cf9b95a2c397c11c55dc5905d817
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 - getchaintxstats
14 - getnetworkhashps
15 - verifychain
17 Tests correspond to code in rpc/blockchain.cpp.
18 """
20 from decimal import Decimal
21 import http.client
22 import subprocess
24 from test_framework.test_framework import BitcoinTestFramework
25 from test_framework.util import (
26 assert_equal,
27 assert_raises,
28 assert_raises_jsonrpc,
29 assert_is_hex_string,
30 assert_is_hash_string,
33 class BlockchainTest(BitcoinTestFramework):
34 def set_test_params(self):
35 self.num_nodes = 1
36 self.extra_args = [['-stopatheight=207']]
38 def run_test(self):
39 self._test_getchaintxstats()
40 self._test_gettxoutsetinfo()
41 self._test_getblockheader()
42 self._test_getdifficulty()
43 self._test_getnetworkhashps()
44 self._test_stopatheight()
45 assert self.nodes[0].verifychain(4, 0)
47 def _test_getchaintxstats(self):
48 chaintxstats = self.nodes[0].getchaintxstats(1)
49 # 200 txs plus genesis tx
50 assert_equal(chaintxstats['txcount'], 201)
51 # tx rate should be 1 per 10 minutes, or 1/600
52 # we have to round because of binary math
53 assert_equal(round(chaintxstats['txrate'] * 600, 10), Decimal(1))
55 def _test_gettxoutsetinfo(self):
56 node = self.nodes[0]
57 res = node.gettxoutsetinfo()
59 assert_equal(res['total_amount'], Decimal('8725.00000000'))
60 assert_equal(res['transactions'], 200)
61 assert_equal(res['height'], 200)
62 assert_equal(res['txouts'], 200)
63 assert_equal(res['bogosize'], 17000),
64 assert_equal(res['bestblock'], node.getblockhash(200))
65 size = res['disk_size']
66 assert size > 6400
67 assert size < 64000
68 assert_equal(len(res['bestblock']), 64)
69 assert_equal(len(res['hash_serialized_2']), 64)
71 self.log.info("Test that gettxoutsetinfo() works for blockchain with just the genesis block")
72 b1hash = node.getblockhash(1)
73 node.invalidateblock(b1hash)
75 res2 = node.gettxoutsetinfo()
76 assert_equal(res2['transactions'], 0)
77 assert_equal(res2['total_amount'], Decimal('0'))
78 assert_equal(res2['height'], 0)
79 assert_equal(res2['txouts'], 0)
80 assert_equal(res2['bogosize'], 0),
81 assert_equal(res2['bestblock'], node.getblockhash(0))
82 assert_equal(len(res2['hash_serialized_2']), 64)
84 self.log.info("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block")
85 node.reconsiderblock(b1hash)
87 res3 = node.gettxoutsetinfo()
88 assert_equal(res['total_amount'], res3['total_amount'])
89 assert_equal(res['transactions'], res3['transactions'])
90 assert_equal(res['height'], res3['height'])
91 assert_equal(res['txouts'], res3['txouts'])
92 assert_equal(res['bogosize'], res3['bogosize'])
93 assert_equal(res['bestblock'], res3['bestblock'])
94 assert_equal(res['hash_serialized_2'], res3['hash_serialized_2'])
96 def _test_getblockheader(self):
97 node = self.nodes[0]
99 assert_raises_jsonrpc(-5, "Block not found",
100 node.getblockheader, "nonsense")
102 besthash = node.getbestblockhash()
103 secondbesthash = node.getblockhash(199)
104 header = node.getblockheader(besthash)
106 assert_equal(header['hash'], besthash)
107 assert_equal(header['height'], 200)
108 assert_equal(header['confirmations'], 1)
109 assert_equal(header['previousblockhash'], secondbesthash)
110 assert_is_hex_string(header['chainwork'])
111 assert_is_hash_string(header['hash'])
112 assert_is_hash_string(header['previousblockhash'])
113 assert_is_hash_string(header['merkleroot'])
114 assert_is_hash_string(header['bits'], length=None)
115 assert isinstance(header['time'], int)
116 assert isinstance(header['mediantime'], int)
117 assert isinstance(header['nonce'], int)
118 assert isinstance(header['version'], int)
119 assert isinstance(int(header['versionHex'], 16), int)
120 assert isinstance(header['difficulty'], Decimal)
122 def _test_getdifficulty(self):
123 difficulty = self.nodes[0].getdifficulty()
124 # 1 hash in 2 should be valid, so difficulty should be 1/2**31
125 # binary => decimal => binary math is why we do this check
126 assert abs(difficulty * 2**31 - 1) < 0.0001
128 def _test_getnetworkhashps(self):
129 hashes_per_second = self.nodes[0].getnetworkhashps()
130 # This should be 2 hashes every 10 minutes or 1/300
131 assert abs(hashes_per_second * 300 - 1) < 0.0001
133 def _test_stopatheight(self):
134 assert_equal(self.nodes[0].getblockcount(), 200)
135 self.nodes[0].generate(6)
136 assert_equal(self.nodes[0].getblockcount(), 206)
137 self.log.debug('Node should not stop at this height')
138 assert_raises(subprocess.TimeoutExpired, lambda: self.nodes[0].process.wait(timeout=3))
139 try:
140 self.nodes[0].generate(1)
141 except (ConnectionError, http.client.BadStatusLine):
142 pass # The node already shut down before response
143 self.log.debug('Node should stop at this height...')
144 self.nodes[0].wait_until_stopped()
145 self.start_node(0)
146 assert_equal(self.nodes[0].getblockcount(), 207)
149 if __name__ == '__main__':
150 BlockchainTest().main()