Skip to content

Commit

Permalink
Merge branch 'main' into v2-forge-test
Browse files Browse the repository at this point in the history
  • Loading branch information
asaj committed Dec 1, 2022
2 parents ef8db69 + 0f49c66 commit 32c901f
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions rust/chains/hyperlane-ethereum/src/provider.rs
@@ -1,9 +1,12 @@
use std::fmt::Debug;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use ethers::prelude::{Middleware, H256};
use eyre::eyre;
use tokio::time::sleep;
use tracing::instrument;

use hyperlane_core::{
Expand Down Expand Up @@ -44,11 +47,7 @@ where
{
#[instrument(err, skip(self))]
async fn get_block_by_hash(&self, hash: &H256) -> eyre::Result<BlockInfo> {
let block = self
.provider
.get_block(*hash)
.await?
.ok_or_else(|| eyre!("Could not find block with hash {}", hash))?;
let block = get_with_retry_on_none(|| self.provider.get_block(*hash)).await?;
Ok(BlockInfo {
hash: *hash,
timestamp: block.timestamp.as_u64(),
Expand All @@ -61,11 +60,7 @@ where

#[instrument(err, skip(self))]
async fn get_txn_by_hash(&self, hash: &H256) -> eyre::Result<TxnInfo> {
let txn = self
.provider
.get_transaction(*hash)
.await?
.ok_or_else(|| eyre!("Could not find txn with hash {}", hash))?;
let txn = get_with_retry_on_none(|| self.provider.get_transaction(*hash)).await?;
let receipt = self
.provider
.get_transaction_receipt(*hash)
Expand Down Expand Up @@ -114,3 +109,23 @@ impl MakeableWithProvider for HyperlaneProviderBuilder {
})
}
}

/// Call a get function that returns a Result<Option<T>> and retry if the inner
/// option is None. This can happen because the provider has not discovered the
/// object we are looking for yet.
async fn get_with_retry_on_none<T, F, O, E>(get: F) -> eyre::Result<T>
where
F: Fn() -> O,
O: Future<Output = Result<Option<T>, E>>,
E: std::error::Error + Send + Sync + 'static,
{
for _ in 0..3 {
if let Some(t) = get().await? {
return Ok(t);
} else {
sleep(Duration::from_secs(5)).await;
continue;
};
}
Err(eyre!("Could not find object from provider"))
}

0 comments on commit 32c901f

Please sign in to comment.