pub async fn handle_block(
        &self,
        block: Arc<Block>,
        broadcast: Broadcast,
        source_peer: Option<NodeId>,
    ) -> Result<(), CommsInterfaceError>
    {
        let block_hash = block.hash();
        let block_height = block.header.height;
        debug!(
            target: LOG_TARGET,
            "Block #{} ({}) received from {}",
            block_height,
            block_hash.to_hex(),
            source_peer
                .as_ref()
                .map(|p| format!("remote peer: {}", p))
                .unwrap_or_else(|| "local services".to_string())
        );
        trace!(target: LOG_TARGET, "Block: {}", block);
        let add_block_result = async_db::add_block(self.blockchain_db.clone(), block.clone()).await;
        // Create block event on block event stream
        match add_block_result {
            Ok(block_add_result) => {
                trace!(target: LOG_TARGET, "Block event created: {}", block_add_result);

                let should_propagate = match &block_add_result {
                    BlockAddResult::Ok => true,
                    BlockAddResult::BlockExists => false,
                    BlockAddResult::OrphanBlock => false,
                    BlockAddResult::ChainReorg(_, _) => true,
                };

                self.publish_block_event(BlockEvent::ValidBlockAdded(block, block_add_result, broadcast));

                if should_propagate && broadcast.is_true() {
                    info!(
                        target: LOG_TARGET,
                        "Propagate block ({}) to network.",
                        block_hash.to_hex()
                    );
                    let exclude_peers = source_peer.into_iter().collect();
                    let new_block = NewBlock::new(block_hash);
                    self.outbound_nci.propagate_block(new_block, exclude_peers).await?;
                }
                Ok(())
            },
            Err(e) => {
                warn!(
                    target: LOG_TARGET,
                    "Block #{} ({}) validation failed: {:?}",
                    block_height,
                    block_hash.to_hex(),
                    e
                );
                self.publish_block_event(BlockEvent::AddBlockFailed(block, broadcast));
                Err(CommsInterfaceError::ChainStorageError(e))
            },
        }
    }
/// Handle inbound blocks from remote nodes and local services.
Handle inbound blocks from remote nodes and local services.
Handle inbound blocks from remote nodes and local services .
pub async fn handle_block(
        &self,
        block: Arc<Block>,
        broadcast: Broadcast,
        source_peer: Option<NodeId>,
    ) -> Result<(), CommsInterfaceError>
    {
        let block_hash = block.hash();
        let block_height = block.header.height;
        debug!(
            target: LOG_TARGET,
            "Block #{} ({}) received from {}",
            block_height,
            block_hash.to_hex(),
            source_peer
                .as_ref()
                .map(|p| format!("remote peer: {}", p))
                .unwrap_or_else(|| "local services".to_string())
        );
        trace!(target: LOG_TARGET, "Block: {}", block);
        let add_block_result = async_db::add_block(self.blockchain_db.clone(), block.clone()).await;
        // Create block event on block event stream
        match add_block_result {
            Ok(block_add_result) => {
                trace!(target: LOG_TARGET, "Block event created: {}", block_add_result);
                let should_propagate = match &block_add_result {
                    BlockAddResult::Ok => true,
                    BlockAddResult::BlockExists => false,
                    BlockAddResult::OrphanBlock => false,
                    BlockAddResult::ChainReorg(_, _) => true,
                };
                self.publish_block_event(BlockEvent::ValidBlockAdded(block, block_add_result, broadcast));
                if should_propagate && broadcast.is_true() {
                    info!(
                        target: LOG_TARGET,
                        "Propagate block ({}) to network.",
                        block_hash.to_hex()
                    );
                    let exclude_peers = source_peer.into_iter().collect();
                    let new_block = NewBlock::new(block_hash);
                    self.outbound_nci.propagate_block(new_block, exclude_peers).await?;
                }
                Ok(())
            },
            Err(e) => {
                warn!(
                    target: LOG_TARGET,
                    "Block #{} ({}) validation failed: {:?}",
                    block_height,
                    block_hash.to_hex(),
                    e
                );
                self.publish_block_event(BlockEvent::AddBlockFailed(block, broadcast));
                Err(CommsInterfaceError::ChainStorageError(e))
            },
        }
    }
pub async fn handle_block ( & self , block : Arc < Block > , broadcast : Broadcast , source_peer : Option < NodeId > , ) -> Result < ( ) , CommsInterfaceError > { let block_hash = block . hash ( ) ; let block_height = block . header . height ; debug ! ( target LOG_TARGET " " block_height block_hash to_hex ( ) source_peer as_ref ( ) map ( p format ( " " p ) ) unwrap_or_else ( " " to_string ( ) ) ) ; trace ! ( target LOG_TARGET " " block ) ; let add_block_result = async_db :: add_block ( self . blockchain_db . clone ( ) , block . clone ( ) ) . await ; // Create block event on block event stream match add_block_result { Ok ( block_add_result ) => { trace ! ( target LOG_TARGET " " block_add_result ) ; let should_propagate = match & block_add_result { BlockAddResult :: Ok => true , BlockAddResult :: BlockExists => false , BlockAddResult :: OrphanBlock => false , BlockAddResult :: ChainReorg ( _ , _ ) => true , } ; self . publish_block_event ( BlockEvent :: ValidBlockAdded ( block , block_add_result , broadcast ) ) ; if should_propagate && broadcast . is_true ( ) { info ! ( target LOG_TARGET " " block_hash to_hex ( ) ) ; let exclude_peers = source_peer . into_iter ( ) . collect ( ) ; let new_block = NewBlock :: new ( block_hash ) ; self . outbound_nci . propagate_block ( new_block , exclude_peers ) . await ? ; } Ok ( ( ) ) } , Err ( e ) => { warn ! ( target LOG_TARGET " " block_height block_hash to_hex ( ) e ) ; self . publish_block_event ( BlockEvent :: AddBlockFailed ( block , broadcast ) ) ; Err ( CommsInterfaceError :: ChainStorageError ( e ) ) } , } }
Handle inbound blocks from remote nodes and local services.
Handle inbound blocks from remote nodes and local services .
&self
block
Arc<Block>
broadcast
Broadcast
source_peer
Option<NodeId>
&self
block
Arc<Block>
broadcast
Broadcast
source_peer
Option<NodeId>