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:
17 Tests correspond to code in rpc/blockchain.cpp.
20 from decimal
import Decimal
24 from test_framework
.test_framework
import (BitcoinTestFramework
, BITCOIND_PROC_WAIT_TIMEOUT
)
25 from test_framework
.util
import (
28 assert_raises_jsonrpc
,
30 assert_is_hash_string
,
34 class BlockchainTest(BitcoinTestFramework
):
38 self
.setup_clean_chain
= False
40 self
.extra_args
= [['-stopatheight=207']]
43 self
._test
_getchaintxstats
()
44 self
._test
_gettxoutsetinfo
()
45 self
._test
_getblockheader
()
46 self
._test
_getdifficulty
()
47 self
._test
_getnetworkhashps
()
48 self
._test
_stopatheight
()
49 assert self
.nodes
[0].verifychain(4, 0)
51 def _test_getchaintxstats(self
):
52 chaintxstats
= self
.nodes
[0].getchaintxstats(1)
53 # 200 txs plus genesis tx
54 assert_equal(chaintxstats
['txcount'], 201)
55 # tx rate should be 1 per 10 minutes, or 1/600
56 # we have to round because of binary math
57 assert_equal(round(chaintxstats
['txrate'] * 600, 10), Decimal(1))
59 def _test_gettxoutsetinfo(self
):
61 res
= node
.gettxoutsetinfo()
63 assert_equal(res
['total_amount'], Decimal('8725.00000000'))
64 assert_equal(res
['transactions'], 200)
65 assert_equal(res
['height'], 200)
66 assert_equal(res
['txouts'], 200)
67 assert_equal(res
['bogosize'], 17000),
68 assert_equal(res
['bestblock'], node
.getblockhash(200))
69 size
= res
['disk_size']
72 assert_equal(len(res
['bestblock']), 64)
73 assert_equal(len(res
['hash_serialized_2']), 64)
75 self
.log
.info("Test that gettxoutsetinfo() works for blockchain with just the genesis block")
76 b1hash
= node
.getblockhash(1)
77 node
.invalidateblock(b1hash
)
79 res2
= node
.gettxoutsetinfo()
80 assert_equal(res2
['transactions'], 0)
81 assert_equal(res2
['total_amount'], Decimal('0'))
82 assert_equal(res2
['height'], 0)
83 assert_equal(res2
['txouts'], 0)
84 assert_equal(res2
['bogosize'], 0),
85 assert_equal(res2
['bestblock'], node
.getblockhash(0))
86 assert_equal(len(res2
['hash_serialized_2']), 64)
88 self
.log
.info("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block")
89 node
.reconsiderblock(b1hash
)
91 res3
= node
.gettxoutsetinfo()
92 assert_equal(res
['total_amount'], res3
['total_amount'])
93 assert_equal(res
['transactions'], res3
['transactions'])
94 assert_equal(res
['height'], res3
['height'])
95 assert_equal(res
['txouts'], res3
['txouts'])
96 assert_equal(res
['bogosize'], res3
['bogosize'])
97 assert_equal(res
['bestblock'], res3
['bestblock'])
98 assert_equal(res
['hash_serialized_2'], res3
['hash_serialized_2'])
100 def _test_getblockheader(self
):
103 assert_raises_jsonrpc(-5, "Block not found",
104 node
.getblockheader
, "nonsense")
106 besthash
= node
.getbestblockhash()
107 secondbesthash
= node
.getblockhash(199)
108 header
= node
.getblockheader(besthash
)
110 assert_equal(header
['hash'], besthash
)
111 assert_equal(header
['height'], 200)
112 assert_equal(header
['confirmations'], 1)
113 assert_equal(header
['previousblockhash'], secondbesthash
)
114 assert_is_hex_string(header
['chainwork'])
115 assert_is_hash_string(header
['hash'])
116 assert_is_hash_string(header
['previousblockhash'])
117 assert_is_hash_string(header
['merkleroot'])
118 assert_is_hash_string(header
['bits'], length
=None)
119 assert isinstance(header
['time'], int)
120 assert isinstance(header
['mediantime'], int)
121 assert isinstance(header
['nonce'], int)
122 assert isinstance(header
['version'], int)
123 assert isinstance(int(header
['versionHex'], 16), int)
124 assert isinstance(header
['difficulty'], Decimal
)
126 def _test_getdifficulty(self
):
127 difficulty
= self
.nodes
[0].getdifficulty()
128 # 1 hash in 2 should be valid, so difficulty should be 1/2**31
129 # binary => decimal => binary math is why we do this check
130 assert abs(difficulty
* 2**31 - 1) < 0.0001
132 def _test_getnetworkhashps(self
):
133 hashes_per_second
= self
.nodes
[0].getnetworkhashps()
134 # This should be 2 hashes every 10 minutes or 1/300
135 assert abs(hashes_per_second
* 300 - 1) < 0.0001
137 def _test_stopatheight(self
):
138 assert_equal(self
.nodes
[0].getblockcount(), 200)
139 self
.nodes
[0].generate(6)
140 assert_equal(self
.nodes
[0].getblockcount(), 206)
141 self
.log
.debug('Node should not stop at this height')
142 assert_raises(subprocess
.TimeoutExpired
, lambda: self
.bitcoind_processes
[0].wait(timeout
=3))
144 self
.nodes
[0].generate(1)
145 except (ConnectionError
, http
.client
.BadStatusLine
):
146 pass # The node already shut down before response
147 self
.log
.debug('Node should stop at this height...')
148 self
.bitcoind_processes
[0].wait(timeout
=BITCOIND_PROC_WAIT_TIMEOUT
)
149 self
.nodes
[0] = self
.start_node(0, self
.options
.tmpdir
)
150 assert_equal(self
.nodes
[0].getblockcount(), 207)
153 if __name__
== '__main__':
154 BlockchainTest().main()