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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 spending coinbase transactions.
  6. The coinbase transaction in block N can appear in block
  7. N+100... so is valid in the mempool when the best block
  8. height is N+99.
  9. This test makes sure coinbase spends that will be mature
  10. in the next block are accepted into the memory pool,
  11. but less mature coinbase spends are NOT.
  12. """
  13. from test_framework.test_framework import BitcoinTestFramework
  14. from test_framework.util import *
  15. # Create one-input, one-output, no-fee transaction:
  16. class MempoolSpendCoinbaseTest(BitcoinTestFramework):
  17. def set_test_params(self):
  18. self.num_nodes = 1
  19. self.extra_args = [["-checkmempool"]]
  20. def run_test(self):
  21. chain_height = self.nodes[0].getblockcount()
  22. assert_equal(chain_height, 200)
  23. node0_address = self.nodes[0].getnewaddress()
  24. # Coinbase at height chain_height-100+1 ok in mempool, should
  25. # get mined. Coinbase at height chain_height-100+2 is
  26. # is too immature to spend.
  27. b = [ self.nodes[0].getblockhash(n) for n in range(101, 103) ]
  28. coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
  29. spends_raw = [ create_tx(self.nodes[0], txid, node0_address, 49.99) for txid in coinbase_txids ]
  30. spend_101_id = self.nodes[0].sendrawtransaction(spends_raw[0])
  31. # coinbase at height 102 should be too immature to spend
  32. assert_raises_rpc_error(-26,"bad-txns-premature-spend-of-coinbase", self.nodes[0].sendrawtransaction, spends_raw[1])
  33. # mempool should have just spend_101:
  34. assert_equal(self.nodes[0].getrawmempool(), [ spend_101_id ])
  35. # mine a block, spend_101 should get confirmed
  36. self.nodes[0].generate(1)
  37. assert_equal(set(self.nodes[0].getrawmempool()), set())
  38. # ... and now height 102 can be spent:
  39. spend_102_id = self.nodes[0].sendrawtransaction(spends_raw[1])
  40. assert_equal(self.nodes[0].getrawmempool(), [ spend_102_id ])
  41. if __name__ == '__main__':
  42. MempoolSpendCoinbaseTest().main()