diff --git a/db/pgstorage/migrations/0008.sql b/db/pgstorage/migrations/0008.sql new file mode 100644 index 00000000..8bccfc2f --- /dev/null +++ b/db/pgstorage/migrations/0008.sql @@ -0,0 +1,22 @@ +-- +migrate Up +ALTER TABLE sync.deposit +ALTER COLUMN orig_net TYPE BIGINT, +ALTER COLUMN dest_net TYPE BIGINT; + +ALTER TABLE sync.claim +ALTER COLUMN orig_net TYPE BIGINT; + +ALTER TABLE sync.token_wrapped +ALTER COLUMN orig_net TYPE BIGINT; + + +-- +migrate Down +ALTER TABLE sync.deposit +ALTER COLUMN orig_net TYPE INTEGER, +ALTER COLUMN dest_net TYPE INTEGER; + +ALTER TABLE sync.claim +ALTER COLUMN orig_net TYPE INTEGER; + +ALTER TABLE sync.token_wrapped +ALTER COLUMN orig_net TYPE INTEGER; \ No newline at end of file diff --git a/db/pgstorage/migrations/0008_test.go b/db/pgstorage/migrations/0008_test.go new file mode 100644 index 00000000..63c1c5ae --- /dev/null +++ b/db/pgstorage/migrations/0008_test.go @@ -0,0 +1,128 @@ +package migrations_test + +import ( + "database/sql" + "testing" + + "github.com/stretchr/testify/assert" +) + +type migrationTest0008 struct{} + +func (m migrationTest0008) InsertData(db *sql.DB) error { + block := "INSERT INTO sync.block (id, block_num, block_hash, parent_hash, network_id, received_at) VALUES(2, 2803824, decode('27474F16174BBE50C294FE13C190B92E42B2368A6D4AEB8A4A015F52816296C3','hex'), decode('C9B5033799ADF3739383A0489EFBE8A0D4D5E4478778A4F4304562FD51AE4C07','hex'), 1, '0001-01-01 01:00:00.000');" + if _, err := db.Exec(block); err != nil { + return err + } + insertDeposit := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim) " + + "VALUES(0, 0, 123, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 456, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 0, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 1, true);" + if _, err := db.Exec(insertDeposit); err != nil { + return err + } + insertDeposit2 := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim) VALUES(0, 0, 0, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 1, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 1, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 2, true);" + if _, err := db.Exec(insertDeposit2); err != nil { + return err + } + insertDeposit3 := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim) VALUES(0, 0, 0, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 1, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 2, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 3, true);" + if _, err := db.Exec(insertDeposit3); err != nil { + return err + } + + insertClaim := "INSERT INTO sync.Claim (network_id, index, orig_net, orig_addr, amount, dest_addr, block_id, tx_hash) VALUES(1, 3, 1234, decode('0000000000000000000000000000000000000000','hex'), '300000000000000000', decode('14567C0DCF79C20FE1A21E36EC975D1775A1905C','hex'), 2, decode('A9505DB7D7EDD08947F12F2B1F7898148FFB43D80BCB977B78161EF14173D575','hex'));" + if _, err := db.Exec(insertClaim); err != nil { + return err + } + + insertTokenWrapped := "INSERT INTO sync.token_wrapped (network_id, orig_net, orig_token_addr,wrapped_token_addr, block_id) " + + "VALUES(1, 1234, decode('0000000000000000000000000000000000000000','hex'),decode('0000000000000000000000000000000000000000','hex'), 2);" + if _, err := db.Exec(insertTokenWrapped); err != nil { + return err + } + return nil +} + +func (m migrationTest0008) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) { + queryDepositCount := "select orig_net,dest_net from sync.deposit where id = 1;" + row := db.QueryRow(queryDepositCount) + var origNet, destNet uint32 + assert.NoError(t, row.Scan(&origNet, &destNet)) + assert.Equal(t, uint32(123), origNet) + assert.Equal(t, uint32(456), destNet) + var err error + insertDeposit4 := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim)" + + " VALUES(0, 0, 4294967295, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 4294967295, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 2, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 4, true);" + _, err = db.Exec(insertDeposit4) + assert.NoError(t, err) + + queryDepositCount4 := "select orig_net,dest_net from sync.deposit where id = 4;" + row = db.QueryRow(queryDepositCount4) + assert.NoError(t, row.Scan(&origNet, &destNet)) + assert.Equal(t, uint32(4294967295), origNet) + assert.Equal(t, uint32(4294967295), destNet) + + // The new deposit need to be deleted because downgrade process will fail + _, err = db.Exec("DELETE FROM sync.deposit WHERE id = 4;") + assert.NoError(t, err) + + queryClaim := "select orig_net from sync.Claim where index = 3;" + row = db.QueryRow(queryClaim) + assert.NoError(t, row.Scan(&origNet)) + assert.Equal(t, uint32(1234), origNet) + + insertClaim := "INSERT INTO sync.Claim (network_id, index, orig_net, orig_addr, amount, dest_addr, block_id, tx_hash) " + + "VALUES(1, 4, 4294967295, decode('0000000000000000000000000000000000000000','hex'), '300000000000000000', decode('14567C0DCF79C20FE1A21E36EC975D1775A1905C','hex'), 2, decode('A9505DB7D7EDD08947F12F2B1F7898148FFB43D80BCB977B78161EF14173D575','hex'));" + _, err = db.Exec(insertClaim) + assert.NoError(t, err) + + row = db.QueryRow("select orig_net from sync.Claim where index = 4;") + assert.NoError(t, row.Scan(&origNet)) + assert.Equal(t, uint32(4294967295), origNet) + // The new deposit need to be deleted because downgrade process will fail + _, err = db.Exec("DELETE FROM sync.Claim WHERE index = 4;") + assert.NoError(t, err) + + insertTokenWrapped := "INSERT INTO sync.token_wrapped (network_id, orig_net, orig_token_addr,wrapped_token_addr, block_id) " + + "VALUES(2, 4294967295, decode('0000000000000000000000000000000000000000','hex'),decode('0000000000000000000000000000000000000000','hex'), 2);" + _, err = db.Exec(insertTokenWrapped) + assert.NoError(t, err) + row = db.QueryRow("select orig_net from sync.token_wrapped where network_id = 2;") + assert.NoError(t, row.Scan(&origNet)) + assert.Equal(t, uint32(4294967295), origNet) + + // The new deposit need to be deleted because downgrade process will fail + _, err = db.Exec("DELETE FROM sync.token_wrapped WHERE network_id = 2;") + assert.NoError(t, err) +} + +func (m migrationTest0008) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) { + queryDepositCount := "select orig_net,dest_net from sync.deposit where id = 1;" + var err error + var origNet, destNet int + + // Due the change of type the first call returns an error and discard cache, so next call will work + // Discard Error: "cached plan must not change result type" "0A000" + row := db.QueryRow(queryDepositCount) + err = row.Scan(&origNet, &destNet) + assert.Error(t, err) // This error is "cached plan must not change result type" "0A000" + + row = db.QueryRow(queryDepositCount) + err = row.Scan(&origNet, &destNet) + assert.NoError(t, err) + assert.Equal(t, int(123), origNet) + assert.Equal(t, int(456), destNet) + + insertDeposit4 := "INSERT INTO sync.deposit(leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, id, ready_for_claim)" + + " VALUES(0, 0, 4294967295, decode('0000000000000000000000000000000000000000','hex'), '10000000000000000000', 4294967295, decode('C949254D682D8C9AD5682521675B8F43B102AEC4','hex'), 2, 2, decode('C2D6575EA98EB55E36B5AC6E11196800362594458A4B3143DB50E4995CB2422E','hex'), decode('','hex'), 4, true);" + _, err = db.Exec(insertDeposit4) + assert.Error(t, err) + + queryClaim := "select orig_net from sync.Claim where index = 3;" + _ = db.QueryRow(queryClaim) // Discard Error: "cached plan must not change result type" "0A000" + row = db.QueryRow(queryClaim) + assert.NoError(t, row.Scan(&origNet)) + assert.Equal(t, int(1234), origNet) +} + +func TestMigration0008(t *testing.T) { + runMigrationTest(t, 8, migrationTest0008{}) +} diff --git a/db/storage_test.go b/db/storage_test.go index 5138759e..1b9d47a3 100644 --- a/db/storage_test.go +++ b/db/storage_test.go @@ -14,6 +14,38 @@ import ( "github.com/stretchr/testify/require" ) +func TestInsertDeposit(t *testing.T) { + cfg := pgstorage.NewConfigFromEnv() + // Init database instance + err := pgstorage.InitOrReset(cfg) + require.NoError(t, err) + ctx := context.Background() + pg, err := pgstorage.NewPostgresStorage(cfg) + require.NoError(t, err) + tx, err := pg.BeginDBTransaction(ctx) + require.NoError(t, err) + + _, err = pg.AddBlock(ctx, ðerman.Block{ + BlockNumber: 1, + }, tx) + require.NoError(t, err) + deposit := ðerman.Deposit{ + NetworkID: 1, + OriginalNetwork: 4294967295, + OriginalAddress: common.HexToAddress("0x6B175474E89094C44Da98b954EedeAC495271d0F"), + Amount: big.NewInt(1000000), + DestinationNetwork: 4294967295, + DestinationAddress: common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"), + BlockNumber: 1, + BlockID: 1, + DepositCount: 1, + Metadata: common.FromHex("0x00"), + } + _, err = pg.AddDeposit(ctx, deposit, tx) + require.NoError(t, err) + require.NoError(t, tx.Rollback(ctx)) +} + func TestL1GlobalExitRoot(t *testing.T) { cfg := pgstorage.NewConfigFromEnv() // Init database instance