From 650e08fb8cab2c1b7626b7e46c55174ad1353cfd Mon Sep 17 00:00:00 2001 From: ptisserand Date: Tue, 16 Apr 2024 09:52:21 +0200 Subject: [PATCH] fix(indexer): don't panic when failed to get ethereum block timestamp (#187) --- apps/indexer/src/ethereum_indexer/client.rs | 31 ++++++++++++-------- apps/indexer/src/ethereum_indexer/indexer.rs | 2 +- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/apps/indexer/src/ethereum_indexer/client.rs b/apps/indexer/src/ethereum_indexer/client.rs index fcbb15d6..e9d62351 100644 --- a/apps/indexer/src/ethereum_indexer/client.rs +++ b/apps/indexer/src/ethereum_indexer/client.rs @@ -94,20 +94,27 @@ impl EthereumClient { } } - pub async fn get_block_timestamp(&self, block_id: u64) -> u64 { - match self - .provider - .get_block(block_id) - .await - .expect(&format!("Can't fetch block {}", block_id)) - { - None => 0, - Some(block) => block - .timestamp - .try_into() - .expect("Can't convert block timestamp to u64"), + pub async fn get_block_timestamp(&self, block_id: u64) -> Result { + let block = self.provider.get_block(block_id).await; + if block.is_ok() { + match block.unwrap() { + None => Ok(0), + Some(block) => match block.timestamp.try_into() { + Ok(v) => Ok(v), + Err(e) => { + let msg = format!("Failed to convert timestamp {}: {:?}", block_id, e); + log::warn!("{}", msg); + Err(anyhow!(msg)) + } + }, + } + } else { + let msg = format!("Eth retrieving block timestamp {}: {:?}", block_id, block); + log::error!("{}", msg); + Err(anyhow!(msg)) } } + /// Fetches logs for the given block options. /// /// There is not pagination in ethereum, and no hard limit on block range. diff --git a/apps/indexer/src/ethereum_indexer/indexer.rs b/apps/indexer/src/ethereum_indexer/indexer.rs index 53bdbb96..239442f2 100644 --- a/apps/indexer/src/ethereum_indexer/indexer.rs +++ b/apps/indexer/src/ethereum_indexer/indexer.rs @@ -290,7 +290,7 @@ where async fn process_pending_withdraws(&self, block_number: u64) -> Result<()> { let pendings = self.store.get_pending_withdraws().await?; - let timestamp = self.client.get_block_timestamp(block_number).await; + let timestamp = self.client.get_block_timestamp(block_number).await?; for pending in pendings { let status = self .client