Skip to content

Commit cc64c29

Browse files
authored
feat(batch proposer): add time limit to commit batches (#323)
1 parent 780d6b3 commit cc64c29

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

bridge/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"batch_gas_threshold": 3000000,
7272
"batch_tx_num_threshold": 44,
7373
"batch_time_sec": 300,
74+
"batch_commit_time_sec": 1200,
7475
"batch_blocks_limit": 100,
7576
"commit_tx_calldata_size_limit": 200000,
7677
"public_input_config": {

bridge/config/l2_config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type BatchProposerConfig struct {
3434
BatchGasThreshold uint64 `json:"batch_gas_threshold"`
3535
// Time waited to generate a batch even if gas_threshold not met
3636
BatchTimeSec uint64 `json:"batch_time_sec"`
37+
// Time waited to commit batches before the calldata met CommitTxCalldataSizeLimit
38+
BatchCommitTimeSec uint64 `json:"batch_commit_time_sec"`
3739
// Max number of blocks in a batch
3840
BatchBlocksLimit uint64 `json:"batch_blocks_limit"`
3941
// Commit tx calldata size limit in bytes, target to cap the gas use of commit tx at 2M gas

bridge/l2/batch_proposer.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type BatchProposer struct {
6464
batchGasThreshold uint64
6565
batchTxNumThreshold uint64
6666
batchBlocksLimit uint64
67+
batchCommitTimeSec uint64
6768
commitCalldataSizeLimit uint64
6869
batchDataBufferSizeLimit uint64
6970

@@ -86,6 +87,7 @@ func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, rela
8687
batchGasThreshold: cfg.BatchGasThreshold,
8788
batchTxNumThreshold: cfg.BatchTxNumThreshold,
8889
batchBlocksLimit: cfg.BatchBlocksLimit,
90+
batchCommitTimeSec: cfg.BatchCommitTimeSec,
8991
commitCalldataSizeLimit: cfg.CommitTxCalldataSizeLimit,
9092
batchDataBufferSizeLimit: 100*cfg.CommitTxCalldataSizeLimit + 1*1024*1024, // @todo: determine the value.
9193
proofGenerationFreq: cfg.ProofGenerationFreq,
@@ -124,6 +126,7 @@ func (p *BatchProposer) Start() {
124126

125127
case <-ticker.C:
126128
p.tryProposeBatch()
129+
p.tryCommitBatches()
127130
}
128131
}
129132
}(ctx)
@@ -225,11 +228,16 @@ func (p *BatchProposer) tryProposeBatch() {
225228

226229
p.proposeBatch(blocks)
227230
}
228-
229-
p.tryCommitBatches()
230231
}
231232

232233
func (p *BatchProposer) tryCommitBatches() {
234+
p.mutex.Lock()
235+
defer p.mutex.Unlock()
236+
237+
if len(p.batchDataBuffer) == 0 {
238+
return
239+
}
240+
233241
// estimate the calldata length to determine whether to commit the pending batches
234242
index := 0
235243
commit := false
@@ -249,7 +257,7 @@ func (p *BatchProposer) tryCommitBatches() {
249257
break
250258
}
251259
}
252-
if !commit {
260+
if !commit && p.batchDataBuffer[0].Timestamp()+p.batchCommitTimeSec > uint64(time.Now().Unix()) {
253261
return
254262
}
255263

@@ -287,14 +295,13 @@ func (p *BatchProposer) proposeBatch(blocks []*types.BlockInfo) {
287295
return
288296
}
289297

290-
var (
291-
length = len(blocks)
292-
gasUsed, txNum uint64
293-
)
298+
var gasUsed, txNum uint64
299+
reachThreshold := false
294300
// add blocks into batch until reach batchGasThreshold
295301
for i, block := range blocks {
296302
if (gasUsed+block.GasUsed > p.batchGasThreshold) || (txNum+block.TxNum > p.batchTxNumThreshold) {
297303
blocks = blocks[:i]
304+
reachThreshold = true
298305
break
299306
}
300307
gasUsed += block.GasUsed
@@ -304,7 +311,7 @@ func (p *BatchProposer) proposeBatch(blocks []*types.BlockInfo) {
304311
// if too few gas gathered, but we don't want to halt, we then check the first block in the batch:
305312
// if it's not old enough we will skip proposing the batch,
306313
// otherwise we will still propose a batch
307-
if length == len(blocks) && blocks[0].BlockTimestamp+p.batchTimeSec > uint64(time.Now().Unix()) {
314+
if !reachThreshold && blocks[0].BlockTimestamp+p.batchTimeSec > uint64(time.Now().Unix()) {
308315
return
309316
}
310317

common/types/batch.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ type BatchData struct {
3939
piCfg *PublicInputHashConfig
4040
}
4141

42+
// Timestamp returns the timestamp of the first block in the BlockData.
43+
func (b *BatchData) Timestamp() uint64 {
44+
if len(b.Batch.Blocks) == 0 {
45+
return 0
46+
}
47+
return b.Batch.Blocks[0].Timestamp
48+
}
49+
4250
// Hash calculates the hash of this batch.
4351
func (b *BatchData) Hash() *common.Hash {
4452
if b.hash != nil {

common/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"runtime/debug"
66
)
77

8-
var tag = "alpha-v1.9"
8+
var tag = "alpha-v1.10"
99

1010
var commit = func() string {
1111
if info, ok := debug.ReadBuildInfo(); ok {

0 commit comments

Comments
 (0)