Skip to content

Commit 061a8c8

Browse files
authored
Merge branch 'master' into aleem/7517-remove-legacy-rest
2 parents c3fd850 + 2c6b866 commit 061a8c8

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
7575

7676
* (types) [\#9627](https://github.com/cosmos/cosmos-sdk/pull/9627) Fix nil pointer panic on `NewBigIntFromInt`
7777
* (x/genutil) [\#9574](https://github.com/cosmos/cosmos-sdk/pull/9575) Actually use the `gentx` client tx flags (like `--keyring-dir`)
78+
* (x/distribution) [\#9599](https://github.com/cosmos/cosmos-sdk/pull/9599) Withdraw rewards event now includes a value attribute even if there are 0 rewards (due to situations like 100% commission).
7879

7980

8081
## [v0.43.0-rc0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.43.0-rc0) - 2021-06-25

x/distribution/keeper/delegation_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,72 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
626626
// commission should be zero
627627
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
628628
}
629+
630+
func Test100PercentCommissionReward(t *testing.T) {
631+
app := simapp.Setup(false)
632+
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
633+
634+
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
635+
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000))
636+
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
637+
initial := int64(20)
638+
639+
// set module account coins
640+
distrAcc := app.DistrKeeper.GetDistributionAccount(ctx)
641+
require.NoError(t, testutil.FundModuleAccount(app.BankKeeper, ctx, distrAcc.GetName(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000)))))
642+
app.AccountKeeper.SetModuleAccount(ctx, distrAcc)
643+
644+
tokens := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.NewDec(initial))}
645+
646+
// create validator with 100% commission
647+
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(10, 1), sdk.NewDecWithPrec(10, 1), sdk.NewDec(0))
648+
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
649+
app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
650+
651+
// end block to bond validator
652+
staking.EndBlocker(ctx, app.StakingKeeper)
653+
// next block
654+
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
655+
656+
// fetch validator
657+
val := app.StakingKeeper.Validator(ctx, valAddrs[0])
658+
659+
// allocate some rewards
660+
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)
661+
662+
// end block
663+
staking.EndBlocker(ctx, app.StakingKeeper)
664+
665+
// next block
666+
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
667+
668+
// allocate some more rewards
669+
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)
670+
671+
// next block
672+
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
673+
674+
// allocate some more rewards
675+
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)
676+
677+
rewards, err := app.DistrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
678+
require.NoError(t, err)
679+
680+
denom, _ := sdk.GetBaseDenom()
681+
zeroRewards := sdk.Coins{
682+
sdk.Coin{
683+
Denom: denom,
684+
Amount: sdk.ZeroInt(),
685+
},
686+
}
687+
require.True(t, rewards.IsEqual(zeroRewards))
688+
events := ctx.EventManager().Events()
689+
lastEvent := events[len(events)-1]
690+
hasValue := false
691+
for _, attr := range lastEvent.Attributes {
692+
if string(attr.Key) == "amount" && string(attr.Value) == "0" {
693+
hasValue = true
694+
}
695+
}
696+
require.True(t, hasValue)
697+
}

x/distribution/keeper/keeper.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ func (k Keeper) WithdrawDelegationRewards(ctx sdk.Context, delAddr sdk.AccAddres
9999
return nil, err
100100
}
101101

102+
if rewards.IsZero() {
103+
baseDenom, _ := sdk.GetBaseDenom()
104+
rewards = sdk.Coins{sdk.Coin{
105+
Denom: baseDenom,
106+
Amount: sdk.ZeroInt(),
107+
}}
108+
}
109+
102110
ctx.EventManager().EmitEvent(
103111
sdk.NewEvent(
104112
types.EventTypeWithdrawRewards,

0 commit comments

Comments
 (0)