Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(sim): add sim genesis state for custom modules #1759

Merged
merged 8 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions x/devgas/v1/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@ import (
"encoding/json"
"fmt"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

abci "github.com/cometbft/cometbft/abci/types"

sdkclient "github.com/cosmos/cosmos-sdk/client"
sdkcodec "github.com/cosmos/cosmos-sdk/codec"
sdkcodectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

devgascli "github.com/NibiruChain/nibiru/x/devgas/v1/client/cli"
devgasexported "github.com/NibiruChain/nibiru/x/devgas/v1/exported"
devgaskeeper "github.com/NibiruChain/nibiru/x/devgas/v1/keeper"
devgastypes "github.com/NibiruChain/nibiru/x/devgas/v1/types"
"github.com/NibiruChain/nibiru/x/devgas/v1/client/cli"
"github.com/NibiruChain/nibiru/x/devgas/v1/exported"
"github.com/NibiruChain/nibiru/x/devgas/v1/keeper"
"github.com/NibiruChain/nibiru/x/devgas/v1/simulation"
"github.com/NibiruChain/nibiru/x/devgas/v1/types"
)

// type check to ensure the interface is properly implemented
var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)

// ConsensusVersion defines the current module consensus version.
Expand All @@ -37,13 +38,13 @@ type AppModuleBasic struct{}

// Name returns the fees module's name.
func (AppModuleBasic) Name() string {
return devgastypes.ModuleName
return types.ModuleName
}

// RegisterLegacyAminoCodec performs a no-op as the fees do not support Amino
// encoding.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *sdkcodec.LegacyAmino) {
devgastypes.RegisterLegacyAminoCodec(cdc)
types.RegisterLegacyAminoCodec(cdc)
}

// ConsensusVersion returns the consensus state-breaking version for the module.
Expand All @@ -56,22 +57,22 @@ func (AppModuleBasic) ConsensusVersion() uint64 {
func (AppModuleBasic) RegisterInterfaces(
interfaceRegistry sdkcodectypes.InterfaceRegistry,
) {
devgastypes.RegisterInterfaces(interfaceRegistry)
types.RegisterInterfaces(interfaceRegistry)
}

// DefaultGenesis returns default genesis state as raw bytes for the fees
// module.
func (AppModuleBasic) DefaultGenesis(cdc sdkcodec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(devgastypes.DefaultGenesisState())
return cdc.MustMarshalJSON(types.DefaultGenesisState())
}

// ValidateGenesis performs genesis state validation for the fees module.
func (b AppModuleBasic) ValidateGenesis(
cdc sdkcodec.JSONCodec, _ sdkclient.TxEncodingConfig, bz json.RawMessage,
) error {
var genesisState devgastypes.GenesisState
var genesisState types.GenesisState
if err := cdc.UnmarshalJSON(bz, &genesisState); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", devgastypes.ModuleName, err)
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}

return genesisState.Validate()
Expand All @@ -82,38 +83,38 @@ func (b AppModuleBasic) ValidateGenesis(
func (b AppModuleBasic) RegisterGRPCGatewayRoutes(
c sdkclient.Context, serveMux *runtime.ServeMux,
) {
if err := devgastypes.RegisterQueryHandlerClient(context.Background(), serveMux, devgastypes.NewQueryClient(c)); err != nil {
if err := types.RegisterQueryHandlerClient(context.Background(), serveMux, types.NewQueryClient(c)); err != nil {
panic(err)
}
}

// GetTxCmd returns the root tx command for the fees module.
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return devgascli.NewTxCmd()
return cli.NewTxCmd()
}

// GetQueryCmd returns the fees module's root query command.
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return devgascli.GetQueryCmd()
return cli.GetQueryCmd()
}

// ___________________________________________________________________________

// AppModule implements the AppModule interface for the fees module.
type AppModule struct {
AppModuleBasic
keeper devgaskeeper.Keeper
keeper keeper.Keeper
ak authkeeper.AccountKeeper

// legacySubspace is used solely for migration of x/params managed parameters
legacySubspace devgasexported.Subspace
legacySubspace exported.Subspace
}

// NewAppModule creates a new AppModule Object
func NewAppModule(
k devgaskeeper.Keeper,
k keeper.Keeper,
ak authkeeper.AccountKeeper,
ss devgasexported.Subspace,
ss exported.Subspace,
) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
Expand All @@ -125,23 +126,23 @@ func NewAppModule(

// Name returns the fees module's name.
func (AppModule) Name() string {
return devgastypes.ModuleName
return types.ModuleName
}

// RegisterInvariants registers the fees module's invariants.
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// QuerierRoute returns the module's query routing key.
func (am AppModule) QuerierRoute() string {
return devgastypes.RouterKey
return types.RouterKey
}

// RegisterServices registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
devgastypes.RegisterMsgServer(cfg.MsgServer(), am.keeper)
devgastypes.RegisterQueryServer(
cfg.QueryServer(), devgaskeeper.NewQuerier(am.keeper),
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterQueryServer(
cfg.QueryServer(), keeper.NewQuerier(am.keeper),
)
}

Expand All @@ -158,7 +159,7 @@ func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.Valid
// InitGenesis performs the fees module's genesis initialization. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc sdkcodec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState devgastypes.GenesisState
var genesisState types.GenesisState

cdc.MustUnmarshalJSON(data, &genesisState)
InitGenesis(ctx, am.keeper, genesisState)
Expand All @@ -170,3 +171,21 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc sdkcodec.JSONCodec) json.
gs := ExportGenesis(ctx, am.keeper)
return cdc.MustMarshalJSON(gs)
}

//----------------------------------------------------------------------------
// AppModuleSimulation functions
//----------------------------------------------------------------------------

// GenerateGenesisState implements module.AppModuleSimulation.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// RegisterStoreDecoder implements module.AppModuleSimulation.
func (AppModule) RegisterStoreDecoder(sdk.StoreDecoderRegistry) {
}

// WeightedOperations implements module.AppModuleSimulation.
func (AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return nil
}
46 changes: 46 additions & 0 deletions x/devgas/v1/simulation/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package simulation

// DONTCOVER

import (
"encoding/json"
"fmt"
"math/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/NibiruChain/nibiru/x/devgas/v1/types"
)

const (
DeveloperFeeShare = "developer_fee_share"
)

func GenDeveloperFeeShare(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(int64(r.Intn(100)), 2)
}

func RandomizedGenState(simState *module.SimulationState) {
var developerFeeShare sdk.Dec
simState.AppParams.GetOrGenerate(
simState.Cdc, DeveloperFeeShare, &developerFeeShare, simState.Rand,
func(r *rand.Rand) { developerFeeShare = GenDeveloperFeeShare(r) },
)

devgasGenesis := types.GenesisState{
Params: types.ModuleParams{
EnableFeeShare: true,
DeveloperShares: developerFeeShare,
AllowedDenoms: []string{},
},
FeeShare: []types.FeeShare{},
}

bz, err := json.MarshalIndent(&devgasGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated x/devgas parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&devgasGenesis)
}
4 changes: 3 additions & 1 deletion x/inflation/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/NibiruChain/nibiru/x/inflation/client/cli"
"github.com/NibiruChain/nibiru/x/inflation/keeper"
"github.com/NibiruChain/nibiru/x/inflation/simulation"
"github.com/NibiruChain/nibiru/x/inflation/types"
)

Expand Down Expand Up @@ -153,7 +154,8 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
// AppModuleSimulation functions

// GenerateGenesisState creates a randomized GenState of the inflation module.
func (am AppModule) GenerateGenesisState(_ *module.SimulationState) {
func (am AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// ProposalContents doesn't return any content functions for governance proposals.
Expand Down
47 changes: 47 additions & 0 deletions x/inflation/simulation/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package simulation

// DONTCOVER

import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/NibiruChain/nibiru/x/inflation/types"
)

// RandomizedGenState generates a random GenesisState for distribution
func RandomizedGenState(simState *module.SimulationState) {
inflationGenesis := types.GenesisState{
Params: types.Params{
InflationEnabled: true,
PolynomialFactors: []sdk.Dec{
sdk.MustNewDecFromStr("-0.00014903"),
sdk.MustNewDecFromStr("0.07527647"),
sdk.MustNewDecFromStr("-19.11742154"),
sdk.MustNewDecFromStr("3170.0969905"),
sdk.MustNewDecFromStr("-339271.31060432"),
sdk.MustNewDecFromStr("18063678.8582418"),
},
InflationDistribution: types.InflationDistribution{
CommunityPool: sdk.NewDecWithPrec(35_159141, 8), // 35.159141%
StakingRewards: sdk.NewDecWithPrec(27_757217, 8), // 27.757217%
StrategicReserves: sdk.NewDecWithPrec(37_083642, 8), // 37.083642%
},
EpochsPerPeriod: 30,
PeriodsPerYear: 12,
MaxPeriod: 8 * 12,
},
Period: 0,
SkippedEpochs: 0,
}

bz, err := json.MarshalIndent(&inflationGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated x/inflation parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&inflationGenesis)
}
23 changes: 20 additions & 3 deletions x/perp/v2/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"github.com/NibiruChain/nibiru/x/perp/v2/client/cli"
"github.com/NibiruChain/nibiru/x/perp/v2/keeper"
types "github.com/NibiruChain/nibiru/x/perp/v2/types"
"github.com/NibiruChain/nibiru/x/perp/v2/simulation"
"github.com/NibiruChain/nibiru/x/perp/v2/types"
)

// type check to ensure the interface is properly implemented
var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -167,3 +170,17 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val
EndBlocker(ctx, am.keeper)
return []abci.ValidatorUpdate{}
}

// GenerateGenesisState implements module.AppModuleSimulation.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}

// RegisterStoreDecoder implements module.AppModuleSimulation.
func (AppModule) RegisterStoreDecoder(sdk.StoreDecoderRegistry) {
}

// WeightedOperations implements module.AppModuleSimulation.
func (AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return []simtypes.WeightedOperation{}
}
24 changes: 24 additions & 0 deletions x/perp/v2/simulation/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package simulation

// DONTCOVER

import (
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/types/module"

"github.com/NibiruChain/nibiru/x/perp/v2/types"
)

// RandomizedGenState generates a random GenesisState for distribution
func RandomizedGenState(simState *module.SimulationState) {
perpGenesis := *types.DefaultGenesis()

bz, err := json.MarshalIndent(&perpGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated x/tokenfactory parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&perpGenesis)
}
Loading
Loading