From c7b1331f4dd4310a799386cff3d99d098aec72d9 Mon Sep 17 00:00:00 2001 From: Ruaridh Date: Tue, 25 Feb 2020 15:11:09 +0000 Subject: [PATCH] Fix simulations (#377) * stub out simulation integration for cdp, pricefeed * stub out simulation integration for auction * fix cdp export * update pricefeed to match * update validator-vesting to match --- app/app.go | 3 +++ x/auction/module.go | 36 +++++++++++++++++++++++++---- x/auction/simulation/decoder.go | 12 ++++++++++ x/auction/simulation/genesis.go | 22 ++++++++++++++++++ x/auction/simulation/params.go | 14 ++++++++++++ x/cdp/alias.go | 1 + x/cdp/genesis.go | 25 ++++++++++++-------- x/cdp/module.go | 32 ++++++++++++++++++++++++-- x/cdp/simulation/decoder.go | 12 ++++++++++ x/cdp/simulation/genesis.go | 22 ++++++++++++++++++ x/cdp/simulation/params.go | 14 ++++++++++++ x/cdp/types/genesis.go | 13 +++++++++++ x/pricefeed/genesis.go | 5 +--- x/pricefeed/module.go | 38 +++++++++++++++++++++++++++---- x/pricefeed/simulation/decoder.go | 12 ++++++++++ x/pricefeed/simulation/genesis.go | 22 ++++++++++++++++++ x/pricefeed/simulation/params.go | 14 ++++++++++++ x/validator-vesting/genesis.go | 2 +- 18 files changed, 274 insertions(+), 25 deletions(-) create mode 100644 x/auction/simulation/decoder.go create mode 100644 x/auction/simulation/genesis.go create mode 100644 x/auction/simulation/params.go create mode 100644 x/cdp/simulation/decoder.go create mode 100644 x/cdp/simulation/genesis.go create mode 100644 x/cdp/simulation/params.go create mode 100644 x/pricefeed/simulation/decoder.go create mode 100644 x/pricefeed/simulation/genesis.go create mode 100644 x/pricefeed/simulation/params.go diff --git a/app/app.go b/app/app.go index 437d90bf..b92d7a95 100644 --- a/app/app.go +++ b/app/app.go @@ -304,6 +304,9 @@ func NewApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, distr.NewAppModule(app.distrKeeper, app.supplyKeeper), staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper), slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper), + cdp.NewAppModule(app.cdpKeeper, app.pricefeedKeeper), // TODO how is the order be decided here? Is this order correct? + pricefeed.NewAppModule(app.pricefeedKeeper), + auction.NewAppModule(app.auctionKeeper, app.supplyKeeper), ) app.sm.RegisterStoreDecoders() diff --git a/x/auction/module.go b/x/auction/module.go index acd36010..07f5a953 100644 --- a/x/auction/module.go +++ b/x/auction/module.go @@ -3,23 +3,28 @@ package auction import ( "encoding/json" "fmt" + "math/rand" + + "github.com/gorilla/mux" + "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" - "github.com/gorilla/mux" - "github.com/spf13/cobra" + sim "github.com/cosmos/cosmos-sdk/x/simulation" abci "github.com/tendermint/tendermint/abci/types" "github.com/kava-labs/kava/x/auction/client/cli" "github.com/kava-labs/kava/x/auction/client/rest" + "github.com/kava-labs/kava/x/auction/simulation" "github.com/kava-labs/kava/x/auction/types" ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) // AppModuleBasic implements the sdk.AppModuleBasic interface. @@ -65,9 +70,32 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the auction module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for auction module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.DecodeStore +} + +// GenerateGenesisState creates a randomized GenState of the auction module +func (AppModuleSimulation) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// RandomizedParams creates randomized auction param changes for the simulator. +func (AppModuleSimulation) RandomizedParams(r *rand.Rand) []sim.ParamChange { + return simulation.ParamChanges(r) +} + +//____________________________________________________________________________ + // AppModule implements the sdk.AppModule interface. type AppModule struct { AppModuleBasic + AppModuleSimulation keeper Keeper supplyKeeper types.SupplyKeeper diff --git a/x/auction/simulation/decoder.go b/x/auction/simulation/decoder.go new file mode 100644 index 00000000..a64f8744 --- /dev/null +++ b/x/auction/simulation/decoder.go @@ -0,0 +1,12 @@ +package simulation + +import ( + "github.com/cosmos/cosmos-sdk/codec" + cmn "github.com/tendermint/tendermint/libs/common" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding auction type +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { + // TODO implement this + return "" +} diff --git a/x/auction/simulation/genesis.go b/x/auction/simulation/genesis.go new file mode 100644 index 00000000..20bac731 --- /dev/null +++ b/x/auction/simulation/genesis.go @@ -0,0 +1,22 @@ +package simulation + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/kava-labs/kava/x/auction/types" +) + +// RandomizedGenState generates a random GenesisState for auction +func RandomizedGenState(simState *module.SimulationState) { + + // TODO implement this fully + // - randomly generating the genesis params + // - overwriting with genesis provided to simulation + auctionGenesis := types.DefaultGenesisState() + + fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, codec.MustMarshalJSONIndent(simState.Cdc, auctionGenesis)) + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(auctionGenesis) +} diff --git a/x/auction/simulation/params.go b/x/auction/simulation/params.go new file mode 100644 index 00000000..8c1f7aff --- /dev/null +++ b/x/auction/simulation/params.go @@ -0,0 +1,14 @@ +package simulation + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/x/simulation" +) + +// ParamChanges defines the parameters that can be modified by param change proposals +// on the simulation +func ParamChanges(r *rand.Rand) []simulation.ParamChange { + // TODO implement this + return []simulation.ParamChange{} +} diff --git a/x/cdp/alias.go b/x/cdp/alias.go index 78080754..d542be32 100644 --- a/x/cdp/alias.go +++ b/x/cdp/alias.go @@ -78,6 +78,7 @@ var ( ErrBelowDebtFloor = types.ErrBelowDebtFloor ErrPaymentExceedsDebt = types.ErrPaymentExceedsDebt ErrLoadingAugmentedCDP = types.ErrLoadingAugmentedCDP + NewGenesisState = types.NewGenesisState DefaultGenesisState = types.DefaultGenesisState GetCdpIDBytes = types.GetCdpIDBytes GetCdpIDFromBytes = types.GetCdpIDFromBytes diff --git a/x/cdp/genesis.go b/x/cdp/genesis.go index 47a654f0..45e34b46 100644 --- a/x/cdp/genesis.go +++ b/x/cdp/genesis.go @@ -64,19 +64,26 @@ func InitGenesis(ctx sdk.Context, k Keeper, pk PricefeedKeeper, gs GenesisState) // ExportGenesis export genesis state for cdp module func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState { params := k.GetParams(ctx) - cdps := k.GetAllCdps(ctx) + + cdps := CDPs{} + deposits := Deposits{} + k.IterateAllCdps(ctx, func(cdp CDP) (stop bool) { + cdps = append(cdps, cdp) + k.IterateDeposits(ctx, cdp.ID, func(deposit Deposit) (stop bool) { + deposits = append(deposits, deposit) + return false + }) + return false + }) + cdpID := k.GetNextCdpID(ctx) + debtDenom := k.GetDebtDenom(ctx) + govDenom := k.GetGovDenom(ctx) + previousBlockTime, found := k.GetPreviousBlockTime(ctx) if !found { previousBlockTime = DefaultPreviousBlockTime } - debtDenom := k.GetDebtDenom(ctx) - return GenesisState{ - Params: params, - StartingCdpID: cdpID, - CDPs: cdps, - PreviousBlockTime: previousBlockTime, - DebtDenom: debtDenom, - } + return NewGenesisState(params, cdps, deposits, cdpID, debtDenom, govDenom, previousBlockTime) } diff --git a/x/cdp/module.go b/x/cdp/module.go index 99855986..108189de 100644 --- a/x/cdp/module.go +++ b/x/cdp/module.go @@ -2,6 +2,7 @@ package cdp import ( "encoding/json" + "math/rand" "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -10,15 +11,18 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + 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" ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) // AppModuleBasic app module basics object @@ -64,9 +68,33 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the cdp module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for cdp module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.DecodeStore +} + +// GenerateGenesisState creates a randomized GenState of the cdp module +func (AppModuleSimulation) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// RandomizedParams creates randomized cdp param changes for the simulator. +func (AppModuleSimulation) RandomizedParams(r *rand.Rand) []sim.ParamChange { + return simulation.ParamChanges(r) +} + +//____________________________________________________________________________ + // AppModule app module type type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper pricefeedKeeper PricefeedKeeper } diff --git a/x/cdp/simulation/decoder.go b/x/cdp/simulation/decoder.go new file mode 100644 index 00000000..edef9051 --- /dev/null +++ b/x/cdp/simulation/decoder.go @@ -0,0 +1,12 @@ +package simulation + +import ( + "github.com/cosmos/cosmos-sdk/codec" + cmn "github.com/tendermint/tendermint/libs/common" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding cdp type +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { + // TODO implement this + return "" +} diff --git a/x/cdp/simulation/genesis.go b/x/cdp/simulation/genesis.go new file mode 100644 index 00000000..96bdfb21 --- /dev/null +++ b/x/cdp/simulation/genesis.go @@ -0,0 +1,22 @@ +package simulation + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/kava-labs/kava/x/cdp/types" +) + +// RandomizedGenState generates a random GenesisState for cdp +func RandomizedGenState(simState *module.SimulationState) { + + // TODO implement this fully + // - randomly generating the genesis params + // - overwriting with genesis provided to simulation + cdpGenesis := types.DefaultGenesisState() + + fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, codec.MustMarshalJSONIndent(simState.Cdc, cdpGenesis)) + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(cdpGenesis) +} diff --git a/x/cdp/simulation/params.go b/x/cdp/simulation/params.go new file mode 100644 index 00000000..8c1f7aff --- /dev/null +++ b/x/cdp/simulation/params.go @@ -0,0 +1,14 @@ +package simulation + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/x/simulation" +) + +// ParamChanges defines the parameters that can be modified by param change proposals +// on the simulation +func ParamChanges(r *rand.Rand) []simulation.ParamChange { + // TODO implement this + return []simulation.ParamChange{} +} diff --git a/x/cdp/types/genesis.go b/x/cdp/types/genesis.go index 8737bce5..4097e6d4 100644 --- a/x/cdp/types/genesis.go +++ b/x/cdp/types/genesis.go @@ -17,6 +17,19 @@ type GenesisState struct { PreviousBlockTime time.Time `json:"previous_block_time" yaml:"previous_block_time"` } +// NewGenesisState returns a new genesis state +func NewGenesisState(params Params, cdps CDPs, deposits Deposits, startingCdpID uint64, debtDenom, govDenom string, previousBlockTime time.Time) GenesisState { + return GenesisState{ + Params: params, + CDPs: cdps, + Deposits: deposits, + StartingCdpID: startingCdpID, + DebtDenom: debtDenom, + GovDenom: govDenom, + PreviousBlockTime: previousBlockTime, + } +} + // DefaultGenesisState returns a default genesis state func DefaultGenesisState() GenesisState { return GenesisState{ diff --git a/x/pricefeed/genesis.go b/x/pricefeed/genesis.go index 41a10705..9919f6a7 100644 --- a/x/pricefeed/genesis.go +++ b/x/pricefeed/genesis.go @@ -48,8 +48,5 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState { postedPrices = append(postedPrices, pp...) } - return GenesisState{ - Params: params, - PostedPrices: postedPrices, - } + return NewGenesisState(params, postedPrices) } diff --git a/x/pricefeed/module.go b/x/pricefeed/module.go index e4136b14..ab167e02 100644 --- a/x/pricefeed/module.go +++ b/x/pricefeed/module.go @@ -2,23 +2,27 @@ package pricefeed import ( "encoding/json" + "math/rand" "github.com/gorilla/mux" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + sim "github.com/cosmos/cosmos-sdk/x/simulation" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/kava-labs/kava/x/pricefeed/client/cli" "github.com/kava-labs/kava/x/pricefeed/client/rest" - - sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" + "github.com/kava-labs/kava/x/pricefeed/simulation" ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModuleSimulation = AppModuleSimulation{} ) // AppModuleBasic app module basics object @@ -64,9 +68,33 @@ func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { return cli.GetQueryCmd(StoreKey, cdc) } +//____________________________________________________________________________ + +// AppModuleSimulation defines the module simulation functions used by the pricefeed module. +type AppModuleSimulation struct{} + +// RegisterStoreDecoder registers a decoder for pricefeed module's types +func (AppModuleSimulation) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { + sdr[StoreKey] = simulation.DecodeStore +} + +// GenerateGenesisState creates a randomized GenState of the pricefeed module +func (AppModuleSimulation) GenerateGenesisState(simState *module.SimulationState) { + simulation.RandomizedGenState(simState) +} + +// RandomizedParams creates randomized pricefeed param changes for the simulator. +func (AppModuleSimulation) RandomizedParams(r *rand.Rand) []sim.ParamChange { + return simulation.ParamChanges(r) +} + +//____________________________________________________________________________ + // AppModule app module type type AppModule struct { AppModuleBasic + AppModuleSimulation + keeper Keeper } diff --git a/x/pricefeed/simulation/decoder.go b/x/pricefeed/simulation/decoder.go new file mode 100644 index 00000000..115565b3 --- /dev/null +++ b/x/pricefeed/simulation/decoder.go @@ -0,0 +1,12 @@ +package simulation + +import ( + "github.com/cosmos/cosmos-sdk/codec" + cmn "github.com/tendermint/tendermint/libs/common" +) + +// DecodeStore unmarshals the KVPair's Value to the corresponding pricefeed type +func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string { + // TODO implement this + return "" +} diff --git a/x/pricefeed/simulation/genesis.go b/x/pricefeed/simulation/genesis.go new file mode 100644 index 00000000..756fc988 --- /dev/null +++ b/x/pricefeed/simulation/genesis.go @@ -0,0 +1,22 @@ +package simulation + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/types/module" + + "github.com/kava-labs/kava/x/pricefeed/types" +) + +// RandomizedGenState generates a random GenesisState for pricefeed +func RandomizedGenState(simState *module.SimulationState) { + + // TODO implement this fully + // - randomly generating the genesis params + // - overwriting with genesis provided to simulation + pricefeedGenesis := types.DefaultGenesisState() + + fmt.Printf("Selected randomly generated %s parameters:\n%s\n", types.ModuleName, codec.MustMarshalJSONIndent(simState.Cdc, pricefeedGenesis)) + simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(pricefeedGenesis) +} diff --git a/x/pricefeed/simulation/params.go b/x/pricefeed/simulation/params.go new file mode 100644 index 00000000..8c1f7aff --- /dev/null +++ b/x/pricefeed/simulation/params.go @@ -0,0 +1,14 @@ +package simulation + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/x/simulation" +) + +// ParamChanges defines the parameters that can be modified by param change proposals +// on the simulation +func ParamChanges(r *rand.Rand) []simulation.ParamChange { + // TODO implement this + return []simulation.ParamChange{} +} diff --git a/x/validator-vesting/genesis.go b/x/validator-vesting/genesis.go index 17762ec3..d3e463c6 100644 --- a/x/validator-vesting/genesis.go +++ b/x/validator-vesting/genesis.go @@ -22,5 +22,5 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, accountKeeper types.AccountKeep // ExportGenesis returns empty genesis state because auth exports all the genesis state we need. func ExportGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState { prevBlockTime := keeper.GetPreviousBlockTime(ctx) - return GenesisState{PreviousBlockTime: prevBlockTime} + return NewGenesisState(prevBlockTime) }