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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 resurrection of mined transactions when
  7. # the blockchain is re-organized.
  8. #
  9. from test_framework.test_framework import BitcoinTestFramework
  10. from test_framework.util import *
  11. import os
  12. import shutil
  13. # Create one-input, one-output, no-fee transaction:
  14. class MempoolCoinbaseTest(BitcoinTestFramework):
  15. def setup_network(self):
  16. # Just need one node for this test
  17. args = ["-checkmempool", "-debug=mempool"]
  18. self.nodes = []
  19. self.nodes.append(start_node(0, self.options.tmpdir, args))
  20. self.is_network_split = False
  21. def create_tx(self, from_txid, to_address, amount):
  22. inputs = [{ "txid" : from_txid, "vout" : 0}]
  23. outputs = { to_address : amount }
  24. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  25. signresult = self.nodes[0].signrawtransaction(rawtx)
  26. assert_equal(signresult["complete"], True)
  27. return signresult["hex"]
  28. def run_test(self):
  29. node0_address = self.nodes[0].getnewaddress()
  30. # Spend block 1/2/3's coinbase transactions
  31. # Mine a block.
  32. # Create three more transactions, spending the spends
  33. # Mine another block.
  34. # ... make sure all the transactions are confirmed
  35. # Invalidate both blocks
  36. # ... make sure all the transactions are put back in the mempool
  37. # Mine a new block
  38. # ... make sure all the transactions are confirmed again.
  39. b = [ self.nodes[0].getblockhash(n) for n in range(1, 4) ]
  40. coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
  41. spends1_raw = [ self.create_tx(txid, node0_address, 50) for txid in coinbase_txids ]
  42. spends1_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends1_raw ]
  43. blocks = []
  44. blocks.extend(self.nodes[0].generate(1))
  45. spends2_raw = [ self.create_tx(txid, node0_address, 49.99) for txid in spends1_id ]
  46. spends2_id = [ self.nodes[0].sendrawtransaction(tx) for tx in spends2_raw ]
  47. blocks.extend(self.nodes[0].generate(1))
  48. # mempool should be empty, all txns confirmed
  49. assert_equal(set(self.nodes[0].getrawmempool()), set())
  50. for txid in spends1_id+spends2_id:
  51. tx = self.nodes[0].gettransaction(txid)
  52. assert(tx["confirmations"] > 0)
  53. # Use invalidateblock to re-org back; all transactions should
  54. # end up unconfirmed and back in the mempool
  55. for node in self.nodes:
  56. node.invalidateblock(blocks[0])
  57. # mempool should be empty, all txns confirmed
  58. assert_equal(set(self.nodes[0].getrawmempool()), set(spends1_id+spends2_id))
  59. for txid in spends1_id+spends2_id:
  60. tx = self.nodes[0].gettransaction(txid)
  61. assert(tx["confirmations"] == 0)
  62. # Generate another block, they should all get mined
  63. self.nodes[0].generate(1)
  64. # mempool should be empty, all txns confirmed
  65. assert_equal(set(self.nodes[0].getrawmempool()), set())
  66. for txid in spends1_id+spends2_id:
  67. tx = self.nodes[0].gettransaction(txid)
  68. assert(tx["confirmations"] > 0)
  69. if __name__ == '__main__':
  70. MempoolCoinbaseTest().main()