From 52cac208142e7a0624828d781d92a90a2792faa5 Mon Sep 17 00:00:00 2001 From: trunghai95 Date: Tue, 2 Jul 2024 13:17:45 +0800 Subject: [PATCH] Fix sql bug --- db/pgstorage/migrations/1004.sql | 6 ++-- db/pgstorage/pgstorage_xlayer.go | 8 +++--- db/pgstorage/pgstorage_xlayer_test.go | 40 +++++++++++++++++++++++++++ server/interfaces.go | 4 +-- server/service_xlayer.go | 2 +- synchronizer/interfaces.go | 4 +-- synchronizer/synchronizer_xlayer.go | 4 +-- 7 files changed, 54 insertions(+), 14 deletions(-) diff --git a/db/pgstorage/migrations/1004.sql b/db/pgstorage/migrations/1004.sql index 74da6014..376828e7 100644 --- a/db/pgstorage/migrations/1004.sql +++ b/db/pgstorage/migrations/1004.sql @@ -7,9 +7,9 @@ ALTER TABLE sync.deposit DROP COLUMN IF EXISTS dest_contract_addr; CREATE TABLE IF NOT EXISTS sync.bridge_balance ( id SERIAL PRIMARY KEY, - original_token_addr VARCHAR, - network_id INTEGER, - balance VARCHAR, + original_token_addr BYTEA NOT NULL, + network_id INTEGER NOT NULL, + balance VARCHAR NOT NULL DEFAULT '0', create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), modify_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), CONSTRAINT bridge_balance_uidx UNIQUE (original_token_addr, network_id) diff --git a/db/pgstorage/pgstorage_xlayer.go b/db/pgstorage/pgstorage_xlayer.go index 5e40fb7a..9e86a350 100644 --- a/db/pgstorage/pgstorage_xlayer.go +++ b/db/pgstorage/pgstorage_xlayer.go @@ -370,13 +370,13 @@ func (p *PostgresStorage) GetDepositsForUnitTest(ctx context.Context, destAddr s return p.getDepositList(ctx, getDepositsSQL, dbTx, common.FromHex(destAddr), limit, offset) } -func (p *PostgresStorage) GetBridgeBalance(ctx context.Context, originalTokenAddr string, networkID uint, forUpdate bool, dbTx pgx.Tx) (*big.Int, error) { +func (p *PostgresStorage) GetBridgeBalance(ctx context.Context, originalTokenAddr common.Address, networkID uint, forUpdate bool, dbTx pgx.Tx) (*big.Int, error) { var getBridgeBalanceSQL = "SELECT balance FROM sync.bridge_balance WHERE original_token_addr = $1 AND network_id = $2" if forUpdate { getBridgeBalanceSQL += " FOR UPDATE" } var s string - err := p.getExecQuerier(dbTx).QueryRow(ctx, getBridgeBalanceSQL, common.FromHex(originalTokenAddr), networkID).Scan(&s) + err := p.getExecQuerier(dbTx).QueryRow(ctx, getBridgeBalanceSQL, originalTokenAddr, networkID).Scan(&s) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return big.NewInt(0), nil @@ -388,13 +388,13 @@ func (p *PostgresStorage) GetBridgeBalance(ctx context.Context, originalTokenAdd return balance, nil } -func (p *PostgresStorage) SetBridgeBalance(ctx context.Context, originalTokenAddr string, networkID uint, balance *big.Int, dbTx pgx.Tx) error { +func (p *PostgresStorage) SetBridgeBalance(ctx context.Context, originalTokenAddr common.Address, networkID uint, balance *big.Int, dbTx pgx.Tx) error { var setBridgeBalanceSQL = ` INSERT INTO sync.bridge_balance (original_token_addr, network_id, balance) VALUES ($1, $2, $3) ON CONFLICT ON CONSTRAINT bridge_balance_uidx DO UPDATE SET balance = EXCLUDED.balance, modify_time = $4` - _, err := p.getExecQuerier(dbTx).Exec(ctx, setBridgeBalanceSQL, common.FromHex(originalTokenAddr), networkID, balance.String(), time.Now()) + _, err := p.getExecQuerier(dbTx).Exec(ctx, setBridgeBalanceSQL, originalTokenAddr, networkID, balance.String(), time.Now()) return err } diff --git a/db/pgstorage/pgstorage_xlayer_test.go b/db/pgstorage/pgstorage_xlayer_test.go index e6d6be37..d44c8da0 100644 --- a/db/pgstorage/pgstorage_xlayer_test.go +++ b/db/pgstorage/pgstorage_xlayer_test.go @@ -2,6 +2,7 @@ package pgstorage import ( "context" + "math/big" "testing" "github.com/0xPolygonHermez/zkevm-bridge-service/utils" @@ -46,3 +47,42 @@ func TestGetDepositsXLayer(t *testing.T) { require.NoError(t, err) require.Len(t, deposits, 2) } + +func TestBridgeBalance(t *testing.T) { + dbCfg := NewConfigFromEnv() + ctx := context.Background() + err := InitOrReset(dbCfg) + require.NoError(t, err) + + store, err := NewPostgresStorage(dbCfg) + require.NoError(t, err) + + addr := common.HexToAddress("0xB36c9325294CBd1b6BF5ECB33d9b035F1f0e9B8A") + networkID := uint(1) + + dbTx, err := store.Begin(ctx) + require.NoError(t, err) + + balance, err := store.GetBridgeBalance(ctx, addr, networkID, false, nil) + require.NoError(t, err) + require.Equal(t, 0, balance.Cmp(big.NewInt(0))) + + balance.SetInt64(123456) + err = store.SetBridgeBalance(ctx, addr, networkID, balance, nil) + require.NoError(t, err) + + balance, err = store.GetBridgeBalance(ctx, addr, networkID, true, nil) + require.NoError(t, err) + require.Equal(t, 0, balance.Cmp(big.NewInt(123456))) + + balance.SetInt64(10000000) + err = store.SetBridgeBalance(ctx, addr, networkID, balance, nil) + require.NoError(t, err) + + balance, err = store.GetBridgeBalance(ctx, addr, networkID, false, nil) + require.NoError(t, err) + require.Equal(t, 0, balance.Cmp(big.NewInt(10000000))) + + err = dbTx.Rollback(ctx) + require.NoError(t, err) +} diff --git a/server/interfaces.go b/server/interfaces.go index 4c337d4e..1c15acee 100644 --- a/server/interfaces.go +++ b/server/interfaces.go @@ -34,6 +34,6 @@ type bridgeServiceStorage interface { GetClaimTxById(ctx context.Context, id uint, dbTx pgx.Tx) (*ctmtypes.MonitoredTx, error) GetClaimTxsByStatusWithLimit(ctx context.Context, statuses []ctmtypes.MonitoredTxStatus, limit uint, offset uint, dbTx pgx.Tx) ([]ctmtypes.MonitoredTx, error) GetDepositsForUnitTest(ctx context.Context, destAddr string, limit uint, offset uint, dbTx pgx.Tx) ([]*etherman.Deposit, error) - GetBridgeBalance(ctx context.Context, originalTokenAddr string, networkID uint, forUpdate bool, dbTx pgx.Tx) (*big.Int, error) - SetBridgeBalance(ctx context.Context, originalTokenAddr string, networkID uint, balance *big.Int, dbTx pgx.Tx) error + GetBridgeBalance(ctx context.Context, originalTokenAddr common.Address, networkID uint, forUpdate bool, dbTx pgx.Tx) (*big.Int, error) + SetBridgeBalance(ctx context.Context, originalTokenAddr common.Address, networkID uint, balance *big.Int, dbTx pgx.Tx) error } diff --git a/server/service_xlayer.go b/server/service_xlayer.go index a457a296..ea76e112 100644 --- a/server/service_xlayer.go +++ b/server/service_xlayer.go @@ -580,7 +580,7 @@ func (s *bridgeService) GetWstEthL2TokenNotWithdrawn(ctx context.Context, req *p }, nil } tokenAddr := processor.GetTokenAddressList()[0] - value, err := s.storage.GetBridgeBalance(ctx, tokenAddr.String(), utils.GetRollupNetworkId(), false, nil) + value, err := s.storage.GetBridgeBalance(ctx, tokenAddr, utils.GetRollupNetworkId(), false, nil) if err != nil { log.Errorf("failed to get wstETH l2TokenNotWithdrawn, err: %v", err) return &pb.GetWstEthL2TokenNotWithdrawnResponse{ diff --git a/synchronizer/interfaces.go b/synchronizer/interfaces.go index f7524870..bbeb6869 100644 --- a/synchronizer/interfaces.go +++ b/synchronizer/interfaces.go @@ -41,8 +41,8 @@ type storageInterface interface { // XLayer GetDeposit(ctx context.Context, depositCounterUser uint, networkID uint, dbTx pgx.Tx) (*etherman.Deposit, error) AddDepositXLayer(ctx context.Context, deposit *etherman.Deposit, dbTx pgx.Tx) (uint64, error) - GetBridgeBalance(ctx context.Context, originalTokenAddr string, networkID uint, forUpdate bool, dbTx pgx.Tx) (*big.Int, error) - SetBridgeBalance(ctx context.Context, originalTokenAddr string, networkID uint, balance *big.Int, dbTx pgx.Tx) error + GetBridgeBalance(ctx context.Context, originalTokenAddr common.Address, networkID uint, forUpdate bool, dbTx pgx.Tx) (*big.Int, error) + SetBridgeBalance(ctx context.Context, originalTokenAddr common.Address, networkID uint, balance *big.Int, dbTx pgx.Tx) error } type bridgectrlInterface interface { diff --git a/synchronizer/synchronizer_xlayer.go b/synchronizer/synchronizer_xlayer.go index 1658466b..6dab7c24 100644 --- a/synchronizer/synchronizer_xlayer.go +++ b/synchronizer/synchronizer_xlayer.go @@ -306,13 +306,13 @@ func (s *ClientSynchronizer) processWstETHCommon(deposit *etherman.Deposit, valu // Update DB using the token original address tokenAddr := processor.GetTokenAddressList()[0] - value, err := s.storage.GetBridgeBalance(s.ctx, tokenAddr.String(), deposit.NetworkID, true, dbTx) + value, err := s.storage.GetBridgeBalance(s.ctx, tokenAddr, deposit.NetworkID, true, dbTx) if err != nil { return errors.Wrap(err, "GetBridgeBalance from DB err") } // Update the value valueUpdateFn(value) log.Debugf("setting wstETH L2TokenNotWithdrawn to %v", value.String()) - err = s.storage.SetBridgeBalance(s.ctx, tokenAddr.String(), deposit.NetworkID, value, dbTx) + err = s.storage.SetBridgeBalance(s.ctx, tokenAddr, deposit.NetworkID, value, dbTx) return errors.Wrap(err, "SetBridgeBalance to DB err") }