Skip to content

Commit

Permalink
fix: use a CALLER address with maxed out balance for calls
Browse files Browse the repository at this point in the history
this is required because in forking mode otherwise the account wont have enough balance
to transact
  • Loading branch information
gakonst authored and onbjerg committed Mar 8, 2022
1 parent 0f7d0bc commit ffa2102
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
7 changes: 3 additions & 4 deletions forge/src/executor/fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub struct FuzzCase {

#[cfg(test)]
mod tests {
use super::*;
use crate::CALLER;

use crate::test_helpers::{fuzz_executor, test_executor, COMPILED};

Expand All @@ -226,9 +226,8 @@ mod tests {
let mut executor = test_executor();

let compiled = COMPILED.find("FuzzTests").expect("could not find contract");
let (addr, _, _, _) = executor
.deploy(Address::zero(), compiled.bytecode().unwrap().0.clone(), 0.into())
.unwrap();
let (addr, _, _, _) =
executor.deploy(*CALLER, compiled.bytecode().unwrap().0.clone(), 0.into()).unwrap();

let executor = fuzz_executor(&executor);

Expand Down
16 changes: 8 additions & 8 deletions forge/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod fuzz;
pub use revm::SpecId;

use self::inspector::InspectorStackConfig;
use crate::CALLER;
use bytes::Bytes;
use ethers::{
abi::{Abi, Detokenize, RawLog, Tokenize},
Expand Down Expand Up @@ -139,6 +140,11 @@ where
self.db.insert_cache(address, account);
}

/// Gets the balance of an account
pub fn get_balance(&self, address: Address) -> U256 {
self.db.basic(address).balance
}

/// Set the nonce of an account.
pub fn set_nonce(&mut self, address: Address, nonce: u64) {
let mut account = self.db.basic(address);
Expand All @@ -152,14 +158,8 @@ where
&mut self,
address: Address,
) -> std::result::Result<(Return, Vec<RawLog>), EvmError> {
let CallResult { status, logs, .. } = self.call_committing::<(), _, _>(
Address::zero(),
address,
"setUp()",
(),
0.into(),
None,
)?;
let CallResult { status, logs, .. } =
self.call_committing::<(), _, _>(*CALLER, address, "setUp()", (), 0.into(), None)?;
Ok((status, logs))
}

Expand Down
8 changes: 6 additions & 2 deletions forge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub trait TestFilter {
fn matches_path(&self, path: impl AsRef<str>) -> bool;
}

use ethers::types::Address;
use once_cell::sync::Lazy;
static CALLER: Lazy<Address> = Lazy::new(Address::random);

#[cfg(test)]
pub mod test_helpers {
use crate::executor::fuzz::FuzzedExecutor;
Expand All @@ -33,7 +37,7 @@ pub mod test_helpers {
use ethers::{
prelude::Lazy,
solc::{AggregatedCompilerOutput, Project, ProjectPathsConfig},
types::{Address, U256},
types::U256,
};
use revm::db::DatabaseRef;

Expand All @@ -59,7 +63,7 @@ pub mod test_helpers {
) -> FuzzedExecutor<'a, DB> {
let cfg = proptest::test_runner::Config { failure_persistence: None, ..Default::default() };

FuzzedExecutor::new(executor, proptest::test_runner::TestRunner::new(cfg), Address::zero())
FuzzedExecutor::new(executor, proptest::test_runner::TestRunner::new(cfg), *CALLER)
}

pub mod filter {
Expand Down
8 changes: 6 additions & 2 deletions forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
fuzz::{FuzzError, FuzzTestResult, FuzzedCases, FuzzedExecutor},
CallResult, EvmError, Executor, RawCallResult,
},
TestFilter,
TestFilter, CALLER,
};
use rayon::iter::ParallelIterator;
use revm::db::DatabaseRef;
Expand Down Expand Up @@ -202,13 +202,17 @@ impl<'a, DB: DatabaseRef + Send + Sync> ContractRunner<'a, DB> {
/// Deploys the test contract inside the runner from the sending account, and optionally runs
/// the `setUp` function on the test contract.
pub fn deploy(&mut self, setup: bool) -> Result<(Address, Vec<RawLog>, bool, Option<String>)> {
// We max out their balance so that they can deploy and make calls.
self.executor.set_balance(self.sender, U256::MAX);
self.executor.set_balance(*CALLER, U256::MAX);

// We set the nonce of the deployer accounts to 1 to get the same addresses as DappTools
self.executor.set_nonce(self.sender, 1);

// Deploy libraries
self.predeploy_libs.iter().for_each(|code| {
self.executor
.deploy(Address::zero(), code.0.clone(), 0u32.into())
.deploy(*CALLER, code.0.clone(), 0u32.into())
.expect("couldn't deploy library");
});

Expand Down

0 comments on commit ffa2102

Please sign in to comment.