Skip to content

Commit 135d6ec

Browse files
committed
Add RPC tests for getblockheader.
1 parent 4745636 commit 135d6ec

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

qa/rpc-tests/blockchain.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#
7-
# Test RPC calls related to blockchain state.
7+
# Test RPC calls related to blockchain state. Tests correspond to code in
8+
# rpcblockchain.cpp.
89
#
910

1011
import decimal
1112

1213
from test_framework.test_framework import BitcoinTestFramework
14+
from test_framework.authproxy import JSONRPCException
1315
from test_framework.util import (
1416
initialize_chain,
1517
assert_equal,
18+
assert_raises,
19+
assert_is_hex_string,
20+
assert_is_hash_string,
1621
start_nodes,
1722
connect_nodes_bi,
1823
)
1924

25+
2026
class BlockchainTest(BitcoinTestFramework):
2127
"""
2228
Test blockchain-related RPC calls:
@@ -36,6 +42,10 @@ def setup_network(self, split=False):
3642
self.sync_all()
3743

3844
def run_test(self):
45+
self._test_gettxoutsetinfo()
46+
self._test_getblockheader()
47+
48+
def _test_gettxoutsetinfo(self):
3949
node = self.nodes[0]
4050
res = node.gettxoutsetinfo()
4151

@@ -47,6 +57,30 @@ def run_test(self):
4757
assert_equal(len(res[u'bestblock']), 64)
4858
assert_equal(len(res[u'hash_serialized']), 64)
4959

60+
def _test_getblockheader(self):
61+
node = self.nodes[0]
62+
63+
assert_raises(
64+
JSONRPCException, lambda: node.getblockheader('nonsense'))
65+
66+
besthash = node.getbestblockhash()
67+
secondbesthash = node.getblockhash(199)
68+
header = node.getblockheader(besthash)
69+
70+
assert_equal(header['hash'], besthash)
71+
assert_equal(header['height'], 200)
72+
assert_equal(header['confirmations'], 1)
73+
assert_equal(header['previousblockhash'], secondbesthash)
74+
assert_is_hex_string(header['chainwork'])
75+
assert_is_hash_string(header['hash'])
76+
assert_is_hash_string(header['previousblockhash'])
77+
assert_is_hash_string(header['merkleroot'])
78+
assert_is_hash_string(header['bits'], length=None)
79+
assert isinstance(header['time'], int)
80+
assert isinstance(header['mediantime'], int)
81+
assert isinstance(header['nonce'], int)
82+
assert isinstance(header['version'], int)
83+
assert isinstance(header['difficulty'], decimal.Decimal)
5084

5185
if __name__ == '__main__':
5286
BlockchainTest().main()

0 commit comments

Comments
 (0)