Skip to content

Commit be451fb

Browse files
authored
fix(anvil): prevent panic in ots (#8835)
* fix(anvil): prevent panic in ots * fix(anvil): use block_by_number in ots_block_tx & ots_search_transactions * nit * nit
1 parent 2097765 commit be451fb

File tree

1 file changed

+25
-31
lines changed
  • crates/anvil/src/eth/otterscan

1 file changed

+25
-31
lines changed

crates/anvil/src/eth/otterscan/api.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -419,20 +419,19 @@ impl EthApi {
419419

420420
let receipt_futs = block.transactions.hashes().map(|hash| self.transaction_receipt(hash));
421421

422-
let receipts = join_all(receipt_futs)
423-
.await
424-
.into_iter()
425-
.map(|r| match r {
426-
Ok(Some(r)) => {
427-
let timestamp =
428-
self.backend.get_block(r.block_number.unwrap()).unwrap().header.timestamp;
429-
let receipt = r.map_inner(OtsReceipt::from);
430-
let res = OtsTransactionReceipt { receipt, timestamp: Some(timestamp) };
431-
Ok(res)
432-
}
433-
_ => Err(BlockchainError::DataUnavailable),
434-
})
435-
.collect::<Result<Vec<_>>>()?;
422+
let receipts = join_all(receipt_futs.map(|r| async {
423+
if let Ok(Some(r)) = r.await {
424+
let block = self.block_by_number(r.block_number.unwrap().into()).await?;
425+
let timestamp = block.ok_or(BlockchainError::BlockNotFound)?.header.timestamp;
426+
let receipt = r.map_inner(OtsReceipt::from);
427+
Ok(OtsTransactionReceipt { receipt, timestamp: Some(timestamp) })
428+
} else {
429+
Err(BlockchainError::BlockNotFound)
430+
}
431+
}))
432+
.await
433+
.into_iter()
434+
.collect::<Result<Vec<_>>>()?;
436435

437436
let transaction_count = block.transactions().len();
438437
let fullblock = OtsBlock { block: block.inner, transaction_count };
@@ -458,23 +457,18 @@ impl EthApi {
458457
Ok(Some(t)) => Ok(t.inner),
459458
_ => Err(BlockchainError::DataUnavailable),
460459
})
461-
.collect::<Result<_>>()?;
462-
463-
let receipts = join_all(hashes.iter().map(|hash| async {
464-
match self.transaction_receipt(*hash).await {
465-
Ok(Some(receipt)) => {
466-
let timestamp = self
467-
.backend
468-
.get_block(receipt.block_number.unwrap())
469-
.unwrap()
470-
.header
471-
.timestamp;
472-
let receipt = receipt.map_inner(OtsReceipt::from);
473-
let res = OtsTransactionReceipt { receipt, timestamp: Some(timestamp) };
474-
Ok(res)
475-
}
476-
Ok(None) => Err(BlockchainError::DataUnavailable),
477-
Err(e) => Err(e),
460+
.collect::<Result<Vec<_>>>()?;
461+
462+
let receipt_futs = hashes.iter().map(|hash| self.transaction_receipt(*hash));
463+
464+
let receipts = join_all(receipt_futs.map(|r| async {
465+
if let Ok(Some(r)) = r.await {
466+
let block = self.block_by_number(r.block_number.unwrap().into()).await?;
467+
let timestamp = block.ok_or(BlockchainError::BlockNotFound)?.header.timestamp;
468+
let receipt = r.map_inner(OtsReceipt::from);
469+
Ok(OtsTransactionReceipt { receipt, timestamp: Some(timestamp) })
470+
} else {
471+
Err(BlockchainError::BlockNotFound)
478472
}
479473
}))
480474
.await

0 commit comments

Comments
 (0)