From b2fb25603a80b3f04f696fb41c9ea6aadf995991 Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 8 Aug 2024 09:44:39 +0300 Subject: [PATCH] chore: use math.LegacyDec --- pkg/converter/converter.go | 3 +-- pkg/types/entry.go | 13 +++++++------ pkg/types/entry_test.go | 17 +++++++++-------- pkg/types/validator.go | 4 ++-- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pkg/converter/converter.go b/pkg/converter/converter.go index 78fdd2b..e62ff2e 100644 --- a/pkg/converter/converter.go +++ b/pkg/converter/converter.go @@ -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" @@ -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, } } diff --git a/pkg/types/entry.go b/pkg/types/entry.go index e1f770c..59ec85c 100644 --- a/pkg/types/entry.go +++ b/pkg/types/entry.go @@ -2,8 +2,9 @@ package types import ( "main/pkg/utils" - "math/big" "sort" + + "cosmossdk.io/math" ) type Entry struct { @@ -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) } } @@ -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] diff --git a/pkg/types/entry_test.go b/pkg/types/entry_test.go index 72a9146..d240c76 100644 --- a/pkg/types/entry_test.go +++ b/pkg/types/entry_test.go @@ -1,9 +1,10 @@ package types import ( - "math/big" "testing" + "cosmossdk.io/math" + "github.com/stretchr/testify/assert" ) @@ -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) { @@ -72,7 +73,7 @@ func TestEntriesSetTotalVotingPower(t *testing.T) { Validator: &Validator{ Moniker: "first", OperatorAddress: "firstaddr", - VotingPower: big.NewFloat(1), + VotingPower: math.LegacyNewDec(1), }, }, "secondaddr": { @@ -80,7 +81,7 @@ func TestEntriesSetTotalVotingPower(t *testing.T) { Validator: &Validator{ Moniker: "second", OperatorAddress: "secondaddr", - VotingPower: big.NewFloat(3), + VotingPower: math.LegacyNewDec(3), }, }, "thirdaddr": { @@ -88,7 +89,7 @@ func TestEntriesSetTotalVotingPower(t *testing.T) { Validator: &Validator{ Moniker: "third", OperatorAddress: "thirdaddr", - VotingPower: big.NewFloat(2), + VotingPower: math.LegacyNewDec(2), }, }, } diff --git a/pkg/types/validator.go b/pkg/types/validator.go index 5fe0cda..68ebaab 100644 --- a/pkg/types/validator.go +++ b/pkg/types/validator.go @@ -1,7 +1,7 @@ package types import ( - "math/big" + "cosmossdk.io/math" ) type SigningInfo struct { @@ -22,7 +22,7 @@ type Validator struct { Jailed bool SigningInfo *SigningInfo - VotingPower *big.Float + VotingPower math.LegacyDec VotingPowerPercent float64 CumulativeVotingPowerPercent float64 Rank int