fix delegation reward index (#888)

This commit is contained in:
Denali Marsh 2021-03-24 00:44:37 +01:00 committed by GitHub
parent eb856b5a1f
commit c7ed944503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 11 deletions

View File

@ -515,8 +515,8 @@ func (k Keeper) SynchronizeHardDelegatorRewards(ctx sdk.Context, delegator sdk.A
userRewardFactor := claim.DelegatorRewardIndexes[delegatorIndex].RewardFactor
rewardsAccumulatedFactor := delagatorFactor.Sub(userRewardFactor)
if rewardsAccumulatedFactor.IsZero() {
return
if rewardsAccumulatedFactor.IsNegative() {
panic(fmt.Sprintf("reward accumulation factor cannot be negative: %s", rewardsAccumulatedFactor))
}
claim.DelegatorRewardIndexes[delegatorIndex].RewardFactor = delagatorFactor
@ -539,16 +539,12 @@ func (k Keeper) SynchronizeHardDelegatorRewards(ctx sdk.Context, delegator sdk.A
}
delegatedTokens := validator.TokensFromShares(delegation.GetShares())
if delegatedTokens.IsZero() || delegatedTokens.IsNegative() {
if delegatedTokens.IsNegative() {
continue
}
totalDelegated = totalDelegated.Add(delegatedTokens)
}
rewardsEarned := rewardsAccumulatedFactor.Mul(totalDelegated).RoundInt()
if rewardsEarned.IsZero() || rewardsEarned.IsNegative() {
return
}
// Add rewards to delegator's hard claim
newRewardsCoin := sdk.NewCoin(types.HardLiquidityRewardDenom, rewardsEarned)

View File

@ -2324,6 +2324,17 @@ func (suite *KeeperTestSuite) TestSynchronizeHardDelegatorReward() {
expectedRewards: cs(c("hard", 52856928000)),
},
},
{
"delegator reward index updated when reward is zero",
args{
delegation: c("ukava", 1),
rewardsPerSecond: c("hard", 1),
initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC),
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
expectedRewardFactor: d("0.000099999900000100"),
expectedRewards: nil,
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
@ -2351,13 +2362,22 @@ func (suite *KeeperTestSuite) TestSynchronizeHardDelegatorReward() {
// Set up hard state (interest factor for the relevant denom)
suite.hardKeeper.SetPreviousAccrualTime(suite.ctx, tc.args.delegation.Denom, tc.args.initialTime)
// Delegator delegates
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], tc.args.delegation)
// Create validator account
staking.BeginBlocker(suite.ctx, suite.stakingKeeper)
selfDelegationCoins := c("ukava", 1_000_000)
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], selfDelegationCoins)
suite.Require().NoError(err)
suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[0], tc.args.delegation)
staking.EndBlocker(suite.ctx, suite.stakingKeeper)
// Delegator delegates
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[0], tc.args.delegation)
suite.Require().NoError(err)
staking.EndBlocker(suite.ctx, suite.stakingKeeper)
// Check that validator account has been created and delegation was successful
valAcc, found := suite.stakingKeeper.GetValidator(suite.ctx, suite.validatorAddrs[0])
suite.True(found)
suite.Require().Equal(valAcc.Status, sdk.Bonded)
suite.Require().Equal(valAcc.Tokens, tc.args.delegation.Amount.Add(selfDelegationCoins.Amount))
// Check that Staking hooks initialized a HardLiquidityProviderClaim
claim, found := suite.keeper.GetHardLiquidityProviderClaim(suite.ctx, suite.addrs[0])