mempool_reorg.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2014-2022 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 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 BitcoinTestFramework 23 from test_framework.util import assert_equal, assert_raises_rpc_error 24 from test_framework.wallet import MiniWallet 25 from test_framework.blocktools import ( 26 create_empty_fork, 27 ) 28 29 # Number of blocks to create in temporary blockchain branch for reorg testing 30 # needs to be long enough to allow MTP to move arbitrarily forward 31 FORK_LENGTH = 20 32 33 class MempoolCoinbaseTest(BitcoinTestFramework): 34 def set_test_params(self): 35 self.num_nodes = 2 36 self.extra_args = [ 37 [ 38 '-whitelist=noban@127.0.0.1', # immediate tx relay 39 ], 40 [] 41 ] 42 43 def trigger_reorg(self, fork_blocks, node): 44 """Trigger reorg of the fork blocks.""" 45 for block in fork_blocks: 46 node.submitblock(block.serialize().hex()) 47 assert_equal(self.nodes[0].getbestblockhash(), fork_blocks[-1].hash_hex) 48 49 def test_reorg_relay(self): 50 self.log.info("Test that transactions from disconnected blocks are available for relay immediately") 51 # Prevent time from moving forward 52 self.nodes[1].setmocktime(int(time.time())) 53 self.connect_nodes(0, 1) 54 self.generate(self.wallet, 3) 55 56 # Disconnect node0 and node1 to create different chains. 57 self.disconnect_nodes(0, 1) 58 # Connect a peer to node1, which doesn't have immediate tx relay 59 peer1 = self.nodes[1].add_p2p_connection(P2PTxInvStore()) 60 61 # Create a transaction that is included in a block. 62 tx_disconnected = self.wallet.send_self_transfer(from_node=self.nodes[1]) 63 self.generate(self.nodes[1], 1, sync_fun=self.no_op) 64 65 # Create a transaction and submit it to node1's mempool. 66 tx_before_reorg = self.wallet.send_self_transfer(from_node=self.nodes[1]) 67 68 # Create a child of that transaction and submit it to node1's mempool. 69 tx_child = self.wallet.send_self_transfer(utxo_to_spend=tx_disconnected["new_utxo"], from_node=self.nodes[1]) 70 assert_equal(self.nodes[1].getmempoolentry(tx_child["txid"])["ancestorcount"], 1) 71 assert_equal(len(peer1.get_invs()), 0) 72 73 # node0 has a longer chain in which tx_disconnected was not confirmed. 74 self.generate(self.nodes[0], 3, sync_fun=self.no_op) 75 76 # Reconnect the nodes and sync chains. node0's chain should win. 77 self.connect_nodes(0, 1) 78 self.sync_blocks() 79 80 # Child now has an ancestor from the disconnected block 81 assert_equal(self.nodes[1].getmempoolentry(tx_child["txid"])["ancestorcount"], 2) 82 assert_equal(self.nodes[1].getmempoolentry(tx_before_reorg["txid"])["ancestorcount"], 1) 83 84 # peer1 should not have received an inv for any of the transactions during this time, as no 85 # mocktime has elapsed for those transactions to be announced. Likewise, it cannot 86 # request very recent, unanounced transactions. 87 assert_equal(len(peer1.get_invs()), 0) 88 # It's too early to request these two transactions 89 requests_too_recent = msg_getdata([CInv(t=MSG_WTX, h=tx["tx"].wtxid_int) for tx in [tx_before_reorg, tx_child]]) 90 peer1.send_and_ping(requests_too_recent) 91 for _ in range(len(requests_too_recent.inv)): 92 peer1.sync_with_ping() 93 with p2p_lock: 94 assert "tx" not in peer1.last_message 95 assert "notfound" in peer1.last_message 96 97 # Request the tx from the disconnected block 98 request_disconnected_tx = msg_getdata([CInv(t=MSG_WTX, h=tx_disconnected["tx"].wtxid_int)]) 99 peer1.send_and_ping(request_disconnected_tx) 100 101 # The tx from the disconnected block was never announced, and it entered the mempool later 102 # than the transactions that are too recent. 103 assert_equal(len(peer1.get_invs()), 0) 104 with p2p_lock: 105 # However, the node will answer requests for the tx from the recently-disconnected block. 106 assert_equal(peer1.last_message["tx"].tx.wtxid_hex,tx_disconnected["tx"].wtxid_hex) 107 108 self.nodes[1].setmocktime(int(time.time()) + 300) 109 peer1.sync_with_ping() 110 # the transactions are now announced 111 assert_equal(len(peer1.get_invs()), 3) 112 for _ in range(3): 113 # make sure all tx requests have been responded to 114 peer1.sync_with_ping() 115 last_tx_received = peer1.last_message["tx"] 116 117 tx_after_reorg = self.wallet.send_self_transfer(from_node=self.nodes[1]) 118 request_after_reorg = msg_getdata([CInv(t=MSG_WTX, h=tx_after_reorg["tx"].wtxid_int)]) 119 assert tx_after_reorg["txid"] in self.nodes[1].getrawmempool() 120 peer1.send_and_ping(request_after_reorg) 121 with p2p_lock: 122 assert_equal(peer1.last_message["tx"], last_tx_received) 123 124 def run_test(self): 125 self.wallet = MiniWallet(self.nodes[0]) 126 wallet = self.wallet 127 128 # Prevent clock from moving blocks further forward in time 129 now = int(time.time()) 130 self.nodes[0].setmocktime(now) 131 132 # Start with a 200 block chain 133 assert_equal(self.nodes[0].getblockcount(), 200) 134 135 self.log.info("Add 4 coinbase utxos to the miniwallet") 136 # Block 76 contains the first spendable coinbase txs. 137 first_block = 76 138 139 # Three scenarios for re-orging coinbase spends in the memory pool: 140 # 1. Direct coinbase spend : spend_1 141 # 2. Indirect (coinbase spend in chain, child in mempool) : spend_2 and spend_2_1 142 # 3. Indirect (coinbase and child both in chain) : spend_3 and spend_3_1 143 # Use re-org to make all of the above coinbase spends invalid (immature coinbase), 144 # and make sure the mempool code behaves correctly. 145 b = [self.nodes[0].getblockhash(n) for n in range(first_block, first_block+4)] 146 coinbase_txids = [self.nodes[0].getblock(h)['tx'][0] for h in b] 147 utxo_1 = wallet.get_utxo(txid=coinbase_txids[1]) 148 utxo_2 = wallet.get_utxo(txid=coinbase_txids[2]) 149 utxo_3 = wallet.get_utxo(txid=coinbase_txids[3]) 150 self.log.info("Create three transactions spending from coinbase utxos: spend_1, spend_2, spend_3") 151 spend_1 = wallet.create_self_transfer(utxo_to_spend=utxo_1) 152 spend_2 = wallet.create_self_transfer(utxo_to_spend=utxo_2) 153 spend_3 = wallet.create_self_transfer(utxo_to_spend=utxo_3) 154 155 self.log.info("Create another transaction which is time-locked to 300 seconds in the future") 156 future = now + 300 157 utxo = wallet.get_utxo(txid=coinbase_txids[0]) 158 timelock_tx = wallet.create_self_transfer( 159 utxo_to_spend=utxo, 160 locktime=future, 161 )['hex'] 162 163 self.log.info("Check that the time-locked transaction is too immature to spend") 164 assert_raises_rpc_error(-26, "non-final", self.nodes[0].sendrawtransaction, timelock_tx) 165 166 self.log.info("Broadcast and mine spend_2 and spend_3") 167 spend_2_id = wallet.sendrawtransaction(from_node=self.nodes[0], tx_hex=spend_2['hex']) 168 wallet.sendrawtransaction(from_node=self.nodes[0], tx_hex=spend_3['hex']) 169 self.log.info("Generate a block") 170 self.generate(self.nodes[0], 1) 171 self.log.info("Check that time-locked transaction is still too immature to spend") 172 assert_raises_rpc_error(-26, 'non-final', self.nodes[0].sendrawtransaction, timelock_tx) 173 174 self.log.info("Create spend_2_1 and spend_3_1") 175 spend_2_1 = wallet.create_self_transfer(utxo_to_spend=spend_2["new_utxo"], version=1) 176 spend_3_1 = wallet.create_self_transfer(utxo_to_spend=spend_3["new_utxo"]) 177 178 self.log.info("Broadcast and mine spend_3_1") 179 spend_3_1_id = self.nodes[0].sendrawtransaction(spend_3_1['hex']) 180 self.log.info("Generate a block") 181 182 # Prep for fork, only go FORK_LENGTH seconds into the MTP future max 183 fork_blocks = create_empty_fork(self.nodes[0], fork_length=FORK_LENGTH) 184 185 # Jump node and MTP 300 seconds and generate a slightly weaker chain than reorg one 186 self.nodes[0].setmocktime(future) 187 self.generate(self.nodes[0], FORK_LENGTH - 1) 188 block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] 189 assert(block_time >= now + 300) 190 191 # generate() implicitly syncs blocks, so that peer 1 gets the block before timelock_tx 192 # Otherwise, peer 1 would put the timelock_tx in m_lazy_recent_rejects 193 self.log.info("The time-locked transaction can now be spent") 194 timelock_tx_id = self.nodes[0].sendrawtransaction(timelock_tx) 195 196 self.log.info("Add spend_1 and spend_2_1 to the mempool") 197 spend_1_id = self.nodes[0].sendrawtransaction(spend_1['hex']) 198 spend_2_1_id = self.nodes[0].sendrawtransaction(spend_2_1['hex']) 199 200 assert_equal(set(self.nodes[0].getrawmempool()), {spend_1_id, spend_2_1_id, timelock_tx_id}) 201 self.sync_all() 202 203 self.trigger_reorg(fork_blocks, self.nodes[0]) 204 self.sync_blocks() 205 206 # We went backwards in time to boot timelock_tx_id 207 fork_block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] 208 assert fork_block_time < block_time 209 210 self.log.info("The time-locked transaction is now too immature and has been removed from the mempool") 211 self.log.info("spend_3_1 has been re-orged out of the chain and is back in the mempool") 212 assert_equal(set(self.nodes[0].getrawmempool()), {spend_1_id, spend_2_1_id, spend_3_1_id}) 213 214 self.log.info("Reorg out enough blocks to get spend_2 back in the mempool, along with its child") 215 216 while (spend_2_id not in self.nodes[0].getrawmempool()): 217 b = self.nodes[0].getbestblockhash() 218 for node in self.nodes: 219 node.invalidateblock(b) 220 221 assert(spend_2_id in self.nodes[0].getrawmempool()) 222 assert(spend_2_1_id in self.nodes[0].getrawmempool()) 223 224 # Chain 10 more transactions off of spend_2_1 225 self.log.info("Give spend_2 some more descendants by creating a chain of 10 transactions spending from it") 226 parent_utxo = spend_2_1["new_utxo"] 227 for i in range(10): 228 tx = wallet.create_self_transfer(utxo_to_spend=parent_utxo, version=1) 229 self.nodes[0].sendrawtransaction(tx['hex']) 230 parent_utxo = tx["new_utxo"] 231 232 self.log.info("Use invalidateblock to re-org back and make all those coinbase spends immature/invalid") 233 b = self.nodes[0].getblockhash(first_block + 100) 234 235 # Use invalidateblock to go backwards in MTP time. 236 # invalidateblock actually moves MTP backwards, making timelock_tx_id valid again. 237 for node in self.nodes: 238 node.invalidateblock(b) 239 240 self.log.info("Check that the mempool is empty") 241 assert_equal(set(self.nodes[0].getrawmempool()), set()) 242 self.sync_all() 243 244 self.test_reorg_relay() 245 246 247 if __name__ == '__main__': 248 MempoolCoinbaseTest(__file__).main()