Skip to content

Commit

Permalink
Fix sql bug
Browse files Browse the repository at this point in the history
  • Loading branch information
trunghai95 committed Jul 2, 2024
1 parent 5ab6477 commit 52cac20
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
6 changes: 3 additions & 3 deletions db/pgstorage/migrations/1004.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions db/pgstorage/pgstorage_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
40 changes: 40 additions & 0 deletions db/pgstorage/pgstorage_xlayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pgstorage

import (
"context"
"math/big"
"testing"

"github.com/0xPolygonHermez/zkevm-bridge-service/utils"
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions server/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion server/service_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions synchronizer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions synchronizer/synchronizer_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 52cac20

Please sign in to comment.