0g-chain/x/incentive/keeper/rewards_delegator_init_test.go
Ruaridh 6f193c7f2a
Refactor incentive accumulators to be the same (#970)
* add test for validate multi reward periods

* tidy up: combine files

* don't accumulate global indexes containing zeros
Previously if the time since last block was 0,
indexes were added containing 0s.
Now leave them out. Missing is assumed to be 0.

* move state independent test to types folder

* clarify reward source concept to "source shares"
- rename variables and update doc comments
- extract method from swap accumulation

* tidy up and expand swap accumulation unit tests

* rename swap test file to match others

* update swap pool id format in tests

* refactor borrow accumulation, use new accumulator

* refactor supply accumulation, use new accumulator

* refactor delegator accumulation, use accumulator

* refactor usdx accumulation, use new accumulator

* fix types const

* remove unsed methods

* more usdx minting param validation.
Protect against the rewards per second denom changing.
It should always be "ukava".

* add safety check in InitGenesis
It prevents huge accumulations on the first block by limiting all
previous accumulation times to be within one year of genesis

* add todo for adding swp token distirbution info
2021-07-22 13:53:18 +01:00

99 lines
3.3 KiB
Go

package keeper_test
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/suite"
"github.com/kava-labs/kava/x/incentive/types"
)
// InitializeDelegatorRewardTests runs unit tests for the keeper.InitializeDelegatorReward method
//
// inputs
// - claim in store if it exists (only claim.DelegatorRewardIndexes)
// - global indexes in store
// - delegator function arg
//
// outputs
// - sets or creates a claim
type InitializeDelegatorRewardTests struct {
unitTester
}
func TestInitializeDelegatorReward(t *testing.T) {
suite.Run(t, new(InitializeDelegatorRewardTests))
}
// 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() {
globalIndex := arbitraryDelegatorRewardIndexes
suite.storeGlobalDelegatorIndexes(globalIndex)
delegator := arbitraryAddress()
suite.keeper.InitializeDelegatorReward(suite.ctx, delegator)
syncedClaim, f := suite.keeper.GetDelegatorClaim(suite.ctx, delegator)
suite.True(f)
suite.Equal(globalIndex, syncedClaim.RewardIndexes)
}
func (suite *InitializeDelegatorRewardTests) TestClaimIsSyncedAndIndexesAreSetWhenClaimDoesExist() {
validatorAddress := arbitraryValidatorAddress()
sk := &fakeStakingKeeper{
delegations: stakingtypes.Delegations{{
ValidatorAddress: validatorAddress,
Shares: d("1000"),
}},
validators: stakingtypes.Validators{{
OperatorAddress: validatorAddress,
Status: sdk.Bonded,
Tokens: i(1000),
DelegatorShares: d("1000"),
}},
}
suite.keeper = suite.NewKeeper(&fakeParamSubspace{}, nil, nil, nil, nil, sk, nil)
claim := types.DelegatorClaim{
BaseMultiClaim: types.BaseMultiClaim{
Owner: arbitraryAddress(),
},
RewardIndexes: arbitraryDelegatorRewardIndexes,
}
suite.storeDelegatorClaim(claim)
// Set the global factor to a value different to one in claim so
// we can detect if it is overwritten.
rewardIndexes, _ := claim.RewardIndexes.Get(types.BondDenom)
globalIndexes := increaseRewardFactors(rewardIndexes)
// Update the claim object with the new global factor
bondIndex, _ := claim.RewardIndexes.GetRewardIndexIndex(types.BondDenom)
claim.RewardIndexes[bondIndex].RewardIndexes = globalIndexes
suite.storeGlobalDelegatorFactor(claim.RewardIndexes)
suite.keeper.InitializeDelegatorReward(suite.ctx, claim.Owner)
syncedClaim, _ := suite.keeper.GetDelegatorClaim(suite.ctx, claim.Owner)
suite.Equal(globalIndexes, syncedClaim.RewardIndexes[bondIndex].RewardIndexes)
suite.Truef(syncedClaim.Reward.IsAllGT(claim.Reward), "'%s' not greater than '%s'", syncedClaim.Reward, claim.Reward)
}
// arbitraryDelegatorRewardIndexes contains only one reward index as there is only ever one bond denom
var arbitraryDelegatorRewardIndexes = types.MultiRewardIndexes{
types.NewMultiRewardIndex(
types.BondDenom,
types.RewardIndexes{
types.NewRewardIndex("hard", d("0.2")),
types.NewRewardIndex("swp", d("0.2")),
},
),
}