Skip to content

Commit

Permalink
Merge pull request #63 from osmosis-labs/anmol/params-genesis
Browse files Browse the repository at this point in the history
Add params setting via genesis
  • Loading branch information
Anmol1696 authored Aug 30, 2023
2 parents 9539fc2 + ab67069 commit 115eec1
Show file tree
Hide file tree
Showing 9 changed files with 624 additions and 10 deletions.
9 changes: 2 additions & 7 deletions demo/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import (
ibcfee "github.com/cosmos/ibc-go/v7/modules/apps/29-fee"
ibcfeekeeper "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/keeper"
ibcfeetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
transfer "github.com/cosmos/ibc-go/v7/modules/apps/transfer"
"github.com/cosmos/ibc-go/v7/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v7/modules/core"
Expand Down Expand Up @@ -871,12 +871,6 @@ func NewMeshApp(
if err := app.WasmKeeper.InitializePinnedCodes(ctx); err != nil {
tmos.Exit(fmt.Sprintf("failed initialize pinned codes %s", err))
}

// todo: remove set-params and initialize via genesis
if err := app.MeshSecKeeper.SetParams(ctx, meshsectypes.DefaultParams(sdk.DefaultBondDenom)); err != nil {
tmos.Exit(fmt.Sprintf("failed to set mesh params: %s", err))
}

}

return app
Expand Down Expand Up @@ -1094,6 +1088,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(icacontrollertypes.SubModuleName)
paramsKeeper.Subspace(wasm.ModuleName)
paramsKeeper.Subspace(meshsectypes.ModuleName)

return paramsKeeper
}
34 changes: 34 additions & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- [Params](#osmosis.meshsecurity.v1beta1.Params)
- [VirtualStakingMaxCapInfo](#osmosis.meshsecurity.v1beta1.VirtualStakingMaxCapInfo)

- [osmosis/meshsecurity/v1beta1/genesis.proto](#osmosis/meshsecurity/v1beta1/genesis.proto)
- [GenesisState](#osmosis.meshsecurity.v1beta1.GenesisState)

- [osmosis/meshsecurity/v1beta1/query.proto](#osmosis/meshsecurity/v1beta1/query.proto)
- [QueryParamsRequest](#osmosis.meshsecurity.v1beta1.QueryParamsRequest)
- [QueryParamsResponse](#osmosis.meshsecurity.v1beta1.QueryParamsResponse)
Expand Down Expand Up @@ -69,6 +72,37 @@ virtual staking max cap



<!-- end messages -->

<!-- end enums -->

<!-- end HasExtensions -->

<!-- end services -->



<a name="osmosis/meshsecurity/v1beta1/genesis.proto"></a>
<p align="right"><a href="#top">Top</a></p>

## osmosis/meshsecurity/v1beta1/genesis.proto



<a name="osmosis.meshsecurity.v1beta1.GenesisState"></a>

### GenesisState
GenesisState defines meshsecurity module's genesis state.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `params` | [Params](#osmosis.meshsecurity.v1beta1.Params) | | |





<!-- end messages -->

<!-- end enums -->
Expand Down
18 changes: 18 additions & 0 deletions proto/osmosis/meshsecurity/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";
package osmosis.meshsecurity.v1beta1;

import "osmosis/meshsecurity/v1beta1/meshsecurity.proto";
import "gogoproto/gogo.proto";
import "amino/amino.proto";

option go_package = "github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types";
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = false;

// GenesisState defines meshsecurity module's genesis state.
message GenesisState {
option (gogoproto.equal) = true;

Params params = 1
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}
18 changes: 18 additions & 0 deletions x/meshsecurity/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types"
)

func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
if err := k.SetParams(ctx, data.Params); err != nil {
panic(err)
}
}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
params := k.GetParams(ctx)
return types.NewGenesisState(params)
}
67 changes: 67 additions & 0 deletions x/meshsecurity/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package keeper

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"

"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types"
)

func TestInitGenesis(t *testing.T) {
specs := map[string]struct {
state types.GenesisState
expErr bool
}{
"custom param, should pass": {
state: types.GenesisState{
Params: types.Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(15_000_000_000)),
EpochLength: 2_000,
MaxGasEndBlocker: 600_000,
},
},
expErr: false,
},
"custom small value param, should pass": {
state: types.GenesisState{
Params: types.Params{
TotalContractsMaxCap: sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1_000_000)),
EpochLength: 20,
MaxGasEndBlocker: 10_000,
},
},
expErr: false,
},
}

for name, spec := range specs {
t.Run(name, func(t *testing.T) {
pCtx, keepers := CreateDefaultTestInput(t)
k := keepers.MeshKeeper

k.InitGenesis(pCtx, spec.state)

p := k.GetParams(pCtx)
assert.Equal(t, spec.state.Params.MaxGasEndBlocker, p.MaxGasEndBlocker)
assert.Equal(t, spec.state.Params.EpochLength, p.EpochLength)
assert.Equal(t, spec.state.Params.TotalContractsMaxCap, p.TotalContractsMaxCap)
})
}
}

func TestExportGenesis(t *testing.T) {
pCtx, keepers := CreateDefaultTestInput(t)
k := keepers.MeshKeeper
params := types.DefaultParams(sdk.DefaultBondDenom)

err := k.SetParams(pCtx, params)
require.NoError(t, err)

exported := k.ExportGenesis(pCtx)
assert.Equal(t, params.MaxGasEndBlocker, exported.Params.MaxGasEndBlocker)
assert.Equal(t, params.EpochLength, exported.Params.EpochLength)
assert.Equal(t, params.TotalContractsMaxCap, exported.Params.TotalContractsMaxCap)
}
26 changes: 23 additions & 3 deletions x/meshsecurity/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package meshsecurity
import (
"context"
"encoding/json"
"fmt"

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

Expand Down Expand Up @@ -49,12 +50,17 @@ func (AppModuleBasic) Name() string {
// DefaultGenesis returns default genesis state as raw bytes for the mesh-security
// module.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return nil
return cdc.MustMarshalJSON(types.DefaultGenesisState(sdk.DefaultBondDenom))
}

// ValidateGenesis performs genesis state validation for the mesh-security module.
func (b AppModuleBasic) ValidateGenesis(marshaler codec.JSONCodec, _ client.TxEncodingConfig, message json.RawMessage) error {
return nil
func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var data types.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}

return types.ValidateGenesis(&data)
}

// GetTxCmd returns the root tx command for the mesh-security module.
Expand Down Expand Up @@ -111,6 +117,20 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// InitGenesis performs genesis initialization for the mesh-security module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) []abci.ValidatorUpdate {
var data types.GenesisState
cdc.MustUnmarshalJSON(bz, &data)
am.k.InitGenesis(ctx, data)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the mesh-security
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(am.k.ExportGenesis(ctx))
}

// QuerierRoute returns the bank module's querier route name.
func (AppModule) QuerierRoute() string {
return types.RouterKey
Expand Down
15 changes: 15 additions & 0 deletions x/meshsecurity/types/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package types

func NewGenesisState(params Params) *GenesisState {
return &GenesisState{
Params: params,
}
}

func DefaultGenesisState(denom string) *GenesisState {
return NewGenesisState(DefaultParams(denom))
}

func ValidateGenesis(gs *GenesisState) error {
return gs.Params.ValidateBasic()
}
Loading

0 comments on commit 115eec1

Please sign in to comment.