
    Bj44              
          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e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d deeeeee      Zy)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            	       j    e Zd ZdZedededefd       Zededededdfd       Z	ededdfd	       Z
y)
IAsyncPeerMetadataz-Async interface for peer metadata operations.peer_idkeyreturnc                    K   yw)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   s      3/Users/sumanjeet/code/py-libp2p/libp2p/abc_async.py	get_asynczIAsyncPeerMetadata.get_async           valNc                    K   yw)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   s       r   	put_asynczIAsyncPeerMetadata.put_async0   r   r   c                    K   yw)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   s     r   clear_metadata_asyncz'IAsyncPeerMetadata.clear_metadata_async@   r   r   )__name__
__module____qualname____doc__r   r   strr   r   r   r    r       r   r   r      s~    7r    . r  - D   	" 	 	 	r&   r   c            	           e Zd ZdZededededdfd       Zedede	e   deddfd	       Z
ededee   fd
       Zededdfd       Zedee   fd       Zededee   fd       Zy)IAsyncAddrBookz,Async interface for address book operations.r   addrttlr   Nc                    K   yw)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*   s       r   add_addr_asynczIAsyncAddrBook.add_addr_asyncP   r   r   addrsc                    K   yw)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*   s       r   add_addrs_asynczIAsyncAddrBook.add_addrs_async`   r   r   c                    K   yw)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   s     r   addrs_asynczIAsyncAddrBook.addrs_asyncr   r   r   c                    K   yw)z
        Clear all addresses for the specified peer.

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

        Nr   r   s     r   clear_addrs_asyncz IAsyncAddrBook.clear_addrs_async   r   r   c                    K   yw)z
        Retrieve all peer IDs that have addresses stored.

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

        Nr   r   s    r   peers_with_addrs_asyncz%IAsyncAddrBook.peers_with_addrs_async   r   r   c                    K   yw)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   s     r   addr_stream_asyncz IAsyncAddrBook.addr_stream_async   r   r   )r!   r"   r#   r$   r   r   r   intr,   r   r/   listr1   r3   r6   r   r8   r   r&   r   r(   r(   M   s    6B i c d   "*9"5<?	 "  Y    	r 	d 	 	 	d2h 	 	 r mI6N  r&   r(   c                       e Zd ZdZededz  fd       Zededdfd       Zededede	fd       Z
ed	ededz  fd
       Zed	eddfd       Zy)IAsyncCertifiedAddrBookz6Async interface for certified address book operations.r   Nc                    K   yw)z/Get the local-signed-record wrapped in EnvelopeNr   r5   s    r   get_local_record_asyncz.IAsyncCertifiedAddrBook.get_local_record_async   r   r   envelopec                    K   yw)z/Set the local-signed-record wrapped in EnvelopeNr   )r   r?   s     r   set_local_record_asyncz.IAsyncCertifiedAddrBook.set_local_record_async   r   r   r*   c                    K   yw)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   r?   r*   s      r   consume_peer_record_asyncz1IAsyncCertifiedAddrBook.consume_peer_record_async   r   r   r   c                    K   yw)z
        Retrieve the most recent signed PeerRecord `Envelope` for a peer.

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

        Nr   r   s     r   get_peer_record_asyncz-IAsyncCertifiedAddrBook.get_peer_record_async   r   r   c                    K   yw)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   s     r   maybe_delete_peer_record_asyncz6IAsyncCertifiedAddrBook.maybe_delete_peer_record_async   r   r   )r!   r"   r#   r$   r   r   r>   rA   r9   boolrC   r   rE   rG   r   r&   r   r<   r<      s    @>ho > > >X >$ > >  s t   	2 	(T/ 	 	 
B 
4 
 
r&   r<   c                       e Zd ZdZededefd       Zedededdfd       Zedede	fd       Z
eded	e	ddfd
       Zedededdfd       Zedee   fd       Zededdfd       Zy)IAsyncKeyBookz(Async interface for key book operations.r   r   c                    K   yw)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   s     r   pubkey_asynczIAsyncKeyBook.pubkey_async   r   r   pubkeyNc                    K   yw)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   rM   s      r   add_pubkey_asynczIAsyncKeyBook.add_pubkey_async   r   r   c                    K   yw)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   s     r   privkey_asynczIAsyncKeyBook.privkey_async  r   r   privkeyc                    K   yw)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   rR   s      r   add_privkey_asynczIAsyncKeyBook.add_privkey_async  r   r   key_pairc                    K   yw)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   rU   s      r   add_key_pair_asyncz IAsyncKeyBook.add_key_pair_async"  r   r   c                    K   yw)z
        Retrieve all peer IDs that have keys stored.

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

        Nr   r5   s    r   peer_with_keys_asyncz"IAsyncKeyBook.peer_with_keys_async0  r   r   c                    K   yw)z
        Clear key data for the specified peer.

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

        Nr   r   s     r   clear_keydata_asyncz!IAsyncKeyBook.clear_keydata_async<  r   r   )r!   r"   r#   r$   r   r   r
   rL   rO   r	   rQ   rT   r   rW   r:   rY   r[   r   r&   r   rJ   rJ      s   2"     b )    2 *    r J 4    g $   	DH 	 	 	 	 	 	r&   rJ   c                   b    e Zd ZdZedededdfd       Zededefd       Zededdfd       Z	y)	IAsyncMetricsz'Async interface for metrics operations.r   RTTr   Nc                    K   yw)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   r^   s      r   record_latency_asyncz"IAsyncMetrics.record_latency_asyncL  r   r   c                    K   yw)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   s     r   latency_EWMA_asyncz IAsyncMetrics.latency_EWMA_asyncZ  r   r   c                    K   yw)z
        Clear metrics for the specified peer.

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

        Nr   r   s     r   clear_metrics_asyncz!IAsyncMetrics.clear_metrics_asynck  r   r   )
r!   r"   r#   r$   r   r   floatr`   rb   rd   r   r&   r   r]   r]   I  sp    1" 5 T    u    	 	 	 	r&   r]   c                      e Zd ZdZededee   fd       Zedede	e   ddfd       Z
edede	e   ddfd       Zedede	e   ddfd	       Zedede	e   dee   fd
       Zedede	e   defd       Zededdfd       Zy)IAsyncProtoBookz-Async interface for protocol book operations.r   r   c                    K   yw)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   s     r   get_protocols_asyncz#IAsyncProtoBook.get_protocols_async{  r   r   	protocolsNc                    K   yw)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   rj   s      r   add_protocols_asyncz#IAsyncProtoBook.add_protocols_async  r   r   c                    K   yw)z
        Set protocols for the specified peer.

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

        Nr   rl   s      r   set_protocols_asyncz#IAsyncProtoBook.set_protocols_async  r   r   c                    K   yw)z
        Remove protocols for the specified peer.

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

        Nr   rl   s      r   remove_protocols_asyncz&IAsyncProtoBook.remove_protocols_async  r   r   c                    K   yw)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   rl   s      r   supports_protocols_asyncz(IAsyncProtoBook.supports_protocols_async  r   r   c                    K   yw)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   rl   s      r   first_supported_protocol_asyncz.IAsyncProtoBook.first_supported_protocol_async  r   r   c                    K   yw)z
        Clear protocol data for the specified peer.

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

        Nr   r   s     r   clear_protocol_data_asyncz)IAsyncProtoBook.clear_protocol_data_async  r   r   )r!   r"   r#   r$   r   r   r:   r%   ri   r   rm   ro   rq   rs   ru   rw   r   r&   r   rg   rg   x  s;   7 S	      RV     RV   &.sm	  &.sm	c ( &.sm	 ( 	r 	d 	 	r&   rg   c                       e Zd ZdZededefd       Zedee   fd       Z	ededdfd       Z
edee   fd       Zedd	eddfd
       Zedd       Zy)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                    K   yw)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   s     r   peer_info_asynczIAsyncPeerStore.peer_info_async  r   r   c                    K   yw)z
        Retrieve all peer identifiers stored in the peer store.

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

        Nr   r5   s    r   peer_ids_asynczIAsyncPeerStore.peer_ids_async  r   r   Nc                    K   yw)z&Clear all data for the specified peer.Nr   r   s     r   clear_peerdata_asyncz$IAsyncPeerStore.clear_peerdata_async  r   r   c                    K   yw)z
        Retrieve all valid (non-expired) peer identifiers.

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

        Nr   r5   s    r   valid_peer_ids_asyncz$IAsyncPeerStore.valid_peer_ids_async  r   r   cleanup_intervalc                    K   yw)z=Start periodic cleanup of expired peer records and addresses.Nr   )r   r   s     r   start_cleanup_taskz"IAsyncPeerStore.start_cleanup_task+  r   r   c                    K   yw)z+Close the peerstore and clean up resources.Nr   r5   s    r   close_asynczIAsyncPeerStore.close_async/  r   r   )i  )r   N)r!   r"   r#   r$   r   r   r   r{   r:   r}   r   r   r9   r   r   r   r&   r   ry   ry     s     R H    	d2h 	 	 5" 5 5 5 	DH 	 	 L L L L : :r&   ry   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<   rJ   r]   rg   ry   r   r&   r   <module>r      s    $ 3  = = - )  )5 5p]S ]@1c 1heC eP,C ,^tc tnB:B:r&   