Skip to content

Commit

Permalink
replace sdk.Dec with math.LegacyDec & related
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek committed Sep 18, 2023
1 parent f9700ab commit c92315c
Show file tree
Hide file tree
Showing 32 changed files with 234 additions and 215 deletions.
11 changes: 6 additions & 5 deletions tests/difference/core/driver/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"time"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

Expand All @@ -29,8 +30,8 @@ type InitState struct {
NumValidators int
MaxValidators int
InitialDelegatorTokens int
SlashDoublesign sdk.Dec
SlashDowntime sdk.Dec
SlashDoublesign math.LegacyDec
SlashDowntime math.LegacyDec
UnbondingP time.Duration
UnbondingC time.Duration
Trusting time.Duration
Expand All @@ -45,7 +46,7 @@ var initStateVar InitState

func init() {
// tokens === power
sdk.DefaultPowerReduction = sdk.NewInt(1)
sdk.DefaultPowerReduction = math.NewInt(1)
initStateVar = InitState{
PKSeeds: []string{
// Fixed seeds are used to create the private keys for validators.
Expand All @@ -59,8 +60,8 @@ func init() {
NumValidators: 4,
MaxValidators: 2,
InitialDelegatorTokens: 10000000000000,
SlashDoublesign: sdk.NewDec(0),
SlashDowntime: sdk.NewDec(0),
SlashDoublesign: math.LegacyNewDec(0),
SlashDowntime: math.LegacyNewDec(0),
UnbondingP: time.Second * 70,
UnbondingC: time.Second * 50,
Trusting: time.Second * 49,
Expand Down
7 changes: 4 additions & 3 deletions tests/difference/core/driver/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"cosmossdk.io/math"
ibctesting "github.com/cosmos/ibc-go/v8/testing"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -149,7 +150,7 @@ func (s *CoreSuite) delegatorBalance() int64 {
func (s *CoreSuite) delegate(val, amt int64) {
providerStaking := s.providerStakingKeeper()
server := stakingkeeper.NewMsgServerImpl(&providerStaking)
coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(amt))
coin := sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(amt))
d := s.delegator()
v := s.validator(val)
msg := stakingtypes.NewMsgDelegate(d, v, coin)
Expand All @@ -162,7 +163,7 @@ func (s *CoreSuite) delegate(val, amt int64) {
func (s *CoreSuite) undelegate(val, amt int64) {
providerStaking := s.providerStakingKeeper()
server := stakingkeeper.NewMsgServerImpl(&providerStaking)
coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(amt))
coin := sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(amt))
d := s.delegator()
v := s.validator(val)
msg := stakingtypes.NewMsgUndelegate(d, v, coin)
Expand All @@ -180,7 +181,7 @@ func (s *CoreSuite) consumerSlash(val sdk.ConsAddress, h int64, isDowntime bool)
}
ctx := s.ctx(C)
before := len(ctx.EventManager().Events())
s.consumerKeeper().SlashWithInfractionReason(ctx, val, h, 0, sdk.Dec{}, kind)
s.consumerKeeper().SlashWithInfractionReason(ctx, val, h, 0, math.LegacyDec{}, kind)
// consumer module emits packets on slash, so these must be collected.
evts := ctx.EventManager().Events()
packets := simibc.ParsePacketsFromEvents(evts[before:])
Expand Down
25 changes: 13 additions & 12 deletions tests/difference/core/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"time"

"cosmossdk.io/math"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v8/modules/core/23-commitment/types"
Expand Down Expand Up @@ -133,7 +134,7 @@ func (b *Builder) getAppBytesAndSenders(

bal := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewIntFromUint64(amt))),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewIntFromUint64(amt))),
}

accounts = append(accounts, acc)
Expand All @@ -155,21 +156,21 @@ func (b *Builder) getAppBytesAndSenders(
delegations := make([]stakingtypes.Delegation, 0, len(validators.Validators))

// Sum bonded is needed for BondedPool account
sumBonded := sdk.NewInt(0)
sumBonded := math.NewInt(0)
initValPowers := []abci.ValidatorUpdate{}

for i, val := range validators.Validators {
status := b.initState.ValStates.Status[i]
delegation := b.initState.ValStates.Delegation[i]
extra := b.initState.ValStates.ValidatorExtraTokens[i]

tokens := sdk.NewInt(int64(delegation + extra))
tokens := math.NewInt(int64(delegation + extra))
b.suite.Require().Equal(status, stakingtypes.Bonded, "All genesis validators should be bonded")
sumBonded = sumBonded.Add(tokens)
// delegator account receives delShares shares
delShares := sdk.NewDec(int64(delegation))
delShares := math.LegacyNewDec(int64(delegation))
// validator has additional sumShares due to extra units
sumShares := sdk.NewDec(int64(delegation + extra))
sumShares := math.LegacyNewDec(int64(delegation + extra))

pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey)
require.NoError(b.suite.T(), err)
Expand All @@ -186,8 +187,8 @@ func (b *Builder) getAppBytesAndSenders(
Description: stakingtypes.Description{},
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: sdk.ZeroInt(),
Commission: stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()),
MinSelfDelegation: math.ZeroInt(),
}

stakingValidators = append(stakingValidators, validator)
Expand Down Expand Up @@ -238,7 +239,7 @@ func (b *Builder) getAppBytesAndSenders(
// add unbonded amount
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin(bondDenom, sdk.ZeroInt())},
Coins: sdk.Coins{sdk.NewCoin(bondDenom, math.ZeroInt())},
})

// update total funds supply
Expand Down Expand Up @@ -404,7 +405,7 @@ func (b *Builder) ensureValidatorLexicographicOrderingMatchesModel() {
// validators in the setup process.
func (b *Builder) delegate(del int, val sdk.ValAddress, amt int64) {
d := b.provider().SenderAccounts[del].SenderAccount.GetAddress()
coins := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(amt))
coins := sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(amt))
msg := stakingtypes.NewMsgDelegate(d, val, coins)
providerStaking := b.providerStakingKeeper()
pskServer := stakingkeeper.NewMsgServerImpl(&providerStaking)
Expand All @@ -415,7 +416,7 @@ func (b *Builder) delegate(del int, val sdk.ValAddress, amt int64) {
// addValidatorToStakingModule creates an additional validator with zero commission
// and zero tokens (zero voting power).
func (b *Builder) addValidatorToStakingModule(privVal mock.PV) {
coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(0))
coin := sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(0))

pubKey, err := privVal.GetPubKey()
require.NoError(b.suite.T(), err)
Expand All @@ -432,8 +433,8 @@ func (b *Builder) addValidatorToStakingModule(privVal mock.PV) {
sdkPK,
coin,
stakingtypes.Description{},
stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
sdk.ZeroInt())
stakingtypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()),
math.ZeroInt())
b.suite.Require().NoError(err)
providerStaking := b.providerStakingKeeper()
pskServer := stakingkeeper.NewMsgServerImpl(&providerStaking)
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ func delegateAndRedelegate(s *CCVTestSuite, delAddr sdk.AccAddress,
}

// delegate delegates bondAmt to the first validator
func delegate(s *CCVTestSuite, delAddr sdk.AccAddress, bondAmt math.Int) (initBalance math.Int, shares sdk.Dec, valAddr sdk.ValAddress) {
func delegate(s *CCVTestSuite, delAddr sdk.AccAddress, bondAmt math.Int) (initBalance math.Int, shares math.LegacyDec, valAddr sdk.ValAddress) {
return delegateByIdx(s, delAddr, bondAmt, 0)
}

// delegateByIdx delegates bondAmt to the validator at specified index in provider val set
func delegateByIdx(s *CCVTestSuite, delAddr sdk.AccAddress, bondAmt math.Int, idx int) (initBalance math.Int, shares sdk.Dec, valAddr sdk.ValAddress) {
func delegateByIdx(s *CCVTestSuite, delAddr sdk.AccAddress, bondAmt math.Int, idx int) (initBalance math.Int, shares math.LegacyDec, valAddr sdk.ValAddress) {
initBalance = getBalance(s, s.providerCtx(), delAddr)
// choose a validator
validator, valAddr := s.getValByIdx(idx)
Expand All @@ -172,7 +172,7 @@ func delegateByIdx(s *CCVTestSuite, delAddr sdk.AccAddress, bondAmt math.Int, id
}

// undelegate unbonds an amount of delegator shares from a given validator
func undelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valAddr sdk.ValAddress, sharesAmount sdk.Dec) (valsetUpdateId uint64) {
func undelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valAddr sdk.ValAddress, sharesAmount math.LegacyDec) (valsetUpdateId uint64) {
_, err := s.providerApp.GetTestStakingKeeper().Undelegate(s.providerCtx(), delAddr, valAddr, sharesAmount)
s.Require().NoError(err)

Expand All @@ -185,7 +185,7 @@ func undelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valAddr sdk.ValAddress,
// Executes a BeginRedelegation (unbonding and redelegation) operation
// on the provider chain using delegated funds from delAddr
func redelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valSrcAddr sdk.ValAddress,
valDstAddr sdk.ValAddress, sharesAmount sdk.Dec,
valDstAddr sdk.ValAddress, sharesAmount math.LegacyDec,
) {
stakingKeeper := s.providerApp.GetTestStakingKeeper()
ctx := s.providerCtx()
Expand Down Expand Up @@ -595,7 +595,7 @@ func (suite *CCVTestSuite) GetConsumerEndpointClientAndConsState(
func (s *CCVTestSuite) setupValidatorPowers() {
delAddr := s.providerChain.SenderAccount.GetAddress()
for idx := range s.providerChain.Vals.Validators {
delegateByIdx(s, delAddr, sdk.NewInt(999999999), idx)
delegateByIdx(s, delAddr, math.NewInt(999999999), idx)
}

s.providerChain.NextBlock()
Expand Down
26 changes: 13 additions & 13 deletions tests/integration/democracy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
bankKeeper := s.consumerApp.GetTestBankKeeper()
bondDenom := stakingKeeper.BondDenom(s.consumerCtx())

currentRepresentativesRewards := map[string]sdk.Dec{}
nextRepresentativesRewards := map[string]sdk.Dec{}
currentRepresentativesRewards := map[string]math.LegacyDec{}
nextRepresentativesRewards := map[string]math.LegacyDec{}
representativesTokens := map[string]math.Int{}

for _, representative := range stakingKeeper.GetAllValidators(s.consumerCtx()) {
currentRepresentativesRewards[representative.OperatorAddress] = sdk.NewDec(0)
nextRepresentativesRewards[representative.OperatorAddress] = sdk.NewDec(0)
currentRepresentativesRewards[representative.OperatorAddress] = math.LegacyNewDec(0)
nextRepresentativesRewards[representative.OperatorAddress] = math.LegacyNewDec(0)
representativesTokens[representative.OperatorAddress] = representative.GetTokens()
}

distrModuleAccount := distrKeeper.GetDistributionAccount(s.consumerCtx())
providerRedistributeAccount := accountKeeper.GetModuleAccount(s.consumerCtx(), consumertypes.ConsumerToSendToProviderName)
// balance of consumer redistribute address will always be 0 when checked between 2 NextBlock() calls

currentDistrModuleAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
currentProviderFeeAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
currentDistrModuleAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
currentProviderFeeAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
currentCommunityPoolBalance := distrKeeper.GetFeePoolCommunityCoins(s.consumerCtx()).AmountOf(bondDenom)
for key := range currentRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
Expand All @@ -101,8 +101,8 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {

s.consumerChain.NextBlock()

nextDistrModuleAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
nextProviderFeeAccountBalance := sdk.NewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
nextDistrModuleAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
nextProviderFeeAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
nextCommunityPoolBalance := distrKeeper.GetFeePoolCommunityCoins(s.consumerCtx()).AmountOf(bondDenom)
for key := range nextRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
Expand All @@ -113,15 +113,15 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
distrModuleDifference := nextDistrModuleAccountBalance.Sub(currentDistrModuleAccountBalance)
providerDifference := nextProviderFeeAccountBalance.Sub(currentProviderFeeAccountBalance)
communityPoolDifference := nextCommunityPoolBalance.Sub(currentCommunityPoolBalance)
representativeDifference := map[string]sdk.Dec{}
representativeDifference := map[string]math.LegacyDec{}
consumerRedistributeDifference := communityPoolDifference

for key, currentReward := range currentRepresentativesRewards {
representativeDifference[key] = nextRepresentativesRewards[key].Sub(currentReward)
consumerRedistributeDifference = consumerRedistributeDifference.Add(representativeDifference[key])
}

consumerRedistributionFraction := sdk.MustNewDecFromStr(s.consumerApp.GetConsumerKeeper().GetConsumerRedistributionFrac(s.consumerCtx()))
consumerRedistributionFraction := math.LegacyMustNewDecFromStr(s.consumerApp.GetConsumerKeeper().GetConsumerRedistributionFrac(s.consumerCtx()))

// confirm that the total amount given to the community pool plus all representatives is equal to the total amount taken out of distribution
s.Require().Equal(distrModuleDifference, consumerRedistributeDifference)
Expand All @@ -135,13 +135,13 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
// check that the fraction actually kept by the provider is the correct fraction. using InEpsilon because the math code uses truncations
s.Require().InEpsilon(providerDifference.Quo(
providerDifference.Add(distrModuleDifference)).MustFloat64(),
sdk.NewDec(1).Sub(consumerRedistributionFraction).MustFloat64(), float64(0.0001))
math.LegacyNewDec(1).Sub(consumerRedistributionFraction).MustFloat64(), float64(0.0001))

totalRepresentativePower := stakingKeeper.GetValidatorSet().TotalBondedTokens(s.consumerCtx())

// check that each representative has gotten the correct amount of rewards
for key, representativeTokens := range representativesTokens {
powerFraction := sdk.NewDecFromInt(representativeTokens).QuoTruncate(sdk.NewDecFromInt(totalRepresentativePower))
powerFraction := math.LegacyNewDecFromInt(representativeTokens).QuoTruncate(math.LegacyNewDecFromInt(totalRepresentativePower))
s.Require().Equal(powerFraction, representativeDifference[key].Quo(consumerRedistributeDifference.Sub(communityPoolDifference)))
}
}
Expand All @@ -154,7 +154,7 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
accountKeeper := s.consumerApp.GetTestAccountKeeper()
mintKeeper := s.consumerApp.GetTestMintKeeper()
newAuthParamValue := uint64(128)
newMintParamValue := sdk.NewDecWithPrec(1, 1) // "0.100000000000000000"
newMintParamValue := math.LegacyNewDecWithPrec(1, 1) // "0.100000000000000000"
votingAccounts := s.consumerChain.SenderAccounts
bondDenom := stakingKeeper.BondDenom(s.consumerCtx())
depositAmount := params.MinDeposit
Expand Down
Loading

0 comments on commit c92315c

Please sign in to comment.