Skip to content

Commit

Permalink
Merge branch 'master' into matt/revm-breaking-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Sep 4, 2022
2 parents 78d7ec5 + 2eceb70 commit 37edeb5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 33 deletions.
4 changes: 2 additions & 2 deletions anvil/src/eth/backend/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ impl<'a, 'b, DB: Db + ?Sized, Validator: TransactionValidator> Iterator
evm.env = env;
evm.database(&mut self.db);

// records all call traces
let mut inspector = Inspector::default().with_tracing();
// records all call and step traces
let mut inspector = Inspector::default().with_tracing().with_steps_tracing();

trace!(target: "backend", "[{:?}] executing", transaction.hash());
// transact and commit the transaction
Expand Down
11 changes: 11 additions & 0 deletions anvil/src/eth/backend/mem/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ impl Inspector {
self.tracer = Some(Default::default());
self
}

/// Enables steps recording for `Tracer`
/// If `Tracer` wasn't configured before, configures it automatically
pub fn with_steps_tracing(mut self) -> Self {
if self.tracer.is_none() {
self = self.with_tracing()
}
self.tracer = self.tracer.map(|tracer| tracer.with_steps_recording());

self
}
}

impl<DB: Database> revm::Inspector<DB> for Inspector {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cmd/forge/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl CoverageArgs {
.insert(source_file.id as usize, ast);
versioned_sources.entry(version.clone()).or_default().insert(
source_file.id as usize,
fs::read_to_string(&path)
fs::read_to_string(project_paths.root.join(&path))
.wrap_err("Could not read source code for analysis")?,
);
report.add_source(version, source_file.id as usize, path);
Expand Down
41 changes: 31 additions & 10 deletions cli/src/opts/multi_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,21 @@ pub struct MultiWallet {
pub private_key: Option<String>,

#[clap(
long = "mnemonic-paths",
long = "mnemonics",
alias = "mnemonic-paths",
help_heading = "WALLET OPTIONS - RAW",
help = "Use the mnemonic files at the specified paths.",
help = "Use the mnemonic phrases or mnemonic files at the specified paths.",
value_name = "PATHS"
)]
pub mnemonic_paths: Option<Vec<String>>,
pub mnemonics: Option<Vec<String>>,

#[clap(
long = "mnemonic-passphrases",
help_heading = "WALLET OPTIONS - RAW",
help = "Use a BIP39 passphrases for the mnemonic.",
value_name = "PASSPHRASE"
)]
pub mnemonic_passphrases: Option<Vec<String>>,

#[clap(
long = "mnemonic-derivation-paths",
Expand Down Expand Up @@ -291,22 +300,34 @@ impl MultiWallet {
}

pub fn mnemonics(&self) -> Result<Option<Vec<LocalWallet>>> {
if let Some(mnemonic_paths) = self.mnemonic_paths.as_ref() {
if let Some(ref mnemonics) = self.mnemonics {
let mut wallets = vec![];
let hd_paths: Vec<_> = if let Some(ref hd_paths) = self.hd_paths {
hd_paths.into_iter().map(String::as_str).map(Some).collect()
hd_paths.iter().map(Some).collect()
} else {
repeat(None).take(mnemonic_paths.len()).collect()
repeat(None).take(mnemonics.len()).collect()
};
let mnemonic_passphrases: Vec<_> =
if let Some(ref mnemonic_passphrases) = self.mnemonic_passphrases {
mnemonic_passphrases.iter().map(Some).collect()
} else {
repeat(None).take(mnemonics.len()).collect()
};
let mnemonic_indexes: Vec<_> = if let Some(ref mnemonic_indexes) = self.mnemonic_indexes
{
mnemonic_indexes.into_iter().copied().collect()
mnemonic_indexes.to_vec()
} else {
repeat(0).take(mnemonic_paths.len()).collect()
repeat(0).take(mnemonics.len()).collect()
};
for (path, hd_path, mnemonic_index) in izip!(mnemonic_paths, hd_paths, mnemonic_indexes)
for (mnemonic, mnemonic_passphrase, hd_path, mnemonic_index) in
izip!(mnemonics, mnemonic_passphrases, hd_paths, mnemonic_indexes)
{
wallets.push(self.get_from_mnemonic(path, hd_path, mnemonic_index)?)
wallets.push(self.get_from_mnemonic(
mnemonic,
mnemonic_passphrase,
hd_path,
mnemonic_index,
)?)
}
return Ok(Some(wallets))
}
Expand Down
61 changes: 41 additions & 20 deletions cli/src/opts/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{str::FromStr, sync::Arc};
use std::{path::Path, str::FromStr, sync::Arc};

use clap::Parser;
use ethers::{
Expand Down Expand Up @@ -81,12 +81,21 @@ pub struct Wallet {
pub private_key: Option<String>,

#[clap(
long = "mnemonic-path",
long = "mnemonic",
alias = "mnemonic-path",
help_heading = "WALLET OPTIONS - RAW",
help = "Use the mnemonic file at the specified path.",
help = "Use the mnemonic phrase of mnemonic file at the specified path.",
value_name = "PATH"
)]
pub mnemonic_path: Option<String>,
pub mnemonic: Option<String>,

#[clap(
long = "mnemonic-passphrase",
help_heading = "WALLET OPTIONS - RAW",
help = "Use a BIP39 passphrase for the mnemonic.",
value_name = "PASSPHRASE"
)]
pub mnemonic_passphrase: Option<String>,

#[clap(
long = "mnemonic-derivation-path",
Expand Down Expand Up @@ -170,8 +179,13 @@ impl Wallet {
}

pub fn mnemonic(&self) -> Result<Option<LocalWallet>> {
Ok(if let Some(ref path) = self.mnemonic_path {
Some(self.get_from_mnemonic(path, self.hd_path.as_deref(), self.mnemonic_index)?)
Ok(if let Some(ref mnemonic) = self.mnemonic {
Some(self.get_from_mnemonic(
mnemonic,
self.mnemonic_passphrase.as_ref(),
self.hd_path.as_ref(),
self.mnemonic_index,
)?)
} else {
None
})
Expand All @@ -196,22 +210,28 @@ pub trait WalletTrait {

fn get_from_mnemonic(
&self,
path: &str,
derivation_path: Option<&str>,
mnemonic: &String,
passphrase: Option<&String>,
derivation_path: Option<&String>,
index: u32,
) -> Result<LocalWallet> {
let mnemonic = fs::read_to_string(path)?.replace('\n', "");
if let Some(hd_path) = derivation_path {
Ok(MnemonicBuilder::<English>::default()
.phrase(mnemonic.as_str())
.derivation_path(hd_path)?
.build()?)
let mnemonic = if Path::new(mnemonic).is_file() {
fs::read_to_string(mnemonic)?.replace('\n', "")
} else {
Ok(MnemonicBuilder::<English>::default()
.phrase(mnemonic.as_str())
.index(index)?
.build()?)
}
mnemonic.to_owned()
};
let builder = MnemonicBuilder::<English>::default().phrase(mnemonic.as_str());
let builder = if let Some(passphrase) = passphrase {
builder.password(passphrase.as_str())
} else {
builder
};
let builder = if let Some(hd_path) = derivation_path {
builder.derivation_path(hd_path.as_str())?
} else {
builder.index(index)?
};
Ok(builder.build()?)
}

fn get_from_keystore(
Expand Down Expand Up @@ -243,7 +263,8 @@ mod tests {
private_key: Some("123".to_string()),
keystore_path: None,
keystore_password: None,
mnemonic_path: None,
mnemonic: None,
mnemonic_passphrase: None,
ledger: false,
trezor: false,
hd_path: None,
Expand Down
16 changes: 16 additions & 0 deletions evm/src/executor/inspector/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ use revm::{
/// An inspector that collects call traces.
#[derive(Default, Debug, Clone)]
pub struct Tracer {
pub record_steps: bool,

pub trace_stack: Vec<usize>,
pub traces: CallTraceArena,
pub step_stack: Vec<(usize, usize)>, // (trace_idx, step_idx)
}

impl Tracer {
pub fn with_steps_recording(mut self) -> Self {
self.record_steps = true;

self
}

pub fn start_trace(
&mut self,
depth: usize,
Expand Down Expand Up @@ -204,6 +212,10 @@ where
data: &mut EVMData<'_, DB>,
_is_static: bool,
) -> Return {
if !self.record_steps {
return Return::Continue
}

let depth = data.journaled_state.depth();
let pc = interp.program_counter();
let op = OpCode(interp.contract.bytecode.bytecode()[pc]);
Expand Down Expand Up @@ -236,6 +248,10 @@ where
_is_static: bool,
status: Return,
) -> Return {
if !self.record_steps {
return Return::Continue
}

self.fill_step(interp.gas.remaining(), status);

status
Expand Down

0 comments on commit 37edeb5

Please sign in to comment.