fix(chunk-proposer): count l1+l2 txs into chunk (#879)

Co-authored-by: colinlyguo <colinlyguo@users.noreply.github.com>
This commit is contained in:
colin
2023-08-26 14:53:59 +08:00
committed by GitHub
parent 87f18efba8
commit 8c71a6d22a
12 changed files with 30 additions and 41 deletions

View File

@@ -64,7 +64,7 @@
"finalize_sender_private_key": "1515151515151515151515151515151515151515151515151515151515151515" "finalize_sender_private_key": "1515151515151515151515151515151515151515151515151515151515151515"
}, },
"chunk_proposer_config": { "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_gas_per_chunk": 11234567,
"max_l1_commit_calldata_size_per_chunk": 112345, "max_l1_commit_calldata_size_per_chunk": 112345,
"chunk_timeout_sec": 300, "chunk_timeout_sec": 300,

View File

@@ -28,7 +28,7 @@ type L2Config struct {
// ChunkProposerConfig loads chunk_proposer configuration items. // ChunkProposerConfig loads chunk_proposer configuration items.
type ChunkProposerConfig struct { 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"` MaxL1CommitGasPerChunk uint64 `json:"max_l1_commit_gas_per_chunk"`
MaxL1CommitCalldataSizePerChunk uint64 `json:"max_l1_commit_calldata_size_per_chunk"` MaxL1CommitCalldataSizePerChunk uint64 `json:"max_l1_commit_calldata_size_per_chunk"`
ChunkTimeoutSec uint64 `json:"chunk_timeout_sec"` ChunkTimeoutSec uint64 `json:"chunk_timeout_sec"`

View File

@@ -23,7 +23,7 @@ func testBatchProposer(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{
MaxL2TxNumPerChunk: 10000, MaxTxNumPerChunk: 10000,
MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitGasPerChunk: 50000000000,
MaxL1CommitCalldataSizePerChunk: 1000000, MaxL1CommitCalldataSizePerChunk: 1000000,
MaxRowConsumptionPerChunk: 1048319, MaxRowConsumptionPerChunk: 1048319,

View File

@@ -55,7 +55,7 @@ type ChunkProposer struct {
chunkOrm *orm.Chunk chunkOrm *orm.Chunk
l2BlockOrm *orm.L2Block l2BlockOrm *orm.L2Block
maxL2TxNumPerChunk uint64 maxTxNumPerChunk uint64
maxL1CommitGasPerChunk uint64 maxL1CommitGasPerChunk uint64
maxL1CommitCalldataSizePerChunk uint64 maxL1CommitCalldataSizePerChunk uint64
maxRowConsumptionPerChunk uint64 maxRowConsumptionPerChunk uint64
@@ -66,7 +66,7 @@ type ChunkProposer struct {
proposeChunkFailureTotal prometheus.Counter proposeChunkFailureTotal prometheus.Counter
proposeChunkUpdateInfoTotal prometheus.Counter proposeChunkUpdateInfoTotal prometheus.Counter
proposeChunkUpdateInfoFailureTotal prometheus.Counter proposeChunkUpdateInfoFailureTotal prometheus.Counter
chunkL2TxNum prometheus.Gauge chunkTxNum prometheus.Gauge
chunkEstimateL1CommitGas prometheus.Gauge chunkEstimateL1CommitGas prometheus.Gauge
totalL1CommitCalldataSize prometheus.Gauge totalL1CommitCalldataSize prometheus.Gauge
totalTxGasUsed prometheus.Gauge totalTxGasUsed prometheus.Gauge
@@ -79,7 +79,7 @@ type ChunkProposer struct {
// NewChunkProposer creates a new ChunkProposer instance. // NewChunkProposer creates a new ChunkProposer instance.
func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, db *gorm.DB, reg prometheus.Registerer) *ChunkProposer { func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, db *gorm.DB, reg prometheus.Registerer) *ChunkProposer {
log.Debug("new chunk proposer", log.Debug("new chunk proposer",
"maxL2TxNumPerChunk", cfg.MaxL2TxNumPerChunk, "maxTxNumPerChunk", cfg.MaxTxNumPerChunk,
"maxL1CommitGasPerChunk", cfg.MaxL1CommitGasPerChunk, "maxL1CommitGasPerChunk", cfg.MaxL1CommitGasPerChunk,
"maxL1CommitCalldataSizePerChunk", cfg.MaxL1CommitCalldataSizePerChunk, "maxL1CommitCalldataSizePerChunk", cfg.MaxL1CommitCalldataSizePerChunk,
"maxRowConsumptionPerChunk", cfg.MaxRowConsumptionPerChunk, "maxRowConsumptionPerChunk", cfg.MaxRowConsumptionPerChunk,
@@ -90,7 +90,7 @@ func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, db *
db: db, db: db,
chunkOrm: orm.NewChunk(db), chunkOrm: orm.NewChunk(db),
l2BlockOrm: orm.NewL2Block(db), l2BlockOrm: orm.NewL2Block(db),
maxL2TxNumPerChunk: cfg.MaxL2TxNumPerChunk, maxTxNumPerChunk: cfg.MaxTxNumPerChunk,
maxL1CommitGasPerChunk: cfg.MaxL1CommitGasPerChunk, maxL1CommitGasPerChunk: cfg.MaxL1CommitGasPerChunk,
maxL1CommitCalldataSizePerChunk: cfg.MaxL1CommitCalldataSizePerChunk, maxL1CommitCalldataSizePerChunk: cfg.MaxL1CommitCalldataSizePerChunk,
maxRowConsumptionPerChunk: cfg.MaxRowConsumptionPerChunk, maxRowConsumptionPerChunk: cfg.MaxRowConsumptionPerChunk,
@@ -113,9 +113,9 @@ func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, db *
Name: "bridge_propose_chunk_update_info_failure_total", Name: "bridge_propose_chunk_update_info_failure_total",
Help: "Total number of propose chunk update info failure total.", Help: "Total number of propose chunk update info failure total.",
}), }),
chunkL2TxNum: promauto.With(reg).NewGauge(prometheus.GaugeOpts{ chunkTxNum: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Name: "bridge_propose_chunk_l2_tx_num", Name: "bridge_propose_chunk_tx_num",
Help: "The chunk l2 tx num", Help: "The chunk tx num",
}), }),
chunkEstimateL1CommitGas: promauto.With(reg).NewGauge(prometheus.GaugeOpts{ chunkEstimateL1CommitGas: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Name: "bridge_propose_chunk_estimate_l1_commit_gas", Name: "bridge_propose_chunk_estimate_l1_commit_gas",
@@ -197,21 +197,21 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) {
var chunk types.Chunk var chunk types.Chunk
var totalTxGasUsed uint64 var totalTxGasUsed uint64
var totalL2TxNum uint64 var totalTxNum uint64
var totalL1CommitCalldataSize uint64 var totalL1CommitCalldataSize uint64
var totalL1CommitGas uint64 var totalL1CommitGas uint64
crc := chunkRowConsumption{} crc := chunkRowConsumption{}
for i, block := range blocks { for i, block := range blocks {
// metric values // metric values
lastTotalL2TxNum := totalL2TxNum lastTotalTxNum := totalTxNum
lastTotalL1CommitGas := totalL1CommitGas lastTotalL1CommitGas := totalL1CommitGas
lastCrcMax := crc.max() lastCrcMax := crc.max()
lastTotalL1CommitCalldataSize := totalL1CommitCalldataSize lastTotalL1CommitCalldataSize := totalL1CommitCalldataSize
lastTotalTxGasUsed := totalTxGasUsed lastTotalTxGasUsed := totalTxGasUsed
totalTxGasUsed += block.Header.GasUsed totalTxGasUsed += block.Header.GasUsed
totalL2TxNum += block.L2TxsNum() totalTxNum += uint64(len(block.Transactions))
totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize()
totalL1CommitGas = chunk.EstimateL1CommitGas() totalL1CommitGas = chunk.EstimateL1CommitGas()
totalOverEstimateL1CommitGas := uint64(p.gasCostIncreaseMultiplier * float64(totalL1CommitGas)) totalOverEstimateL1CommitGas := uint64(p.gasCostIncreaseMultiplier * float64(totalL1CommitGas))
@@ -220,19 +220,19 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) {
} }
crcMax := crc.max() crcMax := crc.max()
if totalL2TxNum > p.maxL2TxNumPerChunk || if totalTxNum > p.maxTxNumPerChunk ||
totalL1CommitCalldataSize > p.maxL1CommitCalldataSizePerChunk || totalL1CommitCalldataSize > p.maxL1CommitCalldataSizePerChunk ||
totalOverEstimateL1CommitGas > p.maxL1CommitGasPerChunk || totalOverEstimateL1CommitGas > p.maxL1CommitGasPerChunk ||
crcMax > p.maxRowConsumptionPerChunk { crcMax > p.maxRowConsumptionPerChunk {
// Check if the first block breaks hard limits. // Check if the first block breaks hard limits.
// If so, it indicates there are bugs in sequencer, manual fix is needed. // If so, it indicates there are bugs in sequencer, manual fix is needed.
if i == 0 { if i == 0 {
if totalL2TxNum > p.maxL2TxNumPerChunk { if totalTxNum > p.maxTxNumPerChunk {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"the first block exceeds l2 tx number limit; block number: %v, number of transactions: %v, max transaction number limit: %v", "the first block exceeds l2 tx number limit; block number: %v, number of transactions: %v, max transaction number limit: %v",
block.Header.Number, block.Header.Number,
totalL2TxNum, totalTxNum,
p.maxL2TxNumPerChunk, p.maxTxNumPerChunk,
) )
} }
@@ -266,8 +266,8 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) {
} }
log.Debug("breaking limit condition in chunking", log.Debug("breaking limit condition in chunking",
"totalL2TxNum", totalL2TxNum, "totalTxNum", totalTxNum,
"maxL2TxNumPerChunk", p.maxL2TxNumPerChunk, "maxTxNumPerChunk", p.maxTxNumPerChunk,
"currentL1CommitCalldataSize", totalL1CommitCalldataSize, "currentL1CommitCalldataSize", totalL1CommitCalldataSize,
"maxL1CommitCalldataSizePerChunk", p.maxL1CommitCalldataSizePerChunk, "maxL1CommitCalldataSizePerChunk", p.maxL1CommitCalldataSizePerChunk,
"currentOverEstimateL1CommitGas", totalOverEstimateL1CommitGas, "currentOverEstimateL1CommitGas", totalOverEstimateL1CommitGas,
@@ -276,7 +276,7 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) {
"chunkRowConsumption", crc, "chunkRowConsumption", crc,
"p.maxRowConsumptionPerChunk", p.maxRowConsumptionPerChunk) "p.maxRowConsumptionPerChunk", p.maxRowConsumptionPerChunk)
p.chunkL2TxNum.Set(float64(lastTotalL2TxNum)) p.chunkTxNum.Set(float64(lastTotalTxNum))
p.chunkEstimateL1CommitGas.Set(float64(lastTotalL1CommitGas)) p.chunkEstimateL1CommitGas.Set(float64(lastTotalL1CommitGas))
p.totalL1CommitCalldataSize.Set(float64(lastTotalL1CommitCalldataSize)) p.totalL1CommitCalldataSize.Set(float64(lastTotalL1CommitCalldataSize))
p.maxTxConsumption.Set(float64(lastCrcMax)) p.maxTxConsumption.Set(float64(lastCrcMax))
@@ -295,7 +295,7 @@ func (p *ChunkProposer) proposeChunk() (*types.Chunk, error) {
"block outdated time threshold", currentTimeSec, "block outdated time threshold", currentTimeSec,
) )
p.chunkFirstBlockTimeoutReached.Inc() p.chunkFirstBlockTimeoutReached.Inc()
p.chunkL2TxNum.Set(float64(totalL2TxNum)) p.chunkTxNum.Set(float64(totalTxNum))
p.chunkEstimateL1CommitGas.Set(float64(totalL1CommitGas)) p.chunkEstimateL1CommitGas.Set(float64(totalL1CommitGas))
p.totalL1CommitCalldataSize.Set(float64(totalL1CommitCalldataSize)) p.totalL1CommitCalldataSize.Set(float64(totalL1CommitCalldataSize))
p.maxTxConsumption.Set(float64(crc.max())) p.maxTxConsumption.Set(float64(crc.max()))

View File

@@ -23,7 +23,7 @@ func testChunkProposer(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{
MaxL2TxNumPerChunk: 10000, MaxTxNumPerChunk: 10000,
MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitGasPerChunk: 50000000000,
MaxL1CommitCalldataSizePerChunk: 1000000, MaxL1CommitCalldataSizePerChunk: 1000000,
MaxRowConsumptionPerChunk: 1048319, MaxRowConsumptionPerChunk: 1048319,
@@ -53,7 +53,7 @@ func testChunkProposerRowConsumption(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ cp := NewChunkProposer(context.Background(), &config.ChunkProposerConfig{
MaxL2TxNumPerChunk: 10000, MaxTxNumPerChunk: 10000,
MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitGasPerChunk: 50000000000,
MaxL1CommitCalldataSizePerChunk: 1000000, MaxL1CommitCalldataSizePerChunk: 1000000,
MaxRowConsumptionPerChunk: 0, // ! MaxRowConsumptionPerChunk: 0, // !

View File

@@ -155,7 +155,7 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go
var totalL1CommitCalldataSize uint64 var totalL1CommitCalldataSize uint64
for _, block := range chunk.Blocks { for _, block := range chunk.Blocks {
totalL2TxGas += block.Header.GasUsed totalL2TxGas += block.Header.GasUsed
totalL2TxNum += block.L2TxsNum() totalL2TxNum += block.NumL2Transactions()
totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize()
} }

View File

@@ -58,7 +58,7 @@ func testCommitBatchAndFinalizeBatch(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
cp := watcher.NewChunkProposer(context.Background(), &config.ChunkProposerConfig{ cp := watcher.NewChunkProposer(context.Background(), &config.ChunkProposerConfig{
MaxL2TxNumPerChunk: 10000, MaxTxNumPerChunk: 10000,
MaxL1CommitGasPerChunk: 50000000000, MaxL1CommitGasPerChunk: 50000000000,
MaxL1CommitCalldataSizePerChunk: 1000000, MaxL1CommitCalldataSizePerChunk: 1000000,
MaxRowConsumptionPerChunk: 1048319, MaxRowConsumptionPerChunk: 1048319,

View File

@@ -130,17 +130,6 @@ func (w *WrappedBlock) EstimateL1CommitGas() uint64 {
return total 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 { func (w *WrappedBlock) getTxPayloadLength(txData *types.TransactionData) uint64 {
if w.txPayloadLengthCache == nil { if w.txPayloadLengthCache == nil {
w.txPayloadLengthCache = make(map[string]uint64) w.txPayloadLengthCache = make(map[string]uint64)

View File

@@ -39,7 +39,7 @@ func TestChunkEncode(t *testing.T) {
assert.NoError(t, json.Unmarshal(templateBlockTrace, wrappedBlock)) assert.NoError(t, json.Unmarshal(templateBlockTrace, wrappedBlock))
assert.Equal(t, uint64(0), wrappedBlock.NumL1Messages(0)) assert.Equal(t, uint64(0), wrappedBlock.NumL1Messages(0))
assert.Equal(t, uint64(358), wrappedBlock.EstimateL1CommitCalldataSize()) assert.Equal(t, uint64(358), wrappedBlock.EstimateL1CommitCalldataSize())
assert.Equal(t, uint64(2), wrappedBlock.L2TxsNum()) assert.Equal(t, uint64(2), wrappedBlock.NumL2Transactions())
chunk = &Chunk{ chunk = &Chunk{
Blocks: []*WrappedBlock{ Blocks: []*WrappedBlock{
wrappedBlock, wrappedBlock,
@@ -61,7 +61,7 @@ func TestChunkEncode(t *testing.T) {
assert.NoError(t, json.Unmarshal(templateBlockTrace2, wrappedBlock2)) assert.NoError(t, json.Unmarshal(templateBlockTrace2, wrappedBlock2))
assert.Equal(t, uint64(11), wrappedBlock2.NumL1Messages(0)) // 0..=9 skipped, 10 included assert.Equal(t, uint64(11), wrappedBlock2.NumL1Messages(0)) // 0..=9 skipped, 10 included
assert.Equal(t, uint64(96), wrappedBlock2.EstimateL1CommitCalldataSize()) assert.Equal(t, uint64(96), wrappedBlock2.EstimateL1CommitCalldataSize())
assert.Equal(t, uint64(1), wrappedBlock2.L2TxsNum()) assert.Equal(t, uint64(1), wrappedBlock2.NumL2Transactions())
chunk = &Chunk{ chunk = &Chunk{
Blocks: []*WrappedBlock{ Blocks: []*WrappedBlock{
wrappedBlock2, wrappedBlock2,

View File

@@ -7,7 +7,7 @@ import (
"strings" "strings"
) )
var tag = "v4.1.115" var tag = "v4.1.116"
var commit = func() string { var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok { if info, ok := debug.ReadBuildInfo(); ok {

View File

@@ -234,7 +234,7 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go
var totalL1CommitGas uint64 var totalL1CommitGas uint64
for _, block := range chunk.Blocks { for _, block := range chunk.Blocks {
totalL2TxGas += block.Header.GasUsed totalL2TxGas += block.Header.GasUsed
totalL2TxNum += block.L2TxsNum() totalL2TxNum += block.NumL2Transactions()
totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize()
totalL1CommitGas += block.EstimateL1CommitGas() totalL1CommitGas += block.EstimateL1CommitGas()
} }

View File

@@ -113,7 +113,7 @@ func (o *Chunk) InsertChunk(ctx context.Context, chunk *types.Chunk, dbTX ...*go
var totalL1CommitGas uint64 var totalL1CommitGas uint64
for _, block := range chunk.Blocks { for _, block := range chunk.Blocks {
totalL2TxGas += block.Header.GasUsed totalL2TxGas += block.Header.GasUsed
totalL2TxNum += block.L2TxsNum() totalL2TxNum += block.NumL2Transactions()
totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize() totalL1CommitCalldataSize += block.EstimateL1CommitCalldataSize()
totalL1CommitGas += block.EstimateL1CommitGas() totalL1CommitGas += block.EstimateL1CommitGas()
} }