Skip to content

Commit

Permalink
fix: use new TransportResult (#6342)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg authored Nov 17, 2023
1 parent 8f55ab3 commit 2f513fb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 68 deletions.
4 changes: 2 additions & 2 deletions crates/cheatcodes/src/evm/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alloy_primitives::{FixedBytes, B256, U256};
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::{Filter, Topic};
use alloy_sol_types::SolValue;
use eyre::WrapErr;
use foundry_common::ProviderBuilder;
use foundry_compilers::utils::RuntimeOrHandle;
use foundry_evm_core::fork::CreateFork;
Expand Down Expand Up @@ -273,8 +274,7 @@ impl Cheatcode for eth_getLogsCall {
// todo: handle the errors somehow
let logs = RuntimeOrHandle::new()
.block_on(provider.get_logs(filter))
.success()
.ok_or_else(|| eyre::eyre!("failed to get logs"))?;
.wrap_err("failed to get logs")?;

let eth_logs = logs
.into_iter()
Expand Down
13 changes: 2 additions & 11 deletions crates/common/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,7 @@ pub async fn estimate_eip1559_fees<P: TempProvider>(
let chain = if let Some(chain) = chain {
chain
} else {
provider
.get_chain_id()
.await
.success()
.ok_or_else(|| eyre::eyre!("Failed to get chain id"))?
.to()
provider.get_chain_id().await.wrap_err("Failed to get chain id")?.to()
};

if let Ok(chain) = NamedChain::try_from(chain) {
Expand All @@ -300,11 +295,7 @@ pub async fn estimate_eip1559_fees<P: TempProvider>(
_ => {}
}
}
provider
.estimate_eip1559_fees(None)
.await
.success()
.ok_or_else(|| eyre::eyre!("Failed fetch EIP1559 fees"))
provider.estimate_eip1559_fees(None).await.wrap_err("Failed fetch EIP1559 fees")
}

#[cfg(not(windows))]
Expand Down
43 changes: 9 additions & 34 deletions crates/evm/core/src/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use alloy_primitives::{keccak256, Address, Bytes, B256, U256};
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::{Block, BlockId, BlockNumberOrTag, Transaction};
use eyre::WrapErr;
use foundry_common::NON_ARCHIVE_NODE_WARNING;
use futures::{
channel::mpsc::{channel, Receiver, Sender},
Expand Down Expand Up @@ -181,13 +182,7 @@ where
// serialize & deserialize back to U256
let idx_req = B256::from(idx);
let storage = provider.get_storage_at(address, idx_req, block_id).await;
(
storage.success().ok_or_else(|| {
eyre::eyre!("could not fetch slot {idx} from {address}")
}),
address,
idx,
)
(storage.wrap_err("could not fetch slot {idx} from {address}"), address, idx)
});
self.pending_requests.push(ProviderRequest::Storage(fut));
}
Expand All @@ -204,23 +199,8 @@ where
let nonce = provider.get_transaction_count(address, block_id);
let code =
provider.get_code_at(address, block_id.unwrap_or(BlockNumberOrTag::Latest.into()));
let (balance, nonce, code) = tokio::join!(balance, nonce, code);
(
balance
.success()
.ok_or_else(|| eyre::eyre!("could not fetch balance for {address}"))
.and_then(|balance| {
Ok((
balance,
nonce.success().ok_or_else(|| {
eyre::eyre!("could not fetch nonce for {address}")
})?,
code.success()
.ok_or_else(|| eyre::eyre!("could not fetch code for {address}"))?,
))
}),
address,
)
let resp = tokio::try_join!(balance, nonce, code).map_err(Into::into);
(resp, address)
});
ProviderRequest::Account(fut)
}
Expand All @@ -242,11 +222,8 @@ where
fn request_full_block(&mut self, number: BlockId, sender: FullBlockSender) {
let provider = self.provider.clone();
let fut = Box::pin(async move {
let block = provider
.get_block(number, true)
.await
.success()
.ok_or_else(|| eyre::eyre!("could not fetch block {number:?}"));
let block =
provider.get_block(number, true).await.wrap_err("could not fetch block {number:?}");
(sender, block, number)
});

Expand All @@ -260,8 +237,7 @@ where
let block = provider
.get_transaction_by_hash(tx)
.await
.success()
.ok_or_else(|| eyre::eyre!("could not get transaction {tx}"));
.wrap_err("could not get transaction {tx}");
(sender, block, tx)
});

Expand All @@ -282,8 +258,7 @@ where
let block = provider
.get_block_by_number(number, false)
.await
.success()
.ok_or_else(|| eyre::eyre!("failed to get block"));
.wrap_err("failed to get block");

let block_hash = match block {
Ok(Some(block)) => Ok(block
Expand Down Expand Up @@ -772,7 +747,7 @@ mod tests {
async fn can_read_write_cache() {
let provider = get_http_provider(ENDPOINT);

let block_num = provider.get_block_number().await.success().unwrap().to();
let block_num = provider.get_block_number().await.unwrap().to();

let config = Config::figment();
let mut evm_opts = config.extract::<EvmOpts>().unwrap();
Expand Down
30 changes: 9 additions & 21 deletions crates/evm/core/src/fork/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::utils::apply_chain_and_block_specific_env_changes;
use alloy_primitives::{Address, U256, U64};
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::{Block, BlockNumberOrTag};
use eyre::WrapErr;
use foundry_common::NON_ARCHIVE_NODE_WARNING;
use revm::primitives::{BlockEnv, CfgEnv, Env, TxEnv};

Expand All @@ -17,32 +18,19 @@ pub async fn environment<P: TempProvider>(
origin: Address,
) -> eyre::Result<(Env, Block)> {
let block_number = if let Some(pin_block) = pin_block {
U256::from(pin_block)
U64::from(pin_block)
} else {
provider
.get_block_number()
.await
.success()
.ok_or_else(|| eyre::eyre!("Failed to get latest block number"))?
};
let (fork_gas_price, rpc_chain_id, block) = {
// todo(onbjerg): we can't use try_join! because the results returned by alloy's provider
// are not actual `Result`s
let (fork_gas_price, rpc_chain_id, block) = tokio::join!(
provider.get_gas_price(),
provider.get_chain_id(),
provider.get_block_by_number(BlockNumberOrTag::Number(block_number.to()), false)
);
(
fork_gas_price.success().ok_or_else(|| eyre::eyre!("Failed to get gas price"))?,
rpc_chain_id.success().ok_or_else(|| eyre::eyre!("Failed to get chain id"))?,
block.success().ok_or_else(|| eyre::eyre!("Failed to get block {block_number}"))?,
)
provider.get_block_number().await.wrap_err("Failed to get latest block number")?
};
let (fork_gas_price, rpc_chain_id, block) = tokio::try_join!(
provider.get_gas_price(),
provider.get_chain_id(),
provider.get_block_by_number(BlockNumberOrTag::Number(block_number.to()), false)
)?;
let block = if let Some(block) = block {
block
} else {
if let Some(latest_block) = provider.get_block_number().await.success() {
if let Ok(latest_block) = provider.get_block_number().await {
// If the `eth_getBlockByNumber` call succeeds, but returns null instead of
// the block, and the block number is less than equal the latest block, then
// the user is forking from a non-archive node with an older block number.
Expand Down

0 comments on commit 2f513fb

Please sign in to comment.