Skip to content

Commit

Permalink
fix queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR552 committed Oct 11, 2023
1 parent 4774a30 commit 62eb771
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions claimtxman/claimtxman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func TestUpdateDepositStatus(t *testing.T) {
require.NoError(t, err)
require.Len(t, deposits, 1)
require.True(t, deposits[0].ReadyForClaim)
require.Equal(t, deposits[0].DepositCount, uint(1))
require.Equal(t, deposits[0].NetworkID, uint(0))
require.Equal(t, uint(1), deposits[0].DepositCount)
require.Equal(t, uint(0), deposits[0].NetworkID)

require.NoError(t, pg.UpdateL2DepositsStatus(ctx, l2Root, 1, nil))
deposits, err = pg.GetDeposits(ctx, destAdr, 10, 0, nil)
Expand Down
6 changes: 3 additions & 3 deletions db/pgstorage/pgstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func (p *PostgresStorage) GetTokenWrapped(ctx context.Context, originalNetwork u
// GetDepositCountByRoot gets the deposit count by the root.
func (p *PostgresStorage) GetDepositCountByRoot(ctx context.Context, root []byte, network uint8, dbTx pgx.Tx) (uint, error) {
var depositCount uint
const getDepositCountByRootSQL = "SELECT deposit_cnt FROM mt.root WHERE root = $1 AND network = $2"
const getDepositCountByRootSQL = "SELECT sync.deposit.deposit_cnt FROM mt.root INNER JOIN sync.deposit ON sync.deposit.id = mt.root.deposit_id WHERE mt.root.root = $1 AND mt.root.network = $2"
err := p.getExecQuerier(dbTx).QueryRow(ctx, getDepositCountByRootSQL, root, network).Scan(&depositCount)
if errors.Is(err, pgx.ErrNoRows) {
return 0, gerror.ErrStorageNotFound
Expand Down Expand Up @@ -457,7 +457,7 @@ func (p *PostgresStorage) UpdateBlocksForTesting(ctx context.Context, networkID
func (p *PostgresStorage) UpdateL1DepositsStatus(ctx context.Context, exitRoot []byte, dbTx pgx.Tx) ([]*etherman.Deposit, error) {
const updateDepositsStatusSQL = `UPDATE sync.deposit SET ready_for_claim = true
WHERE deposit_cnt <=
(SELECT deposit_cnt FROM mt.root WHERE root = $1 AND network = 0)
(SELECT sync.deposit.deposit_cnt FROM mt.root INNER JOIN sync.deposit ON sync.deposit.id = mt.root.deposit_id WHERE mt.root.root = $1 AND mt.root.network = 0)
AND network_id = 0 AND ready_for_claim = false
RETURNING leaf_type, orig_net, orig_addr, amount, dest_net, dest_addr, deposit_cnt, block_id, network_id, tx_hash, metadata, ready_for_claim;`
rows, err := p.getExecQuerier(dbTx).Query(ctx, updateDepositsStatusSQL, exitRoot)
Expand Down Expand Up @@ -485,7 +485,7 @@ func (p *PostgresStorage) UpdateL1DepositsStatus(ctx context.Context, exitRoot [
func (p *PostgresStorage) UpdateL2DepositsStatus(ctx context.Context, exitRoot []byte, networkID uint, dbTx pgx.Tx) error {
const updateDepositsStatusSQL = `UPDATE sync.deposit SET ready_for_claim = true
WHERE deposit_cnt <=
(SELECT deposit_cnt FROM mt.root WHERE root = $1 AND network = $2)
(SELECT sync.deposit.deposit_cnt FROM mt.root INNER JOIN sync.deposit ON sync.deposit.id = mt.root.deposit_id WHERE mt.root.root = $1 AND mt.root.network = $2)
AND network_id = $2 AND ready_for_claim = false;`
_, err := p.getExecQuerier(dbTx).Exec(ctx, updateDepositsStatusSQL, exitRoot, networkID)
return err
Expand Down
12 changes: 6 additions & 6 deletions db/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,20 @@ func TestMTStorage(t *testing.T) {

vals, err := pg.Get(ctx, root, tx)
require.NoError(t, err)
require.Equal(t, vals[0], leaf1)
require.Equal(t, vals[1], leaf2)
require.Equal(t, leaf1, vals[0])
require.Equal(t, leaf2, vals[1])

rRoot, err := pg.GetRoot(ctx, 1, 0, tx)
rRoot, err := pg.GetRoot(ctx, 0, 0, tx)
require.NoError(t, err)
require.Equal(t, rRoot, root)
require.Equal(t, root, rRoot)

count, err := pg.GetLastDepositCount(ctx, 0, tx)
require.NoError(t, err)
require.Equal(t, count, uint(1))
require.Equal(t, uint(0), count)

dCount, err := pg.GetDepositCountByRoot(ctx, root, 0, tx)
require.NoError(t, err)
require.Equal(t, dCount, uint(1))
require.Equal(t, uint(0), dCount)

require.NoError(t, tx.Commit(ctx))
}
Expand Down

0 comments on commit 62eb771

Please sign in to comment.