Skip to content

Commit

Permalink
chore: cleanup, reintroduce trace endpoints (#6577)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evalir authored Dec 12, 2023
1 parent 70c66fd commit 4254f2f
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

43 changes: 13 additions & 30 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,10 @@ use crate::{
};
use alloy_primitives::{Address, Bytes, TxHash, B256, B64, U256, U64};
use alloy_rpc_types::{
state::StateOverride,
AccessList,
AccessListWithGasUsed,
Block,
BlockId,
BlockNumberOrTag as BlockNumber,
BlockTransactions,
CallRequest,
EIP1186AccountProofResponse,
FeeHistory,
Filter,
FilteredParams,
Log,
Transaction,
TransactionReceipt,
TxpoolContent,
TxpoolInspect,
// trace::{geth::{DefaultFrame, GethDefaultTracingOptions, GethTrace},
// parity::LocalizedTransactionTrace},
TxpoolInspectSummary,
TxpoolStatus,
state::StateOverride, AccessList, AccessListWithGasUsed, Block, BlockId,
BlockNumberOrTag as BlockNumber, BlockTransactions, CallRequest, EIP1186AccountProofResponse,
FeeHistory, Filter, FilteredParams, Log, Transaction, TransactionReceipt, TxpoolContent,
TxpoolInspect, TxpoolInspectSummary, TxpoolStatus,
};
use alloy_transport::TransportErrorKind;
use anvil_core::{
Expand Down Expand Up @@ -1015,9 +998,9 @@ impl EthApi {
}

let fees = FeeDetails::new(
request.gas_price.map(|g| g.to_ethers()),
request.max_fee_per_gas.map(|g| g.to_ethers()),
request.max_priority_fee_per_gas.map(|g| g.to_ethers()),
request.gas_price.map(ToEthers::to_ethers),
request.max_fee_per_gas.map(ToEthers::to_ethers),
request.max_priority_fee_per_gas.map(ToEthers::to_ethers),
)?
.or_zero_fees();
let request = call_to_internal_tx_request(&request);
Expand Down Expand Up @@ -1475,9 +1458,9 @@ impl EthApi {
node_info!("debug_traceCall");
let block_request = self.block_request(block_number).await?;
let fees = FeeDetails::new(
request.gas_price.map(|g| g.to_ethers()),
request.max_fee_per_gas.map(|g| g.to_ethers()),
request.max_priority_fee_per_gas.map(|g| g.to_ethers()),
request.gas_price.map(ToEthers::to_ethers),
request.max_fee_per_gas.map(ToEthers::to_ethers),
request.max_priority_fee_per_gas.map(ToEthers::to_ethers),
)?
.or_zero_fees();

Expand Down Expand Up @@ -2044,7 +2027,7 @@ impl EthApi {

fn convert(tx: Arc<PoolTransaction>) -> TxpoolInspectSummary {
let tx = &tx.pending_transaction.transaction;
let to = tx.to().copied().map(|t| t.to_alloy());
let to = tx.to().copied().map(ToAlloy::to_alloy);
let gas_price = tx.gas_price().to_alloy();
let value = tx.value().to_alloy();
let gas = tx.gas_limit().to_alloy();
Expand Down Expand Up @@ -2499,7 +2482,7 @@ impl EthApi {

let gas_limit = request
.gas
.map(|g| g.to_alloy())
.map(ToAlloy::to_alloy)
.map(Ok)
.unwrap_or_else(|| self.current_gas_limit())?;

Expand Down Expand Up @@ -2583,7 +2566,7 @@ impl EthApi {
) -> Result<(U256, U256)> {
let highest_nonce =
self.get_transaction_count(from, Some(BlockId::Number(BlockNumber::Pending))).await?;
let nonce = request.nonce.map(|n| n.to_alloy()).unwrap_or(highest_nonce);
let nonce = request.nonce.map(ToAlloy::to_alloy).unwrap_or(highest_nonce);

Ok((nonce, highest_nonce))
}
Expand Down
30 changes: 21 additions & 9 deletions crates/anvil/src/eth/backend/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ use crate::eth::{backend::db::Db, error::BlockchainError};
use alloy_primitives::{Address, Bytes, StorageKey, StorageValue, B256, U256, U64};
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::{
trace::{GethDebugTracingOptions, GethTrace, LocalizedTransactionTrace as Trace},
trace::{GethDebugTracingOptions, GethTrace},
AccessListWithGasUsed, Block, BlockId, BlockNumberOrTag as BlockNumber, BlockTransactions,
CallRequest, EIP1186AccountProofResponse, FeeHistory, Filter, Log, Transaction,
TransactionReceipt,
};
use alloy_transport::TransportError;
use foundry_common::provider::alloy::{ProviderBuilder, RetryProvider};
use foundry_common::{
provider::alloy::{ProviderBuilder, RetryProvider},
types::ToReth,
};
use parking_lot::{
lock_api::{RwLockReadGuard, RwLockWriteGuard},
RawRwLock, RwLock,
};
use reth_rpc_types::trace::parity::LocalizedTransactionTrace as Trace;
use std::{collections::HashMap, sync::Arc, time::Duration};
use tokio::sync::RwLock as AsyncRwLock;

Expand Down Expand Up @@ -361,7 +365,13 @@ impl ClientFork {
return Ok(traces);
}

let traces = self.provider().trace_transaction(hash).await?;
let traces = self
.provider()
.trace_transaction(hash)
.await?
.into_iter()
.map(ToReth::to_reth)
.collect::<Vec<_>>();

let mut storage = self.storage_write();
storage.transaction_traces.insert(hash, traces.clone());
Expand Down Expand Up @@ -391,7 +401,13 @@ impl ClientFork {
return Ok(traces);
}

let traces = self.provider().trace_block(number.into()).await?;
let traces = self
.provider()
.trace_block(number.into())
.await?
.into_iter()
.map(ToReth::to_reth)
.collect::<Vec<_>>();

let mut storage = self.storage_write();
storage.block_traces.insert(number, traces.clone());
Expand Down Expand Up @@ -525,11 +541,7 @@ impl ClientFork {
block: Block,
index: usize,
) -> Result<Option<Block>, TransportError> {
let block_hash = block
.header
.hash
// TODO: Nicer way to make a custom error from a TransportError
.expect("Missing block hash");
let block_hash = block.header.hash.expect("Missing block hash");
let block_number = block.header.number.expect("Missing block number");
if let Some(uncles) = self.storage_read().uncles.get(&block_hash) {
return Ok(uncles.get(index).cloned());
Expand Down
1 change: 0 additions & 1 deletion crates/anvil/src/eth/backend/mem/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl Inspector {

/// Enables steps recording for `Tracer`.
pub fn with_steps_tracing(self) -> Self {
// todo deprecate?
self.with_tracing()
}
}
Expand Down
37 changes: 13 additions & 24 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,9 @@ use crate::{
};
use alloy_primitives::{Address, Bloom, Bytes, TxHash, B256, B64, U128, U256, U64, U8};
use alloy_rpc_types::{
state::StateOverride,
// trace::{geth::{DefaultFrame, GethDefaultTracingOptions, GethTrace},
// parity::LocalizedTransactionTrace},
AccessList,
Block as AlloyBlock,
BlockId,
BlockNumberOrTag as BlockNumber,
BlockTransactions,
Filter,
FilteredParams,
Header as AlloyHeader,
Log,
Transaction,
TransactionReceipt,
state::StateOverride, AccessList, Block as AlloyBlock, BlockId,
BlockNumberOrTag as BlockNumber, BlockTransactions, Filter, FilteredParams,
Header as AlloyHeader, Log, Transaction, TransactionReceipt,
};
use anvil_core::{
eth::{
Expand Down Expand Up @@ -369,7 +358,7 @@ impl Backend {
pub fn precompiles(&self) -> Vec<Address> {
get_precompiles_for(self.env.read().cfg.spec_id)
.into_iter()
.map(|a| a.to_alloy())
.map(ToAlloy::to_alloy)
.collect_vec()
}

Expand Down Expand Up @@ -1280,7 +1269,7 @@ impl Backend {
for log in logs.into_iter() {
let mut log = Log {
address: log.address.to_alloy(),
topics: log.topics.into_iter().map(|t| t.to_alloy()).collect(),
topics: log.topics.into_iter().map(ToAlloy::to_alloy).collect(),
data: log.data.0.into(),
block_hash: None,
block_number: None,
Expand Down Expand Up @@ -1845,9 +1834,9 @@ impl Backend {
return Ok(traces);
}

// if let Some(fork) = self.get_fork() {
// return Ok(fork.trace_transaction(hash).await?)
// }
if let Some(fork) = self.get_fork() {
return Ok(fork.trace_transaction(hash).await?)
}

Ok(vec![])
}
Expand Down Expand Up @@ -1914,11 +1903,11 @@ impl Backend {
return Ok(traces);
}

// if let Some(fork) = self.get_fork() {
// if fork.predates_fork(number) {
// return Ok(fork.trace_block(number).await.map_err(|_|
// BlockchainError::DataUnavailable)?) }
// }
if let Some(fork) = self.get_fork() {
if fork.predates_fork(number) {
return fork.trace_block(number).await.map_err(|_| BlockchainError::DataUnavailable)
}
}

Ok(vec![])
}
Expand Down
1 change: 1 addition & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ alloy-transport-ipc.workspace = true
alloy-json-rpc.workspace = true
alloy-pubsub.workspace = true
alloy-sol-types.workspace = true
reth-rpc-types = { git = "https://github.com/paradigmxyz/reth/", branch = "main" }

tower.workspace = true

Expand Down
Loading

0 comments on commit 4254f2f

Please sign in to comment.