Skip to content

Commit

Permalink
remove deposit counter from mt.root table (0xPolygonHermez#531)
Browse files Browse the repository at this point in the history
* remove deposit counter from mt.root table

* linter

* fix queries

* fix unit test
  • Loading branch information
ARR552 authored Oct 16, 2023
1 parent 709dd49 commit 325fe34
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bridgectrl/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ type merkleTreeStore interface {
Get(ctx context.Context, key []byte, dbTx pgx.Tx) ([][]byte, error)
BulkSet(ctx context.Context, rows [][]interface{}, dbTx pgx.Tx) error
GetRoot(ctx context.Context, depositCount uint, network uint, dbTx pgx.Tx) ([]byte, error)
SetRoot(ctx context.Context, root []byte, depositID uint64, depositCount uint, network uint, dbTx pgx.Tx) error
SetRoot(ctx context.Context, root []byte, depositID uint64, network uint, dbTx pgx.Tx) error
GetLastDepositCount(ctx context.Context, network uint, dbTx pgx.Tx) (uint, error)
}
2 changes: 1 addition & 1 deletion bridgectrl/merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (mt *MerkleTree) addLeaf(ctx context.Context, depositID uint64, leaf [KeyLe
}
}

err := mt.store.SetRoot(ctx, cur[:], depositID, mt.count, mt.network, dbTx)
err := mt.store.SetRoot(ctx, cur[:], depositID, mt.network, dbTx)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions bridgectrl/merkletree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestMTAddLeaf(t *testing.T) {
DestinationNetwork: testVector.NewLeaf.DestinationNetwork,
DestinationAddress: common.HexToAddress(testVector.NewLeaf.DestinationAddress),
BlockNumber: 0,
DepositCount: uint(i + 1),
DepositCount: uint(i),
Metadata: common.FromHex(testVector.NewLeaf.Metadata),
}
depositID, err := store.AddDeposit(ctx, deposit, nil)
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestMTGetProof(t *testing.T) {
DestinationNetwork: leaf.DestinationNetwork,
DestinationAddress: common.HexToAddress(leaf.DestinationAddress),
BlockID: blockID,
DepositCount: uint(li + 1),
DepositCount: uint(li),
Metadata: common.FromHex(leaf.Metadata),
}
depositID, err := store.AddDeposit(ctx, deposit, nil)
Expand Down
14 changes: 7 additions & 7 deletions claimtxman/claimtxman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestUpdateDepositStatus(t *testing.T) {
depositID, err := pg.AddDeposit(ctx, deposit, nil)
require.NoError(t, err)
l1Root := common.FromHex("0x838c5655cb21c6cb83313b5a631175dff4963772cce9108188b34ac87c81c41e")
require.NoError(t, pg.SetRoot(ctx, l1Root, depositID, deposit.DepositCount, deposit.NetworkID, nil))
require.NoError(t, pg.SetRoot(ctx, l1Root, depositID, deposit.NetworkID, nil))

block = &etherman.Block{
BlockNumber: 1,
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestUpdateDepositStatus(t *testing.T) {
depositID, err = pg.AddDeposit(ctx, deposit, nil)
require.NoError(t, err)
l2Root := common.FromHex("0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30")
require.NoError(t, pg.SetRoot(ctx, l2Root, depositID, deposit.DepositCount, deposit.NetworkID, nil))
require.NoError(t, pg.SetRoot(ctx, l2Root, depositID, deposit.NetworkID, nil))

deposit = &etherman.Deposit{
NetworkID: 1,
Expand All @@ -170,14 +170,14 @@ func TestUpdateDepositStatus(t *testing.T) {
depositID, err = pg.AddDeposit(ctx, deposit, nil)
require.NoError(t, err)
l2Root1 := common.FromHex("0xda7bce9f4e8618b6bd2f4132ce798cdc7a60e7e1460a7299e3c6342a579626d2")
require.NoError(t, pg.SetRoot(ctx, l2Root1, depositID, deposit.DepositCount, deposit.NetworkID, nil))
require.NoError(t, pg.SetRoot(ctx, l2Root1, depositID, deposit.NetworkID, nil))

deposits, err := pg.UpdateL1DepositsStatus(ctx, l1Root, nil)
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 Expand Up @@ -222,7 +222,7 @@ func TestUpdateL2DepositStatusMultipleRollups(t *testing.T) {
depositID1, err := pg.AddDeposit(ctx, deposit1, nil)
require.NoError(t, err)
l2Root1 := common.FromHex("0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30")
require.NoError(t, pg.SetRoot(ctx, l2Root1, depositID1, deposit1.DepositCount, deposit1.NetworkID, nil))
require.NoError(t, pg.SetRoot(ctx, l2Root1, depositID1, deposit1.NetworkID, nil))

block2 := &etherman.Block{
BlockNumber: 1,
Expand All @@ -249,7 +249,7 @@ func TestUpdateL2DepositStatusMultipleRollups(t *testing.T) {
depositID2, err := pg.AddDeposit(ctx, deposit2, nil)
require.NoError(t, err)
l2Root2 := common.FromHex("0x90c89934975cc71a021a11dbe78cb2008d77e018dfffcc629b8d6d4dc905ac5c")
require.NoError(t, pg.SetRoot(ctx, l2Root2, depositID2, deposit2.DepositCount, deposit2.NetworkID, nil))
require.NoError(t, pg.SetRoot(ctx, l2Root2, depositID2, deposit2.NetworkID, nil))

// This root is for network 1, this won't upgrade anything
require.NoError(t, pg.UpdateL2DepositsStatus(ctx, l2Root1, 2, nil))
Expand Down
20 changes: 20 additions & 0 deletions db/pgstorage/migrations/0007.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- +migrate Up
ALTER TABLE mt.root
DROP COLUMN IF EXISTS deposit_cnt;

-- +migrate Down
ALTER TABLE mt.root
ADD COLUMN deposit_cnt BIGINT;

-- +migrate StatementBegin
DO $$
DECLARE
rt RECORD;
BEGIN
FOR rt IN SELECT * FROM mt.root
LOOP
UPDATE mt.root SET deposit_cnt = (SELECT deposit_cnt FROM sync.deposit WHERE id = rt.deposit_id) WHERE deposit_id = rt.deposit_id;
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- +migrate StatementEnd
63 changes: 63 additions & 0 deletions db/pgstorage/migrations/0007_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package migrations_test

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"
)

type migrationTest0007 struct{}

func (m migrationTest0007) InsertData(db *sql.DB) error {
block := "INSERT INTO sync.block (id, block_num, block_hash, parent_hash, network_id, received_at) VALUES(2, 2803824, decode('27474F16174BBE50C294FE13C190B92E42B2368A6D4AEB8A4A015F52816296C3','hex'), decode('C9B5033799ADF3739383A0489EFBE8A0D4D5E4478778A4F4304562FD51AE4C07','hex'), 1, '0001-01-01 01:00:00.000');"
if _, err := db.Exec(block); err != nil {
return err
}
insertDeposit := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim) VALUES(0, 0, 0, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 1, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 0, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 1, true);"
if _, err := db.Exec(insertDeposit); err != nil {
return err
}
insertDeposit2 := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim) VALUES(0, 0, 0, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 1, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 1, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 2, true);"
if _, err := db.Exec(insertDeposit2); err != nil {
return err
}
insertDeposit3 := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim) VALUES(0, 0, 0, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 1, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 2, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 3, true);"
if _, err := db.Exec(insertDeposit3); err != nil {
return err
}
insertRoot := "INSERT INTO mt.root (root, deposit_cnt, network, deposit_id) VALUES(decode('16C571C7A60CF3694BA81AFF143E8A8C9A393D351213DBFD4D539F39F1C4648E','hex'), 0, 0, 1);"
if _, err := db.Exec(insertRoot); err != nil {
return err
}
insertRoot2 := "INSERT INTO mt.root (root, deposit_cnt, network, deposit_id) VALUES(decode('16C571C7A60CF3694BA81AFF143E8A8C9A393D351213DBFD4D539F39F1C4648D','hex'), 1, 0, 2);"
if _, err := db.Exec(insertRoot2); err != nil {
return err
}
insertRoot3 := "INSERT INTO mt.root (root, deposit_cnt, network, deposit_id) VALUES(decode('16C571C7A60CF3694BA81AFF143E8A8C9A393D351213DBFD4D539F39F1C4648C','hex'), 2, 0, 3);"
if _, err := db.Exec(insertRoot3); err != nil {
return err
}
return nil
}

func (m migrationTest0007) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
queryDepositCount := "select deposit_cnt from mt.root where deposit_id = 0;"
row := db.QueryRow(queryDepositCount)
var depositCnt int
assert.Error(t, row.Scan(&depositCnt))
}

func (m migrationTest0007) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
for i := 0; i < 3; i++ {
queryDepositCount := "select deposit_cnt from mt.root where deposit_id = $1;"
row := db.QueryRow(queryDepositCount, i+1)
var depositCnt int
assert.NoError(t, row.Scan(&depositCnt))
assert.Equal(t, i, depositCnt)
}
}

func TestMigration0007(t *testing.T) {
runMigrationTest(t, 7, migrationTest0007{})
}
16 changes: 8 additions & 8 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 All @@ -322,7 +322,7 @@ func (p *PostgresStorage) GetDepositCountByRoot(ctx context.Context, root []byte
// GetRoot gets root by the deposit count from the merkle tree.
func (p *PostgresStorage) GetRoot(ctx context.Context, depositCnt uint, network uint, dbTx pgx.Tx) ([]byte, error) {
var root []byte
const getRootByDepositCntSQL = "SELECT root FROM mt.root WHERE deposit_cnt = $1 AND network = $2"
const getRootByDepositCntSQL = "SELECT root FROM mt.root inner join sync.deposit on mt.root.deposit_id = sync.deposit.id WHERE sync.deposit.deposit_cnt = $1 AND network = $2"
err := p.getExecQuerier(dbTx).QueryRow(ctx, getRootByDepositCntSQL, depositCnt, network).Scan(&root)
if errors.Is(err, pgx.ErrNoRows) {
return nil, gerror.ErrStorageNotFound
Expand All @@ -331,9 +331,9 @@ func (p *PostgresStorage) GetRoot(ctx context.Context, depositCnt uint, network
}

// SetRoot store the root with deposit count to the storage.
func (p *PostgresStorage) SetRoot(ctx context.Context, root []byte, depositID uint64, depositCnt uint, network uint, dbTx pgx.Tx) error {
const setRootSQL = "INSERT INTO mt.root (root, deposit_id, deposit_cnt, network) VALUES ($1, $2, $3, $4);"
_, err := p.getExecQuerier(dbTx).Exec(ctx, setRootSQL, root, depositID, depositCnt, network)
func (p *PostgresStorage) SetRoot(ctx context.Context, root []byte, depositID uint64, network uint, dbTx pgx.Tx) error {
const setRootSQL = "INSERT INTO mt.root (root, deposit_id, network) VALUES ($1, $2, $3);"
_, err := p.getExecQuerier(dbTx).Exec(ctx, setRootSQL, root, depositID, network)
return err
}

Expand Down Expand Up @@ -366,7 +366,7 @@ func (p *PostgresStorage) BulkSet(ctx context.Context, rows [][]interface{}, dbT
// GetLastDepositCount gets the last deposit count from the merkle tree.
func (p *PostgresStorage) GetLastDepositCount(ctx context.Context, network uint, dbTx pgx.Tx) (uint, error) {
var depositCnt int64
const getLastDepositCountSQL = "SELECT coalesce(MAX(deposit_cnt), -1) FROM mt.root WHERE network = $1"
const getLastDepositCountSQL = "SELECT coalesce(MAX(deposit_cnt), -1) FROM sync.deposit WHERE id = (SELECT coalesce(MAX(deposit_id), -1) FROM mt.root WHERE network = $1)"
err := p.getExecQuerier(dbTx).QueryRow(ctx, getLastDepositCountSQL, network).Scan(&depositCnt)
if err != nil {
return 0, nil
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
14 changes: 7 additions & 7 deletions db/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,28 +229,28 @@ func TestMTStorage(t *testing.T) {
}
depositID, err := pg.AddDeposit(ctx, deposit, tx)
require.NoError(t, err)
err = pg.SetRoot(ctx, root, depositID, 1, 0, tx)
err = pg.SetRoot(ctx, root, depositID, 0, tx)
require.NoError(t, err)

err = pg.Set(ctx, root, [][]byte{leaf1, leaf2}, depositID, tx)
require.NoError(t, err)

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 325fe34

Please sign in to comment.