You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blockchain.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.
  6. Test the following RPCs:
  7. - gettxoutsetinfo
  8. - verifychain
  9. Tests correspond to code in rpc/blockchain.cpp.
  10. """
  11. from decimal import Decimal
  12. from test_framework.test_framework import BitcoinTestFramework
  13. from test_framework.util import (
  14. assert_equal,
  15. assert_raises_jsonrpc,
  16. assert_is_hex_string,
  17. assert_is_hash_string,
  18. start_nodes,
  19. connect_nodes_bi,
  20. )
  21. class BlockchainTest(BitcoinTestFramework):
  22. def __init__(self):
  23. super().__init__()
  24. self.setup_clean_chain = False
  25. self.num_nodes = 2
  26. def setup_network(self, split=False):
  27. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
  28. connect_nodes_bi(self.nodes, 0, 1)
  29. self.is_network_split = False
  30. self.sync_all()
  31. def run_test(self):
  32. self._test_gettxoutsetinfo()
  33. self._test_getblockheader()
  34. self.nodes[0].verifychain(4, 0)
  35. def _test_gettxoutsetinfo(self):
  36. node = self.nodes[0]
  37. res = node.gettxoutsetinfo()
  38. assert_equal(res['total_amount'], Decimal('8725.00000000'))
  39. assert_equal(res['transactions'], 200)
  40. assert_equal(res['height'], 200)
  41. assert_equal(res['txouts'], 200)
  42. assert_equal(res['bytes_serialized'], 13924),
  43. assert_equal(len(res['bestblock']), 64)
  44. assert_equal(len(res['hash_serialized']), 64)
  45. def _test_getblockheader(self):
  46. node = self.nodes[0]
  47. assert_raises_jsonrpc(-5, "Block not found", node.getblockheader, "nonsense")
  48. besthash = node.getbestblockhash()
  49. secondbesthash = node.getblockhash(199)
  50. header = node.getblockheader(besthash)
  51. assert_equal(header['hash'], besthash)
  52. assert_equal(header['height'], 200)
  53. assert_equal(header['confirmations'], 1)
  54. assert_equal(header['previousblockhash'], secondbesthash)
  55. assert_is_hex_string(header['chainwork'])
  56. assert_is_hash_string(header['hash'])
  57. assert_is_hash_string(header['previousblockhash'])
  58. assert_is_hash_string(header['merkleroot'])
  59. assert_is_hash_string(header['bits'], length=None)
  60. assert isinstance(header['time'], int)
  61. assert isinstance(header['mediantime'], int)
  62. assert isinstance(header['nonce'], int)
  63. assert isinstance(header['version'], int)
  64. assert isinstance(int(header['versionHex'], 16), int)
  65. assert isinstance(header['difficulty'], Decimal)
  66. if __name__ == '__main__':
  67. BlockchainTest().main()