From 7484b6930afcd0f0f271b3fccfb2462e527e51d4 Mon Sep 17 00:00:00 2001 From: Alonso Date: Wed, 18 Dec 2024 13:36:33 +0100 Subject: [PATCH] reduce code + linter --- claimtxman/claimtxman.go | 2 +- db/pgstorage/pgstorage.go | 88 +++++++++++++-------------------------- 2 files changed, 30 insertions(+), 60 deletions(-) diff --git a/claimtxman/claimtxman.go b/claimtxman/claimtxman.go index df61ba8d..312be515 100644 --- a/claimtxman/claimtxman.go +++ b/claimtxman/claimtxman.go @@ -197,7 +197,7 @@ func (tm *ClaimTxManager) processDepositStatus(ger *etherman.GlobalExitRoot, dbT globalExitRoot, err = tm.storage.GetLatestTrustedGERByDeposit(tm.ctx, deposits[0].DepositCount, deposits[0].NetworkID, deposits[0].DestinationNetwork, dbTx) if errors.Is(err, gerror.ErrStorageNotFound) { log.Infof("RollupID: %d, not fully synced yet. Retrying in 2s...") - time.Sleep(2 * time.Second) + time.Sleep(tm.cfg.RetryInterval.Duration) globalExitRoot, err = tm.storage.GetLatestTrustedGERByDeposit(tm.ctx, deposits[0].DepositCount, deposits[0].NetworkID, deposits[0].DestinationNetwork, dbTx) if errors.Is(err, gerror.ErrStorageNotFound) { log.Infof("RollupID: %d, Still missing. Not fully synced yet. It will retry it later...") diff --git a/db/pgstorage/pgstorage.go b/db/pgstorage/pgstorage.go index cb754559..6a1b64ea 100644 --- a/db/pgstorage/pgstorage.go +++ b/db/pgstorage/pgstorage.go @@ -550,22 +550,7 @@ func (p *PostgresStorage) GetDeposits(ctx context.Context, destAddr string, limi return nil, err } - deposits := make([]*etherman.Deposit, 0, len(rows.RawValues())) - - for rows.Next() { - var ( - deposit etherman.Deposit - amount string - ) - err = rows.Scan(&deposit.Id, &deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.BlockNumber, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim) - if err != nil { - return nil, err - } - deposit.Amount, _ = new(big.Int).SetString(amount, 10) //nolint:gomnd - deposits = append(deposits, &deposit) - } - - return deposits, nil + return parseDeposits(rows, true) } // GetDepositCount gets the deposit count for the destination address. @@ -588,20 +573,7 @@ func (p *PostgresStorage) UpdateL1DepositsStatus(ctx context.Context, exitRoot [ return nil, err } - deposits := make([]*etherman.Deposit, 0, len(rows.RawValues())) - for rows.Next() { - var ( - deposit etherman.Deposit - amount string - ) - err = rows.Scan(&deposit.Id, &deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim) - if err != nil { - return nil, err - } - deposit.Amount, _ = new(big.Int).SetString(amount, 10) //nolint:gomnd - deposits = append(deposits, &deposit) - } - return deposits, nil + return parseDeposits(rows, false) } // UpdateL2DepositsStatus updates the ready_for_claim status of L2 deposits. @@ -614,27 +586,14 @@ func (p *PostgresStorage) UpdateL2DepositsStatus(ctx context.Context, exitRoot [ return err } -// GetDepositsFromOtherL2ToClaim returns L2 deposits whose destination is an specific L2 +// GetDepositsFromOtherL2ToClaim returns L2 deposits whose destination is an specific L2 func (p *PostgresStorage) GetDepositsFromOtherL2ToClaim(ctx context.Context, destinationNetwork uint32, dbTx pgx.Tx) ([]*etherman.Deposit, error) { const getL2DepositsToClaimStatusSQL = `select sync.deposit.id, sync.deposit.leaf_type, sync.deposit.orig_net, sync.deposit.orig_addr, sync.deposit.amount, sync.deposit.dest_net, sync.deposit.dest_addr, sync.deposit.deposit_cnt, sync.deposit.block_id, sync.deposit.network_id, sync.deposit.tx_hash, sync.deposit.metadata, sync.deposit.ready_for_claim FROM sync.deposit where sync.deposit.deposit_cnt not in (select index FROM sync.claim where sync.claim.network_id = $1) and sync.deposit.network_id !=0 and sync.deposit.dest_net = $1 and ready_for_claim =true order by deposit_cnt desc;` rows, err := p.getExecQuerier(dbTx).Query(ctx, getL2DepositsToClaimStatusSQL, destinationNetwork) if err != nil { return nil, err } - deposits := make([]*etherman.Deposit, 0, len(rows.RawValues())) - for rows.Next() { - var ( - deposit etherman.Deposit - amount string - ) - err = rows.Scan(&deposit.Id, &deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim) - if err != nil { - return nil, err - } - deposit.Amount, _ = new(big.Int).SetString(amount, 10) //nolint:gomnd - deposits = append(deposits, &deposit) - } - return deposits, nil + return parseDeposits(rows, false) } // GetLatestTrustedGERByDeposit return the latest trusted ger for an specific deposit @@ -728,21 +687,10 @@ func (p *PostgresStorage) GetPendingDepositsToClaim(ctx context.Context, destAdd return nil, 0, err } - deposits := make([]*etherman.Deposit, 0, len(rows.RawValues())) - - for rows.Next() { - var ( - deposit etherman.Deposit - amount string - ) - err = rows.Scan(&deposit.Id, &deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.BlockNumber, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim) - if err != nil { - return nil, 0, err - } - deposit.Amount, _ = new(big.Int).SetString(amount, 10) //nolint:gomnd - deposits = append(deposits, &deposit) + deposits, err := parseDeposits(rows, true) + if err != nil { + return nil, 0, err } - return deposits, totalCount, nil } @@ -759,3 +707,25 @@ func (p *PostgresStorage) UpdateBlocksForTesting(ctx context.Context, networkID _, err := p.getExecQuerier(dbTx).Exec(ctx, updateBlocksSQL, networkID, blockNum) return err } + +func parseDeposits(rows pgx.Rows, needBlockNum bool) ([]*etherman.Deposit, error) { + deposits := make([]*etherman.Deposit, 0, len(rows.RawValues())) + for rows.Next() { + var ( + deposit etherman.Deposit + amount string + err error + ) + if needBlockNum { + err = rows.Scan(&deposit.Id, &deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.BlockNumber, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim) + } else { + err = rows.Scan(&deposit.Id, &deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim) + } + if err != nil { + return nil, err + } + deposit.Amount, _ = new(big.Int).SetString(amount, 10) //nolint:gomnd + deposits = append(deposits, &deposit) + } + return deposits, nil +}