@@ -361,8 +361,8 @@ pub mod pallet {
361361 // Only notify gateways with different addresses (same address automatically accepts)
362362 for ( existing_state_machine, existing_gateway_info) in Gateways :: < T > :: iter ( ) {
363363 // Skip if same state machine or same gateway address
364- if existing_state_machine == state_machine ||
365- existing_gateway_info. gateway == gateway
364+ if existing_state_machine == state_machine
365+ || existing_gateway_info. gateway == gateway
366366 {
367367 continue ;
368368 }
@@ -616,24 +616,10 @@ pub mod pallet {
616616 {
617617 fn on_initialize ( n : BlockNumberFor < T > ) -> Weight {
618618 let Some ( config) = PhantomOrderConfig :: < T > :: get ( ) else {
619- return Weight :: zero ( ) ;
619+ // Reserve the read on_finalize performs on CurrentPhantomOrder.
620+ return T :: DbWeight :: get ( ) . reads ( 2 ) ;
620621 } ;
621622
622- // Signal each active commitment on the block its bid window closes so the indexer can
623- // aggregate that order's snapshot. Done before the generation gate so it still fires on
624- // blocks where no new batch is produced.
625- if let Some ( active) = CurrentPhantomOrder :: < T > :: get ( ) {
626- let window: BlockNumberFor < T > = Self :: phantom_bid_window ( ) . into ( ) ;
627- for ( commitment, info) in active. iter ( ) {
628- if n == info. created_at_block . saturating_add ( window) {
629- Self :: deposit_event ( Event :: PhantomBidWindowExhausted {
630- commitment : * commitment,
631- created_at : info. created_at_block ,
632- } ) ;
633- }
634- }
635- }
636-
637623 let should_generate = match LastPhantomGeneration :: < T > :: get ( ) {
638624 None => true ,
639625 Some ( last) => {
@@ -642,8 +628,9 @@ pub mod pallet {
642628 } ,
643629 } ;
644630
631+ // reads here (config + last_generation) plus the reads on_finalize performs.
645632 if !should_generate {
646- return T :: DbWeight :: get ( ) . reads ( 3 ) ;
633+ return T :: DbWeight :: get ( ) . reads ( 4 ) ;
647634 }
648635
649636 // Phantom orders carry the latest confirmed height as their deadline so they read
@@ -656,7 +643,7 @@ pub mod pallet {
656643 "No confirmed state machine height for {:?}, skipping phantom order generation" ,
657644 config. chain,
658645 ) ;
659- return T :: DbWeight :: get ( ) . reads ( 4 ) ;
646+ return T :: DbWeight :: get ( ) . reads ( 5 ) ;
660647 } ;
661648
662649 let mut batch: BoundedVec <
@@ -681,7 +668,28 @@ pub mod pallet {
681668 CurrentPhantomOrder :: < T > :: put ( batch) ;
682669 LastPhantomGeneration :: < T > :: put ( n) ;
683670
684- T :: DbWeight :: get ( ) . reads_writes ( 4 , 2 )
671+ // reads: config + last_generation + latest_height + the on_finalize reads.
672+ T :: DbWeight :: get ( ) . reads_writes ( 5 , 2 )
673+ }
674+
675+ fn on_finalize ( n : BlockNumberFor < T > ) {
676+ // Signal each active commitment on the block its bid window closes so the indexer can
677+ // aggregate that order's snapshot. Emitted in on_finalize (after all extrinsics) so any
678+ // bid placed in the window-closing block is already in storage when the snapshot is
679+ // taken. The bid window is expected to be shorter than the generation interval, so the
680+ // active batch is never replaced by on_initialize on the same block its window closes.
681+ let Some ( active) = CurrentPhantomOrder :: < T > :: get ( ) else {
682+ return ;
683+ } ;
684+ let window: BlockNumberFor < T > = Self :: phantom_bid_window ( ) . into ( ) ;
685+ for ( commitment, info) in active. iter ( ) {
686+ if n == info. created_at_block . saturating_add ( window) {
687+ Self :: deposit_event ( Event :: PhantomBidWindowExhausted {
688+ commitment : * commitment,
689+ created_at : info. created_at_block ,
690+ } ) ;
691+ }
692+ }
685693 }
686694 }
687695
0 commit comments