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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. - getdifficulty
  9. - getbestblockhash
  10. - getblockhash
  11. - getblockheader
  12. - getnetworkhashps
  13. - verifychain
  14. Tests correspond to code in rpc/blockchain.cpp.
  15. """
  16. from decimal import Decimal
  17. from test_framework.test_framework import BitcoinTestFramework
  18. from test_framework.util import (
  19. assert_equal,
  20. assert_raises_jsonrpc,
  21. assert_is_hex_string,
  22. assert_is_hash_string,
  23. )
  24. class BlockchainTest(BitcoinTestFramework):
  25. def __init__(self):
  26. super().__init__()
  27. self.setup_clean_chain = False
  28. self.num_nodes = 1
  29. def run_test(self):
  30. self._test_gettxoutsetinfo()
  31. self._test_getblockheader()
  32. self._test_getdifficulty()
  33. self._test_getnetworkhashps()
  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['bogosize'], 17000),
  43. assert_equal(res['bestblock'], node.getblockhash(200))
  44. size = res['disk_size']
  45. assert size > 6400
  46. assert size < 64000
  47. assert_equal(len(res['bestblock']), 64)
  48. assert_equal(len(res['hash_serialized_2']), 64)
  49. self.log.info("Test that gettxoutsetinfo() works for blockchain with just the genesis block")
  50. b1hash = node.getblockhash(1)
  51. node.invalidateblock(b1hash)
  52. res2 = node.gettxoutsetinfo()
  53. assert_equal(res2['transactions'], 0)
  54. assert_equal(res2['total_amount'], Decimal('0'))
  55. assert_equal(res2['height'], 0)
  56. assert_equal(res2['txouts'], 0)
  57. assert_equal(res2['bogosize'], 0),
  58. assert_equal(res2['bestblock'], node.getblockhash(0))
  59. assert_equal(len(res2['hash_serialized_2']), 64)
  60. self.log.info("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block")
  61. node.reconsiderblock(b1hash)
  62. res3 = node.gettxoutsetinfo()
  63. assert_equal(res['total_amount'], res3['total_amount'])
  64. assert_equal(res['transactions'], res3['transactions'])
  65. assert_equal(res['height'], res3['height'])
  66. assert_equal(res['txouts'], res3['txouts'])
  67. assert_equal(res['bogosize'], res3['bogosize'])
  68. assert_equal(res['bestblock'], res3['bestblock'])
  69. assert_equal(res['hash_serialized_2'], res3['hash_serialized_2'])
  70. def _test_getblockheader(self):
  71. node = self.nodes[0]
  72. assert_raises_jsonrpc(-5, "Block not found",
  73. node.getblockheader, "nonsense")
  74. besthash = node.getbestblockhash()
  75. secondbesthash = node.getblockhash(199)
  76. header = node.getblockheader(besthash)
  77. assert_equal(header['hash'], besthash)
  78. assert_equal(header['height'], 200)
  79. assert_equal(header['confirmations'], 1)
  80. assert_equal(header['previousblockhash'], secondbesthash)
  81. assert_is_hex_string(header['chainwork'])
  82. assert_is_hash_string(header['hash'])
  83. assert_is_hash_string(header['previousblockhash'])
  84. assert_is_hash_string(header['merkleroot'])
  85. assert_is_hash_string(header['bits'], length=None)
  86. assert isinstance(header['time'], int)
  87. assert isinstance(header['mediantime'], int)
  88. assert isinstance(header['nonce'], int)
  89. assert isinstance(header['version'], int)
  90. assert isinstance(int(header['versionHex'], 16), int)
  91. assert isinstance(header['difficulty'], Decimal)
  92. def _test_getdifficulty(self):
  93. difficulty = self.nodes[0].getdifficulty()
  94. # 1 hash in 2 should be valid, so difficulty should be 1/2**31
  95. # binary => decimal => binary math is why we do this check
  96. assert abs(difficulty * 2**31 - 1) < 0.0001
  97. def _test_getnetworkhashps(self):
  98. hashes_per_second = self.nodes[0].getnetworkhashps()
  99. # This should be 2 hashes every 10 minutes or 1/300
  100. assert abs(hashes_per_second * 300 - 1) < 0.0001
  101. if __name__ == '__main__':
  102. BlockchainTest().main()