Skip to content

Commit c340503

Browse files
committed
test: p2p: adhere to typical VERSION message protocol flow
The test framework's p2p implementation currently sends out it's VERSION message immediately after an inbound connection (i.e. TestNode outbound connection) is made. This doesn't follow the usual protocol flow where the initiator sends a version first, and the responders processes that and only then responds with its own version message. Change that accordingly by only sending immediate VERSION message for outbound connections (or after v2 handshake for v2 connections, respectively), and sending out VERSION messages as response for incoming VERSION messages (i.e. in the function `on_version`) for inbound connections. Note that some of the overruled `on_version` methods in functional tests needed to be changed to send the version explicitly.
1 parent 7ddfc28 commit c340503

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

test/functional/p2p_add_connections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def on_version(self, message):
1717
# message is received from the test framework. Don't send any responses
1818
# to the node's version message since the connection will already be
1919
# closed.
20-
pass
20+
self.send_version()
2121

2222
class P2PAddConnections(BitcoinTestFramework):
2323
def set_test_params(self):

test/functional/p2p_addr_relay.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def addr_received(self):
7575
return self.num_ipv4_received != 0
7676

7777
def on_version(self, message):
78+
self.send_version()
7879
self.send_message(msg_verack())
7980
if (self.send_getaddr):
8081
self.send_message(msg_getaddr())

test/functional/p2p_sendtxrcncl.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def on_version(self, message):
2929
# Avoid sending verack in response to version.
3030
# When calling add_p2p_connection, wait_for_verack=False must be set (see
3131
# comment in add_p2p_connection).
32+
self.send_version()
3233
if message.nVersion >= 70016 and self.wtxidrelay:
3334
self.send_message(msg_wtxidrelay())
3435

@@ -43,7 +44,8 @@ def on_sendtxrcncl(self, message):
4344

4445
class P2PFeelerReceiver(SendTxrcnclReceiver):
4546
def on_version(self, message):
46-
pass # feeler connections can not send any message other than their own version
47+
# feeler connections can not send any message other than their own version
48+
self.send_version()
4749

4850

4951
class PeerTrackMsgOrder(P2PInterface):

test/functional/test_framework/p2p.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ def connection_made(self, transport):
224224
if self.supports_v2_p2p and self.v2_state.initiating and not self.v2_state.tried_v2_handshake:
225225
send_handshake_bytes = self.v2_state.initiate_v2_handshake()
226226
self.send_raw_message(send_handshake_bytes)
227-
# if v2 connection, send `on_connection_send_msg` after initial v2 handshake.
228-
# if reconnection situation, send `on_connection_send_msg` after version message is received in `on_version()`.
229-
if not self.supports_v2_p2p and not self.reconnect:
227+
# for v1 outbound connections, send version message immediately after opening
228+
# (for v2 outbound connections, send it after the initial v2 handshake)
229+
if self.p2p_connected_to_node and not self.supports_v2_p2p:
230230
self.send_version()
231231
self.on_open()
232232

@@ -284,7 +284,9 @@ def v2_handshake(self):
284284
raise ValueError("invalid v2 mac tag in handshake authentication")
285285
self.recvbuf = self.recvbuf[length:]
286286
if self.v2_state.tried_v2_handshake:
287-
self.send_version()
287+
# for v2 outbound connections, send version message immediately after v2 handshake
288+
if self.p2p_connected_to_node:
289+
self.send_version()
288290
# process post-v2-handshake data immediately, if available
289291
if len(self.recvbuf) > 0:
290292
self._on_data()
@@ -558,8 +560,9 @@ def on_verack(self, message):
558560

559561
def on_version(self, message):
560562
assert message.nVersion >= MIN_P2P_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_P2P_VERSION_SUPPORTED)
561-
# reconnection using v1 P2P has happened since version message can be processed, previously unsent version message is sent using v1 P2P here
562-
if self.reconnect:
563+
# for inbound connections, reply to version with own version message
564+
# (could be due to v1 reconnect after a failed v2 handshake)
565+
if not self.p2p_connected_to_node:
563566
self.send_version()
564567
self.reconnect = False
565568
if message.nVersion >= 70016 and self.wtxidrelay:

0 commit comments

Comments
 (0)