Skip to content

Commit

Permalink
chore: use math.LegacyDec
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Aug 8, 2024
1 parent 6ed24a1 commit b2fb256
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
3 changes: 1 addition & 2 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package converter
import (
"fmt"
"main/pkg/types"
"math/big"

"github.com/cosmos/cosmos-sdk/codec"
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -81,7 +80,7 @@ func (c *Converter) ValidatorFromCosmosValidator(
OperatorAddress: validator.OperatorAddress,
Jailed: validator.Jailed,
SigningInfo: valSigningInfo,
VotingPower: big.NewFloat(0).SetInt(validator.DelegatorShares.BigInt()),
VotingPower: validator.DelegatorShares,
}
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/types/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package types

import (
"main/pkg/utils"
"math/big"
"sort"

"cosmossdk.io/math"
)

type Entry struct {
Expand Down Expand Up @@ -50,12 +51,12 @@ func (e Entries) GetActive() []*Entry {
return activeValidators
}

func (e Entries) GetTotalVotingPower() *big.Float {
sum := big.NewFloat(0)
func (e Entries) GetTotalVotingPower() math.LegacyDec {
sum := math.LegacyZeroDec()

for _, entry := range e {
if entry.IsActive {
sum.Add(sum, entry.Validator.VotingPower)
sum = sum.Add(entry.Validator.VotingPower)
}
}

Expand All @@ -69,12 +70,12 @@ func (e Entries) SetVotingPowerPercent() {

// sorting by voting power desc
sort.Slice(activeAndSortedEntries, func(first, second int) bool {
return activeAndSortedEntries[first].Validator.VotingPower.Cmp(activeAndSortedEntries[second].Validator.VotingPower) > 0
return activeAndSortedEntries[first].Validator.VotingPower.GT(activeAndSortedEntries[second].Validator.VotingPower)
})

var cumulativeVotingPowerPercent float64 = 0
for index, sortedEntry := range activeAndSortedEntries {
percent, _ := new(big.Float).Quo(sortedEntry.Validator.VotingPower, totalVP).Float64()
percent := sortedEntry.Validator.VotingPower.Quo(totalVP).MustFloat64()

entry := e[sortedEntry.Validator.OperatorAddress]

Expand Down
17 changes: 9 additions & 8 deletions pkg/types/entry_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types

import (
"math/big"
"testing"

"cosmossdk.io/math"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -47,20 +48,20 @@ func TestValidatorsGetTotalVotingPower(t *testing.T) {
entries := Entries{
"firstaddr": {
IsActive: true,
Validator: &Validator{Moniker: "first", OperatorAddress: "firstaddr", VotingPower: big.NewFloat(1)},
Validator: &Validator{Moniker: "first", OperatorAddress: "firstaddr", VotingPower: math.LegacyNewDec(1)},
},
"secondaddr": {
IsActive: true,
Validator: &Validator{Moniker: "second", OperatorAddress: "secondaddr", VotingPower: big.NewFloat(2)},
Validator: &Validator{Moniker: "second", OperatorAddress: "secondaddr", VotingPower: math.LegacyNewDec(2)},
},
"thirdaddr": {
IsActive: false,
Validator: &Validator{Moniker: "third", OperatorAddress: "thirdaddr", VotingPower: big.NewFloat(3)},
Validator: &Validator{Moniker: "third", OperatorAddress: "thirdaddr", VotingPower: math.LegacyNewDec(3)},
},
}

totalVotingPower := entries.GetTotalVotingPower()
assert.Equal(t, totalVotingPower, big.NewFloat(3))
assert.Equal(t, totalVotingPower, math.LegacyNewDec(3))
}

func TestEntriesSetTotalVotingPower(t *testing.T) {
Expand All @@ -72,23 +73,23 @@ func TestEntriesSetTotalVotingPower(t *testing.T) {
Validator: &Validator{
Moniker: "first",
OperatorAddress: "firstaddr",
VotingPower: big.NewFloat(1),
VotingPower: math.LegacyNewDec(1),
},
},
"secondaddr": {
IsActive: true,
Validator: &Validator{
Moniker: "second",
OperatorAddress: "secondaddr",
VotingPower: big.NewFloat(3),
VotingPower: math.LegacyNewDec(3),
},
},
"thirdaddr": {
IsActive: false,
Validator: &Validator{
Moniker: "third",
OperatorAddress: "thirdaddr",
VotingPower: big.NewFloat(2),
VotingPower: math.LegacyNewDec(2),
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/validator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"math/big"
"cosmossdk.io/math"
)

type SigningInfo struct {
Expand All @@ -22,7 +22,7 @@ type Validator struct {
Jailed bool
SigningInfo *SigningInfo

VotingPower *big.Float
VotingPower math.LegacyDec
VotingPowerPercent float64
CumulativeVotingPowerPercent float64
Rank int
Expand Down

0 comments on commit b2fb256

Please sign in to comment.