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.

mempool_spendcoinbase.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 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. #
  6. # Test spending coinbase transactions.
  7. # The coinbase transaction in block N can appear in block
  8. # N+100... so is valid in the mempool when the best block
  9. # height is N+99.
  10. # This test makes sure coinbase spends that will be mature
  11. # in the next block are accepted into the memory pool,
  12. # but less mature coinbase spends are NOT.
  13. #
  14. from test_framework.test_framework import BitcoinTestFramework
  15. from test_framework.util import *
  16. import os
  17. import shutil
  18. # Create one-input, one-output, no-fee transaction:
  19. class MempoolSpendCoinbaseTest(BitcoinTestFramework):
  20. def setup_network(self):
  21. # Just need one node for this test
  22. args = ["-checkmempool", "-debug=mempool"]
  23. self.nodes = []
  24. self.nodes.append(start_node(0, self.options.tmpdir, args))
  25. self.is_network_split = False
  26. def create_tx(self, from_txid, to_address, amount):
  27. inputs = [{ "txid" : from_txid, "vout" : 0}]
  28. outputs = { to_address : amount }
  29. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  30. signresult = self.nodes[0].signrawtransaction(rawtx)
  31. assert_equal(signresult["complete"], True)
  32. return signresult["hex"]
  33. def run_test(self):
  34. chain_height = self.nodes[0].getblockcount()
  35. assert_equal(chain_height, 200)
  36. node0_address = self.nodes[0].getnewaddress()
  37. # Coinbase at height chain_height-100+1 ok in mempool, should
  38. # get mined. Coinbase at height chain_height-100+2 is
  39. # is too immature to spend.
  40. b = [ self.nodes[0].getblockhash(n) for n in range(101, 103) ]
  41. coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
  42. spends_raw = [ self.create_tx(txid, node0_address, 50) for txid in coinbase_txids ]
  43. spend_101_id = self.nodes[0].sendrawtransaction(spends_raw[0])
  44. # coinbase at height 102 should be too immature to spend
  45. assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, spends_raw[1])
  46. # mempool should have just spend_101:
  47. assert_equal(self.nodes[0].getrawmempool(), [ spend_101_id ])
  48. # mine a block, spend_101 should get confirmed
  49. self.nodes[0].generate(1)
  50. assert_equal(set(self.nodes[0].getrawmempool()), set())
  51. # ... and now height 102 can be spent:
  52. spend_102_id = self.nodes[0].sendrawtransaction(spends_raw[1])
  53. assert_equal(self.nodes[0].getrawmempool(), [ spend_102_id ])
  54. if __name__ == '__main__':
  55. MempoolSpendCoinbaseTest().main()