Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/migracion #368

Merged
merged 3 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ stop-mockserver: ## Stops the mock bridge service
bench: ## benchmark test
$(STOP_BRIDGE_DB) || true
$(RUN_BRIDGE_DB); sleep 3
trap '$(STOP_BRIDGE_DB)' EXIT; go test -run=NOTEST -bench=Small ./test/benchmark/...
trap '$(STOP_BRIDGE_DB)' EXIT; go test -run=NOTEST -timeout=30m -bench=Small ./test/benchmark/...

.PHONY: bench-full
bench-full: export ZKEVM_BRIDGE_DATABASE_PORT = 5432
Expand Down
30 changes: 18 additions & 12 deletions db/pgstorage/migrations/0002.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
-- +migrate Down

ALTER TABLE mt.rht DROP COLUMN IF EXISTS deposit_id;
ALTER TABLE mt.root DROP COLUMN IF EXISTS deposit_id;
ALTER TABLE sync.deposit DROP COLUMN IF EXISTS id;
ALTER TABLE mt.rht DROP COLUMN IF EXISTS root_id;
ALTER TABLE mt.root DROP COLUMN IF EXISTS id;

ALTER TABLE sync.deposit ADD CONSTRAINT deposit_pkey PRIMARY KEY (network_id, deposit_cnt);
ALTER TABLE mt.root DROP CONSTRAINT IF EXISTS root_pkey;
ALTER TABLE mt.rht DROP CONSTRAINT IF EXISTS rht_pkey;

ALTER TABLE mt.root ADD CONSTRAINT root_pkey PRIMARY KEY (deposit_cnt, network);
ALTER TABLE mt.rht ADD CONSTRAINT rht_pkey PRIMARY KEY (key);

DROP INDEX IF EXISTS mt.root_network_idx;
DROP INDEX IF EXISTS mt.deposit_idx;
Expand All @@ -22,15 +25,15 @@ DROP SCHEMA IF EXISTS mt CASCADE;
ALTER SCHEMA mtv2 RENAME TO mt;
ALTER SCHEMA syncv2 RENAME TO sync;

ALTER TABLE sync.deposit DROP CONSTRAINT IF EXISTS deposit_pkey;
ALTER TABLE sync.deposit ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE mt.root DROP CONSTRAINT IF EXISTS root_pkey;
ALTER TABLE mt.rht DROP CONSTRAINT IF EXISTS rht_pkey;

ALTER TABLE mt.root ADD COLUMN deposit_id BIGINT NOT NULL DEFAULT 1 CONSTRAINT root_deposit_id_fkey REFERENCES sync.deposit (id) ON DELETE CASCADE;
ALTER TABLE mt.root ALTER COLUMN deposit_id DROP DEFAULT;
UPDATE mt.root AS r SET deposit_id = d.id FROM sync.deposit AS d WHERE d.deposit_cnt = r.deposit_cnt AND d.network_id = r.network;
ALTER TABLE mt.rht DROP COLUMN IF EXISTS root_id;
ALTER TABLE mt.root DROP COLUMN IF EXISTS id;

ALTER TABLE mt.rht ADD COLUMN deposit_id BIGINT NOT NULL DEFAULT 1 CONSTRAINT rht_deposit_id_fkey REFERENCES sync.deposit (id) ON DELETE CASCADE;
ALTER TABLE mt.rht ALTER COLUMN deposit_id DROP DEFAULT;
ALTER TABLE mt.root ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE mt.rht ADD COLUMN root_id BIGINT NOT NULL DEFAULT 1 CONSTRAINT rht_root_id_fkey REFERENCES mt.root (id) ON DELETE CASCADE;
ALTER TABLE mt.rht ALTER COLUMN root_id DROP DEFAULT;

-- +migrate StatementBegin
DO $$
Expand All @@ -41,10 +44,13 @@ DECLARE
BEGIN
FOR rt IN SELECT * FROM mt.root
LOOP
IF rt.deposit_cnt > 0 THEN
rt.deposit_cnt = rt.deposit_cnt - 1;
END IF;
pkey = rt.root;
FOR i IN reverse 31..0
LOOP
UPDATE mt.rht SET deposit_id = rt.deposit_id WHERE key = pkey RETURNING value INTO pvalue;
UPDATE mt.rht SET root_id = rt.id WHERE key = pkey RETURNING value INTO pvalue;

IF rt.deposit_cnt & (1 << i) > 0 THEN
pkey = pvalue[2];
Expand Down
33 changes: 7 additions & 26 deletions db/pgstorage/migrations/0002_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package migrations_test
import (
"database/sql"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
Expand All @@ -28,19 +27,7 @@ func (m migrationTest0002) InsertData(db *sql.DB) error {
}); err != nil {
return err
}
// Insert initial block, deposit and root
if _, err := db.Exec("INSERT INTO syncv2.block (block_num, block_hash, parent_hash, network_id, received_at) VALUES ($1, $2, $3, $4, $5)", 1, common.FromHex("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"),
common.FromHex("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"), 0,
time.Now()); err != nil {
return err
}
if _, err := db.Exec("INSERT INTO syncv2.deposit (leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
0, 0, 0, common.FromHex("0x0000000000000000000000000000000000000000"),
"1000000", 1, common.FromHex("0x6B175474E89094C44Da98b954EedeAC495271d0F"), 1, 0,
common.FromHex("0xa4bfa0908dc7b06d98da4309f859023d6947561bc19bc00d77f763dea1a0b9f5"),
[]byte{}); err != nil {
return err
}
// Insert initial root
if _, err := db.Exec("INSERT INTO mtv2.root (root, deposit_cnt, network) VALUES ($1, $2, $3)",
common.FromHex("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"), 0, 0); err != nil {
return err
Expand All @@ -51,24 +38,18 @@ func (m migrationTest0002) InsertData(db *sql.DB) error {
var indexes = []string{"root_network_idx", "deposit_idx", "block_idx", "root_idx", "exit_roots_idx"}

func (m migrationTest0002) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Insert a new Deposit
var depositID uint64
err := db.QueryRow("INSERT INTO sync.deposit (leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING id",
0, 0, 0, common.FromHex("0x0000000000000000000000000000000000000000"),
"1000000", 1, common.FromHex("0x6B175474E89094C44Da98b954EedeAC495271d0F"), 1, 1,
common.FromHex("0xa4bfa0908dc7b06d98da4309f859023d6947561bc19bc00d77f763dea1a0b9f5"),
[]byte{}).Scan(&depositID)
assert.NoError(t, err)
// Insert a new root
const addRoot = "INSERT INTO mt.root (root, deposit_cnt, network, deposit_id) VALUES ($1, $2, $3, $4)"
_, err = db.Exec(addRoot, common.FromHex("0x5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0"), 1, 0, depositID)
const addRoot = "INSERT INTO mt.root (root, deposit_cnt, network) VALUES ($1, $2, $3) RETURNING id"
var rootID int
err := db.QueryRow(addRoot, common.FromHex("0x5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0"), 1, 0).Scan(&rootID)
assert.NoError(t, err)

// Insert a new node to the rht table
const addNode = "INSERT INTO mt.rht (key, value, deposit_id) VALUES ($1, $2, $3)"
const addNode = "INSERT INTO mt.rht (key, value, root_id) VALUES ($1, $2, $3)"
_, err = db.Exec(addNode, common.FromHex("0x5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0"), [][]byte{
common.FromHex("0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30"),
common.FromHex("0x21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85"),
}, depositID)
}, rootID)
assert.NoError(t, err)

// Check indexes adding
Expand Down
87 changes: 87 additions & 0 deletions db/pgstorage/migrations/0003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
-- +migrate Down

ALTER TABLE mt.rht DROP COLUMN IF EXISTS deposit_id;
ALTER TABLE mt.root DROP COLUMN IF EXISTS deposit_id;
ALTER TABLE sync.deposit DROP COLUMN IF EXISTS id;

ALTER TABLE sync.deposit ADD CONSTRAINT deposit_pkey PRIMARY KEY (network_id, deposit_cnt);

DROP INDEX IF EXISTS mt.rht_key_idx;

ALTER TABLE mt.root ADD COLUMN id SERIAL PRIMARY KEY;
ALTER TABLE mt.rht ADD COLUMN root_id BIGINT NOT NULL DEFAULT 1 CONSTRAINT rht_root_id_fkey REFERENCES mt.root (id) ON DELETE CASCADE;
ALTER TABLE mt.rht ALTER COLUMN root_id DROP DEFAULT;

-- +migrate StatementBegin
DO $$
DECLARE
rt RECORD;
pkey BYTEA;
pvalue BYTEA [];
BEGIN
FOR rt IN SELECT * FROM mt.root
LOOP
pkey = rt.root;
FOR i IN reverse 31..0
LOOP
UPDATE mt.rht SET root_id = rt.id WHERE key = pkey RETURNING value INTO pvalue;

IF rt.deposit_cnt & (1 << i) > 0 THEN
pkey = pvalue[2];
ELSE
pkey = pvalue[1];
END IF;
END LOOP;
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- +migrate StatementEnd

UPDATE mt.root SET deposit_cnt = deposit_cnt + 1;

-- +migrate Up

ALTER TABLE mt.rht DROP COLUMN IF EXISTS root_id;
ALTER TABLE mt.root DROP COLUMN IF EXISTS id;

ALTER TABLE sync.deposit DROP CONSTRAINT IF EXISTS deposit_pkey;
ALTER TABLE sync.deposit ADD COLUMN id SERIAL PRIMARY KEY;

ALTER TABLE mt.root ADD COLUMN deposit_id BIGINT NOT NULL DEFAULT 1 CONSTRAINT root_deposit_id_fkey REFERENCES sync.deposit (id) ON DELETE CASCADE;
ALTER TABLE mt.root ALTER COLUMN deposit_id DROP DEFAULT;
UPDATE mt.root AS r SET deposit_id = d.id FROM sync.deposit AS d WHERE d.deposit_cnt = r.deposit_cnt AND d.network_id = r.network;

ALTER TABLE mt.rht ADD COLUMN deposit_id BIGINT NOT NULL DEFAULT 1 CONSTRAINT rht_deposit_id_fkey REFERENCES sync.deposit (id) ON DELETE CASCADE;
ALTER TABLE mt.rht ALTER COLUMN deposit_id DROP DEFAULT;

UPDATE mt.root SET deposit_cnt = deposit_cnt - 1;

DELETE FROM mt.rht a
WHERE a.ctid <> (SELECT min(b.ctid)
FROM mt.rht b
WHERE a.key = b.key);

-- +migrate StatementBegin
DO $$
DECLARE
rt RECORD;
pkey BYTEA;
pvalue BYTEA [];
BEGIN
FOR rt IN SELECT * FROM mt.root
LOOP
pkey = rt.root;
FOR i IN reverse 31..0
LOOP
UPDATE mt.rht SET deposit_id = rt.deposit_id WHERE key = pkey RETURNING value INTO pvalue;

IF rt.deposit_cnt & (1 << i) > 0 THEN
pkey = pvalue[2];
ELSE
pkey = pvalue[1];
END IF;
END LOOP;
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- +migrate StatementEnd
101 changes: 101 additions & 0 deletions db/pgstorage/migrations/0003_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package migrations_test

import (
"database/sql"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)

// This migration creates a different proof table dropping all the information.

type migrationTest0003 struct{}

func (m migrationTest0003) InsertData(db *sql.DB) error {
// Insert initial block, deposit and root
if _, err := db.Exec("INSERT INTO sync.block (block_num, block_hash, parent_hash, network_id, received_at) VALUES ($1, $2, $3, $4, $5)", 1, common.FromHex("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"),
common.FromHex("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"), 0,
time.Now()); err != nil {
return err
}
if _, err := db.Exec("INSERT INTO sync.deposit (leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
0, 0, 0, common.FromHex("0x0000000000000000000000000000000000000000"),
"1000000", 1, common.FromHex("0x6B175474E89094C44Da98b954EedeAC495271d0F"), 1, 0,
common.FromHex("0xa4bfa0908dc7b06d98da4309f859023d6947561bc19bc00d77f763dea1a0b9f5"),
[]byte{}); err != nil {
return err
}
if _, err := db.Exec("INSERT INTO mt.root (root, deposit_cnt, network) VALUES ($1, $2, $3)",
common.FromHex("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"), 1, 0); err != nil {
return err
}

// Insert initial nodes to rht table
const addNode = "INSERT INTO mt.rht (key, value, root_id) VALUES ($1, $2, $3)"
if _, err := db.Exec(addNode, common.FromHex("0xad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5"), [][]byte{
common.FromHex("0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30"),
common.FromHex("0x21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85"),
}, 1); err != nil {
return err
}
if _, err := db.Exec(addNode, common.FromHex("0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30"), [][]byte{
common.FromHex("0xe58769b32a1beaf1ea27375a44095a0d1fb664ce2dd358e7fcbfb78c26a19344"),
common.FromHex("0x0eb01ebfc9ed27500cd4dfc979272d1f0913cc9f66540d7e8005811109e1cf2d"),
}, 1); err != nil {
return err
}
return nil
}

func (m migrationTest0003) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Insert a new Deposit
var depositID uint64
err := db.QueryRow("INSERT INTO sync.deposit (leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING id",
0, 0, 0, common.FromHex("0x0000000000000000000000000000000000000000"),
"1000000", 1, common.FromHex("0x6B175474E89094C44Da98b954EedeAC495271d0F"), 1, 1,
common.FromHex("0xa4bfa0908dc7b06d98da4309f859023d6947561bc19bc00d77f763dea1a0b9f5"),
[]byte{}).Scan(&depositID)
assert.NoError(t, err)
// Insert a new root
const addRoot = "INSERT INTO mt.root (root, deposit_cnt, network, deposit_id) VALUES ($1, $2, $3, $4)"
_, err = db.Exec(addRoot, common.FromHex("0x5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0"), 1, 0, depositID)
assert.NoError(t, err)
// Insert a new node to the rht table
const addNode = "INSERT INTO mt.rht (key, value, deposit_id) VALUES ($1, $2, $3)"
_, err = db.Exec(addNode, common.FromHex("0x5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0"), [][]byte{
common.FromHex("0xb4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30"),
common.FromHex("0x21ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85"),
}, depositID)
assert.NoError(t, err)

// Insert a new Deposit
_, err = db.Exec("INSERT INTO sync.deposit (leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
0, 0, 0, common.FromHex("0x0000000000000000000000000000000000000000"),
"1000000", 1, common.FromHex("0x6B175474E89094C44Da98b954EedeAC495271d0F"), 1, 2,
common.FromHex("0xa4bfa0908dc7b06d98da4309f859023d6947561bc19bc00d77f763dea1a0b9f5"),
[]byte{})
assert.NoError(t, err)
// Check the root deposit count
const getRoot = "SELECT MAX(deposit_cnt), COUNT(*) FROM mt.root"
var maxDepositCnt, rootCount int
err = db.QueryRow(getRoot).Scan(&maxDepositCnt, &rootCount)
assert.NoError(t, err)
assert.Equal(t, maxDepositCnt, 1)
assert.Equal(t, rootCount, 2)
}

func (m migrationTest0003) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
// Check the root deposit count
const getRoot = "SELECT MAX(deposit_cnt), COUNT(*) FROM mt.root"
var maxDepositCnt, rootCount int
err := db.QueryRow(getRoot).Scan(&maxDepositCnt, &rootCount)
assert.NoError(t, err)
assert.Equal(t, maxDepositCnt, 2)
assert.Equal(t, rootCount, 2)
}

func TestMigration0003(t *testing.T) {
runMigrationTest(t, 3, migrationTest0003{})
}