2020-04-24 15:44:44 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2020-04-24 15:44:44 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-01-18 19:12:37 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2020-04-24 15:44:44 +00:00
|
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
2021-01-18 19:12:37 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
|
2021-01-25 12:58:12 +00:00
|
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
2020-04-24 15:44:44 +00:00
|
|
|
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-04-24 15:44:44 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
2021-02-19 20:02:51 +00:00
|
|
|
committeekeeper "github.com/kava-labs/kava/x/committee/keeper"
|
2021-01-21 13:52:09 +00:00
|
|
|
hardkeeper "github.com/kava-labs/kava/x/hard/keeper"
|
2020-04-30 14:13:31 +00:00
|
|
|
"github.com/kava-labs/kava/x/incentive/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
2020-04-24 15:44:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test suite used for all keeper tests
|
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2021-02-19 20:02:51 +00:00
|
|
|
keeper keeper.Keeper
|
|
|
|
hardKeeper hardkeeper.Keeper
|
|
|
|
stakingKeeper stakingkeeper.Keeper
|
|
|
|
committeeKeeper committeekeeper.Keeper
|
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
validatorAddrs []sdk.ValAddress
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The default state used by each test
|
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
2021-03-25 06:10:13 +00:00
|
|
|
config := sdk.GetConfig()
|
|
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
|
|
|
|
_, allAddrs := app.GeneratePrivKeyAddressPairs(10)
|
|
|
|
suite.addrs = allAddrs[:5]
|
|
|
|
for _, a := range allAddrs[5:] {
|
|
|
|
suite.validatorAddrs = append(suite.validatorAddrs, sdk.ValAddress(a))
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:44:44 +00:00
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
2021-01-25 12:58:12 +00:00
|
|
|
|
2020-04-24 15:44:44 +00:00
|
|
|
tApp.InitializeFromGenesisStates()
|
2021-01-25 12:58:12 +00:00
|
|
|
|
2021-03-25 06:10:13 +00:00
|
|
|
suite.keeper = tApp.GetIncentiveKeeper()
|
2020-04-24 15:44:44 +00:00
|
|
|
suite.app = tApp
|
|
|
|
suite.ctx = ctx
|
2021-03-25 06:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getAllAddrs returns all user and validator addresses in the suite
|
|
|
|
func (suite *KeeperTestSuite) getAllAddrs() []sdk.AccAddress {
|
|
|
|
accAddrs := []sdk.AccAddress{} // initialize new slice to avoid accidental modifications to underlying
|
|
|
|
accAddrs = append(accAddrs, suite.addrs...)
|
|
|
|
for _, a := range suite.validatorAddrs {
|
|
|
|
accAddrs = append(accAddrs, sdk.AccAddress(a))
|
|
|
|
}
|
|
|
|
return accAddrs
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) getAccount(addr sdk.AccAddress) authexported.Account {
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
return ak.GetAccount(suite.ctx, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) getModuleAccount(name string) supplyexported.ModuleAccountI {
|
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
return sk.GetModuleAccount(suite.ctx, name)
|
|
|
|
}
|
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetDeleteUSDXMintingClaim() {
|
2021-01-18 19:12:37 +00:00
|
|
|
c := types.NewUSDXMintingClaim(suite.addrs[0], c("ukava", 1000000), types.RewardIndexes{types.NewRewardIndex("bnb-a", sdk.ZeroDec())})
|
2021-01-21 13:52:09 +00:00
|
|
|
_, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().False(found)
|
|
|
|
suite.Require().NotPanics(func() {
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.SetUSDXMintingClaim(suite.ctx, c)
|
2020-04-24 15:44:44 +00:00
|
|
|
})
|
2021-01-21 13:52:09 +00:00
|
|
|
testC, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(c, testC)
|
|
|
|
suite.Require().NotPanics(func() {
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.DeleteUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2020-04-24 15:44:44 +00:00
|
|
|
})
|
2021-01-21 13:52:09 +00:00
|
|
|
_, found = suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().False(found)
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
func (suite *KeeperTestSuite) TestIterateUSDXMintingClaims() {
|
2021-01-18 19:12:37 +00:00
|
|
|
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() {
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.SetUSDXMintingClaim(suite.ctx, c)
|
2021-01-18 19:12:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
claims := types.USDXMintingClaims{}
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.IterateUSDXMintingClaims(suite.ctx, func(c types.USDXMintingClaim) bool {
|
2020-04-24 15:44:44 +00:00
|
|
|
claims = append(claims, c)
|
|
|
|
return false
|
|
|
|
})
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().Equal(len(suite.addrs), len(claims))
|
2020-04-24 15:44:44 +00:00
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
claims = suite.keeper.GetAllUSDXMintingClaims(suite.ctx)
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().Equal(len(suite.addrs), len(claims))
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:12:37 +00:00
|
|
|
func createPeriodicVestingAccount(origVesting sdk.Coins, periods vesting.Periods, startTime, endTime int64) (*vesting.PeriodicVestingAccount, error) {
|
|
|
|
_, addr := app.GeneratePrivKeyAddressPairs(1)
|
|
|
|
bacc := auth.NewBaseAccountWithAddress(addr[0])
|
|
|
|
bacc.Coins = origVesting
|
|
|
|
bva, err := vesting.NewBaseVestingAccount(&bacc, origVesting, endTime)
|
|
|
|
if err != nil {
|
|
|
|
return &vesting.PeriodicVestingAccount{}, err
|
|
|
|
}
|
|
|
|
pva := vesting.NewPeriodicVestingAccountRaw(bva, startTime, periods)
|
|
|
|
err = pva.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return &vesting.PeriodicVestingAccount{}, err
|
|
|
|
}
|
|
|
|
return pva, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid cluttering test cases with long function names
|
|
|
|
func i(in int64) sdk.Int { return sdk.NewInt(in) }
|
|
|
|
func d(str string) sdk.Dec { return sdk.MustNewDecFromStr(str) }
|
|
|
|
func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) }
|
|
|
|
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }
|
2021-01-21 13:52:09 +00:00
|
|
|
|
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
|
|
}
|