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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 mempool re-org scenarios.
  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. # Create one-input, one-output, no-fee transaction:
  12. class MempoolCoinbaseTest(BitcoinTestFramework):
  13. def __init__(self):
  14. super().__init__()
  15. self.num_nodes = 2
  16. self.setup_clean_chain = False
  17. alert_filename = None # Set by setup_network
  18. def setup_network(self):
  19. args = ["-checkmempool"]
  20. self.nodes = []
  21. self.nodes.append(start_node(0, self.options.tmpdir, args))
  22. self.nodes.append(start_node(1, self.options.tmpdir, args))
  23. connect_nodes(self.nodes[1], 0)
  24. self.is_network_split = False
  25. self.sync_all()
  26. def run_test(self):
  27. # Start with a 200 block chain
  28. assert_equal(self.nodes[0].getblockcount(), 200)
  29. # Mine four blocks. After this, nodes[0] blocks
  30. # 101, 102, and 103 are spend-able.
  31. new_blocks = self.nodes[1].generate(4)
  32. self.sync_all()
  33. node0_address = self.nodes[0].getnewaddress()
  34. node1_address = self.nodes[1].getnewaddress()
  35. # Three scenarios for re-orging coinbase spends in the memory pool:
  36. # 1. Direct coinbase spend : spend_101
  37. # 2. Indirect (coinbase spend in chain, child in mempool) : spend_102 and spend_102_1
  38. # 3. Indirect (coinbase and child both in chain) : spend_103 and spend_103_1
  39. # Use invalidatblock to make all of the above coinbase spends invalid (immature coinbase),
  40. # and make sure the mempool code behaves correctly.
  41. b = [ self.nodes[0].getblockhash(n) for n in range(101, 105) ]
  42. coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ]
  43. spend_101_raw = create_tx(self.nodes[0], coinbase_txids[1], node1_address, 49.99)
  44. spend_102_raw = create_tx(self.nodes[0], coinbase_txids[2], node0_address, 49.99)
  45. spend_103_raw = create_tx(self.nodes[0], coinbase_txids[3], node0_address, 49.99)
  46. # Create a transaction which is time-locked to two blocks in the future
  47. timelock_tx = self.nodes[0].createrawtransaction([{"txid": coinbase_txids[0], "vout": 0}], {node0_address: 49.99})
  48. # Set the time lock
  49. timelock_tx = timelock_tx.replace("ffffffff", "11111191", 1)
  50. timelock_tx = timelock_tx[:-8] + hex(self.nodes[0].getblockcount() + 2)[2:] + "000000"
  51. timelock_tx = self.nodes[0].signrawtransaction(timelock_tx)["hex"]
  52. # This will raise an exception because the timelock transaction is too immature to spend
  53. assert_raises_jsonrpc(-26, "non-final", self.nodes[0].sendrawtransaction, timelock_tx)
  54. # Broadcast and mine spend_102 and 103:
  55. spend_102_id = self.nodes[0].sendrawtransaction(spend_102_raw)
  56. spend_103_id = self.nodes[0].sendrawtransaction(spend_103_raw)
  57. self.nodes[0].generate(1)
  58. # Time-locked transaction is still too immature to spend
  59. assert_raises_jsonrpc(-26,'non-final', self.nodes[0].sendrawtransaction, timelock_tx)
  60. # Create 102_1 and 103_1:
  61. spend_102_1_raw = create_tx(self.nodes[0], spend_102_id, node1_address, 49.98)
  62. spend_103_1_raw = create_tx(self.nodes[0], spend_103_id, node1_address, 49.98)
  63. # Broadcast and mine 103_1:
  64. spend_103_1_id = self.nodes[0].sendrawtransaction(spend_103_1_raw)
  65. last_block = self.nodes[0].generate(1)
  66. # Time-locked transaction can now be spent
  67. timelock_tx_id = self.nodes[0].sendrawtransaction(timelock_tx)
  68. # ... now put spend_101 and spend_102_1 in memory pools:
  69. spend_101_id = self.nodes[0].sendrawtransaction(spend_101_raw)
  70. spend_102_1_id = self.nodes[0].sendrawtransaction(spend_102_1_raw)
  71. self.sync_all()
  72. assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, timelock_tx_id})
  73. for node in self.nodes:
  74. node.invalidateblock(last_block[0])
  75. # Time-locked transaction is now too immature and has been removed from the mempool
  76. # spend_103_1 has been re-orged out of the chain and is back in the mempool
  77. assert_equal(set(self.nodes[0].getrawmempool()), {spend_101_id, spend_102_1_id, spend_103_1_id})
  78. # Use invalidateblock to re-org back and make all those coinbase spends
  79. # immature/invalid:
  80. for node in self.nodes:
  81. node.invalidateblock(new_blocks[0])
  82. self.sync_all()
  83. # mempool should be empty.
  84. assert_equal(set(self.nodes[0].getrawmempool()), set())
  85. if __name__ == '__main__':
  86. MempoolCoinbaseTest().main()