From 451ad77d240d85427a8670683049b8f4be280aa9 Mon Sep 17 00:00:00 2001 From: Haichen Shen Date: Fri, 24 Feb 2023 18:39:27 -0800 Subject: [PATCH 1/7] Add time limit to commit batches --- bridge/config.json | 1 + bridge/config/l2_config.go | 2 ++ bridge/l2/batch_proposer.go | 11 ++++++++--- common/types/batch.go | 4 ++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bridge/config.json b/bridge/config.json index d880cd5581..7abcfd51a8 100644 --- a/bridge/config.json +++ b/bridge/config.json @@ -71,6 +71,7 @@ "batch_gas_threshold": 3000000, "batch_tx_num_threshold": 44, "batch_time_sec": 300, + "batch_commit_time_sec": 1200, "batch_blocks_limit": 100, "commit_tx_calldata_size_limit": 200000, "public_input_config": { diff --git a/bridge/config/l2_config.go b/bridge/config/l2_config.go index 212c8d8d36..234ce3cbc4 100644 --- a/bridge/config/l2_config.go +++ b/bridge/config/l2_config.go @@ -34,6 +34,8 @@ type BatchProposerConfig struct { BatchGasThreshold uint64 `json:"batch_gas_threshold"` // Time waited to generate a batch even if gas_threshold not met BatchTimeSec uint64 `json:"batch_time_sec"` + // Time waited to commit batches before the calldata met CommitTxCalldataSizeLimit + BatchCommitTimeSec uint64 `json:"batch_commit_time_sec"` // Max number of blocks in a batch BatchBlocksLimit uint64 `json:"batch_blocks_limit"` // Commit tx calldata size limit in bytes, target to cap the gas use of commit tx at 2M gas diff --git a/bridge/l2/batch_proposer.go b/bridge/l2/batch_proposer.go index 38c6e589da..c9efdc2307 100644 --- a/bridge/l2/batch_proposer.go +++ b/bridge/l2/batch_proposer.go @@ -64,6 +64,7 @@ type BatchProposer struct { batchGasThreshold uint64 batchTxNumThreshold uint64 batchBlocksLimit uint64 + batchCommitTimeSec uint64 commitCalldataSizeLimit uint64 batchDataBufferSizeLimit uint64 @@ -86,6 +87,7 @@ func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, rela batchGasThreshold: cfg.BatchGasThreshold, batchTxNumThreshold: cfg.BatchTxNumThreshold, batchBlocksLimit: cfg.BatchBlocksLimit, + batchCommitTimeSec: cfg.BatchCommitTimeSec, commitCalldataSizeLimit: cfg.CommitTxCalldataSizeLimit, batchDataBufferSizeLimit: 100*cfg.CommitTxCalldataSizeLimit + 1*1024*1024, // @todo: determine the value. proofGenerationFreq: cfg.ProofGenerationFreq, @@ -124,6 +126,7 @@ func (p *BatchProposer) Start() { case <-ticker.C: p.tryProposeBatch() + p.tryCommitBatches() } } }(ctx) @@ -225,11 +228,13 @@ func (p *BatchProposer) tryProposeBatch() { p.proposeBatch(blocks) } - - p.tryCommitBatches() } func (p *BatchProposer) tryCommitBatches() { + if len(p.batchDataBuffer) == 0 { + return + } + // estimate the calldata length to determine whether to commit the pending batches index := 0 commit := false @@ -249,7 +254,7 @@ func (p *BatchProposer) tryCommitBatches() { break } } - if !commit { + if !commit && p.batchDataBuffer[0].Timestamp()+p.batchCommitTimeSec > uint64(time.Now().Unix()) { return } diff --git a/common/types/batch.go b/common/types/batch.go index 106b9d358c..7f7170b5b4 100644 --- a/common/types/batch.go +++ b/common/types/batch.go @@ -39,6 +39,10 @@ type BatchData struct { piCfg *PublicInputHashConfig } +func (b *BatchData) Timestamp() uint64 { + return b.Batch.Blocks[0].Timestamp +} + // Hash calculates the hash of this batch. func (b *BatchData) Hash() *common.Hash { if b.hash != nil { From 692c643d5c60ca32b3d748686443ffb1afdd4f42 Mon Sep 17 00:00:00 2001 From: Haichen Shen Date: Fri, 24 Feb 2023 18:41:36 -0800 Subject: [PATCH 2/7] update version --- common/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/version/version.go b/common/version/version.go index fb63c5d642..08c7b9bf7e 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "alpha-v1.9" +var tag = "alpha-v1.10" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { From b22595cb20e6c55088b9ea8b1d5f1516748f8b1b Mon Sep 17 00:00:00 2001 From: Haichen Shen Date: Fri, 24 Feb 2023 22:41:47 -0800 Subject: [PATCH 3/7] add doc --- common/types/batch.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/types/batch.go b/common/types/batch.go index 7f7170b5b4..09cd58105d 100644 --- a/common/types/batch.go +++ b/common/types/batch.go @@ -39,7 +39,11 @@ type BatchData struct { piCfg *PublicInputHashConfig } +// Timestamp returns the timestamp of the first block in the BlockData. func (b *BatchData) Timestamp() uint64 { + if len(b.Batch.Blocks) == 0 { + return 0 + } return b.Batch.Blocks[0].Timestamp } From 32d72953fb321868dba5b653e3dbdfb229ffdadc Mon Sep 17 00:00:00 2001 From: Haichen Shen Date: Fri, 24 Feb 2023 22:50:24 -0800 Subject: [PATCH 4/7] add lock --- bridge/l2/batch_proposer.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bridge/l2/batch_proposer.go b/bridge/l2/batch_proposer.go index c9efdc2307..41441e0af8 100644 --- a/bridge/l2/batch_proposer.go +++ b/bridge/l2/batch_proposer.go @@ -231,6 +231,9 @@ func (p *BatchProposer) tryProposeBatch() { } func (p *BatchProposer) tryCommitBatches() { + p.mutex.Lock() + defer p.mutex.Unlock() + if len(p.batchDataBuffer) == 0 { return } From ea28851e2884090607f188c24697ae9c850f53c2 Mon Sep 17 00:00:00 2001 From: Haichen Shen Date: Fri, 24 Feb 2023 23:07:23 -0800 Subject: [PATCH 5/7] fix batch propose logic --- bridge/l2/batch_proposer.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bridge/l2/batch_proposer.go b/bridge/l2/batch_proposer.go index 41441e0af8..092c6d3a50 100644 --- a/bridge/l2/batch_proposer.go +++ b/bridge/l2/batch_proposer.go @@ -295,14 +295,13 @@ func (p *BatchProposer) proposeBatch(blocks []*types.BlockInfo) { return } - var ( - length = len(blocks) - gasUsed, txNum uint64 - ) + var gasUsed, txNum uint64 + reachThreshold := false // add blocks into batch until reach batchGasThreshold for i, block := range blocks { if (gasUsed+block.GasUsed > p.batchGasThreshold) || (txNum+block.TxNum > p.batchTxNumThreshold) { blocks = blocks[:i] + reachThreshold = true break } gasUsed += block.GasUsed @@ -312,7 +311,7 @@ func (p *BatchProposer) proposeBatch(blocks []*types.BlockInfo) { // if too few gas gathered, but we don't want to halt, we then check the first block in the batch: // if it's not old enough we will skip proposing the batch, // otherwise we will still propose a batch - if length == len(blocks) && blocks[0].BlockTimestamp+p.batchTimeSec > uint64(time.Now().Unix()) { + if !reachThreshold && blocks[0].BlockTimestamp+p.batchTimeSec > uint64(time.Now().Unix()) { return } From e52374e34f2888b1c11d058ced4dc5be7a91c5cb Mon Sep 17 00:00:00 2001 From: Haichen Shen Date: Fri, 24 Feb 2023 23:59:46 -0800 Subject: [PATCH 6/7] trigger ci From e1b3d60727dc42296be13d3b075d359aa3b642f4 Mon Sep 17 00:00:00 2001 From: Haichen Shen Date: Sat, 25 Feb 2023 00:06:05 -0800 Subject: [PATCH 7/7] trigger ci