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 (cosmos#13323)
  • Loading branch information
alexanderbez authored and Wryhder committed Oct 26, 2022
1 parent dc3168a commit f2b4ac0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -54,6 +54,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.
* [#13214](https://github.com/cosmos/cosmos-sdk/pull/13214) Add `withdraw-proposal` command to group module's CLI transaction commands.
* [#13070](https://github.com/cosmos/cosmos-sdk/pull/13070) Migrate from `gogo/protobuf` to `cosmos/gogoproto`.
* [#12981](https://github.com/cosmos/cosmos-sdk/pull/12981) Return proper error when parsing telemetry configuration.
Expand Down
14 changes: 5 additions & 9 deletions tests/integration/distribution/keeper/delegation_test.go
Expand Up @@ -804,19 +804,15 @@ func Test100PercentCommissionReward(t *testing.T) {
rewards, err := distrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
require.NoError(t, err)

denom, _ := sdk.GetBaseDenom()
zeroRewards := sdk.Coins{
sdk.Coin{
Denom: denom,
Amount: math.ZeroInt(),
},
}
zeroRewards := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, math.ZeroInt())}
require.True(t, rewards.IsEqual(zeroRewards))

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

var hasValue bool
for _, attr := range lastEvent.Attributes {
if string(attr.Key) == "amount" && string(attr.Value) == "0" {
if attr.Key == "amount" && attr.Value == "0stake" {
hasValue = true
}
}
Expand Down
9 changes: 7 additions & 2 deletions testutil/sims/app_helpers.go
Expand Up @@ -14,6 +14,7 @@ import (

"cosmossdk.io/depinject"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -186,8 +187,11 @@ func SetupWithConfiguration(appConfig depinject.Config, startupConfig StartupCon
}

// GenesisStateWithValSet returns a new genesis state with the validator set
func GenesisStateWithValSet(codec codec.Codec, genesisState map[string]json.RawMessage,
valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount,
func GenesisStateWithValSet(
codec codec.Codec,
genesisState map[string]json.RawMessage,
valSet *tmtypes.ValidatorSet,
genAccs []authtypes.GenesisAccount,
balances ...banktypes.Balance,
) (map[string]json.RawMessage, error) {
// set genesis accounts
Expand Down Expand Up @@ -227,6 +231,7 @@ func GenesisStateWithValSet(codec codec.Codec, genesisState map[string]json.RawM
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), math.LegacyOneDec()))

}

// set validators and delegations
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
genesisState[stakingtypes.ModuleName] = codec.MustMarshalJSON(stakingGenesis)
Expand Down
31 changes: 25 additions & 6 deletions x/distribution/keeper/delegation.go
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

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

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -163,13 +163,13 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.Vali
)
}

// truncate coins, return remainder to community pool
coins, remainder := rewards.TruncateDecimal()
// truncate reward dec coins, return remainder to community pool
finalRewards, remainder := rewards.TruncateDecimal()

// add coins to user account
if !coins.IsZero() {
if !finalRewards.IsZero() {
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, del.GetDelegatorAddr())
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, coins)
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, finalRewards)
if err != nil {
return nil, err
}
Expand All @@ -190,5 +190,24 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.Vali
// remove delegator starting info
k.DeleteDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())

return coins, nil
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, math.ZeroInt())}
}

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

return finalRewards, nil
}
15 changes: 6 additions & 9 deletions x/distribution/keeper/delegation_test.go
Expand Up @@ -8,6 +8,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
Expand Down Expand Up @@ -959,19 +960,15 @@ func Test100PercentCommissionReward(t *testing.T) {
rewards, err := distrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(addr), valAddr)
require.NoError(t, err)

denom, _ := sdk.GetBaseDenom()
zeroRewards := sdk.Coins{
sdk.Coin{
Denom: denom,
Amount: math.ZeroInt(),
},
}
zeroRewards := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, math.ZeroInt())}
require.True(t, rewards.IsEqual(zeroRewards))

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

var hasValue bool
for _, attr := range lastEvent.Attributes {
if string(attr.Key) == "amount" && string(attr.Value) == "0" {
if attr.Key == "amount" && attr.Value == "0stake" {
hasValue = true
}
}
Expand Down
17 changes: 0 additions & 17 deletions x/distribution/keeper/keeper.go
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"fmt"

"cosmossdk.io/math"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -98,22 +97,6 @@ func (k Keeper) WithdrawDelegationRewards(ctx sdk.Context, delAddr sdk.AccAddres
return nil, err
}

if rewards.IsZero() {
baseDenom, _ := sdk.GetBaseDenom()
rewards = sdk.Coins{sdk.Coin{
Denom: baseDenom,
Amount: math.ZeroInt(),
}}
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeWithdrawRewards,
sdk.NewAttribute(sdk.AttributeKeyAmount, rewards.String()),
sdk.NewAttribute(types.AttributeKeyValidator, valAddr.String()),
),
)

// reinitialize the delegation
k.initializeDelegation(ctx, valAddr, delAddr)
return rewards, nil
Expand Down

0 comments on commit f2b4ac0

Please sign in to comment.