Skip to content
This repository was archived by the owner on Jul 1, 2021. It is now read-only.

Commit 73d842e

Browse files
committed
Allow Ping, Pong, Disconnect Messages snappy compressible
1 parent 07cabc7 commit 73d842e

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

p2p/p2p_proto.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ class P2PProtocol(Protocol):
9595
_commands = [Hello, Ping, Pong, Disconnect]
9696
cmd_length = 16
9797

98-
def __init__(self, peer: 'BasePeer') -> None:
98+
def __init__(self, peer: 'BasePeer', snappy_support: bool) -> None:
9999
# For the base protocol the cmd_id_offset is always 0.
100100
# For the base protocol snappy compression should be disabled
101-
super().__init__(peer, cmd_id_offset=0, snappy_support=False)
101+
super().__init__(peer, cmd_id_offset=0, snappy_support=snappy_support)
102102

103103
def send_handshake(self) -> None:
104104
# TODO: move import out once this is in the trinity codebase

p2p/peer.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ def __init__(self,
211211
# Networking reader and writer objects for communication
212212
self.reader = connection.reader
213213
self.writer = connection.writer
214-
self.base_protocol = P2PProtocol(self)
214+
# Initially while doing the handshake, the base protocol shouldn't support
215+
# snappy compression
216+
self.base_protocol = P2PProtocol(self, snappy_support=False)
215217

216218
# Flag indicating whether the connection this peer represents was
217219
# established from a dial-out or dial-in (True: dial-in, False:
@@ -493,6 +495,12 @@ async def process_p2p_handshake(
493495
# based on other peer's p2p protocol version
494496
snappy_support = msg['version'] >= SNAPPY_PROTOCOL_VERSION
495497

498+
if snappy_support:
499+
# Now update the base protocol to support snappy compression
500+
# This is needed so that Trinity is compatible with parity since
501+
# parity sends Ping even after Handshake
502+
self.base_protocol = P2PProtocol(self, snappy_support=snappy_support)
503+
496504
remote_capabilities = msg['capabilities']
497505
try:
498506
self.sub_proto = self.select_sub_protocol(remote_capabilities, snappy_support)

tests/core/p2p-proto/test_peer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async def test_les_handshake():
5454
)
5555
)
5656
def test_sub_protocol_selection(snappy_support):
57-
peer = ProtoMatchingPeer([LESProtocol, LESProtocolV2])
57+
peer = ProtoMatchingPeer([LESProtocol, LESProtocolV2], snappy_support)
5858

5959
proto = peer.select_sub_protocol([
6060
(LESProtocol.name, LESProtocol.version),
@@ -102,6 +102,6 @@ class LESProtocolV3(LESProtocol):
102102

103103
class ProtoMatchingPeer(LESPeer):
104104

105-
def __init__(self, supported_sub_protocols):
105+
def __init__(self, supported_sub_protocols, snappy_support):
106106
self._supported_sub_protocols = supported_sub_protocols
107-
self.base_protocol = P2PProtocol(self)
107+
self.base_protocol = P2PProtocol(self, snappy_support)

0 commit comments

Comments
 (0)