perf: only fetch parent if not latest (#14412)

This commit is contained in:
Matthias Seitz
2025-02-11 19:09:37 +01:00
committed by GitHub
parent d727701a75
commit 5383f4b08e

View File

@ -139,14 +139,23 @@ where
let latest_header = let latest_header =
self.provider.latest_header()?.ok_or_else(|| ValidationApiError::MissingLatestBlock)?; self.provider.latest_header()?.ok_or_else(|| ValidationApiError::MissingLatestBlock)?;
let parent_header = self let parent_header = if block.parent_hash() == latest_header.hash() {
.provider latest_header
.sealed_header_by_hash(block.parent_hash())? } else {
.ok_or_else(|| ValidationApiError::MissingParentBlock)?; // 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.consensus.validate_header_against_parent(block.sealed_header(), &parent_header)?;
self.validate_gas_limit(registered_gas_limit, &parent_header, block.sealed_header())?; self.validate_gas_limit(registered_gas_limit, &parent_header, block.sealed_header())?;
let parent_header_hash = parent_header.hash(); let parent_header_hash = parent_header.hash();