@@ -64,6 +64,7 @@ type BatchProposer struct {
64
64
batchGasThreshold uint64
65
65
batchTxNumThreshold uint64
66
66
batchBlocksLimit uint64
67
+ batchCommitTimeSec uint64
67
68
commitCalldataSizeLimit uint64
68
69
batchDataBufferSizeLimit uint64
69
70
@@ -86,6 +87,7 @@ func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, rela
86
87
batchGasThreshold : cfg .BatchGasThreshold ,
87
88
batchTxNumThreshold : cfg .BatchTxNumThreshold ,
88
89
batchBlocksLimit : cfg .BatchBlocksLimit ,
90
+ batchCommitTimeSec : cfg .BatchCommitTimeSec ,
89
91
commitCalldataSizeLimit : cfg .CommitTxCalldataSizeLimit ,
90
92
batchDataBufferSizeLimit : 100 * cfg .CommitTxCalldataSizeLimit + 1 * 1024 * 1024 , // @todo: determine the value.
91
93
proofGenerationFreq : cfg .ProofGenerationFreq ,
@@ -124,6 +126,7 @@ func (p *BatchProposer) Start() {
124
126
125
127
case <- ticker .C :
126
128
p .tryProposeBatch ()
129
+ p .tryCommitBatches ()
127
130
}
128
131
}
129
132
}(ctx )
@@ -225,11 +228,16 @@ func (p *BatchProposer) tryProposeBatch() {
225
228
226
229
p .proposeBatch (blocks )
227
230
}
228
-
229
- p .tryCommitBatches ()
230
231
}
231
232
232
233
func (p * BatchProposer ) tryCommitBatches () {
234
+ p .mutex .Lock ()
235
+ defer p .mutex .Unlock ()
236
+
237
+ if len (p .batchDataBuffer ) == 0 {
238
+ return
239
+ }
240
+
233
241
// estimate the calldata length to determine whether to commit the pending batches
234
242
index := 0
235
243
commit := false
@@ -249,7 +257,7 @@ func (p *BatchProposer) tryCommitBatches() {
249
257
break
250
258
}
251
259
}
252
- if ! commit {
260
+ if ! commit && p . batchDataBuffer [ 0 ]. Timestamp () + p . batchCommitTimeSec > uint64 ( time . Now (). Unix ()) {
253
261
return
254
262
}
255
263
@@ -287,14 +295,13 @@ func (p *BatchProposer) proposeBatch(blocks []*types.BlockInfo) {
287
295
return
288
296
}
289
297
290
- var (
291
- length = len (blocks )
292
- gasUsed , txNum uint64
293
- )
298
+ var gasUsed , txNum uint64
299
+ reachThreshold := false
294
300
// add blocks into batch until reach batchGasThreshold
295
301
for i , block := range blocks {
296
302
if (gasUsed + block .GasUsed > p .batchGasThreshold ) || (txNum + block .TxNum > p .batchTxNumThreshold ) {
297
303
blocks = blocks [:i ]
304
+ reachThreshold = true
298
305
break
299
306
}
300
307
gasUsed += block .GasUsed
@@ -304,7 +311,7 @@ func (p *BatchProposer) proposeBatch(blocks []*types.BlockInfo) {
304
311
// if too few gas gathered, but we don't want to halt, we then check the first block in the batch:
305
312
// if it's not old enough we will skip proposing the batch,
306
313
// 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 ()) {
308
315
return
309
316
}
310
317
0 commit comments