mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 22:56:43 +00:00
params: fix bor key-value config look-up (#1055)
* Retire calculateBorConfigHelper in favour of borKeyValueConfigHelper * Use borKeyValueConfigHelper for CalculateBurntContract * Numerical instead of lexicographic sorting of config keys * Add test for burntContract * fix lint --------- Co-authored-by: marcello33 <marcelloardizzone@hotmail.it> Co-authored-by: Manav Darji <manavdarji.india@gmail.com>
This commit is contained in:
parent
0133e325a6
commit
c8548c9d37
2 changed files with 61 additions and 32 deletions
|
|
@ -716,11 +716,11 @@ func (c *BorConfig) CalculateSprint(number uint64) uint64 {
|
|||
}
|
||||
|
||||
func (c *BorConfig) CalculateBackupMultiplier(number uint64) uint64 {
|
||||
return c.calculateBorConfigHelper(c.BackupMultiplier, number)
|
||||
return borKeyValueConfigHelper(c.BackupMultiplier, number)
|
||||
}
|
||||
|
||||
func (c *BorConfig) CalculatePeriod(number uint64) uint64 {
|
||||
return c.calculateBorConfigHelper(c.Period, number)
|
||||
return borKeyValueConfigHelper(c.Period, number)
|
||||
}
|
||||
|
||||
func (c *BorConfig) IsJaipur(number *big.Int) bool {
|
||||
|
|
@ -754,44 +754,29 @@ func (c *BorConfig) IsSprintStart(number uint64) bool {
|
|||
return number%c.CalculateSprint(number) == 0
|
||||
}
|
||||
|
||||
func (c *BorConfig) calculateBorConfigHelper(field map[string]uint64, number uint64) uint64 {
|
||||
keys := make([]string, 0, len(field))
|
||||
for k := range field {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for i := 0; i < len(keys)-1; i++ {
|
||||
valUint, _ := strconv.ParseUint(keys[i], 10, 64)
|
||||
valUintNext, _ := strconv.ParseUint(keys[i+1], 10, 64)
|
||||
|
||||
if number > valUint && number < valUintNext {
|
||||
return field[keys[i]]
|
||||
}
|
||||
}
|
||||
|
||||
return field[keys[len(keys)-1]]
|
||||
}
|
||||
|
||||
func borKeyValueConfigHelper[T uint64 | string](field map[string]T, number uint64) T {
|
||||
keys := make([]string, 0, len(field))
|
||||
for k := range field {
|
||||
keys = append(keys, k)
|
||||
keys := make([]uint64, 0, len(field))
|
||||
fieldUint := make(map[uint64]T)
|
||||
|
||||
for k, v := range field {
|
||||
keyUint, err := strconv.ParseUint(k, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
keys = append(keys, keyUint)
|
||||
|
||||
fieldUint[keyUint] = v
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
|
||||
|
||||
for i := 0; i < len(keys)-1; i++ {
|
||||
valUint, _ := strconv.ParseUint(keys[i], 10, 64)
|
||||
valUintNext, _ := strconv.ParseUint(keys[i+1], 10, 64)
|
||||
|
||||
if number >= valUint && number < valUintNext {
|
||||
return field[keys[i]]
|
||||
if number >= keys[i] && number < keys[i+1] {
|
||||
return fieldUint[keys[i]]
|
||||
}
|
||||
}
|
||||
|
||||
return field[keys[len(keys)-1]]
|
||||
return fieldUint[keys[len(keys)-1]]
|
||||
}
|
||||
|
||||
func (c *BorConfig) CalculateBurntContract(number uint64) string {
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"gotest.tools/assert"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/math"
|
||||
)
|
||||
|
||||
|
|
@ -134,3 +136,45 @@ func TestConfigRules(t *testing.T) {
|
|||
t.Errorf("expected %v to be shanghai", 0)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBorKeyValueConfigHelper(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
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))
|
||||
|
||||
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))
|
||||
|
||||
burntContract := map[string]string{
|
||||
"22640000": "0x70bcA57F4579f58670aB2d18Ef16e02C17553C38",
|
||||
"41824608": "0x617b94CCCC2511808A3C9478ebb96f455CF167aA",
|
||||
}
|
||||
assert.Equal(t, borKeyValueConfigHelper(burntContract, 22640000), "0x70bcA57F4579f58670aB2d18Ef16e02C17553C38")
|
||||
assert.Equal(t, borKeyValueConfigHelper(burntContract, 22640000+1), "0x70bcA57F4579f58670aB2d18Ef16e02C17553C38")
|
||||
assert.Equal(t, borKeyValueConfigHelper(burntContract, 41824608-1), "0x70bcA57F4579f58670aB2d18Ef16e02C17553C38")
|
||||
assert.Equal(t, borKeyValueConfigHelper(burntContract, 41824608), "0x617b94CCCC2511808A3C9478ebb96f455CF167aA")
|
||||
assert.Equal(t, borKeyValueConfigHelper(burntContract, 41824608+1), "0x617b94CCCC2511808A3C9478ebb96f455CF167aA")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue