66 "errors"
77 "fmt"
88 mutex "sync"
9+ "time"
910
1011 aggkitcommon "github.com/agglayer/aggkit/common"
1112 "github.com/agglayer/aggkit/db"
@@ -28,13 +29,14 @@ var (
2829)
2930
3031type processor struct {
31- db * sql.DB
32- l1InfoTree treetypes.FullTreer
33- rollupExitTree treetypes.FullTreer
34- mu mutex.RWMutex
35- halted bool
36- haltedReason string
37- log * log.Logger
32+ db * sql.DB
33+ l1InfoTree treetypes.FullTreer
34+ rollupExitTree treetypes.FullTreer
35+ mu mutex.RWMutex
36+ halted bool
37+ haltedReason string
38+ log * log.Logger
39+ gerReorgNotifier aggkitcommon.PubSub [GERReorgEvent ]
3840}
3941
4042// UpdateL1InfoTree representation of the UpdateL1InfoTree event
@@ -121,6 +123,18 @@ func (l *L1InfoTreeInitial) String() string {
121123 return fmt .Sprintf ("BlockNumber: %d, LeafCount: %d, L1InfoRoot: %s" , l .BlockNumber , l .LeafCount , l .L1InfoRoot .String ())
122124}
123125
126+ // GERReorgEvent represents information about GERs removed during a reorg
127+ type GERReorgEvent struct {
128+ FirstReorgedBlock uint64 // Block number where reorg started
129+ ReorgedLeaves []* L1InfoTreeLeaf // All affected L1InfoTree leaves
130+ Timestamp uint64 // Unix timestamp when reorg occurred
131+ }
132+
133+ func (g * GERReorgEvent ) String () string {
134+ return fmt .Sprintf ("GERReorgEvent{FirstReorgedBlock: %d, ReorgedLeaves: %d, Timestamp: %d}" ,
135+ g .FirstReorgedBlock , len (g .ReorgedLeaves ), g .Timestamp )
136+ }
137+
124138// Hash as expected by the tree
125139func (l * L1InfoTreeLeaf ) GetHash () common.Hash {
126140 rawTimestamp := aggkitcommon .Uint64ToBigEndianBytes (l .Timestamp )
@@ -148,10 +162,11 @@ func newProcessor(dbPath string) (*processor, error) {
148162 return nil , err
149163 }
150164 return & processor {
151- db : database ,
152- l1InfoTree : tree .NewAppendOnlyTree (database , migrations .L1InfoTreePrefix ),
153- rollupExitTree : tree .NewUpdatableTree (database , migrations .RollupExitTreePrefix ),
154- log : log .WithFields ("processor" , "l1infotreesync" ),
165+ db : database ,
166+ l1InfoTree : tree .NewAppendOnlyTree (database , migrations .L1InfoTreePrefix ),
167+ rollupExitTree : tree .NewUpdatableTree (database , migrations .RollupExitTreePrefix ),
168+ log : log .WithFields ("processor" , "l1infotreesync" ),
169+ gerReorgNotifier : aggkitcommon .NewGenericSubscriber [GERReorgEvent ](),
155170 }, nil
156171}
157172
@@ -327,6 +342,18 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
327342 }
328343 }()
329344
345+ // Query affected L1InfoTreeLeaves BEFORE cascade delete
346+ var reorgedLeaves []* L1InfoTreeLeaf
347+ err = meddler .QueryAll (tx , & reorgedLeaves ,
348+ `SELECT * FROM l1info_leaf WHERE block_num >= $1 ORDER BY block_num ASC, block_pos ASC;` ,
349+ firstReorgedBlock )
350+ if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
351+ return fmt .Errorf ("failed to query affected l1info_leaf entries: %w" , err )
352+ }
353+
354+ p .log .Debugf ("found %d l1info_leaf entries to be reorged from block %d" ,
355+ len (reorgedLeaves ), firstReorgedBlock )
356+
330357 res , err := tx .Exec (`DELETE FROM block WHERE num >= $1;` , firstReorgedBlock )
331358 if err != nil {
332359 return err
@@ -349,12 +376,24 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
349376 return err
350377 }
351378
352- p .log .Infof ("reorged to block %d, %d rows affected" , firstReorgedBlock , rowsAffected )
379+ p .log .Infof ("reorged to block %d, %d block rows affected, %d l1info_leaf entries removed" ,
380+ firstReorgedBlock , rowsAffected , len (reorgedLeaves ))
353381
354382 shouldRollback = false
355383
356384 if rowsAffected > 0 {
357385 p .unhalt ()
386+
387+ // Publish notification ONLY if there were affected leaves
388+ if len (reorgedLeaves ) > 0 {
389+ event := GERReorgEvent {
390+ FirstReorgedBlock : firstReorgedBlock ,
391+ ReorgedLeaves : reorgedLeaves ,
392+ Timestamp : uint64 (time .Now ().Unix ()),
393+ }
394+ p .log .Infof ("publishing GER reorg event: %s" , event .String ())
395+ p .gerReorgNotifier .Publish (event )
396+ }
358397 }
359398 return nil
360399}
0 commit comments