From e84f3227a62827c0ecbc5e2741eba6b7436e21dd Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Tue, 27 Aug 2024 01:03:54 +0400 Subject: [PATCH 1/2] fix: throw error on vm.expectEmit on anonymous event --- crates/cheatcodes/src/inspector.rs | 4 ++-- crates/cheatcodes/src/test/expect.rs | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/cheatcodes/src/inspector.rs b/crates/cheatcodes/src/inspector.rs index 09ced929462b5..a11d24bb92b16 100644 --- a/crates/cheatcodes/src/inspector.rs +++ b/crates/cheatcodes/src/inspector.rs @@ -1031,9 +1031,9 @@ impl Inspector for Cheatcodes { } } - fn log(&mut self, _interpreter: &mut Interpreter, _ecx: &mut EvmContext, log: &Log) { + fn log(&mut self, interpreter: &mut Interpreter, _ecx: &mut EvmContext, log: &Log) { if !self.expected_emits.is_empty() { - expect::handle_expect_emit(self, log); + expect::handle_expect_emit(self, log, interpreter); } // `recordLogs` diff --git a/crates/cheatcodes/src/test/expect.rs b/crates/cheatcodes/src/test/expect.rs index b68ae700a57b4..4592f6367c0ec 100644 --- a/crates/cheatcodes/src/test/expect.rs +++ b/crates/cheatcodes/src/test/expect.rs @@ -1,7 +1,9 @@ -use crate::{Cheatcode, Cheatcodes, CheatsCtxt, DatabaseExt, Result, Vm::*}; +use crate::{Cheatcode, Cheatcodes, CheatsCtxt, DatabaseExt, Error, Result, Vm::*}; use alloy_primitives::{address, hex, Address, Bytes, LogData as RawLog, U256}; use alloy_sol_types::{SolError, SolValue}; -use revm::interpreter::{return_ok, InstructionResult}; +use revm::interpreter::{ + return_ok, InstructionResult, Interpreter, InterpreterAction, InterpreterResult, +}; use spec::Vm; use std::collections::{hash_map::Entry, HashMap}; @@ -444,7 +446,11 @@ fn expect_emit( Ok(Default::default()) } -pub(crate) fn handle_expect_emit(state: &mut Cheatcodes, log: &alloy_primitives::Log) { +pub(crate) fn handle_expect_emit( + state: &mut Cheatcodes, + log: &alloy_primitives::Log, + interpreter: &mut Interpreter, +) { // Fill or check the expected emits. // We expect for emit checks to be filled as they're declared (from oldest to newest), // so we fill them and push them to the back of the queue. @@ -473,11 +479,19 @@ pub(crate) fn handle_expect_emit(state: &mut Cheatcodes, log: &alloy_primitives: let Some(expected) = &event_to_fill_or_check.log else { // Unless the caller is trying to match an anonymous event, the first topic must be // filled. - // TODO: failing this check should probably cause a warning if event_to_fill_or_check.anonymous || log.topics().first().is_some() { event_to_fill_or_check.log = Some(log.data.clone()); + state.expected_emits.push_back(event_to_fill_or_check); + } else { + interpreter.instruction_result = InstructionResult::Revert; + interpreter.next_action = InterpreterAction::Return { + result: InterpreterResult { + output: Error::encode("use vm.expectEmitAnonymous to match anonymous events"), + gas: interpreter.gas, + result: InstructionResult::Revert, + }, + }; } - state.expected_emits.push_back(event_to_fill_or_check); return }; From b1753fd9ce501f23290c78887c42320145eac8d8 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Tue, 27 Aug 2024 01:13:15 +0400 Subject: [PATCH 2/2] add test --- testdata/default/repros/Issue7457.t.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testdata/default/repros/Issue7457.t.sol b/testdata/default/repros/Issue7457.t.sol index 8d9d6f0753d5c..3c4080df2f8e7 100644 --- a/testdata/default/repros/Issue7457.t.sol +++ b/testdata/default/repros/Issue7457.t.sol @@ -61,10 +61,10 @@ contract Issue7457Test is DSTest, ITarget { target.emitAnonymousEventEmpty(); } - function testFailEmitEventNonIndexed() public { + function testEmitEventNonIndexedReverts() public { vm.expectEmit(false, false, false, true); + vm.expectRevert("use vm.expectEmitAnonymous to match anonymous events"); emit AnonymousEventNonIndexed(1); - target.emitAnonymousEventNonIndexed(1); } function testEmitEventNonIndexed() public {