Skip to content

Commit

Permalink
eliminate dependency on go1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Mar 4, 2024
1 parent 1c03ff0 commit e3ce0b8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
19 changes: 13 additions & 6 deletions common/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package network
import (
"fmt"
"math/big"
"slices"
"sort"

"github.com/scroll-tech/go-ethereum/params"
)
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
9 changes: 7 additions & 2 deletions common/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (
)

func TestCollectSortedForkBlocks(t *testing.T) {
require.Equal(t, CollectSortedForkHeights(&params.ChainConfig{
l, m := CollectSortedForkHeights(&params.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[uint64]bool{
3: true,
4: true,
}, m)
}
9 changes: 4 additions & 5 deletions rollup/internal/controller/watcher/batch_proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package watcher
import (
"context"
"fmt"
"slices"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion rollup/internal/controller/watcher/chunk_proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e3ce0b8

Please sign in to comment.