diff --git a/anvil/server/src/ipc.rs b/anvil/server/src/ipc.rs index d61de1cdb327c..4b34abbb7f249 100644 --- a/anvil/server/src/ipc.rs +++ b/anvil/server/src/ipc.rs @@ -150,7 +150,6 @@ impl tokio_util::codec::Decoder for JsonRpcCodec { } else if is_whitespace(byte) { whitespaces += 1; } - is_escaped = byte == b'\\' && !is_escaped && in_str; if depth == 0 && idx != start_idx && idx - start_idx + 1 > whitespaces { diff --git a/cli/src/cmd/cast/run.rs b/cli/src/cmd/cast/run.rs index ab854536d7509..758c1e5e91752 100644 --- a/cli/src/cmd/cast/run.rs +++ b/cli/src/cmd/cast/run.rs @@ -198,20 +198,16 @@ impl RunArgs { let mut etherscan_identifier = EtherscanIdentifier::new(&config, evm_opts.get_remote_chain_id())?; - let labeled_addresses: BTreeMap = self - .label - .iter() - .filter_map(|label_str| { - let mut iter = label_str.split(':'); - - if let Some(addr) = iter.next() { - if let (Ok(address), Some(label)) = (Address::from_str(addr), iter.next()) { - return Some((address, label.to_string())) - } + let labeled_addresses = self.label.iter().filter_map(|label_str| { + let mut iter = label_str.split(':'); + + if let Some(addr) = iter.next() { + if let (Ok(address), Some(label)) = (Address::from_str(addr), iter.next()) { + return Some((address, label.to_string())) } - None - }) - .collect(); + } + None + }); let mut decoder = CallTraceDecoderBuilder::new().with_labels(labeled_addresses).build(); diff --git a/cli/src/cmd/forge/test/mod.rs b/cli/src/cmd/forge/test/mod.rs index 03dbfcf57f698..bc9ca5b03a017 100644 --- a/cli/src/cmd/forge/test/mod.rs +++ b/cli/src/cmd/forge/test/mod.rs @@ -555,7 +555,7 @@ fn test( // Identify addresses in each trace let mut decoder = CallTraceDecoderBuilder::new() .with_labels(result.labeled_addresses.clone()) - .with_events(local_identifier.events()) + .with_events(local_identifier.events().cloned()) .with_verbosity(verbosity) .build(); diff --git a/cli/src/cmd/retry.rs b/cli/src/cmd/retry.rs index 057a378a12700..0a8db8f1fd523 100644 --- a/cli/src/cmd/retry.rs +++ b/cli/src/cmd/retry.rs @@ -46,15 +46,15 @@ mod tests { #[test] fn test_cli() { - let args = RetryArgs::parse_from(&["foundry-cli", "--retries", "10"]); + let args = RetryArgs::parse_from(["foundry-cli", "--retries", "10"]); assert_eq!(args.retries, 10); assert_eq!(args.delay, 5); - let args = RetryArgs::parse_from(&["foundry-cli", "--delay", "10"]); + let args = RetryArgs::parse_from(["foundry-cli", "--delay", "10"]); assert_eq!(args.retries, 5); assert_eq!(args.delay, 10); - let args = RetryArgs::parse_from(&["foundry-cli", "--retries", "10", "--delay", "10"]); + let args = RetryArgs::parse_from(["foundry-cli", "--retries", "10", "--delay", "10"]); assert_eq!(args.retries, 10); assert_eq!(args.delay, 10); } diff --git a/evm/src/trace/decoder.rs b/evm/src/trace/decoder.rs index f33bee92af6c0..f7ae128504842 100644 --- a/evm/src/trace/decoder.rs +++ b/evm/src/trace/decoder.rs @@ -10,7 +10,7 @@ use crate::{ }; use ethers::{ abi::{Abi, Address, Event, Function, Param, ParamType, Token}, - types::H256, + types::{H160, H256}, }; use foundry_common::{abi::get_indexed_event, SELECTOR_LEN}; use hashbrown::HashSet; @@ -28,24 +28,24 @@ impl CallTraceDecoderBuilder { } /// Add known labels to the decoder. - pub fn with_labels(mut self, labels: BTreeMap) -> Self { - for (address, label) in labels.into_iter() { - self.decoder.labels.insert(address, label); - } + pub fn with_labels(mut self, labels: impl IntoIterator) -> Self { + self.decoder.labels.extend(labels); self } /// Add known events to the decoder. - pub fn with_events(mut self, events: Vec) -> Self { - events - .into_iter() - .map(|event| ((event.signature(), indexed_inputs(&event)), event)) - .for_each(|(sig, event)| { - self.decoder.events.entry(sig).or_default().push(event); - }); + pub fn with_events(mut self, events: impl IntoIterator) -> Self { + for event in events { + self.decoder + .events + .entry((event.signature(), indexed_inputs(&event))) + .or_default() + .push(event); + } self } + /// Sets the verbosity level of the decoder. pub fn with_verbosity(mut self, level: u8) -> Self { self.decoder.verbosity = level; self @@ -88,107 +88,72 @@ pub struct CallTraceDecoder { pub verbosity: u8, } +/// Returns an expression of the type `[(Address, Function); N]` +macro_rules! precompiles { + ($($number:literal : $name:ident($( $name_in:ident : $in:expr ),* $(,)?) -> ($( $name_out:ident : $out:expr ),* $(,)?)),+ $(,)?) => {{ + use std::string::String as RustString; + use ParamType::*; + [$( + ( + H160([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $number]), + #[allow(deprecated)] + Function { + name: RustString::from(stringify!($name)), + inputs: vec![$(Param { name: RustString::from(stringify!($name_in)), kind: $in, internal_type: None, }),*], + outputs: vec![$(Param { name: RustString::from(stringify!($name_out)), kind: $out, internal_type: None, }),*], + constant: None, + state_mutability: ethers::abi::StateMutability::Pure, + }, + ), + )+] + }}; +} + impl CallTraceDecoder { /// Creates a new call trace decoder. /// /// The call trace decoder always knows how to decode calls to the cheatcode address, as well /// as DSTest-style logs. pub fn new() -> Self { - let functions = HARDHAT_CONSOLE_ABI - .functions() - .map(|func| (func.short_signature(), vec![func.clone()])) - .chain(HEVM_ABI.functions().map(|func| (func.short_signature(), vec![func.clone()]))) - .collect::>>(); - Self { // TODO: These are the Ethereum precompiles. We should add a way to support precompiles // for other networks, too. - precompiles: [ - precompile( - 1, - "ecrecover", - [ - ParamType::FixedBytes(32), - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ], - [ParamType::Address], - ), - precompile(2, "keccak", [ParamType::Bytes], [ParamType::FixedBytes(32)]), - precompile(3, "ripemd", [ParamType::Bytes], [ParamType::FixedBytes(32)]), - precompile(4, "identity", [ParamType::Bytes], [ParamType::Bytes]), - precompile( - 5, - "modexp", - [ - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Bytes, - ], - [ParamType::Bytes], - ), - precompile( - 6, - "ecadd", - [ - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ], - [ParamType::Uint(256), ParamType::Uint(256)], - ), - precompile( - 7, - "ecmul", - [ParamType::Uint(256), ParamType::Uint(256), ParamType::Uint(256)], - [ParamType::Uint(256), ParamType::Uint(256)], - ), - precompile( - 8, - "ecpairing", - [ - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ParamType::Uint(256), - ], - [ParamType::Uint(256)], - ), - precompile( - 9, - "blake2f", - [ - ParamType::Uint(4), - ParamType::FixedBytes(64), - ParamType::FixedBytes(128), - ParamType::FixedBytes(16), - ParamType::FixedBytes(1), - ], - [ParamType::FixedBytes(64)], - ), - ] - .into(), + precompiles: precompiles!( + 0x01: ecrecover(hash: FixedBytes(32), v: Uint(256), r: Uint(256), s: Uint(256)) -> (publicAddress: Address), + 0x02: sha256(data: Bytes) -> (hash: FixedBytes(32)), + 0x03: ripemd(data: Bytes) -> (hash: FixedBytes(32)), + 0x04: identity(data: Bytes) -> (data: Bytes), + 0x05: modexp(Bsize: Uint(256), Esize: Uint(256), Msize: Uint(256), BEM: Bytes) -> (value: Bytes), + 0x06: ecadd(x1: Uint(256), y1: Uint(256), x2: Uint(256), y2: Uint(256)) -> (x: Uint(256), y: Uint(256)), + 0x07: ecmul(x1: Uint(256), y1: Uint(256), s: Uint(256)) -> (x: Uint(256), y: Uint(256)), + 0x08: ecpairing(x1: Uint(256), y1: Uint(256), x3: Uint(256), x2: Uint(256), y3: Uint(256), y2: Uint(256)) -> (success: Uint(256)), + 0x09: blake2f(rounds: Uint(4), h: FixedBytes(64), m: FixedBytes(128), t: FixedBytes(16), f: FixedBytes(1)) -> (h: FixedBytes(64)), + ).into(), + contracts: Default::default(), + labels: [ (CHEATCODE_ADDRESS, "VM".to_string()), (HARDHAT_CONSOLE_ADDRESS, "console".to_string()), (DEFAULT_CREATE2_DEPLOYER, "Create2Deployer".to_string()), ] .into(), - functions, + + functions: HARDHAT_CONSOLE_ABI + .functions() + .chain(HEVM_ABI.functions()) + .map(|func| (func.short_signature(), vec![func.clone()])) + .collect(), + events: CONSOLE_ABI .events() .map(|event| ((event.signature(), indexed_inputs(event)), vec![event.clone()])) - .collect::>>(), - errors: Abi::default(), + .collect(), + + errors: Default::default(), signature_identifier: None, receive_contracts: Default::default(), - verbosity: u8::default(), + verbosity: 0, } } @@ -384,30 +349,6 @@ fn patch_nameless_params(event: &mut Event) -> HashSet { patches } -fn precompile(number: u8, name: impl ToString, inputs: I, outputs: O) -> (Address, Function) -where - I: IntoIterator, - O: IntoIterator, -{ - ( - Address::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, number]), - #[allow(deprecated)] - Function { - name: name.to_string(), - inputs: inputs - .into_iter() - .map(|kind| Param { name: "".to_string(), kind, internal_type: None }) - .collect(), - outputs: outputs - .into_iter() - .map(|kind| Param { name: "".to_string(), kind, internal_type: None }) - .collect(), - constant: None, - state_mutability: ethers::abi::StateMutability::Pure, - }, - ) -} - fn indexed_inputs(event: &Event) -> usize { event.inputs.iter().filter(|param| param.indexed).count() } diff --git a/evm/src/trace/identifier/local.rs b/evm/src/trace/identifier/local.rs index 255f0deb1f89c..9f07720ab1453 100644 --- a/evm/src/trace/identifier/local.rs +++ b/evm/src/trace/identifier/local.rs @@ -24,8 +24,8 @@ impl LocalTraceIdentifier { } /// Get all the events of the local contracts. - pub fn events(&self) -> Vec { - self.local_contracts.iter().flat_map(|(_, (_, abi))| abi.events().cloned()).collect() + pub fn events(&self) -> impl Iterator { + self.local_contracts.iter().flat_map(|(_, (_, abi))| abi.events()) } } diff --git a/forge/src/coverage.rs b/forge/src/coverage.rs index b1f13217f35e2..d9cfabaff900b 100644 --- a/forge/src/coverage.rs +++ b/forge/src/coverage.rs @@ -135,7 +135,6 @@ impl<'a> CoverageReporter for LcovReporter<'a> { } /// A super verbose reporter for debugging coverage while it is still unstable. -#[derive(Default)] pub struct DebugReporter; impl CoverageReporter for DebugReporter { diff --git a/utils/src/abi.rs b/utils/src/abi.rs index d941af0917e75..974a80d6d5cc6 100644 --- a/utils/src/abi.rs +++ b/utils/src/abi.rs @@ -45,9 +45,8 @@ pub fn abi_to_solidity(contract_abi: &RawAbi, mut contract_name: &str) -> eyre:: .collect::>>()? .join(", "); - let event_final = format!("event {}({inputs})", event.name); - - events.push(format!("{event_final};")); + let event_string = format!("event {}({inputs});", event.name); + events.push(event_string); } let mut functions = Vec::with_capacity(contract_abi.functions.len());