0g-chain/x/incentive/keeper/keeper_test.go
Draco 614d4e40fe
Update cosmos-sdk to v0.47.7 (#1811)
* Update cometbft, cosmos, ethermint, and ibc-go

* Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft

* Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db

* Replace gogo/protobuf with cosmos/gogoproto & simapp replacement

* Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims

* Remove no longer used simulations

* Replace ibchost with ibcexported
See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants

* Add new consensus params keeper

* Add consensus keeper to blockers

* Fix keeper and module issues in app.go

* Add IsSendEnabledCoins and update SetParams interface changes

* Fix protobuf build for cosmos 47 (#1800)

* fix cp errors by using -f; fix lint by only linting our proto dir;
and use proofs.proto directly from ics23 for ibc-go v7

* run proto-all; commit updated third party deps and swagger changes

* regenerate proto files

* use correct gocosmos build plugin for buf

* re-gen all protobuf files to update paths for new gocosmos plugin

* update protoc and buf to latest versions

* fix staking keeper issues in app.go

* update tally handler for gov changes

* chain id fix and flag fixes

* update deps for cometbft 47.7 upgrade

* remove all module legacy queriers

* update stakingKeeper to pointer

* Replace ModuleCdc from govv1beta1 to govcodec

* remove simulations

* abci.LastCommitInfo → abci.CommitInfo

* Remove unused code in keys.go

* simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi

* Fix chain id issues in tests

* Fix remaining unit test issues

* Update changelog for upgrade

* Fix e2e tests using updated kvtool

* Update protonet to v47 compatible genesis

* Bump cometbft-db to v0.9.1-kava.1

* Update kvtool

* Remove extra changelog

* Fix merged rocksdb issues

* go mod cleanup

* Bump cometbft-db to v9 and go to 1.21

* Bump rocksdb version to v8.10.0

* Update kvtool to latest version

* Update gin to v1.9.0

* Use ibctm.ModuleName in app_test

* Fallback to genesis chain id instead of client toml

* Remove all simulations

* Fix cdp migrations issue with v47

* Update dependencies to correct tags

---------

Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 17:54:10 -05:00

630 lines
16 KiB
Go

package keeper_test
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
tmprototypes "github.com/cometbft/cometbft/proto/tendermint/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, tmprototypes.Header{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) TestGetSetDeleteSwapClaims() {
suite.SetupApp()
c := types.NewSwapClaim(suite.addrs[0], arbitraryCoins(), nonEmptyMultiRewardIndexes)
_, found := suite.keeper.GetSwapClaim(suite.ctx, suite.addrs[0])
suite.Require().False(found)
suite.Require().NotPanics(func() {
suite.keeper.SetSwapClaim(suite.ctx, c)
})
testC, found := suite.keeper.GetSwapClaim(suite.ctx, suite.addrs[0])
suite.Require().True(found)
suite.Require().Equal(c, testC)
suite.Require().NotPanics(func() {
suite.keeper.DeleteSwapClaim(suite.ctx, suite.addrs[0])
})
_, found = suite.keeper.GetSwapClaim(suite.ctx, suite.addrs[0])
suite.Require().False(found)
}
func (suite *KeeperTestSuite) TestIterateSwapClaims() {
suite.SetupApp()
claims := types.SwapClaims{
types.NewSwapClaim(suite.addrs[0], arbitraryCoins(), nonEmptyMultiRewardIndexes),
types.NewSwapClaim(suite.addrs[1], nil, nil), // different claim to the first
}
for _, claim := range claims {
suite.keeper.SetSwapClaim(suite.ctx, claim)
}
var actualClaims types.SwapClaims
suite.keeper.IterateSwapClaims(suite.ctx, func(c types.SwapClaim) bool {
actualClaims = append(actualClaims, c)
return false
})
suite.Require().Equal(claims, actualClaims)
}
func (suite *KeeperTestSuite) TestGetSetSwapRewardIndexes() {
testCases := []struct {
name string
poolName string
indexes types.RewardIndexes
wantIndex 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"),
},
},
wantIndex: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
},
{
name: "indexes with empty pool name panics",
poolName: "",
indexes: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
panics: true,
},
{
// this test is to detect any changes in behavior
name: "setting empty indexes does not panic",
poolName: "btc/usdx",
// Marshalling empty slice results in [] bytes, unmarshalling the []
// empty bytes results in a nil slice instead of an empty slice
indexes: types.RewardIndexes{},
wantIndex: nil,
panics: false,
},
{
// this test is to detect any changes in behavior
name: "setting nil indexes does not panic",
poolName: "btc/usdx",
indexes: nil,
wantIndex: nil,
panics: false,
},
}
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.wantIndex, storedIndexes)
})
}
}
func (suite *KeeperTestSuite) TestIterateSwapRewardIndexes() {
suite.SetupApp()
multiIndexes := types.MultiRewardIndexes{
{
CollateralType: "bnb/usdx",
RewardIndexes: types.RewardIndexes{
{
CollateralType: "swap",
RewardFactor: d("0.0000002"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
},
{
CollateralType: "btcb/usdx",
RewardIndexes: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
},
},
}
for _, mi := range multiIndexes {
suite.keeper.SetSwapRewardIndexes(suite.ctx, mi.CollateralType, mi.RewardIndexes)
}
var actualMultiIndexes types.MultiRewardIndexes
suite.keeper.IterateSwapRewardIndexes(suite.ctx, func(poolID string, i types.RewardIndexes) bool {
actualMultiIndexes = actualMultiIndexes.With(poolID, i)
return false
})
suite.Require().Equal(multiIndexes, actualMultiIndexes)
}
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 (suite *KeeperTestSuite) TestGetSetDeleteEarnClaims() {
suite.SetupApp()
c := types.NewEarnClaim(suite.addrs[0], arbitraryCoins(), nonEmptyMultiRewardIndexes)
_, found := suite.keeper.GetEarnClaim(suite.ctx, suite.addrs[0])
suite.Require().False(found)
suite.Require().NotPanics(func() {
suite.keeper.SetEarnClaim(suite.ctx, c)
})
testC, found := suite.keeper.GetEarnClaim(suite.ctx, suite.addrs[0])
suite.Require().True(found)
suite.Require().Equal(c, testC)
suite.Require().NotPanics(func() {
suite.keeper.DeleteEarnClaim(suite.ctx, suite.addrs[0])
})
_, found = suite.keeper.GetEarnClaim(suite.ctx, suite.addrs[0])
suite.Require().False(found)
}
func (suite *KeeperTestSuite) TestIterateEarnClaims() {
suite.SetupApp()
claims := types.EarnClaims{
types.NewEarnClaim(suite.addrs[0], arbitraryCoins(), nonEmptyMultiRewardIndexes),
types.NewEarnClaim(suite.addrs[1], nil, nil), // different claim to the first
}
for _, claim := range claims {
suite.keeper.SetEarnClaim(suite.ctx, claim)
}
var actualClaims types.EarnClaims
suite.keeper.IterateEarnClaims(suite.ctx, func(c types.EarnClaim) bool {
actualClaims = append(actualClaims, c)
return false
})
suite.Require().Equal(claims, actualClaims)
}
func (suite *KeeperTestSuite) TestGetSetEarnRewardIndexes() {
testCases := []struct {
name string
vaultDenom string
indexes types.RewardIndexes
wantIndex types.RewardIndexes
panics bool
}{
{
name: "two factors can be written and read",
vaultDenom: "usdx",
indexes: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
wantIndex: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
},
{
name: "indexes with empty vault name panics",
vaultDenom: "",
indexes: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
panics: true,
},
{
// this test is to detect any changes in behavior
name: "setting empty indexes does not panic",
vaultDenom: "usdx",
// Marshalling empty slice results in [] bytes, unmarshalling the []
// empty bytes results in a nil slice instead of an empty slice
indexes: types.RewardIndexes{},
wantIndex: nil,
panics: false,
},
{
// this test is to detect any changes in behavior
name: "setting nil indexes does not panic",
vaultDenom: "usdx",
indexes: nil,
wantIndex: nil,
panics: false,
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupApp()
_, found := suite.keeper.GetEarnRewardIndexes(suite.ctx, tc.vaultDenom)
suite.False(found)
setFunc := func() { suite.keeper.SetEarnRewardIndexes(suite.ctx, tc.vaultDenom, tc.indexes) }
if tc.panics {
suite.Panics(setFunc)
return
} else {
suite.NotPanics(setFunc)
}
storedIndexes, found := suite.keeper.GetEarnRewardIndexes(suite.ctx, tc.vaultDenom)
suite.True(found)
suite.Equal(tc.wantIndex, storedIndexes)
})
}
}
func (suite *KeeperTestSuite) TestIterateEarnRewardIndexes() {
suite.SetupApp()
multiIndexes := types.MultiRewardIndexes{
{
CollateralType: "ukava",
RewardIndexes: types.RewardIndexes{
{
CollateralType: "earn",
RewardFactor: d("0.0000002"),
},
{
CollateralType: "ukava",
RewardFactor: d("0.04"),
},
},
},
{
CollateralType: "usdx",
RewardIndexes: types.RewardIndexes{
{
CollateralType: "hard",
RewardFactor: d("0.02"),
},
},
},
}
for _, mi := range multiIndexes {
suite.keeper.SetEarnRewardIndexes(suite.ctx, mi.CollateralType, mi.RewardIndexes)
}
var actualMultiIndexes types.MultiRewardIndexes
suite.keeper.IterateEarnRewardIndexes(suite.ctx, func(vaultDenom string, i types.RewardIndexes) bool {
actualMultiIndexes = actualMultiIndexes.With(vaultDenom, i)
return false
})
suite.Require().Equal(multiIndexes, actualMultiIndexes)
}
func (suite *KeeperTestSuite) TestGetSetEarnRewardAccrualTimes() {
testCases := []struct {
name string
vaultDenom string
accrualTime time.Time
panics bool
}{
{
name: "normal time can be written and read",
vaultDenom: "usdx",
accrualTime: time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "zero time can be written and read",
vaultDenom: "usdx",
accrualTime: time.Time{},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupApp()
_, found := suite.keeper.GetEarnRewardAccrualTime(suite.ctx, tc.vaultDenom)
suite.False(found)
setFunc := func() { suite.keeper.SetEarnRewardAccrualTime(suite.ctx, tc.vaultDenom, tc.accrualTime) }
if tc.panics {
suite.Panics(setFunc)
return
} else {
suite.NotPanics(setFunc)
}
storedTime, found := suite.keeper.GetEarnRewardAccrualTime(suite.ctx, tc.vaultDenom)
suite.True(found)
suite.Equal(tc.accrualTime, storedTime)
})
}
}
type accrualtime struct {
denom string
time time.Time
}
var nonEmptyAccrualTimes = []accrualtime{
{
denom: "btcb",
time: time.Date(1998, 1, 1, 0, 0, 0, 1, time.UTC),
},
{
denom: "ukava",
time: time.Time{},
},
}
func (suite *KeeperTestSuite) TestIterateUSDXMintingAccrualTimes() {
suite.SetupApp()
expectedAccrualTimes := nonEmptyAccrualTimes
for _, at := range expectedAccrualTimes {
suite.keeper.SetPreviousUSDXMintingAccrualTime(suite.ctx, at.denom, at.time)
}
var actualAccrualTimes []accrualtime
suite.keeper.IterateUSDXMintingAccrualTimes(suite.ctx, func(denom string, accrualTime time.Time) bool {
actualAccrualTimes = append(actualAccrualTimes, accrualtime{denom: denom, time: accrualTime})
return false
})
suite.Equal(expectedAccrualTimes, actualAccrualTimes)
}
func (suite *KeeperTestSuite) TestIterateHardSupplyRewardAccrualTimes() {
suite.SetupApp()
expectedAccrualTimes := nonEmptyAccrualTimes
for _, at := range expectedAccrualTimes {
suite.keeper.SetPreviousHardSupplyRewardAccrualTime(suite.ctx, at.denom, at.time)
}
var actualAccrualTimes []accrualtime
suite.keeper.IterateHardSupplyRewardAccrualTimes(suite.ctx, func(denom string, accrualTime time.Time) bool {
actualAccrualTimes = append(actualAccrualTimes, accrualtime{denom: denom, time: accrualTime})
return false
})
suite.Equal(expectedAccrualTimes, actualAccrualTimes)
}
func (suite *KeeperTestSuite) TestIterateHardBorrowrRewardAccrualTimes() {
suite.SetupApp()
expectedAccrualTimes := nonEmptyAccrualTimes
for _, at := range expectedAccrualTimes {
suite.keeper.SetPreviousHardBorrowRewardAccrualTime(suite.ctx, at.denom, at.time)
}
var actualAccrualTimes []accrualtime
suite.keeper.IterateHardBorrowRewardAccrualTimes(suite.ctx, func(denom string, accrualTime time.Time) bool {
actualAccrualTimes = append(actualAccrualTimes, accrualtime{denom: denom, time: accrualTime})
return false
})
suite.Equal(expectedAccrualTimes, actualAccrualTimes)
}
func (suite *KeeperTestSuite) TestIterateDelegatorRewardAccrualTimes() {
suite.SetupApp()
expectedAccrualTimes := nonEmptyAccrualTimes
for _, at := range expectedAccrualTimes {
suite.keeper.SetPreviousDelegatorRewardAccrualTime(suite.ctx, at.denom, at.time)
}
var actualAccrualTimes []accrualtime
suite.keeper.IterateDelegatorRewardAccrualTimes(suite.ctx, func(denom string, accrualTime time.Time) bool {
actualAccrualTimes = append(actualAccrualTimes, accrualtime{denom: denom, time: accrualTime})
return false
})
suite.Equal(expectedAccrualTimes, actualAccrualTimes)
}
func (suite *KeeperTestSuite) TestIterateSwapRewardAccrualTimes() {
suite.SetupApp()
expectedAccrualTimes := nonEmptyAccrualTimes
for _, at := range expectedAccrualTimes {
suite.keeper.SetSwapRewardAccrualTime(suite.ctx, at.denom, at.time)
}
var actualAccrualTimes []accrualtime
suite.keeper.IterateSwapRewardAccrualTimes(suite.ctx, func(denom string, accrualTime time.Time) bool {
actualAccrualTimes = append(actualAccrualTimes, accrualtime{denom: denom, time: accrualTime})
return false
})
suite.Equal(expectedAccrualTimes, actualAccrualTimes)
}
func (suite *KeeperTestSuite) TestIterateEarnRewardAccrualTimes() {
suite.SetupApp()
expectedAccrualTimes := nonEmptyAccrualTimes
for _, at := range expectedAccrualTimes {
suite.keeper.SetEarnRewardAccrualTime(suite.ctx, at.denom, at.time)
}
var actualAccrualTimes []accrualtime
suite.keeper.IterateEarnRewardAccrualTimes(suite.ctx, func(denom string, accrualTime time.Time) bool {
actualAccrualTimes = append(actualAccrualTimes, accrualtime{denom: denom, time: accrualTime})
return false
})
suite.Equal(expectedAccrualTimes, actualAccrualTimes)
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}