async fn chain_exchange_request(
        &self,
        peer_id: PeerId,
        request: ChainExchangeRequest,
    ) -> Result<ChainExchangeResponse, String> {
        trace!("Sending ChainExchange Request {:?}", request);

        let req_pre_time = SystemTime::now();

        let (tx, rx) = oneshot_channel();
        if self
            .network_send
            .send(NetworkMessage::ChainExchangeRequest {
                peer_id: peer_id.clone(),
                request,
                response_channel: tx,
            })
            .await
            .is_err()
        {
            return Err("Failed to send chain exchange request to network".to_string());
        };

        let res = future::timeout(Duration::from_secs(RPC_TIMEOUT), rx).await;
        let res_duration = SystemTime::now()
            .duration_since(req_pre_time)
            .unwrap_or_default();
        match res {
            Ok(Ok(Ok(bs_res))) => {
                // Successful response
                self.peer_manager.log_success(&peer_id, res_duration).await;
                Ok(bs_res)
            }
            Ok(Ok(Err(e))) => {
                // Internal libp2p error, score failure for peer and potentially disconnect
                self.peer_manager.log_failure(&peer_id, res_duration).await;
                match e {
                    RequestResponseError::ConnectionClosed
                    | RequestResponseError::DialFailure
                    | RequestResponseError::UnsupportedProtocols => {
                        self.peer_manager.remove_peer(&peer_id).await;
                    }
                    // Ignore dropping peer on timeout for now. Can't be confident yet that the
                    // specified timeout is adequate time.
                    RequestResponseError::Timeout => (),
                }
                Err(format!("Internal libp2p error: {:?}", e))
            }
            Ok(Err(_)) | Err(_) => {
                // Sender channel internally dropped or timeout, both should log failure which will
                // negatively score the peer, but not drop yet.
                self.peer_manager.log_failure(&peer_id, res_duration).await;
                Err("Chain exchange request timed out".to_string())
            }
        }
    }
/// Send a chain_exchange request to the network and await response.
Send a chain_exchange request to the network and await response.
Send a chain_exchange request to the network and await response .
async fn chain_exchange_request(
        &self,
        peer_id: PeerId,
        request: ChainExchangeRequest,
    ) -> Result<ChainExchangeResponse, String> {
        trace!("Sending ChainExchange Request {:?}", request);
        let req_pre_time = SystemTime::now();
        let (tx, rx) = oneshot_channel();
        if self
            .network_send
            .send(NetworkMessage::ChainExchangeRequest {
                peer_id: peer_id.clone(),
                request,
                response_channel: tx,
            })
            .await
            .is_err()
        {
            return Err("Failed to send chain exchange request to network".to_string());
        };
        let res = future::timeout(Duration::from_secs(RPC_TIMEOUT), rx).await;
        let res_duration = SystemTime::now()
            .duration_since(req_pre_time)
            .unwrap_or_default();
        match res {
            Ok(Ok(Ok(bs_res))) => {
                // Successful response
                self.peer_manager.log_success(&peer_id, res_duration).await;
                Ok(bs_res)
            }
            Ok(Ok(Err(e))) => {
                // Internal libp2p error, score failure for peer and potentially disconnect
                self.peer_manager.log_failure(&peer_id, res_duration).await;
                match e {
                    RequestResponseError::ConnectionClosed
                    | RequestResponseError::DialFailure
                    | RequestResponseError::UnsupportedProtocols => {
                        self.peer_manager.remove_peer(&peer_id).await;
                    }
                    // Ignore dropping peer on timeout for now. Can't be confident yet that the
                    // specified timeout is adequate time.
                    RequestResponseError::Timeout => (),
                }
                Err(format!("Internal libp2p error: {:?}", e))
            }
            Ok(Err(_)) | Err(_) => {
                // Sender channel internally dropped or timeout, both should log failure which will
                // negatively score the peer, but not drop yet.
                self.peer_manager.log_failure(&peer_id, res_duration).await;
                Err("Chain exchange request timed out".to_string())
            }
        }
    }
async fn chain_exchange_request ( & self , peer_id : PeerId , request : ChainExchangeRequest , ) -> Result < ChainExchangeResponse , String > { trace ! ( " " request ) ; let req_pre_time = SystemTime :: now ( ) ; let ( tx , rx ) = oneshot_channel ( ) ; if self . network_send . send ( NetworkMessage :: ChainExchangeRequest { peer_id : peer_id . clone ( ) , request , response_channel : tx , } ) . await . is_err ( ) { return Err ( " " . to_string ( ) ) ; } ; let res = future :: timeout ( Duration :: from_secs ( RPC_TIMEOUT ) , rx ) . await ; let res_duration = SystemTime :: now ( ) . duration_since ( req_pre_time ) . unwrap_or_default ( ) ; match res { Ok ( Ok ( Ok ( bs_res ) ) ) => { // Successful response self . peer_manager . log_success ( & peer_id , res_duration ) . await ; Ok ( bs_res ) } Ok ( Ok ( Err ( e ) ) ) => { // Internal libp2p error, score failure for peer and potentially disconnect self . peer_manager . log_failure ( & peer_id , res_duration ) . await ; match e { RequestResponseError :: ConnectionClosed | RequestResponseError :: DialFailure | RequestResponseError :: UnsupportedProtocols => { self . peer_manager . remove_peer ( & peer_id ) . await ; } // Ignore dropping peer on timeout for now. Can't be confident yet that the // specified timeout is adequate time. RequestResponseError :: Timeout => ( ) , } Err ( format ! ( " " e ) ) } Ok ( Err ( _ ) ) | Err ( _ ) => { // Sender channel internally dropped or timeout, both should log failure which will // negatively score the peer, but not drop yet. self . peer_manager . log_failure ( & peer_id , res_duration ) . await ; Err ( " " . to_string ( ) ) } } }
Send a chain_exchange request to the network and await response.
Send a chain_exchange request to the network and await response .
&self
peer_id
PeerId
request
ChainExchangeRequest