Skip to content

Commit

Permalink
Add state db migrations to store blobInner information (0xPolygonHerm…
Browse files Browse the repository at this point in the history
…ez#3438)

* add db migrations to store blobInner information

* add check for blob_inner table in migration test file
  • Loading branch information
agnusmor authored Mar 8, 2024
1 parent af1d205 commit 567d1df
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
19 changes: 19 additions & 0 deletions db/migrations/state/0017.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- +migrate Up
CREATE TABLE state.blob_inner
(
blob_inner_num BIGINT PRIMARY KEY,
data BYTEA,
block_num BIGINT NOT NULL REFERENCES state.block (block_num) ON DELETE CASCADE
);
ALTER TABLE state.virtual_batch
ADD COLUMN IF NOT EXISTS blob_inner_num BIGINT REFERENCES state.blob_inner(blob_inner_num),
ADD COLUMN IF NOT EXISTS prev_l1_it_root VARCHAR,
ADD COLUMN IF NOT EXISTS prev_l1_it_index BIGINT;

-- +migrate Down
ALTER TABLE state.virtual_batch
DROP COLUMN IF EXISTS blob_inner_num,
DROP COLUMN IF EXISTS prev_l1_it_root,
DROP COLUMN IF EXISTS prev_l1_it_index;

DROP TABLE state.blob_inner;
125 changes: 125 additions & 0 deletions db/migrations/state/0017_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package migrations_test

import (
"database/sql"
"testing"

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

type migrationTest0017 struct{}

func (m migrationTest0017) InsertData(db *sql.DB) error {
const insertBatch1 = `
INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num, wip)
VALUES (1,'0x0001', '0x0001', '0x0001', '0x0001', now(), '0x0001', null, null, true)`

_, err := db.Exec(insertBatch1)
if err != nil {
return err
}

const insertBatch2 = `
INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num, wip)
VALUES (2,'0x0002', '0x0002', '0x0002', '0x0002', now(), '0x0002', null, null, true)`

_, err = db.Exec(insertBatch2)
if err != nil {
return err
}

const insertBlock1 = "INSERT INTO state.block (block_num, block_hash, parent_hash, received_at) VALUES (1,'0x0001', '0x0001', now())"

_, err = db.Exec(insertBlock1)
if err != nil {
return err
}

const insertBlock2 = "INSERT INTO state.block (block_num, block_hash, parent_hash, received_at) VALUES (2,'0x0002', '0x0002', now())"

_, err = db.Exec(insertBlock2)
if err != nil {
return err
}

return nil
}

func (m migrationTest0017) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
var result int

// Check table state.blob_inner exists
const getBlobInnerTable = `SELECT count(*) FROM information_schema.tables WHERE table_schema='state' and table_name='blob_inner'`
row := db.QueryRow(getBlobInnerTable)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)

// Check column blob_inner_num exists in state.virtual_batch table
const getBlobInnerNumColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='virtual_batch' and column_name='blob_inner_num'`
row = db.QueryRow(getBlobInnerNumColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)

// Check column prev_l1_it_root exists in state.virtual_batch table
const getPrevL1ITRootColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='virtual_batch' and column_name='prev_l1_it_root'`
row = db.QueryRow(getPrevL1ITRootColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)

// Check column prev_l1_it_index exists in state.virtual_batch table
const getPrevL1ITIndexColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='virtual_batch' and column_name='prev_l1_it_index'`
row = db.QueryRow(getPrevL1ITIndexColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)

// Insert blobInner 1
const insertBlobInner = `INSERT INTO state.blob_inner (blob_inner_num, data, block_num) VALUES (1, E'\\x1234', 1);`
_, err := db.Exec(insertBlobInner)
assert.NoError(t, err)

const insertBatch1 = `
INSERT INTO state.virtual_batch (batch_num, tx_hash, coinbase, block_num, sequencer_addr, timestamp_batch_etrog, l1_info_root, blob_inner_num, prev_l1_it_root, prev_l1_it_index)
VALUES (1,'0x0001', '0x0001', 1, '0x0001', now(), '0x0001', 1, '0x0001', 1)`

_, err = db.Exec(insertBatch1)
assert.NoError(t, err)

const insertBatch2 = `
INSERT INTO state.virtual_batch (batch_num, tx_hash, coinbase, block_num, sequencer_addr, timestamp_batch_etrog, l1_info_root, blob_inner_num, prev_l1_it_root, prev_l1_it_index)
VALUES (2,'0x0002', '0x0002', 2, '0x0002', now(), '0x0002', 1, '0x0002', 2)`

_, err = db.Exec(insertBatch2)
assert.NoError(t, err)
}

func (m migrationTest0017) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
var result int

// Check table state.blob_inner doesn't exists
const getBlobInnerTable = `SELECT count(*) FROM information_schema.tables WHERE table_schema='state' and table_name='blob_inner'`
row := db.QueryRow(getBlobInnerTable)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)

// Check column blob_inner_num doesn't exists in state.virtual_batch table
const getBlobInnerNumColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='virtual_batch' and column_name='blob_inner_num'`
row = db.QueryRow(getBlobInnerNumColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)

// Check column prev_l1_it_root doesn't exists in state.virtual_batch table
const getPrevL1ITRootColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='virtual_batch' and column_name='prev_l1_it_root'`
row = db.QueryRow(getPrevL1ITRootColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)

// Check column prev_l1_it_index doesn't exists in state.virtual_batch table
const getPrevL1ITIndexColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='virtual_batch' and column_name='prev_l1_it_index'`
row = db.QueryRow(getPrevL1ITIndexColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)
}

func TestMigration0017(t *testing.T) {
runMigrationTest(t, 17, migrationTest0017{})
}

0 comments on commit 567d1df

Please sign in to comment.