Skip to content

Commit b198b9c

Browse files
committed
test: p2p: introduce helper for sending prepared VERSION message
This deduplicates code for sending out the VERSION message (if available and not sent yet), currently used at three different places: 1) in the `connection_made` asyncio callback (for v1 connections that are not v2 reconnects) 2) at the end of `v2_handshake`, if the v2 handshake succeeded 3) in the `on_version` callback, if a reconnection with v1 happens
1 parent aa9231f commit b198b9c

File tree

1 file changed

+10
-9
lines changed
  • test/functional/test_framework

1 file changed

+10
-9
lines changed

test/functional/test_framework/p2p.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,8 @@ def connection_made(self, transport):
226226
self.send_raw_message(send_handshake_bytes)
227227
# if v2 connection, send `on_connection_send_msg` after initial v2 handshake.
228228
# if reconnection situation, send `on_connection_send_msg` after version message is received in `on_version()`.
229-
if self.on_connection_send_msg and not self.supports_v2_p2p and not self.reconnect:
230-
self.send_message(self.on_connection_send_msg)
231-
self.on_connection_send_msg = None # Never used again
229+
if not self.supports_v2_p2p and not self.reconnect:
230+
self.send_version()
232231
self.on_open()
233232

234233
def connection_lost(self, exc):
@@ -284,9 +283,8 @@ def v2_handshake(self):
284283
if not is_mac_auth:
285284
raise ValueError("invalid v2 mac tag in handshake authentication")
286285
self.recvbuf = self.recvbuf[length:]
287-
if self.v2_state.tried_v2_handshake and self.on_connection_send_msg:
288-
self.send_message(self.on_connection_send_msg)
289-
self.on_connection_send_msg = None
286+
if self.v2_state.tried_v2_handshake:
287+
self.send_version()
290288

291289
# Socket read methods
292290

@@ -559,9 +557,7 @@ def on_version(self, message):
559557
assert message.nVersion >= MIN_P2P_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_P2P_VERSION_SUPPORTED)
560558
# reconnection using v1 P2P has happened since version message can be processed, previously unsent version message is sent using v1 P2P here
561559
if self.reconnect:
562-
if self.on_connection_send_msg:
563-
self.send_message(self.on_connection_send_msg)
564-
self.on_connection_send_msg = None
560+
self.send_version()
565561
self.reconnect = False
566562
if message.nVersion >= 70016 and self.wtxidrelay:
567563
self.send_message(msg_wtxidrelay())
@@ -676,6 +672,11 @@ def test_function():
676672

677673
# Message sending helper functions
678674

675+
def send_version(self):
676+
if self.on_connection_send_msg:
677+
self.send_message(self.on_connection_send_msg)
678+
self.on_connection_send_msg = None # Never used again
679+
679680
def send_and_ping(self, message, timeout=60):
680681
self.send_message(message)
681682
self.sync_with_ping(timeout=timeout)

0 commit comments

Comments
 (0)