diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 9ac9785e098..03ecee0d85c 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -1416,51 +1416,53 @@ impl BlockReader for DatabaseProvider { let len = range.end().saturating_sub(*range.start()) as usize; let mut blocks = Vec::with_capacity(len); - let mut headers_cursor = self.tx.cursor_read::()?; + let headers = self.headers_range(range)?; let mut ommers_cursor = self.tx.cursor_read::()?; let mut withdrawals_cursor = self.tx.cursor_read::()?; let mut block_body_cursor = self.tx.cursor_read::()?; let mut tx_cursor = self.tx.cursor_read::()?; - for num in range { - if let Some((_, header)) = headers_cursor.seek_exact(num)? { - // If the body indices are not found, this means that the transactions either do - // not exist in the database yet, or they do exit but are - // not indexed. If they exist but are not indexed, we don't - // have enough information to return the block anyways, so - // we skip the block. - if let Some((_, block_body_indices)) = block_body_cursor.seek_exact(num)? { - let tx_range = block_body_indices.tx_num_range(); - let body = if tx_range.is_empty() { - Vec::new() + for header in headers { + // If the body indices are not found, this means that the transactions either do + // not exist in the database yet, or they do exit but are + // not indexed. If they exist but are not indexed, we don't + // have enough information to return the block anyways, so + // we skip the block. + if let Some((_, block_body_indices)) = block_body_cursor.seek_exact(header.number)? { + let tx_range = block_body_indices.tx_num_range(); + let body = if tx_range.is_empty() { + Vec::new() + } else { + self.transactions_by_tx_range_with_cursor(tx_range, &mut tx_cursor)? + .into_iter() + .map(Into::into) + .collect() + }; + + // If we are past shanghai, then all blocks should have a withdrawal list, + // even if empty + let withdrawals = + if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) { + Some( + withdrawals_cursor + .seek_exact(header.number)? + .map(|(_, w)| w.withdrawals) + .unwrap_or_default(), + ) } else { - self.transactions_by_tx_range_with_cursor(tx_range, &mut tx_cursor)? - .into_iter() - .map(Into::into) - .collect() + None }; - - // If we are past shanghai, then all blocks should have a withdrawal list, - // even if empty - let withdrawals = - if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) { - Some( - withdrawals_cursor - .seek_exact(num)? - .map(|(_, w)| w.withdrawals) - .unwrap_or_default(), - ) - } else { - None - }; - let ommers = if self.chain_spec.final_paris_total_difficulty(num).is_some() { + let ommers = + if self.chain_spec.final_paris_total_difficulty(header.number).is_some() { Vec::new() } else { - ommers_cursor.seek_exact(num)?.map(|(_, o)| o.ommers).unwrap_or_default() + ommers_cursor + .seek_exact(header.number)? + .map(|(_, o)| o.ommers) + .unwrap_or_default() }; - blocks.push(Block { header, body, ommers, withdrawals }); - } + blocks.push(Block { header, body, ommers, withdrawals }); } } Ok(blocks)