-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #6005: x/params: Raw Parameter Querying
- Loading branch information
1 parent
09a55f6
commit 4da4bb6
Showing
8 changed files
with
212 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¶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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |