@@ -6,7 +6,7 @@ use alloy::{
66} ;
77use event_scanner:: { ScannerStatus , assert_empty, assert_next} ;
88
9- use crate :: common:: { TestCounter , setup_sync_scanner} ;
9+ use crate :: common:: { SyncScannerSetup , TestCounter , setup_sync_scanner} ;
1010
1111#[ tokio:: test]
1212async fn replays_historical_then_switches_to_live ( ) -> anyhow:: Result < ( ) > {
@@ -75,29 +75,37 @@ async fn sync_from_future_block_waits_until_minted() -> anyhow::Result<()> {
7575 Ok ( ( ) )
7676}
7777
78- #[ test_log :: test ( tokio:: test) ]
78+ #[ tokio:: test]
7979async fn block_confirmations_mitigate_reorgs ( ) -> anyhow:: Result < ( ) > {
8080 // any reorg ≤ 5 should be invisible to consumers
81- let setup = setup_sync_scanner ( None , None , BlockNumberOrTag :: Earliest , 5 ) . await ?;
82- let provider = setup. provider ;
83- let contract = setup. contract ;
84- let scanner = setup. scanner ;
85- let mut stream = setup. stream ;
81+ let SyncScannerSetup { provider, contract, scanner, mut stream, anvil : _anvil } =
82+ setup_sync_scanner ( None , None , BlockNumberOrTag :: Earliest , 5 ) . await ?;
8683
8784 // mine some initial "historic" blocks
88- contract. increase ( ) . send ( ) . await ?. watch ( ) . await ?;
89- provider. anvil_mine ( Some ( 5 ) , None ) . await ?;
85+ for _ in 0 ..7 {
86+ contract. increase ( ) . send ( ) . await ?. watch ( ) . await ?;
87+ }
9088
9189 scanner. start ( ) . await ?;
9290
9391 // emit "live" events
94- for _ in 0 ..4 {
92+ for _ in 0 ..2 {
9593 contract. increase ( ) . send ( ) . await ?. watch ( ) . await ?;
9694 }
9795
98- // assert only the first events has enough confirmations to be streamed
99- assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 1 ) } ] ) ;
96+ // assert historic events are streamed in a batch
97+ assert_next ! (
98+ stream,
99+ & [
100+ TestCounter :: CountIncreased { newCount: U256 :: from( 1 ) } ,
101+ TestCounter :: CountIncreased { newCount: U256 :: from( 2 ) }
102+ ]
103+ ) ;
104+ // switching to "live" phase
100105 assert_next ! ( stream, ScannerStatus :: SwitchingToLive ) ;
106+ // assert confirmed live events are streamed separately
107+ assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 3 ) } ] ) ;
108+ assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 4 ) } ] ) ;
101109 let stream = assert_empty ! ( stream) ;
102110
103111 // Perform a shallow reorg on the live tail
@@ -116,13 +124,14 @@ async fn block_confirmations_mitigate_reorgs() -> anyhow::Result<()> {
116124 provider. anvil_mine ( Some ( 10 ) , None ) . await ?;
117125
118126 // no `ReorgDetected` should be emitted
119- assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 2 ) } ] ) ;
120- assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 3 ) } ] ) ;
127+ assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 5 ) } ] ) ;
128+ assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 6 ) } ] ) ;
129+ assert_next ! ( stream, & [ TestCounter :: CountIncreased { newCount: U256 :: from( 7 ) } ] ) ;
121130 assert_next ! (
122131 stream,
123132 & [
124- TestCounter :: CountIncreased { newCount: U256 :: from( 4 ) } ,
125- TestCounter :: CountIncreased { newCount: U256 :: from( 5 ) }
133+ TestCounter :: CountIncreased { newCount: U256 :: from( 8 ) } ,
134+ TestCounter :: CountIncreased { newCount: U256 :: from( 9 ) }
126135 ]
127136 ) ;
128137 assert_empty ! ( stream) ;
0 commit comments