Skip to content

Commit

Permalink
migration + tests and queries (0xPolygonHermez#2625) (0xPolygonHermez…
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR552 authored Oct 6, 2023
1 parent 12327f9 commit 88cd7a3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
8 changes: 8 additions & 0 deletions db/migrations/state/0010.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
-- +migrate Up
CREATE INDEX IF NOT EXISTS l2block_block_hash_idx ON state.l2block (block_hash);

ALTER TABLE state.sequences ADD PRIMARY KEY(from_batch_num);
ALTER TABLE state.trusted_reorg ADD PRIMARY KEY(timestamp);
ALTER TABLE state.sync_info ADD PRIMARY KEY(last_batch_num_seen, last_batch_num_consolidated, init_sync_batch);

-- +migrate Down
DROP INDEX IF EXISTS state.l2block_block_hash_idx;

ALTER TABLE state.sequences DROP CONSTRAINT sequences_pkey;
ALTER TABLE state.trusted_reorg DROP CONSTRAINT trusted_reorg_pkey;
ALTER TABLE state.sync_info DROP CONSTRAINT sync_info_pkey;
22 changes: 22 additions & 0 deletions db/migrations/state/0010_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ func (m migrationTest0010) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)
}

constraints := []string{"sequences_pkey", "trusted_reorg_pkey", "sync_info_pkey"}
// Check constraint adding
for _, idx := range constraints {
// getConstraint
const getConstraint = ` SELECT count(*) FROM pg_constraint c WHERE c.conname = $1;`
row := db.QueryRow(getConstraint, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)
}
}

func (m migrationTest0010) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
Expand All @@ -38,6 +49,17 @@ func (m migrationTest0010) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)
}

constraints := []string{"sequences_pkey", "trusted_reorg_pkey", "sync_info_pkey"}
// Check constraint adding
for _, idx := range constraints {
// getConstraint
const getConstraint = ` SELECT count(*) FROM pg_constraint c WHERE c.conname = $1;`
row := db.QueryRow(getConstraint, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)
}
}

func TestMigration0010(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,7 @@ func (p *PostgresStorage) GetLatestVirtualBatchTimestamp(ctx context.Context, db
func (p *PostgresStorage) SetLastBatchInfoSeenOnEthereum(ctx context.Context, lastBatchNumberSeen, lastBatchNumberVerified uint64, dbTx pgx.Tx) error {
const query = `
UPDATE state.sync_info
SET last_batch_num_seen = $1
, last_batch_num_consolidated = $2`
SET last_batch_num_seen = $1, last_batch_num_consolidated = $2`

e := p.getExecQuerier(dbTx)
_, err := e.Exec(ctx, query, lastBatchNumberSeen, lastBatchNumberVerified)
Expand Down Expand Up @@ -2159,7 +2158,7 @@ func (p *PostgresStorage) GetExitRootByGlobalExitRoot(ctx context.Context, ger c

// AddSequence stores the sequence information to allow the aggregator verify sequences.
func (p *PostgresStorage) AddSequence(ctx context.Context, sequence Sequence, dbTx pgx.Tx) error {
const addSequenceSQL = "INSERT INTO state.sequences (from_batch_num, to_batch_num) VALUES($1, $2)"
const addSequenceSQL = "INSERT INTO state.sequences (from_batch_num, to_batch_num) VALUES($1, $2) ON CONFLICT (from_batch_num) DO UPDATE SET to_batch_num = $2"

e := p.getExecQuerier(dbTx)
_, err := e.Exec(ctx, addSequenceSQL, sequence.FromBatchNumber, sequence.ToBatchNumber)
Expand Down
33 changes: 32 additions & 1 deletion state/pgstatestorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,16 @@ func TestAddAndGetSequences(t *testing.T) {

sequence3 := state.Sequence{
FromBatchNumber: 7,
ToBatchNumber: 8,
ToBatchNumber: 7,
}
err = testState.AddSequence(ctx, sequence3, dbTx)
require.NoError(t, err)

// Insert it again to test on conflict
sequence3.ToBatchNumber = 8
err = testState.AddSequence(ctx, sequence3, dbTx)
require.NoError(t, err)

sequences, err := testState.GetSequences(ctx, 0, dbTx)
require.NoError(t, err)
require.Equal(t, 3, len(sequences))
Expand Down Expand Up @@ -655,6 +660,32 @@ func TestGetFinalizedL2BlockNumber(t *testing.T) {
}
}

func TestSyncInfo(t *testing.T) {
// Init database instance
initOrResetDB()

ctx := context.Background()
tx, err := testState.BeginStateTransaction(ctx)
require.NoError(t, err)

// Test update on conflict
err = testState.SetInitSyncBatch(ctx, 1, tx)
require.NoError(t, err)
err = testState.SetInitSyncBatch(ctx, 1, tx)
require.NoError(t, err)
err = testState.SetLastBatchInfoSeenOnEthereum(ctx, 10, 8, tx)
require.NoError(t, err)
err = testState.SetInitSyncBatch(ctx, 1, tx)
require.NoError(t, err)
err = testState.SetLastBatchInfoSeenOnEthereum(ctx, 10, 8, tx)
require.NoError(t, err)
err = testState.SetLastBatchInfoSeenOnEthereum(ctx, 10, 8, tx)
require.NoError(t, err)

err = tx.Commit(ctx)
require.NoError(t, err)
}

func TestGetBatchByNumber(t *testing.T) {
initOrResetDB()

Expand Down

0 comments on commit 88cd7a3

Please sign in to comment.