Skip to content

Commit

Permalink
feat: make batch proposer respect fork
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Mar 4, 2024
1 parent 8f146de commit 9c53347
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rollup/cmd/rollup_relayer/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func action(ctx *cli.Context) error {
log.Crit("failed to create chunkProposer", "config file", cfgFile, "error", err)
}

batchProposer := watcher.NewBatchProposer(subCtx, cfg.L2Config.BatchProposerConfig, db, registry)
batchProposer := watcher.NewBatchProposer(subCtx, cfg.L2Config.BatchProposerConfig, network.GenesisConfig(), db, registry)
if err != nil {
log.Crit("failed to create batchProposer", "config file", cfgFile, "error", err)
}
Expand Down
25 changes: 22 additions & 3 deletions rollup/internal/controller/watcher/batch_proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package watcher
import (
"context"
"fmt"
"slices"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/params"
"gorm.io/gorm"

"scroll-tech/common/network"
"scroll-tech/common/types"

"scroll-tech/rollup/internal/config"
Expand All @@ -30,6 +33,7 @@ type BatchProposer struct {
maxL1CommitCalldataSizePerBatch uint32
batchTimeoutSec uint64
gasCostIncreaseMultiplier float64
forkHeights []uint64

batchProposerCircleTotal prometheus.Counter
proposeBatchFailureTotal prometheus.Counter
Expand All @@ -43,13 +47,15 @@ type BatchProposer struct {
}

// NewBatchProposer creates a new BatchProposer instance.
func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, db *gorm.DB, reg prometheus.Registerer) *BatchProposer {
func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, chainCfg *params.ChainConfig, db *gorm.DB, reg prometheus.Registerer) *BatchProposer {
forkHeights := network.CollectSortedForkHeights(chainCfg)
log.Debug("new batch proposer",
"maxChunkNumPerBatch", cfg.MaxChunkNumPerBatch,
"maxL1CommitGasPerBatch", cfg.MaxL1CommitGasPerBatch,
"maxL1CommitCalldataSizePerBatch", cfg.MaxL1CommitCalldataSizePerBatch,
"batchTimeoutSec", cfg.BatchTimeoutSec,
"gasCostIncreaseMultiplier", cfg.GasCostIncreaseMultiplier)
"gasCostIncreaseMultiplier", cfg.GasCostIncreaseMultiplier,
"forkHeights", forkHeights)

return &BatchProposer{
ctx: ctx,
Expand All @@ -62,6 +68,7 @@ func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, db *
maxL1CommitCalldataSizePerBatch: cfg.MaxL1CommitCalldataSizePerBatch,
batchTimeoutSec: cfg.BatchTimeoutSec,
gasCostIncreaseMultiplier: cfg.GasCostIncreaseMultiplier,
forkHeights: forkHeights,

batchProposerCircleTotal: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "rollup_propose_batch_circle_total",
Expand Down Expand Up @@ -193,6 +200,18 @@ func (p *BatchProposer) proposeBatchChunks() ([]*orm.Chunk, *types.BatchMeta, er
totalL1CommitGas += types.CalldataNonZeroByteGas * uint64(len(parentBatch.BatchHeader)) // parent batch header in calldata
}

maxChunksThisBatch := p.maxChunkNumPerBatch
for i, chunk := range dbChunks {
// if a chunk is starting at a fork boundary, only consider earlier chunks
if i != 0 && slices.Index(p.forkHeights, chunk.StartBlockNumber) != -1 {
dbChunks = dbChunks[:i]
if uint64(len(dbChunks)) < maxChunksThisBatch {
maxChunksThisBatch = uint64(len(dbChunks))
}
break
}
}

for i, chunk := range dbChunks {
// metric values
batchMeta.TotalL1CommitGas = totalL1CommitGas
Expand Down Expand Up @@ -253,7 +272,7 @@ func (p *BatchProposer) proposeBatchChunks() ([]*orm.Chunk, *types.BatchMeta, er

currentTimeSec := uint64(time.Now().Unix())
if dbChunks[0].StartBlockTime+p.batchTimeoutSec < currentTimeSec ||
totalChunks == p.maxChunkNumPerBatch {
totalChunks == maxChunksThisBatch {
if dbChunks[0].StartBlockTime+p.batchTimeoutSec < currentTimeSec {
log.Warn("first block timeout",
"start block number", dbChunks[0].StartBlockNumber,
Expand Down
20 changes: 18 additions & 2 deletions rollup/internal/controller/watcher/batch_proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package watcher

import (
"context"
"math/big"
"testing"

"github.com/scroll-tech/go-ethereum/params"
Expand All @@ -21,6 +22,7 @@ func testBatchProposerLimits(t *testing.T) {
maxL1CommitGas uint64
maxL1CommitCalldataSize uint32
batchTimeoutSec uint64
forkBlock *big.Int
expectedBatchesLen int
expectedChunksInFirstBatch uint64 // only be checked when expectedBatchesLen > 0
}{
Expand Down Expand Up @@ -84,6 +86,16 @@ func testBatchProposerLimits(t *testing.T) {
expectedBatchesLen: 1,
expectedChunksInFirstBatch: 1,
},
{
name: "ForkBlockReached",
maxChunkNum: 10,
maxL1CommitGas: 50000000000,
maxL1CommitCalldataSize: 1000000,
batchTimeoutSec: 1000000000000,
expectedBatchesLen: 1,
expectedChunksInFirstBatch: 1,
forkBlock: big.NewInt(3),
},
}

for _, tt := range tests {
Expand All @@ -103,7 +115,9 @@ func testBatchProposerLimits(t *testing.T) {
MaxRowConsumptionPerChunk: 1000000,
ChunkTimeoutSec: 300,
GasCostIncreaseMultiplier: 1.2,
}, &params.ChainConfig{}, db, nil)
}, &params.ChainConfig{
HomesteadBlock: tt.forkBlock,
}, db, nil)
cp.TryProposeChunk() // chunk1 contains block1
cp.TryProposeChunk() // chunk2 contains block2

Expand All @@ -121,6 +135,8 @@ func testBatchProposerLimits(t *testing.T) {
MaxL1CommitCalldataSizePerBatch: tt.maxL1CommitCalldataSize,
BatchTimeoutSec: tt.batchTimeoutSec,
GasCostIncreaseMultiplier: 1.2,
}, &params.ChainConfig{
HomesteadBlock: tt.forkBlock,
}, db, nil)
bp.TryProposeBatch()

Expand Down Expand Up @@ -180,7 +196,7 @@ func testBatchCommitGasAndCalldataSizeEstimation(t *testing.T) {
MaxL1CommitCalldataSizePerBatch: 1000000,
BatchTimeoutSec: 0,
GasCostIncreaseMultiplier: 1.2,
}, db, nil)
}, &params.ChainConfig{}, db, nil)
bp.TryProposeBatch()

batchOrm := orm.NewBatch(db)
Expand Down
2 changes: 1 addition & 1 deletion rollup/tests/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func testCommitBatchAndFinalizeBatch(t *testing.T) {
MaxL1CommitGasPerBatch: 50000000000,
MaxL1CommitCalldataSizePerBatch: 1000000,
BatchTimeoutSec: 300,
}, db, nil)
}, &params.ChainConfig{}, db, nil)
bp.TryProposeBatch()

l2Relayer.ProcessPendingBatches()
Expand Down

0 comments on commit 9c53347

Please sign in to comment.