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 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 __init__(self):
  11. super().__init__()
  12. self.num_nodes = 1
  13. self.setup_clean_chain = False
  14. def setup_network(self):
  15. # Just need one node for this test
  16. args = ["-checkmempool"]
  17. self.nodes = []
  18. self.nodes.append(start_node(0, self.options.tmpdir, args))
  19. self.is_network_split = False
  20. def run_test(self):
  21. node0_address = self.nodes[0].getnewaddress()
  22. # Spend block 1/2/3's coinbase transactions
  23. # Mine a block.
  24. # Create three more transactions, spending the spends
  25. # Mine another block.
  26. # ... make sure all the transactions are confirmed
  27. # Invalidate both blocks
  28. # ... make sure all the transactions are put back in the mempool
  29. # Mine a new block
  30. # ... make sure all the transactions are confirmed again.
  31. b = [ self.nodes[0].getblockhash(n) for n in range(1, 4) ]
  32. coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
  33. spends1_raw = [ create_tx(self.nodes[0], txid, node0_address, 49.99) for txid in coinbase_txids ]
  34. spends1_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends1_raw ]
  35. blocks = []
  36. blocks.extend(self.nodes[0].generate(1))
  37. spends2_raw = [ create_tx(self.nodes[0], txid, node0_address, 49.98) for txid in spends1_id ]
  38. spends2_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends2_raw ]
  39. blocks.extend(self.nodes[0].generate(1))
  40. # mempool should be empty, all txns confirmed
  41. assert_equal(set(self.nodes[0].getrawmempool()), set())
  42. for txid in spends1_id+spends2_id:
  43. tx = self.nodes[0].gettransaction(txid)
  44. assert(tx["confirmations"] > 0)
  45. # Use invalidateblock to re-org back; all transactions should
  46. # end up unconfirmed and back in the mempool
  47. for node in self.nodes:
  48. node.invalidateblock(blocks[0])
  49. # mempool should be empty, all txns confirmed
  50. assert_equal(set(self.nodes[0].getrawmempool()), set(spends1_id+spends2_id))
  51. for txid in spends1_id+spends2_id:
  52. tx = self.nodes[0].gettransaction(txid)
  53. assert(tx["confirmations"] == 0)
  54. # Generate another block, they should all get mined
  55. self.nodes[0].generate(1)
  56. # mempool should be empty, all txns confirmed
  57. assert_equal(set(self.nodes[0].getrawmempool()), set())
  58. for txid in spends1_id+spends2_id:
  59. tx = self.nodes[0].gettransaction(txid)
  60. assert(tx["confirmations"] > 0)
  61. if __name__ == '__main__':
  62. MempoolCoinbaseTest().main()