From 3a42d72cd97630b99d4e65dc6220bd017dc74d05 Mon Sep 17 00:00:00 2001 From: mhchia Date: Mon, 29 Jul 2019 22:50:02 +0800 Subject: [PATCH] Fix a minor bug for pb optional field In `Pubsub.continuously_read_stream`, it checks whether this is a control message enclosed in RPC message with `if rpc_incoming.control:`. However, in pb2, the condition is always true because a default value is returned when a field is not set. Solved it by changing it to `if rpc_incoming.HasField("control"):`. --- libp2p/pubsub/pubsub.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index 265635b6..b46acdaa 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -159,7 +159,11 @@ class Pubsub: for message in rpc_incoming.subscriptions: self.handle_subscription(peer_id, message) - if rpc_incoming.control: + # pylint: disable=line-too-long + # NOTE: Check if `rpc_incoming.control` is set through `HasField`. + # This is necessary because `control` is an optional field in pb2. + # Ref: https://developers.google.com/protocol-buffers/docs/reference/python-generated#singular-fields-proto2 + if rpc_incoming.HasField("control"): # Pass rpc to router so router could perform custom logic await self.router.handle_rpc(rpc_incoming, peer_id)