pub async fn handle_new_block_message(
        &mut self,
        new_block: NewBlock,
        source_peer: NodeId,
    ) -> Result<(), CommsInterfaceError>
    {
        let NewBlock { block_hash } = new_block;

        // Only a single block request can complete at a time.
        // As multiple NewBlock requests arrive from propagation, this semaphore prevents multiple requests to nodes for
        // the same full block. The first request that succeeds will stop the node from requesting the block from any
        // other node (block_exists is true).
        let _permit = self.new_block_request_semaphore.acquire().await;

        if async_db::block_exists(self.blockchain_db.clone(), block_hash.clone()).await? {
            debug!(
                target: LOG_TARGET,
                "Block with hash `{}` already stored",
                block_hash.to_hex()
            );
            return Ok(());
        }

        debug!(
            target: LOG_TARGET,
            "Block with hash `{}` is unknown. Requesting it from peer `{}`.",
            block_hash.to_hex(),
            source_peer.short_str()
        );
        let mut block = self
            .outbound_nci
            .request_blocks_with_hashes_from_peer(vec![block_hash], Some(source_peer.clone()))
            .await?;

        match block.pop() {
            Some(block) => {
                self.handle_block(Arc::new(block.block), true.into(), Some(source_peer))
                    .await
            },
            None => {
                // TODO: #banheuristic - peer propagated block hash for which it could not return the full block
                debug!(
                    target: LOG_TARGET,
                    "Peer `{}` failed to return the block that was requested.",
                    source_peer.short_str()
                );
                Err(CommsInterfaceError::InvalidPeerResponse(format!(
                    "Invalid response from peer `{}`: Peer failed to provide the block that was propagated",
                    source_peer.short_str()
                )))
            },
        }
    }
/// Handles a `NewBlock` message. Only a single `NewBlock` message can be handled at once to prevent extraneous
/// requests for the full block.
/// This may (asynchronously) block until the other request(s) complete or time out and so should typically be
/// executed in a dedicated task.
Handles a `NewBlock` message. Only a single `NewBlock` message can be handled at once to prevent extraneous
requests for the full block.
This may (asynchronously) block until the other request(s) complete or time out and so should typically be
executed in a dedicated task.
Handles a ` NewBlock ` message . Only a single ` NewBlock ` message can be handled at once to prevent extraneous requests for the full block . This may ( asynchronously ) block until the other request ( s ) complete or time out and so should typically be executed in a dedicated task .
pub async fn handle_new_block_message(
        &mut self,
        new_block: NewBlock,
        source_peer: NodeId,
    ) -> Result<(), CommsInterfaceError>
    {
        let NewBlock { block_hash } = new_block;
        // Only a single block request can complete at a time.
        // As multiple NewBlock requests arrive from propagation, this semaphore prevents multiple requests to nodes for
        // the same full block. The first request that succeeds will stop the node from requesting the block from any
        // other node (block_exists is true).
        let _permit = self.new_block_request_semaphore.acquire().await;
        if async_db::block_exists(self.blockchain_db.clone(), block_hash.clone()).await? {
            debug!(
                target: LOG_TARGET,
                "Block with hash `{}` already stored",
                block_hash.to_hex()
            );
            return Ok(());
        }
        debug!(
            target: LOG_TARGET,
            "Block with hash `{}` is unknown. Requesting it from peer `{}`.",
            block_hash.to_hex(),
            source_peer.short_str()
        );
        let mut block = self
            .outbound_nci
            .request_blocks_with_hashes_from_peer(vec![block_hash], Some(source_peer.clone()))
            .await?;
        match block.pop() {
            Some(block) => {
                self.handle_block(Arc::new(block.block), true.into(), Some(source_peer))
                    .await
            },
            None => {
                // TODO: #banheuristic - peer propagated block hash for which it could not return the full block
                debug!(
                    target: LOG_TARGET,
                    "Peer `{}` failed to return the block that was requested.",
                    source_peer.short_str()
                );
                Err(CommsInterfaceError::InvalidPeerResponse(format!(
                    "Invalid response from peer `{}`: Peer failed to provide the block that was propagated",
                    source_peer.short_str()
                )))
            },
        }
    }
pub async fn handle_new_block_message ( & mut self , new_block : NewBlock , source_peer : NodeId , ) -> Result < ( ) , CommsInterfaceError > { let NewBlock { block_hash } = new_block ; // Only a single block request can complete at a time. // As multiple NewBlock requests arrive from propagation, this semaphore prevents multiple requests to nodes for // the same full block. The first request that succeeds will stop the node from requesting the block from any // other node (block_exists is true). let _permit = self . new_block_request_semaphore . acquire ( ) . await ; if async_db :: block_exists ( self . blockchain_db . clone ( ) , block_hash . clone ( ) ) . await ? { debug ! ( target LOG_TARGET " " block_hash to_hex ( ) ) ; return Ok ( ( ) ) ; } debug ! ( target LOG_TARGET " " block_hash to_hex ( ) source_peer short_str ( ) ) ; let mut block = self . outbound_nci . request_blocks_with_hashes_from_peer ( vec ! [ block_hash ] , Some ( source_peer . clone ( ) ) ) . await ? ; match block . pop ( ) { Some ( block ) => { self . handle_block ( Arc :: new ( block . block ) , true . into ( ) , Some ( source_peer ) ) . await } , None => { // TODO: #banheuristic - peer propagated block hash for which it could not return the full block debug ! ( target LOG_TARGET " " source_peer short_str ( ) ) ; Err ( CommsInterfaceError :: InvalidPeerResponse ( format ! ( " " source_peer short_str ( ) ) ) ) } , } }
Handles a `NewBlock` message.
Handles a ` NewBlock ` message .
&mut self
new_block
NewBlock
source_peer
NodeId
&mut self
new_block
NewBlock
source_peer
NodeId