Skip to content

Commit

Permalink
BorKeyValueConfigHelper -> ConfigValueLookup (#13450)
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis authored Jan 15, 2025
1 parent fa5e176 commit ba25a32
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
9 changes: 7 additions & 2 deletions erigon-lib/chain/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (c *Config) GetBurntContract(num uint64) *common.Address {
if len(c.BurntContract) == 0 {
return nil
}
addr := borKeyValueConfigHelper(c.BurntContract, num)
addr := ConfigValueLookup(c.BurntContract, num)
return &addr
}

Expand Down Expand Up @@ -482,7 +482,12 @@ func (c *CliqueConfig) String() string {
return "clique"
}

func borKeyValueConfigHelper[T uint64 | common.Address](field map[string]T, number uint64) T {
// Looks up a config value as of a given block number (or time).
// The assumption here is that config is a càdlàg map of starting_from_block -> value.
// For example, config of {"0": "0xA", "10": "0xB", "20": "0xC"}
// means that the config value is 0xA for blocks 0–9,
// 0xB for blocks 10–19, and 0xC for block 20 and above.
func ConfigValueLookup[T uint64 | common.Address](field map[string]T, number uint64) T {
fieldUint := make(map[uint64]T)
for k, v := range field {
keyUint, err := strconv.ParseUint(k, 10, 64)
Expand Down
44 changes: 22 additions & 22 deletions erigon-lib/chain/chain_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@ import (
"github.com/erigontech/erigon-lib/common"
)

func TestBorKeyValueConfigHelper(t *testing.T) {
func TestConfigValueLookup(t *testing.T) {
backupMultiplier := map[string]uint64{
"0": 2,
"25275000": 5,
"29638656": 2,
}
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 0), uint64(2))
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 1), uint64(2))
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 25275000-1), uint64(2))
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 25275000), uint64(5))
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 25275000+1), uint64(5))
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 29638656-1), uint64(5))
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 29638656), uint64(2))
assert.Equal(t, borKeyValueConfigHelper(backupMultiplier, 29638656+1), uint64(2))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 0), uint64(2))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 1), uint64(2))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 25275000-1), uint64(2))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 25275000), uint64(5))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 25275000+1), uint64(5))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 29638656-1), uint64(5))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 29638656), uint64(2))
assert.Equal(t, ConfigValueLookup(backupMultiplier, 29638656+1), uint64(2))

config := map[string]uint64{
"0": 1,
"90000000": 2,
"100000000": 3,
}
assert.Equal(t, borKeyValueConfigHelper(config, 0), uint64(1))
assert.Equal(t, borKeyValueConfigHelper(config, 1), uint64(1))
assert.Equal(t, borKeyValueConfigHelper(config, 90000000-1), uint64(1))
assert.Equal(t, borKeyValueConfigHelper(config, 90000000), uint64(2))
assert.Equal(t, borKeyValueConfigHelper(config, 90000000+1), uint64(2))
assert.Equal(t, borKeyValueConfigHelper(config, 100000000-1), uint64(2))
assert.Equal(t, borKeyValueConfigHelper(config, 100000000), uint64(3))
assert.Equal(t, borKeyValueConfigHelper(config, 100000000+1), uint64(3))
assert.Equal(t, ConfigValueLookup(config, 0), uint64(1))
assert.Equal(t, ConfigValueLookup(config, 1), uint64(1))
assert.Equal(t, ConfigValueLookup(config, 90000000-1), uint64(1))
assert.Equal(t, ConfigValueLookup(config, 90000000), uint64(2))
assert.Equal(t, ConfigValueLookup(config, 90000000+1), uint64(2))
assert.Equal(t, ConfigValueLookup(config, 100000000-1), uint64(2))
assert.Equal(t, ConfigValueLookup(config, 100000000), uint64(3))
assert.Equal(t, ConfigValueLookup(config, 100000000+1), uint64(3))

address1 := common.HexToAddress("0x70bcA57F4579f58670aB2d18Ef16e02C17553C38")
address2 := common.HexToAddress("0x617b94CCCC2511808A3C9478ebb96f455CF167aA")
Expand All @@ -60,9 +60,9 @@ func TestBorKeyValueConfigHelper(t *testing.T) {
"22640000": address1,
"41874000": address2,
}
assert.Equal(t, borKeyValueConfigHelper(burntContract, 22640000), address1)
assert.Equal(t, borKeyValueConfigHelper(burntContract, 22640000+1), address1)
assert.Equal(t, borKeyValueConfigHelper(burntContract, 41874000-1), address1)
assert.Equal(t, borKeyValueConfigHelper(burntContract, 41874000), address2)
assert.Equal(t, borKeyValueConfigHelper(burntContract, 41874000+1), address2)
assert.Equal(t, ConfigValueLookup(burntContract, 22640000), address1)
assert.Equal(t, ConfigValueLookup(burntContract, 22640000+1), address1)
assert.Equal(t, ConfigValueLookup(burntContract, 41874000-1), address1)
assert.Equal(t, ConfigValueLookup(burntContract, 41874000), address2)
assert.Equal(t, ConfigValueLookup(burntContract, 41874000+1), address2)
}
30 changes: 5 additions & 25 deletions polygon/bor/borcfg/bor_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sort"
"strconv"

"github.com/erigontech/erigon-lib/chain"
"github.com/erigontech/erigon-lib/common"
)

Expand Down Expand Up @@ -53,7 +54,7 @@ func (c *BorConfig) String() string {
}

func (c *BorConfig) CalculateProducerDelay(number uint64) uint64 {
return borKeyValueConfigHelper(c.ProducerDelay, number)
return chain.ConfigValueLookup(c.ProducerDelay, number)
}

func (c *BorConfig) IsSprintStart(number uint64) bool {
Expand Down Expand Up @@ -111,11 +112,11 @@ func (c *BorConfig) CalculateSprintNumber(number uint64) uint64 {
}

func (c *BorConfig) CalculateBackupMultiplier(number uint64) uint64 {
return borKeyValueConfigHelper(c.BackupMultiplier, number)
return chain.ConfigValueLookup(c.BackupMultiplier, number)
}

func (c *BorConfig) CalculatePeriod(number uint64) uint64 {
return borKeyValueConfigHelper(c.Period, number)
return chain.ConfigValueLookup(c.Period, number)
}

// isForked returns whether a fork scheduled at block s is active at the given head block.
Expand Down Expand Up @@ -164,34 +165,13 @@ func (c *BorConfig) GetNapoliBlock() *big.Int {
}

func (c *BorConfig) CalculateStateSyncDelay(number uint64) uint64 {
return borKeyValueConfigHelper(c.StateSyncConfirmationDelay, number)
return chain.ConfigValueLookup(c.StateSyncConfirmationDelay, number)
}

func (c *BorConfig) StateReceiverContractAddress() common.Address {
return common.HexToAddress(c.StateReceiverContract)
}

func borKeyValueConfigHelper[T uint64 | common.Address](field map[string]T, number uint64) T {
fieldUint := make(map[uint64]T)
for k, v := range field {
keyUint, err := strconv.ParseUint(k, 10, 64)
if err != nil {
panic(err)
}
fieldUint[keyUint] = v
}

keys := common.SortedKeys(fieldUint)

for i := 0; i < len(keys)-1; i++ {
if number >= keys[i] && number < keys[i+1] {
return fieldUint[keys[i]]
}
}

return fieldUint[keys[len(keys)-1]]
}

type sprint struct {
from, size uint64
}
Expand Down

0 comments on commit ba25a32

Please sign in to comment.