From 2964e428dd1c3c659ef23c3836f795ce351687f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20IRMAK?= Date: Mon, 4 Mar 2024 19:14:13 +0300 Subject: [PATCH] eliminate dependency on go1.21 --- common/network/network.go | 19 +++++++++++++------ common/network/network_test.go | 9 +++++++-- .../controller/watcher/batch_proposer.go | 9 ++++----- .../controller/watcher/chunk_proposer.go | 2 +- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/common/network/network.go b/common/network/network.go index 1a85ee15cd..d1b144f699 100644 --- a/common/network/network.go +++ b/common/network/network.go @@ -3,7 +3,7 @@ package network import ( "fmt" "math/big" - "slices" + "sort" "github.com/scroll-tech/go-ethereum/params" ) @@ -40,8 +40,8 @@ func (n Network) GenesisConfig() *params.ChainConfig { } // CollectSortedForkHeights returns a sorted set of block numbers that one or more forks are activated on -func CollectSortedForkHeights(config *params.ChainConfig) []uint64 { - var forkHeights []uint64 +func CollectSortedForkHeights(config *params.ChainConfig) ([]uint64, map[uint64]bool) { + forkHeightsMap := make(map[uint64]bool) for _, fork := range []*big.Int{ config.HomesteadBlock, config.DAOForkBlock, @@ -62,9 +62,16 @@ func CollectSortedForkHeights(config *params.ChainConfig) []uint64 { if fork == nil { continue } else if height := fork.Uint64(); height != 0 { - forkHeights = append(forkHeights, height) + forkHeightsMap[height] = true } } - slices.Sort(forkHeights) - return slices.Compact(forkHeights) // Remove duplicates + + var forkHeights []uint64 + for height := range forkHeightsMap { + forkHeights = append(forkHeights, height) + } + sort.Slice(forkHeights, func(i, j int) bool { + return forkHeights[i] < forkHeights[j] + }) + return forkHeights, forkHeightsMap } diff --git a/common/network/network_test.go b/common/network/network_test.go index 3cbe93ca85..4becaa4abe 100644 --- a/common/network/network_test.go +++ b/common/network/network_test.go @@ -9,13 +9,18 @@ import ( ) func TestCollectSortedForkBlocks(t *testing.T) { - require.Equal(t, CollectSortedForkHeights(¶ms.ChainConfig{ + l, m := CollectSortedForkHeights(¶ms.ChainConfig{ EIP155Block: big.NewInt(4), EIP158Block: big.NewInt(3), ByzantiumBlock: big.NewInt(3), ConstantinopleBlock: big.NewInt(0), - }), []uint64{ + }) + require.Equal(t, l, []uint64{ 3, 4, }) + require.Equal(t, map[uint]bool{ + 3: true, + 4: true, + }, m) } diff --git a/rollup/internal/controller/watcher/batch_proposer.go b/rollup/internal/controller/watcher/batch_proposer.go index d07ec1ff55..5e4e378833 100644 --- a/rollup/internal/controller/watcher/batch_proposer.go +++ b/rollup/internal/controller/watcher/batch_proposer.go @@ -3,7 +3,6 @@ package watcher import ( "context" "fmt" - "slices" "time" "github.com/prometheus/client_golang/prometheus" @@ -33,7 +32,7 @@ type BatchProposer struct { maxL1CommitCalldataSizePerBatch uint32 batchTimeoutSec uint64 gasCostIncreaseMultiplier float64 - forkHeights []uint64 + forkMap map[uint64]bool batchProposerCircleTotal prometheus.Counter proposeBatchFailureTotal prometheus.Counter @@ -48,7 +47,7 @@ type BatchProposer struct { // NewBatchProposer creates a new BatchProposer instance. func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, chainCfg *params.ChainConfig, db *gorm.DB, reg prometheus.Registerer) *BatchProposer { - forkHeights := network.CollectSortedForkHeights(chainCfg) + forkHeights, forkMap := network.CollectSortedForkHeights(chainCfg) log.Debug("new batch proposer", "maxChunkNumPerBatch", cfg.MaxChunkNumPerBatch, "maxL1CommitGasPerBatch", cfg.MaxL1CommitGasPerBatch, @@ -68,7 +67,7 @@ func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, chai maxL1CommitCalldataSizePerBatch: cfg.MaxL1CommitCalldataSizePerBatch, batchTimeoutSec: cfg.BatchTimeoutSec, gasCostIncreaseMultiplier: cfg.GasCostIncreaseMultiplier, - forkHeights: forkHeights, + forkMap: forkMap, batchProposerCircleTotal: promauto.With(reg).NewCounter(prometheus.CounterOpts{ Name: "rollup_propose_batch_circle_total", @@ -203,7 +202,7 @@ func (p *BatchProposer) proposeBatchChunks() ([]*orm.Chunk, *types.BatchMeta, er maxChunksThisBatch := p.maxChunkNumPerBatch for i, chunk := range dbChunks { // if a chunk is starting at a fork boundary, only consider earlier chunks - if i != 0 && slices.Index(p.forkHeights, chunk.StartBlockNumber) != -1 { + if i != 0 && p.forkMap[chunk.StartBlockNumber] { dbChunks = dbChunks[:i] if uint64(len(dbChunks)) < maxChunksThisBatch { maxChunksThisBatch = uint64(len(dbChunks)) diff --git a/rollup/internal/controller/watcher/chunk_proposer.go b/rollup/internal/controller/watcher/chunk_proposer.go index a6833ed336..81e943e0f6 100644 --- a/rollup/internal/controller/watcher/chunk_proposer.go +++ b/rollup/internal/controller/watcher/chunk_proposer.go @@ -78,7 +78,7 @@ type ChunkProposer struct { // NewChunkProposer creates a new ChunkProposer instance. func NewChunkProposer(ctx context.Context, cfg *config.ChunkProposerConfig, chainCfg *params.ChainConfig, db *gorm.DB, reg prometheus.Registerer) *ChunkProposer { - forkHeights := network.CollectSortedForkHeights(chainCfg) + forkHeights, _ := network.CollectSortedForkHeights(chainCfg) log.Debug("new chunk proposer", "maxTxNumPerChunk", cfg.MaxTxNumPerChunk, "maxL1CommitGasPerChunk", cfg.MaxL1CommitGasPerChunk,