Skip to content

Commit 1f39b61

Browse files
authored
fix: use provider for header range on fn block_range (#7429)
1 parent 50fc3aa commit 1f39b61

File tree

1 file changed

+36
-34
lines changed
  • crates/storage/provider/src/providers/database

1 file changed

+36
-34
lines changed

crates/storage/provider/src/providers/database/provider.rs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,51 +1416,53 @@ impl<TX: DbTx> BlockReader for DatabaseProvider<TX> {
14161416
let len = range.end().saturating_sub(*range.start()) as usize;
14171417
let mut blocks = Vec::with_capacity(len);
14181418

1419-
let mut headers_cursor = self.tx.cursor_read::<tables::Headers>()?;
1419+
let headers = self.headers_range(range)?;
14201420
let mut ommers_cursor = self.tx.cursor_read::<tables::BlockOmmers>()?;
14211421
let mut withdrawals_cursor = self.tx.cursor_read::<tables::BlockWithdrawals>()?;
14221422
let mut block_body_cursor = self.tx.cursor_read::<tables::BlockBodyIndices>()?;
14231423
let mut tx_cursor = self.tx.cursor_read::<tables::Transactions>()?;
14241424

1425-
for num in range {
1426-
if let Some((_, header)) = headers_cursor.seek_exact(num)? {
1427-
// If the body indices are not found, this means that the transactions either do
1428-
// not exist in the database yet, or they do exit but are
1429-
// not indexed. If they exist but are not indexed, we don't
1430-
// have enough information to return the block anyways, so
1431-
// we skip the block.
1432-
if let Some((_, block_body_indices)) = block_body_cursor.seek_exact(num)? {
1433-
let tx_range = block_body_indices.tx_num_range();
1434-
let body = if tx_range.is_empty() {
1435-
Vec::new()
1425+
for header in headers {
1426+
// If the body indices are not found, this means that the transactions either do
1427+
// not exist in the database yet, or they do exit but are
1428+
// not indexed. If they exist but are not indexed, we don't
1429+
// have enough information to return the block anyways, so
1430+
// we skip the block.
1431+
if let Some((_, block_body_indices)) = block_body_cursor.seek_exact(header.number)? {
1432+
let tx_range = block_body_indices.tx_num_range();
1433+
let body = if tx_range.is_empty() {
1434+
Vec::new()
1435+
} else {
1436+
self.transactions_by_tx_range_with_cursor(tx_range, &mut tx_cursor)?
1437+
.into_iter()
1438+
.map(Into::into)
1439+
.collect()
1440+
};
1441+
1442+
// If we are past shanghai, then all blocks should have a withdrawal list,
1443+
// even if empty
1444+
let withdrawals =
1445+
if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) {
1446+
Some(
1447+
withdrawals_cursor
1448+
.seek_exact(header.number)?
1449+
.map(|(_, w)| w.withdrawals)
1450+
.unwrap_or_default(),
1451+
)
14361452
} else {
1437-
self.transactions_by_tx_range_with_cursor(tx_range, &mut tx_cursor)?
1438-
.into_iter()
1439-
.map(Into::into)
1440-
.collect()
1453+
None
14411454
};
1442-
1443-
// If we are past shanghai, then all blocks should have a withdrawal list,
1444-
// even if empty
1445-
let withdrawals =
1446-
if self.chain_spec.is_shanghai_active_at_timestamp(header.timestamp) {
1447-
Some(
1448-
withdrawals_cursor
1449-
.seek_exact(num)?
1450-
.map(|(_, w)| w.withdrawals)
1451-
.unwrap_or_default(),
1452-
)
1453-
} else {
1454-
None
1455-
};
1456-
let ommers = if self.chain_spec.final_paris_total_difficulty(num).is_some() {
1455+
let ommers =
1456+
if self.chain_spec.final_paris_total_difficulty(header.number).is_some() {
14571457
Vec::new()
14581458
} else {
1459-
ommers_cursor.seek_exact(num)?.map(|(_, o)| o.ommers).unwrap_or_default()
1459+
ommers_cursor
1460+
.seek_exact(header.number)?
1461+
.map(|(_, o)| o.ommers)
1462+
.unwrap_or_default()
14601463
};
14611464

1462-
blocks.push(Block { header, body, ommers, withdrawals });
1463-
}
1465+
blocks.push(Block { header, body, ommers, withdrawals });
14641466
}
14651467
}
14661468
Ok(blocks)

0 commit comments

Comments
 (0)