Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WithdrawRewards event emit value when no rewards #9599

Merged
merged 5 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,72 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
// commission should be zero
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
}

func Test100PercentCommissionReward(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})

tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
initial := int64(20)

// set module account coins
distrAcc := app.DistrKeeper.GetDistributionAccount(ctx)
require.NoError(t, simapp.FundModuleAccount(app.BankKeeper, ctx, distrAcc.GetName(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000)))))
app.AccountKeeper.SetModuleAccount(ctx, distrAcc)

tokens := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.NewDec(initial))}

// create validator with 100% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(10, 1), sdk.NewDecWithPrec(10, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])

// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
// next block
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)

// fetch validator
val := app.StakingKeeper.Validator(ctx, valAddrs[0])

// allocate some rewards
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)

// end block
staking.EndBlocker(ctx, app.StakingKeeper)

// next block
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)

// allocate some more rewards
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)

// next block
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)

// allocate some more rewards
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)

rewards, err := app.DistrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
require.NoError(t, err)

denom, _ := sdk.GetBaseDenom()
zeroRewards := sdk.Coins{
sdk.Coin{
Denom: denom,
Amount: sdk.ZeroInt(),
},
}
require.True(t, rewards.IsEqual(zeroRewards))
events := ctx.EventManager().Events()
lastEvent := events[len(events)-1]
hasValue := false
for _, attr := range lastEvent.Attributes {
if string(attr.Key) == "amount" && string(attr.Value) == "0" {
hasValue = true
}
}
require.True(t, hasValue)
}
8 changes: 8 additions & 0 deletions x/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ func (k Keeper) WithdrawDelegationRewards(ctx sdk.Context, delAddr sdk.AccAddres
return nil, err
}

if rewards.IsZero() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A zero coin technically isn't valid. I don't have strong feeling here though in terms of emitting the event. I'd have to defer to others.

Copy link
Contributor Author

@technicallyty technicallyty Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsZero doubles as an IsEmpty check so 0 rewards is the same as no rewards here 🤷🏻‍♂️

but yeah just to clarify, without the added check, the value simply gets axed from the event

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally. I meant in the state-machine we do not allow zero coins. However, since this is for UX/client purposes only, I suppose it's OK.

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

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeWithdrawRewards,
Expand Down