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 2 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.75"
var tag = "v4.4.76"

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 @@ -103,8 +103,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"`
}
39 changes: 14 additions & 25 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 @@ -41,17 +39,16 @@ type BundleProposer struct {

// 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 Down Expand Up @@ -129,8 +126,8 @@ func (p *BundleProposer) proposeBundle() error {
}

// select at most maxBlocksThisChunk blocks
maxBatchesThisBundle := p.maxBatchNumPerBundle
batches, err := p.batchOrm.GetBatchesGEIndexGECodecVersion(p.ctx, firstUnbundledBatchIndex, encoding.CodecV3, int(maxBatchesThisBundle))
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,21 +158,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 maxBlocksThisChunk to trigger chunking, because these blocks are the last blocks 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)
if uint64(len(batches)) == batchesThisBundle {
log.Info("reached 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)))
colinlyguo marked this conversation as resolved.
Show resolved Hide resolved
return p.updateDBBundleInfo(batches, codecVersion)
Expand Down
28 changes: 13 additions & 15 deletions rollup/internal/controller/watcher/bundle_proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ 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,
batchNumPerBundle: 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 +115,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 @@ -205,8 +204,7 @@ func testBundleProposerRespectHardforks(t *testing.T) {
}

bup := NewBundleProposer(context.Background(), &config.BundleProposerConfig{
MaxBatchNumPerBundle: math.MaxUint64,
BundleTimeoutSec: 0,
BatchNumPerBundle: math.MaxUint64,
}, 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: 1000000,
}, 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: 1000000,
}, chainConfig, db, nil)

cp.TryProposeChunk()
Expand Down