@@ -4,6 +4,7 @@ use std::time::Duration;
44use crate :: metrics:: Metrics ;
55use crate :: pending_blocks:: PendingBlocks ;
66use alloy_eips:: { BlockId , BlockNumberOrTag } ;
7+ use alloy_primitives:: map:: foldhash:: { HashSet , HashSetExt } ;
78use alloy_primitives:: { Address , TxHash , U256 } ;
89use alloy_rpc_types:: simulate:: { SimBlock , SimulatePayload , SimulatedBlock } ;
910use alloy_rpc_types:: state:: { EvmOverrides , StateOverride , StateOverridesBuilder } ;
@@ -490,25 +491,33 @@ where
490491
491492 let pending_blocks = self . flashblocks_state . get_pending_blocks ( ) ;
492493
494+ let mut fetched_logs = HashSet :: new ( ) ;
493495 // Get historical logs if fromBlock is not pending
494496 if !matches ! ( from_block, Some ( BlockNumberOrTag :: Pending ) ) {
495- // Use the canonical block number from pending blocks to ensure consistency
496- let canonical_block = pending_blocks. get_canonical_block_number ( ) ;
497-
498- // Create a filter for historical data (fromBlock to canonical block)
497+ // Create a filter for historical data (fromBlock to latest)
499498 let mut historical_filter = filter. clone ( ) ;
500499 historical_filter. block_option = alloy_rpc_types_eth:: FilterBlockOption :: Range {
501500 from_block,
502- to_block : Some ( canonical_block ) ,
501+ to_block : Some ( BlockNumberOrTag :: Latest ) ,
503502 } ;
504503
505- let historical_logs = self . eth_filter . logs ( historical_filter) . await ?;
504+ let historical_logs: Vec < Log > = self . eth_filter . logs ( historical_filter) . await ?;
505+ for log in & historical_logs {
506+ fetched_logs. insert ( ( log. block_number , log. log_index ) ) ;
507+ }
506508 all_logs. extend ( historical_logs) ;
507509 }
508510
509511 // Always get pending logs when toBlock is pending
510512 let pending_logs = pending_blocks. get_pending_logs ( & filter) ;
511- all_logs. extend ( pending_logs) ;
513+
514+ // Dedup any logs from the pending state that may already have been covered in the historical logs
515+ let deduped_pending_logs: Vec < Log > = pending_logs
516+ . iter ( )
517+ . filter ( |log| !fetched_logs. contains ( & ( log. block_number , log. log_index ) ) )
518+ . cloned ( )
519+ . collect ( ) ;
520+ all_logs. extend ( deduped_pending_logs) ;
512521
513522 Ok ( all_logs)
514523 }
0 commit comments