@@ -922,22 +922,47 @@ func (p *PostgresStorage) GetSyncStatus(ctx context.Context, dbTx interface{}) (
922922}
923923
924924// ResetDeposits resets the state to a depositCount.
925- func (p * PostgresStorage ) ResetDeposits (ctx context.Context , depositCount uint32 , networkID uint32 , dbTx interface {}) error {
925+ func (p * PostgresStorage ) ResetDeposits (ctx context.Context , depositCount uint32 , networkID uint32 , backwardLETID uint64 , dbTx interface {}) error {
926926 if networkID == 0 {
927927 return errors .New ("cannot reset L1 deposits" )
928928 }
929- const resetSQL = "DELETE FROM sync.deposit WHERE deposit_cnt >= $1 AND network_id = $2"
929+ // Store the deposits in other table and remove from deposit table
930+ // Using CTE to ensure deposits are returned in ascending order by deposit_cnt
931+ const resetSQL = `
932+ WITH deleted_deposits AS (
933+ DELETE FROM sync.deposit
934+ WHERE deposit_cnt >= $1 AND network_id = $2
935+ RETURNING id, leaf_type, orig_net, orig_addr, amount, dest_net, dest_addr, deposit_cnt, block_id, network_id, tx_hash, metadata, ready_for_claim
936+ )
937+ SELECT id, leaf_type, orig_net, orig_addr, amount, dest_net, dest_addr, deposit_cnt, block_id, network_id, tx_hash, metadata, ready_for_claim
938+ FROM deleted_deposits
939+ ORDER BY deposit_cnt ASC`
930940 e := p .getExecQuerier (dbTx )
931- _ , err := e .Exec (ctx , resetSQL , depositCount , networkID )
932- return err
941+ rows , err := e .Query (ctx , resetSQL , depositCount , networkID )
942+ if err != nil {
943+ return err
944+ }
945+ defer rows .Close ()
946+ deposits , err := parseDeposits (rows , false )
947+ if err != nil {
948+ return err
949+ }
950+ const addDepositSQL = "INSERT INTO sync.deposit_backup (leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, ready_for_claim, backward_let_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)"
951+ for _ , deposit := range deposits {
952+ _ , err = e .Exec (ctx , addDepositSQL , deposit .LeafType , deposit .NetworkID , deposit .OriginalNetwork , deposit .OriginalAddress , deposit .Amount .String (), deposit .DestinationNetwork , deposit .DestinationAddress , deposit .BlockID , deposit .DepositCount , deposit .TxHash , deposit .Metadata , deposit .ReadyForClaim , backwardLETID )
953+ if err != nil {
954+ return err
955+ }
956+ }
957+ return nil
933958}
934959
935960// AddBackwardLET adds a new BackwardLET event to the db.
936- func (p * PostgresStorage ) AddBackwardLET (ctx context.Context , backwardLET * etherman.BackwardLET , dbTx interface {}) error {
937- const addExitRootSQL = "INSERT INTO sync.backward_let(block_id, previous_deposit_cnt, previous_root, new_deposit_cnt, new_root) VALUES ($1, $2, $3, $4, $5)"
938- e := p . getExecQuerier ( dbTx )
939- _ , err := e . Exec ( ctx , addExitRootSQL , backwardLET .BlockID , backwardLET .PreviousDepositCount , backwardLET .PreviousRoot , backwardLET .NewDepositCount , backwardLET .NewRoot )
940- return err
961+ func (p * PostgresStorage ) AddBackwardLET (ctx context.Context , backwardLET * etherman.BackwardLET , dbTx interface {}) ( uint64 , error ) {
962+ const addExitRootSQL = "INSERT INTO sync.backward_let(block_id, previous_deposit_cnt, previous_root, new_deposit_cnt, new_root) VALUES ($1, $2, $3, $4, $5) RETURNING id "
963+ var id uint64
964+ err := p . getExecQuerier ( dbTx ). QueryRow ( ctx , addExitRootSQL , backwardLET .BlockID , backwardLET .PreviousDepositCount , backwardLET .PreviousRoot , backwardLET .NewDepositCount , backwardLET .NewRoot ). Scan ( & id )
965+ return id , err
941966}
942967
943968// AddForwardLET adds a new ForwardLET event to the db.
@@ -983,6 +1008,37 @@ func (p *PostgresStorage) GetLastComputedRoot(ctx context.Context, networkID uin
9831008 return root , nil
9841009}
9851010
1011+ // GetAndDeleteOrphanDepositBackups finds and deletes deposit_backup records whose backward_let_id
1012+ // does not exist in the backward_let table, returning the deleted deposits.
1013+ // This uses a single optimized DELETE query with RETURNING clause and LEFT JOIN for better performance.
1014+ func (p * PostgresStorage ) GetAndDeleteOrphanDepositBackups (ctx context.Context , dbTx interface {}) ([]* etherman.Deposit , error ) {
1015+ // Single query that deletes orphan deposits and returns them in one operation
1016+ // Using LEFT JOIN with NULL check is often faster than NOT EXISTS for this use case
1017+ const deleteAndReturnOrphanDepositsSQL = `
1018+ DELETE FROM sync.deposit_backup AS db
1019+ USING (
1020+ SELECT db2.id
1021+ FROM sync.deposit_backup AS db2
1022+ LEFT JOIN sync.backward_let AS bl ON bl.id = db2.backward_let_id
1023+ WHERE bl.id IS NULL
1024+ ORDER BY db2.deposit_cnt ASC
1025+ ) AS orphans
1026+ WHERE db.id = orphans.id
1027+ RETURNING db.id, db.leaf_type, db.orig_net, db.orig_addr, db.amount, db.dest_net,
1028+ db.dest_addr, db.deposit_cnt, db.block_id, db.network_id, db.tx_hash,
1029+ db.metadata, db.ready_for_claim`
1030+
1031+ e := p .getExecQuerier (dbTx )
1032+ rows , err := e .Query (ctx , deleteAndReturnOrphanDepositsSQL )
1033+ if err != nil {
1034+ return nil , err
1035+ }
1036+ defer rows .Close ()
1037+
1038+ // Parse and return the deleted deposits
1039+ return parseDeposits (rows , false )
1040+ }
1041+
9861042// UpdateDepositsStatusForTesting updates the ready_for_claim status of all deposits for testing.
9871043func (p * PostgresStorage ) UpdateDepositsStatusForTesting (ctx context.Context , dbTx interface {}) error {
9881044 const updateDepositsStatusSQL = "UPDATE sync.deposit SET ready_for_claim = true;"
0 commit comments