From a3a370302a2e97a736fe5d952352bd47048c9719 Mon Sep 17 00:00:00 2001 From: Pawan Dhananjay Date: Wed, 13 Dec 2023 14:24:36 -0800 Subject: [PATCH] Use the block header to compute the canonical_root (#5003) --- .../beacon_chain/src/block_verification.rs | 21 +++++++++++++++++-- beacon_node/beacon_chain/src/metrics.rs | 4 ++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/beacon_node/beacon_chain/src/block_verification.rs b/beacon_node/beacon_chain/src/block_verification.rs index 145614c4b..23c9ab725 100644 --- a/beacon_node/beacon_chain/src/block_verification.rs +++ b/beacon_node/beacon_chain/src/block_verification.rs @@ -780,7 +780,10 @@ impl GossipVerifiedBlock { // it to the slasher if an error occurs, because that's the end of this block's journey, // and it could be a repeat proposal (a likely cause for slashing!). let header = block.signed_block_header(); - Self::new_without_slasher_checks(block, chain).map_err(|e| { + // The `SignedBeaconBlock` and `SignedBeaconBlockHeader` have the same canonical root, + // but it's way quicker to calculate root of the header since the hash of the tree rooted + // at `BeaconBlockBody` is already computed in the header. + Self::new_without_slasher_checks(block, &header, chain).map_err(|e| { process_block_slash_info::<_, BlockError>( chain, BlockSlashInfo::from_early_error_block(header, e), @@ -791,6 +794,7 @@ impl GossipVerifiedBlock { /// As for new, but doesn't pass the block to the slasher. fn new_without_slasher_checks( block: Arc>, + block_header: &SignedBeaconBlockHeader, chain: &BeaconChain, ) -> Result> { // Ensure the block is the correct structure for the fork at `block.slot()`. @@ -810,7 +814,7 @@ impl GossipVerifiedBlock { }); } - let block_root = get_block_root(&block); + let block_root = get_block_header_root(block_header); // Disallow blocks that conflict with the anchor (weak subjectivity checkpoint), if any. check_block_against_anchor_slot(block.message(), chain)?; @@ -1771,6 +1775,19 @@ pub fn get_block_root(block: &SignedBeaconBlock) -> Hash256 { block_root } +/// Returns the canonical root of the given `block_header`. +/// +/// Use this function to ensure that we report the block hashing time Prometheus metric. +pub fn get_block_header_root(block_header: &SignedBeaconBlockHeader) -> Hash256 { + let block_root_timer = metrics::start_timer(&metrics::BLOCK_HEADER_PROCESSING_BLOCK_ROOT); + + let block_root = block_header.message.canonical_root(); + + metrics::stop_timer(block_root_timer); + + block_root +} + /// Verify the parent of `block` is known, returning some information about the parent block from /// fork choice. #[allow(clippy::type_complexity)] diff --git a/beacon_node/beacon_chain/src/metrics.rs b/beacon_node/beacon_chain/src/metrics.rs index ca04366b0..b9a748b6d 100644 --- a/beacon_node/beacon_chain/src/metrics.rs +++ b/beacon_node/beacon_chain/src/metrics.rs @@ -40,6 +40,10 @@ lazy_static! { "beacon_block_processing_block_root_seconds", "Time spent calculating the block root when processing a block." ); + pub static ref BLOCK_HEADER_PROCESSING_BLOCK_ROOT: Result = try_create_histogram( + "beacon_block_header_processing_block_root_seconds", + "Time spent calculating the block root for a beacon block header." + ); pub static ref BLOCK_PROCESSING_BLOB_ROOT: Result = try_create_histogram( "beacon_block_processing_blob_root_seconds", "Time spent calculating the blob root when processing a block."