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_reorg.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 re-org scenarios with a mempool that contains transactions
  7. # that spend (directly or indirectly) coinbase transactions.
  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. alert_filename = None # Set by setup_network
  16. def setup_network(self):
  17. args = ["-checkmempool", "-debug=mempool"]
  18. self.nodes = []
  19. self.nodes.append(start_node(0, self.options.tmpdir, args))
  20. self.nodes.append(start_node(1, self.options.tmpdir, args))
  21. connect_nodes(self.nodes[1], 0)
  22. self.is_network_split = False
  23. self.sync_all
  24. def create_tx(self, from_txid, to_address, amount):
  25. inputs = [{ "txid" : from_txid, "vout" : 0}]
  26. outputs = { to_address : amount }
  27. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  28. signresult = self.nodes[0].signrawtransaction(rawtx)
  29. assert_equal(signresult["complete"], True)
  30. return signresult["hex"]
  31. def run_test(self):
  32. start_count = self.nodes[0].getblockcount()
  33. # Mine three blocks. After this, nodes[0] blocks
  34. # 101, 102, and 103 are spend-able.
  35. new_blocks = self.nodes[1].generate(4)
  36. self.sync_all()
  37. node0_address = self.nodes[0].getnewaddress()
  38. node1_address = self.nodes[1].getnewaddress()
  39. # Three scenarios for re-orging coinbase spends in the memory pool:
  40. # 1. Direct coinbase spend : spend_101
  41. # 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
  42. # 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
  43. # Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
  44. # and make sure the mempool code behaves correctly.
  45. b = [ self.nodes[0].getblockhash(n) for n in range(101, 105) ]
  46. coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
  47. spend_101_raw = self.create_tx(coinbase_txids[1], node1_address, 50)
  48. spend_102_raw = self.create_tx(coinbase_txids[2], node0_address, 50)
  49. spend_103_raw = self.create_tx(coinbase_txids[3], node0_address, 50)
  50. # Create a block-height-locked transaction which will be invalid after reorg
  51. timelock_tx = self.nodes[0].createrawtransaction([{"txid": coinbase_txids[0], "vout": 0}], {node0_address: 50})
  52. # Set the time lock
  53. timelock_tx = timelock_tx.replace("ffffffff", "11111111", 1)
  54. timelock_tx = timelock_tx[:-8] + hex(self.nodes[0].getblockcount() + 2)[2:] + "000000"
  55. timelock_tx = self.nodes[0].signrawtransaction(timelock_tx)["hex"]
  56. assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, timelock_tx)
  57. # Broadcast and mine spend_102 and 103:
  58. spend_102_id = self.nodes[0].sendrawtransaction(spend_102_raw)
  59. spend_103_id = self.nodes[0].sendrawtransaction(spend_103_raw)
  60. self.nodes[0].generate(1)
  61. assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, timelock_tx)
  62. # Create 102_1 and 103_1:
  63. spend_102_1_raw = self.create_tx(spend_102_id, node1_address, 50)
  64. spend_103_1_raw = self.create_tx(spend_103_id, node1_address, 50)
  65. # Broadcast and mine 103_1:
  66. spend_103_1_id = self.nodes[0].sendrawtransaction(spend_103_1_raw)
  67. last_block = self.nodes[0].generate(1)
  68. timelock_tx_id = self.nodes[0].sendrawtransaction(timelock_tx)
  69. # ... now put spend_101 and spend_102_1 in memory pools:
  70. spend_101_id = self.nodes[0].sendrawtransaction(spend_101_raw)
  71. spend_102_1_id = self.nodes[0].sendrawtransaction(spend_102_1_raw)
  72. self.sync_all()
  73. assert_equal(set(self.nodes[0].getrawmempool()), set([ spend_101_id, spend_102_1_id, timelock_tx_id ]))
  74. for node in self.nodes:
  75. node.invalidateblock(last_block[0])
  76. assert_equal(set(self.nodes[0].getrawmempool()), set([ spend_101_id, spend_102_1_id, spend_103_1_id ]))
  77. # Use invalidateblock to re-org back and make all those coinbase spends
  78. # immature/invalid:
  79. for node in self.nodes:
  80. node.invalidateblock(new_blocks[0])
  81. self.sync_all()
  82. # mempool should be empty.
  83. assert_equal(set(self.nodes[0].getrawmempool()), set())
  84. if __name__ == '__main__':
  85. MempoolCoinbaseTest().main()