Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split ethers/alloy providers #6378

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use ethers::{
utils::{format_ether, hex, to_checksum, WEI_IN_ETHER},
};
use foundry_common::{
ProviderBuilder, ALCHEMY_FREE_TIER_CUPS, NON_ARCHIVE_NODE_WARNING, REQUEST_TIMEOUT,
provider::alloy::ProviderBuilder, ALCHEMY_FREE_TIER_CUPS, NON_ARCHIVE_NODE_WARNING,
REQUEST_TIMEOUT,
};
use foundry_config::Config;
use foundry_evm::{
Expand Down Expand Up @@ -768,7 +769,7 @@ impl NodeConfig {
.expect("Failed writing json");
}
if self.silent {
return
return;
}

println!("{}", self.as_string(fork))
Expand All @@ -779,7 +780,7 @@ impl NodeConfig {
/// See also [ Config::foundry_block_cache_file()]
pub fn block_cache_path(&self, block: u64) -> Option<PathBuf> {
if self.no_storage_caching || self.eth_rpc_url.is_none() {
return None
return None;
}
let chain_id = self.get_chain_id();

Expand Down Expand Up @@ -1195,7 +1196,7 @@ async fn find_latest_fork_block<P: TempProvider>(provider: P) -> Result<u64, Tra
for _ in 0..2 {
if let Some(block) = provider.get_block(num.into(), false).await? {
if block.header.hash.is_some() {
break
break;
}
}
// block not actually finalized, so we try the block before
Expand Down
68 changes: 34 additions & 34 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ use ethers::{
},
utils::rlp,
};
use foundry_common::ProviderBuilder;
use foundry_common::provider::alloy::ProviderBuilder;
use foundry_evm::{
backend::DatabaseError,
revm::{
Expand Down Expand Up @@ -442,7 +442,7 @@ impl EthApi {
for signer in self.signers.iter() {
if signer.accounts().contains(&from.to_ethers()) {
let signature = signer.sign_transaction(request.clone(), &from.to_ethers())?;
return build_typed_transaction(request, signature)
return build_typed_transaction(request, signature);
}
}
Err(BlockchainError::NoSignerAvailable)
Expand Down Expand Up @@ -609,7 +609,7 @@ impl EthApi {
return Ok(fork
.get_balance(address, number.to::<u64>())
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
}
Expand Down Expand Up @@ -641,7 +641,7 @@ impl EthApi {
)
.await
.map_err(|_| BlockchainError::DataUnavailable)?,
))
));
}
}
}
Expand Down Expand Up @@ -671,7 +671,7 @@ impl EthApi {
pub async fn block_by_number(&self, number: BlockNumber) -> Result<Option<Block>> {
node_info!("eth_getBlockByNumber");
if number == BlockNumber::Pending {
return Ok(Some(self.pending_block().await))
return Ok(Some(self.pending_block().await));
}

self.backend.block_by_number(number).await
Expand All @@ -683,7 +683,7 @@ impl EthApi {
pub async fn block_by_number_full(&self, number: BlockNumber) -> Result<Option<Block>> {
node_info!("eth_getBlockByNumber");
if number == BlockNumber::Pending {
return Ok(self.pending_block_full().await)
return Ok(self.pending_block_full().await);
}
self.backend.block_by_number_full(number).await
}
Expand Down Expand Up @@ -728,7 +728,7 @@ impl EthApi {
let block_request = self.block_request(Some(block_number.into())).await?;
if let BlockRequest::Pending(txs) = block_request {
let block = self.backend.pending_block(txs).await;
return Ok(Some(U256::from(block.transactions.len())))
return Ok(Some(U256::from(block.transactions.len())));
}
let block = self.backend.block_by_number(block_number).await?;
let txs = block.map(|b| match b.transactions {
Expand Down Expand Up @@ -775,7 +775,7 @@ impl EthApi {
return Ok(fork
.get_code(address, number.to::<u64>())
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
}
Expand Down Expand Up @@ -803,7 +803,7 @@ impl EthApi {
return Ok(fork
.get_proof(address, keys, Some((*number).into()))
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
}
Expand Down Expand Up @@ -920,7 +920,7 @@ impl EthApi {
node_info!("eth_sendRawTransaction");
let data = tx.as_ref();
if data.is_empty() {
return Err(BlockchainError::EmptyRawTransactionData)
return Err(BlockchainError::EmptyRawTransactionData);
}
let transaction = if data[0] > 0x7f {
// legacy transaction
Expand Down Expand Up @@ -986,12 +986,12 @@ impl EthApi {
if overrides.is_some() {
return Err(BlockchainError::StateOverrideError(
"not available on past forked blocks".to_string(),
))
));
}
return Ok(fork
.call(&request, Some(number.to::<u64>().into()))
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ impl EthApi {
return Ok(fork
.create_access_list(&request, Some(number.to::<u64>().into()))
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
}
Expand Down Expand Up @@ -1152,7 +1152,7 @@ impl EthApi {
node_info!("eth_getTransactionReceipt");
let tx = self.pool.get_transaction(hash);
if tx.is_some() {
return Ok(None)
return Ok(None);
}
self.backend.transaction_receipt(hash).await
}
Expand All @@ -1173,7 +1173,7 @@ impl EthApi {
return Ok(fork
.uncle_by_block_hash_and_index(block_hash, idx.into())
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
// It's impossible to have uncles outside of fork mode
Expand All @@ -1195,7 +1195,7 @@ impl EthApi {
return Ok(fork
.uncle_by_block_number_and_index(number, idx.into())
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
// It's impossible to have uncles outside of fork mode
Expand Down Expand Up @@ -1277,7 +1277,7 @@ impl EthApi {
&reward_percentiles,
)
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}

Expand All @@ -1295,7 +1295,7 @@ impl EthApi {

// only support ranges that are in cache range
if lowest < self.backend.best_number().to::<u64>().saturating_sub(self.fee_history_limit) {
return Err(FeeHistoryError::InvalidBlockRange.into())
return Err(FeeHistoryError::InvalidBlockRange.into());
}

let fee_history = self.fee_history_cache.lock();
Expand Down Expand Up @@ -1447,7 +1447,7 @@ impl EthApi {
) -> Result<GethTrace> {
node_info!("debug_traceTransaction");
if opts.tracer.is_some() {
return Err(RpcError::invalid_params("non-default tracer not supported yet").into())
return Err(RpcError::invalid_params("non-default tracer not supported yet").into());
}

self.backend.debug_trace_transaction(tx_hash, opts).await
Expand All @@ -1464,7 +1464,7 @@ impl EthApi {
) -> Result<DefaultFrame> {
node_info!("debug_traceCall");
if opts.tracer.is_some() {
return Err(RpcError::invalid_params("non-default tracer not supported yet").into())
return Err(RpcError::invalid_params("non-default tracer not supported yet").into());
}
let block_request = self.block_request(block_number).await?;
let fees = FeeDetails::new(
Expand Down Expand Up @@ -1542,7 +1542,7 @@ impl EthApi {
node_info!("evm_setAutomine");
if self.miner.is_auto_mine() {
if enable_automine {
return Ok(())
return Ok(());
}
self.miner.set_mining_mode(MiningMode::None);
} else if enable_automine {
Expand All @@ -1561,7 +1561,7 @@ impl EthApi {
let interval = interval.map(|i| i.to::<u64>());
let blocks = num_blocks.unwrap_or(U256::from(1));
if blocks == U256::ZERO {
return Ok(())
return Ok(());
}

// mine all the blocks
Expand Down Expand Up @@ -1685,7 +1685,7 @@ impl EthApi {
return Err(RpcError::invalid_params(
"anvil_setMinGasPrice is not supported when EIP-1559 is active",
)
.into())
.into());
}
self.backend.set_gas_price(gas);
Ok(())
Expand All @@ -1700,7 +1700,7 @@ impl EthApi {
return Err(RpcError::invalid_params(
"anvil_setNextBlockBaseFeePerGas is only supported when EIP-1559 is active",
)
.into())
.into());
}
self.backend.set_base_fee(basefee);
Ok(())
Expand Down Expand Up @@ -2167,7 +2167,7 @@ impl EthApi {
return Ok(fork
.estimate_gas(&request, Some((*number).into()))
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
}
Expand Down Expand Up @@ -2199,7 +2199,7 @@ impl EthApi {
if let Some(to) = request.to {
if let Ok(target_code) = self.backend.get_code_with_state(&state, to.to_alloy()) {
if target_code.as_ref().is_empty() {
return Ok(MIN_TRANSACTION_GAS)
return Ok(MIN_TRANSACTION_GAS);
}
}
}
Expand All @@ -2224,7 +2224,7 @@ impl EthApi {
self.backend.get_balance_with_state(&state, from.to_alloy())?;
if let Some(value) = request.value {
if value > available_funds.to_ethers() {
return Err(InvalidTransactionError::InsufficientFunds.into())
return Err(InvalidTransactionError::InsufficientFunds.into());
}
// safe: value < available_funds
available_funds -= value.to_alloy();
Expand Down Expand Up @@ -2263,7 +2263,7 @@ impl EthApi {
block_env,
fees,
gas_limit.to_alloy(),
))
));
}
}

Expand Down Expand Up @@ -2293,11 +2293,11 @@ impl EthApi {
// the transaction did fail due to lack of gas from the user
Err(InvalidTransactionError::Revert(Some(convert_transact_out(&out).0.into()))
.into())
}
};
}
reason => {
warn!(target: "node", "estimation failed due to {:?}", reason);
return Err(BlockchainError::EvmError(reason))
return Err(BlockchainError::EvmError(reason));
}
}

Expand Down Expand Up @@ -2336,7 +2336,7 @@ impl EthApi {

// new midpoint
mid_gas_limit = ((highest_gas_limit + lowest_gas_limit.to_ethers()) / 2).to_alloy();
continue
continue;
}

match ethres {
Expand Down Expand Up @@ -2368,7 +2368,7 @@ impl EthApi {
// real error.
Err(reason) => {
warn!(target: "node", "estimation failed due to {:?}", reason);
return Err(reason)
return Err(reason);
}
}
// new midpoint
Expand Down Expand Up @@ -2545,7 +2545,7 @@ impl EthApi {
return Ok(fork
.get_nonce(address, (*number).to::<u64>())
.await
.map_err(|_| BlockchainError::DataUnavailable)?)
.map_err(|_| BlockchainError::DataUnavailable)?);
}
}
}
Expand Down Expand Up @@ -2607,7 +2607,7 @@ impl EthApi {

fn required_marker(provided_nonce: U256, on_chain_nonce: U256, from: Address) -> Vec<TxMarker> {
if provided_nonce == on_chain_nonce {
return Vec::new()
return Vec::new();
}
let prev_nonce = provided_nonce.saturating_sub(U256::from(1));
if on_chain_nonce <= prev_nonce {
Expand Down
Loading