From 3eb62880fe47250ff7c436979a1ff56e0c343700 Mon Sep 17 00:00:00 2001 From: colin <102356659+colinlyguo@users.noreply.github.com> Date: Fri, 28 Jul 2023 19:27:15 +0800 Subject: [PATCH] feat(orm): add more info for agg proving (#678) --- .../internal/controller/relayer/l2_relayer.go | 6 +-- .../internal/controller/watcher/l2_watcher.go | 10 ++-- bridge/internal/orm/batch.go | 4 +- bridge/internal/orm/chunk.go | 12 +++++ bridge/internal/orm/l2_block.go | 46 +++++++++--------- bridge/tests/gas_oracle_test.go | 4 +- bridge/tests/rollup_test.go | 6 +-- common/types/block.go | 6 +-- common/version/version.go | 2 +- coordinator/internal/orm/batch.go | 15 ++++-- coordinator/internal/orm/chunk.go | 33 +++++++++---- coordinator/internal/orm/l2_block.go | 47 ++++++++++--------- coordinator/internal/types/block.go | 4 +- .../migrate/migrations/00001_l1_message.sql | 6 +-- .../migrate/migrations/00002_l1_block.sql | 4 +- .../migrate/migrations/00003_l2_block.sql | 9 ++-- database/migrate/migrations/00004_chunk.sql | 10 ++-- database/migrate/migrations/00005_batch.sql | 5 +- 18 files changed, 138 insertions(+), 91 deletions(-) diff --git a/bridge/internal/controller/relayer/l2_relayer.go b/bridge/internal/controller/relayer/l2_relayer.go index 1a1799190..03ca12887 100644 --- a/bridge/internal/controller/relayer/l2_relayer.go +++ b/bridge/internal/controller/relayer/l2_relayer.go @@ -171,9 +171,9 @@ func (r *Layer2Relayer) initializeGenesis() error { chunk := &types.Chunk{ Blocks: []*types.WrappedBlock{{ - Header: genesis, - Transactions: nil, - WithdrawTrieRoot: common.Hash{}, + Header: genesis, + Transactions: nil, + WithdrawRoot: common.Hash{}, }}, } diff --git a/bridge/internal/controller/watcher/l2_watcher.go b/bridge/internal/controller/watcher/l2_watcher.go index 1172fe5e4..0513c0acc 100644 --- a/bridge/internal/controller/watcher/l2_watcher.go +++ b/bridge/internal/controller/watcher/l2_watcher.go @@ -169,15 +169,15 @@ func (w *L2WatcherClient) getAndStoreBlockTraces(ctx context.Context, from, to u log.Info("retrieved block", "height", block.Header().Number, "hash", block.Header().Hash().String()) - withdrawTrieRoot, err3 := w.StorageAt(ctx, w.messageQueueAddress, w.withdrawTrieRootSlot, big.NewInt(int64(number))) + withdrawRoot, err3 := w.StorageAt(ctx, w.messageQueueAddress, w.withdrawTrieRootSlot, big.NewInt(int64(number))) if err3 != nil { - return fmt.Errorf("failed to get withdrawTrieRoot: %v. number: %v", err3, number) + return fmt.Errorf("failed to get withdrawRoot: %v. number: %v", err3, number) } blocks = append(blocks, &types.WrappedBlock{ - Header: block.Header(), - Transactions: txsToTxsData(block.Transactions()), - WithdrawTrieRoot: common.BytesToHash(withdrawTrieRoot), + Header: block.Header(), + Transactions: txsToTxsData(block.Transactions()), + WithdrawRoot: common.BytesToHash(withdrawRoot), }) } diff --git a/bridge/internal/orm/batch.go b/bridge/internal/orm/batch.go index 23c448c32..e12fc7338 100644 --- a/bridge/internal/orm/batch.go +++ b/bridge/internal/orm/batch.go @@ -30,6 +30,7 @@ type Batch struct { EndChunkHash string `json:"end_chunk_hash" gorm:"column:end_chunk_hash"` StateRoot string `json:"state_root" gorm:"column:state_root"` WithdrawRoot string `json:"withdraw_root" gorm:"column:withdraw_root"` + ParentBatchHash string `json:"parent_batch_hash" gorm:"column:parent_batch_hash"` BatchHeader []byte `json:"batch_header" gorm:"column:batch_header"` // proof @@ -258,7 +259,8 @@ func (o *Batch) InsertBatch(ctx context.Context, startChunkIndex, endChunkIndex EndChunkHash: endChunkHash, EndChunkIndex: endChunkIndex, StateRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].Header.Root.Hex(), - WithdrawRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].WithdrawTrieRoot.Hex(), + WithdrawRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].WithdrawRoot.Hex(), + ParentBatchHash: parentBatchHash.Hex(), BatchHeader: batchHeader.Encode(), ChunkProofsStatus: int16(types.ChunkProofsStatusPending), ProvingStatus: int16(types.ProvingTaskUnassigned), diff --git a/bridge/internal/orm/chunk.go b/bridge/internal/orm/chunk.go index 4d884e134..d044b0f48 100644 --- a/bridge/internal/orm/chunk.go +++ b/bridge/internal/orm/chunk.go @@ -26,6 +26,10 @@ type Chunk struct { StartBlockTime uint64 `json:"start_block_time" gorm:"column:start_block_time"` TotalL1MessagesPoppedBefore uint64 `json:"total_l1_messages_popped_before" gorm:"column:total_l1_messages_popped_before"` TotalL1MessagesPoppedInChunk uint32 `json:"total_l1_messages_popped_in_chunk" gorm:"column:total_l1_messages_popped_in_chunk"` + ParentChunkHash string `json:"parent_chunk_hash" gorm:"column:parent_chunk_hash"` + StateRoot string `json:"state_root" gorm:"column:state_root"` + ParentChunkStateRoot string `json:"parent_chunk_state_root" gorm:"column:parent_chunk_state_root"` + WithdrawRoot string `json:"withdraw_root" gorm:"column:withdraw_root"` // proof ProvingStatus int16 `json:"proving_status" gorm:"column:proving_status;default:1"` @@ -118,6 +122,8 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go var chunkIndex uint64 var totalL1MessagePoppedBefore uint64 + var parentChunkHash string + var parentChunkStateRoot string parentChunk, err := o.GetLatestChunk(ctx) if err != nil && !errors.Is(errors.Unwrap(err), gorm.ErrRecordNotFound) { log.Error("failed to get latest chunk", "err", err) @@ -130,6 +136,8 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go if parentChunk != nil { chunkIndex = parentChunk.Index + 1 totalL1MessagePoppedBefore = parentChunk.TotalL1MessagesPoppedBefore + uint64(parentChunk.TotalL1MessagesPoppedInChunk) + parentChunkHash = parentChunk.Hash + parentChunkStateRoot = parentChunk.StateRoot } hash, err := chunk.Hash(totalL1MessagePoppedBefore) @@ -164,6 +172,10 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go StartBlockTime: chunk.Blocks[0].Header.Time, TotalL1MessagesPoppedBefore: totalL1MessagePoppedBefore, TotalL1MessagesPoppedInChunk: uint32(chunk.NumL1Messages(totalL1MessagePoppedBefore)), + ParentChunkHash: parentChunkHash, + StateRoot: chunk.Blocks[numBlocks-1].Header.Root.Hex(), + ParentChunkStateRoot: parentChunkStateRoot, + WithdrawRoot: chunk.Blocks[numBlocks-1].WithdrawRoot.Hex(), ProvingStatus: int16(types.ProvingTaskUnassigned), } diff --git a/bridge/internal/orm/l2_block.go b/bridge/internal/orm/l2_block.go index 8a3208caf..e17ff56b7 100644 --- a/bridge/internal/orm/l2_block.go +++ b/bridge/internal/orm/l2_block.go @@ -19,15 +19,16 @@ type L2Block struct { db *gorm.DB `gorm:"column:-"` // block - Number uint64 `json:"number" gorm:"number"` - Hash string `json:"hash" gorm:"hash"` - ParentHash string `json:"parent_hash" gorm:"parent_hash"` - Header string `json:"header" gorm:"header"` - Transactions string `json:"transactions" gorm:"transactions"` - WithdrawTrieRoot string `json:"withdraw_trie_root" gorm:"withdraw_trie_root"` - TxNum uint32 `json:"tx_num" gorm:"tx_num"` - GasUsed uint64 `json:"gas_used" gorm:"gas_used"` - BlockTimestamp uint64 `json:"block_timestamp" gorm:"block_timestamp"` + Number uint64 `json:"number" gorm:"number"` + Hash string `json:"hash" gorm:"hash"` + ParentHash string `json:"parent_hash" gorm:"parent_hash"` + Header string `json:"header" gorm:"header"` + Transactions string `json:"transactions" gorm:"transactions"` + WithdrawRoot string `json:"withdraw_root" gorm:"withdraw_root"` + StateRoot string `json:"state_root" gorm:"state_root"` + TxNum uint32 `json:"tx_num" gorm:"tx_num"` + GasUsed uint64 `json:"gas_used" gorm:"gas_used"` + BlockTimestamp uint64 `json:"block_timestamp" gorm:"block_timestamp"` // chunk ChunkHash string `json:"chunk_hash" gorm:"chunk_hash;default:NULL"` @@ -67,7 +68,7 @@ func (o *L2Block) GetL2BlocksLatestHeight(ctx context.Context) (uint64, error) { func (o *L2Block) GetUnchunkedBlocks(ctx context.Context) ([]*types.WrappedBlock, error) { db := o.db.WithContext(ctx) db = db.Model(&L2Block{}) - db = db.Select("header, transactions, withdraw_trie_root") + db = db.Select("header, transactions, withdraw_root") db = db.Where("chunk_hash IS NULL") db = db.Order("number ASC") @@ -89,7 +90,7 @@ func (o *L2Block) GetUnchunkedBlocks(ctx context.Context) ([]*types.WrappedBlock return nil, fmt.Errorf("L2Block.GetUnchunkedBlocks error: %w", err) } - wrappedBlock.WithdrawTrieRoot = common.HexToHash(v.WithdrawTrieRoot) + wrappedBlock.WithdrawRoot = common.HexToHash(v.WithdrawRoot) wrappedBlocks = append(wrappedBlocks, &wrappedBlock) } @@ -133,7 +134,7 @@ func (o *L2Block) GetL2BlocksInRange(ctx context.Context, startBlockNumber uint6 db := o.db.WithContext(ctx) db = db.Model(&L2Block{}) - db = db.Select("header, transactions, withdraw_trie_root") + db = db.Select("header, transactions, withdraw_root") db = db.Where("number >= ? AND number <= ?", startBlockNumber, endBlockNumber) db = db.Order("number ASC") @@ -160,7 +161,7 @@ func (o *L2Block) GetL2BlocksInRange(ctx context.Context, startBlockNumber uint6 return nil, fmt.Errorf("L2Block.GetL2BlocksInRange error: %w, start block: %v, end block: %v", err, startBlockNumber, endBlockNumber) } - wrappedBlock.WithdrawTrieRoot = common.HexToHash(v.WithdrawTrieRoot) + wrappedBlock.WithdrawRoot = common.HexToHash(v.WithdrawRoot) wrappedBlocks = append(wrappedBlocks, &wrappedBlock) } @@ -184,15 +185,16 @@ func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*types.WrappedBlo } l2Block := L2Block{ - Number: block.Header.Number.Uint64(), - Hash: block.Header.Hash().String(), - ParentHash: block.Header.ParentHash.String(), - Transactions: string(txs), - WithdrawTrieRoot: block.WithdrawTrieRoot.Hex(), - TxNum: uint32(len(block.Transactions)), - GasUsed: block.Header.GasUsed, - BlockTimestamp: block.Header.Time, - Header: string(header), + Number: block.Header.Number.Uint64(), + Hash: block.Header.Hash().String(), + ParentHash: block.Header.ParentHash.String(), + Transactions: string(txs), + WithdrawRoot: block.WithdrawRoot.Hex(), + StateRoot: block.Header.Root.Hex(), + TxNum: uint32(len(block.Transactions)), + GasUsed: block.Header.GasUsed, + BlockTimestamp: block.Header.Time, + Header: string(header), } l2Blocks = append(l2Blocks, l2Block) } diff --git a/bridge/tests/gas_oracle_test.go b/bridge/tests/gas_oracle_test.go index 98ac877dd..dc04ef35a 100644 --- a/bridge/tests/gas_oracle_test.go +++ b/bridge/tests/gas_oracle_test.go @@ -80,8 +80,8 @@ func testImportL2GasPrice(t *testing.T) { Difficulty: big.NewInt(0), BaseFee: big.NewInt(0), }, - Transactions: nil, - WithdrawTrieRoot: common.Hash{}, + Transactions: nil, + WithdrawRoot: common.Hash{}, }, }, } diff --git a/bridge/tests/rollup_test.go b/bridge/tests/rollup_test.go index fbf950e96..d47332988 100644 --- a/bridge/tests/rollup_test.go +++ b/bridge/tests/rollup_test.go @@ -45,9 +45,9 @@ func testCommitBatchAndFinalizeBatch(t *testing.T) { BaseFee: big.NewInt(0), } wrappedBlocks = append(wrappedBlocks, &types.WrappedBlock{ - Header: &header, - Transactions: nil, - WithdrawTrieRoot: common.Hash{}, + Header: &header, + Transactions: nil, + WithdrawRoot: common.Hash{}, }) } diff --git a/common/types/block.go b/common/types/block.go index 5955cd5c3..b8eb04c2f 100644 --- a/common/types/block.go +++ b/common/types/block.go @@ -13,12 +13,12 @@ import ( const nonZeroByteGas uint64 = 16 const zeroByteGas uint64 = 4 -// WrappedBlock contains the block's Header, Transactions and WithdrawTrieRoot hash. +// WrappedBlock contains the block's Header, Transactions and WithdrawRoot hash. type WrappedBlock struct { Header *types.Header `json:"header"` // Transactions is only used for recover types.Transactions, the from of types.TransactionData field is missing. - Transactions []*types.TransactionData `json:"transactions"` - WithdrawTrieRoot common.Hash `json:"withdraw_trie_root,omitempty"` + Transactions []*types.TransactionData `json:"transactions"` + WithdrawRoot common.Hash `json:"withdraw_trie_root,omitempty"` } // NumL1Messages returns the number of L1 messages in this block. diff --git a/common/version/version.go b/common/version/version.go index 34ae213d8..06a88de89 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.0.28" +var tag = "v4.0.29" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { diff --git a/coordinator/internal/orm/batch.go b/coordinator/internal/orm/batch.go index 481b32a7e..be2aae688 100644 --- a/coordinator/internal/orm/batch.go +++ b/coordinator/internal/orm/batch.go @@ -30,6 +30,7 @@ type Batch struct { EndChunkHash string `json:"end_chunk_hash" gorm:"column:end_chunk_hash"` StateRoot string `json:"state_root" gorm:"column:state_root"` WithdrawRoot string `json:"withdraw_root" gorm:"column:withdraw_root"` + ParentBatchHash string `json:"parent_batch_hash" gorm:"column:parent_batch_hash"` BatchHeader []byte `json:"batch_header" gorm:"column:batch_header"` // proof @@ -48,7 +49,7 @@ type Batch struct { FinalizedAt *time.Time `json:"finalized_at" gorm:"column:finalized_at;default:NULL"` // gas oracle - OracleStatus int16 `json:"oracle_status" gorm:"column:oracle_status;default:1;default:1"` + OracleStatus int16 `json:"oracle_status" gorm:"column:oracle_status;default:1"` OracleTxHash string `json:"oracle_tx_hash" gorm:"column:oracle_tx_hash;default:NULL"` // metadata @@ -131,7 +132,7 @@ func (o *Batch) GetLatestBatch(ctx context.Context) (*Batch, error) { // InsertBatch inserts a new batch into the database. // for unit test -func (o *Batch) InsertBatch(ctx context.Context, startChunkIndex, endChunkIndex uint64, startChunkHash, endChunkHash string, chunks []*types.Chunk) (*Batch, error) { +func (o *Batch) InsertBatch(ctx context.Context, startChunkIndex, endChunkIndex uint64, startChunkHash, endChunkHash string, chunks []*types.Chunk, dbTX ...*gorm.DB) (*Batch, error) { if len(chunks) == 0 { return nil, errors.New("invalid args") } @@ -184,14 +185,20 @@ func (o *Batch) InsertBatch(ctx context.Context, startChunkIndex, endChunkIndex EndChunkHash: endChunkHash, EndChunkIndex: endChunkIndex, StateRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].Header.Root.Hex(), - WithdrawRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].WithdrawTrieRoot.Hex(), + WithdrawRoot: chunks[numChunks-1].Blocks[lastChunkBlockNum-1].WithdrawRoot.Hex(), + ParentBatchHash: parentBatchHash.Hex(), BatchHeader: batchHeader.Encode(), ChunkProofsStatus: int16(types.ChunkProofsStatusPending), ProvingStatus: int16(types.ProvingTaskUnassigned), RollupStatus: int16(types.RollupPending), + OracleStatus: int16(types.GasOraclePending), } - db := o.db.WithContext(ctx) + db := o.db + if len(dbTX) > 0 && dbTX[0] != nil { + db = dbTX[0] + } + db.WithContext(ctx) db = db.Model(&Batch{}) if err := db.Create(&newBatch).Error; err != nil { diff --git a/coordinator/internal/orm/chunk.go b/coordinator/internal/orm/chunk.go index d3f5ac768..0cc5956fa 100644 --- a/coordinator/internal/orm/chunk.go +++ b/coordinator/internal/orm/chunk.go @@ -27,7 +27,11 @@ type Chunk struct { EndBlockHash string `json:"end_block_hash" gorm:"column:end_block_hash"` StartBlockTime uint64 `json:"start_block_time" gorm:"column:start_block_time"` TotalL1MessagesPoppedBefore uint64 `json:"total_l1_messages_popped_before" gorm:"column:total_l1_messages_popped_before"` - TotalL1MessagesPoppedInChunk uint64 `json:"total_l1_messages_popped_in_chunk" gorm:"column:total_l1_messages_popped_in_chunk"` + TotalL1MessagesPoppedInChunk uint32 `json:"total_l1_messages_popped_in_chunk" gorm:"column:total_l1_messages_popped_in_chunk"` + ParentChunkHash string `json:"parent_chunk_hash" gorm:"column:parent_chunk_hash"` + StateRoot string `json:"state_root" gorm:"column:state_root"` + ParentChunkStateRoot string `json:"parent_chunk_state_root" gorm:"column:parent_chunk_state_root"` + WithdrawRoot string `json:"withdraw_root" gorm:"column:withdraw_root"` // proof ProvingStatus int16 `json:"proving_status" gorm:"column:proving_status;default:1"` @@ -177,17 +181,19 @@ func (o *Chunk) GetChunkBatchHash(ctx context.Context, chunkHash string) (string // InsertChunk inserts a new chunk into the database. // for unit test -func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk) (*Chunk, error) { +func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*gorm.DB) (*Chunk, error) { if chunk == nil || len(chunk.Blocks) == 0 { return nil, errors.New("invalid args") } var chunkIndex uint64 var totalL1MessagePoppedBefore uint64 + var parentChunkHash string + var parentChunkStateRoot string parentChunk, err := o.GetLatestChunk(ctx) if err != nil && !errors.Is(errors.Unwrap(err), gorm.ErrRecordNotFound) { log.Error("failed to get latest chunk", "err", err) - return nil, err + return nil, fmt.Errorf("Chunk.InsertChunk error: %w", err) } // if parentChunk==nil then err==gorm.ErrRecordNotFound, which means there's @@ -195,13 +201,15 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk) (*Chunk, er // if parentChunk!=nil then err=nil, then we fill the parentChunk-related data into the creating chunk if parentChunk != nil { chunkIndex = parentChunk.Index + 1 - totalL1MessagePoppedBefore = parentChunk.TotalL1MessagesPoppedBefore + parentChunk.TotalL1MessagesPoppedInChunk + totalL1MessagePoppedBefore = parentChunk.TotalL1MessagesPoppedBefore + uint64(parentChunk.TotalL1MessagesPoppedInChunk) + parentChunkHash = parentChunk.Hash + parentChunkStateRoot = parentChunk.StateRoot } hash, err := chunk.Hash(totalL1MessagePoppedBefore) if err != nil { log.Error("failed to get chunk hash", "err", err) - return nil, err + return nil, fmt.Errorf("Chunk.InsertChunk error: %w", err) } var totalL2TxGas uint64 @@ -229,16 +237,23 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk) (*Chunk, er TotalL1CommitGas: totalL1CommitGas, StartBlockTime: chunk.Blocks[0].Header.Time, TotalL1MessagesPoppedBefore: totalL1MessagePoppedBefore, - TotalL1MessagesPoppedInChunk: chunk.NumL1Messages(totalL1MessagePoppedBefore), + TotalL1MessagesPoppedInChunk: uint32(chunk.NumL1Messages(totalL1MessagePoppedBefore)), + ParentChunkHash: parentChunkHash, + StateRoot: chunk.Blocks[numBlocks-1].Header.Root.Hex(), + ParentChunkStateRoot: parentChunkStateRoot, + WithdrawRoot: chunk.Blocks[numBlocks-1].WithdrawRoot.Hex(), ProvingStatus: int16(types.ProvingTaskUnassigned), } - db := o.db.WithContext(ctx) + db := o.db + if len(dbTX) > 0 && dbTX[0] != nil { + db = dbTX[0] + } + db = db.WithContext(ctx) db = db.Model(&Chunk{}) if err := db.Create(&newChunk).Error; err != nil { - log.Error("failed to insert chunk", "hash", hash, "err", err) - return nil, err + return nil, fmt.Errorf("Chunk.InsertChunk error: %w, chunk hash: %v", err, newChunk.Hash) } return &newChunk, nil diff --git a/coordinator/internal/orm/l2_block.go b/coordinator/internal/orm/l2_block.go index d84b0e4dc..cc8b67954 100644 --- a/coordinator/internal/orm/l2_block.go +++ b/coordinator/internal/orm/l2_block.go @@ -19,15 +19,16 @@ type L2Block struct { db *gorm.DB `gorm:"column:-"` // block - Number uint64 `json:"number" gorm:"number"` - Hash string `json:"hash" gorm:"hash"` - ParentHash string `json:"parent_hash" gorm:"parent_hash"` - Header string `json:"header" gorm:"header"` - Transactions string `json:"transactions" gorm:"transactions"` - WithdrawTrieRoot string `json:"withdraw_trie_root" gorm:"withdraw_trie_root"` - TxNum uint32 `json:"tx_num" gorm:"tx_num"` - GasUsed uint64 `json:"gas_used" gorm:"gas_used"` - BlockTimestamp uint64 `json:"block_timestamp" gorm:"block_timestamp"` + Number uint64 `json:"number" gorm:"number"` + Hash string `json:"hash" gorm:"hash"` + ParentHash string `json:"parent_hash" gorm:"parent_hash"` + Header string `json:"header" gorm:"header"` + Transactions string `json:"transactions" gorm:"transactions"` + WithdrawRoot string `json:"withdraw_root" gorm:"withdraw_root"` + StateRoot string `json:"state_root" gorm:"state_root"` + TxNum uint32 `json:"tx_num" gorm:"tx_num"` + GasUsed uint64 `json:"gas_used" gorm:"gas_used"` + BlockTimestamp uint64 `json:"block_timestamp" gorm:"block_timestamp"` // chunk ChunkHash string `json:"chunk_hash" gorm:"chunk_hash;default:NULL"` @@ -53,7 +54,7 @@ func (*L2Block) TableName() string { func (o *L2Block) GetL2BlocksByChunkHash(ctx context.Context, chunkHash string) ([]*types.WrappedBlock, error) { db := o.db.WithContext(ctx) db = db.Model(&L2Block{}) - db = db.Select("header, transactions, withdraw_trie_root") + db = db.Select("header, transactions, withdraw_root") db = db.Where("chunk_hash = ?", chunkHash) db = db.Order("number ASC") @@ -75,7 +76,7 @@ func (o *L2Block) GetL2BlocksByChunkHash(ctx context.Context, chunkHash string) return nil, fmt.Errorf("L2Block.GetL2BlocksByChunkHash error: %w, chunk hash: %v", err, chunkHash) } - wrappedBlock.WithdrawTrieRoot = common.HexToHash(v.WithdrawTrieRoot) + wrappedBlock.WithdrawRoot = common.HexToHash(v.WithdrawRoot) wrappedBlocks = append(wrappedBlocks, &wrappedBlock) } @@ -83,31 +84,33 @@ func (o *L2Block) GetL2BlocksByChunkHash(ctx context.Context, chunkHash string) } // InsertL2Blocks inserts l2 blocks into the "l2_block" table. +// for unit test func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*types.WrappedBlock) error { var l2Blocks []L2Block for _, block := range blocks { header, err := json.Marshal(block.Header) if err != nil { log.Error("failed to marshal block header", "hash", block.Header.Hash().String(), "err", err) - return fmt.Errorf("L2Block.InsertL2Blocks error: %w, block hash: %v", err, block.Header.Hash().String()) + return fmt.Errorf("L2Block.InsertL2Blocks error: %w", err) } txs, err := json.Marshal(block.Transactions) if err != nil { log.Error("failed to marshal transactions", "hash", block.Header.Hash().String(), "err", err) - return fmt.Errorf("L2Block.InsertL2Blocks error: %w, block hash: %v", err, block.Header.Hash().String()) + return fmt.Errorf("L2Block.InsertL2Blocks error: %w", err) } l2Block := L2Block{ - Number: block.Header.Number.Uint64(), - Hash: block.Header.Hash().String(), - ParentHash: block.Header.ParentHash.String(), - Transactions: string(txs), - WithdrawTrieRoot: block.WithdrawTrieRoot.Hex(), - TxNum: uint32(len(block.Transactions)), - GasUsed: block.Header.GasUsed, - BlockTimestamp: block.Header.Time, - Header: string(header), + Number: block.Header.Number.Uint64(), + Hash: block.Header.Hash().String(), + ParentHash: block.Header.ParentHash.String(), + Transactions: string(txs), + WithdrawRoot: block.WithdrawRoot.Hex(), + StateRoot: block.Header.Root.Hex(), + TxNum: uint32(len(block.Transactions)), + GasUsed: block.Header.GasUsed, + BlockTimestamp: block.Header.Time, + Header: string(header), } l2Blocks = append(l2Blocks, l2Block) } diff --git a/coordinator/internal/types/block.go b/coordinator/internal/types/block.go index 78d417365..43bf9709b 100644 --- a/coordinator/internal/types/block.go +++ b/coordinator/internal/types/block.go @@ -9,8 +9,8 @@ import ( type WrappedBlock struct { Header *types.Header `json:"header"` // Transactions is only used for recover types.Transactions, the from of types.TransactionData field is missing. - Transactions []*types.TransactionData `json:"transactions"` - WithdrawTrieRoot common.Hash `json:"withdraw_trie_root,omitempty"` + Transactions []*types.TransactionData `json:"transactions"` + WithdrawRoot common.Hash `json:"withdraw_trie_root,omitempty"` } // BatchInfo contains the BlockBatch's main info diff --git a/database/migrate/migrations/00001_l1_message.sql b/database/migrate/migrations/00001_l1_message.sql index 93d601013..6859d3b62 100644 --- a/database/migrate/migrations/00001_l1_message.sql +++ b/database/migrate/migrations/00001_l1_message.sql @@ -24,13 +24,13 @@ comment on column l1_message.status is 'undefined, pending, submitted, confirmed, failed, expired, relay_failed'; create unique index l1_message_hash_uindex -on l1_message (msg_hash); +on l1_message (msg_hash) where deleted_at IS NULL; create unique index l1_message_nonce_uindex -on l1_message (queue_index); +on l1_message (queue_index) where deleted_at IS NULL; create index l1_message_height_index - on l1_message (height); +on l1_message (height) where deleted_at IS NULL; -- +goose StatementEnd diff --git a/database/migrate/migrations/00002_l1_block.sql b/database/migrate/migrations/00002_l1_block.sql index 87b8176ec..90b88a5ad 100644 --- a/database/migrate/migrations/00002_l1_block.sql +++ b/database/migrate/migrations/00002_l1_block.sql @@ -22,10 +22,10 @@ comment on column l1_block.oracle_status is 'undefined, pending, importing, imported, failed'; create unique index l1_block_hash_uindex -on l1_block (hash); +on l1_block (hash) where deleted_at IS NULL; create unique index l1_block_number_uindex -on l1_block (number); +on l1_block (number) where deleted_at IS NULL; -- +goose StatementEnd diff --git a/database/migrate/migrations/00003_l2_block.sql b/database/migrate/migrations/00003_l2_block.sql index 7ab6d1891..d2f7baf94 100644 --- a/database/migrate/migrations/00003_l2_block.sql +++ b/database/migrate/migrations/00003_l2_block.sql @@ -9,7 +9,8 @@ create table l2_block parent_hash VARCHAR NOT NULL, header TEXT NOT NULL, transactions TEXT NOT NULL, - withdraw_trie_root VARCHAR NOT NULL, + withdraw_root VARCHAR NOT NULL, + state_root VARCHAR NOT NULL, tx_num INTEGER NOT NULL, gas_used BIGINT NOT NULL, block_timestamp NUMERIC NOT NULL, @@ -24,13 +25,13 @@ create table l2_block ); create unique index l2_block_hash_uindex - on l2_block (hash); +on l2_block (hash) where deleted_at IS NULL; create unique index l2_block_number_uindex - on l2_block (number); +on l2_block (number) where deleted_at IS NULL; create index l2_block_chunk_hash_index - on l2_block (chunk_hash); +on l2_block (chunk_hash) where deleted_at IS NULL; -- +goose StatementEnd diff --git a/database/migrate/migrations/00004_chunk.sql b/database/migrate/migrations/00004_chunk.sql index 4611a83a4..8e3a18e83 100644 --- a/database/migrate/migrations/00004_chunk.sql +++ b/database/migrate/migrations/00004_chunk.sql @@ -13,6 +13,10 @@ create table chunk total_l1_messages_popped_before BIGINT NOT NULL, total_l1_messages_popped_in_chunk INTEGER NOT NULL, start_block_time BIGINT NOT NULL, + parent_chunk_hash VARCHAR NOT NULL, + state_root VARCHAR NOT NULL, + parent_chunk_state_root VARCHAR NOT NULL, + withdraw_root VARCHAR NOT NULL, -- proof proving_status SMALLINT NOT NULL DEFAULT 1, @@ -38,13 +42,13 @@ comment on column chunk.proving_status is 'undefined, unassigned, assigned, proved, verified, failed'; create unique index chunk_index_uindex -on chunk (index); +on chunk (index) where deleted_at IS NULL; create unique index chunk_hash_uindex -on chunk (hash); +on chunk (hash) where deleted_at IS NULL; create index batch_hash_index -on chunk (batch_hash); +on chunk (batch_hash) where deleted_at IS NULL; -- +goose StatementEnd diff --git a/database/migrate/migrations/00005_batch.sql b/database/migrate/migrations/00005_batch.sql index 404a838e6..5824c81ba 100644 --- a/database/migrate/migrations/00005_batch.sql +++ b/database/migrate/migrations/00005_batch.sql @@ -12,6 +12,7 @@ create table batch end_chunk_hash VARCHAR NOT NULL, state_root VARCHAR NOT NULL, withdraw_root VARCHAR NOT NULL, + parent_batch_hash VARCHAR NOT NULL, batch_header BYTEA NOT NULL, -- proof @@ -40,10 +41,10 @@ create table batch ); create unique index batch_index_uindex -on batch (index); +on batch (index) where deleted_at IS NULL; create unique index batch_hash_uindex -on batch (hash); +on batch (hash) where deleted_at IS NULL; comment on column batch.chunk_proofs_status is 'undefined, pending, ready';