Skip to content

Commit a3a3703

Browse files
authored
Use the block header to compute the canonical_root (sigp#5003)
1 parent a3fb27c commit a3a3703

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

beacon_node/beacon_chain/src/block_verification.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,10 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
780780
// it to the slasher if an error occurs, because that's the end of this block's journey,
781781
// and it could be a repeat proposal (a likely cause for slashing!).
782782
let header = block.signed_block_header();
783-
Self::new_without_slasher_checks(block, chain).map_err(|e| {
783+
// The `SignedBeaconBlock` and `SignedBeaconBlockHeader` have the same canonical root,
784+
// but it's way quicker to calculate root of the header since the hash of the tree rooted
785+
// at `BeaconBlockBody` is already computed in the header.
786+
Self::new_without_slasher_checks(block, &header, chain).map_err(|e| {
784787
process_block_slash_info::<_, BlockError<T::EthSpec>>(
785788
chain,
786789
BlockSlashInfo::from_early_error_block(header, e),
@@ -791,6 +794,7 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
791794
/// As for new, but doesn't pass the block to the slasher.
792795
fn new_without_slasher_checks(
793796
block: Arc<SignedBeaconBlock<T::EthSpec>>,
797+
block_header: &SignedBeaconBlockHeader,
794798
chain: &BeaconChain<T>,
795799
) -> Result<Self, BlockError<T::EthSpec>> {
796800
// Ensure the block is the correct structure for the fork at `block.slot()`.
@@ -810,7 +814,7 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
810814
});
811815
}
812816

813-
let block_root = get_block_root(&block);
817+
let block_root = get_block_header_root(block_header);
814818

815819
// Disallow blocks that conflict with the anchor (weak subjectivity checkpoint), if any.
816820
check_block_against_anchor_slot(block.message(), chain)?;
@@ -1771,6 +1775,19 @@ pub fn get_block_root<E: EthSpec>(block: &SignedBeaconBlock<E>) -> Hash256 {
17711775
block_root
17721776
}
17731777

1778+
/// Returns the canonical root of the given `block_header`.
1779+
///
1780+
/// Use this function to ensure that we report the block hashing time Prometheus metric.
1781+
pub fn get_block_header_root(block_header: &SignedBeaconBlockHeader) -> Hash256 {
1782+
let block_root_timer = metrics::start_timer(&metrics::BLOCK_HEADER_PROCESSING_BLOCK_ROOT);
1783+
1784+
let block_root = block_header.message.canonical_root();
1785+
1786+
metrics::stop_timer(block_root_timer);
1787+
1788+
block_root
1789+
}
1790+
17741791
/// Verify the parent of `block` is known, returning some information about the parent block from
17751792
/// fork choice.
17761793
#[allow(clippy::type_complexity)]

beacon_node/beacon_chain/src/metrics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ lazy_static! {
4040
"beacon_block_processing_block_root_seconds",
4141
"Time spent calculating the block root when processing a block."
4242
);
43+
pub static ref BLOCK_HEADER_PROCESSING_BLOCK_ROOT: Result<Histogram> = try_create_histogram(
44+
"beacon_block_header_processing_block_root_seconds",
45+
"Time spent calculating the block root for a beacon block header."
46+
);
4347
pub static ref BLOCK_PROCESSING_BLOB_ROOT: Result<Histogram> = try_create_histogram(
4448
"beacon_block_processing_blob_root_seconds",
4549
"Time spent calculating the blob root when processing a block."

0 commit comments

Comments
 (0)