Skip to content

Commit fb5779d

Browse files
authored
revive: Skip redundant eth_block_hash RPC call in block subscription (#11476)
backport #11475 - skip one cache call - add some extra logs
1 parent 7d4d8cc commit fb5779d

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

prdoc/unstable2507/pr_11476.prdoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
title: 'revive: Skip redundant eth_block_hash RPC call in block subscription'
2+
doc:
3+
- audience: Runtime Dev
4+
description: |-
5+
Backport of #11475. Skip a redundant eth_block_hash RPC call during block subscription by
6+
reusing the block hash already available in the subscription context. Adds extra logging for
7+
receipt extraction.
8+
crates:
9+
- name: pallet-revive-eth-rpc
10+
bump: patch

substrate/frame/revive/rpc/src/client.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,30 @@ impl Client {
361361
) -> Result<(), ClientError> {
362362
log::info!(target: LOG_TARGET, "🔌 Subscribing to new blocks ({subscription_type:?})");
363363
self.subscribe_new_blocks(subscription_type, |block| async {
364+
macro_rules! time {
365+
($label:expr, $expr:expr) => {{
366+
let t = std::time::Instant::now();
367+
let r = $expr;
368+
log::trace!(
369+
target: LOG_TARGET,
370+
"⏱️ [{subscription_type:?}] #{} {}: {:?}",
371+
block.number(),
372+
$label,
373+
t.elapsed(),
374+
);
375+
r
376+
}};
377+
}
378+
364379
let hash = block.hash();
365-
let evm_block = self.runtime_api(hash).eth_block().await?;
366-
let (_, receipts): (Vec<_>, Vec<_>) = self
367-
.receipt_provider
368-
.insert_block_receipts(&block, &evm_block.hash)
369-
.await?
370-
.into_iter()
371-
.unzip();
380+
let evm_block = time!("eth_block", self.runtime_api(hash).eth_block().await?);
381+
382+
let (_, receipts): (Vec<_>, Vec<_>) = time!(
383+
"insert_block_receipts",
384+
self.receipt_provider.insert_block_receipts(&block, &evm_block.hash).await?
385+
)
386+
.into_iter()
387+
.unzip();
372388

373389
self.block_provider.update_latest(Arc::new(block), subscription_type).await;
374390
self.fee_history_provider.update_fee_history(&evm_block, &receipts).await;

substrate/frame/revive/rpc/src/receipt_extractor.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,26 @@ impl ReceiptExtractor {
232232
pub async fn extract_from_block(
233233
&self,
234234
block: &SubstrateBlock,
235+
) -> Result<Vec<(TransactionSigned, ReceiptInfo)>, ClientError> {
236+
let eth_block_hash = self
237+
.get_ethereum_block_hash(&block.hash(), block.number() as u64)
238+
.await
239+
.unwrap_or(block.hash());
240+
self.extract_from_block_with_eth_hash(block, eth_block_hash).await
241+
}
242+
243+
/// Extract receipts from block, using a pre-fetched ethereum block hash.
244+
pub async fn extract_from_block_with_eth_hash(
245+
&self,
246+
block: &SubstrateBlock,
247+
eth_block_hash: H256,
235248
) -> Result<Vec<(TransactionSigned, ReceiptInfo)>, ClientError> {
236249
if self.is_before_earliest_block(block.number()) {
237250
return Ok(vec![]);
238251
}
239252

240253
let ext_iter = self.get_block_extrinsics(block).await?;
241254

242-
let substrate_block_number = block.number() as u64;
243-
let substrate_block_hash = block.hash();
244-
let eth_block_hash =
245-
(self.fetch_eth_block_hash)(substrate_block_hash, substrate_block_number)
246-
.await
247-
.unwrap_or(substrate_block_hash);
248-
249255
// Process extrinsics in order while maintaining parallelism within buffer window
250256
stream::iter(ext_iter)
251257
.map(|(ext, call, receipt, ext_idx)| async move {

substrate/frame/revive/rpc/src/receipt_provider.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ impl<B: BlockInfoProvider> ReceiptProvider<B> {
245245
block: &SubstrateBlock,
246246
ethereum_hash: &H256,
247247
) -> Result<Vec<(TransactionSigned, ReceiptInfo)>, ClientError> {
248-
let receipts = self.receipts_from_block(block).await?;
248+
let receipts = self
249+
.receipt_extractor
250+
.extract_from_block_with_eth_hash(block, *ethereum_hash)
251+
.await?;
249252
self.insert(block, &receipts, ethereum_hash).await?;
250253
Ok(receipts)
251254
}

0 commit comments

Comments
 (0)