Skip to content

Commit 63a7c45

Browse files
mergify[bot]alexanderbezjulienrbrt
authored
fix: ensure withdraw_rewards events are always emitted on reward withdrawal (backport #13323) (#13340)
* fix: ensure withdraw_rewards events are always emitted on reward withdrawal (#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 conflicts * move changelog to right place * fix typo Co-authored-by: Aleksandr Bezobchuk <[email protected]> Co-authored-by: Julien Robert <[email protected]>
1 parent e70b5db commit 63a7c45

File tree

6 files changed

+55
-41
lines changed

6 files changed

+55
-41
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## [Unreleased]
3939

40+
### Improvements
41+
42+
* [#13323](https://github.com/cosmos/cosmos-sdk/pull/13323) Ensure `withdraw_rewards` rewards are emitted from all actions that result in rewards being withdrawn.
43+
* [#13321](https://github.com/cosmos/cosmos-sdk/pull/13321) Add flag to disable fast node migration and usage.
44+
4045
### API Breaking Changes
4146

4247
- (cli) [#13089](https://github.com/cosmos/cosmos-sdk/pull/13089) Fix rollback command don't actually delete multistore versions, added method `RollbackToVersion` to interface `CommitMultiStore` and added method `CommitMultiStore` to `Application` interface.
@@ -51,7 +56,6 @@ Ref: https://keepachangelog.com/en/1.0.0/
5156
* [#12693](https://github.com/cosmos/cosmos-sdk/pull/12693) Make sure the order of each node is consistent when emitting proto events.
5257
* (simapp) [#13107](https://github.com/cosmos/cosmos-sdk/pull/13107) Call `SetIAVLCacheSize` with the configured value in simapp.
5358
* (cli) [#12742](https://github.com/cosmos/cosmos-sdk/pull/12742) Add the `prune` CLI cmd to manually prune app store history versions based on the pruning options.
54-
* [#13321](https://github.com/cosmos/cosmos-sdk/pull/13321) Add flag to disable fast node migration and usage.
5559

5660
### Bug Fixes
5761

server/config/config.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ type BaseConfig struct {
7373
// IavlCacheSize set the size of the iavl tree cache.
7474
IAVLCacheSize uint64 `mapstructure:"iavl-cache-size"`
7575

76-
// IAVLDisableFastnNode enables or disables the fast sync node.
77-
IAVLDisableFastnNode bool `mapstructure:"iavl-disable-fastnode"`
76+
// IAVLDisableFastNode enables or disables the fast sync node.
77+
IAVLDisableFastNode bool `mapstructure:"iavl-disable-fastnode"`
7878
}
7979

8080
// APIConfig defines the API listener configuration.
@@ -207,16 +207,16 @@ func (c *Config) GetMinGasPrices() sdk.DecCoins {
207207
func DefaultConfig() *Config {
208208
return &Config{
209209
BaseConfig: BaseConfig{
210-
MinGasPrices: defaultMinGasPrices,
211-
InterBlockCache: true,
212-
Pruning: storetypes.PruningOptionDefault,
213-
PruningKeepRecent: "0",
214-
PruningKeepEvery: "0",
215-
PruningInterval: "0",
216-
MinRetainBlocks: 0,
217-
IndexEvents: make([]string, 0),
218-
IAVLCacheSize: 781250, // 50 MB
219-
IAVLDisableFastnNode: false,
210+
MinGasPrices: defaultMinGasPrices,
211+
InterBlockCache: true,
212+
Pruning: storetypes.PruningOptionDefault,
213+
PruningKeepRecent: "0",
214+
PruningKeepEvery: "0",
215+
PruningInterval: "0",
216+
MinRetainBlocks: 0,
217+
IndexEvents: make([]string, 0),
218+
IAVLCacheSize: 781250, // 50 MB
219+
IAVLDisableFastNode: false,
220220
},
221221
Telemetry: telemetry.Config{
222222
Enabled: false,
@@ -273,17 +273,17 @@ func GetConfig(v *viper.Viper) (Config, error) {
273273

274274
return Config{
275275
BaseConfig: BaseConfig{
276-
MinGasPrices: v.GetString("minimum-gas-prices"),
277-
InterBlockCache: v.GetBool("inter-block-cache"),
278-
Pruning: v.GetString("pruning"),
279-
PruningKeepRecent: v.GetString("pruning-keep-recent"),
280-
PruningInterval: v.GetString("pruning-interval"),
281-
HaltHeight: v.GetUint64("halt-height"),
282-
HaltTime: v.GetUint64("halt-time"),
283-
IndexEvents: v.GetStringSlice("index-events"),
284-
MinRetainBlocks: v.GetUint64("min-retain-blocks"),
285-
IAVLCacheSize: v.GetUint64("iavl-cache-size"),
286-
IAVLDisableFastnNode: v.GetBool("iavl-disable-fastnode"),
276+
MinGasPrices: v.GetString("minimum-gas-prices"),
277+
InterBlockCache: v.GetBool("inter-block-cache"),
278+
Pruning: v.GetString("pruning"),
279+
PruningKeepRecent: v.GetString("pruning-keep-recent"),
280+
PruningInterval: v.GetString("pruning-interval"),
281+
HaltHeight: v.GetUint64("halt-height"),
282+
HaltTime: v.GetUint64("halt-time"),
283+
IndexEvents: v.GetStringSlice("index-events"),
284+
MinRetainBlocks: v.GetUint64("min-retain-blocks"),
285+
IAVLCacheSize: v.GetUint64("iavl-cache-size"),
286+
IAVLDisableFastNode: v.GetBool("iavl-disable-fastnode"),
287287
},
288288
Telemetry: telemetry.Config{
289289
ServiceName: v.GetString("telemetry.service-name"),

server/config/toml.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ index-events = {{ .BaseConfig.IndexEvents }}
7474
# Default cache size is 50mb.
7575
iavl-cache-size = {{ .BaseConfig.IAVLCacheSize }}
7676
77-
# IavlDisableFastnNode enables or disables the fast node feature of IAVL.
77+
# IAVLDisableFastNode enables or disables the fast node feature of IAVL.
7878
# Default is false.
79-
iavl-disable-fastnode = {{ .BaseConfig.IAVLDisableFastnNode }}
79+
iavl-disable-fastnode = {{ .BaseConfig.IAVLDisableFastNode }}
8080
8181
###############################################################################
8282
### Telemetry Configuration ###

simapp/simd/cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func initAppConfig() (string, interface{}) {
118118
//
119119
// In simapp, we set the min gas prices to 0.
120120
srvCfg.MinGasPrices = "0stake"
121-
// srvCfg.BaseConfig.IAVLDisableFastnNode = true // disable fastnode by default
121+
// srvCfg.BaseConfig.IAVLDisableFastNode = true // disable fastnode by default
122122

123123
customAppConfig := CustomAppConfig{
124124
Config: *srvCfg,

x/distribution/keeper/delegation.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55

66
sdk "github.com/cosmos/cosmos-sdk/types"
7-
87
"github.com/cosmos/cosmos-sdk/x/distribution/types"
98
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
109
)
@@ -162,13 +161,13 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.Vali
162161
)
163162
}
164163

165-
// truncate coins, return remainder to community pool
166-
coins, remainder := rewards.TruncateDecimal()
164+
// truncate reward dec coins, return remainder to community pool
165+
finalRewards, remainder := rewards.TruncateDecimal()
167166

168167
// add coins to user account
169-
if !coins.IsZero() {
168+
if !finalRewards.IsZero() {
170169
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, del.GetDelegatorAddr())
171-
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, coins)
170+
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, finalRewards)
172171
if err != nil {
173172
return nil, err
174173
}
@@ -189,5 +188,24 @@ func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.Vali
189188
// remove delegator starting info
190189
k.DeleteDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr())
191190

192-
return coins, nil
191+
if finalRewards.IsZero() {
192+
baseDenom, _ := sdk.GetBaseDenom()
193+
if baseDenom == "" {
194+
baseDenom = sdk.DefaultBondDenom
195+
}
196+
197+
// Note, we do not call the NewCoins constructor as we do not want the zero
198+
// coin removed.
199+
finalRewards = sdk.Coins{sdk.NewCoin(baseDenom, sdk.ZeroInt())}
200+
}
201+
202+
ctx.EventManager().EmitEvent(
203+
sdk.NewEvent(
204+
types.EventTypeWithdrawRewards,
205+
sdk.NewAttribute(sdk.AttributeKeyAmount, finalRewards.String()),
206+
sdk.NewAttribute(types.AttributeKeyValidator, val.GetOperator().String()),
207+
),
208+
)
209+
210+
return finalRewards, nil
193211
}

x/distribution/keeper/keeper.go

-8
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,6 @@ func (k Keeper) WithdrawDelegationRewards(ctx sdk.Context, delAddr sdk.AccAddres
9898
return nil, err
9999
}
100100

101-
ctx.EventManager().EmitEvent(
102-
sdk.NewEvent(
103-
types.EventTypeWithdrawRewards,
104-
sdk.NewAttribute(sdk.AttributeKeyAmount, rewards.String()),
105-
sdk.NewAttribute(types.AttributeKeyValidator, valAddr.String()),
106-
),
107-
)
108-
109101
// reinitialize the delegation
110102
k.initializeDelegation(ctx, valAddr, delAddr)
111103
return rewards, nil

0 commit comments

Comments
 (0)