2021-06-21 21:05:17 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/incentive/types"
|
2021-06-21 21:05:17 +00:00
|
|
|
)
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
// InitializeDelegatorRewardTests runs unit tests for the keeper.InitializeDelegatorReward method
|
2021-06-21 21:05:17 +00:00
|
|
|
//
|
|
|
|
// inputs
|
|
|
|
// - claim in store if it exists (only claim.DelegatorRewardIndexes)
|
|
|
|
// - global indexes in store
|
|
|
|
// - delegator function arg
|
|
|
|
//
|
|
|
|
// outputs
|
|
|
|
// - sets or creates a claim
|
2021-07-07 16:50:14 +00:00
|
|
|
type InitializeDelegatorRewardTests struct {
|
2021-06-21 21:05:17 +00:00
|
|
|
unitTester
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
func TestInitializeDelegatorReward(t *testing.T) {
|
|
|
|
suite.Run(t, new(InitializeDelegatorRewardTests))
|
2021-06-21 21:05:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
// Hardcoded to use bond denom
|
|
|
|
func (suite *InitializeDelegatorRewardTests) storeGlobalDelegatorFactor(multiRewardIndexes types.MultiRewardIndexes) {
|
|
|
|
multiRewardIndex, _ := multiRewardIndexes.GetRewardIndex(types.BondDenom)
|
|
|
|
suite.keeper.SetDelegatorRewardIndexes(suite.ctx, types.BondDenom, multiRewardIndex.RewardIndexes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *InitializeDelegatorRewardTests) TestClaimIndexesAreSetWhenClaimDoesNotExist() {
|
2021-06-21 21:05:17 +00:00
|
|
|
globalIndex := arbitraryDelegatorRewardIndexes
|
2021-07-07 13:23:06 +00:00
|
|
|
suite.storeGlobalDelegatorIndexes(globalIndex)
|
2021-06-21 21:05:17 +00:00
|
|
|
|
|
|
|
delegator := arbitraryAddress()
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.keeper.InitializeDelegatorReward(suite.ctx, delegator)
|
2021-06-21 21:05:17 +00:00
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
syncedClaim, f := suite.keeper.GetDelegatorClaim(suite.ctx, delegator)
|
2021-06-21 21:05:17 +00:00
|
|
|
suite.True(f)
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.Equal(globalIndex, syncedClaim.RewardIndexes)
|
2021-06-21 21:05:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
func (suite *InitializeDelegatorRewardTests) TestClaimIsSyncedAndIndexesAreSetWhenClaimDoesExist() {
|
2021-06-21 21:05:17 +00:00
|
|
|
validatorAddress := arbitraryValidatorAddress()
|
2021-07-22 12:53:18 +00:00
|
|
|
sk := &fakeStakingKeeper{
|
2021-06-21 21:05:17 +00:00
|
|
|
delegations: stakingtypes.Delegations{{
|
2022-01-08 00:39:27 +00:00
|
|
|
ValidatorAddress: validatorAddress.String(),
|
2021-06-21 21:05:17 +00:00
|
|
|
Shares: d("1000"),
|
|
|
|
}},
|
|
|
|
validators: stakingtypes.Validators{{
|
2022-01-08 00:39:27 +00:00
|
|
|
OperatorAddress: validatorAddress.String(),
|
|
|
|
Status: stakingtypes.Bonded,
|
2021-06-21 21:05:17 +00:00
|
|
|
Tokens: i(1000),
|
|
|
|
DelegatorShares: d("1000"),
|
|
|
|
}},
|
|
|
|
}
|
2022-09-23 16:38:22 +00:00
|
|
|
suite.keeper = suite.NewKeeper(&fakeParamSubspace{}, nil, nil, nil, nil, sk, nil, nil, nil, nil)
|
2021-06-21 21:05:17 +00:00
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
claim := types.DelegatorClaim{
|
2021-06-21 21:05:17 +00:00
|
|
|
BaseMultiClaim: types.BaseMultiClaim{
|
|
|
|
Owner: arbitraryAddress(),
|
|
|
|
},
|
2021-07-07 16:50:14 +00:00
|
|
|
RewardIndexes: arbitraryDelegatorRewardIndexes,
|
2021-06-21 21:05:17 +00:00
|
|
|
}
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.storeDelegatorClaim(claim)
|
2021-06-21 21:05:17 +00:00
|
|
|
|
|
|
|
// Set the global factor to a value different to one in claim so
|
|
|
|
// we can detect if it is overwritten.
|
2021-07-07 16:50:14 +00:00
|
|
|
rewardIndexes, _ := claim.RewardIndexes.Get(types.BondDenom)
|
2021-07-05 22:01:25 +00:00
|
|
|
globalIndexes := increaseRewardFactors(rewardIndexes)
|
|
|
|
|
|
|
|
// Update the claim object with the new global factor
|
2021-07-07 16:50:14 +00:00
|
|
|
bondIndex, _ := claim.RewardIndexes.GetRewardIndexIndex(types.BondDenom)
|
|
|
|
claim.RewardIndexes[bondIndex].RewardIndexes = globalIndexes
|
|
|
|
suite.storeGlobalDelegatorFactor(claim.RewardIndexes)
|
2021-06-21 21:05:17 +00:00
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
suite.keeper.InitializeDelegatorReward(suite.ctx, claim.Owner)
|
2021-06-21 21:05:17 +00:00
|
|
|
|
2021-07-07 16:50:14 +00:00
|
|
|
syncedClaim, _ := suite.keeper.GetDelegatorClaim(suite.ctx, claim.Owner)
|
|
|
|
suite.Equal(globalIndexes, syncedClaim.RewardIndexes[bondIndex].RewardIndexes)
|
2021-06-21 21:05:17 +00:00
|
|
|
suite.Truef(syncedClaim.Reward.IsAllGT(claim.Reward), "'%s' not greater than '%s'", syncedClaim.Reward, claim.Reward)
|
|
|
|
}
|
|
|
|
|
2021-07-07 13:23:06 +00:00
|
|
|
// arbitraryDelegatorRewardIndexes contains only one reward index as there is only ever one bond denom
|
2021-07-05 22:01:25 +00:00
|
|
|
var arbitraryDelegatorRewardIndexes = types.MultiRewardIndexes{
|
|
|
|
types.NewMultiRewardIndex(
|
|
|
|
types.BondDenom,
|
|
|
|
types.RewardIndexes{
|
|
|
|
types.NewRewardIndex("hard", d("0.2")),
|
|
|
|
types.NewRewardIndex("swp", d("0.2")),
|
|
|
|
},
|
|
|
|
),
|
2021-06-21 21:05:17 +00:00
|
|
|
}
|