perf(provider): return empty ommers after merge (#3222)

This commit is contained in:
Roman Krasiuk
2023-06-19 11:50:00 +03:00
committed by GitHub
parent a8be68a82c
commit 37e8f7b140
2 changed files with 10 additions and 6 deletions

View File

@@ -221,11 +221,11 @@ impl ChainSpec {
}
}
/// Returns the final difficulty if the given block number is after the Paris hardfork.
/// Returns the final total difficulty if the given block number is after the Paris hardfork.
///
/// Note: technically this would also be valid for the block before the paris upgrade, but this
/// edge case is omitted here.
pub fn final_paris_difficulty(&self, block_number: u64) -> Option<U256> {
pub fn final_paris_total_difficulty(&self, block_number: u64) -> Option<U256> {
self.paris_block_and_final_difficulty.and_then(|(activated_at, final_difficulty)| {
if block_number >= activated_at {
Some(final_difficulty)

View File

@@ -1410,7 +1410,7 @@ impl<'this, TX: DbTx<'this>> HeaderProvider for DatabaseProvider<'this, TX> {
}
fn header_td_by_number(&self, number: BlockNumber) -> Result<Option<U256>> {
if let Some(td) = self.chain_spec.final_paris_difficulty(number) {
if let Some(td) = self.chain_spec.final_paris_total_difficulty(number) {
// if this block is higher than the final paris(merge) block, return the final paris
// difficulty
return Ok(Some(td))
@@ -1505,8 +1505,7 @@ impl<'this, TX: DbTx<'this>> BlockProvider for DatabaseProvider<'this, TX> {
if let Some(number) = self.convert_hash_or_number(id)? {
if let Some(header) = self.header_by_number(number)? {
let withdrawals = self.withdrawals_by_block(number.into(), header.timestamp)?;
let ommers = if withdrawals.is_none() { self.ommers(number.into())? } else { None }
.unwrap_or_default();
let ommers = self.ommers(number.into())?.unwrap_or_default();
let transactions = self
.transactions_by_block(number.into())?
.ok_or(ProviderError::BlockBodyIndicesNotFound(number))?;
@@ -1524,7 +1523,12 @@ impl<'this, TX: DbTx<'this>> BlockProvider for DatabaseProvider<'this, TX> {
fn ommers(&self, id: BlockHashOrNumber) -> Result<Option<Vec<Header>>> {
if let Some(number) = self.convert_hash_or_number(id)? {
// TODO: this can be optimized to return empty Vec post-merge
// If the Paris (Merge) hardfork block is known and block is after it, return empty
// ommers.
if self.chain_spec.final_paris_total_difficulty(number).is_some() {
return Ok(Some(Vec::new()))
}
let ommers = self.tx.get::<tables::BlockOmmers>(number)?.map(|o| o.ommers);
return Ok(ommers)
}