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

x/params: Raw Parameter Querying #6005

Merged
merged 5 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ information on how to implement the new `Keyring` interface.
* [ICS 023 - Vector Commitments](https://github.com/cosmos/ics/tree/master/spec/ics-023-vector-commitments) subpackage
* (ibc/ante) Implement IBC `AnteHandler` as per [ADR 15 - IBC Packet Receiver](https://github.com/cosmos/tree/master/docs/architecture/adr-015-ibc-packet-receiver.md).
* (x/capability) [\#5828](https://github.com/cosmos/cosmos-sdk/pull/5828) Capability module integration as outlined in [ADR 3 - Dynamic Capability Store](https://github.com/cosmos/tree/master/docs/architecture/adr-003-dynamic-capability-store.md).
* (x/params) [\#6005](https://github.com/cosmos/cosmos-sdk/pull/6005) Add new CLI command for querying raw x/params parameters by subspace and key.

### Bug Fixes

Expand Down
3 changes: 2 additions & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func NewSimApp(
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
ibc.NewAppModule(app.IBCKeeper),
params.NewAppModule(app.ParamsKeeper),
transferModule,
)

Expand Down Expand Up @@ -312,7 +313,7 @@ func NewSimApp(
staking.NewAppModule(app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.SupplyKeeper),
distr.NewAppModule(app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.SupplyKeeper, app.StakingKeeper),
slashing.NewAppModule(app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
params.NewAppModule(), // NOTE: only used for simulation to generate randomized param change proposals
params.NewAppModule(app.ParamsKeeper),
)

app.sm.RegisterStoreDecoders()
Expand Down
31 changes: 19 additions & 12 deletions x/params/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@ import (
)

const (
StoreKey = types.StoreKey
TStoreKey = types.TStoreKey
StoreKey = types.StoreKey
TStoreKey = types.TStoreKey
ModuleName = types.ModuleName
QuerierRoute = types.QuerierRoute
)

var (
// functions aliases
NewKeeper = keeper.NewKeeper
NewParamSetPair = types.NewParamSetPair
NewKeyTable = types.NewKeyTable
NewKeeper = keeper.NewKeeper
NewParamSetPair = types.NewParamSetPair
NewKeyTable = types.NewKeyTable
NewQuerySubspaceParams = types.NewQuerySubspaceParams
NewQuerier = keeper.NewQuerier
NewSubspaceParamsResponse = types.NewSubspaceParamsResponse
)

type (
Keeper = keeper.Keeper
ParamSetPair = types.ParamSetPair
ParamSetPairs = types.ParamSetPairs
ParamSet = types.ParamSet
Subspace = types.Subspace
ReadOnlySubspace = types.ReadOnlySubspace
KeyTable = types.KeyTable
Keeper = keeper.Keeper
ParamSetPair = types.ParamSetPair
ParamSetPairs = types.ParamSetPairs
ParamSet = types.ParamSet
Subspace = types.Subspace
ReadOnlySubspace = types.ReadOnlySubspace
KeyTable = types.KeyTable
QuerySubspaceParams = types.QuerySubspaceParams
SubspaceParamsResponse = types.SubspaceParamsResponse
)
63 changes: 63 additions & 0 deletions x/params/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/params/types"
)

// NewQueryCmd returns a root CLI command handler for all x/params query commands.
func NewQueryCmd(m codec.Marshaler) *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the params module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(NewQuerySubspaceParamsCmd(m))

return cmd
}

// NewQuerySubspaceParamsCmd returns a CLI command handler for querying subspace
// parameters managed by the x/params module.
func NewQuerySubspaceParamsCmd(m codec.Marshaler) *cobra.Command {
cmd := &cobra.Command{
Use: "subspace [subspace] [key]",
Short: "Query for raw parameters by subspace and key",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithMarshaler(m)

params := types.NewQuerySubspaceParams(args[0], args[1])
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)

bz, err := m.MarshalJSON(params)
if err != nil {
return fmt.Errorf("failed to marshal params: %w", err)
}

bz, _, err = cliCtx.QueryWithData(route, bz)
if err != nil {
return err
}

var resp types.SubspaceParamsResponse
if err := m.UnmarshalJSON(bz, &resp); err != nil {
return err
}

return cliCtx.PrintOutput(resp)
},
}

return flags.GetCommands(cmd)[0]
}
47 changes: 47 additions & 0 deletions x/params/keeper/querier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper

import (
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
)

// NewQuerier returns a new querier handler for the x/params module.
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryParams:
return queryParams(ctx, req, k)

default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
}
}
}

func queryParams(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QuerySubspaceParams

if err := codec.Cdc.UnmarshalJSON(req.Data, &params); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

ss, ok := k.GetSubspace(params.Subspace)
if !ok {
return nil, sdkerrors.Wrap(proposal.ErrUnknownSubspace, params.Subspace)
}

rawValue := ss.GetRaw(ctx, []byte(params.Key))
resp := types.NewSubspaceParamsResponse(params.Subspace, params.Key, string(rawValue))

bz, err := codec.MarshalJSONIndent(codec.Cdc, resp)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

return bz, nil
}
40 changes: 36 additions & 4 deletions x/params/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gorilla/mux"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -17,6 +18,7 @@ import (
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
Expand Down Expand Up @@ -55,21 +57,38 @@ func (AppModuleBasic) GetQueryCmd(_ *codec.Codec) *cobra.Command { return nil }
// AppModule implements an application module for the distribution module.
type AppModule struct {
AppModuleBasic

keeper Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule() AppModule {
func NewAppModule(k Keeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: k,
}
}

//____________________________________________________________________________
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// AppModuleSimulation functions
func (am AppModule) NewHandler() sdk.Handler { return nil }

// InitGenesis performs a no-op.
func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}

func (AppModule) Route() string { return "" }

// GenerateGenesisState performs a no-op.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {}

// QuerierRoute returns the x/param module's querier route name.
func (AppModule) QuerierRoute() string { return QuerierRoute }

// NewQuerierHandler returns the x/params querier handler.
func (am AppModule) NewQuerierHandler() sdk.Querier {
return NewQuerier(am.keeper)
}

// ProposalContents returns all the params content functions used to
Expand All @@ -90,3 +109,16 @@ func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {}
func (am AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation {
return nil
}

// ExportGenesis performs a no-op.
func (am AppModule) ExportGenesis(_ sdk.Context, _ codec.JSONMarshaler) json.RawMessage {
return nil
}

// BeginBlock performs a no-op.
func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock performs a no-op.
func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}
9 changes: 9 additions & 0 deletions x/params/types/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package types

const (
// ModuleName defines the module name
ModuleName = "params"

// QuerierRoute defines the module's query routing key
QuerierRoute = ModuleName
)
35 changes: 35 additions & 0 deletions x/params/types/querier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package types

// Querier path constants
const (
QueryParams = "params"
)

// QuerySubspaceParams defines the params for querying module params by a given
// subspace and key.
type QuerySubspaceParams struct {
Subspace string
Key string
}

// SubspaceParamsResponse defines the response for quering parameters by subspace.
type SubspaceParamsResponse struct {
Subspace string
Key string
Value string
}

func NewQuerySubspaceParams(ss, key string) QuerySubspaceParams {
return QuerySubspaceParams{
Subspace: ss,
Key: key,
}
}

func NewSubspaceParamsResponse(ss, key, value string) SubspaceParamsResponse {
return SubspaceParamsResponse{
Subspace: ss,
Key: key,
Value: value,
}
}