Merge branch 'develop' into feat-support-blob-tx-in-sender

This commit is contained in:
colin
2024-03-19 12:33:48 +08:00
committed by GitHub
14 changed files with 213 additions and 160 deletions

View File

@@ -99,10 +99,6 @@ func TestCodecV0(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
batchL1CommitCalldataSize, err := EstimateBatchL1CommitCalldataSize(batch)
@@ -154,10 +150,6 @@ func TestCodecV0(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
batchL1CommitCalldataSize, err = EstimateBatchL1CommitCalldataSize(batch)
@@ -211,10 +203,6 @@ func TestCodecV0(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
batchL1CommitCalldataSize, err = EstimateBatchL1CommitCalldataSize(batch)
@@ -276,20 +264,11 @@ func TestCodecV0(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 61, len(chunkBytes2))
daChunk1Hash, err := daChunk1.Hash()
assert.NoError(t, err)
daChunk2Hash, err := daChunk2.Hash()
assert.NoError(t, err)
batch = &encoding.Batch{
Index: 1,
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk1, chunk2},
StartChunkIndex: 0,
EndChunkIndex: 1,
StartChunkHash: daChunk1Hash,
EndChunkHash: daChunk2Hash,
}
batchL1CommitCalldataSize, err = EstimateBatchL1CommitCalldataSize(batch)
@@ -343,10 +322,6 @@ func TestCodecV0(t *testing.T) {
TotalL1MessagePoppedBefore: 37,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
batchL1CommitCalldataSize, err = EstimateBatchL1CommitCalldataSize(batch)
@@ -400,10 +375,6 @@ func TestCodecV0(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
batchL1CommitCalldataSize, err = EstimateBatchL1CommitCalldataSize(batch)
@@ -457,10 +428,6 @@ func TestCodecV0(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
batchL1CommitCalldataSize, err = EstimateBatchL1CommitCalldataSize(batch)
@@ -514,10 +481,6 @@ func TestCodecV0(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: parentBatchHash,
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
batchL1CommitCalldataSize, err = EstimateBatchL1CommitCalldataSize(batch)

View File

@@ -27,12 +27,6 @@ type Batch struct {
TotalL1MessagePoppedBefore uint64
ParentBatchHash common.Hash
Chunks []*Chunk
// Only used in updating db info.
StartChunkIndex uint64
EndChunkIndex uint64
StartChunkHash common.Hash
EndChunkHash common.Hash
}
// NumL1Messages returns the number of L1 messages in this block.

View File

@@ -167,6 +167,9 @@ func (o *Batch) GetLatestBatch(ctx context.Context) (*Batch, error) {
var latestBatch Batch
if err := db.First(&latestBatch).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, fmt.Errorf("Batch.GetLatestBatch error: %w", err)
}
return &latestBatch, nil
@@ -190,21 +193,73 @@ func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, dbTX ...
return nil, errors.New("invalid args: batch is nil")
}
numChunks := uint64(len(batch.Chunks))
if numChunks == 0 {
return nil, errors.New("invalid args: batch contains 0 chunk")
}
daBatch, err := codecv0.NewDABatch(batch)
if err != nil {
log.Error("failed to create new DA batch",
"index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", len(batch.Chunks), "err", err)
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, err
}
var startChunkIndex uint64
parentBatch, err := o.GetLatestBatch(ctx)
if err != nil {
log.Error("failed to get latest batch", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
// if parentBatch==nil then err==gorm.ErrRecordNotFound, which means there's
// no batch record in the db, we then use default empty values for the creating batch;
// if parentBatch!=nil then err==nil, then we fill the parentBatch-related data into the creating batch
if parentBatch != nil {
startChunkIndex = parentBatch.EndChunkIndex + 1
}
startDAChunk, err := codecv0.NewDAChunk(batch.Chunks[0], batch.TotalL1MessagePoppedBefore)
if err != nil {
log.Error("failed to create start DA chunk", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
startDAChunkHash, err := startDAChunk.Hash()
if err != nil {
log.Error("failed to get start DA chunk hash", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
totalL1MessagePoppedBeforeEndDAChunk := batch.TotalL1MessagePoppedBefore
for i := uint64(0); i < numChunks-1; i++ {
totalL1MessagePoppedBeforeEndDAChunk += batch.Chunks[i].NumL1Messages(totalL1MessagePoppedBeforeEndDAChunk)
}
endDAChunk, err := codecv0.NewDAChunk(batch.Chunks[numChunks-1], totalL1MessagePoppedBeforeEndDAChunk)
if err != nil {
log.Error("failed to create end DA chunk", "index", batch.Index, "total l1 message popped before", totalL1MessagePoppedBeforeEndDAChunk,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
endDAChunkHash, err := endDAChunk.Hash()
if err != nil {
log.Error("failed to get end DA chunk hash", "index", batch.Index, "total l1 message popped before", totalL1MessagePoppedBeforeEndDAChunk,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
newBatch := Batch{
Index: batch.Index,
Hash: daBatch.Hash().Hex(),
StartChunkHash: batch.StartChunkHash.Hex(),
StartChunkIndex: batch.StartChunkIndex,
EndChunkHash: batch.EndChunkHash.Hex(),
EndChunkIndex: batch.EndChunkIndex,
StartChunkHash: startDAChunkHash.Hex(),
StartChunkIndex: startChunkIndex,
EndChunkHash: endDAChunkHash.Hex(),
EndChunkIndex: startChunkIndex + numChunks - 1,
StateRoot: batch.StateRoot().Hex(),
WithdrawRoot: batch.WithdrawRoot().Hex(),
ParentBatchHash: batch.ParentBatchHash.Hex(),

View File

@@ -153,15 +153,18 @@ func (o *Chunk) GetProofsByBatchHash(ctx context.Context, batchHash string) ([]*
return proofs, nil
}
// GetLatestChunk retrieves the latest chunk from the database.
func (o *Chunk) GetLatestChunk(ctx context.Context) (*Chunk, error) {
// getLatestChunk retrieves the latest chunk from the database.
func (o *Chunk) getLatestChunk(ctx context.Context) (*Chunk, error) {
db := o.db.WithContext(ctx)
db = db.Model(&Chunk{})
db = db.Order("index desc")
var latestChunk Chunk
if err := db.First(&latestChunk).Error; err != nil {
return nil, fmt.Errorf("Chunk.GetLatestChunk error: %w", err)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, fmt.Errorf("Chunk.getLatestChunk error: %w", err)
}
return &latestChunk, nil
}
@@ -230,15 +233,15 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *encoding.Chunk, dbTX ...
var totalL1MessagePoppedBefore uint64
var parentChunkHash string
var parentChunkStateRoot string
parentChunk, err := o.GetLatestChunk(ctx)
if err != nil && !errors.Is(errors.Unwrap(err), gorm.ErrRecordNotFound) {
parentChunk, err := o.getLatestChunk(ctx)
if err != nil {
log.Error("failed to get latest chunk", "err", err)
return nil, fmt.Errorf("Chunk.InsertChunk error: %w", err)
}
// if parentChunk==nil then err==gorm.ErrRecordNotFound, which means there's
// not chunk record in the db, we then use default empty values for the creating chunk;
// if parentChunk!=nil then err=nil, then we fill the parentChunk-related data into the creating chunk
// no chunk record in the db, we then use default empty values for the creating chunk;
// 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

View File

@@ -203,10 +203,6 @@ func (r *Layer2Relayer) initializeGenesis() error {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: common.HexToHash(dbChunk.Hash),
EndChunkHash: common.HexToHash(dbChunk.Hash),
}
var dbBatch *orm.Batch

View File

@@ -69,10 +69,6 @@ func testL2RelayerProcessPendingBatches(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1, chunk2},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
batchOrm := orm.NewBatch(db)
@@ -101,10 +97,6 @@ func testL2RelayerProcessCommittedBatches(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1, chunk2},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
batchOrm := orm.NewBatch(db)
@@ -154,10 +146,6 @@ func testL2RelayerFinalizeTimeoutBatches(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1, chunk2},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
batchOrm := orm.NewBatch(db)
@@ -198,10 +186,6 @@ func testL2RelayerCommitConfirm(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1, chunk2},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
dbBatch, err := batchOrm.InsertBatch(context.Background(), batch)
@@ -258,10 +242,6 @@ func testL2RelayerFinalizeConfirm(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1, chunk2},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
dbBatch, err := batchOrm.InsertBatch(context.Background(), batch)
@@ -305,10 +285,6 @@ func testL2RelayerGasOracleConfirm(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 0,
EndChunkHash: chunkHash1,
}
batchOrm := orm.NewBatch(db)
@@ -320,10 +296,6 @@ func testL2RelayerGasOracleConfirm(t *testing.T) {
TotalL1MessagePoppedBefore: batch1.TotalL1MessagePoppedBefore,
ParentBatchHash: common.HexToHash(dbBatch1.Hash),
Chunks: []*encoding.Chunk{chunk2},
StartChunkIndex: 1,
StartChunkHash: chunkHash2,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
dbBatch2, err := batchOrm.InsertBatch(context.Background(), batch2)
@@ -482,10 +454,6 @@ func testGetBatchStatusByIndex(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1, chunk2},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
batchOrm := orm.NewBatch(db)

View File

@@ -234,10 +234,6 @@ func (p *BatchProposer) proposeBatch() (*encoding.Batch, error) {
"maxL1CommitGasPerBatch", p.maxL1CommitGasPerBatch)
batch.Chunks = batch.Chunks[:len(batch.Chunks)-1]
batch.StartChunkIndex = dbChunks[0].Index
batch.EndChunkIndex = dbChunks[batch.NumChunks()-1].Index
batch.StartChunkHash = common.HexToHash(dbChunks[0].Hash)
batch.EndChunkHash = common.HexToHash(dbChunks[batch.NumChunks()-1].Hash)
totalL1CommitCalldataSize, err := codecv0.EstimateBatchL1CommitCalldataSize(&batch)
if err != nil {
@@ -286,11 +282,6 @@ func (p *BatchProposer) proposeBatch() (*encoding.Batch, error) {
p.totalL1CommitGas.Set(float64(totalL1CommitGas))
p.batchChunksNum.Set(float64(batch.NumChunks()))
batch.StartChunkIndex = dbChunks[0].Index
batch.EndChunkIndex = dbChunks[batch.NumChunks()-1].Index
batch.StartChunkHash = common.HexToHash(dbChunks[0].Hash)
batch.EndChunkHash = common.HexToHash(dbChunks[batch.NumChunks()-1].Hash)
return &batch, nil
}

View File

@@ -231,11 +231,16 @@ func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, dbTX ...
return nil, errors.New("invalid args: batch is nil")
}
numChunks := uint64(len(batch.Chunks))
if numChunks == 0 {
return nil, errors.New("invalid args: batch contains 0 chunk")
}
daBatch, err := codecv0.NewDABatch(batch)
if err != nil {
log.Error("failed to create new DA batch",
"index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", len(batch.Chunks), "err", err)
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, err
}
@@ -255,13 +260,60 @@ func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, dbTX ...
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
var startChunkIndex uint64
parentBatch, err := o.GetLatestBatch(ctx)
if err != nil {
log.Error("failed to get latest batch", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
// if parentBatch==nil then err==gorm.ErrRecordNotFound, which means there's
// no batch record in the db, we then use default empty values for the creating batch;
// if parentBatch!=nil then err==nil, then we fill the parentBatch-related data into the creating batch
if parentBatch != nil {
startChunkIndex = parentBatch.EndChunkIndex + 1
}
startDAChunk, err := codecv0.NewDAChunk(batch.Chunks[0], batch.TotalL1MessagePoppedBefore)
if err != nil {
log.Error("failed to create start DA chunk", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
startDAChunkHash, err := startDAChunk.Hash()
if err != nil {
log.Error("failed to get start DA chunk hash", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
totalL1MessagePoppedBeforeEndDAChunk := batch.TotalL1MessagePoppedBefore
for i := uint64(0); i < numChunks-1; i++ {
totalL1MessagePoppedBeforeEndDAChunk += batch.Chunks[i].NumL1Messages(totalL1MessagePoppedBeforeEndDAChunk)
}
endDAChunk, err := codecv0.NewDAChunk(batch.Chunks[numChunks-1], totalL1MessagePoppedBeforeEndDAChunk)
if err != nil {
log.Error("failed to create end DA chunk", "index", batch.Index, "total l1 message popped before", totalL1MessagePoppedBeforeEndDAChunk,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
endDAChunkHash, err := endDAChunk.Hash()
if err != nil {
log.Error("failed to get end DA chunk hash", "index", batch.Index, "total l1 message popped before", totalL1MessagePoppedBeforeEndDAChunk,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
newBatch := Batch{
Index: batch.Index,
Hash: daBatch.Hash().Hex(),
StartChunkHash: batch.StartChunkHash.Hex(),
StartChunkIndex: batch.StartChunkIndex,
EndChunkHash: batch.EndChunkHash.Hex(),
EndChunkIndex: batch.EndChunkIndex,
StartChunkHash: startDAChunkHash.Hex(),
StartChunkIndex: startChunkIndex,
EndChunkHash: endDAChunkHash.Hex(),
EndChunkIndex: startChunkIndex + numChunks - 1,
StateRoot: batch.StateRoot().Hex(),
WithdrawRoot: batch.WithdrawRoot().Hex(),
ParentBatchHash: batch.ParentBatchHash.Hex(),

View File

@@ -89,15 +89,18 @@ func (o *Chunk) GetChunksInRange(ctx context.Context, startIndex uint64, endInde
return chunks, nil
}
// GetLatestChunk retrieves the latest chunk from the database.
func (o *Chunk) GetLatestChunk(ctx context.Context) (*Chunk, error) {
// getLatestChunk retrieves the latest chunk from the database.
func (o *Chunk) getLatestChunk(ctx context.Context) (*Chunk, error) {
db := o.db.WithContext(ctx)
db = db.Model(&Chunk{})
db = db.Order("index desc")
var latestChunk Chunk
if err := db.First(&latestChunk).Error; err != nil {
return nil, fmt.Errorf("Chunk.GetLatestChunk error: %w", err)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, fmt.Errorf("Chunk.getLatestChunk error: %w", err)
}
return &latestChunk, nil
}
@@ -105,15 +108,15 @@ func (o *Chunk) GetLatestChunk(ctx context.Context) (*Chunk, error) {
// GetUnchunkedBlockHeight retrieves the first unchunked block number.
func (o *Chunk) GetUnchunkedBlockHeight(ctx context.Context) (uint64, error) {
// Get the latest chunk
latestChunk, err := o.GetLatestChunk(ctx)
latestChunk, err := o.getLatestChunk(ctx)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// if there is no chunk, return block number 1,
// because no need to chunk genesis block number
return 1, nil
}
return 0, fmt.Errorf("Chunk.GetChunkedBlockHeight error: %w", err)
}
if latestChunk == nil {
// if there is no chunk, return block number 1,
// because no need to chunk genesis block number
return 1, nil
}
return latestChunk.EndBlockNumber + 1, nil
}
@@ -146,15 +149,15 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *encoding.Chunk, dbTX ...
var totalL1MessagePoppedBefore uint64
var parentChunkHash string
var parentChunkStateRoot string
parentChunk, err := o.GetLatestChunk(ctx)
if err != nil && !errors.Is(errors.Unwrap(err), gorm.ErrRecordNotFound) {
parentChunk, err := o.getLatestChunk(ctx)
if err != nil {
log.Error("failed to get latest chunk", "err", err)
return nil, fmt.Errorf("Chunk.InsertChunk error: %w", err)
}
// if parentChunk==nil then err==gorm.ErrRecordNotFound, which means there's
// not chunk record in the db, we then use default empty values for the creating chunk;
// if parentChunk!=nil then err=nil, then we fill the parentChunk-related data into the creating chunk
// no chunk record in the db, we then use default empty values for the creating chunk;
// 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

View File

@@ -234,10 +234,6 @@ func TestBatchOrm(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1},
StartChunkIndex: 0,
StartChunkHash: chunkHash1,
EndChunkIndex: 0,
EndChunkHash: chunkHash1,
}
batch1, err := batchOrm.InsertBatch(context.Background(), batch)
assert.NoError(t, err)
@@ -255,10 +251,6 @@ func TestBatchOrm(t *testing.T) {
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk1},
StartChunkIndex: 1,
StartChunkHash: chunkHash2,
EndChunkIndex: 1,
EndChunkHash: chunkHash2,
}
batch2, err := batchOrm.InsertBatch(context.Background(), batch)
assert.NoError(t, err)

View File

@@ -12,7 +12,6 @@ import (
"scroll-tech/common/database"
"scroll-tech/common/types"
"scroll-tech/common/types/encoding"
"scroll-tech/common/types/encoding/codecv0"
"scroll-tech/rollup/internal/controller/relayer"
"scroll-tech/rollup/internal/controller/watcher"
@@ -90,20 +89,11 @@ func testImportL2GasPrice(t *testing.T) {
},
},
}
daChunk, err := codecv0.NewDAChunk(chunk, 0)
assert.NoError(t, err)
chunkHash, err := daChunk.Hash()
assert.NoError(t, err)
batch := &encoding.Batch{
Index: 0,
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
StartChunkHash: chunkHash,
EndChunkIndex: 0,
EndChunkHash: chunkHash,
}
batchOrm := orm.NewBatch(db)

View File

@@ -22,7 +22,6 @@ import (
"scroll-tech/common/database"
"scroll-tech/common/docker"
"scroll-tech/common/types/encoding"
"scroll-tech/common/types/encoding/codecv0"
"scroll-tech/common/utils"
"scroll-tech/common/version"
@@ -93,22 +92,11 @@ func TestCoordinatorProverInteraction(t *testing.T) {
RowConsumption: &gethTypes.RowConsumption{},
}
chunk := &encoding.Chunk{Blocks: []*encoding.Block{block}}
daChunk, err := codecv0.NewDAChunk(chunk, 0)
assert.NoError(t, err)
daChunkHash, err := daChunk.Hash()
assert.NoError(t, err)
batch := &encoding.Batch{
Index: 0,
TotalL1MessagePoppedBefore: 0,
ParentBatchHash: common.Hash{},
Chunks: []*encoding.Chunk{chunk},
StartChunkIndex: 0,
EndChunkIndex: 0,
StartChunkHash: daChunkHash,
EndChunkHash: daChunkHash,
}
err = l2BlockOrm.InsertL2Blocks(context.Background(), []*encoding.Block{block})

View File

@@ -73,6 +73,9 @@ func (o *Batch) GetLatestBatch(ctx context.Context) (*Batch, error) {
var latestBatch Batch
if err := db.First(&latestBatch).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, fmt.Errorf("Batch.GetLatestBatch error: %w", err)
}
return &latestBatch, nil
@@ -85,21 +88,73 @@ func (o *Batch) InsertBatch(ctx context.Context, batch *encoding.Batch, dbTX ...
return nil, errors.New("invalid args: batch is nil")
}
numChunks := uint64(len(batch.Chunks))
if numChunks == 0 {
return nil, errors.New("invalid args: batch contains 0 chunk")
}
daBatch, err := codecv0.NewDABatch(batch)
if err != nil {
log.Error("failed to create new DA batch",
"index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", len(batch.Chunks), "err", err)
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, err
}
var startChunkIndex uint64
parentBatch, err := o.GetLatestBatch(ctx)
if err != nil {
log.Error("failed to get latest batch", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
// if parentBatch==nil then err==gorm.ErrRecordNotFound, which means there's
// no batch record in the db, we then use default empty values for the creating batch;
// if parentBatch!=nil then err==nil, then we fill the parentBatch-related data into the creating batch
if parentBatch != nil {
startChunkIndex = parentBatch.EndChunkIndex + 1
}
startDAChunk, err := codecv0.NewDAChunk(batch.Chunks[0], batch.TotalL1MessagePoppedBefore)
if err != nil {
log.Error("failed to create start DA chunk", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
startDAChunkHash, err := startDAChunk.Hash()
if err != nil {
log.Error("failed to get start DA chunk hash", "index", batch.Index, "total l1 message popped before", batch.TotalL1MessagePoppedBefore,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
totalL1MessagePoppedBeforeEndDAChunk := batch.TotalL1MessagePoppedBefore
for i := uint64(0); i < numChunks-1; i++ {
totalL1MessagePoppedBeforeEndDAChunk += batch.Chunks[i].NumL1Messages(totalL1MessagePoppedBeforeEndDAChunk)
}
endDAChunk, err := codecv0.NewDAChunk(batch.Chunks[numChunks-1], totalL1MessagePoppedBeforeEndDAChunk)
if err != nil {
log.Error("failed to create end DA chunk", "index", batch.Index, "total l1 message popped before", totalL1MessagePoppedBeforeEndDAChunk,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
endDAChunkHash, err := endDAChunk.Hash()
if err != nil {
log.Error("failed to get end DA chunk hash", "index", batch.Index, "total l1 message popped before", totalL1MessagePoppedBeforeEndDAChunk,
"parent hash", batch.ParentBatchHash, "number of chunks", numChunks, "err", err)
return nil, fmt.Errorf("Batch.InsertBatch error: %w", err)
}
newBatch := Batch{
Index: batch.Index,
Hash: daBatch.Hash().Hex(),
StartChunkHash: batch.StartChunkHash.Hex(),
StartChunkIndex: batch.StartChunkIndex,
EndChunkHash: batch.EndChunkHash.Hex(),
EndChunkIndex: batch.EndChunkIndex,
StartChunkHash: startDAChunkHash.Hex(),
StartChunkIndex: startChunkIndex,
EndChunkHash: endDAChunkHash.Hex(),
EndChunkIndex: startChunkIndex + numChunks - 1,
StateRoot: batch.StateRoot().Hex(),
WithdrawRoot: batch.WithdrawRoot().Hex(),
ParentBatchHash: batch.ParentBatchHash.Hex(),

View File

@@ -63,15 +63,18 @@ func (*Chunk) TableName() string {
return "chunk"
}
// GetLatestChunk retrieves the latest chunk from the database.
func (o *Chunk) GetLatestChunk(ctx context.Context) (*Chunk, error) {
// getLatestChunk retrieves the latest chunk from the database.
func (o *Chunk) getLatestChunk(ctx context.Context) (*Chunk, error) {
db := o.db.WithContext(ctx)
db = db.Model(&Chunk{})
db = db.Order("index desc")
var latestChunk Chunk
if err := db.First(&latestChunk).Error; err != nil {
return nil, fmt.Errorf("Chunk.GetLatestChunk error: %w", err)
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, fmt.Errorf("Chunk.getLatestChunk error: %w", err)
}
return &latestChunk, nil
}
@@ -87,15 +90,15 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *encoding.Chunk, dbTX ...
var totalL1MessagePoppedBefore uint64
var parentChunkHash string
var parentChunkStateRoot string
parentChunk, err := o.GetLatestChunk(ctx)
if err != nil && !errors.Is(errors.Unwrap(err), gorm.ErrRecordNotFound) {
parentChunk, err := o.getLatestChunk(ctx)
if err != nil {
log.Error("failed to get latest chunk", "err", err)
return nil, fmt.Errorf("Chunk.InsertChunk error: %w", err)
}
// if parentChunk==nil then err==gorm.ErrRecordNotFound, which means there's
// not chunk record in the db, we then use default empty values for the creating chunk;
// if parentChunk!=nil then err=nil, then we fill the parentChunk-related data into the creating chunk
// no chunk record in the db, we then use default empty values for the creating chunk;
// 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