0g-chain/x/incentive/keeper/keeper_test.go
Ruaridh c7962e45c0
Swaps accumulate global rewards (#947)
* add get set methods for swap reward indexes

* add get set methods for swap accrual time

* tidy up location of multi periods

* add swap reward periods to params

* add initial legacy types for incentive

* minor refactor of migration code

* add incentive migration for swap params

* minor incentive test refactors

* add math methods to RewardIndexes

* add keeper method to increment global indexes

* add swap keeper to incentive keeper

* indicate if pool shares were found or not

* add accumulator to compute new rewards each block

* accumulate swap rewards globally

* remove unecessary keeper method

* expand doc comments on accumulator methods

* test precision not lost in accumulation

* minor fixes from merge

* rename storeGlobalDelegatorFactor to match others

* fix migration from merge

* fix bug in app setup

* fix accumulation bug when starting with no state

* rename swap files to match others

* add swap accumulation times to genesis

* remove old migration refactor

* minor updates to spec

* add high level description of how rewards work
2021-07-07 14:23:06 +01:00

202 lines
5.1 KiB
Go

package keeper_test
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/incentive/keeper"
"github.com/kava-labs/kava/x/incentive/types"
)
// Test suite used for all keeper tests
type KeeperTestSuite struct {
suite.Suite
keeper keeper.Keeper
app app.TestApp
ctx sdk.Context
genesisTime time.Time
addrs []sdk.AccAddress
}
// SetupTest is run automatically before each suite test
func (suite *KeeperTestSuite) SetupTest() {
config := sdk.GetConfig()
app.SetBech32AddressPrefixes(config)
_, suite.addrs = app.GeneratePrivKeyAddressPairs(5)
suite.genesisTime = time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC)
}
func (suite *KeeperTestSuite) SetupApp() {
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetIncentiveKeeper()
suite.ctx = suite.app.NewContext(true, abci.Header{Height: 1, Time: suite.genesisTime})
}
func (suite *KeeperTestSuite) TestGetSetDeleteUSDXMintingClaim() {
suite.SetupApp()
c := types.NewUSDXMintingClaim(suite.addrs[0], c("ukava", 1000000), types.RewardIndexes{types.NewRewardIndex("bnb-a", sdk.ZeroDec())})
_, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
suite.Require().False(found)
suite.Require().NotPanics(func() {
suite.keeper.SetUSDXMintingClaim(suite.ctx, c)
})
testC, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
suite.Require().True(found)
suite.Require().Equal(c, testC)
suite.Require().NotPanics(func() {
suite.keeper.DeleteUSDXMintingClaim(suite.ctx, suite.addrs[0])
})
_, found = suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
suite.Require().False(found)
}
func (suite *KeeperTestSuite) TestIterateUSDXMintingClaims() {
suite.SetupApp()
for i := 0; i < len(suite.addrs); i++ {
c := types.NewUSDXMintingClaim(suite.addrs[i], c("ukava", 100000), types.RewardIndexes{types.NewRewardIndex("bnb-a", sdk.ZeroDec())})
suite.Require().NotPanics(func() {
suite.keeper.SetUSDXMintingClaim(suite.ctx, c)
})
}
claims := types.USDXMintingClaims{}
suite.keeper.IterateUSDXMintingClaims(suite.ctx, func(c types.USDXMintingClaim) bool {
claims = append(claims, c)
return false
})
suite.Require().Equal(len(suite.addrs), len(claims))
claims = suite.keeper.GetAllUSDXMintingClaims(suite.ctx)
suite.Require().Equal(len(suite.addrs), len(claims))
}
func (suite *KeeperTestSuite) TestGetSetSwapRewardIndexes() {
testCases := []struct {
name string
poolName string
indexes types.RewardIndexes
panics bool
}{
{
name: "two factors can be written and read",
poolName: "btc/usdx",
indexes: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
},
{
name: "indexes with empty pool name can be written and read",
poolName: "",
indexes: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
},
{
// this test is to detect any changes in behavior, it would be nice if Set didn't panic
name: "setting empty indexes panics",
poolName: "btc/usdx",
indexes: types.RewardIndexes{},
panics: true,
},
{
// this test is to detect any changes in behavior, it would be nice if Set didn't panic
name: "setting nil indexes panics",
poolName: "btc/usdx",
indexes: nil,
panics: true,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupApp()
_, found := suite.keeper.GetSwapRewardIndexes(suite.ctx, tc.poolName)
suite.False(found)
setFunc := func() { suite.keeper.SetSwapRewardIndexes(suite.ctx, tc.poolName, tc.indexes) }
if tc.panics {
suite.Panics(setFunc)
return
} else {
suite.NotPanics(setFunc)
}
storedIndexes, found := suite.keeper.GetSwapRewardIndexes(suite.ctx, tc.poolName)
suite.True(found)
suite.Equal(tc.indexes, storedIndexes)
})
}
}
func (suite *KeeperTestSuite) TestGetSetSwapRewardAccrualTimes() {
testCases := []struct {
name string
poolName string
accrualTime time.Time
panics bool
}{
{
name: "normal time can be written and read",
poolName: "btc/usdx",
accrualTime: time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "zero time can be written and read",
poolName: "btc/usdx",
accrualTime: time.Time{},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupApp()
_, found := suite.keeper.GetSwapRewardAccrualTime(suite.ctx, tc.poolName)
suite.False(found)
setFunc := func() { suite.keeper.SetSwapRewardAccrualTime(suite.ctx, tc.poolName, tc.accrualTime) }
if tc.panics {
suite.Panics(setFunc)
return
} else {
suite.NotPanics(setFunc)
}
storedTime, found := suite.keeper.GetSwapRewardAccrualTime(suite.ctx, tc.poolName)
suite.True(found)
suite.Equal(tc.accrualTime, storedTime)
})
}
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}