p2p_nobloomfilter_messages.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2015-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 invalid p2p messages for nodes with bloom filters disabled.
   6  
   7  Test that, when bloom filters are not enabled, peers are disconnected if:
   8  1. They send a p2p mempool message
   9  2. They send a p2p filterload message
  10  3. They send a p2p filteradd message
  11  4. They send a p2p filterclear message
  12  """
  13  
  14  from test_framework.messages import msg_mempool, msg_filteradd, msg_filterload, msg_filterclear
  15  from test_framework.p2p import P2PInterface
  16  from test_framework.test_framework import BitcoinTestFramework
  17  from test_framework.util import assert_equal
  18  
  19  
  20  class P2PNoBloomFilterMessages(BitcoinTestFramework):
  21      def set_test_params(self):
  22          self.setup_clean_chain = True
  23          self.num_nodes = 1
  24          self.extra_args = [["-peerbloomfilters=0"]]
  25  
  26      def test_message_causes_disconnect(self, message):
  27          """Add a p2p connection that sends a message and check that it disconnects."""
  28          peer = self.nodes[0].add_p2p_connection(P2PInterface())
  29          peer.send_without_ping(message)
  30          peer.wait_for_disconnect()
  31          assert_equal(self.nodes[0].getconnectioncount(), 0)
  32  
  33      def run_test(self):
  34          self.log.info("Test that peer is disconnected if it sends mempool message")
  35          self.test_message_causes_disconnect(msg_mempool())
  36  
  37          self.log.info("Test that peer is disconnected if it sends filterload message")
  38          self.test_message_causes_disconnect(msg_filterload())
  39  
  40          self.log.info("Test that peer is disconnected if it sends filteradd message")
  41          self.test_message_causes_disconnect(msg_filteradd(data=b'\xcc'))
  42  
  43          self.log.info("Test that peer is disconnected if it sends a filterclear message")
  44          self.test_message_causes_disconnect(msg_filterclear())
  45  
  46  
  47  if __name__ == '__main__':
  48      P2PNoBloomFilterMessages(__file__).main()
  49