From 2bb7f42c207f27ad362e168e0ee77ece91eba38b Mon Sep 17 00:00:00 2001 From: NIC619 Date: Sun, 4 Aug 2019 18:13:45 +0800 Subject: [PATCH] Add validators to `push_msg` --- libp2p/pubsub/pubsub.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index a94542c..b367fe9 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -21,6 +21,7 @@ from libp2p.peer.id import ID from .pb import rpc_pb2 from .pubsub_notifee import PubsubNotifee +from .validators import signature_validator if TYPE_CHECKING: from .pubsub_router_interface import IPubsubRouter @@ -392,8 +393,11 @@ class Pubsub: # TODO: Implement throttle on async validators - results = await asyncio.gather(*async_topic_validator_futures) - return all(results) + if len(async_topic_validator_futures) > 0: + results = await asyncio.gather(*async_topic_validator_futures) + return all(results) + else: + return True async def push_msg(self, msg_forwarder: ID, msg: rpc_pb2.Message) -> None: """ @@ -411,6 +415,14 @@ class Pubsub: return # TODO: - Validate the message. If failed, reject it. + # Validate the signature of the message + # FIXME: `signature_validator` is currently a stub. + if not signature_validator(msg.key, msg.SerializeToString()): + return + # Validate the message with registered topic validators + is_validation_passed = await self.validate_msg(msg_forwarder, msg) + if not is_validation_passed: + return self._mark_msg_seen(msg) await self.handle_talk(msg)