您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

blockchain.py 5.6KB

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