Skip to content

Commit 24796c7

Browse files
committed
revive: Skip redundant eth_block_hash RPC call in block subscription
insert_block_receipts already receives the ethereum block hash from the caller (via eth_block().hash), but extract_from_block was fetching it again via a separate state_call RPC. This redundant call occasionally stalls for 10-20s on public RPC nodes, causing the block cache to fall behind and the health check to report out-of-sync. Add extract_from_block_with_eth_hash that accepts a pre-fetched hash, and use it from insert_block_receipts. The original extract_from_block still fetches the hash for callers that don't have it.
1 parent a754446 commit 24796c7

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,27 @@ impl Client {
471471
) -> Result<(), ClientError> {
472472
log::info!(target: LOG_TARGET, "🔌 Subscribing to new blocks ({subscription_type:?})");
473473
self.subscribe_new_blocks(subscription_type, |block| async {
474+
macro_rules! time {
475+
($label:expr, $expr:expr) => {{
476+
let t = std::time::Instant::now();
477+
let r = $expr;
478+
log::trace!(target: LOG_TARGET, "⏱️ [{subscription_type:?}] #{} {}: {:?}", block.number(), $label, t.elapsed());
479+
r
480+
}};
481+
}
482+
474483
let hash = block.hash();
475484
let block_number = block.number();
476-
let evm_block = self.runtime_api(hash).eth_block().await?;
477-
478-
let (_, receipts): (Vec<_>, Vec<_>) = self
479-
.receipt_provider
480-
.insert_block_receipts(&block, &evm_block.hash)
481-
.await?
482-
.into_iter()
483-
.unzip();
485+
let evm_block = time!("eth_block", self.runtime_api(hash).eth_block().await?);
486+
487+
let (_, receipts): (Vec<_>, Vec<_>) = time!(
488+
"insert_block_receipts",
489+
self.receipt_provider
490+
.insert_block_receipts(&block, &evm_block.hash)
491+
.await?
492+
)
493+
.into_iter()
494+
.unzip();
484495

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

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,20 +252,25 @@ impl ReceiptExtractor {
252252
pub async fn extract_from_block(
253253
&self,
254254
block: &SubstrateBlock,
255+
) -> Result<Vec<(TransactionSigned, ReceiptInfo)>, ClientError> {
256+
let eth_block_hash = (self.fetch_eth_block_hash)(block.hash(), block.number() as u64)
257+
.await
258+
.unwrap_or(block.hash());
259+
self.extract_from_block_with_eth_hash(block, eth_block_hash).await
260+
}
261+
262+
/// Extract receipts from block, using a pre-fetched ethereum block hash.
263+
pub async fn extract_from_block_with_eth_hash(
264+
&self,
265+
block: &SubstrateBlock,
266+
eth_block_hash: H256,
255267
) -> Result<Vec<(TransactionSigned, ReceiptInfo)>, ClientError> {
256268
if self.is_before_first_evm_block(block.number()) {
257269
return Ok(vec![]);
258270
}
259271

260272
let ext_iter = self.get_block_extrinsics(block).await?;
261273

262-
let substrate_block_number = block.number() as u64;
263-
let substrate_block_hash = block.hash();
264-
let eth_block_hash =
265-
(self.fetch_eth_block_hash)(substrate_block_hash, substrate_block_number)
266-
.await
267-
.unwrap_or(substrate_block_hash);
268-
269274
// Process extrinsics in order while maintaining parallelism within buffer window
270275
stream::iter(ext_iter)
271276
.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
@@ -445,7 +445,10 @@ impl<B: BlockInfoProvider> ReceiptProvider<B> {
445445
block: &SubstrateBlock,
446446
ethereum_hash: &H256,
447447
) -> Result<Vec<(TransactionSigned, ReceiptInfo)>, ClientError> {
448-
let receipts = self.receipts_from_block(block).await?;
448+
let receipts = self
449+
.receipt_extractor
450+
.extract_from_block_with_eth_hash(block, *ethereum_hash)
451+
.await?;
449452
self.insert(block, &receipts, ethereum_hash).await?;
450453
Ok(receipts)
451454
}

0 commit comments

Comments
 (0)