Skip to content

Commit

Permalink
bor: remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
anshalshukla committed Oct 20, 2023
1 parent 9f44579 commit b2a1f18
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -754,48 +754,33 @@ 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]]
}
}

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.
Expand Down

0 comments on commit b2a1f18

Please sign in to comment.