p2p_ibd_txrelay.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2020-present 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 transaction relay behavior during IBD:
   6  - Set fee filters to MAX_MONEY
   7  - Don't request transactions
   8  - Ignore all transaction messages
   9  """
  10  
  11  from decimal import Decimal
  12  import time
  13  
  14  from test_framework.blocktools import create_block
  15  from test_framework.messages import (
  16          CInv,
  17          COIN,
  18          CTransaction,
  19          from_hex,
  20          msg_inv,
  21          msg_tx,
  22          MSG_WTX,
  23  )
  24  from test_framework.p2p import (
  25          NONPREF_PEER_TX_DELAY,
  26          P2PDataStore,
  27          P2PInterface,
  28          p2p_lock
  29  )
  30  from test_framework.test_framework import BitcoinTestFramework
  31  
  32  MAX_FEE_FILTER = Decimal(9936506) / COIN
  33  NORMAL_FEE_FILTER = Decimal(10) / COIN
  34  
  35  
  36  class P2PIBDTxRelayTest(BitcoinTestFramework):
  37      def set_test_params(self):
  38          self.setup_clean_chain = True
  39          self.num_nodes = 2
  40          self.extra_args = [
  41              ["-minrelaytxfee={:.8f}".format(NORMAL_FEE_FILTER)],
  42              ["-minrelaytxfee={:.8f}".format(NORMAL_FEE_FILTER)],
  43          ]
  44  
  45      def run_test(self):
  46          self.log.info("Check that nodes set minfilter to MAX_MONEY while still in IBD")
  47          for node in self.nodes:
  48              assert node.getblockchaininfo()['initialblockdownload']
  49              self.wait_until(lambda: all(peer['minfeefilter'] == MAX_FEE_FILTER for peer in node.getpeerinfo()))
  50  
  51          self.nodes[0].setmocktime(int(time.time()))
  52          self.log.info("Mine one old block so we stay in IBD, then remember its coinbase wtxid")
  53          block = create_block(int(self.nodes[0].getbestblockhash(), 16), height=1, ntime=int(time.time()) - 2 * 24 * 60 * 60)
  54          block.solve()
  55          self.nodes[0].submitblock(block.serialize().hex())
  56          assert self.nodes[0].getblockchaininfo()['initialblockdownload']
  57          ibd_wtxid = int(self.nodes[0].getblock(f"{block.hash_int:064x}", 2)["tx"][0]["hash"], 16)
  58  
  59          self.log.info("Check that nodes don't send getdatas for transactions while still in IBD")
  60          peer_inver = self.nodes[0].add_p2p_connection(P2PDataStore())
  61          txid = 0xdeadbeef
  62          peer_inver.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=txid)]))
  63          # The node should not send a getdata, but if it did, it would first delay 2 seconds
  64          self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
  65          peer_inver.sync_with_ping()
  66          with p2p_lock:
  67              assert txid not in peer_inver.getdata_requests
  68          self.nodes[0].disconnect_p2ps()
  69  
  70          self.log.info("Check that nodes don't process unsolicited transactions while still in IBD")
  71          # A transaction hex pulled from tx_valid.json. There are no valid transactions since no UTXOs
  72          # exist yet, but it should be a well-formed transaction.
  73          rawhex = "0100000001b14bdcbc3e01bdaad36cc08e81e69c82e1060bc14e518db2b49aa43ad90ba260000000004a01ff473" + \
  74              "04402203f16c6f40162ab686621ef3000b04e75418a0c0cb2d8aebeac894ae360ac1e780220ddc15ecdfc3507ac48e168" + \
  75              "1a33eb60996631bf6bf5bc0a0682c4db743ce7ca2b01ffffffff0140420f00000000001976a914660d4ef3a743e3e696a" + \
  76              "d990364e555c271ad504b88ac00000000"
  77          assert self.nodes[1].decoderawtransaction(rawhex) # returns a dict, should not throw
  78          tx = from_hex(CTransaction(), rawhex)
  79          peer_txer = self.nodes[0].add_p2p_connection(P2PInterface())
  80          with self.nodes[0].assert_debug_log(expected_msgs=["received: tx"], unexpected_msgs=["was not accepted"]):
  81              peer_txer.send_and_ping(msg_tx(tx))
  82          self.nodes[0].disconnect_p2ps()
  83  
  84          # Come out of IBD by generating a block
  85          self.generate(self.nodes[0], 1)
  86  
  87          self.log.info("Check that nodes reset minfilter after coming out of IBD")
  88          for node in self.nodes:
  89              assert not node.getblockchaininfo()['initialblockdownload']
  90              self.wait_until(lambda: all(peer['minfeefilter'] == NORMAL_FEE_FILTER for peer in node.getpeerinfo()))
  91  
  92          self.log.info("Check that txs confirmed during IBD are not in the recently-confirmed filter once out of ibd")
  93          peer_inver = self.nodes[0].add_p2p_connection(P2PDataStore())
  94          peer_inver.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=ibd_wtxid)]))
  95          self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
  96          peer_inver.wait_for_getdata([ibd_wtxid])
  97          self.nodes[0].disconnect_p2ps()
  98  
  99          self.log.info("Check that nodes process the same transaction, even when unsolicited, when no longer in IBD")
 100          peer_txer = self.nodes[0].add_p2p_connection(P2PInterface())
 101          with self.nodes[0].assert_debug_log(expected_msgs=["was not accepted"]):
 102              peer_txer.send_and_ping(msg_tx(tx))
 103  
 104  if __name__ == '__main__':
 105      P2PIBDTxRelayTest(__file__).main()
 106