@@ -1306,7 +1306,12 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
13061306 newDepositCount , prevDepositCount , err )
13071307 }
13081308
1309+ restoredDepositCounts := make ([]uint32 , 0 , len (restoredBridges ))
13091310 for _ , restoredBridge := range restoredBridges {
1311+ if p .log .IsEnabledLogLevel (zapcore .DebugLevel ) {
1312+ restoredDepositCounts = append (restoredDepositCounts , restoredBridge .DepositCount )
1313+ }
1314+
13101315 if _ , err = p .exitTree .PutLeaf (tx , restoredBridge .BlockNum , restoredBridge .BlockPos ,
13111316 types.Leaf {
13121317 Index : restoredBridge .DepositCount ,
@@ -1319,6 +1324,8 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
13191324 }
13201325 }
13211326
1327+ p .log .Debugf ("restored bridges with deposit counts: %v" , restoredDepositCounts )
1328+
13221329 // ---------------------------------------------------------------------
13231330 // 4. Remove restored bridges from the bridge_archive table
13241331 // ---------------------------------------------------------------------
@@ -1460,43 +1467,22 @@ func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error {
14601467 }
14611468
14621469 if event .BackwardLET != nil {
1463- newDepositCountU64 , err := aggkitcommon . SafeUint64 (event .BackwardLET .NewDepositCount )
1470+ newDepositCount , leafIndex , err := normalizeDepositCount (event .BackwardLET .NewDepositCount )
14641471 if err != nil {
1465- return fmt .Errorf ("failed to convert new deposit count to uint64: %w" , err )
1466- }
1467-
1468- leafIndex , err := aggkitcommon .SafeUint32 (newDepositCountU64 )
1469- if err != nil {
1470- return fmt .Errorf ("failed to convert new deposit count (uint64) to leaf index (uint32): %w" ,
1471- err )
1472+ return err
14721473 }
14731474
14741475 // 1. remove all the bridges whose deposit_count is greater than the one captured by the BackwardLET event
1475- deleteBridgesSQL := fmt .Sprintf ("DELETE from %s WHERE deposit_count > $1 RETURNING deposit_count" , bridgeTableName )
1476- rows , err := tx .Query (deleteBridgesSQL , newDepositCountU64 )
1476+ err = p .deleteBridgesAbove (ctx , tx , newDepositCount )
14771477 if err != nil {
1478- return fmt .Errorf ("failed to delete bridges: %w" , err )
1479- }
1480- defer rows .Close ()
1481-
1482- var deleted []uint32
1483- for rows .Next () {
1484- var depositCount uint32
1485- if err := rows .Scan (& depositCount ); err != nil {
1486- return err
1487- }
1488- deleted = append (deleted , depositCount )
1489- }
1490-
1491- if len (deleted ) > 0 {
1492- p .log .Debugf ("deleted bridges with deposit_count > %d due to BackwardLET: %v" ,
1493- newDepositCountU64 , deleted )
1478+ return fmt .Errorf ("failed to delete bridges above deposit count %d: %w" ,
1479+ newDepositCount , err )
14941480 }
14951481
14961482 // 2. remove all leafs from the exit tree with indices greater than leafIndex in the exit tree
14971483 if err := p .exitTree .BackwardToIndex (ctx , tx , leafIndex ); err != nil {
14981484 p .log .Errorf ("failed to backward local exit tree to leaf index %d (deposit count: %d)" ,
1499- leafIndex , newDepositCountU64 )
1485+ leafIndex , newDepositCount )
15001486 return err
15011487 }
15021488
@@ -1534,6 +1520,54 @@ func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error {
15341520 return nil
15351521}
15361522
1523+ // normalizeDepositCount checks whether given depositCount can fit into the uint64 and uint32 and downcasts it.
1524+ // Otherwise it returns an error.
1525+ func normalizeDepositCount (depositCount * big.Int ) (uint64 , uint32 , error ) {
1526+ u64 , err := aggkitcommon .SafeUint64 (depositCount )
1527+ if err != nil {
1528+ return 0 , 0 , fmt .Errorf ("invalid deposit count: %w" , err )
1529+ }
1530+
1531+ u32 , err := aggkitcommon .SafeUint32 (u64 )
1532+ if err != nil {
1533+ return 0 , 0 , fmt .Errorf ("invalid deposit count: %w" , err )
1534+ }
1535+
1536+ return u64 , u32 , nil
1537+ }
1538+
1539+ // deleteBridgesAbove removes all the bridges whose depositCount is greater than the provided one.
1540+ func (p * processor ) deleteBridgesAbove (ctx context.Context , tx dbtypes.Txer , depositCount uint64 ) error {
1541+ query := fmt .Sprintf (`
1542+ DELETE FROM %s
1543+ WHERE deposit_count > $1
1544+ RETURNING deposit_count
1545+ ` , bridgeTableName )
1546+
1547+ rows , err := tx .QueryContext (ctx , query , depositCount )
1548+ if err != nil {
1549+ return err
1550+ }
1551+ defer rows .Close ()
1552+
1553+ var deleted []uint32
1554+ for rows .Next () {
1555+ var dc uint32
1556+ if err := rows .Scan (& dc ); err != nil {
1557+ return err
1558+ }
1559+ deleted = append (deleted , dc )
1560+ }
1561+
1562+ if len (deleted ) > 0 {
1563+ p .log .Debugf ("BackwardLET removed bridges with deposit_count > %d: %v" ,
1564+ depositCount , deleted ,
1565+ )
1566+ }
1567+
1568+ return nil
1569+ }
1570+
15371571// GetTotalNumberOfRecords returns the total number of records in the given table
15381572func (p * processor ) GetTotalNumberOfRecords (ctx context.Context , tableName , whereClause string ) (int , error ) {
15391573 if ! tableNameRegex .MatchString (tableName ) {
0 commit comments