2021-05-04 14:47:21 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
2021-06-10 13:35:44 +00:00
|
|
|
"testing"
|
2021-05-04 14:47:21 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
2021-05-04 14:47:21 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2021-06-10 13:35:44 +00:00
|
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2022-01-08 00:39:27 +00:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2021-06-10 13:35:44 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2021-05-04 14:47:21 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2021-05-04 14:47:21 +00:00
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/incentive/keeper"
|
2021-07-15 14:05:54 +00:00
|
|
|
"github.com/kava-labs/kava/x/incentive/testutil"
|
2021-05-04 14:47:21 +00:00
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
)
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
// Test suite used for all keeper tests
|
|
|
|
type DelegatorRewardsTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
keeper keeper.Keeper
|
|
|
|
stakingKeeper stakingkeeper.Keeper
|
|
|
|
|
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
|
|
|
|
genesisTime time.Time
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
validatorAddrs []sdk.ValAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupTest is run automatically before each suite test
|
|
|
|
func (suite *DelegatorRewardsTestSuite) SetupTest() {
|
|
|
|
config := sdk.GetConfig()
|
|
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
|
|
|
|
_, allAddrs := app.GeneratePrivKeyAddressPairs(10)
|
|
|
|
suite.addrs = allAddrs[:5]
|
|
|
|
for _, a := range allAddrs[5:] {
|
|
|
|
suite.validatorAddrs = append(suite.validatorAddrs, sdk.ValAddress(a))
|
|
|
|
}
|
|
|
|
suite.genesisTime = time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DelegatorRewardsTestSuite) SetupApp() {
|
|
|
|
suite.app = app.NewTestApp()
|
|
|
|
|
|
|
|
suite.keeper = suite.app.GetIncentiveKeeper()
|
|
|
|
suite.stakingKeeper = suite.app.GetStakingKeeper()
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.ctx = suite.app.NewContext(true, tmproto.Header{Height: 1, Time: suite.genesisTime})
|
2021-06-10 13:35:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) SetupWithGenState(authBuilder *app.AuthBankGenesisBuilder, incentBuilder testutil.IncentiveGenesisBuilder) {
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.SetupApp()
|
|
|
|
|
|
|
|
suite.app.InitializeFromGenesisStatesWithTime(
|
|
|
|
suite.genesisTime,
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder.BuildMarshalled(suite.app.AppCodec()),
|
|
|
|
NewStakingGenesisState(suite.app.AppCodec()),
|
|
|
|
incentBuilder.BuildMarshalled(suite.app.AppCodec()),
|
2021-06-10 13:35:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) TestAccumulateDelegatorRewards() {
|
2021-05-04 14:47:21 +00:00
|
|
|
type args struct {
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation sdk.Coin
|
|
|
|
rewardsPerSecond sdk.Coins
|
|
|
|
timeElapsed int
|
|
|
|
expectedRewardIndexes types.RewardIndexes
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
}
|
|
|
|
testCases := []test{
|
|
|
|
{
|
|
|
|
"7 seconds",
|
|
|
|
args{
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354)),
|
|
|
|
timeElapsed: 7,
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("0.428239000000000000")),
|
|
|
|
},
|
2021-05-04 14:47:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1 day",
|
|
|
|
args{
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354)),
|
|
|
|
timeElapsed: 86400,
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("5285.692800000000000000")),
|
|
|
|
},
|
2021-05-04 14:47:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"0 seconds",
|
|
|
|
args{
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354)),
|
|
|
|
timeElapsed: 0,
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("0.0")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple reward coins",
|
|
|
|
args{
|
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354), c("swp", 567889)),
|
|
|
|
timeElapsed: 7,
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("0.428239000000000000")),
|
|
|
|
types.NewRewardIndex("swp", d("1.987611500000000000")),
|
|
|
|
},
|
2021-05-04 14:47:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder := app.NewAuthBankGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[0]), cs(c("ukava", 1e9)))
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
incentBuilder := testutil.NewIncentiveGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithGenesisTime(suite.genesisTime).
|
|
|
|
WithSimpleDelegatorRewardPeriod(tc.args.delegation.Denom, tc.args.rewardsPerSecond)
|
|
|
|
|
|
|
|
suite.SetupWithGenState(authBuilder, incentBuilder)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], tc.args.delegation)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[0], tc.args.delegation)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
staking.EndBlocker(suite.ctx, suite.stakingKeeper)
|
|
|
|
|
|
|
|
// Set up chain context at future time
|
|
|
|
runAtTime := suite.ctx.BlockTime().Add(time.Duration(int(time.Second) * tc.args.timeElapsed))
|
|
|
|
runCtx := suite.ctx.WithBlockTime(runAtTime)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardPeriods, found := suite.keeper.GetDelegatorRewardPeriods(runCtx, tc.args.delegation.Denom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-22 12:53:18 +00:00
|
|
|
suite.keeper.AccumulateDelegatorRewards(runCtx, rewardPeriods)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardIndexes, _ := suite.keeper.GetDelegatorRewardIndexes(runCtx, tc.args.delegation.Denom)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(tc.args.expectedRewardIndexes, rewardIndexes)
|
2021-05-04 14:47:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) TestSynchronizeDelegatorReward() {
|
2021-05-04 14:47:21 +00:00
|
|
|
type args struct {
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation sdk.Coin
|
|
|
|
rewardsPerSecond sdk.Coins
|
|
|
|
blockTimes []int
|
|
|
|
expectedRewardIndexes types.RewardIndexes
|
|
|
|
expectedRewards sdk.Coins
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []test{
|
|
|
|
{
|
|
|
|
"10 blocks",
|
|
|
|
args{
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354)),
|
|
|
|
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("6.117700000000000000")),
|
|
|
|
},
|
|
|
|
expectedRewards: cs(c("hard", 6117700)),
|
2021-05-04 14:47:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"10 blocks - long block time",
|
|
|
|
args{
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354)),
|
|
|
|
blockTimes: []int{86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400},
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("52856.928000000000000000")),
|
|
|
|
},
|
|
|
|
expectedRewards: cs(c("hard", 52856928000)),
|
2021-05-04 14:47:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"delegator reward index updated when reward is zero",
|
|
|
|
args{
|
2021-07-05 22:01:25 +00:00
|
|
|
delegation: c("ukava", 1),
|
|
|
|
rewardsPerSecond: cs(c("hard", 1)),
|
|
|
|
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("0.000099999900000100")),
|
|
|
|
},
|
|
|
|
expectedRewards: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple reward coins",
|
|
|
|
args{
|
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354), c("swp", 56789)),
|
|
|
|
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("6.117700000000000000")),
|
|
|
|
types.NewRewardIndex("swp", d("2.839450000000000000")),
|
|
|
|
},
|
|
|
|
expectedRewards: cs(c("hard", 6117700), c("swp", 2839450)),
|
2021-05-04 14:47:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder := app.NewAuthBankGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[0]), cs(c("ukava", 1e9)))
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
incentBuilder := testutil.NewIncentiveGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithGenesisTime(suite.genesisTime).
|
|
|
|
WithSimpleDelegatorRewardPeriod(tc.args.delegation.Denom, tc.args.rewardsPerSecond)
|
|
|
|
|
|
|
|
suite.SetupWithGenState(authBuilder, incentBuilder)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
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)
|
|
|
|
|
|
|
|
// Check that validator account has been created and delegation was successful
|
|
|
|
valAcc, found := suite.stakingKeeper.GetValidator(suite.ctx, suite.validatorAddrs[0])
|
|
|
|
suite.True(found)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().Equal(valAcc.Status, stakingtypes.Bonded)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().Equal(valAcc.Tokens, tc.args.delegation.Amount.Add(selfDelegationCoins.Amount))
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
// Check that Staking hooks initialized a DelegatorClaim
|
|
|
|
claim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
for _, rewardIndex := range claim.RewardIndexes[0].RewardIndexes {
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(sdk.ZeroDec(), rewardIndex.RewardFactor)
|
|
|
|
}
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Run accumulator at several intervals
|
|
|
|
var timeElapsed int
|
|
|
|
previousBlockTime := suite.ctx.BlockTime()
|
|
|
|
for _, t := range tc.args.blockTimes {
|
|
|
|
timeElapsed += t
|
|
|
|
updatedBlockTime := previousBlockTime.Add(time.Duration(int(time.Second) * t))
|
|
|
|
previousBlockTime = updatedBlockTime
|
|
|
|
blockCtx := suite.ctx.WithBlockTime(updatedBlockTime)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardPeriods, found := suite.keeper.GetDelegatorRewardPeriods(blockCtx, tc.args.delegation.Denom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
suite.keeper.AccumulateDelegatorRewards(blockCtx, rewardPeriods)
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
updatedBlockTime := suite.ctx.BlockTime().Add(time.Duration(int(time.Second) * timeElapsed))
|
|
|
|
suite.ctx = suite.ctx.WithBlockTime(updatedBlockTime)
|
|
|
|
|
|
|
|
// After we've accumulated, run synchronize
|
|
|
|
suite.Require().NotPanics(func() {
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.keeper.SynchronizeDelegatorRewards(suite.ctx, suite.addrs[0], nil, false)
|
2021-05-04 14:47:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Check that reward factor and claim have been updated as expected
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardIndexes, _ := suite.keeper.GetDelegatorRewardIndexes(suite.ctx, tc.args.delegation.Denom)
|
2021-07-05 22:01:25 +00:00
|
|
|
for i, rewardPerSecond := range tc.args.rewardsPerSecond {
|
|
|
|
rewardFactor, _ := rewardIndexes.Get(rewardPerSecond.Denom)
|
|
|
|
suite.Require().Equal(tc.args.expectedRewardIndexes[i].RewardFactor, rewardFactor)
|
|
|
|
}
|
2021-05-04 14:47:21 +00:00
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
claim, found = suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
for i, delegatorRewardIndex := range claim.RewardIndexes[0].RewardIndexes {
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(tc.args.expectedRewardIndexes[i].RewardFactor, delegatorRewardIndex.RewardFactor)
|
|
|
|
}
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().Equal(tc.args.expectedRewards, claim.Reward)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) TestSimulateDelegatorRewardSynchronization() {
|
2021-05-04 14:47:21 +00:00
|
|
|
type args struct {
|
|
|
|
delegation sdk.Coin
|
2021-07-05 22:01:25 +00:00
|
|
|
rewardsPerSecond sdk.Coins
|
2021-05-04 14:47:21 +00:00
|
|
|
blockTimes []int
|
|
|
|
expectedRewardIndexes types.RewardIndexes
|
|
|
|
expectedRewards sdk.Coins
|
|
|
|
}
|
|
|
|
type test struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []test{
|
|
|
|
{
|
|
|
|
"10 blocks",
|
|
|
|
args{
|
|
|
|
delegation: c("ukava", 1_000_000),
|
2021-07-05 22:01:25 +00:00
|
|
|
rewardsPerSecond: cs(c("hard", 122354)),
|
2021-05-04 14:47:21 +00:00
|
|
|
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
2021-07-05 22:01:25 +00:00
|
|
|
expectedRewardIndexes: types.RewardIndexes{types.NewRewardIndex("hard", d("6.117700000000000000"))},
|
2021-05-04 14:47:21 +00:00
|
|
|
expectedRewards: cs(c("hard", 6117700)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"10 blocks - long block time",
|
|
|
|
args{
|
|
|
|
delegation: c("ukava", 1_000_000),
|
2021-07-05 22:01:25 +00:00
|
|
|
rewardsPerSecond: cs(c("hard", 122354)),
|
2021-05-04 14:47:21 +00:00
|
|
|
blockTimes: []int{86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400},
|
2021-07-05 22:01:25 +00:00
|
|
|
expectedRewardIndexes: types.RewardIndexes{types.NewRewardIndex("hard", d("52856.928000000000000000"))},
|
2021-05-04 14:47:21 +00:00
|
|
|
expectedRewards: cs(c("hard", 52856928000)),
|
|
|
|
},
|
|
|
|
},
|
2021-07-05 22:01:25 +00:00
|
|
|
{
|
|
|
|
"multiple rewards coins",
|
|
|
|
args{
|
|
|
|
delegation: c("ukava", 1_000_000),
|
|
|
|
rewardsPerSecond: cs(c("hard", 122354), c("swp", 56789)),
|
|
|
|
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
|
|
expectedRewardIndexes: types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("6.117700000000000000")),
|
|
|
|
types.NewRewardIndex("swp", d("2.839450000000000000")),
|
|
|
|
},
|
|
|
|
expectedRewards: cs(c("hard", 6117700), c("swp", 2839450)),
|
|
|
|
},
|
|
|
|
},
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder := app.NewAuthBankGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[0]), cs(c("ukava", 1e9)))
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
incentBuilder := testutil.NewIncentiveGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithGenesisTime(suite.genesisTime).
|
|
|
|
WithSimpleDelegatorRewardPeriod(tc.args.delegation.Denom, tc.args.rewardsPerSecond)
|
|
|
|
|
|
|
|
suite.SetupWithGenState(authBuilder, incentBuilder)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Delegator delegates
|
|
|
|
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], tc.args.delegation)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[0], tc.args.delegation)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
staking.EndBlocker(suite.ctx, suite.stakingKeeper)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
// Check that Staking hooks initialized a DelegatorClaim
|
|
|
|
claim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
for _, rewardIndex := range claim.RewardIndexes[0].RewardIndexes {
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(sdk.ZeroDec(), rewardIndex.RewardFactor)
|
|
|
|
}
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Run accumulator at several intervals
|
|
|
|
var timeElapsed int
|
|
|
|
previousBlockTime := suite.ctx.BlockTime()
|
|
|
|
for _, t := range tc.args.blockTimes {
|
|
|
|
timeElapsed += t
|
|
|
|
updatedBlockTime := previousBlockTime.Add(time.Duration(int(time.Second) * t))
|
|
|
|
previousBlockTime = updatedBlockTime
|
|
|
|
blockCtx := suite.ctx.WithBlockTime(updatedBlockTime)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
// Accumulate delegator rewards
|
|
|
|
rewardPeriods, found := suite.keeper.GetDelegatorRewardPeriods(blockCtx, tc.args.delegation.Denom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-22 12:53:18 +00:00
|
|
|
suite.keeper.AccumulateDelegatorRewards(blockCtx, rewardPeriods)
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
updatedBlockTime := suite.ctx.BlockTime().Add(time.Duration(int(time.Second) * timeElapsed))
|
|
|
|
suite.ctx = suite.ctx.WithBlockTime(updatedBlockTime)
|
|
|
|
|
|
|
|
// Check that the synced claim held in memory has properly simulated syncing
|
2021-07-07 16:50:14 +00:00
|
|
|
syncedClaim := suite.keeper.SimulateDelegatorSynchronization(suite.ctx, claim)
|
2021-07-05 22:01:25 +00:00
|
|
|
|
|
|
|
for i, expectedRewardIndex := range tc.args.expectedRewardIndexes {
|
2021-05-04 14:47:21 +00:00
|
|
|
// Check that the user's claim's reward index matches the expected reward index
|
2021-07-07 16:50:14 +00:00
|
|
|
multiRewardIndex, found := syncedClaim.RewardIndexes.Get(types.BondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(expectedRewardIndex, multiRewardIndex[i])
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Check that the user's claim holds the expected amount of reward coins
|
|
|
|
suite.Require().Equal(
|
|
|
|
tc.args.expectedRewards.AmountOf(expectedRewardIndex.CollateralType),
|
|
|
|
syncedClaim.Reward.AmountOf(expectedRewardIndex.CollateralType),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) deliverMsgCreateValidator(ctx sdk.Context, address sdk.ValAddress, selfDelegation sdk.Coin) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
msg, err := stakingtypes.NewMsgCreateValidator(
|
2021-05-04 14:47:21 +00:00
|
|
|
address,
|
|
|
|
ed25519.GenPrivKey().PubKey(),
|
|
|
|
selfDelegation,
|
2022-01-08 00:39:27 +00:00
|
|
|
stakingtypes.Description{},
|
|
|
|
stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
|
2021-05-04 14:47:21 +00:00
|
|
|
sdk.NewInt(1_000_000),
|
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:47:21 +00:00
|
|
|
handleStakingMsg := staking.NewHandler(suite.stakingKeeper)
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err = handleStakingMsg(ctx, msg)
|
2021-05-04 14:47:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) deliverMsgDelegate(ctx sdk.Context, delegator sdk.AccAddress, validator sdk.ValAddress, amount sdk.Coin) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := stakingtypes.NewMsgDelegate(
|
2021-05-04 14:47:21 +00:00
|
|
|
delegator,
|
|
|
|
validator,
|
|
|
|
amount,
|
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2021-05-04 14:47:21 +00:00
|
|
|
handleStakingMsg := staking.NewHandler(suite.stakingKeeper)
|
|
|
|
_, err := handleStakingMsg(ctx, msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) deliverMsgRedelegate(ctx sdk.Context, delegator sdk.AccAddress, sourceValidator, destinationValidator sdk.ValAddress, amount sdk.Coin) error {
|
2022-01-08 00:39:27 +00:00
|
|
|
msg := stakingtypes.NewMsgBeginRedelegate(
|
2021-05-04 14:47:21 +00:00
|
|
|
delegator,
|
|
|
|
sourceValidator,
|
|
|
|
destinationValidator,
|
|
|
|
amount,
|
|
|
|
)
|
|
|
|
handleStakingMsg := staking.NewHandler(suite.stakingKeeper)
|
|
|
|
_, err := handleStakingMsg(ctx, msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// given a user has a delegation to a bonded validator, when the validator starts unbonding, the user does not accumulate rewards
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) TestUnbondingValidatorSyncsClaim() {
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder := app.NewAuthBankGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(suite.addrs[2], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[0]), cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[1]), cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[2]), cs(c("ukava", 1e9)))
|
2021-05-04 14:47:21 +00:00
|
|
|
|
2021-07-05 22:01:25 +00:00
|
|
|
rewardsPerSecond := cs(c("hard", 122354))
|
2021-05-04 14:47:21 +00:00
|
|
|
bondDenom := "ukava"
|
2021-06-10 13:35:44 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
incentBuilder := testutil.NewIncentiveGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithGenesisTime(suite.genesisTime).
|
|
|
|
WithSimpleDelegatorRewardPeriod(bondDenom, rewardsPerSecond)
|
|
|
|
|
|
|
|
suite.SetupWithGenState(authBuilder, incentBuilder)
|
|
|
|
|
|
|
|
blockDuration := 10 * time.Second
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Reduce the size of the validator set
|
|
|
|
stakingParams := suite.app.GetStakingKeeper().GetParams(suite.ctx)
|
|
|
|
stakingParams.MaxValidators = 2
|
|
|
|
suite.app.GetStakingKeeper().SetParams(suite.ctx, stakingParams)
|
|
|
|
|
|
|
|
// Create 3 validators
|
|
|
|
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], c(bondDenom, 10_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[1], c(bondDenom, 5_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[2], c(bondDenom, 1_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// End the block so top validators become bonded
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(1 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{}) // height and time in header are ignored by module begin blockers
|
|
|
|
|
|
|
|
// Delegate to a bonded validator from the test user. This will initialize their incentive claim.
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[1], c(bondDenom, 1_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Start a new block to accumulate some delegation rewards for the user.
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(2 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{}) // height and time in header are ignored by module begin blockers
|
|
|
|
|
|
|
|
// Delegate to the unbonded validator to push it into the bonded validator set, pushing out the user's delegated validator
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[2], suite.validatorAddrs[2], c(bondDenom, 8_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// End the block to start unbonding the user's validator
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
|
|
|
// but don't start the next block as it will accumulate delegator rewards and we won't be able to tell if the user's reward was synced.
|
|
|
|
|
|
|
|
// Check that the user's claim has been synced. ie rewards added, index updated
|
2021-07-07 16:50:14 +00:00
|
|
|
claim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardIndexes, found := suite.keeper.GetDelegatorRewardIndexes(suite.ctx, bondDenom)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
globalIndex, found := rewardIndexes.Get(rewardsPerSecond[0].Denom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
claimIndex, found := claim.RewardIndexes.GetRewardIndex(bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(globalIndex, claimIndex.RewardIndexes[0].RewardFactor)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
suite.Require().Equal(
|
2021-07-05 22:01:25 +00:00
|
|
|
cs(c(rewardsPerSecond[0].Denom, 76471)),
|
2021-05-04 14:47:21 +00:00
|
|
|
claim.Reward,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run another block and check the claim is not accumulating more rewards
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(3 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{})
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.keeper.SynchronizeDelegatorRewards(suite.ctx, suite.addrs[0], nil, false)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// rewards are the same as before
|
2021-07-07 16:50:14 +00:00
|
|
|
laterClaim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(claim.Reward, laterClaim.Reward)
|
|
|
|
|
|
|
|
// claim index has been updated to latest global value
|
2021-07-07 16:50:14 +00:00
|
|
|
laterClaimIndex, found := laterClaim.RewardIndexes.GetRewardIndex(bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardIndexes, found = suite.keeper.GetDelegatorRewardIndexes(suite.ctx, bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
globalIndex, found = rewardIndexes.Get(rewardsPerSecond[0].Denom)
|
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(globalIndex, laterClaimIndex.RewardIndexes[0].RewardFactor)
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// given a user has a delegation to an unbonded validator, when the validator becomes bonded, the user starts accumulating rewards
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) TestBondingValidatorSyncsClaim() {
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder := app.NewAuthBankGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(suite.addrs[2], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[0]), cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[1]), cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[2]), cs(c("ukava", 1e9)))
|
2021-05-04 14:47:21 +00:00
|
|
|
|
2021-07-05 22:01:25 +00:00
|
|
|
rewardsPerSecond := cs(c("hard", 122354))
|
2021-05-04 14:47:21 +00:00
|
|
|
bondDenom := "ukava"
|
2021-06-10 13:35:44 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
incentBuilder := testutil.NewIncentiveGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithGenesisTime(suite.genesisTime).
|
|
|
|
WithSimpleDelegatorRewardPeriod(bondDenom, rewardsPerSecond)
|
|
|
|
|
|
|
|
suite.SetupWithGenState(authBuilder, incentBuilder)
|
|
|
|
|
|
|
|
blockDuration := 10 * time.Second
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Reduce the size of the validator set
|
|
|
|
stakingParams := suite.app.GetStakingKeeper().GetParams(suite.ctx)
|
|
|
|
stakingParams.MaxValidators = 2
|
|
|
|
suite.app.GetStakingKeeper().SetParams(suite.ctx, stakingParams)
|
|
|
|
|
|
|
|
// Create 3 validators
|
|
|
|
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], c(bondDenom, 10_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[1], c(bondDenom, 5_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[2], c(bondDenom, 1_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// End the block so top validators become bonded
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(1 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{}) // height and time in header are ignored by module begin blockers
|
|
|
|
|
|
|
|
// Delegate to an unbonded validator from the test user. This will initialize their incentive claim.
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[2], c(bondDenom, 1_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Start a new block to accumulate some delegation rewards globally.
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(2 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{})
|
|
|
|
|
|
|
|
// Delegate to the user's unbonded validator to push it into the bonded validator set
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[2], suite.validatorAddrs[2], c(bondDenom, 4_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// End the block to bond the user's validator
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
|
|
|
// but don't start the next block as it will accumulate delegator rewards and we won't be able to tell if the user's reward was synced.
|
|
|
|
|
|
|
|
// Check that the user's claim has been synced. ie rewards added, index updated
|
2021-07-07 16:50:14 +00:00
|
|
|
claim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardIndexes, found := suite.keeper.GetDelegatorRewardIndexes(suite.ctx, bondDenom)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
globalIndex, found := rewardIndexes.Get(rewardsPerSecond[0].Denom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
claimIndex, found := claim.RewardIndexes.GetRewardIndex(bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(globalIndex, claimIndex.RewardIndexes[0].RewardFactor)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
suite.Require().Equal(
|
|
|
|
sdk.Coins(nil),
|
|
|
|
claim.Reward,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run another block and check the claim is accumulating more rewards
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(3 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{})
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.keeper.SynchronizeDelegatorRewards(suite.ctx, suite.addrs[0], nil, false)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// rewards are greater than before
|
2021-07-07 16:50:14 +00:00
|
|
|
laterClaim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().True(laterClaim.Reward.IsAllGT(claim.Reward))
|
|
|
|
|
|
|
|
// claim index has been updated to latest global value
|
2021-07-07 16:50:14 +00:00
|
|
|
laterClaimIndex, found := laterClaim.RewardIndexes.GetRewardIndex(bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardIndexes, found = suite.keeper.GetDelegatorRewardIndexes(suite.ctx, bondDenom)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
globalIndex, found = rewardIndexes.Get(rewardsPerSecond[0].Denom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(globalIndex, laterClaimIndex.RewardIndexes[0].RewardFactor)
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a validator is slashed delegators should have their claims synced
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) TestSlashingValidatorSyncsClaim() {
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder := app.NewAuthBankGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[0]), cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[1]), cs(c("ukava", 1e9)))
|
2021-05-04 14:47:21 +00:00
|
|
|
|
2021-07-05 22:01:25 +00:00
|
|
|
rewardsPerSecond := cs(c("hard", 122354))
|
2021-05-04 14:47:21 +00:00
|
|
|
bondDenom := "ukava"
|
2021-06-10 13:35:44 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
incentBuilder := testutil.NewIncentiveGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithGenesisTime(suite.genesisTime).
|
|
|
|
WithSimpleDelegatorRewardPeriod(bondDenom, rewardsPerSecond)
|
|
|
|
|
|
|
|
suite.SetupWithGenState(authBuilder, incentBuilder)
|
|
|
|
|
|
|
|
blockDuration := 10 * time.Second
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Reduce the size of the validator set
|
|
|
|
stakingParams := suite.app.GetStakingKeeper().GetParams(suite.ctx)
|
|
|
|
stakingParams.MaxValidators = 2
|
|
|
|
suite.app.GetStakingKeeper().SetParams(suite.ctx, stakingParams)
|
|
|
|
|
|
|
|
// Create 2 validators
|
|
|
|
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], c(bondDenom, 10_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[1], c(bondDenom, 10_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// End the block so validators become bonded
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(1 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{}) // height and time in header are ignored by module begin blockers
|
|
|
|
|
|
|
|
// Delegate to a bonded validator from the test user. This will initialize their incentive claim.
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[1], c(bondDenom, 1_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Check that claim has been created with synced reward index but no reward coins
|
2021-07-07 16:50:14 +00:00
|
|
|
initialClaim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
initialGlobalIndex, found := suite.keeper.GetDelegatorRewardIndexes(suite.ctx, bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
initialClaimIndex, found := initialClaim.RewardIndexes.GetRewardIndex(bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(initialGlobalIndex, initialClaimIndex.RewardIndexes)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.True(initialClaim.Reward.Empty()) // Initial claim should not have any rewards
|
|
|
|
|
|
|
|
// Start a new block to accumulate some delegation rewards for the user.
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(2 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{}) // height and time in header are ignored by module begin blockers
|
|
|
|
|
|
|
|
// Fetch validator and slash them
|
|
|
|
stakingKeeper := suite.app.GetStakingKeeper()
|
|
|
|
validator, found := stakingKeeper.GetValidator(suite.ctx, suite.validatorAddrs[1])
|
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().True(validator.GetTokens().IsPositive())
|
|
|
|
fraction := sdk.NewDecWithPrec(5, 1)
|
2022-01-08 00:39:27 +00:00
|
|
|
|
|
|
|
consAddr, err := validator.GetConsAddr()
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
stakingKeeper.Slash(suite.ctx, consAddr, suite.ctx.BlockHeight(), 10, fraction)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Check that the user's claim has been synced. ie rewards added, index updated
|
2021-07-07 16:50:14 +00:00
|
|
|
claim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
globalIndex, found := suite.keeper.GetDelegatorRewardIndexes(suite.ctx, bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
claimIndex, found := claim.RewardIndexes.GetRewardIndex(bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(globalIndex, claimIndex.RewardIndexes)
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Check that rewards were added
|
|
|
|
suite.Require().Equal(
|
2021-07-05 22:01:25 +00:00
|
|
|
cs(c(rewardsPerSecond[0].Denom, 58264)),
|
2021-05-04 14:47:21 +00:00
|
|
|
claim.Reward,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check that reward factor increased from initial value
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.True(claimIndex.RewardIndexes[0].RewardFactor.GT(initialClaimIndex.RewardIndexes[0].RewardFactor))
|
2021-05-04 14:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given a delegation to a bonded validator, when a user redelegates everything to another (bonded) validator, the user's claim is synced
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *DelegatorRewardsTestSuite) TestRedelegationSyncsClaim() {
|
2022-01-08 00:39:27 +00:00
|
|
|
authBuilder := app.NewAuthBankGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[0]), cs(c("ukava", 1e9))).
|
|
|
|
WithSimpleAccount(sdk.AccAddress(suite.validatorAddrs[1]), cs(c("ukava", 1e9)))
|
2021-05-04 14:47:21 +00:00
|
|
|
|
2021-07-05 22:01:25 +00:00
|
|
|
rewardsPerSecond := cs(c("hard", 122354))
|
2021-05-04 14:47:21 +00:00
|
|
|
bondDenom := "ukava"
|
2021-06-10 13:35:44 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
incentBuilder := testutil.NewIncentiveGenesisBuilder().
|
2021-06-10 13:35:44 +00:00
|
|
|
WithGenesisTime(suite.genesisTime).
|
|
|
|
WithSimpleDelegatorRewardPeriod(bondDenom, rewardsPerSecond)
|
|
|
|
|
|
|
|
suite.SetupWithGenState(authBuilder, incentBuilder)
|
|
|
|
|
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime)
|
|
|
|
blockDuration := 10 * time.Second
|
2021-05-04 14:47:21 +00:00
|
|
|
|
|
|
|
// Create 2 validators
|
|
|
|
err := suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[0], c(bondDenom, 10_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
err = suite.deliverMsgCreateValidator(suite.ctx, suite.validatorAddrs[1], c(bondDenom, 5_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Delegatefrom the test user. This will initialize their incentive claim.
|
|
|
|
err = suite.deliverMsgDelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[0], c(bondDenom, 1_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Start a new block to accumulate some delegation rewards globally.
|
|
|
|
_ = suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(1 * blockDuration))
|
2021-05-04 14:47:21 +00:00
|
|
|
_ = suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{}) // height and time in header are ignored by module begin blockers
|
|
|
|
|
|
|
|
// Redelegate the user's delegation between the two validators. This should trigger hooks that sync the user's claim.
|
|
|
|
err = suite.deliverMsgRedelegate(suite.ctx, suite.addrs[0], suite.validatorAddrs[0], suite.validatorAddrs[1], c(bondDenom, 1_000_000))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// Check that the user's claim has been synced. ie rewards added, index updated
|
2021-07-07 16:50:14 +00:00
|
|
|
claim, found := suite.keeper.GetDelegatorClaim(suite.ctx, suite.addrs[0])
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
globalIndex, found := suite.keeper.GetDelegatorRewardIndexes(suite.ctx, bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-07 16:50:14 +00:00
|
|
|
claimIndex, found := claim.RewardIndexes.GetRewardIndex(bondDenom)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().True(found)
|
2021-07-05 22:01:25 +00:00
|
|
|
suite.Require().Equal(globalIndex, claimIndex.RewardIndexes)
|
2021-05-04 14:47:21 +00:00
|
|
|
suite.Require().Equal(
|
2021-07-05 22:01:25 +00:00
|
|
|
cs(c(rewardsPerSecond[0].Denom, 76471)),
|
2021-05-04 14:47:21 +00:00
|
|
|
claim.Reward,
|
|
|
|
)
|
|
|
|
}
|
2021-06-10 13:35:44 +00:00
|
|
|
|
|
|
|
func TestDelegatorRewardsTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(DelegatorRewardsTestSuite))
|
|
|
|
}
|