Skip to content

Commit

Permalink
test(sim): add sim genesis state for custom modules (#1759)
Browse files Browse the repository at this point in the history
* test(sim): add randomized inflation genesis state

* test(sim): add tokenfactory random genesis state

* test(sim): add tokenfactory random genesis state

* test(sim): update sudo random genesis

* test(sim): add devgas random genesis

* test(sim): add perp random genesis

* test(sim): wire spot sim genesis

* chore: update changelog
  • Loading branch information
k-yang authored Jan 1, 2024
1 parent f235fe2 commit d2aa561
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 117 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1728](https://github.com/NibiruChain/nibiru/pull/1728) - test(devgas-cli): CLI tests for devgas txs
* [#1735](https://github.com/NibiruChain/nibiru/pull/1735) - test(sim): fix simulation tests
* [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs
* [#1736](https://github.com/NibiruChain/nibiru/pull/1736) - test(sim): add sim genesis state for all cusom modules

### Dependencies

- Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750))
- Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756))
* Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756))

* Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720))
* Bump `golang.org/x/crypto` from 0.15.0 to 0.17.0 ([#1724](https://github.com/NibiruChain/nibiru/pull/1724))
Expand Down
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

0 comments on commit d2aa561

Please sign in to comment.