@@ -37,47 +37,12 @@ def set_test_params(self):
3737 self .setup_clean_chain = True
3838
3939 def run_test (self ):
40- """
41- . Test msg header
42- 0. Send a bunch of large (4MB) messages of an unrecognized type. Check to see
43- that it isn't an effective DoS against the node.
44- """
4540 self .test_magic_bytes ()
4641 self .test_checksum ()
4742 self .test_size ()
4843 self .test_command ()
4944 self .test_large_inv ()
50-
51- node = self .nodes [0 ]
52- self .node = node
53- node .add_p2p_connection (P2PDataStore ())
54- conn2 = node .add_p2p_connection (P2PDataStore ())
55-
56-
57- #
58- # 0.
59- #
60- # Send as large a message as is valid, ensure we aren't disconnected but
61- # also can't exhaust resources.
62- #
63- msg_at_size = msg_unrecognized (str_data = "b" * VALID_DATA_LIMIT )
64- assert len (msg_at_size .serialize ()) == MSG_LIMIT
65-
66- self .log .info ("Sending a bunch of large, junk messages to test memory exhaustion. May take a bit..." )
67-
68- # Run a bunch of times to test for memory exhaustion.
69- for _ in range (80 ):
70- node .p2p .send_message (msg_at_size )
71-
72- # Check that, even though the node is being hammered by nonsense from one
73- # connection, it can still service other peers in a timely way.
74- for _ in range (20 ):
75- conn2 .sync_with_ping (timeout = 2 )
76-
77- # Peer 1, despite serving up a bunch of nonsense, should still be connected.
78- self .log .info ("Waiting for node to drop junk messages." )
79- node .p2p .sync_with_ping (timeout = 320 )
80- assert node .p2p .is_connected
45+ self .test_resource_exhaustion ()
8146
8247 def test_magic_bytes (self ):
8348 conn = self .nodes [0 ].add_p2p_connection (P2PDataStore ())
@@ -141,6 +106,29 @@ def test_large_inv(self): # future: add Misbehaving value check, first invalid m
141106 conn .send_and_ping (msg )
142107 self .nodes [0 ].disconnect_p2ps ()
143108
109+ def test_resource_exhaustion (self ):
110+ conn = self .nodes [0 ].add_p2p_connection (P2PDataStore ())
111+ conn2 = self .nodes [0 ].add_p2p_connection (P2PDataStore ())
112+ msg_at_size = msg_unrecognized (str_data = "b" * VALID_DATA_LIMIT )
113+ assert len (msg_at_size .serialize ()) == MSG_LIMIT
114+
115+ self .log .info ("Sending a bunch of large, junk messages to test memory exhaustion. May take a bit..." )
116+
117+ # Run a bunch of times to test for memory exhaustion.
118+ for _ in range (80 ):
119+ conn .send_message (msg_at_size )
120+
121+ # Check that, even though the node is being hammered by nonsense from one
122+ # connection, it can still service other peers in a timely way.
123+ for _ in range (20 ):
124+ conn2 .sync_with_ping (timeout = 2 )
125+
126+ # Peer 1, despite being served up a bunch of nonsense, should still be connected.
127+ self .log .info ("Waiting for node to drop junk messages." )
128+ conn .sync_with_ping (timeout = 400 )
129+ assert conn .is_connected
130+ self .nodes [0 ].disconnect_p2ps ()
131+
144132
145133if __name__ == '__main__' :
146134 InvalidMessagesTest ().main ()
0 commit comments