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_resurrect_test.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 resurrection of mined transactions when the blockchain is re-organized."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. # Create one-input, one-output, no-fee transaction:
  9. class MempoolCoinbaseTest(BitcoinTestFramework):
  10. def set_test_params(self):
  11. self.num_nodes = 1
  12. self.extra_args = [["-checkmempool"]]
  13. def run_test(self):
  14. node0_address = self.nodes[0].getnewaddress()
  15. # Spend block 1/2/3's coinbase transactions
  16. # Mine a block.
  17. # Create three more transactions, spending the spends
  18. # Mine another block.
  19. # ... make sure all the transactions are confirmed
  20. # Invalidate both blocks
  21. # ... make sure all the transactions are put back in the mempool
  22. # Mine a new block
  23. # ... make sure all the transactions are confirmed again.
  24. b = [ self.nodes[0].getblockhash(n) for n in range(1, 4) ]
  25. coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
  26. spends1_raw = [ create_tx(self.nodes[0], txid, node0_address, 49.99) for txid in coinbase_txids ]
  27. spends1_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends1_raw ]
  28. blocks = []
  29. blocks.extend(self.nodes[0].generate(1))
  30. spends2_raw = [ create_tx(self.nodes[0], txid, node0_address, 49.98) for txid in spends1_id ]
  31. spends2_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends2_raw ]
  32. blocks.extend(self.nodes[0].generate(1))
  33. # mempool should be empty, all txns confirmed
  34. assert_equal(set(self.nodes[0].getrawmempool()), set())
  35. for txid in spends1_id+spends2_id:
  36. tx = self.nodes[0].gettransaction(txid)
  37. assert(tx["confirmations"] > 0)
  38. # Use invalidateblock to re-org back; all transactions should
  39. # end up unconfirmed and back in the mempool
  40. for node in self.nodes:
  41. node.invalidateblock(blocks[0])
  42. # mempool should be empty, all txns confirmed
  43. assert_equal(set(self.nodes[0].getrawmempool()), set(spends1_id+spends2_id))
  44. for txid in spends1_id+spends2_id:
  45. tx = self.nodes[0].gettransaction(txid)
  46. assert(tx["confirmations"] == 0)
  47. # Generate another block, they should all get mined
  48. self.nodes[0].generate(1)
  49. # mempool should be empty, all txns confirmed
  50. assert_equal(set(self.nodes[0].getrawmempool()), set())
  51. for txid in spends1_id+spends2_id:
  52. tx = self.nodes[0].gettransaction(txid)
  53. assert(tx["confirmations"] > 0)
  54. if __name__ == '__main__':
  55. MempoolCoinbaseTest().main()