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

Feature/#1579 migrations index #1612

Merged
merged 6 commits into from
Feb 1, 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 cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func startMetricsHttpServer(c *config.Config) {
return
}
mux.Handle(metrics.Endpoint, promhttp.Handler())
metricsServer := &http.Server{
metricsServer := &http.Server{ //nolint Potential Slowloris Attack
Handler: mux,
}
log.Infof("metrics server listening on port %d", c.Metrics.Port)
Expand Down
69 changes: 69 additions & 0 deletions db/migrations/state/0002_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package migrations_test

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

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

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

type migrationTest0002 struct{}

func (m migrationTest0002) InsertData(db *sql.DB) error {
// Insert block to respect the FKey
const addBlock = "INSERT INTO state.block (block_num, received_at, block_hash) VALUES ($1, $2, $3)"
if _, err := db.Exec(addBlock, 1, time.Now(), "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1"); err != nil {
return err
}
// Insert batch to respect the FKey
_, err := db.Exec("INSERT INTO state.batch (batch_num) VALUES (1)")
if err != nil {
return err
}
// Insert old proof
const insertProof = `INSERT INTO state.proof (
batch_num, proof, proof_id, input_prover, prover
) VALUES (
1,'{}','proof_identifier','{}','prover 1'
);`
_, err = db.Exec(insertProof)
return err
}

func (m migrationTest0002) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Insert new proof
const insertProof = `INSERT INTO state.proof (
batch_num, batch_num_final, proof, proof_id, input_prover, prover, generating
) VALUES (
1, 1, '{}','proof_identifier','{}','prover 1', true
);`
_, err := db.Exec(insertProof)
assert.NoError(t, err)
}

func (m migrationTest0002) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
// Insert new proof
const insertNewProof = `INSERT INTO state.proof (
batch_num, batch_num_final, proof, proof_id, input_prover, prover, generating
) VALUES (
1, 1, '{}','proof_identifier','{}','prover 1', true
);`
_, err := db.Exec(insertNewProof)
assert.Error(t, err)

// Insert old proof
const insertProof = `INSERT INTO state.proof (
batch_num, proof, proof_id, input_prover, prover
) VALUES (
1,'{}','proof_identifier','{}','prover 1'
);`
_, err = db.Exec(insertProof)
assert.NoError(t, err)
}

func TestMigration0002(t *testing.T) {
runMigrationTest(t, 2, migrationTest0002{})
}
33 changes: 33 additions & 0 deletions db/migrations/state/0003_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package migrations_test

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

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

// This migration adds the column `eth_tx_hash` on `batch`

type migrationTest0003 struct{}

func (m migrationTest0003) InsertData(db *sql.DB) error {
return nil
}

func (m migrationTest0003) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
const insertDebug = `INSERT INTO state.debug (error_type, timestamp, payload) VALUES ('error type', $1, 'payload stored')`
_, err := db.Exec(insertDebug, time.Now())
assert.NoError(t, err)
}

func (m migrationTest0003) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
const insertDebug = `INSERT INTO state.debug (error_type, timestamp, payload) VALUES ('error type', $1, 'payload stored')`
_, err := db.Exec(insertDebug, time.Now())
assert.Error(t, err)
}

func TestMigration0003(t *testing.T) {
runMigrationTest(t, 3, migrationTest0003{})
}
71 changes: 71 additions & 0 deletions db/migrations/state/0004_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package migrations_test

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

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

// This migration creates the fiat table

type migrationTest0004 struct{}

func (m migrationTest0004) InsertData(db *sql.DB) error {
// Insert block to respect the FKey
const addBlock = "INSERT INTO state.block (block_num, received_at, block_hash) VALUES ($1, $2, $3)"
if _, err := db.Exec(addBlock, 1, time.Now(), "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1"); err != nil {
return err
}
const addForcedBatch = "INSERT INTO state.forced_batch (forced_batch_num, global_exit_root, raw_txs_data, coinbase, timestamp, block_num, batch_num) VALUES ($1, $2, $3, $4, $5, $6, $7)"
if _, err := db.Exec(addForcedBatch, 1, "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1", "", "0x2536C2745Ac4A584656A830f7bdCd329c94e8F30", time.Now(), 1, 1); err != nil {
return err
}
// Insert batch
_, err := db.Exec(`INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, state_root, acc_input_hash, timestamp, coinbase, raw_txs_data)
VALUES (1, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
'0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
$1, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $2)`, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1"))
if err != nil {
return err
}
return nil
}

func (m migrationTest0004) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Insert batch
_, err := db.Exec(`INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, state_root, acc_input_hash, timestamp, coinbase, raw_txs_data, forced_batch_num)
VALUES (2, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
'0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
$1, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $2, 1)`, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1"))
assert.NoError(t, err)
addForcedBatch := "INSERT INTO state.forced_batch (forced_batch_num, global_exit_root, raw_txs_data, coinbase, timestamp, block_num, batch_num) VALUES ($1, $2, $3, $4, $5, $6, $7)"
_, err = db.Exec(addForcedBatch, 1, "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1", "", "0x2536C2745Ac4A584656A830f7bdCd329c94e8F30", time.Now(), 1, 2)
assert.Error(t, err)
addForcedBatch = "INSERT INTO state.forced_batch (forced_batch_num, global_exit_root, raw_txs_data, coinbase, timestamp, block_num) VALUES ($1, $2, $3, $4, $5, $6)"
_, err = db.Exec(addForcedBatch, 2, "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1", "", "0x2536C2745Ac4A584656A830f7bdCd329c94e8F30", time.Now(), 1)
assert.NoError(t, err)
}

func (m migrationTest0004) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
const addForcedBatch = "INSERT INTO state.forced_batch (forced_batch_num, global_exit_root, raw_txs_data, coinbase, timestamp, block_num, batch_num) VALUES ($1, $2, $3, $4, $5, $6, $7)"
_, err := db.Exec(addForcedBatch, 3, "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1", "", "0x2536C2745Ac4A584656A830f7bdCd329c94e8F30", time.Now(), 1, 1)
assert.NoError(t, err)
// Insert batch
_, err = db.Exec(`INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, state_root, acc_input_hash, timestamp, coinbase, raw_txs_data, forced_batch_num)
VALUES (3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
'0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
$1, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $2, 1)`, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1"))
assert.Error(t, err)
_, err = db.Exec(`INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, state_root, acc_input_hash, timestamp, coinbase, raw_txs_data)
VALUES (3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
'0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
$1, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $2)`, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1"))
assert.NoError(t, err)
}

func TestMigration0004(t *testing.T) {
runMigrationTest(t, 4, migrationTest0004{})
}
2 changes: 1 addition & 1 deletion db/migrations/state/0005.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ CREATE TABLE state.monitored_txs
);

ALTER TABLE state.verified_batch
ADD COLUMN is_trusted BOOLEAN DEFAULT true;
ADD COLUMN is_trusted BOOLEAN DEFAULT true;
125 changes: 125 additions & 0 deletions db/migrations/state/0005_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package migrations_test

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

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

// this migration changes length of the token name
type migrationTest0005 struct{}

func (m migrationTest0005) InsertData(db *sql.DB) error {
// Insert block to respect the FKey
const addBlock = "INSERT INTO state.block (block_num, received_at, block_hash) VALUES ($1, $2, $3)"
if _, err := db.Exec(addBlock, 1, time.Now(), "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1"); err != nil {
return err
}
// Insert batch
_, err := db.Exec(`INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, state_root, acc_input_hash, timestamp, coinbase, raw_txs_data)
VALUES (1, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
'0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
$1, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $2)`, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1"))
if err != nil {
return err
}
// Insert virtual batch
const insertVirtualBatch = `INSERT INTO state.virtual_batch (
batch_num, tx_hash, coinbase, block_num
) VALUES (
1, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', 1);`
_, err = db.Exec(insertVirtualBatch)
if err != nil {
return err
}
// Insert verified batch
const insertVerifiedBatch = `INSERT INTO state.verified_batch (
batch_num, tx_hash, aggregator, state_root, block_num
) VALUES (
1, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', 1
);`
_, err = db.Exec(insertVerifiedBatch)
return err
}

func (m migrationTest0005) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Insert batch
_, err := db.Exec(`INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, state_root, acc_input_hash, timestamp, coinbase, raw_txs_data)
VALUES (2, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
'0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
$1, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $2)`, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1"))
assert.NoError(t, err)
// Insert virtual batch
const insertVirtualBatch = `INSERT INTO state.virtual_batch (
batch_num, tx_hash, coinbase, block_num
) VALUES (
2, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', 1);`
_, err = db.Exec(insertVirtualBatch)
assert.NoError(t, err)

// Insert verified batch
const insertVerifiedBatch = `INSERT INTO state.verified_batch (
batch_num, tx_hash, aggregator, state_root, block_num, is_trusted
) VALUES (
2, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', 1, true
);`
_, err = db.Exec(insertVerifiedBatch)
assert.NoError(t, err)

// Insert monitored_txs
const insertMonitoredTxs = `INSERT INTO state.monitored_txs (
owner, id, from_addr, to_addr, nonce, value, data, gas, gas_price, status, block_num, created_at, updated_at
) VALUES (
'0x514910771af9ca656af840dff83e8264ecf986ca', '1', '0x514910771af9ca656af840dff83e8264ecf986ca', '0x514910771af9ca656af840dff83e8264ecf986ca', 1, 0, '0x', 100, 12, 'created', 1, $1, $2
);`
_, err = db.Exec(insertMonitoredTxs, time.Now(), time.Now())
assert.NoError(t, err)
}

func (m migrationTest0005) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
// Insert batch
_, err := db.Exec(`INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, state_root, acc_input_hash, timestamp, coinbase, raw_txs_data)
VALUES (3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
'0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1',
$1, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $2)`, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1"))
assert.NoError(t, err)
// Insert virtual batch
const insertVirtualBatch = `INSERT INTO state.virtual_batch (
batch_num, tx_hash, coinbase, block_num
) VALUES (
3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', 1);`
_, err = db.Exec(insertVirtualBatch)
assert.NoError(t, err)

// Insert verified batch
insertVerifiedBatch := `INSERT INTO state.verified_batch (
batch_num, tx_hash, aggregator, state_root, block_num, is_trusted
) VALUES (
3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', 1, true
);`
_, err = db.Exec(insertVerifiedBatch)
assert.Error(t, err)
insertVerifiedBatch = `INSERT INTO state.verified_batch (
batch_num, tx_hash, aggregator, state_root, block_num
) VALUES (
3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', 1
);`
_, err = db.Exec(insertVerifiedBatch)
assert.NoError(t, err)

// Insert monitored_txs
const insertMonitoredTxs = `INSERT INTO state.monitored_txs (
owner, id, from_addr, to_addr, nonce, value, data, gas, gas_price, status, block_num, created_at, updated_at
) VALUES (
'0x514910771af9ca656af840dff83e8264ecf986ca', '1', '0x514910771af9ca656af840dff83e8264ecf986ca', '0x514910771af9ca656af840dff83e8264ecf986ca', 1, 0, '0x', 100, 12, 'created', 1, $1, $2
);`
_, err = db.Exec(insertMonitoredTxs, time.Now(), time.Now())
assert.Error(t, err)
}

func TestMigration0005(t *testing.T) {
runMigrationTest(t, 5, migrationTest0005{})
}
15 changes: 15 additions & 0 deletions db/migrations/state/0006.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +migrate Up
CREATE INDEX IF NOT EXISTS transaction_l2_block_num_idx ON state.transaction (l2_block_num);
CREATE INDEX IF NOT EXISTS l2block_batch_num_idx ON state.l2block (batch_num);
CREATE INDEX IF NOT EXISTS l2block_received_at_idx ON state.l2block (received_at);
CREATE INDEX IF NOT EXISTS batch_timestamp_idx ON state.batch ("timestamp");
CREATE INDEX IF NOT EXISTS log_tx_hash_idx ON state.log (tx_hash);
CREATE INDEX IF NOT EXISTS log_address_idx ON state.log (address);

-- +migrate Down
DROP INDEX IF EXISTS state.transaction_l2_block_num_idx;
DROP INDEX IF EXISTS state.l2block_batch_num_idx;
DROP INDEX IF EXISTS state.l2block_received_at_idx;
DROP INDEX IF EXISTS state.batch_timestamp_idx;
DROP INDEX IF EXISTS state.log_tx_hash_idx;
DROP INDEX IF EXISTS state.log_address_idx;
44 changes: 44 additions & 0 deletions db/migrations/state/0006_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package migrations_test

import (
"database/sql"
"testing"

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

// this migration changes length of the token name
type migrationTest0006 struct{}

func (m migrationTest0006) InsertData(db *sql.DB) error {
return nil
}

var indexes = []string{"transaction_l2_block_num_idx", "l2block_batch_num_idx", "l2block_received_at_idx",
"batch_timestamp_idx", "log_tx_hash_idx", "log_address_idx"}

func (m migrationTest0006) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
for _, idx := range indexes {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)
}
}

func (m migrationTest0006) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
for _, idx := range indexes {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)
}
}

func TestMigration0006(t *testing.T) {
runMigrationTest(t, 6, migrationTest0006{})
}
Loading