github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/db/migrations/state/0002_test.go (about) 1 package migrations_test 2 3 import ( 4 "database/sql" 5 "testing" 6 "time" 7 8 "github.com/ethereum/go-ethereum/common" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 // this migration changes length of the token name 13 type migrationTest0002 struct{} 14 15 func (m migrationTest0002) InsertData(db *sql.DB) error { 16 // Insert block to respect the FKey 17 const addBlock = "INSERT INTO state.block (block_num, received_at, block_hash) VALUES ($1, $2, $3)" 18 if _, err := db.Exec(addBlock, 1, time.Now(), "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1"); err != nil { 19 return err 20 } 21 // Insert batches 22 for i := 0; i < 4; i++ { 23 _, 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) 24 VALUES ($1, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', 25 '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', 26 $2, '0x2536C2745Ac4A584656A830f7bdCd329c94e8F30', $3)`, i, time.Now(), common.HexToHash("0x29e885edaf8e0000000000000000a23cf2d7d9f1")) 27 if err != nil { 28 return err 29 } 30 } 31 // Insert proof 32 const insertProof = `INSERT INTO state.proof ( 33 batch_num, batch_num_final, proof, proof_id, input_prover, prover, generating 34 ) VALUES ( 35 1, 1, '{"test": "test"}','proof_identifier','{"test": "test"}','prover 1', true 36 );` 37 _, err := db.Exec(insertProof) 38 if err != nil { 39 return err 40 } 41 // Insert virtual batch 42 const insertVirtualBatch = `INSERT INTO state.virtual_batch ( 43 batch_num, tx_hash, coinbase, block_num 44 ) VALUES ( 45 1, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', 1);` 46 _, err = db.Exec(insertVirtualBatch) 47 if err != nil { 48 return err 49 } 50 return nil 51 } 52 53 var indexes = []string{"transaction_l2_block_num_idx", "l2block_batch_num_idx", "l2block_received_at_idx", 54 "batch_timestamp_idx", "log_tx_hash_idx", "log_address_idx"} 55 56 func (m migrationTest0002) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) { 57 for _, idx := range indexes { 58 // getIndex 59 const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;` 60 row := db.QueryRow(getIndex, idx) 61 var result int 62 assert.NoError(t, row.Scan(&result)) 63 assert.Equal(t, 1, result) 64 } 65 // Insert new proof 66 const insertNewProof = `INSERT INTO state.proof ( 67 batch_num, batch_num_final, proof, proof_id, input_prover, prover, generating_since, prover_id, updated_at, created_at 68 ) VALUES ( 69 2, 2, '{"test": "test"}','proof_identifier','{"test": "test"}','prover 1', $1, 'prover identifier', $1, $1 70 );` 71 _, err := db.Exec(insertNewProof, time.Now()) 72 assert.NoError(t, err) 73 const insertOldProof = `INSERT INTO state.proof ( 74 batch_num, batch_num_final, proof, proof_id, input_prover, prover, generating 75 ) VALUES ( 76 3, 3, '{"test": "test"}','proof_identifier','{"test": "test"}','prover 1', true 77 );` 78 _, err = db.Exec(insertOldProof) 79 assert.Error(t, err) 80 // Insert virtual batch 81 const insertVirtualBatch = `INSERT INTO state.virtual_batch ( 82 batch_num, tx_hash, coinbase, block_num, sequencer_addr) 83 VALUES (2, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', 1, '0x514910771af9ca656af840dff83e8264ecf986ca');` 84 _, err = db.Exec(insertVirtualBatch) 85 assert.NoError(t, err) 86 // Insert reorg 87 const insertReorg = `INSERT INTO state.trusted_reorg (batch_num, reason) 88 VALUES (2, 'reason of the trusted reorg');` 89 _, err = db.Exec(insertReorg) 90 assert.NoError(t, err) 91 } 92 93 func (m migrationTest0002) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) { 94 for _, idx := range indexes { 95 // getIndex 96 const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;` 97 row := db.QueryRow(getIndex, idx) 98 var result int 99 assert.NoError(t, row.Scan(&result)) 100 assert.Equal(t, 0, result) 101 } 102 // Insert new proof 103 const insertNewProof = `INSERT INTO state.proof ( 104 batch_num, batch_num_final, proof, proof_id, input_prover, prover, generating_since, prover_id, updated_at, created_at 105 ) VALUES ( 106 3, 3, '{"test": "test"}','proof_identifier','{"test": "test"}','prover 1', $1, 'prover identifier', $1, $1 107 );` 108 _, err := db.Exec(insertNewProof, time.Now()) 109 assert.Error(t, err) 110 const insertOldProof = `INSERT INTO state.proof ( 111 batch_num, batch_num_final, proof, proof_id, input_prover, prover, generating 112 ) VALUES ( 113 3, 3, '{"test": "test"}','proof_identifier','{"test": "test"}','prover 1', true 114 );` 115 _, err = db.Exec(insertOldProof) 116 assert.NoError(t, err) 117 // Insert virtual batch 118 insertVirtualBatch := `INSERT INTO state.virtual_batch ( 119 batch_num, tx_hash, coinbase, block_num, sequencer_addr, 120 ) VALUES ( 121 3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', 1, '0x514910771af9ca656af840dff83e8264ecf986ca');` 122 _, err = db.Exec(insertVirtualBatch) 123 assert.Error(t, err) 124 // Insert virtual batch 125 insertVirtualBatch = `INSERT INTO state.virtual_batch ( 126 batch_num, tx_hash, coinbase, block_num) 127 VALUES (3, '0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1', '0x514910771af9ca656af840dff83e8264ecf986ca', 1);` 128 _, err = db.Exec(insertVirtualBatch) 129 assert.NoError(t, err) 130 // Insert reorg 131 const insertReorg = `INSERT INTO state.trusted_reorg (batch_num, reason) 132 VALUES (2, 'reason of the trusted reorg');` 133 _, err = db.Exec(insertReorg) 134 assert.Error(t, err) 135 } 136 137 func TestMigration0002(t *testing.T) { 138 runMigrationTest(t, 2, migrationTest0002{}) 139 }