Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deps): Move to Alloy ABI encoding/decoding & alloy types #5986

Merged
merged 83 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
ee18e87
feat: find and replace all ethers_solc mentions outside anvil
Evalir Oct 5, 2023
6c6aa4e
chore: keep resolving errors as fuzz is getting migrated
Evalir Oct 5, 2023
f33e414
feat: fuzz/trace changes
Evalir Oct 6, 2023
a8bb5b0
feat: partial trace migration
Evalir Oct 6, 2023
adc2786
chore: use temporal sol! macro in diff file for decode migration
Evalir Oct 17, 2023
8bc67d2
feat: use proptest traits instead of custom impl
Evalir Oct 17, 2023
65f0918
chore: address comments
Evalir Oct 17, 2023
a54005a
chore: remove alloy console bindings
Evalir Oct 17, 2023
2c70bd9
feat: introduce foundry-block-explorers
Evalir Oct 17, 2023
55ad9bf
chore: partial common abi helpers migration
Evalir Oct 17, 2023
94fb974
feat: partial decode migration
Evalir Oct 18, 2023
b3935cc
feat: re-introduce block-explorers
Evalir Oct 18, 2023
8737a52
feat: fix compiler errors
Evalir Oct 18, 2023
988e756
chore
Evalir Oct 18, 2023
5b8c207
chore: tentative inspector migration
Evalir Oct 18, 2023
bd2fbb3
Merge branch 'master' into evalir/introduce-foundry-compilers
Evalir Oct 18, 2023
3c3b35c
feat: switch to using static decoder to decode errors
Evalir Oct 18, 2023
7484d3b
chore: clippy
Evalir Oct 18, 2023
3adebd5
feat: migrate trace types temporarily
Evalir Oct 19, 2023
c44e796
chore: replace ethers tracing types for local tracing types
Evalir Oct 19, 2023
b51f741
chore: rebase
Evalir Oct 19, 2023
bb68371
fix: handle decoding with static decoder, tests
Evalir Oct 19, 2023
079c879
chore: use JsonAbi for console/hardhat/hevm abis
Evalir Oct 19, 2023
d9207b2
chore: add todo
Evalir Oct 19, 2023
efd0323
chore: replace types downstream and remove glue
Evalir Oct 19, 2023
de459d4
feat: fix last evm issues, start fixing downstream type issues
Evalir Oct 20, 2023
8356303
chore: cargo
Evalir Oct 20, 2023
bcb57a2
chore: more downstream error fixes
Evalir Oct 20, 2023
4d4255b
chore: fix test files
Evalir Oct 20, 2023
5f145b7
chore: more downstream fixes
Evalir Oct 20, 2023
90d5b05
chore: fmt
Evalir Oct 20, 2023
3ea011c
feat: migrate unit utils, replace
Evalir Oct 20, 2023
48832e5
chore: fix tests, fmt
Evalir Oct 20, 2023
2cefb6f
compiles
Evalir Oct 22, 2023
6bef6ad
clippy
Evalir Oct 22, 2023
170f5e6
chore: clippy
Evalir Oct 22, 2023
d8e54b9
Merge branch 'master' into evalir/introduce-foundry-compilers
Evalir Oct 22, 2023
5b404e0
chore: last fixes
Evalir Oct 22, 2023
2dbe680
chore: update block explorers
Evalir Oct 22, 2023
ea9b980
chore: actually coerce values correctly
Evalir Oct 23, 2023
a1b7442
chore: fix broken test
Evalir Oct 23, 2023
23e281d
chore: fix estimation test, parse values as alloy nums
Evalir Oct 23, 2023
bdc95bb
chore: fix abi parsing
Evalir Oct 23, 2023
8507f92
chore: selector tests
Evalir Oct 23, 2023
e42aeba
chore: fix more tests, remove more glue
Evalir Oct 23, 2023
dc05ee5
chore: properly decode logs
Evalir Oct 23, 2023
944174a
chore: use selector_type to handle tuples correctly
Evalir Oct 23, 2023
4b2f0ea
chore: clippy and fix another test
Evalir Oct 23, 2023
f297ace
chore: fix remaining abi tests
Evalir Oct 23, 2023
403de57
chore: use proptest traits for fuzzer
Evalir Oct 23, 2023
ffcb558
more test fixes ongod
Evalir Oct 23, 2023
2c64c3d
clippy
Evalir Oct 23, 2023
ef21ace
chore: use abigen for console logs for now
Evalir Oct 23, 2023
99c228d
fix: generate valid values in fuzzer
Evalir Oct 23, 2023
ebf9523
chore: clippy
Evalir Oct 23, 2023
92c6b5a
chore: readd settings
Evalir Oct 23, 2023
e3dfa8b
chore: various fixes
Evalir Oct 23, 2023
da9b483
chore: fix script arguments decoding
Evalir Oct 24, 2023
adc940a
chore: fix more tests
Evalir Oct 24, 2023
b1f5ff7
chore: last ots fixes
Evalir Oct 24, 2023
0dbcf71
fix: decoding
Evalir Oct 24, 2023
0e2c954
chore: clippy
Evalir Oct 24, 2023
4d9ea34
chore: fmt
Evalir Oct 24, 2023
9b5c45c
chore: fix deny check
Evalir Oct 24, 2023
1d1dc01
chore: deny fixes
Evalir Oct 24, 2023
e50cc48
chore: force default features off
Evalir Oct 24, 2023
5ce86fe
Merge branch 'master' into evalir/introduce-foundry-compilers
Evalir Oct 24, 2023
5c37c4b
chore: update block-explorers
Evalir Oct 24, 2023
30b2295
chore: doc fixes
Evalir Oct 24, 2023
cd25d3b
chore: ignore invariant storage test due to flakyness
Evalir Oct 24, 2023
6c88473
chore: update foundry-block-explorers
Evalir Oct 24, 2023
fb7bd39
chore: cleanup, config migration
Evalir Oct 24, 2023
5500c14
chore: resolve comments, more cleanup, remove unwraps
Evalir Oct 24, 2023
e4e347a
chore: remove last mentions of ethers::etherscan
Evalir Oct 24, 2023
823a6e3
chore: remove ethers-solc feat
Evalir Oct 24, 2023
27f107a
Merge branch 'master' into evalir/introduce-foundry-compilers
Evalir Oct 24, 2023
cf7fdce
chore: use alloy master again
Evalir Oct 24, 2023
371e817
chore: readd NameOrAddress
Evalir Oct 24, 2023
4e4e4c1
chore: clippy/fmt
Evalir Oct 24, 2023
bcd565f
chore: readd support on storage
Evalir Oct 24, 2023
bd3e506
fix: add remappings on settings
Evalir Oct 25, 2023
f958758
chore: address comments (remove create2, noop map, remove eyre from d…
Evalir Oct 25, 2023
963234d
chore: use NameOrAddress
Evalir Oct 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions crates/evm/src/executor/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ use crate::{
},
CALLER, TEST_CONTRACT_ADDRESS,
};
use alloy_primitives::{b256, Address, B256, U256, U64};
use alloy_primitives::{b256, Address, B256, U256, U64, keccak256};
use ethers::{
prelude::Block,
types::{BlockNumber, Transaction},
utils::keccak256,
};
use foundry_utils::types::{ToAlloy, ToEthers};
pub use in_memory_db::MemDb;
Expand Down
3 changes: 1 addition & 2 deletions crates/evm/src/executor/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use crate::executor::{
backend::error::{DatabaseError, DatabaseResult},
fork::{cache::FlushJsonBlockCacheDB, BlockchainDb},
};
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_primitives::{Address, Bytes, B256, U256, keccak256};
use ethers::{
core::abi::ethereum_types::BigEndianHash,
providers::Middleware,
types::{Block, BlockId, NameOrAddress, Transaction},
utils::keccak256,
};
use foundry_common::NON_ARCHIVE_NODE_WARNING;
use foundry_utils::types::{ToAlloy, ToEthers};
Expand Down
13 changes: 0 additions & 13 deletions crates/evm/src/executor/fork/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::executor::fork::{
BackendHandler, BlockchainDb, BlockchainDbMeta, CreateFork, SharedBackend,
};
use ethers::{
abi::{AbiDecode, AbiEncode, AbiError},
providers::Provider,
types::{BlockId, BlockNumber},
};
Expand Down Expand Up @@ -49,18 +48,6 @@ impl<T: Into<String>> From<T> for ForkId {
}
}

impl AbiEncode for ForkId {
fn encode(self) -> Vec<u8> {
AbiEncode::encode(self.0)
}
}

impl AbiDecode for ForkId {
fn decode(bytes: impl AsRef<[u8]>) -> Result<Self, AbiError> {
Ok(Self(String::decode(bytes)?))
}
}

/// The Sender half of multi fork pair.
/// Can send requests to the `MultiForkHandler` to create forks
#[derive(Debug, Clone)]
Expand Down
50 changes: 23 additions & 27 deletions crates/evm/src/trace/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use crate::{
trace::{node::CallTraceNode, utils},
CALLER, TEST_CONTRACT_ADDRESS,
};
use alloy_primitives::FixedBytes;
use ethers::{
abi::{Abi, Address, Event, Function, Param, ParamType, Token},
types::{H160, H256},
};
use alloy_primitives::{Address, FixedBytes, B256};
use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_json_abi::{JsonAbi as Abi, Event, Function, Param};
use foundry_common::{abi::get_indexed_event, SELECTOR_LEN};
use foundry_utils::types::ToEthers;
use hashbrown::HashSet;
Expand Down Expand Up @@ -47,7 +45,7 @@ impl CallTraceDecoderBuilder {
for event in events {
self.decoder
.events
.entry((event.signature(), indexed_inputs(&event)))
.entry((event.selector(), indexed_inputs(&event)))
.or_default()
.push(event);
}
Expand Down Expand Up @@ -97,7 +95,7 @@ pub struct CallTraceDecoder {
/// A mapping of signatures to their known functions
pub functions: BTreeMap<FixedBytes<4>, Vec<Function>>,
/// All known events
pub events: BTreeMap<(H256, usize), Vec<Event>>,
pub events: BTreeMap<(B256, usize), Vec<Event>>,
/// All known errors
pub errors: Abi,
/// A signature identifier for events and functions.
Expand All @@ -110,17 +108,15 @@ pub struct CallTraceDecoder {
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]),
Address::new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $number]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address::with_last_byte

#[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,
inputs: vec![$(Param { name: RustString::from(stringify!($name_in)), ty: $in, components: vec![], internal_type: None, }),*],
outputs: vec![$(Param { name: RustString::from(stringify!($name_out)), ty: $out, components: vec![], internal_type: None, }),*],
state_mutability: alloy_json_abi::StateMutability::Pure,
},
),
)+]
Expand All @@ -144,25 +140,25 @@ impl CallTraceDecoder {
// TODO: These are the Ethereum precompiles. We should add a way to support precompiles
// for other networks, too.
precompiles: precompiles!(
0x01: ecrecover(hash: FixedBytes(32), v: Uint(256), r: Uint(256), s: Uint(256)) -> (publicAddress: Address),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks a bit odd cc @DaniPopes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole point of this macro is to make it easy to modify. Slapping format everywhere doesn't really help towards that. Anyway I am working on removing this in #5998 since some precompiles don't adhere to ABI so it doesn't matter.

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), x2: Uint(256), y2: Uint(256), x3: Uint(256), y3: 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)),
0x01: ecrecover(hash: format!("bytes32"), v: format!("uint256"), r: format!("uint256"), s: format!("uint256")) -> (publicAddress: format!("address")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very ugly and needs to be removed -- what's the plan for this? sol macro? let's pls track this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracking on #6102

0x02: sha256(data: format!("bytes")) -> (hash: format!("bytes32")),
0x03: ripemd(data: format!("bytes")) -> (hash: format!("bytes32")),
0x04: identity(data: format!("bytes")) -> (data: format!("bytes")),
0x05: modexp(Bsize: format!("uint256"), Esize: format!("uint256"), Msize: format!("uint256"), BEM: format!("bytes")) -> (value: format!("bytes")),
0x06: ecadd(x1: format!("uint256"), y1: format!("uint256"), x2: format!("uint256"), y2: format!("uint256")) -> (x: format!("uint256"), y: format!("uint256")),
0x07: ecmul(x1: format!("uint256"), y1: format!("uint256"), s: format!("uint256")) -> (x: format!("uint256"), y: format!("uint256")),
0x08: ecpairing(x1: format!("uint256"), y1: format!("uint256"), x2: format!("uint256"), y2: format!("uint256"), x3: format!("uint256"), y3: format!("uint256")) -> (success: Uint(256)),
0x09: blake2f(rounds: DynSolType::Uint(4).to_string(), h: DynSolType::FixedBytes(64).to_string(), m: DynSolType::FixedBytes(128).to_string(), t: DynSolType::FixedBytes(16).to_string(), f: DynSolType::FixedBytes(1).to_string()) -> (h: DynSolType::FixedBytes(64).to_string()),
).into(),

contracts: Default::default(),

labels: [
(CHEATCODE_ADDRESS.to_ethers(), "VM".to_string()),
(HARDHAT_CONSOLE_ADDRESS.to_ethers(), "console".to_string()),
(DEFAULT_CREATE2_DEPLOYER.to_ethers(), "Create2Deployer".to_string()),
(CALLER.to_ethers(), "DefaultSender".to_string()),
(TEST_CONTRACT_ADDRESS.to_ethers(), "DefaultTestContract".to_string()),
(CHEATCODE_ADDRESS, "VM".to_string()),
(HARDHAT_CONSOLE_ADDRESS, "console".to_string()),
(DEFAULT_CREATE2_DEPLOYER, "Create2Deployer".to_string()),
(CALLER, "DefaultSender".to_string()),
(TEST_CONTRACT_ADDRESS, "DefaultTestContract".to_string()),
]
.into(),

Expand Down
21 changes: 9 additions & 12 deletions crates/evm/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ use crate::{
abi::CHEATCODE_ADDRESS, debug::Instruction, trace::identifier::LocalTraceIdentifier, CallKind,
};
pub use decoder::{CallTraceDecoder, CallTraceDecoderBuilder};
use ethers::{
abi::{ethereum_types::BigEndianHash, Address, RawLog},
core::utils::to_checksum,
types::{Bytes, DefaultFrame, GethDebugTracingOptions, StructLog, H256, U256},
};
use ethers::types::{DefaultFrame, GethDebugTracingOptions, StructLog};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These types should also go to alloy? I guess RPC crate under core/?

Think good opportunity to get that started, along with the TxRequest/Response/Receipt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, we should move these to alloy somehow. Probably reth might have these somewhat reworked

use alloy_primitives::{Address, B256, U256, Log as RawLog, Bytes};
pub use executor::TracingExecutor;
use foundry_common::contracts::{ContractsByAddress, ContractsByArtifact};
use foundry_utils::types::{ToAlloy, ToEthers};
Expand Down Expand Up @@ -97,7 +94,7 @@ impl CallTraceArena {
// Recursively fill in the geth trace by going through the traces
fn add_to_geth_trace(
&self,
storage: &mut HashMap<Address, BTreeMap<H256, H256>>,
storage: &mut HashMap<Address, BTreeMap<B256, B256>>,
trace_node: &CallTraceNode,
struct_logs: &mut Vec<StructLog>,
opts: &GethDebugTracingOptions,
Expand All @@ -111,7 +108,7 @@ impl CallTraceArena {
if !opts.disable_storage.unwrap_or_default() {
let contract_storage = storage.entry(step.contract).or_default();
if let Some((key, value)) = step.state_diff {
contract_storage.insert(H256::from_uint(&key), H256::from_uint(&value));
contract_storage.insert(B256::from(key), B256::from(value));
log.storage = Some(contract_storage.clone());
}
}
Expand Down Expand Up @@ -275,7 +272,7 @@ impl fmt::Display for RawOrDecodedLog {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RawOrDecodedLog::Raw(log) => {
for (i, topic) in log.topics.iter().enumerate() {
for (i, topic) in log.topics().iter().enumerate() {
writeln!(
f,
"{:>13}: {}",
Expand Down Expand Up @@ -505,7 +502,7 @@ impl Default for CallTrace {

impl fmt::Display for CallTrace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let address = to_checksum(&self.address, None);
let address = self.address.to_checksum(None);
if self.created() {
write!(
f,
Expand Down Expand Up @@ -543,7 +540,7 @@ impl fmt::Display for CallTrace {
self.gas_cost,
color.paint(self.label.as_ref().unwrap_or(&address)),
color.paint(func),
if !self.value.is_zero() {
if !self.value == U256::ZERO {
format!("{{value: {}}}", self.value)
} else {
"".to_string()
Expand Down Expand Up @@ -593,7 +590,7 @@ impl TraceKind {

/// Chooses the color of the trace depending on the destination address and status of the call.
fn trace_color(trace: &CallTrace) -> Color {
if trace.address == CHEATCODE_ADDRESS.to_ethers() {
if trace.address == CHEATCODE_ADDRESS {
Color::Blue
} else if trace.success {
Color::Green
Expand All @@ -619,7 +616,7 @@ pub fn load_contracts(
.iter()
.filter_map(|(addr, name)| {
if let Ok(Some((_, (abi, _)))) = contracts.find_by_name_or_identifier(name) {
return Some(((*addr).to_alloy(), (name.clone(), abi.clone())))
return Some((*addr, (name.clone(), abi.clone())))
}
None
})
Expand Down
23 changes: 11 additions & 12 deletions crates/evm/src/trace/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use crate::{
},
CallKind,
};
use ethers::{
abi::{Abi, Function},
types::{Action, Address, Call, CallResult, Create, CreateResult, Res, Suicide},
};
use alloy_dyn_abi::{JsonAbiExt, FunctionExt};
use ethers::types::{Action, Call, CallResult, Create, CreateResult, Res, Suicide};
use alloy_primitives::Address;
use alloy_json_abi::{JsonAbi as Abi, Function};
use foundry_common::SELECTOR_LEN;
use foundry_utils::types::ToEthers;
use revm::interpreter::InstructionResult;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -110,19 +109,19 @@ impl CallTraceNode {

if let RawOrDecodedCall::Raw(ref bytes) = self.trace.data {
let inputs = if bytes.len() >= SELECTOR_LEN {
if self.trace.address == CHEATCODE_ADDRESS.to_ethers() {
if self.trace.address == CHEATCODE_ADDRESS {
// Try to decode cheatcode inputs in a more custom way
utils::decode_cheatcode_inputs(func, bytes, errors, verbosity).unwrap_or_else(
|| {
func.decode_input(&bytes[SELECTOR_LEN..])
func.abi_decode_input(&bytes[SELECTOR_LEN..], false)
.expect("bad function input decode")
.iter()
.map(|token| utils::label(token, labels))
.collect()
},
)
} else {
match func.decode_input(&bytes[SELECTOR_LEN..]) {
match func.abi_decode_input(&bytes[SELECTOR_LEN..], false) {
Ok(v) => v.iter().map(|token| utils::label(token, labels)).collect(),
Err(_) => Vec::new(),
}
Expand All @@ -137,7 +136,7 @@ impl CallTraceNode {

if let RawOrDecodedReturnData::Raw(bytes) = &self.trace.output {
if !bytes.is_empty() && self.trace.success {
if self.trace.address == CHEATCODE_ADDRESS.to_ethers() {
if self.trace.address == CHEATCODE_ADDRESS {
if let Some(decoded) = funcs
.iter()
.find_map(|func| decode_cheatcode_outputs(func, bytes, verbosity))
Expand All @@ -148,7 +147,7 @@ impl CallTraceNode {
}

if let Some(tokens) =
funcs.iter().find_map(|func| func.decode_output(bytes).ok())
funcs.iter().find_map(|func| func.abi_decode_output(bytes, false).ok())
{
// Functions coming from an external database do not have any outputs
// specified, and will lead to returning an empty list of tokens.
Expand Down Expand Up @@ -183,15 +182,15 @@ impl CallTraceNode {
self.trace.data = RawOrDecodedCall::Decoded(
precompile_fn.name.clone(),
precompile_fn.signature(),
precompile_fn.decode_input(bytes).map_or_else(
precompile_fn.abi_decode_input(bytes, false).map_or_else(
|_| vec![hex::encode(bytes)],
|tokens| tokens.iter().map(|token| utils::label(token, labels)).collect(),
),
);

if let RawOrDecodedReturnData::Raw(ref bytes) = self.trace.output {
self.trace.output = RawOrDecodedReturnData::Decoded(
precompile_fn.decode_output(bytes).map_or_else(
precompile_fn.abi_decode_output(bytes, false).map_or_else(
|_| hex::encode(bytes),
|tokens| {
tokens
Expand Down
29 changes: 15 additions & 14 deletions crates/evm/src/trace/utils.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
//! utilities used within tracing

use crate::decode;
use ethers::{
abi::{Abi, Address, Function, ParamType, Token},
core::utils::to_checksum,
};
use alloy_primitives::Address;
use alloy_json_abi::{JsonAbi as Abi, Function};
use alloy_dyn_abi::{DynSolValue, DynSolType, JsonAbiExt};
use foundry_common::{abi::format_token, SELECTOR_LEN};
use std::collections::HashMap;

/// Returns the label for the given `token`
/// Returns the label for the given [DynSolValue]
///
/// If the `token` is an `Address` then we look abel the label map.
/// by default the token is formatted using standard formatting
pub fn label(token: &Token, labels: &HashMap<Address, String>) -> String {
pub fn label(token: &DynSolValue, labels: &HashMap<Address, String>) -> String {
match token {
Token::Address(addr) => {
DynSolValue::Address(addr) => {
if let Some(label) = labels.get(addr) {
format!("{label}: [{}]", to_checksum(addr, None))
format!("{label}: [{}]", addr.to_checksum(None))
} else {
format_token(token)
}
Expand All @@ -39,7 +38,8 @@ pub(crate) fn decode_cheatcode_inputs(
"rememberKey" | "addr" | "startBroadcast" | "broadcast" => {
// these functions accept a private key as uint256, which should not be
// converted to plain text
if !func.inputs.is_empty() && matches!(&func.inputs[0].kind, ParamType::Uint(_)) {
let expected_type = DynSolType::Uint(256).to_string();
if !func.inputs.is_empty() && matches!(&func.inputs[0].ty, expected_type) {
// redact private key input
Some(vec!["<pk>".to_string()])
} else {
Expand All @@ -48,9 +48,10 @@ pub(crate) fn decode_cheatcode_inputs(
}
"sign" => {
// sign(uint256,bytes32)
let mut decoded = func.decode_input(&data[SELECTOR_LEN..]).ok()?;
if !decoded.is_empty() && matches!(&func.inputs[0].kind, ParamType::Uint(_)) {
decoded[0] = Token::String("<pk>".to_string());
let mut decoded = func.abi_decode_input(&data[SELECTOR_LEN..], false).ok()?;
let expected_type = DynSolType::Uint(256).to_string();
if !decoded.is_empty() && matches!(&func.inputs[0].ty, expected_type) {
decoded[0] = DynSolValue::String("<pk>".to_string());
}
Some(decoded.iter().map(format_token).collect())
}
Expand Down Expand Up @@ -82,14 +83,14 @@ pub(crate) fn decode_cheatcode_inputs(
if verbosity >= 5 {
None
} else {
let mut decoded = func.decode_input(&data[SELECTOR_LEN..]).ok()?;
let mut decoded = func.abi_decode_input(&data[SELECTOR_LEN..], false).ok()?;
let token =
if func.name.as_str() == "parseJson" || func.name.as_str() == "keyExists" {
"<JSON file>"
} else {
"<stringified JSON>"
};
decoded[0] = Token::String(token.to_string());
decoded[0] = DynSolValue::String(token.to_string());
Some(decoded.iter().map(format_token).collect())
}
}
Expand Down