Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bundle-proposer): static batch per bundle #1566

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime/debug"
)

var tag = "v4.4.76"
var tag = "v4.4.77"

var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
Expand Down
3 changes: 1 addition & 2 deletions rollup/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@
"max_uncompressed_batch_bytes_size": 634880
},
"bundle_proposer_config": {
"max_batch_num_per_bundle": 20,
"bundle_timeout_sec": 36000
"batch_num_per_bundle": 20
}
},
"db_config": {
Expand Down
3 changes: 1 addition & 2 deletions rollup/internal/config/l2.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ type BatchProposerConfig struct {

// BundleProposerConfig loads bundle_proposer configuration items.
type BundleProposerConfig struct {
MaxBatchNumPerBundle uint64 `json:"max_batch_num_per_bundle"`
BundleTimeoutSec uint64 `json:"bundle_timeout_sec"`
BatchNumPerBundle uint64 `json:"batch_num_per_bundle"`
}
47 changes: 15 additions & 32 deletions rollup/internal/controller/watcher/bundle_proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package watcher
import (
"context"
"errors"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -25,8 +24,7 @@ type BundleProposer struct {
batchOrm *orm.Batch
bundleOrm *orm.Bundle

maxBatchNumPerBundle uint64
bundleTimeoutSec uint64
batchNumPerBundle uint64
colinlyguo marked this conversation as resolved.
Show resolved Hide resolved

chainCfg *params.ChainConfig

Expand All @@ -35,23 +33,21 @@ type BundleProposer struct {
proposeBundleUpdateInfoTotal prometheus.Counter
proposeBundleUpdateInfoFailureTotal prometheus.Counter
bundleBatchesNum prometheus.Gauge
bundleFirstBlockTimeoutReached prometheus.Counter
bundleBatchesProposeNotEnoughTotal prometheus.Counter
}

// NewBundleProposer creates a new BundleProposer instance.
func NewBundleProposer(ctx context.Context, cfg *config.BundleProposerConfig, chainCfg *params.ChainConfig, db *gorm.DB, reg prometheus.Registerer) *BundleProposer {
log.Info("new bundle proposer", "bundleBatchesNum", cfg.MaxBatchNumPerBundle, "bundleTimeoutSec", cfg.BundleTimeoutSec)
log.Info("new bundle proposer", "bundleBatchesNum", cfg.BatchNumPerBundle)

p := &BundleProposer{
ctx: ctx,
db: db,
chunkOrm: orm.NewChunk(db),
batchOrm: orm.NewBatch(db),
bundleOrm: orm.NewBundle(db),
maxBatchNumPerBundle: cfg.MaxBatchNumPerBundle,
bundleTimeoutSec: cfg.BundleTimeoutSec,
chainCfg: chainCfg,
ctx: ctx,
db: db,
chunkOrm: orm.NewChunk(db),
batchOrm: orm.NewBatch(db),
bundleOrm: orm.NewBundle(db),
batchNumPerBundle: cfg.BatchNumPerBundle,
chainCfg: chainCfg,

bundleProposerCircleTotal: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "rollup_propose_bundle_circle_total",
Expand All @@ -73,10 +69,6 @@ func NewBundleProposer(ctx context.Context, cfg *config.BundleProposerConfig, ch
Name: "rollup_propose_bundle_batches_number",
Help: "The number of batches in the current bundle.",
}),
bundleFirstBlockTimeoutReached: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "rollup_propose_bundle_first_block_timeout_reached_total",
Help: "Total times the first block in a bundle reached the timeout.",
}),
bundleBatchesProposeNotEnoughTotal: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "rollup_propose_bundle_batches_propose_not_enough_total",
Help: "Total number of times there were not enough batches to propose a bundle.",
Expand Down Expand Up @@ -128,9 +120,9 @@ func (p *BundleProposer) proposeBundle() error {
return err
}

// select at most maxBlocksThisChunk blocks
maxBatchesThisBundle := p.maxBatchNumPerBundle
batches, err := p.batchOrm.GetBatchesGEIndexGECodecVersion(p.ctx, firstUnbundledBatchIndex, encoding.CodecV3, int(maxBatchesThisBundle))
// select at most batchesThisBundle batches
batchesThisBundle := p.batchNumPerBundle
batches, err := p.batchOrm.GetBatchesGEIndexGECodecVersion(p.ctx, firstUnbundledBatchIndex, encoding.CodecV3, int(batchesThisBundle))
colinlyguo marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -161,22 +153,13 @@ func (p *BundleProposer) proposeBundle() error {
currentHardfork := encoding.GetHardforkName(p.chainCfg, chunk.StartBlockNumber, chunk.StartBlockTime)
if currentHardfork != hardforkName {
batches = batches[:i]
maxBatchesThisBundle = uint64(i) // update maxBlocksThisChunk to trigger chunking, because these blocks are the last blocks before the hardfork
batchesThisBundle = uint64(i) // update batchesThisBundle as these are the last batches before the hardfork
break
}
}

if uint64(len(batches)) == maxBatchesThisBundle {
log.Info("reached maximum number of batches per bundle", "batch count", len(batches), "start batch index", batches[0].Index, "end batch index", batches[len(batches)-1].Index)
p.bundleFirstBlockTimeoutReached.Inc()
p.bundleBatchesNum.Set(float64(len(batches)))
return p.updateDBBundleInfo(batches, codecVersion)
}

currentTimeSec := uint64(time.Now().Unix())
if firstChunk.StartBlockTime+p.bundleTimeoutSec < currentTimeSec {
log.Info("first block timeout", "batch count", len(batches), "start block number", firstChunk.StartBlockNumber, "start block timestamp", firstChunk.StartBlockTime, "current time", currentTimeSec)
p.bundleFirstBlockTimeoutReached.Inc()
if uint64(len(batches)) == batchesThisBundle {
log.Info("bundle complete: reached target batch count", "batch count", len(batches), "start batch index", batches[0].Index, "end batch index", batches[len(batches)-1].Index)
p.bundleBatchesNum.Set(float64(len(batches)))
return p.updateDBBundleInfo(batches, codecVersion)
}
Expand Down
34 changes: 13 additions & 21 deletions rollup/internal/controller/watcher/bundle_proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,26 @@ import (
func testBundleProposerLimits(t *testing.T) {
tests := []struct {
name string
maxBatchNumPerBundle uint64
batchNumPerBundle uint64
bundleTimeoutSec uint64
expectedBundlesLen int
expectedBatchesInFirstBundle uint64 // only be checked when expectedBundlesLen > 0
}{
{
name: "NoLimitReached",
maxBatchNumPerBundle: math.MaxUint64,
bundleTimeoutSec: math.MaxUint32,
expectedBundlesLen: 0,
name: "NoLimitReached",
batchNumPerBundle: math.MaxUint64,
bundleTimeoutSec: math.MaxUint32,
expectedBundlesLen: 0,
},
{
name: "Timeout",
maxBatchNumPerBundle: math.MaxUint64,
bundleTimeoutSec: 0,
expectedBundlesLen: 1,
expectedBatchesInFirstBundle: 2,
},
{
name: "maxBatchNumPerBundleIs0",
maxBatchNumPerBundle: 0,
bundleTimeoutSec: math.MaxUint32,
expectedBundlesLen: 0,
name: "maxBatchNumPerBundleIs0",
batchNumPerBundle: 0,
bundleTimeoutSec: math.MaxUint32,
expectedBundlesLen: 0,
},
{
name: "maxBatchNumPerBundleIs1",
maxBatchNumPerBundle: 1,
batchNumPerBundle: 1,
bundleTimeoutSec: math.MaxUint32,
expectedBundlesLen: 1,
expectedBatchesInFirstBundle: 1,
Expand Down Expand Up @@ -115,8 +108,7 @@ func testBundleProposerLimits(t *testing.T) {
bap.TryProposeBatch() // batch2 contains chunk2

bup := NewBundleProposer(context.Background(), &config.BundleProposerConfig{
MaxBatchNumPerBundle: tt.maxBatchNumPerBundle,
BundleTimeoutSec: tt.bundleTimeoutSec,
BatchNumPerBundle: tt.batchNumPerBundle,
}, chainConfig, db, nil)

bup.TryProposeBundle()
Expand Down Expand Up @@ -144,6 +136,7 @@ func testBundleProposerRespectHardforks(t *testing.T) {
BernoulliBlock: big.NewInt(1),
CurieBlock: big.NewInt(2),
DarwinTime: func() *uint64 { t := uint64(4); return &t }(),
DarwinV2Time: func() *uint64 { t := uint64(4); return &t }(),
}

// Add genesis batch.
Expand Down Expand Up @@ -205,8 +198,7 @@ func testBundleProposerRespectHardforks(t *testing.T) {
}

bup := NewBundleProposer(context.Background(), &config.BundleProposerConfig{
MaxBatchNumPerBundle: math.MaxUint64,
BundleTimeoutSec: 0,
BatchNumPerBundle: 1,
}, chainConfig, db, nil)

for i := 0; i < 5; i++ {
Expand Down
6 changes: 2 additions & 4 deletions rollup/tests/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ func testCommitBatchAndFinalizeBatchOrBundleWithAllCodecVersions(t *testing.T) {
}, chainConfig, db, nil)

bup := watcher.NewBundleProposer(context.Background(), &config.BundleProposerConfig{
MaxBatchNumPerBundle: 1000000,
BundleTimeoutSec: 300,
BatchNumPerBundle: 2,
}, chainConfig, db, nil)

l2BlockOrm := orm.NewL2Block(db)
Expand Down Expand Up @@ -280,8 +279,7 @@ func testCommitBatchAndFinalizeBatchOrBundleCrossingAllTransitions(t *testing.T)
}, chainConfig, db, nil)

bup := watcher.NewBundleProposer(context.Background(), &config.BundleProposerConfig{
MaxBatchNumPerBundle: 1000000,
BundleTimeoutSec: 300,
BatchNumPerBundle: 1,
}, chainConfig, db, nil)

cp.TryProposeChunk()
Expand Down