Skip to content

Commit abafa52

Browse files
authored
perf(rpc): optimize block_cfg (#100)
* perf(rpc): optimize block_cfg We were unnecessarily loading a whole block for getting the block cfg, when we only need the header. This means that any calls that use block_cfg were much more expensive than needed. * v bump
1 parent dc6d81d commit abafa52

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["crates/*"]
33
resolver = "2"
44

55
[workspace.package]
6-
version = "0.8.2"
6+
version = "0.8.3"
77
edition = "2021"
88
rust-version = "1.81"
99
authors = ["init4"]

crates/rpc/src/ctx.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ where
312312
Ok(builder.build())
313313
}
314314

315+
/// Get the [`Header`] for a given block.
316+
pub async fn raw_header(
317+
&self,
318+
t: impl Into<BlockId>,
319+
) -> Result<Option<(B256, Header)>, EthApiError> {
320+
let Some(hash) = self.provider.block_hash_for_id(t.into())? else {
321+
return Ok(None);
322+
};
323+
324+
let header = self.cache.get_header(hash).await.map_err(EthApiError::from)?;
325+
326+
Ok(Some((hash, header)))
327+
}
328+
315329
/// Get the block for a given block, returning the block hash and
316330
/// the block itself.
317331
pub async fn raw_block(
@@ -513,20 +527,18 @@ where
513527
build_signet_receipt(tx, meta, receipt, all_receipts.to_vec()).map(Some)
514528
}
515529

516-
/// Create the [`Block`] object for a specific [`BlockId`].
530+
/// Create the [`Header`] object for a specific [`BlockId`].
517531
pub async fn block_cfg(&self, mut block_id: BlockId) -> Result<Header, EthApiError> {
518532
// If the block is pending, we'll load the latest and
519533
let pending = block_id.is_pending();
520534
if pending {
521535
block_id = BlockId::latest();
522536
}
523537

524-
let Some((_, block)) = self.raw_block(block_id).await? else {
538+
let Some((_, mut header)) = self.raw_header(block_id).await? else {
525539
return Err(EthApiError::HeaderNotFound(block_id));
526540
};
527541

528-
let mut header = block.clone_header();
529-
530542
// Modify the header for pending blocks, to simulate the next block.
531543
if pending {
532544
header.parent_hash = header.hash_slow();

0 commit comments

Comments
 (0)