Skip to content

Commit 56010f9

Browse files
committed
test: hoist p2p values to test framework constants
1 parent 75447f0 commit 56010f9

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

test/functional/p2p_invalid_messages.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from test_framework.messages import (
88
CBlockHeader,
99
CInv,
10+
MAX_HEADERS_RESULTS,
11+
MAX_INV_SIZE,
12+
MAX_PROTOCOL_MESSAGE_LENGTH,
1013
msg_getdata,
1114
msg_headers,
1215
msg_inv,
@@ -24,8 +27,7 @@
2427
wait_until,
2528
)
2629

27-
MSG_LIMIT = 4 * 1000 * 1000 # 4MB, per MAX_PROTOCOL_MESSAGE_LENGTH
28-
VALID_DATA_LIMIT = MSG_LIMIT - 5 # Account for the 5-byte length prefix
30+
VALID_DATA_LIMIT = MAX_PROTOCOL_MESSAGE_LENGTH - 5 # Account for the 5-byte length prefix
2931

3032
class msg_unrecognized:
3133
"""Nonsensical message. Modeled after similar types in test_framework.messages."""
@@ -132,20 +134,23 @@ def test_oversized_msg(self, msg, size):
132134
self.nodes[0].disconnect_p2ps()
133135

134136
def test_oversized_inv_msg(self):
135-
self.test_oversized_msg(msg_inv([CInv(MSG_TX, 1)] * 50001), 50001)
137+
size = MAX_INV_SIZE + 1
138+
self.test_oversized_msg(msg_inv([CInv(MSG_TX, 1)] * size), size)
136139

137140
def test_oversized_getdata_msg(self):
138-
self.test_oversized_msg(msg_getdata([CInv(MSG_TX, 1)] * 50001), 50001)
141+
size = MAX_INV_SIZE + 1
142+
self.test_oversized_msg(msg_getdata([CInv(MSG_TX, 1)] * size), size)
139143

140144
def test_oversized_headers_msg(self):
141-
self.test_oversized_msg(msg_headers([CBlockHeader()] * 2001), 2001)
145+
size = MAX_HEADERS_RESULTS + 1
146+
self.test_oversized_msg(msg_headers([CBlockHeader()] * size), size)
142147

143148
def test_resource_exhaustion(self):
144149
self.log.info("Test node stays up despite many large junk messages")
145150
conn = self.nodes[0].add_p2p_connection(P2PDataStore())
146151
conn2 = self.nodes[0].add_p2p_connection(P2PDataStore())
147152
msg_at_size = msg_unrecognized(str_data="b" * VALID_DATA_LIMIT)
148-
assert len(msg_at_size.serialize()) == MSG_LIMIT
153+
assert len(msg_at_size.serialize()) == MAX_PROTOCOL_MESSAGE_LENGTH
149154

150155
self.log.info("(a) Send 80 messages, each of maximum valid data size (4MB)")
151156
for _ in range(80):

test/functional/test_framework/messages.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545

4646
BIP125_SEQUENCE_NUMBER = 0xfffffffd # Sequence number that is BIP 125 opt-in and BIP 68-opt-out
4747

48+
MAX_PROTOCOL_MESSAGE_LENGTH = 4000000 # Maximum length of incoming protocol messages
49+
MAX_HEADERS_RESULTS = 2000 # Number of headers sent in one getheaders result
50+
MAX_INV_SIZE = 50000 # Maximum number of entries in an 'inv' protocol message
51+
4852
NODE_NETWORK = (1 << 0)
4953
NODE_GETUTXO = (1 << 1)
5054
NODE_BLOOM = (1 << 2)

test/functional/test_framework/mininode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from test_framework.messages import (
2828
CBlockHeader,
29+
MAX_HEADERS_RESULTS,
2930
MIN_VERSION_SUPPORTED,
3031
msg_addr,
3132
msg_block,
@@ -553,7 +554,6 @@ def on_getheaders(self, message):
553554
return
554555

555556
headers_list = [self.block_store[self.last_block_hash]]
556-
maxheaders = 2000
557557
while headers_list[-1].sha256 not in locator.vHave:
558558
# Walk back through the block store, adding headers to headers_list
559559
# as we go.
@@ -569,7 +569,7 @@ def on_getheaders(self, message):
569569
break
570570

571571
# Truncate the list if there are too many headers
572-
headers_list = headers_list[:-maxheaders - 1:-1]
572+
headers_list = headers_list[:-MAX_HEADERS_RESULTS - 1:-1]
573573
response = msg_headers(headers_list)
574574

575575
if response is not None:

0 commit comments

Comments
 (0)