Skip to content

Commit

Permalink
add log_poller_blocks migration and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Jun 26, 2024
1 parent 3d2f239 commit c2b2a28
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
23 changes: 19 additions & 4 deletions core/store/migrate/plugins/relayer/evm/0002_initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ var keyStatesUpTmpl string
//go:embed key_statesDown.tmpl.sql
var keyStatesDownTmpl string

//go:embed log_poller_blocksUp.tmpl.sql
var logPollerBlocksUpTmpl string

//go:embed log_poller_blocksDown.tmpl.sql
var logPollerBlocksDownTmpl string

type initialMigration struct {
upTmpl string
downTmpl string
Expand All @@ -38,19 +44,28 @@ var (
forwarderMigration = initialMigration{
upTmpl: forwardersUpTmpl,
downTmpl: forwardersDownTmpl,
version: 2}
version: 2,
}

headsMigration = initialMigration{
upTmpl: headsUpTmpl,
downTmpl: headsDownTmpl,
version: 3}
version: 3,
}

keyStatesMigration = initialMigration{
upTmpl: keyStatesUpTmpl,
downTmpl: keyStatesDownTmpl,
version: 4}
version: 4,
}

logPollerBlocksMigration = initialMigration{
upTmpl: logPollerBlocksUpTmpl,
downTmpl: logPollerBlocksDownTmpl,
version: 5,
}

initialMigrations = []initialMigration{forwarderMigration, headsMigration, keyStatesMigration}
initialMigrations = []initialMigration{forwarderMigration, headsMigration, keyStatesMigration, logPollerBlocksMigration}
)

func generateGoMigration(val Cfg, m initialMigration) (*goose.Migration, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
INSERT INTO evm.log_poller_blocks (evm_chain_id, block_hash, block_number, created_at, block_timestamp, finalized_block_number)
SELECT '{{ .ChainID }}', block_hash, block_number, created_at, block_timestamp, finalized_block_number FROM {{ .Schema }}.log_poller_blocks;

DROP TABLE {{ .Schema }}.log_poller_blocks;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE {{ .Schema }}.log_poller_blocks (
-- evm_chain_id numeric(78) NOT NULL,
block_hash bytea NOT NULL,
block_number int8 NOT NULL,
created_at timestamptz NOT NULL,
block_timestamp timestamptz NOT NULL,
finalized_block_number int8 DEFAULT 0 NOT NULL,
CONSTRAINT block_hash_uniq UNIQUE (block_hash),
CONSTRAINT log_poller_blocks_block_number_check CHECK ((block_number > 0)),
CONSTRAINT log_poller_blocks_finalized_block_number_check CHECK ((finalized_block_number >= 0)),
CONSTRAINT log_poller_blocks_pkey PRIMARY KEY (block_number)
);
CREATE INDEX idx_evm_log_poller_blocks_order_by_block ON {{ .Schema }}.log_poller_blocks USING btree (block_number DESC);

INSERT INTO {{ .Schema }}.log_poller_blocks (block_hash, block_number, created_at, block_timestamp, finalized_block_number)
SELECT block_hash, block_number, created_at, block_timestamp, finalized_block_number FROM evm.log_poller_blocks WHERE evm_chain_id = '{{ .ChainID}}';

DELETE FROM evm.log_poller_blocks WHERE evm_chain_id = '{{ .ChainID}}';
14 changes: 14 additions & 0 deletions core/store/migrate/plugins/relayer/evm/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ func TestGoDataMigration(t *testing.T) {
{
table: "key_states",
},
{
table: "log_poller_blocks",
},
}
for _, tt := range cases {
t.Run(tt.table+" data migration", func(t *testing.T) {
t.Parallel()
ctx := testutils.Context(t)
db := loadLegacyDatabase(t, ctx)

Expand Down Expand Up @@ -250,5 +254,15 @@ func loadLegacyDatabase(t *testing.T, ctx context.Context) *sqlx.DB {
err = db.Get(&cnt, "SELECT COUNT(*) FROM evm.key_states")
require.NoError(t, err)
require.Equal(t, 3, cnt)

// load the legacy log_poller_blocks data
logPollerBlocksMigration, err := os.ReadFile("testdata/log_poller_blocks/initial.sql")
require.NoError(t, err)
_, err = db.Exec(string(logPollerBlocksMigration))
require.NoError(t, err)
err = db.Get(&cnt, "SELECT COUNT(*) FROM evm.log_poller_blocks")
require.NoError(t, err)
require.Equal(t, 3, cnt)

return db
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
INSERT INTO evm.log_poller_blocks (evm_chain_id,block_hash,block_number,created_at,block_timestamp,finalized_block_number) VALUES
(0,decode('303132333435363738393061626364656630313233343536373839616263646566','hex'),1,'2024-06-26 17:14:14.633973-06','2024-06-26 17:14:14.633973-06',0),
(0,decode('6162636465663031323334353637383961626364656630313233343536373839','hex'),2,'2024-06-26 17:14:18.605884-06','2024-06-26 17:14:18.605884-06',1),
(1,decode('303132333435363738393061626364656630313233343536373839616263646566','hex'),1,'2024-06-26 17:14:32.572612-06','2024-06-26 17:14:32.572612-06',0);

0 comments on commit c2b2a28

Please sign in to comment.