0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

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
This commit is contained in:
Sebastian Falbesoner 2024-01-30 02:59:01 +01:00
parent aa9231fafe
commit b198b9c2ce

View file

@ -226,9 +226,8 @@ class P2PConnection(asyncio.Protocol):
self.send_raw_message(send_handshake_bytes)
# if v2 connection, send `on_connection_send_msg` after initial v2 handshake.
# if reconnection situation, send `on_connection_send_msg` after version message is received in `on_version()`.
if self.on_connection_send_msg and not self.supports_v2_p2p and not self.reconnect:
self.send_message(self.on_connection_send_msg)
self.on_connection_send_msg = None # Never used again
if not self.supports_v2_p2p and not self.reconnect:
self.send_version()
self.on_open()
def connection_lost(self, exc):
@ -284,9 +283,8 @@ class P2PConnection(asyncio.Protocol):
if not is_mac_auth:
raise ValueError("invalid v2 mac tag in handshake authentication")
self.recvbuf = self.recvbuf[length:]
if self.v2_state.tried_v2_handshake and self.on_connection_send_msg:
self.send_message(self.on_connection_send_msg)
self.on_connection_send_msg = None
if self.v2_state.tried_v2_handshake:
self.send_version()
# Socket read methods
@ -559,9 +557,7 @@ class P2PInterface(P2PConnection):
assert message.nVersion >= MIN_P2P_VERSION_SUPPORTED, "Version {} received. Test framework only supports versions greater than {}".format(message.nVersion, MIN_P2P_VERSION_SUPPORTED)
# reconnection using v1 P2P has happened since version message can be processed, previously unsent version message is sent using v1 P2P here
if self.reconnect:
if self.on_connection_send_msg:
self.send_message(self.on_connection_send_msg)
self.on_connection_send_msg = None
self.send_version()
self.reconnect = False
if message.nVersion >= 70016 and self.wtxidrelay:
self.send_message(msg_wtxidrelay())
@ -676,6 +672,11 @@ class P2PInterface(P2PConnection):
# Message sending helper functions
def send_version(self):
if self.on_connection_send_msg:
self.send_message(self.on_connection_send_msg)
self.on_connection_send_msg = None # Never used again
def send_and_ping(self, message, timeout=60):
self.send_message(message)
self.sync_with_ping(timeout=timeout)