0g-chain/x/incentive/genesis_test.go
Ruaridh cf16029e77
Incentive refactor: hard rewards (#929)
* organise testing committee gen state

* remove repeated test app initialization

* minor fixes from linter in tests

* move more setup to SetupApp

* split up KeeperTestSuite for each reward type

* simplify KeeperTestSuite

* simplify PayoutKeeperSuite

* simplify DelegatorRewardSuite

* simplify SupplyRewardsSuite

* simplify BorrowRewardsSuite

* simplify USDXRewardsSuite

* add auth genesis builder for easier test setup

* migrate all incentive tests to auth builder

* add incentive genesis builder for easier setup
migrate hard incentive tests

* migrate all tests to incentive builder

* add hard genesis builder

* small tidy ups

* deduplicate initialTime from borrow tests

* deduplicate initialtTime from supply tests

* deduplicate initialTime from usdx and keeper tests

* deduplicate initialTime in delgator tests

* deduplicate genesis time in payout test

* deduplicate test app initialization

* make authGenesisBuilder available for all modules

* remove unused pricefeed setup

* export incentive genesis builder

* remove commented out test cases

* migrate cdp test to new test state builders

* migrate vv payout tests to use new builders

* add SynchronizeHardBorrowReward unit test

* extract calculatReward method

* tidy up unit test for borrow rewards

* add helper method to RewardIndexes

* user helper to extract logic from SyncBorrowReward

* add Get methods to (Multi)RewardIndexes

* replace params.Subspace in keeper to test easier

* add unit tests for usdx minting

* refactor InitializeUSDXMintingClaim

* add unit tests for InitializeHardBorrowRewards

* refactor SynchronizeUSDXMintingReward

* add unit tests for UpdateHardBorrowIndexDenoms

* change rewardSource type to Dec
needed by delegation rewards

* fix typo in test names

* refactor UpdateHardBorrowIndexDenoms

* update genesis test TODO to use auth builder

* add skipped test for bug in usdx sync

* extract common method for calculating rewards

* doc comment tidy

* add unit tests for delegator rewards

* tidy up test files

* remove old TODOs

* reaarrange InitializeHardDelegatorReward
to fit with other init reward functions

* duplicate borrow unit tests to create supply tests

* add tests for syncing with zero rewards per second

* refactor SynchronizeHardDelegatorRewards

* refactor supply rewards in same way as borrow

* fix total delegation calculation bug

* fix new usdx reward bug

* fix new supply/borrow reward bug

* remove working comment

* standardize behaviour when global factors missing

* improve documentation for CalculateRewards

* standardize variable names

* remove panic from calculateSingleReward

* wip

* Tidy up comments

* remove wip comment
2021-06-21 22:05:17 +01:00

126 lines
4.8 KiB
Go

package incentive_test
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/hard"
"github.com/kava-labs/kava/x/incentive"
"github.com/kava-labs/kava/x/kavadist"
)
const (
oneYear time.Duration = 365 * 24 * time.Hour
)
type GenesisTestSuite struct {
suite.Suite
ctx sdk.Context
app app.TestApp
keeper incentive.Keeper
addrs []sdk.AccAddress
genesisTime time.Time
}
func (suite *GenesisTestSuite) SetupTest() {
tApp := app.NewTestApp()
keeper := tApp.GetIncentiveKeeper()
suite.genesisTime = time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)
_, addrs := app.GeneratePrivKeyAddressPairs(3)
authBuilder := app.NewAuthGenesisBuilder().
WithSimpleAccount(addrs[0], cs(c("bnb", 1e10), c("ukava", 1e10))).
WithSimpleModuleAccount(kavadist.KavaDistMacc, cs(c("hard", 1e15), c("ukava", 1e15)))
loanToValue, _ := sdk.NewDecFromStr("0.6")
borrowLimit := sdk.NewDec(1000000000000000)
hardGS := hard.NewGenesisState(
hard.NewParams(
hard.MoneyMarkets{
hard.NewMoneyMarket("ukava", hard.NewBorrowLimit(false, borrowLimit, loanToValue), "kava:usd", sdk.NewInt(1000000), hard.NewInterestRateModel(sdk.MustNewDecFromStr("0.05"), sdk.MustNewDecFromStr("2"), sdk.MustNewDecFromStr("0.8"), sdk.MustNewDecFromStr("10")), sdk.MustNewDecFromStr("0.05"), sdk.ZeroDec()),
hard.NewMoneyMarket("bnb", hard.NewBorrowLimit(false, borrowLimit, loanToValue), "bnb:usd", sdk.NewInt(1000000), hard.NewInterestRateModel(sdk.MustNewDecFromStr("0.05"), sdk.MustNewDecFromStr("2"), sdk.MustNewDecFromStr("0.8"), sdk.MustNewDecFromStr("10")), sdk.MustNewDecFromStr("0.05"), sdk.ZeroDec()),
},
sdk.NewDec(10),
),
hard.DefaultAccumulationTimes,
hard.DefaultDeposits,
hard.DefaultBorrows,
hard.DefaultTotalSupplied,
hard.DefaultTotalBorrowed,
hard.DefaultTotalReserves,
)
incentiveGS := incentive.NewGenesisState(
incentive.NewParams(
incentive.RewardPeriods{incentive.NewRewardPeriod(true, "bnb-a", suite.genesisTime.Add(-1*oneYear), suite.genesisTime.Add(oneYear), c("ukava", 122354))},
incentive.MultiRewardPeriods{incentive.NewMultiRewardPeriod(true, "bnb", suite.genesisTime.Add(-1*oneYear), suite.genesisTime.Add(oneYear), cs(c("hard", 122354)))},
incentive.MultiRewardPeriods{incentive.NewMultiRewardPeriod(true, "bnb", suite.genesisTime.Add(-1*oneYear), suite.genesisTime.Add(oneYear), cs(c("hard", 122354)))},
incentive.RewardPeriods{incentive.NewRewardPeriod(true, "ukava", suite.genesisTime.Add(-1*oneYear), suite.genesisTime.Add(oneYear), c("hard", 122354))},
incentive.Multipliers{incentive.NewMultiplier(incentive.Small, 1, d("0.25")), incentive.NewMultiplier(incentive.Large, 12, d("1.0"))},
suite.genesisTime.Add(5*oneYear),
),
incentive.DefaultGenesisAccumulationTimes,
incentive.DefaultGenesisAccumulationTimes,
incentive.DefaultGenesisAccumulationTimes,
incentive.DefaultGenesisAccumulationTimes,
incentive.DefaultUSDXClaims,
incentive.DefaultHardClaims,
)
tApp.InitializeFromGenesisStatesWithTime(
suite.genesisTime,
authBuilder.BuildMarshalled(),
app.GenesisState{incentive.ModuleName: incentive.ModuleCdc.MustMarshalJSON(incentiveGS)},
app.GenesisState{hard.ModuleName: hard.ModuleCdc.MustMarshalJSON(hardGS)},
NewCDPGenStateMulti(),
NewPricefeedGenStateMulti(),
)
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: suite.genesisTime})
suite.addrs = addrs
suite.keeper = keeper
suite.app = tApp
suite.ctx = ctx
}
// Test to cover an bug where paid out claims would zero out rewards incorrectly, creating an invalid coins object.
// The invalid reward coins would fail the genesis state validation
func (suite *GenesisTestSuite) TestPaidOutClaimsPassValidateGenesis() {
hardHandler := hard.NewHandler(suite.app.GetHardKeeper())
_, err := hardHandler(suite.ctx, hard.NewMsgDeposit(suite.addrs[0], cs(c("bnb", 100_000_000))))
suite.Require().NoError(err)
suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(1 * 10 * time.Second))
suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{})
suite.app.EndBlocker(suite.ctx, abci.RequestEndBlock{})
suite.ctx = suite.ctx.WithBlockTime(suite.genesisTime.Add(2 * 10 * time.Second))
suite.app.BeginBlocker(suite.ctx, abci.RequestBeginBlock{})
_, err = hardHandler(suite.ctx, hard.NewMsgWithdraw(suite.addrs[0], cs(c("bnb", 100_000_000))))
suite.Require().NoError(err)
incentiveHandler := incentive.NewHandler(suite.keeper)
_, err = incentiveHandler(suite.ctx, incentive.NewMsgClaimHardReward(suite.addrs[0], string(incentive.Large)))
suite.Require().NoError(err)
genState := incentive.ExportGenesis(suite.ctx, suite.keeper)
suite.Require().NoError(
genState.Validate(),
)
}
func TestGenesisTestSuite(t *testing.T) {
suite.Run(t, new(GenesisTestSuite))
}