Skip to content

Commit

Permalink
Merge PR cosmos#3680: Convenience ToDec()
Browse files Browse the repository at this point in the history
* NewDecFromInt -> ToDec

* pending

* Update PENDING.md

Co-Authored-By: rigelrozanski <[email protected]>

* test typo

* typo
  • Loading branch information
rigelrozanski authored Feb 21, 2019
1 parent a07b235 commit ab9de3a
Show file tree
Hide file tree
Showing 23 changed files with 91 additions and 86 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

### SDK

* \#3456 Integrate in the Int.ToDec() convenience function
* [\#3300] Update the spec-spec, spec file reorg, and TOC updates.
* [\#3665] Overhaul sdk.Uint type in preparation for Coins's Int -> Uint migration.

Expand Down
4 changes: 2 additions & 2 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func TestBonding(t *testing.T) {
require.Equal(t, 2, len(operAddrs))

amt := sdk.TokensFromTendermintPower(60)
amtDec := sdk.NewDecFromInt(amt)
amtDec := amt.ToDec()
validator := getValidator(t, port, operAddrs[0])

acc := getAccount(t, port, addr)
Expand Down Expand Up @@ -545,7 +545,7 @@ func TestBonding(t *testing.T) {
require.Equal(t, resultTx.Height, txs[0].Height)

// query delegations, unbondings and redelegations from validator and delegator
rdShares := sdk.NewDecFromInt(rdTokens)
rdShares := rdTokens.ToDec()
delegatorDels = getDelegatorDelegations(t, port, addr)
require.Len(t, delegatorDels, 1)
require.Equal(t, rdShares, delegatorDels[0].GetShares())
Expand Down
4 changes: 2 additions & 2 deletions client/lcd/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ func doUndelegate(
BaseReq: baseReq,
DelegatorAddr: delAddr,
ValidatorAddr: valAddr,
SharesAmount: sdk.NewDecFromInt(amount),
SharesAmount: amount.ToDec(),
}

req, err := cdc.MarshalJSON(msg)
Expand Down Expand Up @@ -905,7 +905,7 @@ func doBeginRedelegation(
DelegatorAddr: delAddr,
ValidatorSrcAddr: valSrcAddr,
ValidatorDstAddr: valDstAddr,
SharesAmount: sdk.NewDecFromInt(amount),
SharesAmount: amount.ToDec(),
}

req, err := cdc.MarshalJSON(msg)
Expand Down
6 changes: 3 additions & 3 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewDecCoin(denom string, amount Int) DecCoin {

return DecCoin{
Denom: denom,
Amount: NewDecFromInt(amount),
Amount: amount.ToDec(),
}
}

Expand All @@ -54,7 +54,7 @@ func NewDecCoinFromCoin(coin Coin) DecCoin {

return DecCoin{
Denom: coin.Denom,
Amount: NewDecFromInt(coin.Amount),
Amount: coin.Amount.ToDec(),
}
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (coin DecCoin) Minus(coinB DecCoin) DecCoin {
// return the decimal coins with trunctated decimals, and return the change
func (coin DecCoin) TruncateDecimal() (Coin, DecCoin) {
truncated := coin.Amount.TruncateInt()
change := coin.Amount.Sub(NewDecFromInt(truncated))
change := coin.Amount.Sub(truncated.ToDec())
return NewCoin(coin.Denom, truncated), DecCoin{coin.Denom, change}
}

Expand Down
5 changes: 5 additions & 0 deletions types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ func ZeroInt() Int { return Int{big.NewInt(0)} }
// OneInt returns Int value with one
func OneInt() Int { return Int{big.NewInt(1)} }

// ToDec converts Int to Dec
func (i Int) ToDec() Dec {
return NewDecFromInt(i)
}

// Int64 converts Int to int64
// Panics if the value is out of range
func (i Int) Int64() int64 {
Expand Down
2 changes: 1 addition & 1 deletion x/auth/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (cva ContinuousVestingAccount) GetVestedCoins(blockTime time.Time) sdk.Coin
s := sdk.NewDec(x).Quo(sdk.NewDec(y))

for _, ovc := range cva.OriginalVesting {
vestedAmt := sdk.NewDecFromInt(ovc.Amount).Mul(s).RoundInt()
vestedAmt := ovc.Amount.ToDec().Mul(s).RoundInt()
vestedCoins = append(vestedCoins, sdk.NewCoin(ovc.Denom, vestedAmt))
}

Expand Down
16 changes: 8 additions & 8 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) {

// allocate some rewards
initial := sdk.TokensFromTendermintPower(10)
tokens := sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDecFromInt(initial)}}
tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial.ToDec()}}
k.AllocateTokensToValidator(ctx, val, tokens)

// end period
Expand All @@ -118,10 +118,10 @@ func TestCalculateRewardsAfterSlash(t *testing.T) {
rewards = k.calculateDelegationRewards(ctx, val, del, endingPeriod)

// rewards should be half the tokens
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDecFromInt(initial.DivRaw(2))}}, rewards)
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, initial.DivRaw(2).ToDec()}}, rewards)

// commission should be the other half
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDecFromInt(initial.DivRaw(2))}},
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, initial.DivRaw(2).ToDec()}},
k.GetValidatorAccumulatedCommission(ctx, valOpAddr1))
}

Expand Down Expand Up @@ -170,7 +170,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) {

// allocate some rewards
initial := sdk.TokensFromTendermintPower(10)
tokens := sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDecFromInt(initial)}}
tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial.ToDec()}}
k.AllocateTokensToValidator(ctx, val, tokens)

// slash the validator by 50% again
Expand All @@ -192,10 +192,10 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) {
rewards = k.calculateDelegationRewards(ctx, val, del, endingPeriod)

// rewards should be half the tokens
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDecFromInt(initial)}}, rewards)
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, initial.ToDec()}}, rewards)

// commission should be the other half
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, sdk.NewDecFromInt(initial)}},
require.Equal(t, sdk.DecCoins{{sdk.DefaultBondDenom, initial.ToDec()}},
k.GetValidatorAccumulatedCommission(ctx, valOpAddr1))
}

Expand Down Expand Up @@ -359,7 +359,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) {
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)

// allocate some rewards
initial := sdk.NewDecFromInt(sdk.TokensFromTendermintPower(10))
initial := sdk.TokensFromTendermintPower(10).ToDec()
tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial}}
k.AllocateTokensToValidator(ctx, val, tokens)

Expand Down Expand Up @@ -414,7 +414,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) {
del1 := sk.Delegation(ctx, sdk.AccAddress(valOpAddr1), valOpAddr1)

// allocate some rewards
initial := sdk.NewDecFromInt(sdk.TokensFromTendermintPower(30))
initial := sdk.TokensFromTendermintPower(30).ToDec()
tokens := sdk.DecCoins{{sdk.DefaultBondDenom, initial}}
k.AllocateTokensToValidator(ctx, val, tokens)

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (k Keeper) incrementValidatorPeriod(ctx sdk.Context, val sdk.Validator) uin
current = sdk.DecCoins{}
} else {
// note: necessary to truncate so we don't allow withdrawing more rewards than owed
current = rewards.Rewards.QuoDecTruncate(sdk.NewDecFromInt(val.GetTokens()))
current = rewards.Rewards.QuoDecTruncate(val.GetTokens().ToDec())
}

// fetch historical rewards for last period
Expand Down
2 changes: 1 addition & 1 deletion x/gov/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func tally(ctx sdk.Context, keeper Keeper, proposal Proposal) (passes bool, tall
return false, tallyResults
}
// If there is not enough quorum of votes, the proposal fails
percentVoting := totalVotingPower.Quo(sdk.NewDecFromInt(keeper.vs.TotalBondedTokens(ctx)))
percentVoting := totalVotingPower.Quo(keeper.vs.TotalBondedTokens(ctx).ToDec())
if percentVoting.LT(tallyParams.Quorum) {
return false, tallyResults
}
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestJailedValidatorDelegations(t *testing.T) {
got = staking.NewHandler(stakingKeeper)(ctx, msgDelegate)
require.True(t, got.IsOK(), "expected delegation to be ok, got %v", got)

unbondShares := sdk.NewDecFromInt(bondAmount)
unbondShares := bondAmount.ToDec()

// unbond validator total self-delegations (which should jail the validator)
msgUndelegate := staking.NewMsgUndelegate(sdk.AccAddress(valAddr), valAddr, unbondShares)
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestHandleAbsentValidator(t *testing.T) {
validator, _ = sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val))
require.Equal(t, sdk.Unbonding, validator.GetStatus())

slashAmt := sdk.NewDecFromInt(amt).Mul(keeper.SlashFractionDowntime(ctx)).RoundInt64()
slashAmt := amt.ToDec().Mul(keeper.SlashFractionDowntime(ctx)).RoundInt64()

// validator should have been slashed
require.Equal(t, amt.Int64()-slashAmt, validator.GetTokens().Int64())
Expand Down
6 changes: 3 additions & 3 deletions x/staking/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestStakingMsgs(t *testing.T) {
require.True(sdk.IntEq(t, bondTokens, validator.Tokens))

// check the bond that should have been created as well
checkDelegation(t, mApp, keeper, addr1, sdk.ValAddress(addr1), true, sdk.NewDecFromInt(bondTokens))
checkDelegation(t, mApp, keeper, addr1, sdk.ValAddress(addr1), true, bondTokens.ToDec())

// edit the validator
description = NewDescription("bar_moniker", "", "", "")
Expand All @@ -162,10 +162,10 @@ func TestStakingMsgs(t *testing.T) {

mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{delegateMsg}, []uint64{0}, []uint64{1}, true, true, priv2)
mock.CheckBalance(t, mApp, addr2, sdk.Coins{genCoin.Minus(bondCoin)})
checkDelegation(t, mApp, keeper, addr2, sdk.ValAddress(addr1), true, sdk.NewDecFromInt(bondTokens))
checkDelegation(t, mApp, keeper, addr2, sdk.ValAddress(addr1), true, bondTokens.ToDec())

// begin unbonding
beginUnbondingMsg := NewMsgUndelegate(addr2, sdk.ValAddress(addr1), sdk.NewDecFromInt(bondTokens))
beginUnbondingMsg := NewMsgUndelegate(addr2, sdk.ValAddress(addr1), bondTokens.ToDec())
mock.SignCheckDeliver(t, mApp.Cdc, mApp.BaseApp, []sdk.Msg{beginUnbondingMsg}, []uint64{0}, []uint64{2}, true, true, priv2)

// delegation should exist anymore
Expand Down
6 changes: 3 additions & 3 deletions x/staking/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestInitGenesis(t *testing.T) {
validators[0].Description = NewDescription("hoop", "", "", "")
validators[0].Status = sdk.Bonded
validators[0].Tokens = valTokens
validators[0].DelegatorShares = sdk.NewDecFromInt(valTokens)
validators[0].DelegatorShares = valTokens.ToDec()
validators[1].OperatorAddr = sdk.ValAddress(keep.Addrs[1])
validators[1].ConsPubKey = keep.PKs[1]
validators[1].Description = NewDescription("bloop", "", "", "")
validators[1].Status = sdk.Bonded
validators[1].Tokens = valTokens
validators[1].DelegatorShares = sdk.NewDecFromInt(valTokens)
validators[1].DelegatorShares = valTokens.ToDec()

genesisState := types.NewGenesisState(pool, params, validators, delegations)
vals, err := InitGenesis(ctx, keeper, genesisState)
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestInitGenesisLargeValidatorSet(t *testing.T) {
tokens = sdk.TokensFromTendermintPower(2)
}
validators[i].Tokens = tokens
validators[i].DelegatorShares = sdk.NewDecFromInt(tokens)
validators[i].DelegatorShares = tokens.ToDec()
}

genesisState := types.NewGenesisState(pool, params, validators, delegations)
Expand Down
Loading

0 comments on commit ab9de3a

Please sign in to comment.