From ba25a32d7bea37c8f183619d44aecacdf5b4620b Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:04:53 +0100 Subject: [PATCH] BorKeyValueConfigHelper -> ConfigValueLookup (#13450) --- erigon-lib/chain/chain_config.go | 9 ++++-- erigon-lib/chain/chain_config_test.go | 44 +++++++++++++-------------- polygon/bor/borcfg/bor_config.go | 30 +++--------------- 3 files changed, 34 insertions(+), 49 deletions(-) diff --git a/erigon-lib/chain/chain_config.go b/erigon-lib/chain/chain_config.go index 96fbfc7666f..f9073cd85f2 100644 --- a/erigon-lib/chain/chain_config.go +++ b/erigon-lib/chain/chain_config.go @@ -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 } @@ -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) diff --git a/erigon-lib/chain/chain_config_test.go b/erigon-lib/chain/chain_config_test.go index 3896fc2fef9..406610b941c 100644 --- a/erigon-lib/chain/chain_config_test.go +++ b/erigon-lib/chain/chain_config_test.go @@ -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") @@ -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) } diff --git a/polygon/bor/borcfg/bor_config.go b/polygon/bor/borcfg/bor_config.go index fba29adfa5e..28fbaa2f005 100644 --- a/polygon/bor/borcfg/bor_config.go +++ b/polygon/bor/borcfg/bor_config.go @@ -21,6 +21,7 @@ import ( "sort" "strconv" + "github.com/erigontech/erigon-lib/chain" "github.com/erigontech/erigon-lib/common" ) @@ -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 { @@ -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. @@ -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 }