mempool_reorg.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2014-2022 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 mempool re-org scenarios.
   6  
   7  Test re-org scenarios with a mempool that contains transactions
   8  that spend (directly or indirectly) coinbase transactions.
   9  """
  10  
  11  import time
  12  
  13  from test_framework.messages import (
  14      CInv,
  15      MSG_WTX,
  16      msg_getdata,
  17  )
  18  from test_framework.p2p import (
  19      P2PTxInvStore,
  20      p2p_lock,
  21  )
  22  from test_framework.test_framework import LimenkaTestFramework
  23  from test_framework.util import assert_equal, assert_raises_rpc_error
  24  from test_framework.wallet import MiniWallet
  25  
  26  class MempoolCoinbaseTest(LimenkaTestFramework):
  27      def set_test_params(self):
  28          self.num_nodes = 2
  29          self.extra_args = [
  30              [
  31                  '-whitelist=noban@127.0.0.1',  # immediate tx relay
  32              ],
  33              []
  34          ]
  35  
  36      def test_reorg_relay(self):
  37          self.log.info("Test that transactions from disconnected blocks are available for relay immediately")
  38          # Prevent time from moving forward
  39          self.nodes[1].setmocktime(int(time.time()))
  40          self.connect_nodes(0, 1)
  41          self.generate(self.wallet, 3)
  42  
  43          # Disconnect node0 and node1 to create different chains.
  44          self.disconnect_nodes(0, 1)
  45          # Connect a peer to node1, which doesn't have immediate tx relay
  46          peer1 = self.nodes[1].add_p2p_connection(P2PTxInvStore())
  47  
  48          # Create a transaction that is included in a block.
  49          tx_disconnected = self.wallet.send_self_transfer(from_node=self.nodes[1])
  50          self.generate(self.nodes[1], 1, sync_fun=self.no_op)
  51  
  52          # Create a transaction and submit it to node1's mempool.
  53          tx_before_reorg = self.wallet.send_self_transfer(from_node=self.nodes[1])
  54  
  55          # Create a child of that transaction and submit it to node1's mempool.
  56          tx_child = self.wallet.send_self_transfer(utxo_to_spend=tx_disconnected["new_utxo"], from_node=self.nodes[1])
  57          assert_equal(self.nodes[1].getmempoolentry(tx_child["txid"])["ancestorcount"], 1)
  58          assert_equal(len(peer1.get_invs()), 0)
  59  
  60          # node0 has a longer chain in which tx_disconnected was not confirmed.
  61          self.generate(self.nodes[0], 3, sync_fun=self.no_op)
  62  
  63          # Reconnect the nodes and sync chains. node0's chain should win.
  64          self.connect_nodes(0, 1)
  65          self.sync_blocks()
  66  
  67          # Child now has an ancestor from the disconnected block
  68          assert_equal(self.nodes[1].getmempoolentry(tx_child["txid"])["ancestorcount"], 2)
  69          assert_equal(self.nodes[1].getmempoolentry(tx_before_reorg["txid"])["ancestorcount"], 1)
  70  
  71          # peer1 should not have received an inv for any of the transactions during this time, as no
  72          # mocktime has elapsed for those transactions to be announced. Likewise, it cannot
  73          # request very recent, unanounced transactions.
  74          assert_equal(len(peer1.get_invs()), 0)
  75          # It's too early to request these two transactions
  76          requests_too_recent = msg_getdata([CInv(t=MSG_WTX, h=int(tx["tx"].getwtxid(), 16)) for tx in [tx_before_reorg, tx_child]])
  77          peer1.send_and_ping(requests_too_recent)
  78          for _ in range(len(requests_too_recent.inv)):
  79              peer1.sync_with_ping()
  80          with p2p_lock:
  81              assert "tx" not in peer1.last_message
  82              assert "notfound" in peer1.last_message
  83  
  84          # Request the tx from the disconnected block
  85          request_disconnected_tx = msg_getdata([CInv(t=MSG_WTX, h=int(tx_disconnected["tx"].getwtxid(), 16))])
  86          peer1.send_and_ping(request_disconnected_tx)
  87  
  88          # The tx from the disconnected block was never announced, and it entered the mempool later
  89          # than the transactions that are too recent.
  90          assert_equal(len(peer1.get_invs()), 0)
  91          with p2p_lock:
  92              # However, the node will answer requests for the tx from the recently-disconnected block.
  93              assert_equal(peer1.last_message["tx"].tx.getwtxid(),tx_disconnected["tx"].getwtxid())
  94  
  95          self.nodes[1].setmocktime(int(time.time()) + 300)
  96          peer1.sync_with_ping()
  97          # the transactions are now announced
  98          assert_equal(len(peer1.get_invs()), 3)
  99          for _ in range(3):
 100              # make sure all tx requests have been responded to
 101              peer1.sync_with_ping()
 102          last_tx_received = peer1.last_message["tx"]
 103  
 104          tx_after_reorg = self.wallet.send_self_transfer(from_node=self.nodes[1])
 105          request_after_reorg = msg_getdata([CInv(t=MSG_WTX, h=int(tx_after_reorg["tx"].getwtxid(), 16))])
 106          assert tx_after_reorg["txid"] in self.nodes[1].getrawmempool()
 107          peer1.send_and_ping(request_after_reorg)
 108          with p2p_lock:
 109              assert_equal(peer1.last_message["tx"], last_tx_received)
 110  
 111      def run_test(self):
 112          self.wallet = MiniWallet(self.nodes[0])
 113          wallet = self.wallet
 114  
 115          # Start with a 200 block chain
 116          assert_equal(self.nodes[0].getblockcount(), 200)
 117  
 118          self.log.info("Add 4 coinbase utxos to the miniwallet")
 119          # Block 76 contains the first spendable coinbase txs.
 120          first_block = 76
 121  
 122          # Three scenarios for re-orging coinbase spends in the memory pool:
 123          # 1. Direct coinbase spend  :  spend_1
 124          # 2. Indirect (coinbase spend in chain, child in mempool) : spend_2 and spend_2_1
 125          # 3. Indirect (coinbase and child both in chain) : spend_3 and spend_3_1
 126          # Use invalidateblock to make all of the above coinbase spends invalid (immature coinbase),
 127          # and make sure the mempool code behaves correctly.
 128          b = [self.nodes[0].getblockhash(n) for n in range(first_block, first_block+4)]
 129          coinbase_txids = [self.nodes[0].getblock(h)['tx'][0] for h in b]
 130          utxo_1 = wallet.get_utxo(txid=coinbase_txids[1])
 131          utxo_2 = wallet.get_utxo(txid=coinbase_txids[2])
 132          utxo_3 = wallet.get_utxo(txid=coinbase_txids[3])
 133          self.log.info("Create three transactions spending from coinbase utxos: spend_1, spend_2, spend_3")
 134          spend_1 = wallet.create_self_transfer(utxo_to_spend=utxo_1)
 135          spend_2 = wallet.create_self_transfer(utxo_to_spend=utxo_2)
 136          spend_3 = wallet.create_self_transfer(utxo_to_spend=utxo_3)
 137  
 138          self.log.info("Create another transaction which is time-locked to two blocks in the future")
 139          utxo = wallet.get_utxo(txid=coinbase_txids[0])
 140          timelock_tx = wallet.create_self_transfer(
 141              utxo_to_spend=utxo,
 142              locktime=self.nodes[0].getblockcount() + 2,
 143          )['hex']
 144  
 145          self.log.info("Check that the time-locked transaction is too immature to spend")
 146          assert_raises_rpc_error(-26, "non-final", self.nodes[0].sendrawtransaction, timelock_tx)
 147  
 148          self.log.info("Broadcast and mine spend_2 and spend_3")
 149          wallet.sendrawtransaction(from_node=self.nodes[0], tx_hex=spend_2['hex'])
 150          wallet.sendrawtransaction(from_node=self.nodes[0], tx_hex=spend_3['hex'])
 151          self.log.info("Generate a block")
 152          self.generate(self.nodes[0], 1)
 153          self.log.info("Check that time-locked transaction is still too immature to spend")
 154          assert_raises_rpc_error(-26, 'non-final', self.nodes[0].sendrawtransaction, timelock_tx)
 155  
 156          self.log.info("Create spend_2_1 and spend_3_1")
 157          spend_2_1 = wallet.create_self_transfer(utxo_to_spend=spend_2["new_utxo"])
 158          spend_3_1 = wallet.create_self_transfer(utxo_to_spend=spend_3["new_utxo"])
 159  
 160          self.log.info("Broadcast and mine spend_3_1")
 161          spend_3_1_id = self.nodes[0].sendrawtransaction(spend_3_1['hex'])
 162          self.log.info("Generate a block")
 163          last_block = self.generate(self.nodes[0], 1)
 164          # generate() implicitly syncs blocks, so that peer 1 gets the block before timelock_tx
 165          # Otherwise, peer 1 would put the timelock_tx in m_lazy_recent_rejects
 166  
 167          self.log.info("The time-locked transaction can now be spent")
 168          timelock_tx_id = self.nodes[0].sendrawtransaction(timelock_tx)
 169  
 170          self.log.info("Add spend_1 and spend_2_1 to the mempool")
 171          spend_1_id = self.nodes[0].sendrawtransaction(spend_1['hex'])
 172          spend_2_1_id = self.nodes[0].sendrawtransaction(spend_2_1['hex'])
 173  
 174          assert_equal(set(self.nodes[0].getrawmempool()), {spend_1_id, spend_2_1_id, timelock_tx_id})
 175          self.sync_all()
 176  
 177          self.log.info("invalidate the last block")
 178          for node in self.nodes:
 179              node.invalidateblock(last_block[0])
 180          self.log.info("The time-locked transaction is now too immature and has been removed from the mempool")
 181          self.log.info("spend_3_1 has been re-orged out of the chain and is back in the mempool")
 182          assert_equal(set(self.nodes[0].getrawmempool()), {spend_1_id, spend_2_1_id, spend_3_1_id})
 183  
 184          self.log.info("Use invalidateblock to re-org back and make all those coinbase spends immature/invalid")
 185          b = self.nodes[0].getblockhash(first_block + 100)
 186          for node in self.nodes:
 187              node.invalidateblock(b)
 188  
 189          self.log.info("Check that the mempool is empty")
 190          assert_equal(set(self.nodes[0].getrawmempool()), set())
 191          self.sync_all()
 192  
 193          self.test_reorg_relay()
 194  
 195  
 196  if __name__ == '__main__':
 197      MempoolCoinbaseTest(__file__).main()
 198