From 8c71a6d22ad39f4e45a6d0608f513543911d46e3 Mon Sep 17 00:00:00 2001 From: colin <102356659+colinlyguo@users.noreply.github.com> Date: Sat, 26 Aug 2023 14:53:59 +0800 Subject: [PATCH] fix(chunk-proposer): count l1+l2 txs into chunk (#879) Co-authored-by: colinlyguo --- bridge/conf/config.json | 2 +- bridge/internal/config/l2.go | 2 +- .../controller/watcher/batch_proposer_test.go | 2 +- .../controller/watcher/chunk_proposer.go | 36 +++++++++---------- .../controller/watcher/chunk_proposer_test.go | 4 +-- bridge/internal/orm/chunk.go | 2 +- bridge/tests/rollup_test.go | 2 +- common/types/block.go | 11 ------ common/types/chunk_test.go | 4 +-- common/version/version.go | 2 +- coordinator/internal/orm/chunk.go | 2 +- tests/integration-test/orm/chunk.go | 2 +- 12 files changed, 30 insertions(+), 41 deletions(-) diff --git a/bridge/conf/config.json b/bridge/conf/config.json index 71e44d049..0e58bc18d 100644 --- a/bridge/conf/config.json +++ b/bridge/conf/config.json @@ -64,7 +64,7 @@ "finalize_sender_private_key": "1515151515151515151515151515151515151515151515151515151515151515" }, "chunk_proposer_config": { - "max_l2_tx_num_per_chunk": 1123, + "max_tx_num_per_chunk": 1123, "max_l1_commit_gas_per_chunk": 11234567, "max_l1_commit_calldata_size_per_chunk": 112345, "chunk_timeout_sec": 300, diff --git a/bridge/internal/config/l2.go b/bridge/internal/config/l2.go index f8e6382b2..099ee8e26 100644 --- a/bridge/internal/config/l2.go +++ b/bridge/internal/config/l2.go @@ -28,7 +28,7 @@ type L2Config struct { // ChunkProposerConfig loads chunk_proposer configuration items. type ChunkProposerConfig struct { - MaxL2TxNumPerChunk uint64 `json:"max_l2_tx_num_per_chunk"` + MaxTxNumPerChunk uint64 `json:"max_tx_num_per_chunk"` MaxL1CommitGasPerChunk uint64 `json:"max_l1_commit_gas_per_chunk"` MaxL1CommitCalldataSizePerChunk uint64 `json:"max_l1_commit_calldata_size_per_chunk"` ChunkTimeoutSec uint64 `json:"chunk_timeout_sec"` diff --git a/bridge/internal/controller/watcher/batch_proposer_test.go b/bridge/internal/controller/watcher/batch_proposer_test.go index 07855ef76..46fee467a 100644 --- a/bridge/internal/controller/watcher/batch_proposer_test.go +++ b/bridge/internal/controller/watcher/batch_proposer_test.go @@ -23,7 +23,7 @@ func testBatchProposer(t *testing.T) { assert.NoError(t, err) cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ - MaxL2TxNumPerChunk: 10000, + MaxTxNumPerChunk: 10000, MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitCalldataSizePerChunk: 1000000, MaxRowConsumptionPerChunk: 1048319, diff --git a/bridge/internal/controller/watcher/chunk_proposer.go b/bridge/internal/controller/watcher/chunk_proposer.go index 310da14af..112a398d9 100644 --- a/bridge/internal/controller/watcher/chunk_proposer.go +++ b/bridge/internal/controller/watcher/chunk_proposer.go @@ -55,7 +55,7 @@ type ChunkProposer struct { chunkOrm *orm.Chunk l2BlockOrm *orm.L2Block - maxL2TxNumPerChunk uint64 + maxTxNumPerChunk uint64 maxL1CommitGasPerChunk uint64 maxL1CommitCalldataSizePerChunk uint64 maxRowConsumptionPerChunk uint64 @@ -66,7 +66,7 @@ type ChunkProposer struct { proposeChunkFailureTotal prometheus.Counter proposeChunkUpdateInfoTotal prometheus.Counter proposeChunkUpdateInfoFailureTotal prometheus.Counter - chunkL2TxNum prometheus.Gauge + chunkTxNum prometheus.Gauge chunkEstimateL1CommitGas prometheus.Gauge totalL1CommitCalldataSize prometheus.Gauge totalTxGasUsed prometheus.Gauge @@ -79,7 +79,7 @@ type ChunkProposer struct { // NewChunkProposer creates a new ChunkProposer instance. func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, db *gorm.DB, reg prometheus.Registerer) *ChunkProposer { log.Debug("new chunk proposer", - "maxL2TxNumPerChunk", cfg.MaxL2TxNumPerChunk, + "maxTxNumPerChunk", cfg.MaxTxNumPerChunk, "maxL1CommitGasPerChunk", cfg.MaxL1CommitGasPerChunk, "maxL1CommitCalldataSizePerChunk", cfg.MaxL1CommitCalldataSizePerChunk, "maxRowConsumptionPerChunk", cfg.MaxRowConsumptionPerChunk, @@ -90,7 +90,7 @@ func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, db * db: db, chunkOrm: orm.NewChunk(db), l2BlockOrm: orm.NewL2Block(db), - maxL2TxNumPerChunk: cfg.MaxL2TxNumPerChunk, + maxTxNumPerChunk: cfg.MaxTxNumPerChunk, maxL1CommitGasPerChunk: cfg.MaxL1CommitGasPerChunk, maxL1CommitCalldataSizePerChunk: cfg.MaxL1CommitCalldataSizePerChunk, maxRowConsumptionPerChunk: cfg.MaxRowConsumptionPerChunk, @@ -113,9 +113,9 @@ func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, db * Name: "bridge_propose_chunk_update_info_failure_total", Help: "Total number of propose chunk update info failure total.", }), - chunkL2TxNum: promauto.With(reg).NewGauge(prometheus.GaugeOpts{ - Name: "bridge_propose_chunk_l2_tx_num", - Help: "The chunk l2 tx num", + chunkTxNum: promauto.With(reg).NewGauge(prometheus.GaugeOpts{ + Name: "bridge_propose_chunk_tx_num", + Help: "The chunk tx num", }), chunkEstimateL1CommitGas: promauto.With(reg).NewGauge(prometheus.GaugeOpts{ Name: "bridge_propose_chunk_estimate_l1_commit_gas", @@ -197,21 +197,21 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) { var chunk types.Chunk var totalTxGasUsed uint64 - var totalL2TxNum uint64 + var totalTxNum uint64 var totalL1CommitCalldataSize uint64 var totalL1CommitGas uint64 crc := chunkRowConsumption{} for i, block := range blocks { // metric values - lastTotalL2TxNum := totalL2TxNum + lastTotalTxNum := totalTxNum lastTotalL1CommitGas := totalL1CommitGas lastCrcMax := crc.max() lastTotalL1CommitCalldataSize := totalL1CommitCalldataSize lastTotalTxGasUsed := totalTxGasUsed totalTxGasUsed += block.Header.GasUsed - totalL2TxNum += block.L2TxsNum() + totalTxNum += uint64(len(block.Transactions)) totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() totalL1CommitGas = chunk.EstimateL1CommitGas() totalOverEstimateL1CommitGas := uint64(p.gasCostIncreaseMultiplier * float64(totalL1CommitGas)) @@ -220,19 +220,19 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) { } crcMax := crc.max() - if totalL2TxNum > p.maxL2TxNumPerChunk || + if totalTxNum > p.maxTxNumPerChunk || totalL1CommitCalldataSize > p.maxL1CommitCalldataSizePerChunk || totalOverEstimateL1CommitGas > p.maxL1CommitGasPerChunk || crcMax > p.maxRowConsumptionPerChunk { // Check if the first block breaks hard limits. // If so, it indicates there are bugs in sequencer, manual fix is needed. if i == 0 { - if totalL2TxNum > p.maxL2TxNumPerChunk { + if totalTxNum > p.maxTxNumPerChunk { return nil, fmt.Errorf( "the first block exceeds l2 tx number limit; block number: %v, number of transactions: %v, max transaction number limit: %v", block.Header.Number, - totalL2TxNum, - p.maxL2TxNumPerChunk, + totalTxNum, + p.maxTxNumPerChunk, ) } @@ -266,8 +266,8 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) { } log.Debug("breaking limit condition in chunking", - "totalL2TxNum", totalL2TxNum, - "maxL2TxNumPerChunk", p.maxL2TxNumPerChunk, + "totalTxNum", totalTxNum, + "maxTxNumPerChunk", p.maxTxNumPerChunk, "currentL1CommitCalldataSize", totalL1CommitCalldataSize, "maxL1CommitCalldataSizePerChunk", p.maxL1CommitCalldataSizePerChunk, "currentOverEstimateL1CommitGas", totalOverEstimateL1CommitGas, @@ -276,7 +276,7 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) { "chunkRowConsumption", crc, "p.maxRowConsumptionPerChunk", p.maxRowConsumptionPerChunk) - p.chunkL2TxNum.Set(float64(lastTotalL2TxNum)) + p.chunkTxNum.Set(float64(lastTotalTxNum)) p.chunkEstimateL1CommitGas.Set(float64(lastTotalL1CommitGas)) p.totalL1CommitCalldataSize.Set(float64(lastTotalL1CommitCalldataSize)) p.maxTxConsumption.Set(float64(lastCrcMax)) @@ -295,7 +295,7 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) { "block outdated time threshold", currentTimeSec, ) p.chunkFirstBlockTimeoutReached.Inc() - p.chunkL2TxNum.Set(float64(totalL2TxNum)) + p.chunkTxNum.Set(float64(totalTxNum)) p.chunkEstimateL1CommitGas.Set(float64(totalL1CommitGas)) p.totalL1CommitCalldataSize.Set(float64(totalL1CommitCalldataSize)) p.maxTxConsumption.Set(float64(crc.max())) diff --git a/bridge/internal/controller/watcher/chunk_proposer_test.go b/bridge/internal/controller/watcher/chunk_proposer_test.go index 4fa5b19c4..a0691f4af 100644 --- a/bridge/internal/controller/watcher/chunk_proposer_test.go +++ b/bridge/internal/controller/watcher/chunk_proposer_test.go @@ -23,7 +23,7 @@ func testChunkProposer(t *testing.T) { assert.NoError(t, err) cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ - MaxL2TxNumPerChunk: 10000, + MaxTxNumPerChunk: 10000, MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitCalldataSizePerChunk: 1000000, MaxRowConsumptionPerChunk: 1048319, @@ -53,7 +53,7 @@ func testChunkProposerRowConsumption(t *testing.T) { assert.NoError(t, err) cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ - MaxL2TxNumPerChunk: 10000, + MaxTxNumPerChunk: 10000, MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitCalldataSizePerChunk: 1000000, MaxRowConsumptionPerChunk: 0, // ! diff --git a/bridge/internal/orm/chunk.go b/bridge/internal/orm/chunk.go index 95bfb6cb3..54f7444ef 100644 --- a/bridge/internal/orm/chunk.go +++ b/bridge/internal/orm/chunk.go @@ -155,7 +155,7 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go var totalL1CommitCalldataSize uint64 for _, block := range chunk.Blocks { totalL2TxGas += block.Header.GasUsed - totalL2TxNum += block.L2TxsNum() + totalL2TxNum += block.NumL2Transactions() totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() } diff --git a/bridge/tests/rollup_test.go b/bridge/tests/rollup_test.go index f3c983954..ed8448a23 100644 --- a/bridge/tests/rollup_test.go +++ b/bridge/tests/rollup_test.go @@ -58,7 +58,7 @@ func testCommitBatchAndFinalizeBatch(t *testing.T) { assert.NoError(t, err) cp := watcher.NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ - MaxL2TxNumPerChunk: 10000, + MaxTxNumPerChunk: 10000, MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitCalldataSizePerChunk: 1000000, MaxRowConsumptionPerChunk: 1048319, diff --git a/common/types/block.go b/common/types/block.go index a823755b5..e89d423f0 100644 --- a/common/types/block.go +++ b/common/types/block.go @@ -130,17 +130,6 @@ func (w *WrappedBlock) EstimateL1CommitGas() uint64 { return total } -// L2TxsNum calculates the number of l2 txs. -func (w *WrappedBlock) L2TxsNum() uint64 { - var count uint64 - for _, txData := range w.Transactions { - if txData.Type != types.L1MessageTxType { - count++ - } - } - return count -} - func (w *WrappedBlock) getTxPayloadLength(txData *types.TransactionData) uint64 { if w.txPayloadLengthCache == nil { w.txPayloadLengthCache = make(map[string]uint64) diff --git a/common/types/chunk_test.go b/common/types/chunk_test.go index 3f8875336..43b4087ac 100644 --- a/common/types/chunk_test.go +++ b/common/types/chunk_test.go @@ -39,7 +39,7 @@ func TestChunkEncode(t *testing.T) { assert.NoError(t, json.Unmarshal(templateBlockTrace, wrappedBlock)) assert.Equal(t, uint64(0), wrappedBlock.NumL1Messages(0)) assert.Equal(t, uint64(358), wrappedBlock.EstimateL1CommitCalldataSize()) - assert.Equal(t, uint64(2), wrappedBlock.L2TxsNum()) + assert.Equal(t, uint64(2), wrappedBlock.NumL2Transactions()) chunk = &Chunk{ Blocks: []*WrappedBlock{ wrappedBlock, @@ -61,7 +61,7 @@ func TestChunkEncode(t *testing.T) { assert.NoError(t, json.Unmarshal(templateBlockTrace2, wrappedBlock2)) assert.Equal(t, uint64(11), wrappedBlock2.NumL1Messages(0)) // 0..=9 skipped, 10 included assert.Equal(t, uint64(96), wrappedBlock2.EstimateL1CommitCalldataSize()) - assert.Equal(t, uint64(1), wrappedBlock2.L2TxsNum()) + assert.Equal(t, uint64(1), wrappedBlock2.NumL2Transactions()) chunk = &Chunk{ Blocks: []*WrappedBlock{ wrappedBlock2, diff --git a/common/version/version.go b/common/version/version.go index aec3399af..6cbbf7e27 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -7,7 +7,7 @@ import ( "strings" ) -var tag = "v4.1.115" +var tag = "v4.1.116" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { diff --git a/coordinator/internal/orm/chunk.go b/coordinator/internal/orm/chunk.go index 55c9a39fd..3f8eb9014 100644 --- a/coordinator/internal/orm/chunk.go +++ b/coordinator/internal/orm/chunk.go @@ -234,7 +234,7 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go var totalL1CommitGas uint64 for _, block := range chunk.Blocks { totalL2TxGas += block.Header.GasUsed - totalL2TxNum += block.L2TxsNum() + totalL2TxNum += block.NumL2Transactions() totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() totalL1CommitGas += block.EstimateL1CommitGas() } diff --git a/tests/integration-test/orm/chunk.go b/tests/integration-test/orm/chunk.go index 0e41aa251..da65774d6 100644 --- a/tests/integration-test/orm/chunk.go +++ b/tests/integration-test/orm/chunk.go @@ -113,7 +113,7 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go var totalL1CommitGas uint64 for _, block := range chunk.Blocks { totalL2TxGas += block.Header.GasUsed - totalL2TxNum += block.L2TxsNum() + totalL2TxNum += block.NumL2Transactions() totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() totalL1CommitGas += block.EstimateL1CommitGas() }