From 19ce5bb4200c7ff63aaf3afb6c79966bdcae35d0 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Sun, 4 Aug 2019 18:13:34 +0800 Subject: [PATCH] Add `signature_validator` stub and docstring --- libp2p/pubsub/pubsub.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index 84df48ff..a94542cb 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -1,7 +1,17 @@ import asyncio from collections import namedtuple import time -from typing import Any, Awaitable, Callable, Dict, Iterable, List, Tuple, Union, TYPE_CHECKING +from typing import ( + Any, + Awaitable, + Callable, + Dict, + Iterable, + List, + Tuple, + Union, + TYPE_CHECKING, +) from lru import LRU @@ -166,12 +176,16 @@ class Pubsub: ) -> None: """ Register a validator under the given topic. One topic can only have one validtor. + :param topic: the topic to register validator under + :param validator: the validator used to validate messages published to the topic + :param is_async_validator: indicate if the validator is an asynchronous validator """ self.topic_validators[topic] = TopicValidator(validator, is_async_validator) def remove_topic_validator(self, topic: str) -> None: """ Remove the validator from the given topic. + :param topic: the topic to remove validator from """ if topic in self.topic_validators: del self.topic_validators[topic] @@ -179,6 +193,7 @@ class Pubsub: def get_msg_validators(self, msg: rpc_pb2.Message) -> Iterable[TopicValidator]: """ Get all validators corresponding to the topics in the message. + :param msg: the message published to the topic """ for topic in msg.topicIDs: if topic in self.topic_validators: @@ -356,6 +371,11 @@ class Pubsub: await self.push_msg(self.host.get_id(), msg) async def validate_msg(self, msg_forwarder: ID, msg: rpc_pb2.Message) -> bool: + """ + Validate the received message + :param msg_forwarder: the peer who forward us the message. + :param msg: the message. + """ sync_topic_validators = [] async_topic_validator_futures = [] for topic_validator in self.get_msg_validators(msg):