Skip to content

Commit

Permalink
fix: ensure withdraw_rewards events are always emitted on reward with…
Browse files Browse the repository at this point in the history
…drawal (backport cosmos#13323) (cosmos#13339)

* fix: ensure withdraw_rewards events are always emitted on reward withdrawal (cosmos#13323)

(cherry picked from commit c1c23a7)

# Conflicts:
#	CHANGELOG.md
#	tests/integration/distribution/keeper/delegation_test.go
#	testutil/sims/app_helpers.go
#	x/distribution/keeper/delegation.go
#	x/distribution/keeper/delegation_test.go
#	x/distribution/keeper/keeper.go

* fix changelog

* fix conflcits

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent 2c863a1 commit a32be97
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [#13323](https://github.com/cosmos/cosmos-sdk/pull/13323) Ensure `withdraw_rewards` rewards are emitted from all actions that result in rewards being withdrawn.
* [#13233](https://github.com/cosmos/cosmos-sdk/pull/13233) Add `--append` to `add-genesis-account` sub-command to append new tokens after an account is already created.
* (x/group) [#13214](https://github.com/cosmos/cosmos-sdk/pull/13214) Add `withdraw-proposal` command to group module's CLI transaction commands.
* (x/auth) [#13048](https://github.com/cosmos/cosmos-sdk/pull/13048) Add handling of AccountNumberStoreKeyPrefix to the simulation decoder.
Expand Down
19 changes: 19 additions & 0 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,24 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.Vali
),
)

if finalRewards.IsZero() {
baseDenom, _ := sdk.GetBaseDenom()
if baseDenom == "" {
baseDenom = sdk.DefaultBondDenom
}

// Note, we do not call the NewCoins constructor as we do not want the zero
// coin removed.
finalRewards = sdk.Coins{sdk.NewCoin(baseDenom, sdk.ZeroInt())}
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeWithdrawRewards,
sdk.NewAttribute(sdk.AttributeKeyAmount, finalRewards.String()),
sdk.NewAttribute(types.AttributeKeyValidator, val.GetOperator().String()),
),
)

return finalRewards, nil
}
6 changes: 3 additions & 3 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1193,15 +1193,15 @@ func Test100PercentCommissionReward(t *testing.T) {
rewards, err := distrKeeper.WithdrawDelegationRewards(ctx, addr, valAddr)
require.NoError(t, err)

zeroRewards := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, math.ZeroInt())}
require.True(t, rewards.Equal(zeroRewards))
zeroRewards := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.ZeroInt())}
require.True(t, rewards.IsEqual(zeroRewards))

events := ctx.EventManager().Events()
lastEvent := events[len(events)-1]

var hasValue bool
for _, attr := range lastEvent.Attributes {
if attr.Key == "amount" && attr.Value == "0stake" {
if string(attr.Key) == "amount" && string(attr.Value) == "0stake" {
hasValue = true
}
}
Expand Down

0 comments on commit a32be97

Please sign in to comment.