@@ -464,14 +464,16 @@ fn expect_emit(
464464 address : Option < Address > ,
465465 anonymous : bool ,
466466) -> Result {
467- state. expected_emits . push_back ( ExpectedEmit {
468- depth,
469- checks,
470- address,
471- found : false ,
472- log : None ,
473- anonymous,
474- } ) ;
467+ let expected_emit = ExpectedEmit { depth, checks, address, found : false , log : None , anonymous } ;
468+ if let Some ( found_emit_pos) = state. expected_emits . iter ( ) . position ( |emit| emit. found ) {
469+ // The order of emits already found (back of queue) should not be modified, hence push any
470+ // new emit before first found emit.
471+ state. expected_emits . insert ( found_emit_pos, expected_emit) ;
472+ } else {
473+ // If no expected emits then push new one at the back of queue.
474+ state. expected_emits . push_back ( expected_emit) ;
475+ }
476+
475477 Ok ( Default :: default ( ) )
476478}
477479
@@ -494,23 +496,34 @@ pub(crate) fn handle_expect_emit(
494496 return
495497 }
496498
497- // If there's anything to fill, we need to pop back.
498- // Otherwise, if there are any events that are unmatched, we try to match to match them
499- // in the order declared, so we start popping from the front (like a queue).
500- let mut event_to_fill_or_check =
501- if state. expected_emits . iter ( ) . any ( |expected| expected. log . is_none ( ) ) {
502- state. expected_emits . pop_back ( )
503- } else {
504- state. expected_emits . pop_front ( )
505- }
499+ let should_fill_logs = state. expected_emits . iter ( ) . any ( |expected| expected. log . is_none ( ) ) ;
500+ let index_to_fill_or_check = if should_fill_logs {
501+ // If there's anything to fill, we start with the last event to match in the queue
502+ // (without taking into account events already matched).
503+ state
504+ . expected_emits
505+ . iter ( )
506+ . position ( |emit| emit. found )
507+ . unwrap_or ( state. expected_emits . len ( ) )
508+ . saturating_sub ( 1 )
509+ } else {
510+ // Otherwise, if all expected logs are filled, we start to check any unmatched event
511+ // in the declared order, so we start from the front (like a queue).
512+ 0
513+ } ;
514+
515+ let mut event_to_fill_or_check = state
516+ . expected_emits
517+ . remove ( index_to_fill_or_check)
506518 . expect ( "we should have an emit to fill or check" ) ;
507519
508520 let Some ( expected) = & event_to_fill_or_check. log else {
509521 // Unless the caller is trying to match an anonymous event, the first topic must be
510522 // filled.
511523 if event_to_fill_or_check. anonymous || log. topics ( ) . first ( ) . is_some ( ) {
512524 event_to_fill_or_check. log = Some ( log. data . clone ( ) ) ;
513- state. expected_emits . push_back ( event_to_fill_or_check) ;
525+ // If we only filled the expected log then we put it back at the same position.
526+ state. expected_emits . insert ( index_to_fill_or_check, event_to_fill_or_check) ;
514527 } else {
515528 interpreter. instruction_result = InstructionResult :: Revert ;
516529 interpreter. next_action = InterpreterAction :: Return {
0 commit comments