From b2a1f18ae04cf1963327168eed88d7139adee2bf Mon Sep 17 00:00:00 2001 From: Anshal Shukla Date: Fri, 20 Oct 2023 15:07:04 +0530 Subject: [PATCH] bor: remove redundant code --- params/config.go | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/params/config.go b/params/config.go index 078e903176..bd0a1a8599 100644 --- a/params/config.go +++ b/params/config.go @@ -708,19 +708,19 @@ func (b *BorConfig) String() string { } func (c *BorConfig) CalculateProducerDelay(number uint64) uint64 { - return borKeyValueConfigHelper(c.ProducerDelay, number) + return calculateBorConfigHelper(c.ProducerDelay, number) } func (c *BorConfig) CalculateSprint(number uint64) uint64 { - return borKeyValueConfigHelper(c.Sprint, number) + return calculateBorConfigHelper(c.Sprint, number) } func (c *BorConfig) CalculateBackupMultiplier(number uint64) uint64 { - return c.calculateBorConfigHelper(c.BackupMultiplier, number) + return calculateBorConfigHelper(c.BackupMultiplier, number) } func (c *BorConfig) CalculatePeriod(number uint64) uint64 { - return c.calculateBorConfigHelper(c.Period, number) + return calculateBorConfigHelper(c.Period, number) } func (c *BorConfig) IsJaipur(number *big.Int) bool { @@ -736,7 +736,7 @@ func (c *BorConfig) IsIndore(number *big.Int) bool { } func (c *BorConfig) CalculateStateSyncDelay(number uint64) uint64 { - return borKeyValueConfigHelper(c.StateSyncConfirmationDelay, number) + return calculateBorConfigHelper(c.StateSyncConfirmationDelay, number) } // TODO: modify this function once the block number is finalized @@ -754,19 +754,14 @@ 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) +func calculateBorConfigHelper[T uint64 | string](field map[string]T, number uint64) T { + keys := sortMapKeys(field) 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 { + if number >= valUint && number < valUintNext { return field[keys[i]] } } @@ -774,28 +769,18 @@ func (c *BorConfig) calculateBorConfigHelper(field map[string]uint64, number uin 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 { +func sortMapKeys[T uint64 | string](m map[string]T) []string { + keys := make([]string, 0, len(m)) + for k := range m { 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]] + return keys } func (c *BorConfig) CalculateBurntContract(number uint64) string { - return borKeyValueConfigHelper(c.BurntContract, number) + return calculateBorConfigHelper(c.BurntContract, number) } // Description returns a human-readable description of ChainConfig.