Skip to content

Commit

Permalink
fix l1InfoRoot in sync, aggregator and state (0xPolygonHermez#3052)
Browse files Browse the repository at this point in the history
* fix l1InfoRoot in sync, aggregator and state

* fix
  • Loading branch information
ARR552 authored Jan 11, 2024
1 parent 2cdfdbd commit ed8f7a7
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 94 deletions.
10 changes: 7 additions & 3 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,12 @@ func (a *Aggregator) buildInputProver(ctx context.Context, batchToVerify *state.
}

l1InfoTreeData := map[uint32]*prover.L1Data{}
l1InfoRoot := common.Hash{}
vb, err := a.State.GetVirtualBatch(ctx, batchToVerify.BatchNumber, nil)
if err != nil {
log.Errorf("Failed getting virtualBatch %d, err: %v", batchToVerify.BatchNumber, err)
return nil, err
}
l1InfoRoot := vb.L1InfoRoot
forcedBlockhashL1 := common.Hash{}

if !isForcedBatch {
Expand Down Expand Up @@ -1041,13 +1046,12 @@ func (a *Aggregator) buildInputProver(ctx context.Context, batchToVerify *state.
} else {
// Initial batch must be handled differently
if batchToVerify.BatchNumber == 1 {
l1InfoRoot = batchToVerify.GlobalExitRoot
forcedBlockhashL1, err = a.State.GetVirtualBatchParentHash(ctx, batchToVerify.BatchNumber, nil)
if err != nil {
return nil, err
}
} else {
forcedBlockhashL1, l1InfoRoot, err = a.State.GetForcedBatchParentHashAndGER(ctx, *batchToVerify.ForcedBatchNum, nil)
forcedBlockhashL1, err = a.State.GetForcedBatchParentHash(ctx, *batchToVerify.ForcedBatchNum, nil)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion aggregator/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ type stateInterface interface {
GetL1InfoRootLeafByIndex(ctx context.Context, l1InfoTreeIndex uint32, dbTx pgx.Tx) (state.L1InfoTreeExitRootStorageEntry, error)
GetLeafsByL1InfoRoot(ctx context.Context, l1InfoRoot common.Hash, dbTx pgx.Tx) ([]state.L1InfoTreeExitRootStorageEntry, error)
GetVirtualBatchParentHash(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHashAndGER(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, common.Hash, error)
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetVirtualBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.VirtualBatch, error)
}
55 changes: 38 additions & 17 deletions aggregator/mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions db/migrations/state/0013.sql
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
-- +migrate Up
ALTER TABLE state.exit_root
ADD COLUMN prev_block_hash BYTEA DEFAULT NULL,
ADD COLUMN l1_info_root BYTEA DEFAULT NULL,
ADD COLUMN l1_info_tree_index BIGINT DEFAULT NULL UNIQUE;
ADD COLUMN IF NOT EXISTS prev_block_hash BYTEA DEFAULT NULL,
ADD COLUMN IF NOT EXISTS l1_info_root BYTEA DEFAULT NULL,
ADD COLUMN IF NOT EXISTS l1_info_tree_index BIGINT DEFAULT NULL UNIQUE;
CREATE INDEX IF NOT EXISTS idx_exit_root_l1_info_tree_index ON state.exit_root (l1_info_tree_index);

ALTER TABLE state.transaction
ADD COLUMN l2_hash VARCHAR UNIQUE;
ADD COLUMN IF NOT EXISTS l2_hash VARCHAR UNIQUE;

CREATE INDEX IF NOT EXISTS idx_transaction_l2_hash ON state.transaction (l2_hash);

ALTER TABLE state.batch
ADD COLUMN wip BOOLEAN NOT NULL;
ADD COLUMN IF NOT EXISTS wip BOOLEAN NOT NULL;

ALTER TABLE state.virtual_batch
ADD COLUMN timestamp_batch_etrog TIMESTAMP WITH TIME ZONE NULL;
ADD COLUMN IF NOT EXISTS timestamp_batch_etrog TIMESTAMP WITH TIME ZONE NULL,
ADD COLUMN IF NOT EXISTS l1_info_root VARCHAR;

-- +migrate Down
ALTER TABLE state.exit_root
DROP COLUMN prev_block_hash,
DROP COLUMN l1_info_root,
DROP COLUMN l1_info_tree_index;
DROP COLUMN IF EXISTS prev_block_hash,
DROP COLUMN IF EXISTS l1_info_root,
DROP COLUMN IF EXISTS l1_info_tree_index;
DROP INDEX IF EXISTS state.idx_exit_root_l1_info_tree_index;

ALTER TABLE state.transaction
DROP COLUMN l2_hash;
DROP COLUMN IF EXISTS l2_hash;

DROP INDEX IF EXISTS state.idx_transaction_l2_hash;

ALTER TABLE state.batch
DROP COLUMN IF EXISTS wip;

ALTER TABLE state.virtual_batch
DROP COLUMN IF EXISTS timestamp_batch_etrog;
DROP COLUMN IF EXISTS timestamp_batch_etrog,
DROP COLUMN IF EXISTS l1_info_root;

12 changes: 12 additions & 0 deletions db/migrations/state/0013_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ func (m migrationTest0013) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
// Try to insert data into the transactions table
err = m.InsertDataIntoTransactionsTable(db)
assert.NoError(t, err)

insertVirtualBatch := `INSERT INTO state.virtual_batch
(batch_num, tx_hash, coinbase, block_num, sequencer_addr, timestamp_batch_etrog)
VALUES(0, '0x23970ef3f8184daa93385faf802df142a521b479e8e59fbeafa11b8927eb77b1', '0x0000000000000000000000000000000000000000', 1, '0x6645F64d1cE0513bbf5E6713b7e4D0A957AC853c', '2023-12-22 16:53:00.000');`
_, err = db.Exec(insertVirtualBatch)
assert.NoError(t, err)
}

func (m migrationTest0013) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
Expand All @@ -170,6 +176,12 @@ func (m migrationTest0013) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB
row = db.QueryRow(getWIPColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)

insertVirtualBatch := `INSERT INTO state.virtual_batch
(batch_num, tx_hash, coinbase, block_num, sequencer_addr, timestamp_batch_etrog)
VALUES(0, '0x23970ef3f8184daa93385faf802df142a521b479e8e59fbeafa11b8927eb77b1', '0x0000000000000000000000000000000000000000', 1, '0x6645F64d1cE0513bbf5E6713b7e4D0A957AC853c', '2023-12-22 16:53:00.000');`
_, err = db.Exec(insertVirtualBatch)
assert.Error(t, err)
}

func TestMigration0013(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ type stateInterface interface {
GetL1InfoTreeDataFromBatchL2Data(ctx context.Context, batchL2Data []byte, dbTx pgx.Tx) (map[uint32]state.L1DataV2, common.Hash, error)
GetBlockByNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (*state.Block, error)
GetVirtualBatchParentHash(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHashAndGER(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, common.Hash, error)
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetL1InfoRootLeafByIndex(ctx context.Context, l1InfoTreeIndex uint32, dbTx pgx.Tx) (state.L1InfoTreeExitRootStorageEntry, error)
GetVirtualBatch(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.VirtualBatch, error)
}

type workerInterface interface {
Expand Down
55 changes: 38 additions & 17 deletions sequencer/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type VirtualBatch struct {
Coinbase common.Address
SequencerAddr common.Address
BlockNumber uint64
L1InfoRoot *common.Hash
// TimestampBatchEtrog etrog: Batch timestamp comes from L1 block timestamp
// for previous batches is NULL because the batch timestamp is in batch table
TimestampBatchEtrog *time.Time
Expand Down Expand Up @@ -564,8 +565,12 @@ func (s *State) GetL1InfoTreeDataFromBatchL2Data(ctx context.Context, batchL2Dat
}

l1InfoTreeData := map[uint32]L1DataV2{}
lastL1InfoRoot := ZeroHash

maxIndex := findMax(batchRaw.Blocks)
l1InfoTreeExitRoot, err := s.GetL1InfoRootLeafByIndex(ctx, maxIndex, dbTx)
if err != nil {
return nil, ZeroHash, err
}
l1InfoRoot := l1InfoTreeExitRoot.L1InfoTreeRoot
for _, l2blockRaw := range batchRaw.Blocks {
// Index 0 is a special case, it means that the block is not changing GlobalExitRoot.
// it must not be included in l1InfoTreeData. If all index are 0 L1InfoRoot == ZeroHash
Expand All @@ -584,11 +589,19 @@ func (s *State) GetL1InfoTreeDataFromBatchL2Data(ctx context.Context, batchL2Dat
}

l1InfoTreeData[l2blockRaw.IndexL1InfoTree] = l1Data

lastL1InfoRoot = l1InfoTreeExitRootStorageEntry.L1InfoTreeRoot
}
}
}

return l1InfoTreeData, lastL1InfoRoot, nil
return l1InfoTreeData, l1InfoRoot, nil
}

func findMax(blocks []L2BlockRaw) uint32 {
maxIndex := blocks[0].IndexL1InfoTree
for _, b := range blocks {
if b.IndexL1InfoTree > maxIndex {
maxIndex = b.IndexL1InfoTree
}
}
return maxIndex
}
15 changes: 5 additions & 10 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,11 @@ func (s *State) ProcessAndStoreClosedBatchV2(ctx context.Context, processingCtx
}
}
}
var ger common.Hash
if processingCtx.BatchNumber == 1 {
ger = processingCtx.L1InfoRoot
}
return common.BytesToHash(processed.NewStateRoot), processed.FlushId, processed.ProverId, s.CloseBatchInStorage(ctx, ProcessingReceipt{
BatchNumber: processingCtx.BatchNumber,
StateRoot: processedBatch.NewStateRoot,
LocalExitRoot: processedBatch.NewLocalExitRoot,
AccInputHash: processedBatch.NewAccInputHash,
BatchL2Data: *BatchL2Data,
GlobalExitRoot: ger,
BatchNumber: processingCtx.BatchNumber,
StateRoot: processedBatch.NewStateRoot,
LocalExitRoot: processedBatch.NewLocalExitRoot,
AccInputHash: processedBatch.NewAccInputHash,
BatchL2Data: *BatchL2Data,
}, dbTx)
}
Loading

0 comments on commit ed8f7a7

Please sign in to comment.