o
    Bj44                  	   @   s   d Z ddlmZmZ ddlmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ ddlmZ dd	lmZ G d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deeeeeeZdS )z
Asynchronous interfaces for py-libp2p components.

This module defines async variants of the core libp2p interfaces,
allowing for fully asynchronous implementations alongside the
synchronous ones.
    )ABCabstractmethod)AsyncIterableSequence)	Multiaddr)KeyPair
PrivateKey	PublicKey)MetadataValue)Envelope)ID)PeerInfoc                	   @   s^   e Zd ZdZedededefddZededededdfd	d
Z	ededdfddZ
dS )IAsyncPeerMetadataz-Async interface for peer metadata operations.peer_idkeyreturnc                       dS )a  
        Retrieve the value associated with a key for a specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        key : str
            The key to look up.

        Returns
        -------
        MetadataValue
            The value corresponding to the specified key.

        Raises
        ------
        PeerStoreError
            If the peer ID or value is not found.

        N )selfr   r   r   r   3/Users/sumanjeet/code/py-libp2p/libp2p/abc_async.py	get_async       zIAsyncPeerMetadata.get_asyncvalNc                    r   )a  
        Store a key-value pair for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        key : str
            The key for the data.
        val : MetadataValue
            The value to store.

        Nr   )r   r   r   r   r   r   r   	put_async0   r   zIAsyncPeerMetadata.put_asyncc                    r   )z
        Clear metadata for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer whose metadata is to be cleared.

        Nr   r   r   r   r   r   clear_metadata_async@   r   z'IAsyncPeerMetadata.clear_metadata_async)__name__
__module____qualname____doc__r   r   strr
   r   r   r   r   r   r   r   r      s    r   c                	   @   s   e Zd ZdZededededdfddZeded	e	e deddfd
dZ
ededee fddZededdfddZedee fddZededee fddZdS )IAsyncAddrBookz,Async interface for address book operations.r   addrttlr   Nc                    r   )a  
        Add an address for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        addr : Multiaddr
            The multiaddress to add.
        ttl : int
            The time-to-live for the record.

        Nr   )r   r   r"   r#   r   r   r   add_addr_asyncP   r   zIAsyncAddrBook.add_addr_asyncaddrsc                    r   )a9  
        Add multiple addresses for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        addrs : Sequence[Multiaddr]
            A sequence of multiaddresses to add.
        ttl : int
            The time-to-live for the record.

        Nr   )r   r   r%   r#   r   r   r   add_addrs_async`   r   zIAsyncAddrBook.add_addrs_asyncc                    r   )a  
        Retrieve the addresses for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Returns
        -------
        list[Multiaddr]
            A list of multiaddresses.

        Nr   r   r   r   r   addrs_asyncr   r   zIAsyncAddrBook.addrs_asyncc                    r   )z
        Clear all addresses for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Nr   r   r   r   r   clear_addrs_async   r   z IAsyncAddrBook.clear_addrs_asyncc                    r   )z
        Retrieve all peer IDs that have addresses stored.

        Returns
        -------
        list[ID]
            A list of peer IDs with stored addresses.

        Nr   r   r   r   r   peers_with_addrs_async   r   z%IAsyncAddrBook.peers_with_addrs_asyncc                    r   )aU  
        Returns an async stream of newly added addresses for the given peer.

        Parameters
        ----------
        peer_id : ID
            The peer ID to monitor for address updates.

        Returns
        -------
        AsyncIterable[Multiaddr]
            An async iterator yielding new addresses as they are added.

        Nr   r   r   r   r   addr_stream_async   r   z IAsyncAddrBook.addr_stream_async)r   r   r   r   r   r   r   intr$   r   r&   listr'   r(   r*   r   r+   r   r   r   r   r!   M   s,    r!   c                   @   s   e Zd ZdZededB fddZededdfddZeded	ede	fd
dZ
edededB fddZededdfddZdS )IAsyncCertifiedAddrBookz6Async interface for certified address book operations.r   Nc                    r   )z/Get the local-signed-record wrapped in EnvelopeNr   r)   r   r   r   get_local_record_async   r   z.IAsyncCertifiedAddrBook.get_local_record_asyncenvelopec                    r   )z/Set the local-signed-record wrapped in EnvelopeNr   )r   r0   r   r   r   set_local_record_async   r   z.IAsyncCertifiedAddrBook.set_local_record_asyncr#   c                    r   )a-  
        Accept and store a signed PeerRecord, unless it's older
        than the one already stored.

        Parameters
        ----------
        envelope:
            Signed envelope containing a PeerRecord.
        ttl:
            Time-to-live for the included multiaddrs (in seconds).

        Nr   )r   r0   r#   r   r   r   consume_peer_record_async   r   z1IAsyncCertifiedAddrBook.consume_peer_record_asyncr   c                    r   )z
        Retrieve the most recent signed PeerRecord `Envelope` for a peer.

        Parameters
        ----------
        peer_id : ID
            The peer to look up.

        Nr   r   r   r   r   get_peer_record_async   r   z-IAsyncCertifiedAddrBook.get_peer_record_asyncc                    r   )z
        Delete the signed peer record for a peer if it has no
        known (non-expired) addresses.

        Parameters
        ----------
        peer_id : ID
            The peer whose record we may delete.

        Nr   r   r   r   r   maybe_delete_peer_record_async   r   z6IAsyncCertifiedAddrBook.maybe_delete_peer_record_async)r   r   r   r   r   r   r/   r1   r,   boolr2   r   r3   r4   r   r   r   r   r.      s    r.   c                   @   s   e Zd ZdZededefddZedededdfdd	Zedede	fd
dZ
edede	ddfddZedededdfddZedee fddZededdfddZdS )IAsyncKeyBookz(Async interface for key book operations.r   r   c                    r   )a  
        Retrieve the public key for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Returns
        -------
        PublicKey
            The public key of the peer.

        Nr   r   r   r   r   pubkey_async   r   zIAsyncKeyBook.pubkey_asyncpubkeyNc                    r   )z
        Add a public key for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        pubkey : PublicKey
            The public key to add.

        Nr   )r   r   r8   r   r   r   add_pubkey_async   r   zIAsyncKeyBook.add_pubkey_asyncc                    r   )a  
        Retrieve the private key for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Returns
        -------
        PrivateKey
            The private key of the peer.

        Nr   r   r   r   r   privkey_async  r   zIAsyncKeyBook.privkey_asyncprivkeyc                    r   )z
        Add a private key for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        privkey : PrivateKey
            The private key to add.

        Nr   )r   r   r;   r   r   r   add_privkey_async  r   zIAsyncKeyBook.add_privkey_asynckey_pairc                    r   )z
        Add a key pair for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        key_pair : KeyPair
            The key pair to add.

        Nr   )r   r   r=   r   r   r   add_key_pair_async"  r   z IAsyncKeyBook.add_key_pair_asyncc                    r   )z
        Retrieve all peer IDs that have keys stored.

        Returns
        -------
        list[ID]
            A list of peer IDs with stored keys.

        Nr   r)   r   r   r   peer_with_keys_async0  r   z"IAsyncKeyBook.peer_with_keys_asyncc                    r   )z
        Clear key data for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Nr   r   r   r   r   clear_keydata_async<  r   z!IAsyncKeyBook.clear_keydata_async)r   r   r   r   r   r   r	   r7   r9   r   r:   r<   r   r>   r-   r?   r@   r   r   r   r   r6      s     r6   c                   @   sV   e Zd ZdZedededdfddZededefdd	Zededdfd
dZ	dS )IAsyncMetricsz'Async interface for metrics operations.r   RTTr   Nc                    r   )z
        Record a new latency measurement for the given peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        RTT : float
            The round-trip time measurement.

        Nr   )r   r   rB   r   r   r   record_latency_asyncL  r   z"IAsyncMetrics.record_latency_asyncc                    r   )z
        Retrieve the latency EWMA for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Returns
        -------
        float
            The latency EWMA value.

        Nr   r   r   r   r   latency_EWMA_asyncZ  r   z IAsyncMetrics.latency_EWMA_asyncc                    r   )z
        Clear metrics for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Nr   r   r   r   r   clear_metrics_asynck  r   z!IAsyncMetrics.clear_metrics_async)
r   r   r   r   r   r   floatrC   rD   rE   r   r   r   r   rA   I  s    rA   c                   @   s   e Zd ZdZededee fddZedede	e ddfdd	Z
edede	e ddfd
dZedede	e ddfddZedede	e dee fddZedede	e defddZededdfddZdS )IAsyncProtoBookz-Async interface for protocol book operations.r   r   c                    r   )a	  
        Retrieve the protocols supported by the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Returns
        -------
        list[str]
            A list of protocol strings.

        Nr   r   r   r   r   get_protocols_async{  r   z#IAsyncProtoBook.get_protocols_async	protocolsNc                    r   )z
        Add protocols for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        protocols : Sequence[str]
            The protocols to add.

        Nr   r   r   rI   r   r   r   add_protocols_async  r   z#IAsyncProtoBook.add_protocols_asyncc                    r   )z
        Set protocols for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        protocols : Sequence[str]
            The protocols to set.

        Nr   rJ   r   r   r   set_protocols_async  r   z#IAsyncProtoBook.set_protocols_asyncc                    r   )z
        Remove protocols for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        protocols : Sequence[str]
            The protocols to remove.

        Nr   rJ   r   r   r   remove_protocols_async  r   z&IAsyncProtoBook.remove_protocols_asyncc                    r   )aU  
        Check which protocols are supported by the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        protocols : Sequence[str]
            The protocols to check.

        Returns
        -------
        list[str]
            A list of supported protocols.

        Nr   rJ   r   r   r   supports_protocols_async  r   z(IAsyncProtoBook.supports_protocols_asyncc                    r   )aI  
        Get the first supported protocol from the given list.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.
        protocols : Sequence[str]
            The protocols to check.

        Returns
        -------
        str
            The first supported protocol.

        Nr   rJ   r   r   r   first_supported_protocol_async  r   z.IAsyncProtoBook.first_supported_protocol_asyncc                    r   )z
        Clear protocol data for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Nr   r   r   r   r   clear_protocol_data_async  r   z)IAsyncProtoBook.clear_protocol_data_async)r   r   r   r   r   r   r-   r    rH   r   rK   rL   rM   rN   rO   rP   r   r   r   r   rG   x  sD    rG   c                   @   s   e Zd ZdZededefddZedee fddZ	ededdfd	d
Z
edee fddZeddeddfddZedddZdS )IAsyncPeerStorez
    Async interface for a peer store.

    Provides async methods for managing peer information including address
    management, protocol handling, and key storage.
    r   r   c                    r   )a  
        Retrieve the peer information for the specified peer.

        Parameters
        ----------
        peer_id : ID
            The identifier of the peer.

        Returns
        -------
        PeerInfo
            The peer information object for the given peer.

        Nr   r   r   r   r   peer_info_async  r   zIAsyncPeerStore.peer_info_asyncc                    r   )z
        Retrieve all peer identifiers stored in the peer store.

        Returns
        -------
        list[ID]
            A list of all peer IDs in the store.

        Nr   r)   r   r   r   peer_ids_async  r   zIAsyncPeerStore.peer_ids_asyncNc                    r   )z&Clear all data for the specified peer.Nr   r   r   r   r   clear_peerdata_async  r   z$IAsyncPeerStore.clear_peerdata_asyncc                    r   )z
        Retrieve all valid (non-expired) peer identifiers.

        Returns
        -------
        list[ID]
            A list of valid peer IDs in the store.

        Nr   r)   r   r   r   valid_peer_ids_async  r   z$IAsyncPeerStore.valid_peer_ids_async  cleanup_intervalc                    r   )z=Start periodic cleanup of expired peer records and addresses.Nr   )r   rW   r   r   r   start_cleanup_task+  r   z"IAsyncPeerStore.start_cleanup_taskc                    r   )z+Close the peerstore and clean up resources.Nr   r)   r   r   r   close_async/  r   zIAsyncPeerStore.close_async)rV   )r   N)r   r   r   r   r   r   r   rR   r-   rS   rT   rU   r,   rX   rY   r   r   r   r   rQ     s    rQ   N)r   abcr   r   collections.abcr   r   	multiaddrr   libp2p.crypto.keysr   r   r	   libp2p.custom_typesr
   libp2p.peer.enveloper   libp2p.peer.idr   libp2p.peer.peerinfor   r   r!   r.   r6   rA   rG   rQ   r   r   r   r   <module>   s.    8`4h/
w