From 5383f4b08e31b8ff828ca3cfad419a4a9fe10cc3 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 11 Feb 2025 19:09:37 +0100 Subject: [PATCH] perf: only fetch parent if not latest (#14412) --- crates/rpc/rpc/src/validation.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/crates/rpc/rpc/src/validation.rs b/crates/rpc/rpc/src/validation.rs index cd512b488..e1597aaac 100644 --- a/crates/rpc/rpc/src/validation.rs +++ b/crates/rpc/rpc/src/validation.rs @@ -139,14 +139,23 @@ where let latest_header = self.provider.latest_header()?.ok_or_else(|| ValidationApiError::MissingLatestBlock)?; - let parent_header = self - .provider - .sealed_header_by_hash(block.parent_hash())? - .ok_or_else(|| ValidationApiError::MissingParentBlock)?; + let parent_header = if block.parent_hash() == latest_header.hash() { + latest_header + } else { + // parent is not the latest header so we need to fetch it and ensure it's not too old + let parent_header = self + .provider + .sealed_header_by_hash(block.parent_hash())? + .ok_or_else(|| ValidationApiError::MissingParentBlock)?; + + if latest_header.number().saturating_sub(parent_header.number()) > + self.validation_window + { + return Err(ValidationApiError::BlockTooOld) + } + parent_header + }; - if latest_header.number().saturating_sub(parent_header.number()) > self.validation_window { - return Err(ValidationApiError::BlockTooOld) - } self.consensus.validate_header_against_parent(block.sealed_header(), &parent_header)?; self.validate_gas_limit(registered_gas_limit, &parent_header, block.sealed_header())?; let parent_header_hash = parent_header.hash();