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.5KB

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