mempool_resurrect.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2014-2021 The Limenka 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  
   7  from test_framework.test_framework import LimenkaTestFramework
   8  from test_framework.util import assert_equal
   9  from test_framework.wallet import MiniWallet
  10  
  11  
  12  class MempoolCoinbaseTest(LimenkaTestFramework):
  13      def set_test_params(self):
  14          self.num_nodes = 1
  15  
  16      def run_test(self):
  17          node = self.nodes[0]
  18          wallet = MiniWallet(node)
  19  
  20          # Spend block 1/2/3's coinbase transactions
  21          # Mine a block
  22          # Create three more transactions, spending the spends
  23          # Mine another block
  24          # ... make sure all the transactions are confirmed
  25          # Invalidate both blocks
  26          # ... make sure all the transactions are put back in the mempool
  27          # Mine a new block
  28          # ... make sure all the transactions are confirmed again
  29          blocks = []
  30          spends1_ids = [wallet.send_self_transfer(from_node=node)['txid'] for _ in range(3)]
  31          blocks.extend(self.generate(node, 1))
  32          spends2_ids = [wallet.send_self_transfer(from_node=node)['txid'] for _ in range(3)]
  33          blocks.extend(self.generate(node, 1))
  34  
  35          spends_ids = set(spends1_ids + spends2_ids)
  36  
  37          # mempool should be empty, all txns confirmed
  38          assert_equal(set(node.getrawmempool()), set())
  39          confirmed_txns = set(node.getblock(blocks[0])['tx'] + node.getblock(blocks[1])['tx'])
  40          # Checks that all spend txns are contained in the mined blocks
  41          assert spends_ids < confirmed_txns
  42  
  43          # Use invalidateblock to re-org back
  44          node.invalidateblock(blocks[0])
  45  
  46          # All txns should be back in mempool with 0 confirmations
  47          assert_equal(set(node.getrawmempool()), spends_ids)
  48  
  49          # Generate another block, they should all get mined
  50          blocks = self.generate(node, 1)
  51          # mempool should be empty, all txns confirmed
  52          assert_equal(set(node.getrawmempool()), set())
  53          confirmed_txns = set(node.getblock(blocks[0])['tx'])
  54          assert spends_ids < confirmed_txns
  55  
  56  
  57  if __name__ == '__main__':
  58      MempoolCoinbaseTest(__file__).main()
  59