@@ -171,6 +171,28 @@ func (db *ChainsDB) Finalized(chainID types.ChainID) (types.BlockSeal, error) {
171171 if finalizedL1 == (eth.L1BlockRef {}) {
172172 return types.BlockSeal {}, fmt .Errorf ("no finalized L1 signal, cannot determine L2 finality of chain %s yet" , chainID )
173173 }
174+
175+ // compare the finalized L1 block with the last derived block in the cross DB
176+ xDB , ok := db .crossDBs .Get (chainID )
177+ if ! ok {
178+ return types.BlockSeal {}, types .ErrUnknownChain
179+ }
180+ latestDerivedFrom , latestDerived , err := xDB .Latest ()
181+ if err != nil {
182+ return types.BlockSeal {}, fmt .Errorf ("could not get the latest derived pair for chain %s: %w" , chainID , err )
183+ }
184+ // if the finalized L1 block is newer than the latest L1 block used to derive L2 blocks,
185+ // the finality signal automatically applies to all previous blocks, including the latest derived block
186+ if finalizedL1 .Number > latestDerivedFrom .Number {
187+ db .logger .Warn ("Finalized L1 block is newer than the latest L1 for this chain. Assuming latest L2 is finalized" ,
188+ "chain" , chainID ,
189+ "finalizedL1" , finalizedL1 .Number ,
190+ "latestDerivedFrom" , latestDerivedFrom .Number ,
191+ "latestDerived" , latestDerivedFrom )
192+ return latestDerived , nil
193+ }
194+
195+ // otherwise, use the finalized L1 block to determine the final L2 block that was derived from it
174196 derived , err := db .LastDerivedFrom (chainID , finalizedL1 .ID ())
175197 if err != nil {
176198 return types.BlockSeal {}, fmt .Errorf ("could not find what was last derived in L2 chain %s from the finalized L1 block %s: %w" , chainID , finalizedL1 , err )
0 commit comments