From c175e9e856fe7a5bb63af092d70c8f1bc4845fa4 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Wed, 29 Apr 2020 10:10:41 -0400 Subject: [PATCH] Add kava modules to TestAppImportExport (#472) * add kava modules to TestAppImportExport --- app/params/params.go | 8 ++--- app/sim_test.go | 30 ++++++++++++++----- x/cdp/alias.go | 2 -- x/cdp/genesis.go | 6 ++-- x/cdp/keeper/cdp.go | 15 +++------- x/cdp/keeper/savings_test.go | 2 +- x/cdp/module.go | 10 +++---- x/cdp/simulation/operations.go | 26 ++++++++-------- x/cdp/types/expected_keepers.go | 5 ++-- x/validator-vesting/simulation/decoder.go | 8 ++--- .../simulation/decoder_test.go | 17 ++++++----- x/validator-vesting/simulation/genesis.go | 2 ++ 12 files changed, 72 insertions(+), 59 deletions(-) diff --git a/app/params/params.go b/app/params/params.go index 17138c3d..64cf7471 100644 --- a/app/params/params.go +++ b/app/params/params.go @@ -8,9 +8,9 @@ const ( // Default simulation operation weights for messages and gov proposals const ( - DefaultWeightMsgPlaceBid int = 100 - DefaultWeightMsgCreateAtomicSwap int = 100 - DefaultWeightMsgUpdatePrices int = 100 + DefaultWeightMsgPlaceBid int = 75 + DefaultWeightMsgCreateAtomicSwap int = 50 + DefaultWeightMsgUpdatePrices int = 50 DefaultWeightMsgCdp int = 100 - DefaultWeightMsgClaimReward int = 100 + DefaultWeightMsgClaimReward int = 50 ) diff --git a/app/sim_test.go b/app/sim_test.go index 4081978a..d6bd256b 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -6,11 +6,6 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/log" - dbm "github.com/tendermint/tm-db" - "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/simapp/helpers" @@ -25,6 +20,17 @@ import ( "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/staking" "github.com/cosmos/cosmos-sdk/x/supply" + "github.com/kava-labs/kava/x/auction" + "github.com/kava-labs/kava/x/bep3" + "github.com/kava-labs/kava/x/cdp" + "github.com/kava-labs/kava/x/incentive" + "github.com/kava-labs/kava/x/kavadist" + "github.com/kava-labs/kava/x/pricefeed" + validatorvesting "github.com/kava-labs/kava/x/validator-vesting" + "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" + dbm "github.com/tendermint/tm-db" ) type StoreKeysPrefixes struct { @@ -162,6 +168,13 @@ func TestAppImportExport(t *testing.T) { {app.keys[supply.StoreKey], newApp.keys[supply.StoreKey], [][]byte{}}, {app.keys[params.StoreKey], newApp.keys[params.StoreKey], [][]byte{}}, {app.keys[gov.StoreKey], newApp.keys[gov.StoreKey], [][]byte{}}, + {app.keys[auction.StoreKey], newApp.keys[auction.StoreKey], [][]byte{}}, + {app.keys[bep3.StoreKey], newApp.keys[bep3.StoreKey], [][]byte{}}, + {app.keys[cdp.StoreKey], newApp.keys[cdp.StoreKey], [][]byte{}}, + {app.keys[incentive.StoreKey], newApp.keys[incentive.StoreKey], [][]byte{}}, + {app.keys[kavadist.StoreKey], newApp.keys[kavadist.StoreKey], [][]byte{}}, + {app.keys[pricefeed.StoreKey], newApp.keys[pricefeed.StoreKey], [][]byte{}}, + {app.keys[validatorvesting.StoreKey], newApp.keys[validatorvesting.StoreKey], [][]byte{}}, } for _, skp := range storeKeysPrefixes { @@ -170,8 +183,9 @@ func TestAppImportExport(t *testing.T) { failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.Prefixes) require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare") - - fmt.Printf("compared %d key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B) + if len(failedKVAs) != 0 { + fmt.Printf("found %d non-equal key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B) + } require.Equal(t, len(failedKVAs), 0, simapp.GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, app.Codec(), failedKVAs, failedKVBs)) } } @@ -284,4 +298,4 @@ func TestAppStateDeterminism(t *testing.T) { ) } } -} \ No newline at end of file +} diff --git a/x/cdp/alias.go b/x/cdp/alias.go index d7b24658..a390e006 100644 --- a/x/cdp/alias.go +++ b/x/cdp/alias.go @@ -142,8 +142,6 @@ type ( AugmentedCDPs = types.AugmentedCDPs Deposit = types.Deposit Deposits = types.Deposits - SupplyKeeper = types.SupplyKeeper - PricefeedKeeper = types.PricefeedKeeper GenesisState = types.GenesisState MsgCreateCDP = types.MsgCreateCDP MsgDeposit = types.MsgDeposit diff --git a/x/cdp/genesis.go b/x/cdp/genesis.go index 7015359a..241fdcd7 100644 --- a/x/cdp/genesis.go +++ b/x/cdp/genesis.go @@ -4,10 +4,11 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/kava-labs/kava/x/cdp/types" ) // InitGenesis sets initial genesis state for cdp module -func InitGenesis(ctx sdk.Context, k Keeper, pk PricefeedKeeper, sk SupplyKeeper, gs GenesisState) { +func InitGenesis(ctx sdk.Context, k Keeper, pk types.PricefeedKeeper, sk types.SupplyKeeper, gs GenesisState) { if err := gs.Validate(); err != nil { panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err)) @@ -61,12 +62,13 @@ func InitGenesis(ctx sdk.Context, k Keeper, pk PricefeedKeeper, sk SupplyKeeper, k.IndexCdpByOwner(ctx, cdp) ratio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees)) k.IndexCdpByCollateralRatio(ctx, cdp.Collateral.Denom, cdp.ID, ratio) - k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, cdp.Principal) + k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, cdp.Principal.Add(cdp.AccumulatedFees)) } k.SetNextCdpID(ctx, gs.StartingCdpID) k.SetDebtDenom(ctx, gs.DebtDenom) k.SetGovDenom(ctx, gs.GovDenom) + k.SetPreviousSavingsDistribution(ctx, gs.PreviousDistributionTime) for _, d := range gs.Deposits { k.SetDeposit(ctx, d) diff --git a/x/cdp/keeper/cdp.go b/x/cdp/keeper/cdp.go index 90b765ef..ba140aa2 100644 --- a/x/cdp/keeper/cdp.go +++ b/x/cdp/keeper/cdp.go @@ -1,7 +1,6 @@ package keeper import ( - "bytes" "fmt" "github.com/cosmos/cosmos-sdk/store/prefix" @@ -151,8 +150,7 @@ func (k Keeper) GetCdpID(ctx sdk.Context, owner sdk.AccAddress, denom string) (u func (k Keeper) GetCdpIdsByOwner(ctx sdk.Context, owner sdk.AccAddress) ([]uint64, bool) { store := prefix.NewStore(ctx.KVStore(k.key), types.CdpIDKeyPrefix) bz := store.Get(owner) - // TODO figure out why this is necessary - if bz == nil || bytes.Equal(bz, []byte{0}) { + if bz == nil { return []uint64{}, false } var cdpIDs []uint64 @@ -272,13 +270,8 @@ func (k Keeper) IndexCdpByOwner(ctx sdk.Context, cdp types.CDP) { store.Set(cdp.Owner, idBytes) return } - for _, id := range cdpIDs { - if id == cdp.ID { - return - } - cdpIDs = append(cdpIDs, cdp.ID) - store.Set(cdp.Owner, k.cdc.MustMarshalBinaryLengthPrefixed(cdpIDs)) - } + cdpIDs = append(cdpIDs, cdp.ID) + store.Set(cdp.Owner, k.cdc.MustMarshalBinaryLengthPrefixed(cdpIDs)) } // RemoveCdpOwnerIndex deletes the cdp id from the store's index of cdps by owner @@ -296,9 +289,9 @@ func (k Keeper) RemoveCdpOwnerIndex(ctx sdk.Context, cdp types.CDP) { } if len(updatedCdpIds) == 0 { store.Delete(cdp.Owner) + return } store.Set(cdp.Owner, k.cdc.MustMarshalBinaryLengthPrefixed(updatedCdpIds)) - } // IndexCdpByCollateralRatio sets the cdp id in the store, indexed by the collateral type and collateral to debt ratio diff --git a/x/cdp/keeper/savings_test.go b/x/cdp/keeper/savings_test.go index 5e5a910e..5c04b269 100644 --- a/x/cdp/keeper/savings_test.go +++ b/x/cdp/keeper/savings_test.go @@ -66,7 +66,7 @@ func (suite *SavingsTestSuite) TestGetSetPreviousDistributionTime() { now := tmtime.Now() _, f := suite.keeper.GetPreviousSavingsDistribution(suite.ctx) - suite.False(f) + suite.True(f) suite.NotPanics(func() { suite.keeper.SetPreviousSavingsDistribution(suite.ctx, now) }) diff --git a/x/cdp/module.go b/x/cdp/module.go index 1794aba2..9eb8ad3d 100644 --- a/x/cdp/module.go +++ b/x/cdp/module.go @@ -11,13 +11,13 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" - "github.com/cosmos/cosmos-sdk/x/auth" sim "github.com/cosmos/cosmos-sdk/x/simulation" abci "github.com/tendermint/tendermint/abci/types" "github.com/kava-labs/kava/x/cdp/client/cli" "github.com/kava-labs/kava/x/cdp/client/rest" "github.com/kava-labs/kava/x/cdp/simulation" + "github.com/kava-labs/kava/x/cdp/types" ) var ( @@ -76,13 +76,13 @@ type AppModule struct { AppModuleBasic keeper Keeper - accountKeeper auth.AccountKeeper - pricefeedKeeper PricefeedKeeper - supplyKeeper SupplyKeeper + accountKeeper types.AccountKeeper + pricefeedKeeper types.PricefeedKeeper + supplyKeeper types.SupplyKeeper } // NewAppModule creates a new AppModule object -func NewAppModule(keeper Keeper, accountKeeper auth.AccountKeeper, pricefeedKeeper PricefeedKeeper, supplyKeeper SupplyKeeper) AppModule { +func NewAppModule(keeper Keeper, accountKeeper types.AccountKeeper, pricefeedKeeper types.PricefeedKeeper, supplyKeeper types.SupplyKeeper) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{}, keeper: keeper, diff --git a/x/cdp/simulation/operations.go b/x/cdp/simulation/operations.go index 4e83e931..21496f22 100644 --- a/x/cdp/simulation/operations.go +++ b/x/cdp/simulation/operations.go @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/simapp/helpers" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth" authexported "github.com/cosmos/cosmos-sdk/x/auth/exported" "github.com/cosmos/cosmos-sdk/x/simulation" @@ -23,7 +22,7 @@ const ( // WeightedOperations returns all the operations from the module with their respective weights func WeightedOperations( - appParams simulation.AppParams, cdc *codec.Codec, ak auth.AccountKeeper, + appParams simulation.AppParams, cdc *codec.Codec, ak types.AccountKeeper, k keeper.Keeper, pfk types.PricefeedKeeper, ) simulation.WeightedOperations { var weightMsgCdp int @@ -43,7 +42,7 @@ func WeightedOperations( } // SimulateMsgCdp generates a MsgCreateCdp or MsgDepositCdp with random values. -func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedKeeper) simulation.Operation { +func SimulateMsgCdp(ak types.AccountKeeper, k keeper.Keeper, pfk types.PricefeedKeeper) simulation.Operation { return func( r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simulation.Account, chainID string, ) (simulation.OperationMsg, []simulation.FutureOperation, error) { @@ -125,12 +124,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK simAccount.PrivKey, ) - _, result, err := app.Deliver(tx) + _, _, err := app.Deliver(tx) if err != nil { return simulation.NoOpMsg(types.ModuleName), nil, err } - return simulation.NewOperationMsg(msg, true, result.Log), nil, nil + return simulation.NewOperationMsg(msg, true, ""), nil, nil } // a cdp already exists, deposit to it, draw debt from it, or repay debt to it @@ -149,12 +148,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK simAccount.PrivKey, ) - _, result, err := app.Deliver(tx) + _, _, err := app.Deliver(tx) if err != nil { return simulation.NoOpMsg(types.ModuleName), nil, err } - return simulation.NewOperationMsg(msg, true, result.Log), nil, nil + return simulation.NewOperationMsg(msg, true, ""), nil, nil } // deposit 25% of the time @@ -172,12 +171,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK simAccount.PrivKey, ) - _, result, err := app.Deliver(tx) + _, _, err := app.Deliver(tx) if err != nil { return simulation.NoOpMsg(types.ModuleName), nil, err } - return simulation.NewOperationMsg(msg, true, result.Log), nil, nil + return simulation.NewOperationMsg(msg, true, ""), nil, nil } // draw debt 25% of the time @@ -215,12 +214,13 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK simAccount.PrivKey, ) - _, result, err := app.Deliver(tx) + _, _, err := app.Deliver(tx) + if err != nil { return simulation.NoOpMsg(types.ModuleName), nil, err } - return simulation.NewOperationMsg(msg, true, result.Log), nil, nil + return simulation.NewOperationMsg(msg, true, ""), nil, nil } // repay debt 25% of the time @@ -248,12 +248,12 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k keeper.Keeper, pfk types.PricefeedK simAccount.PrivKey, ) - _, result, err := app.Deliver(tx) + _, _, err := app.Deliver(tx) if err != nil { return simulation.NoOpMsg(types.ModuleName), nil, err } - return simulation.NewOperationMsg(msg, true, result.Log), nil, nil + return simulation.NewOperationMsg(msg, true, ""), nil, nil } return simulation.NoOpMsg(types.ModuleName), nil, nil diff --git a/x/cdp/types/expected_keepers.go b/x/cdp/types/expected_keepers.go index ae5bb5bd..c0e2e3d9 100644 --- a/x/cdp/types/expected_keepers.go +++ b/x/cdp/types/expected_keepers.go @@ -9,7 +9,7 @@ import ( pftypes "github.com/kava-labs/kava/x/pricefeed/types" ) -// SupplyKeeper defines the expected supply keeper for module accounts +// SupplyKeeper defines the expected supply keeper for module accounts (noalias) type SupplyKeeper interface { GetModuleAddress(name string) sdk.AccAddress GetModuleAccount(ctx sdk.Context, name string) supplyexported.ModuleAccountI @@ -25,7 +25,7 @@ type SupplyKeeper interface { GetSupply(ctx sdk.Context) (supply supplyexported.SupplyI) } -// PricefeedKeeper defines the expected interface for the pricefeed +// PricefeedKeeper defines the expected interface for the pricefeed (noalias) type PricefeedKeeper interface { GetCurrentPrice(sdk.Context, string) (pftypes.CurrentPrice, error) GetParams(sdk.Context) pftypes.Params @@ -45,4 +45,5 @@ type AuctionKeeper interface { // AccountKeeper expected interface for the account keeper (noalias) type AccountKeeper interface { IterateAccounts(ctx sdk.Context, cb func(account authexported.Account) (stop bool)) + GetAccount(ctx sdk.Context, addr sdk.AccAddress) authexported.Account } diff --git a/x/validator-vesting/simulation/decoder.go b/x/validator-vesting/simulation/decoder.go index 6e3207ab..7dc83ff6 100644 --- a/x/validator-vesting/simulation/decoder.go +++ b/x/validator-vesting/simulation/decoder.go @@ -6,7 +6,7 @@ import ( "time" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/x/auth/exported" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/kava-labs/kava/x/validator-vesting/types" "github.com/tendermint/tendermint/libs/kv" ) @@ -15,9 +15,9 @@ import ( func DecodeStore(cdc *codec.Codec, kvA, kvB kv.Pair) string { switch { case bytes.Equal(kvA.Key[:1], types.ValidatorVestingAccountPrefix): - var accA, accB exported.Account - cdc.MustUnmarshalBinaryBare(kvA.Value, &accA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &accB) + var accA, accB sdk.AccAddress + accA = sdk.AccAddress(kvA.Key[1:]) + accB = sdk.AccAddress(kvB.Key[1:]) return fmt.Sprintf("%v\n%v", accA, accB) case bytes.Equal(kvA.Key, types.BlocktimeKey): var btA, btB time.Time diff --git a/x/validator-vesting/simulation/decoder_test.go b/x/validator-vesting/simulation/decoder_test.go index adc0cb24..c029499b 100644 --- a/x/validator-vesting/simulation/decoder_test.go +++ b/x/validator-vesting/simulation/decoder_test.go @@ -5,14 +5,18 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/libs/kv" - "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/kava-labs/kava/x/validator-vesting/types" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/libs/kv" +) + +var ( + pk1 = ed25519.GenPrivKey().PubKey() + addr1 = sdk.AccAddress(pk1.Address()) ) func makeTestCodec() (cdc *codec.Codec) { @@ -28,11 +32,10 @@ func makeTestCodec() (cdc *codec.Codec) { func TestDecodeDistributionStore(t *testing.T) { cdc := makeTestCodec() - acc := types.ValidatorVestingAccount{SigningThreshold: 1} now := time.Now().UTC() kvPairs := kv.Pairs{ - kv.Pair{Key: types.ValidatorVestingAccountPrefix, Value: cdc.MustMarshalBinaryBare(acc)}, + kv.Pair{Key: append(types.ValidatorVestingAccountPrefix, addr1.Bytes()...), Value: []byte{0}}, kv.Pair{Key: types.BlocktimeKey, Value: cdc.MustMarshalBinaryLengthPrefixed(now)}, kv.Pair{Key: []byte{0x99}, Value: []byte{0x99}}, } @@ -41,7 +44,7 @@ func TestDecodeDistributionStore(t *testing.T) { name string expectedLog string }{ - {"ValidatorVestingAccount", fmt.Sprintf("%v\n%v", acc, acc)}, + {"ValidatorVestingAccount", fmt.Sprintf("%v\n%v", addr1, addr1)}, {"BlockTime", fmt.Sprintf("%s\n%s", now, now)}, {"other", ""}, } diff --git a/x/validator-vesting/simulation/genesis.go b/x/validator-vesting/simulation/genesis.go index 7b133dff..414bb0f9 100644 --- a/x/validator-vesting/simulation/genesis.go +++ b/x/validator-vesting/simulation/genesis.go @@ -73,6 +73,8 @@ func RandomizedGenState(simState *module.SimulationState) { } newAuthGenesis := authtypes.NewGenesisState(authGenState.Params, newGenesisAccs) simState.GenState[authtypes.ModuleName] = simState.Cdc.MustMarshalJSON(newAuthGenesis) + vestGenState := types.DefaultGenesisState() + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(vestGenState) } func getRandomValidatorConsAddr(simState *module.SimulationState, rint int) sdk.ConsAddress {