From 4da4bb64df275aa2c037ab6c3bd53e750d764269 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Thu, 16 Apr 2020 12:22:58 -0400 Subject: [PATCH] Merge PR #6005: x/params: Raw Parameter Querying --- CHANGELOG.md | 1 + simapp/app.go | 3 +- x/params/alias.go | 31 +++++++++++------- x/params/client/cli/query.go | 63 ++++++++++++++++++++++++++++++++++++ x/params/keeper/querier.go | 47 +++++++++++++++++++++++++++ x/params/module.go | 40 ++++++++++++++++++++--- x/params/types/keys.go | 9 ++++++ x/params/types/querier.go | 35 ++++++++++++++++++++ 8 files changed, 212 insertions(+), 17 deletions(-) create mode 100644 x/params/client/cli/query.go create mode 100644 x/params/keeper/querier.go create mode 100644 x/params/types/keys.go create mode 100644 x/params/types/querier.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 932b3f28a17d..ac443ae1902d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/simapp/app.go b/simapp/app.go index c4f7eeb2018a..316ee412a22b 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -278,6 +278,7 @@ func NewSimApp( upgrade.NewAppModule(app.UpgradeKeeper), evidence.NewAppModule(app.EvidenceKeeper), ibc.NewAppModule(app.IBCKeeper), + params.NewAppModule(app.ParamsKeeper), transferModule, ) @@ -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() diff --git a/x/params/alias.go b/x/params/alias.go index 74b700f7d19d..9649567b3eec 100644 --- a/x/params/alias.go +++ b/x/params/alias.go @@ -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 ) diff --git a/x/params/client/cli/query.go b/x/params/client/cli/query.go new file mode 100644 index 000000000000..39a7390d04dc --- /dev/null +++ b/x/params/client/cli/query.go @@ -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] +} diff --git a/x/params/keeper/querier.go b/x/params/keeper/querier.go new file mode 100644 index 000000000000..a898bacc6d42 --- /dev/null +++ b/x/params/keeper/querier.go @@ -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, ¶ms); 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 +} diff --git a/x/params/module.go b/x/params/module.go index 0aca58729456..c2a6f5860e5a 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -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" @@ -17,6 +18,7 @@ import ( ) var ( + _ module.AppModule = AppModule{} _ module.AppModuleBasic = AppModuleBasic{} _ module.AppModuleSimulation = AppModule{} ) @@ -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 @@ -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{} +} diff --git a/x/params/types/keys.go b/x/params/types/keys.go new file mode 100644 index 000000000000..25df79848dae --- /dev/null +++ b/x/params/types/keys.go @@ -0,0 +1,9 @@ +package types + +const ( + // ModuleName defines the module name + ModuleName = "params" + + // QuerierRoute defines the module's query routing key + QuerierRoute = ModuleName +) diff --git a/x/params/types/querier.go b/x/params/types/querier.go new file mode 100644 index 000000000000..afcc773ff24e --- /dev/null +++ b/x/params/types/querier.go @@ -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, + } +}