Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Oct 13, 2023
1 parent da7baeb commit 836eff3
Show file tree
Hide file tree
Showing 36 changed files with 148 additions and 5,696 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ foundry-abi = { path = "crates/abi" }
foundry-binder = { path = "crates/binder" }
foundry-cli = { path = "crates/cli" }
foundry-common = { path = "crates/common" }
foundry-cheatcodes = { path = "crates/cheatcodes" }
foundry-config = { path = "crates/config" }
foundry-evm = { path = "crates/evm" }
foundry-macros = { path = "crates/macros" }
Expand Down
5 changes: 4 additions & 1 deletion crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ alloy-dyn-abi = { workspace = true, optional = true }
alloy-json-abi = { workspace = true, optional = true }
ethers = { workspace = true, optional = true, features = ["ethers-solc"] }
eyre = { workspace = true, optional = true }
futures = { version = "0.3", optional = true }
hex = { workspace = true, optional = true }
itertools = { workspace = true, optional = true }
jsonpath_lib = { workspace = true, optional = true }
Expand All @@ -46,11 +47,13 @@ impls = [
"dep:alloy-dyn-abi",
"dep:alloy-json-abi",
"dep:ethers",
"dep:eyre",
"dep:futures",
"dep:hex",
"dep:thiserror",
"dep:itertools",
"dep:jsonpath_lib",
"dep:revm",
"dep:thiserror",
"dep:tracing",
"dep:walkdir",
]
29 changes: 13 additions & 16 deletions crates/cheatcodes/src/impls/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{config::EvmOpts, Result};
use super::config::EvmOpts;
use crate::Cheatcodes;
use alloy_primitives::{Address, B256, U256};
use itertools::Itertools;
Expand Down Expand Up @@ -107,7 +107,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
fork: CreateFork,
env: &mut Env,
journaled_state: &mut JournaledState,
) -> Result<LocalForkId> {
) -> eyre::Result<LocalForkId> {
let id = self.create_fork(fork)?;
self.select_fork(id, env, journaled_state)?;
Ok(id)
Expand All @@ -122,21 +122,21 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
env: &mut Env,
journaled_state: &mut JournaledState,
transaction: B256,
) -> Result<LocalForkId> {
) -> eyre::Result<LocalForkId> {
let id = self.create_fork_at_transaction(fork, transaction)?;
self.select_fork(id, env, journaled_state)?;
Ok(id)
}

/// Creates a new fork but does _not_ select it
fn create_fork(&mut self, fork: CreateFork) -> Result<LocalForkId>;
fn create_fork(&mut self, fork: CreateFork) -> eyre::Result<LocalForkId>;

/// Creates a new fork but does _not_ select it
fn create_fork_at_transaction(
&mut self,
fork: CreateFork,
transaction: B256,
) -> Result<LocalForkId>;
) -> eyre::Result<LocalForkId>;

/// Selects the fork's state
///
Expand All @@ -152,7 +152,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
id: LocalForkId,
env: &mut Env,
journaled_state: &mut JournaledState,
) -> Result<()>;
) -> eyre::Result<()>;

/// Updates the fork to given block number.
///
Expand All @@ -167,7 +167,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
block_number: U256,
env: &mut Env,
journaled_state: &mut JournaledState,
) -> Result<()>;
) -> eyre::Result<()>;

/// Updates the fork to given transaction hash
///
Expand All @@ -183,7 +183,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
transaction: B256,
env: &mut Env,
journaled_state: &mut JournaledState,
) -> Result<()>;
) -> eyre::Result<()>;

/// Fetches the given transaction for the fork and executes it, committing the state in the DB
fn transact(
Expand All @@ -193,7 +193,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
env: &mut Env,
journaled_state: &mut JournaledState,
cheatcodes_inspector: Option<&mut Cheatcodes>,
) -> Result<()>;
) -> eyre::Result<()>;

/// Returns the `ForkId` that's currently used in the database, if fork mode is on
fn active_fork_id(&self) -> Option<LocalForkId>;
Expand All @@ -216,10 +216,7 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
/// Returns an error if the given `id` does not match any forks
///
/// Returns an error if no fork exits
fn ensure_fork(&self, id: Option<LocalForkId>) -> Result<LocalForkId>;

/// Ensures that a corresponding `ForkId` exists for the given local `id`
fn ensure_fork_id(&self, id: LocalForkId) -> Result<&()>;
fn ensure_fork(&self, id: Option<LocalForkId>) -> eyre::Result<LocalForkId>;

/// Handling multiple accounts/new contracts in a multifork environment can be challenging since
/// every fork has its own standalone storage section. So this can be a common error to run
Expand Down Expand Up @@ -292,16 +289,16 @@ pub trait DatabaseExt: Database<Error = DatabaseError> {
/// Ensures that `account` is allowed to execute cheatcodes
///
/// Returns an error if [`Self::has_cheatcode_access`] returns `false`
fn ensure_cheatcode_access(&self, account: Address) -> Result<()> {
fn ensure_cheatcode_access(&self, account: Address) -> Result<(), DatabaseError> {
if !self.has_cheatcode_access(account) {
bail!("no cheats available for {account}");
return Err(DatabaseError::NoCheats(account))
}
Ok(())
}

/// Same as [`Self::ensure_cheatcode_access()`] but only enforces it if the backend is currently
/// in forking mode
fn ensure_cheatcode_access_forking_mode(&self, account: Address) -> Result<()> {
fn ensure_cheatcode_access_forking_mode(&self, account: Address) -> Result<(), DatabaseError> {
if self.is_forked_mode() {
return self.ensure_cheatcode_access(account)
}
Expand Down
29 changes: 28 additions & 1 deletion crates/cheatcodes/src/impls/db/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use alloy_primitives::{Address, B256, U256};
use ethers::types::BlockId;
use std::sync::Arc;
use futures::channel::mpsc::{SendError, TrySendError};
use std::{
convert::Infallible,
sync::{mpsc::RecvError, Arc},
};

/// Result alias with `DatabaseError` as error
pub type DatabaseResult<T> = Result<T, DatabaseError>;
Expand All @@ -17,6 +21,10 @@ pub enum DatabaseError {
MissingAccount(Address),
#[error("code should already be loaded: {0}")]
MissingCode(B256),
#[error(transparent)]
Recv(#[from] RecvError),
#[error(transparent)]
Send(#[from] SendError),
#[error("failed to get account for {0}: {1}")]
GetAccount(Address, Arc<eyre::Error>),
#[error("failed to get storage for {0} at {1}: {2}")]
Expand Down Expand Up @@ -46,6 +54,11 @@ impl DatabaseError {
DatabaseError::Message(msg.into())
}

/// Create a new error with a message
pub fn display(msg: impl std::fmt::Display) -> Self {
DatabaseError::Message(msg.to_string())
}

fn get_rpc_error(&self) -> Option<&eyre::Error> {
match self {
Self::GetAccount(_, err) => Some(err),
Expand All @@ -57,6 +70,8 @@ impl DatabaseError {
Self::NoCheats(_) |
Self::MissingAccount(_) |
Self::MissingCode(_) |
Self::Recv(_) |
Self::Send(_) |
Self::Message(_) |
Self::BlockNotFound(_) |
Self::TransactionNotFound(_) |
Expand All @@ -74,3 +89,15 @@ impl DatabaseError {
.unwrap_or(false)
}
}

impl<T> From<TrySendError<T>> for DatabaseError {
fn from(err: TrySendError<T>) -> Self {
err.into_send_error().into()
}
}

impl From<Infallible> for DatabaseError {
fn from(value: Infallible) -> Self {
match value {}
}
}
1 change: 1 addition & 0 deletions crates/cheatcodes/src/impls/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ impl_from!(
ethers::types::SignatureError,
FsPathError,
hex::FromHexError,
eyre::Error,
super::db::DatabaseError,
jsonpath_lib::JsonPathError,
serde_json::Error,
Expand Down
1 change: 1 addition & 0 deletions crates/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository.workspace = true
[dependencies]
foundry-abi.workspace = true
foundry-utils.workspace = true
foundry-cheatcodes = { workspace = true, features = ["impls"] }
foundry-common.workspace = true
foundry-config.workspace = true
foundry-macros.workspace = true
Expand Down
6 changes: 2 additions & 4 deletions crates/evm/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! Various utilities to decode test results
use crate::{
abi::ConsoleEvents::{self, *},
executor::inspector::cheatcodes::util::MAGIC_SKIP_BYTES,
};
use crate::abi::ConsoleEvents::{self, *};
use alloy_primitives::B256;
use ethers::{
abi::{decode, AbiDecode, Contract as Abi, ParamType, RawLog, Token},
contract::EthLogDecode,
prelude::U256,
types::Log,
};
use foundry_cheatcodes::impls::MAGIC_SKIP_BYTES;
use foundry_common::{abi::format_token, SELECTOR_LEN};
use foundry_utils::error::ERROR_PREFIX;
use itertools::Itertools;
Expand Down
55 changes: 0 additions & 55 deletions crates/evm/src/executor/backend/diagnostic.rs

This file was deleted.

Loading

0 comments on commit 836eff3

Please sign in to comment.