Skip to content

Commit af2a145

Browse files
committed
Refactor resource exhaustion test
This is a simple refactor of the specified test. It is now brought in line with the rest of the tests in the module. This should make things easier to debug, as all of the tests are now grouped together at the top.
1 parent 5c4648d commit af2a145

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,47 +46,12 @@ def set_test_params(self):
4646
self.setup_clean_chain = True
4747

4848
def run_test(self):
49-
"""
50-
. Test msg header
51-
0. Send a bunch of large (4MB) messages of an unrecognized type. Check to see
52-
that it isn't an effective DoS against the node.
53-
"""
5449
self.test_magic_bytes()
5550
self.test_checksum()
5651
self.test_size()
5752
self.test_msgtype()
5853
self.test_large_inv()
59-
60-
node = self.nodes[0]
61-
self.node = node
62-
node.add_p2p_connection(P2PDataStore())
63-
conn2 = node.add_p2p_connection(P2PDataStore())
64-
65-
66-
#
67-
# 0.
68-
#
69-
# Send as large a message as is valid, ensure we aren't disconnected but
70-
# also can't exhaust resources.
71-
#
72-
msg_at_size = msg_unrecognized(str_data="b" * VALID_DATA_LIMIT)
73-
assert len(msg_at_size.serialize()) == MSG_LIMIT
74-
75-
self.log.info("Sending a bunch of large, junk messages to test memory exhaustion. May take a bit...")
76-
77-
# Run a bunch of times to test for memory exhaustion.
78-
for _ in range(80):
79-
node.p2p.send_message(msg_at_size)
80-
81-
# Check that, even though the node is being hammered by nonsense from one
82-
# connection, it can still service other peers in a timely way.
83-
for _ in range(20):
84-
conn2.sync_with_ping(timeout=2)
85-
86-
# Peer 1, despite serving up a bunch of nonsense, should still be connected.
87-
self.log.info("Waiting for node to drop junk messages.")
88-
node.p2p.sync_with_ping(timeout=400)
89-
assert node.p2p.is_connected
54+
self.test_resource_exhaustion()
9055

9156
def test_magic_bytes(self):
9257
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
@@ -154,6 +119,29 @@ def test_large_inv(self):
154119
conn.send_and_ping(msg)
155120
self.nodes[0].disconnect_p2ps()
156121

122+
def test_resource_exhaustion(self):
123+
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
124+
conn2 = self.nodes[0].add_p2p_connection(P2PDataStore())
125+
msg_at_size = msg_unrecognized(str_data="b" * VALID_DATA_LIMIT)
126+
assert len(msg_at_size.serialize()) == MSG_LIMIT
127+
128+
self.log.info("Sending a bunch of large, junk messages to test memory exhaustion. May take a bit...")
129+
130+
# Run a bunch of times to test for memory exhaustion.
131+
for _ in range(80):
132+
conn.send_message(msg_at_size)
133+
134+
# Check that, even though the node is being hammered by nonsense from one
135+
# connection, it can still service other peers in a timely way.
136+
for _ in range(20):
137+
conn2.sync_with_ping(timeout=2)
138+
139+
# Peer 1, despite being served up a bunch of nonsense, should still be connected.
140+
self.log.info("Waiting for node to drop junk messages.")
141+
conn.sync_with_ping(timeout=400)
142+
assert conn.is_connected
143+
self.nodes[0].disconnect_p2ps()
144+
157145

158146
if __name__ == '__main__':
159147
InvalidMessagesTest().main()

0 commit comments

Comments
 (0)