From e07deef7506cd4443d8aa8fb8a4c5c4bb1a0731c Mon Sep 17 00:00:00 2001 From: yperbasis Date: Mon, 23 Oct 2023 10:41:44 +0200 Subject: [PATCH] Numerical instead of lexicographic sorting of config keys --- params/config.go | 23 +++++++++++++---------- params/config_test.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/params/config.go b/params/config.go index b6d51df758..2bbbb805b0 100644 --- a/params/config.go +++ b/params/config.go @@ -750,23 +750,26 @@ func (c *BorConfig) IsSprintStart(number uint64) bool { } 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 { diff --git a/params/config_test.go b/params/config_test.go index 4011e891d2..e710561058 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -162,4 +162,18 @@ func TestBorKeyValueConfigHelper(t *testing.T) { 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)) }