2022-03-30 11:51:06 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2022-03-30 11:51:06 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/savings/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/savings/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type invariantTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
tApp app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
keeper keeper.Keeper
|
|
|
|
bankKeeper bankkeeper.Keeper
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
invariants map[string]map[string]sdk.Invariant
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *invariantTestSuite) SetupTest() {
|
|
|
|
config := sdk.GetConfig()
|
|
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(1)
|
|
|
|
suite.addrs = addrs
|
|
|
|
|
2022-12-09 22:31:31 +00:00
|
|
|
suite.tApp = tApp
|
2022-03-30 11:51:06 +00:00
|
|
|
suite.ctx = ctx
|
|
|
|
suite.keeper = tApp.GetSavingsKeeper()
|
|
|
|
suite.bankKeeper = tApp.GetBankKeeper()
|
|
|
|
|
|
|
|
suite.invariants = make(map[string]map[string]sdk.Invariant)
|
|
|
|
keeper.RegisterInvariants(suite, suite.keeper)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *invariantTestSuite) RegisterRoute(moduleName string, route string, invariant sdk.Invariant) {
|
|
|
|
_, exists := suite.invariants[moduleName]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
suite.invariants[moduleName] = make(map[string]sdk.Invariant)
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.invariants[moduleName][route] = invariant
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *invariantTestSuite) SetupValidState() {
|
2023-04-05 23:21:59 +00:00
|
|
|
depositAmt := sdk.NewCoins(sdk.NewCoin("ukava", sdkmath.NewInt(2e8)))
|
2022-03-30 11:51:06 +00:00
|
|
|
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, types.NewDeposit(
|
|
|
|
suite.addrs[0],
|
|
|
|
depositAmt,
|
|
|
|
))
|
|
|
|
|
2022-12-09 22:31:31 +00:00
|
|
|
err := suite.tApp.FundModuleAccount(suite.ctx, types.ModuleName, depositAmt)
|
2022-03-30 11:51:06 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *invariantTestSuite) runInvariant(route string, invariant func(k keeper.Keeper) sdk.Invariant) (string, bool) {
|
|
|
|
ctx := suite.ctx
|
|
|
|
registeredInvariant := suite.invariants[types.ModuleName][route]
|
|
|
|
suite.Require().NotNil(registeredInvariant)
|
|
|
|
|
|
|
|
// direct call
|
|
|
|
dMessage, dBroken := invariant(suite.keeper)(ctx)
|
|
|
|
// registered call
|
|
|
|
rMessage, rBroken := registeredInvariant(ctx)
|
|
|
|
// all call
|
|
|
|
aMessage, aBroken := keeper.AllInvariants(suite.keeper)(ctx)
|
|
|
|
|
|
|
|
// require matching values for direct call and registered call
|
|
|
|
suite.Require().Equal(dMessage, rMessage, "expected registered invariant message to match")
|
|
|
|
suite.Require().Equal(dBroken, rBroken, "expected registered invariant broken to match")
|
|
|
|
// require matching values for direct call and all invariants call if broken
|
|
|
|
suite.Require().Equal(dBroken, aBroken, "expected all invariant broken to match")
|
|
|
|
if dBroken {
|
|
|
|
suite.Require().Equal(dMessage, aMessage, "expected all invariant message to match")
|
|
|
|
}
|
|
|
|
|
|
|
|
// return message, broken
|
|
|
|
return dMessage, dBroken
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *invariantTestSuite) TestDepositsInvariant() {
|
|
|
|
message, broken := suite.runInvariant("deposits", keeper.DepositsInvariant)
|
|
|
|
suite.Equal("savings: validate deposits broken invariant\ndeposit invalid\n", message)
|
|
|
|
suite.Equal(false, broken)
|
|
|
|
|
|
|
|
suite.SetupValidState()
|
|
|
|
message, broken = suite.runInvariant("deposits", keeper.DepositsInvariant)
|
|
|
|
suite.Equal("savings: validate deposits broken invariant\ndeposit invalid\n", message)
|
|
|
|
suite.Equal(false, broken)
|
|
|
|
|
|
|
|
// broken with invalid deposit
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, types.NewDeposit(
|
|
|
|
suite.addrs[0],
|
|
|
|
sdk.Coins{},
|
|
|
|
))
|
|
|
|
|
|
|
|
message, broken = suite.runInvariant("deposits", keeper.DepositsInvariant)
|
|
|
|
suite.Equal("savings: validate deposits broken invariant\ndeposit invalid\n", message)
|
|
|
|
suite.Equal(true, broken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *invariantTestSuite) TestSolvencyInvariant() {
|
|
|
|
message, broken := suite.runInvariant("solvency", keeper.SolvencyInvariant)
|
|
|
|
suite.Equal("savings: module solvency broken invariant\ntotal deposited amount does not match module account\n", message)
|
|
|
|
suite.Equal(false, broken)
|
|
|
|
|
|
|
|
suite.SetupValidState()
|
|
|
|
message, broken = suite.runInvariant("solvency", keeper.SolvencyInvariant)
|
|
|
|
suite.Equal("savings: module solvency broken invariant\ntotal deposited amount does not match module account\n", message)
|
|
|
|
suite.Equal(false, broken)
|
|
|
|
|
|
|
|
// broken when deposits are greater than module balance
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, types.NewDeposit(
|
|
|
|
suite.addrs[0],
|
2023-04-05 23:21:59 +00:00
|
|
|
sdk.NewCoins(sdk.NewCoin("ukava", sdkmath.NewInt(3e8))),
|
2022-03-30 11:51:06 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
message, broken = suite.runInvariant("solvency", keeper.SolvencyInvariant)
|
|
|
|
suite.Equal("savings: module solvency broken invariant\ntotal deposited amount does not match module account\n", message)
|
|
|
|
suite.Equal(true, broken)
|
|
|
|
|
|
|
|
// broken when deposits are less than the module balance
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, types.NewDeposit(
|
|
|
|
suite.addrs[0],
|
2023-04-05 23:21:59 +00:00
|
|
|
sdk.NewCoins(sdk.NewCoin("ukava", sdkmath.NewInt(1e8))),
|
2022-03-30 11:51:06 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
message, broken = suite.runInvariant("solvency", keeper.SolvencyInvariant)
|
|
|
|
suite.Equal("savings: module solvency broken invariant\ntotal deposited amount does not match module account\n", message)
|
|
|
|
suite.Equal(true, broken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvariantTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(invariantTestSuite))
|
|
|
|
}
|