Merge PR #6005: x/params: Raw Parameter Querying

This commit is contained in:
Alexander Bezobchuk 2020-04-16 12:22:58 -04:00 committed by GitHub
parent 09a55f68b5
commit 4da4bb64df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 212 additions and 17 deletions

View File

@ -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

View File

@ -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()

View File

@ -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
)

View File

@ -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]
}

View File

@ -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
}

View File

@ -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{}
}

9
x/params/types/keys.go Normal file
View File

@ -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
x/params/types/querier.go Normal file
View File

@ -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,
}
}