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

fix: patch empty event args #3348

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 28 additions & 2 deletions evm/src/trace/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ethers::{
};
use foundry_common::SELECTOR_LEN;
use foundry_utils::get_indexed_event;
use hashbrown::HashSet;
use std::collections::{BTreeMap, HashMap};

/// Build a new [CallTraceDecoder].
Expand Down Expand Up @@ -316,14 +317,24 @@ impl CallTraceDecoder {
}
}

for event in events {
for mut event in events {
// ensure all params are named, otherwise this will cause issues with decoding: See also <https://github.com/rust-ethereum/ethabi/issues/206>
let empty_params = patch_nameless_params(&mut event);
if let Ok(decoded) = event.parse_log(raw_log.clone()) {
*log = RawOrDecodedLog::Decoded(
event.name,
decoded
.params
.into_iter()
.map(|param| (param.name, self.apply_label(&param.value)))
.map(|param| {
// undo patched names
let name = if empty_params.contains(&param.name) {
"".to_string()
} else {
param.name
};
(name, self.apply_label(&param.value))
})
.collect(),
);
break
Expand All @@ -337,6 +348,21 @@ impl CallTraceDecoder {
}
}

/// This is a bit horrible but due to <https://github.com/rust-ethereum/ethabi/issues/206> we need to patch nameless (valid) params before decoding a logs, otherwise [`Event::parse_log()`] will result in wrong results since they're identified by name.
///
/// Returns a set of patched param names, that originally were empty.
fn patch_nameless_params(event: &mut Event) -> HashSet<String> {
let mut patches = HashSet::new();
if event.inputs.iter().filter(|input| input.name.is_empty()).count() > 1 {
for (idx, param) in event.inputs.iter_mut().enumerate() {
// this is an illegal arg name, which ensures patched identifiers are unique
param.name = format!("<patched {}>", idx);
patches.insert(param.name.clone());
}
}
patches
}

fn precompile<I, O>(number: u8, name: impl ToString, inputs: I, outputs: O) -> (Address, Function)
where
I: IntoIterator<Item = ParamType>,
Expand Down
5 changes: 5 additions & 0 deletions forge/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl TestConfig {
self
}

/// Executes the test runner
pub fn test(&mut self) -> BTreeMap<String, SuiteResult> {
self.runner.test(&self.filter, None, self.opts).unwrap()
}

#[track_caller]
pub fn run(&mut self) {
self.try_run().unwrap()
Expand Down
52 changes: 51 additions & 1 deletion forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests for reproducing issues

use crate::{config::*, test_helpers::filter::Filter};
use ethers::abi::Address;
use ethers::abi::{Address, Event, EventParam, Log, LogParam, ParamType, RawLog, Token};
use foundry_config::Config;
use std::str::FromStr;

Expand Down Expand Up @@ -37,6 +37,25 @@ macro_rules! test_repro_with_sender {
};
}

macro_rules! run_test_repro {
($issue:expr) => {
run_test_repro!($issue, false, None)
};
($issue:expr, $should_fail:expr, $sender:expr) => {{
let pattern = concat!(".*repros/", $issue);
let filter = Filter::path(pattern);

let mut config = Config::default();
if let Some(sender) = $sender {
config.sender = sender;
}

let mut config = TestConfig::with_filter(runner_with_config(config), filter)
.set_should_fail($should_fail);
config.test()
}};
}

// <https://github.com/foundry-rs/foundry/issues/2623>
#[test]
fn test_issue_2623() {
Expand Down Expand Up @@ -134,3 +153,34 @@ fn test_issue_3223() {
fn test_issue_3220() {
test_repro!("Issue3220");
}

// <https://github.com/foundry-rs/foundry/issues/3347>
#[test]
fn test_issue_3347() {
let mut res = run_test_repro!("Issue3347");
let mut res = res.remove("repros/Issue3347.sol:Issue3347Test").unwrap();
let test = res.test_results.remove("test()").unwrap();
assert_eq!(test.logs.len(), 1);
let event = Event {
name: "log2".to_string(),
inputs: vec![
EventParam { name: "x".to_string(), kind: ParamType::Uint(256), indexed: false },
EventParam { name: "y".to_string(), kind: ParamType::Uint(256), indexed: false },
],
anonymous: false,
};
let raw_log = RawLog {
topics: test.logs[0].topics.clone(),
data: test.logs[0].data.clone().to_vec().into(),
};
let log = event.parse_log(raw_log).unwrap();
assert_eq!(
log,
Log {
params: vec![
LogParam { name: "x".to_string(), value: Token::Uint(1u64.into()) },
LogParam { name: "y".to_string(), value: Token::Uint(2u64.into()) }
]
}
);
}
14 changes: 14 additions & 0 deletions testdata/repros/Issue3347.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;

import "ds-test/test.sol";
import "../cheats/Cheats.sol";

// https://github.com/foundry-rs/foundry/issues/3347
contract Issue3347Test is DSTest {
event log2(uint256, uint256);

function test() public {
emit log2(1, 2);
}
}