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

feijoa: l1infotree recursive #3474

Merged
merged 21 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5466845
first iteration of l1infotree recursive
joanestebanr Mar 21, 2024
246b3f2
fix lint, and integration with state
joanestebanr Mar 21, 2024
923fd73
fix migration
joanestebanr Mar 21, 2024
3e523ae
implement migration 19 tests and improve migration tests in general
tclemos Mar 26, 2024
1a180aa
fix linter issues
tclemos Mar 26, 2024
c1177b2
normalize l1 info tree recursive names; implement state l1 info tree …
tclemos Mar 26, 2024
27c438a
fix l1InfoTreeRecursive vectors and tests
tclemos Mar 27, 2024
541ec06
fix linter issues
tclemos Mar 27, 2024
27583fe
refactoring l1InfoTreeRecursive to fix the empty root and have snapshots
tclemos Mar 28, 2024
51cd19e
add feijoa l1InfoTree Processor; refactor ProcessorBase and ForkIDs
tclemos Apr 1, 2024
19d7303
fix linter issues
tclemos Apr 2, 2024
c5b8291
revert l1InfoTreeRecursive snapshot implementation; adjust etrog and …
tclemos Apr 2, 2024
3ce462f
force initialize l1InfoTreeRecursive
tclemos Apr 2, 2024
6f4d817
Merge branch 'develop' into feature/3465-feijoa_l1infotree
tclemos Apr 2, 2024
e18fc93
Merge branch 'develop' into feature/3465-feijoa_l1infotree
tclemos Apr 3, 2024
1988705
docker compose fix
tclemos Apr 3, 2024
5466d88
fix linter issues
tclemos Apr 3, 2024
5d7eb7b
Merge branch 'develop' into feature/3465-feijoa_l1infotree
tclemos Apr 4, 2024
9d106a5
update docker compose check
tclemos Apr 4, 2024
6ed68fe
Merge branch 'develop' into feature/3465-feijoa_l1infotree
tclemos Apr 4, 2024
ea4de0e
Revert "update docker compose check"
tclemos Apr 4, 2024
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 @@ -490,7 +490,7 @@ func newState(ctx context.Context, c *config.Config, etherman *etherman.Client,
}
stateDb := pgstatestorage.NewPostgresStorage(stateCfg, sqlDB)

st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog, nil)
st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog, nil, nil)
// This is to force to build cache, and check that DB is ok before starting the application
l1InfoRoot, err := st.GetCurrentL1InfoRoot(ctx, nil)
if err != nil {
Expand Down
44 changes: 25 additions & 19 deletions db/migrations/state/0013_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import (
"github.com/stretchr/testify/assert"
)

const (
blockHashValue = "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1"
mainExitRootValue = "0x83fc198de31e1b2b1a8212d2430fbb7766c13d9ad305637dea3759065606475d"
rollupExitRootValue = "0xadb91a6a1fce56eaea561002bc9a993f4e65a7710bd72f4eee3067cbd73a743c"
globalExitRootValue = "0x5bf4af1a651a2a74b36e6eb208481f94c69fc959f756223dfa49608061937585"
previousBlockHashValue = "0xe865e912b504572a4d80ad018e29797e3c11f00bf9ae2549548a25779c9d7e57"
l1InfoRootValue = "0x2b9484b83c6398033241865b015fb9430eb3e159182a6075d00c924845cc393e"
)

// this migration changes length of the token name
type migrationTest0013 struct{}
type migrationTest0013 struct {
blockHashValue string
mainExitRootValue string
rollupExitRootValue string
globalExitRootValue string
previousBlockHashValue string
l1InfoRootValue string
}

func (m migrationTest0013) insertBlock(blockNumber uint64, db *sql.DB) error {
const addBlock = "INSERT INTO state.block (block_num, received_at, block_hash) VALUES ($1, $2, $3)"
if _, err := db.Exec(addBlock, blockNumber, time.Now(), blockHashValue); err != nil {
if _, err := db.Exec(addBlock, blockNumber, time.Now(), m.blockHashValue); err != nil {
return err
}
return nil
Expand All @@ -45,10 +43,10 @@ func (m migrationTest0013) InsertData(db *sql.DB) error {
if err = m.insertBlock(uint64(124), db); err != nil {
return err
}
if err = m.insertRowInOldTable(db, []interface{}{123, time.Now(), mainExitRootValue, rollupExitRootValue, globalExitRootValue}); err != nil {
if err = m.insertRowInOldTable(db, []interface{}{123, time.Now(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue}); err != nil {
return err
}
if err = m.insertRowInOldTable(db, []interface{}{124, time.Now(), mainExitRootValue, rollupExitRootValue, globalExitRootValue}); err != nil {
if err = m.insertRowInOldTable(db, []interface{}{124, time.Now(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue}); err != nil {
return err
}

Expand Down Expand Up @@ -113,16 +111,16 @@ func (m migrationTest0013) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
assert.NoError(t, err)
err = m.insertBlock(uint64(127), db)
assert.NoError(t, err)
prevBlockHash := previousBlockHashValue
l1InfoRoot := l1InfoRootValue
err = m.insertRowInMigratedTable(db, []interface{}{125, time.Now(), mainExitRootValue, rollupExitRootValue, globalExitRootValue, prevBlockHash, l1InfoRoot, 1})
prevBlockHash := m.previousBlockHashValue
l1InfoRoot := m.l1InfoRootValue
err = m.insertRowInMigratedTable(db, []interface{}{125, time.Now(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, prevBlockHash, l1InfoRoot, 1})
assert.NoError(t, err)
// insert duplicated l1_info_root
err = m.insertRowInMigratedTable(db, []interface{}{126, time.Now(), mainExitRootValue, rollupExitRootValue, globalExitRootValue, prevBlockHash, l1InfoRoot, 1})
err = m.insertRowInMigratedTable(db, []interface{}{126, time.Now(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, prevBlockHash, l1InfoRoot, 1})
assert.Error(t, err)

// insert in the old way must work
err = m.insertRowInOldTable(db, []interface{}{127, time.Now(), mainExitRootValue, rollupExitRootValue, globalExitRootValue})
err = m.insertRowInOldTable(db, []interface{}{127, time.Now(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue})
assert.NoError(t, err)

sqlSelect := `SELECT prev_block_hash, l1_info_root FROM state.exit_root WHERE l1_info_tree_index = $1`
Expand Down Expand Up @@ -185,5 +183,13 @@ func (m migrationTest0013) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB
}

func TestMigration0013(t *testing.T) {
runMigrationTest(t, 13, migrationTest0013{})
m := migrationTest0013{
blockHashValue: "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1",
mainExitRootValue: "0x83fc198de31e1b2b1a8212d2430fbb7766c13d9ad305637dea3759065606475d",
rollupExitRootValue: "0xadb91a6a1fce56eaea561002bc9a993f4e65a7710bd72f4eee3067cbd73a743c",
globalExitRootValue: "0x5bf4af1a651a2a74b36e6eb208481f94c69fc959f756223dfa49608061937585",
previousBlockHashValue: "0xe865e912b504572a4d80ad018e29797e3c11f00bf9ae2549548a25779c9d7e57",
l1InfoRootValue: "0x2b9484b83c6398033241865b015fb9430eb3e159182a6075d00c924845cc393e",
}
runMigrationTest(t, 13, m)
}
48 changes: 26 additions & 22 deletions db/migrations/state/0018_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/stretchr/testify/assert"
)

type migrationTest0018 struct{}
type migrationTest0018 struct {
migrationBase
}

func (m migrationTest0018) InsertData(db *sql.DB) error {
const insertBatch1 = `
Expand Down Expand Up @@ -46,16 +48,7 @@ func (m migrationTest0018) InsertData(db *sql.DB) error {
}

func (m migrationTest0018) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
assertTableNotExists(t, db, "state", "proof")

assertTableExists(t, db, "state", "blob_inner")
assertTableExists(t, db, "state", "batch_proof")
assertTableExists(t, db, "state", "blob_inner_proof")
assertTableExists(t, db, "state", "blob_outer_proof")

assertColumnExists(t, db, "state", "virtual_batch", "blob_inner_num")
assertColumnExists(t, db, "state", "virtual_batch", "prev_l1_it_root")
assertColumnExists(t, db, "state", "virtual_batch", "prev_l1_it_index")
m.AssertNewAndRemovedItemsAfterMigrationUp(t, db)

// Insert blobInner 1
const insertBlobInner = `INSERT INTO state.blob_inner (blob_inner_num, data, block_num) VALUES (1, E'\\x1234', 1);`
Expand All @@ -80,16 +73,7 @@ func (m migrationTest0018) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
func (m migrationTest0018) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
var result int

assertTableExists(t, db, "state", "proof")

assertTableNotExists(t, db, "state", "blob_inner")
assertTableNotExists(t, db, "state", "batch_proof")
assertTableNotExists(t, db, "state", "blob_inner_proof")
assertTableNotExists(t, db, "state", "blob_outer_proof")

assertColumnNotExists(t, db, "state", "virtual_batch", "blob_inner_num")
assertColumnNotExists(t, db, "state", "virtual_batch", "prev_l1_it_root")
assertColumnNotExists(t, db, "state", "virtual_batch", "prev_l1_it_index")
m.AssertNewAndRemovedItemsAfterMigrationDown(t, db)

// Check column blob_inner_num doesn't exists in state.virtual_batch table
const getBlobInnerNumColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='virtual_batch' and column_name='blob_inner_num'`
Expand All @@ -111,5 +95,25 @@ func (m migrationTest0018) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB
}

func TestMigration0018(t *testing.T) {
runMigrationTest(t, 18, migrationTest0018{})
m := migrationTest0018{
migrationBase: migrationBase{
removedTables: []tableMetadata{
{"state", "proof"},
},

newTables: []tableMetadata{
{"state", "blob_inner"},
{"state", "batch_proof"},
{"state", "blob_inner_proof"},
{"state", "blob_outer_proof"},
},

newColumns: []columnMetadata{
{"state", "virtual_batch", "blob_inner_num"},
{"state", "virtual_batch", "prev_l1_it_root"},
{"state", "virtual_batch", "prev_l1_it_index"},
},
},
}
runMigrationTest(t, 18, m)
}
12 changes: 12 additions & 0 deletions db/migrations/state/0019.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- +migrate Up

-- +migrate Up
ALTER TABLE state.exit_root
ADD COLUMN IF NOT EXISTS l1_info_tree_recursive_index BIGINT DEFAULT NULL UNIQUE;
CREATE INDEX IF NOT EXISTS idx_exit_root_l1_info_tree_recursive_index ON state.exit_root (l1_info_tree_recursive_index);

-- +migrate Down
ALTER TABLE state.exit_root
DROP COLUMN IF EXISTS l1_info_tree_recursive_index;
DROP INDEX IF EXISTS state.idx_exit_root_l1_info_tree_recursive_index;

106 changes: 106 additions & 0 deletions db/migrations/state/0019_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package migrations_test

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

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

type migrationTest0019 struct {
migrationBase

blockHashValue string
mainExitRootValue string
rollupExitRootValue string
globalExitRootValue string
previousBlockHashValue string
l1InfoRootValue string
}

func (m migrationTest0019) insertBlock(blockNumber uint64, db *sql.DB) error {
const addBlock = "INSERT INTO state.block (block_num, received_at, block_hash) VALUES ($1, $2, $3)"
if _, err := db.Exec(addBlock, blockNumber, time.Now(), m.blockHashValue); err != nil {
return err
}
return nil
}

func (m migrationTest0019) insertRowInOldTable(db *sql.DB, args ...interface{}) error {
sql := `
INSERT INTO state.exit_root (block_num, "timestamp", mainnet_exit_root, rollup_exit_root, global_exit_root, prev_block_hash, l1_info_root, l1_info_tree_index)
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8);`

_, err := db.Exec(sql, args...)
return err
}

func (m migrationTest0019) insertRowInMigratedTable(db *sql.DB, args ...interface{}) error {
sql := `
INSERT INTO state.exit_root (block_num, "timestamp", mainnet_exit_root, rollup_exit_root, global_exit_root, prev_block_hash, l1_info_root, l1_info_tree_index, l1_info_tree_recursive_index)
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9);`

_, err := db.Exec(sql, args...)
return err
}

func (m migrationTest0019) InsertData(db *sql.DB) error {
var err error
for i := uint64(1); i <= 6; i++ {
if err = m.insertBlock(i, db); err != nil {
return err
}
}

return nil
}

func (m migrationTest0019) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
m.AssertNewAndRemovedItemsAfterMigrationUp(t, db)

var nilL1InfoTreeIndex *uint = nil
err := m.insertRowInOldTable(db, 1, time.Now().UTC(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, m.previousBlockHashValue, m.l1InfoRootValue, nilL1InfoTreeIndex)
assert.NoError(t, err)

err = m.insertRowInOldTable(db, 2, time.Now().UTC(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, m.previousBlockHashValue, m.l1InfoRootValue, uint(1))
assert.NoError(t, err)

err = m.insertRowInMigratedTable(db, 3, time.Now().UTC(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, m.previousBlockHashValue, m.l1InfoRootValue, nilL1InfoTreeIndex, 1)
assert.NoError(t, err)
}

func (m migrationTest0019) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
m.AssertNewAndRemovedItemsAfterMigrationDown(t, db)

var nilL1InfoTreeIndex *uint = nil
err := m.insertRowInOldTable(db, 4, time.Now().UTC(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, m.previousBlockHashValue, m.l1InfoRootValue, nilL1InfoTreeIndex)
assert.NoError(t, err)

err = m.insertRowInOldTable(db, 5, time.Now().UTC(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, m.previousBlockHashValue, m.l1InfoRootValue, uint(2))
assert.NoError(t, err)

err = m.insertRowInMigratedTable(db, 6, time.Now().UTC(), m.mainExitRootValue, m.rollupExitRootValue, m.globalExitRootValue, m.previousBlockHashValue, m.l1InfoRootValue, nilL1InfoTreeIndex, 2)
assert.Error(t, err)
}

func TestMigration0019(t *testing.T) {
m := migrationTest0019{
migrationBase: migrationBase{
newIndexes: []string{
"idx_exit_root_l1_info_tree_recursive_index",
},
newColumns: []columnMetadata{
{"state", "exit_root", "l1_info_tree_recursive_index"},
},
},

blockHashValue: "0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1",
mainExitRootValue: "0x83fc198de31e1b2b1a8212d2430fbb7766c13d9ad305637dea3759065606475d",
rollupExitRootValue: "0xadb91a6a1fce56eaea561002bc9a993f4e65a7710bd72f4eee3067cbd73a743c",
globalExitRootValue: "0x5bf4af1a651a2a74b36e6eb208481f94c69fc959f756223dfa49608061937585",
previousBlockHashValue: "0xe865e912b504572a4d80ad018e29797e3c11f00bf9ae2549548a25779c9d7e57",
l1InfoRootValue: "0x2b9484b83c6398033241865b015fb9430eb3e159182a6075d00c924845cc393e",
}
runMigrationTest(t, 19, m)
}
Loading
Loading