From 91bee9df8915817f742f61bac3ff1da04f929167 Mon Sep 17 00:00:00 2001 From: lla-dane Date: Sat, 23 Aug 2025 16:20:24 +0530 Subject: [PATCH] Moved env_to_send_in_RPC function to libp2p/peer/peerstore.py --- libp2p/__init__.py | 69 ------------------------------ libp2p/kad_dht/kad_dht.py | 2 +- libp2p/kad_dht/peer_routing.py | 2 +- libp2p/kad_dht/provider_store.py | 2 +- libp2p/kad_dht/value_store.py | 2 +- libp2p/peer/peerstore.py | 72 ++++++++++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 73 deletions(-) diff --git a/libp2p/__init__.py b/libp2p/__init__.py index e95bacc0..350ae46b 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -156,75 +156,6 @@ def get_default_muxer_options() -> TMuxerOptions: else: # YAMUX is default return create_yamux_muxer_option() -def env_to_send_in_RPC(host: IHost) -> tuple[bytes, bool]: - """ - Return the signed peer record (Envelope) to be sent in an RPC. - - This function checks whether the host already has a cached signed peer record - (SPR). If one exists and its addresses match the host's current listen - addresses, the cached envelope is reused. Otherwise, a new signed peer record - is created, cached, and returned. - - Parameters - ---------- - host : IHost - The local host instance, providing access to peer ID, listen addresses, - private key, and the peerstore. - - Returns - ------- - tuple[bytes, bool] - A 2-tuple where the first element is the serialized envelope (bytes) - for the signed peer record, and the second element is a boolean flag - indicating whether a new record was created (True) or an existing cached - one was reused (False). - """ - listen_addrs_set = {addr for addr in host.get_addrs()} - local_env = host.get_peerstore().get_local_record() - - if local_env is None: - # No cached SPR yet -> create one - return issue_and_cache_local_record(host), True - else: - record_addrs_set = local_env._env_addrs_set() - if record_addrs_set == listen_addrs_set: - # Perfect match -> reuse cached envelope - return local_env.marshal_envelope(), False - else: - # Addresses changed -> issue a new SPR and cache it - return issue_and_cache_local_record(host), True - - -def issue_and_cache_local_record(host: IHost) -> bytes: - """ - Create and cache a new signed peer record (Envelope) for the host. - - This function generates a new signed peer record from the host’s peer ID, - listen addresses, and private key. The resulting envelope is stored in - the peerstore as the local record for future reuse. - - Parameters - ---------- - host : IHost - The local host instance, providing access to peer ID, listen addresses, - private key, and the peerstore. - - Returns - ------- - bytes - The serialized envelope (bytes) representing the newly created signed - peer record. - """ - env = create_signed_peer_record( - host.get_id(), - host.get_addrs(), - host.get_private_key(), - ) - # Cache it for nexxt time use - host.get_peerstore().set_local_record(env) - return env.marshal_envelope() - - def new_swarm( key_pair: KeyPair | None = None, muxer_opt: TMuxerOptions | None = None, diff --git a/libp2p/kad_dht/kad_dht.py b/libp2p/kad_dht/kad_dht.py index 39de7cc0..b5064e23 100644 --- a/libp2p/kad_dht/kad_dht.py +++ b/libp2p/kad_dht/kad_dht.py @@ -18,7 +18,6 @@ from multiaddr import ( import trio import varint -from libp2p import env_to_send_in_RPC from libp2p.abc import ( IHost, ) @@ -34,6 +33,7 @@ from libp2p.peer.id import ( from libp2p.peer.peerinfo import ( PeerInfo, ) +from libp2p.peer.peerstore import env_to_send_in_RPC from libp2p.tools.async_service import ( Service, ) diff --git a/libp2p/kad_dht/peer_routing.py b/libp2p/kad_dht/peer_routing.py index 9dc18c83..195209a2 100644 --- a/libp2p/kad_dht/peer_routing.py +++ b/libp2p/kad_dht/peer_routing.py @@ -10,7 +10,6 @@ import logging import trio import varint -from libp2p import env_to_send_in_RPC from libp2p.abc import ( IHost, INetStream, @@ -23,6 +22,7 @@ from libp2p.peer.id import ( from libp2p.peer.peerinfo import ( PeerInfo, ) +from libp2p.peer.peerstore import env_to_send_in_RPC from .common import ( ALPHA, diff --git a/libp2p/kad_dht/provider_store.py b/libp2p/kad_dht/provider_store.py index 45be2dba..77bb464f 100644 --- a/libp2p/kad_dht/provider_store.py +++ b/libp2p/kad_dht/provider_store.py @@ -16,7 +16,6 @@ from multiaddr import ( import trio import varint -from libp2p import env_to_send_in_RPC from libp2p.abc import ( IHost, ) @@ -30,6 +29,7 @@ from libp2p.peer.id import ( from libp2p.peer.peerinfo import ( PeerInfo, ) +from libp2p.peer.peerstore import env_to_send_in_RPC from .common import ( ALPHA, diff --git a/libp2p/kad_dht/value_store.py b/libp2p/kad_dht/value_store.py index 39223f02..2002965f 100644 --- a/libp2p/kad_dht/value_store.py +++ b/libp2p/kad_dht/value_store.py @@ -9,7 +9,6 @@ import time import varint -from libp2p import env_to_send_in_RPC from libp2p.abc import ( IHost, ) @@ -20,6 +19,7 @@ from libp2p.kad_dht.utils import maybe_consume_signed_record from libp2p.peer.id import ( ID, ) +from libp2p.peer.peerstore import env_to_send_in_RPC from .common import ( DEFAULT_TTL, diff --git a/libp2p/peer/peerstore.py b/libp2p/peer/peerstore.py index ad6f08db..993a8523 100644 --- a/libp2p/peer/peerstore.py +++ b/libp2p/peer/peerstore.py @@ -16,6 +16,7 @@ import trio from trio import MemoryReceiveChannel, MemorySendChannel from libp2p.abc import ( + IHost, IPeerStore, ) from libp2p.crypto.keys import ( @@ -49,6 +50,77 @@ def create_signed_peer_record( return envelope +def env_to_send_in_RPC(host: IHost) -> tuple[bytes, bool]: + """ + Return the signed peer record (Envelope) to be sent in an RPC. + + This function checks whether the host already has a cached signed peer record + (SPR). If one exists and its addresses match the host's current listen + addresses, the cached envelope is reused. Otherwise, a new signed peer record + is created, cached, and returned. + + Parameters + ---------- + host : IHost + The local host instance, providing access to peer ID, listen addresses, + private key, and the peerstore. + + Returns + ------- + tuple[bytes, bool] + A 2-tuple where the first element is the serialized envelope (bytes) + for the signed peer record, and the second element is a boolean flag + indicating whether a new record was created (True) or an existing cached + one was reused (False). + + """ + listen_addrs_set = {addr for addr in host.get_addrs()} + local_env = host.get_peerstore().get_local_record() + + if local_env is None: + # No cached SPR yet -> create one + return issue_and_cache_local_record(host), True + else: + record_addrs_set = local_env._env_addrs_set() + if record_addrs_set == listen_addrs_set: + # Perfect match -> reuse cached envelope + return local_env.marshal_envelope(), False + else: + # Addresses changed -> issue a new SPR and cache it + return issue_and_cache_local_record(host), True + + +def issue_and_cache_local_record(host: IHost) -> bytes: + """ + Create and cache a new signed peer record (Envelope) for the host. + + This function generates a new signed peer record from the host’s peer ID, + listen addresses, and private key. The resulting envelope is stored in + the peerstore as the local record for future reuse. + + Parameters + ---------- + host : IHost + The local host instance, providing access to peer ID, listen addresses, + private key, and the peerstore. + + Returns + ------- + bytes + The serialized envelope (bytes) representing the newly created signed + peer record. + + """ + env = create_signed_peer_record( + host.get_id(), + host.get_addrs(), + host.get_private_key(), + ) + # Cache it for nexxt time use + host.get_peerstore().set_local_record(env) + return env.marshal_envelope() + + class PeerRecordState: envelope: Envelope seq: int