0g-chain/x/incentive/keeper/rewards_usdx_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

299 lines
10 KiB
Go

package keeper_test
import (
"testing"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app"
cdpkeeper "github.com/kava-labs/kava/x/cdp/keeper"
cdptypes "github.com/kava-labs/kava/x/cdp/types"
"github.com/kava-labs/kava/x/incentive/keeper"
"github.com/kava-labs/kava/x/incentive/testutil"
)
// Test suite used for all keeper tests
type USDXRewardsTestSuite struct {
suite.Suite
keeper keeper.Keeper
cdpKeeper cdpkeeper.Keeper
app app.TestApp
ctx sdk.Context
genesisTime time.Time
addrs []sdk.AccAddress
}
// SetupTest is run automatically before each suite test
func (suite *USDXRewardsTestSuite) 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 *USDXRewardsTestSuite) SetupApp() {
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetIncentiveKeeper()
suite.cdpKeeper = suite.app.GetCDPKeeper()
suite.ctx = suite.app.NewContext(true, abci.Header{Height: 1, Time: suite.genesisTime})
}
func (suite *USDXRewardsTestSuite) SetupWithGenState(authBuilder app.AuthGenesisBuilder, incentBuilder testutil.IncentiveGenesisBuilder) {
suite.SetupApp()
suite.app.InitializeFromGenesisStatesWithTime(
suite.genesisTime,
authBuilder.BuildMarshalled(),
NewPricefeedGenStateMultiFromTime(suite.genesisTime),
NewCDPGenStateMulti(),
incentBuilder.BuildMarshalled(),
)
}
func (suite *USDXRewardsTestSuite) TestAccumulateUSDXMintingRewards() {
type args struct {
ctype string
rewardsPerSecond sdk.Coin
initialTotalPrincipal sdk.Coin
timeElapsed int
expectedRewardFactor sdk.Dec
}
type test struct {
name string
args args
}
testCases := []test{
{
"7 seconds",
args{
ctype: "bnb-a",
rewardsPerSecond: c("ukava", 122354),
initialTotalPrincipal: c("usdx", 1000000000000),
timeElapsed: 7,
expectedRewardFactor: d("0.000000856478000000"),
},
},
{
"1 day",
args{
ctype: "bnb-a",
rewardsPerSecond: c("ukava", 122354),
initialTotalPrincipal: c("usdx", 1000000000000),
timeElapsed: 86400,
expectedRewardFactor: d("0.0105713856"),
},
},
{
"0 seconds",
args{
ctype: "bnb-a",
rewardsPerSecond: c("ukava", 122354),
initialTotalPrincipal: c("usdx", 1000000000000),
timeElapsed: 0,
expectedRewardFactor: d("0.0"),
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
incentBuilder := testutil.NewIncentiveGenesisBuilder().WithGenesisTime(suite.genesisTime).WithSimpleUSDXRewardPeriod(tc.args.ctype, tc.args.rewardsPerSecond)
suite.SetupWithGenState(app.NewAuthGenesisBuilder(), incentBuilder)
// setup cdp state
suite.cdpKeeper.SetTotalPrincipal(suite.ctx, tc.args.ctype, cdptypes.DefaultStableDenom, tc.args.initialTotalPrincipal.Amount)
updatedBlockTime := suite.ctx.BlockTime().Add(time.Duration(int(time.Second) * tc.args.timeElapsed))
suite.ctx = suite.ctx.WithBlockTime(updatedBlockTime)
rewardPeriod, found := suite.keeper.GetUSDXMintingRewardPeriod(suite.ctx, tc.args.ctype)
suite.Require().True(found)
suite.keeper.AccumulateUSDXMintingRewards(suite.ctx, rewardPeriod)
rewardFactor, _ := suite.keeper.GetUSDXMintingRewardFactor(suite.ctx, tc.args.ctype)
suite.Require().Equal(tc.args.expectedRewardFactor, rewardFactor)
})
}
}
func (suite *USDXRewardsTestSuite) TestSynchronizeUSDXMintingReward() {
type args struct {
ctype string
rewardsPerSecond sdk.Coin
initialCollateral sdk.Coin
initialPrincipal sdk.Coin
blockTimes []int
expectedRewardFactor sdk.Dec
expectedRewards sdk.Coin
}
type test struct {
name string
args args
}
testCases := []test{
{
"10 blocks",
args{
ctype: "bnb-a",
rewardsPerSecond: c("ukava", 122354),
initialCollateral: c("bnb", 1000000000000),
initialPrincipal: c("usdx", 10000000000),
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
expectedRewardFactor: d("0.001223540000000000"),
expectedRewards: c("ukava", 12235400),
},
},
{
"10 blocks - long block time",
args{
ctype: "bnb-a",
rewardsPerSecond: c("ukava", 122354),
initialCollateral: c("bnb", 1000000000000),
initialPrincipal: c("usdx", 10000000000),
blockTimes: []int{86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400},
expectedRewardFactor: d("10.57138560000000000"),
expectedRewards: c("ukava", 105713856000),
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
authBuilder := app.NewAuthGenesisBuilder().WithSimpleAccount(suite.addrs[0], cs(tc.args.initialCollateral))
incentBuilder := testutil.NewIncentiveGenesisBuilder().WithGenesisTime(suite.genesisTime).WithSimpleUSDXRewardPeriod(tc.args.ctype, tc.args.rewardsPerSecond)
suite.SetupWithGenState(authBuilder, incentBuilder)
// setup cdp state
err := suite.cdpKeeper.AddCdp(suite.ctx, suite.addrs[0], tc.args.initialCollateral, tc.args.initialPrincipal, tc.args.ctype)
suite.Require().NoError(err)
claim, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
suite.Require().True(found)
suite.Require().Equal(sdk.ZeroDec(), claim.RewardIndexes[0].RewardFactor)
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)
rewardPeriod, found := suite.keeper.GetUSDXMintingRewardPeriod(blockCtx, tc.args.ctype)
suite.Require().True(found)
suite.keeper.AccumulateUSDXMintingRewards(blockCtx, rewardPeriod)
}
updatedBlockTime := suite.ctx.BlockTime().Add(time.Duration(int(time.Second) * timeElapsed))
suite.ctx = suite.ctx.WithBlockTime(updatedBlockTime)
cdp, found := suite.cdpKeeper.GetCdpByOwnerAndCollateralType(suite.ctx, suite.addrs[0], tc.args.ctype)
suite.Require().True(found)
suite.Require().NotPanics(func() {
suite.keeper.SynchronizeUSDXMintingReward(suite.ctx, cdp)
})
rewardFactor, _ := suite.keeper.GetUSDXMintingRewardFactor(suite.ctx, tc.args.ctype)
suite.Require().Equal(tc.args.expectedRewardFactor, rewardFactor)
claim, found = suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
suite.Require().True(found)
suite.Require().Equal(tc.args.expectedRewardFactor, claim.RewardIndexes[0].RewardFactor)
suite.Require().Equal(tc.args.expectedRewards, claim.Reward)
})
}
}
func (suite *USDXRewardsTestSuite) TestSimulateUSDXMintingRewardSynchronization() {
type args struct {
ctype string
rewardsPerSecond sdk.Coin
initialCollateral sdk.Coin
initialPrincipal sdk.Coin
blockTimes []int
expectedRewardFactor sdk.Dec
expectedRewards sdk.Coin
}
type test struct {
name string
args args
}
testCases := []test{
{
"10 blocks",
args{
ctype: "bnb-a",
rewardsPerSecond: c("ukava", 122354),
initialCollateral: c("bnb", 1000000000000),
initialPrincipal: c("usdx", 10000000000),
blockTimes: []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
expectedRewardFactor: d("0.001223540000000000"),
expectedRewards: c("ukava", 12235400),
},
},
{
"10 blocks - long block time",
args{
ctype: "bnb-a",
rewardsPerSecond: c("ukava", 122354),
initialCollateral: c("bnb", 1000000000000),
initialPrincipal: c("usdx", 10000000000),
blockTimes: []int{86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400, 86400},
expectedRewardFactor: d("10.57138560000000000"),
expectedRewards: c("ukava", 105713856000),
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
authBuilder := app.NewAuthGenesisBuilder().WithSimpleAccount(suite.addrs[0], cs(tc.args.initialCollateral))
incentBuilder := testutil.NewIncentiveGenesisBuilder().WithGenesisTime(suite.genesisTime).WithSimpleUSDXRewardPeriod(tc.args.ctype, tc.args.rewardsPerSecond)
suite.SetupWithGenState(authBuilder, incentBuilder)
// setup cdp state
err := suite.cdpKeeper.AddCdp(suite.ctx, suite.addrs[0], tc.args.initialCollateral, tc.args.initialPrincipal, tc.args.ctype)
suite.Require().NoError(err)
claim, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
suite.Require().True(found)
suite.Require().Equal(sdk.ZeroDec(), claim.RewardIndexes[0].RewardFactor)
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)
rewardPeriod, found := suite.keeper.GetUSDXMintingRewardPeriod(blockCtx, tc.args.ctype)
suite.Require().True(found)
suite.keeper.AccumulateUSDXMintingRewards(blockCtx, rewardPeriod)
}
updatedBlockTime := suite.ctx.BlockTime().Add(time.Duration(int(time.Second) * timeElapsed))
suite.ctx = suite.ctx.WithBlockTime(updatedBlockTime)
claim, found = suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
suite.Require().True(found)
suite.Require().Equal(claim.RewardIndexes[0].RewardFactor, sdk.ZeroDec())
suite.Require().Equal(claim.Reward, sdk.NewCoin("ukava", sdk.ZeroInt()))
updatedClaim := suite.keeper.SimulateUSDXMintingSynchronization(suite.ctx, claim)
suite.Require().Equal(tc.args.expectedRewardFactor, updatedClaim.RewardIndexes[0].RewardFactor)
suite.Require().Equal(tc.args.expectedRewards, updatedClaim.Reward)
})
}
}
func TestUSDXRewardsTestSuite(t *testing.T) {
suite.Run(t, new(USDXRewardsTestSuite))
}