diff options
Diffstat (limited to 'test/functional/test_framework/p2p.py')
-rwxr-xr-x | test/functional/test_framework/p2p.py | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/test/functional/test_framework/p2p.py b/test/functional/test_framework/p2p.py index c113a4c8d8..dc04696114 100755 --- a/test/functional/test_framework/p2p.py +++ b/test/functional/test_framework/p2p.py @@ -224,11 +224,10 @@ class P2PConnection(asyncio.Protocol): if self.supports_v2_p2p and self.v2_state.initiating and not self.v2_state.tried_v2_handshake: send_handshake_bytes = self.v2_state.initiate_v2_handshake() 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 + # for v1 outbound connections, send version message immediately after opening + # (for v2 outbound connections, send it after the initial v2 handshake) + if self.p2p_connected_to_node and not self.supports_v2_p2p: + self.send_version() self.on_open() def connection_lost(self, exc): @@ -243,7 +242,7 @@ class P2PConnection(asyncio.Protocol): self.on_close() # v2 handshake method - def v2_handshake(self): + def _on_data_v2_handshake(self): """v2 handshake performed before P2P messages are exchanged (see BIP324). P2PConnection is the initiator (in inbound connections to TestNode) and the responder (in outbound connections from TestNode). Performed by: @@ -284,9 +283,13 @@ 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: + # for v2 outbound connections, send version message immediately after v2 handshake + if self.p2p_connected_to_node: + self.send_version() + # process post-v2-handshake data immediately, if available + if len(self.recvbuf) > 0: + self._on_data() # Socket read methods @@ -295,7 +298,7 @@ class P2PConnection(asyncio.Protocol): if len(t) > 0: self.recvbuf += t if self.supports_v2_p2p and not self.v2_state.tried_v2_handshake: - self.v2_handshake() + self._on_data_v2_handshake() else: self._on_data() @@ -557,11 +560,10 @@ class P2PInterface(P2PConnection): def on_version(self, message): 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 + # for inbound connections, reply to version with own version message + # (could be due to v1 reconnect after a failed v2 handshake) + if not self.p2p_connected_to_node: + self.send_version() self.reconnect = False if message.nVersion >= 70016 and self.wtxidrelay: self.send_message(msg_wtxidrelay()) @@ -593,9 +595,7 @@ class P2PInterface(P2PConnection): def wait_for_reconnect(self, timeout=60): def test_function(): - if not (self.is_connected and self.last_message.get('version') and self.v2_state is None): - return False - return True + return self.is_connected and self.last_message.get('version') and not self.supports_v2_p2p self.wait_until(test_function, timeout=timeout, check_connected=False) # Message receiving helper methods @@ -676,6 +676,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) |