Added and docstrings and removed typos

This commit is contained in:
lla-dane
2025-08-23 15:36:57 +05:30
parent 3917d7b596
commit 15f4a399ec
2 changed files with 32 additions and 6 deletions

View File

@ -434,7 +434,7 @@ class ProviderStore:
# Consume the sender's signed-peer-record if sent # Consume the sender's signed-peer-record if sent
if not maybe_consume_signed_record(response, self.host, peer_id): if not maybe_consume_signed_record(response, self.host, peer_id):
logger.error( logger.error(
"Recieved an invalid-signed-record, ignoring the response" "Received an invalid-signed-record, ignoring the response"
) )
return [] return []
@ -446,7 +446,7 @@ class ProviderStore:
# already sent with the provider-proto # already sent with the provider-proto
if not maybe_consume_signed_record(provider_proto, self.host): if not maybe_consume_signed_record(provider_proto, self.host):
logger.error( logger.error(
"Recieved an invalid-signed-record, " "Received an invalid-signed-record, "
"ignoring the response" "ignoring the response"
) )
return [] return []

View File

@ -24,6 +24,31 @@ logger = logging.getLogger("kademlia-example.utils")
def maybe_consume_signed_record( def maybe_consume_signed_record(
msg: Message | Message.Peer, host: IHost, peer_id: ID | None = None msg: Message | Message.Peer, host: IHost, peer_id: ID | None = None
) -> bool: ) -> bool:
"""
Attempt to parse and store a signed-peer-record (Envelope) received during
DHT communication. If the record is invalid, the peer-id does not match, or
updating the peerstore fails, the function logs an error and returns False.
Parameters
----------
msg : Message | Message.Peer
The protobuf message received during DHT communication. Can either be a
top-level `Message` containing `senderRecord` or a `Message.Peer`
containing `signedRecord`.
host : IHost
The local host instance, providing access to the peerstore for storing
verified peer records.
peer_id : ID | None, optional
The expected peer ID for record validation. If provided, the peer ID
inside the record must match this value.
Returns
-------
bool
True if a valid signed peer record was successfully consumed and stored,
False otherwise.
"""
if isinstance(msg, Message): if isinstance(msg, Message):
if msg.HasField("senderRecord"): if msg.HasField("senderRecord"):
try: try:
@ -37,13 +62,13 @@ def maybe_consume_signed_record(
return False return False
# Use the default TTL of 2 hours (7200 seconds) # Use the default TTL of 2 hours (7200 seconds)
if not host.get_peerstore().consume_peer_record(envelope, 7200): if not host.get_peerstore().consume_peer_record(envelope, 7200):
logger.error("Updating the certified-addr-book was unsuccessful") logger.error("Failed to update the Certified-Addr-Book")
return False
except Exception as e: except Exception as e:
logger.error("Error updating teh certified addr book for peer: %s", e) logger.error("Failed to update the Certified-Addr-Book: %s", e)
return False return False
else: else:
if msg.HasField("signedRecord"): if msg.HasField("signedRecord"):
# TODO: Check in with the Message.Peer id with the record's id
try: try:
# Convert the signed-peer-record(Envelope) from # Convert the signed-peer-record(Envelope) from
# protobuf bytes # protobuf bytes
@ -56,9 +81,10 @@ def maybe_consume_signed_record(
# Use the default TTL of 2 hours (7200 seconds) # Use the default TTL of 2 hours (7200 seconds)
if not host.get_peerstore().consume_peer_record(envelope, 7200): if not host.get_peerstore().consume_peer_record(envelope, 7200):
logger.error("Failed to update the Certified-Addr-Book") logger.error("Failed to update the Certified-Addr-Book")
return False
except Exception as e: except Exception as e:
logger.error( logger.error(
"Error updating the certified-addr-book: %s", "Failed to update the Certified-Addr-Book: %s",
e, e,
) )
return False return False