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.

mining.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 mining RPCs
  6. - getmininginfo
  7. - getblocktemplate proposal mode
  8. - submitblock"""
  9. import copy
  10. from binascii import b2a_hex
  11. from decimal import Decimal
  12. from test_framework.blocktools import create_coinbase
  13. from test_framework.mininode import CBlock
  14. from test_framework.test_framework import BitcoinTestFramework
  15. from test_framework.util import assert_equal, assert_raises_jsonrpc
  16. def b2x(b):
  17. return b2a_hex(b).decode('ascii')
  18. def assert_template(node, block, expect, rehash=True):
  19. if rehash:
  20. block.hashMerkleRoot = block.calc_merkle_root()
  21. rsp = node.getblocktemplate({'data': b2x(block.serialize()), 'mode': 'proposal'})
  22. assert_equal(rsp, expect)
  23. class MiningTest(BitcoinTestFramework):
  24. def set_test_params(self):
  25. self.num_nodes = 2
  26. self.setup_clean_chain = False
  27. def run_test(self):
  28. node = self.nodes[0]
  29. self.log.info('getmininginfo')
  30. mining_info = node.getmininginfo()
  31. assert_equal(mining_info['blocks'], 200)
  32. assert_equal(mining_info['chain'], 'regtest')
  33. assert_equal(mining_info['currentblocktx'], 0)
  34. assert_equal(mining_info['currentblockweight'], 0)
  35. assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
  36. assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
  37. assert_equal(mining_info['pooledtx'], 0)
  38. # Mine a block to leave initial block download
  39. node.generate(1)
  40. tmpl = node.getblocktemplate()
  41. self.log.info("getblocktemplate: Test capability advertised")
  42. assert 'proposal' in tmpl['capabilities']
  43. assert 'coinbasetxn' not in tmpl
  44. coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
  45. # sequence numbers must not be max for nLockTime to have effect
  46. coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
  47. coinbase_tx.rehash()
  48. block = CBlock()
  49. block.nVersion = tmpl["version"]
  50. block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
  51. block.nTime = tmpl["curtime"]
  52. block.nBits = int(tmpl["bits"], 16)
  53. block.nNonce = 0
  54. block.vtx = [coinbase_tx]
  55. self.log.info("getblocktemplate: Test valid block")
  56. assert_template(node, block, None)
  57. self.log.info("submitblock: Test block decode failure")
  58. assert_raises_jsonrpc(-22, "Block decode failed", node.submitblock, b2x(block.serialize()[:-15]))
  59. self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
  60. bad_block = copy.deepcopy(block)
  61. bad_block.vtx[0].vin[0].prevout.hash += 1
  62. bad_block.vtx[0].rehash()
  63. assert_template(node, bad_block, 'bad-cb-missing')
  64. self.log.info("submitblock: Test invalid coinbase transaction")
  65. assert_raises_jsonrpc(-22, "Block does not start with a coinbase", node.submitblock, b2x(bad_block.serialize()))
  66. self.log.info("getblocktemplate: Test truncated final transaction")
  67. assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal'})
  68. self.log.info("getblocktemplate: Test duplicate transaction")
  69. bad_block = copy.deepcopy(block)
  70. bad_block.vtx.append(bad_block.vtx[0])
  71. assert_template(node, bad_block, 'bad-txns-duplicate')
  72. self.log.info("getblocktemplate: Test invalid transaction")
  73. bad_block = copy.deepcopy(block)
  74. bad_tx = copy.deepcopy(bad_block.vtx[0])
  75. bad_tx.vin[0].prevout.hash = 255
  76. bad_tx.rehash()
  77. bad_block.vtx.append(bad_tx)
  78. assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
  79. self.log.info("getblocktemplate: Test nonfinal transaction")
  80. bad_block = copy.deepcopy(block)
  81. bad_block.vtx[0].nLockTime = 2 ** 32 - 1
  82. bad_block.vtx[0].rehash()
  83. assert_template(node, bad_block, 'bad-txns-nonfinal')
  84. self.log.info("getblocktemplate: Test bad tx count")
  85. # The tx count is immediately after the block header
  86. TX_COUNT_OFFSET = 80
  87. bad_block_sn = bytearray(block.serialize())
  88. assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
  89. bad_block_sn[TX_COUNT_OFFSET] += 1
  90. assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
  91. self.log.info("getblocktemplate: Test bad bits")
  92. bad_block = copy.deepcopy(block)
  93. bad_block.nBits = 469762303 # impossible in the real world
  94. assert_template(node, bad_block, 'bad-diffbits')
  95. self.log.info("getblocktemplate: Test bad merkle root")
  96. bad_block = copy.deepcopy(block)
  97. bad_block.hashMerkleRoot += 1
  98. assert_template(node, bad_block, 'bad-txnmrklroot', False)
  99. self.log.info("getblocktemplate: Test bad timestamps")
  100. bad_block = copy.deepcopy(block)
  101. bad_block.nTime = 2 ** 31 - 1
  102. assert_template(node, bad_block, 'time-too-new')
  103. bad_block.nTime = 0
  104. assert_template(node, bad_block, 'time-too-old')
  105. self.log.info("getblocktemplate: Test not best block")
  106. bad_block = copy.deepcopy(block)
  107. bad_block.hashPrevBlock = 123
  108. assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
  109. if __name__ == '__main__':
  110. MiningTest().main()