Skip to content

Commit

Permalink
Add raw params querier (#591)
Browse files Browse the repository at this point in the history
* add raw-params cmd

* add querier test

* update changelog
  • Loading branch information
rhuairahrighairidh authored Jun 23, 2020
1 parent 2ea7545 commit b539820
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

[\#591](https://github.com/Kava-Labs/kava/pull/591) Add a `raw-params` cli method to query raw parameter values for use in manual verification of gov proposals.

[\#596](https://github.com/Kava-Labs/kava/pull/596) Add REST client and CLI query to get module account information for the CDP module

[\#590](https://github.com/Kava-Labs/kava/pull/590) Add CLI query to return kavadist module account balance
Expand Down
37 changes: 36 additions & 1 deletion x/committee/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
GetCmdQueryVotes(queryRoute, cdc),
// other
GetCmdQueryProposer(queryRoute, cdc),
GetCmdQueryTally(queryRoute, cdc))...)
GetCmdQueryTally(queryRoute, cdc),
GetCmdQueryRawParams(queryRoute, cdc))...)

return queryCmd
}
Expand Down Expand Up @@ -292,3 +293,37 @@ func GetCmdQueryProposer(queryRoute string, cdc *codec.Codec) *cobra.Command {
},
}
}

func GetCmdQueryRawParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "raw-params [subspace] [key]",
Args: cobra.ExactArgs(2),
Short: "Query raw parameter values from any module.",
Long: "Query the byte value of any module's parameters. Useful in debugging and verifying governance proposals.",
Example: fmt.Sprintf("%s query %s raw-params cdp CollateralParams", version.ClientName, types.ModuleName),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

// Prepare params for querier
bz, err := cdc.MarshalJSON(types.NewQueryRawParamsParams(args[0], args[1]))
if err != nil {
return err
}

// Query
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryRawParams), bz)
if err != nil {
return err
}

// Decode and print results
rawParams := []byte{}
err = cdc.UnmarshalJSON(res, &rawParams)
if err != nil {
return err
}
// raw params are encoded raw json bytes, so should be ok to print out as a string
return cliCtx.PrintOutput(string(rawParams))
},
}
}
27 changes: 27 additions & 0 deletions x/committee/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
return queryVote(ctx, path[1:], req, keeper)
case types.QueryTally:
return queryTally(ctx, path[1:], req, keeper)
case types.QueryRawParams:
return queryRawParams(ctx, path[1:], req, keeper)

default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
Expand Down Expand Up @@ -172,3 +174,28 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
}
return bz, nil
}

// ------------------------------------------
// Raw Params
// ------------------------------------------

func queryRawParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
var params types.QueryRawParamsParams
err := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

subspace, found := keeper.ParamKeeper.GetSubspace(params.Subspace)
if !found {
return nil, sdkerrors.Wrapf(types.ErrUnknownSubspace, "subspace: %s", params.Subspace)
}
rawParams := subspace.GetRaw(ctx, []byte(params.Key))

// encode the raw params as json, which converts them to a base64 string
bz, err := codec.MarshalJSONIndent(keeper.cdc, rawParams)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
52 changes: 52 additions & 0 deletions x/committee/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/params"

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

Expand Down Expand Up @@ -238,6 +239,57 @@ func (suite *QuerierTestSuite) TestQueryTally() {
// Check
suite.Equal(int64(len(suite.votes[propID])), tally)
}

type TestSubParam struct {
Some string
Test sdk.Dec
Params []types.Vote
}
type TestParams struct {
TestKey TestSubParam
}

const paramKey = "TestKey"

func (p *TestParams) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
params.NewParamSetPair([]byte(paramKey), &p.TestKey, func(interface{}) error { return nil }),
}
}
func (suite *QuerierTestSuite) TestQueryRawParams() {
ctx := suite.ctx.WithIsCheckTx(false) // ?

// Create a new param subspace to avoid adding dependency to another module. Set a test param value.
subspaceName := "test"
subspace := suite.app.GetParamsKeeper().Subspace(subspaceName)
subspace = subspace.WithKeyTable(params.NewKeyTable().RegisterParamSet(&TestParams{}))

paramValue := TestSubParam{
Some: "test",
Test: d("1000000000000.000000000000000001"),
Params: []types.Vote{{1, suite.addresses[0]}, {12, suite.addresses[1]}},
}
subspace.Set(ctx, []byte(paramKey), paramValue)

// Set up request query
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryRawParams}, "/"),
Data: suite.cdc.MustMarshalJSON(types.NewQueryRawParamsParams(subspaceName, paramKey)),
}

// Execute query
bz, err := suite.querier(ctx, []string{types.QueryRawParams}, query)
suite.NoError(err)
suite.NotNil(bz)

// Unmarshal the bytes
var returnedParamValue []byte
suite.NoError(suite.cdc.UnmarshalJSON(bz, &returnedParamValue))

// Check
suite.Equal(suite.cdc.MustMarshalJSON(paramValue), returnedParamValue)
}

func TestQuerierTestSuite(t *testing.T) {
suite.Run(t, new(QuerierTestSuite))
}
1 change: 1 addition & 0 deletions x/committee/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ var (
ErrUnknownVote = sdkerrors.Register(ModuleName, 7, "vote not found")
ErrInvalidGenesis = sdkerrors.Register(ModuleName, 8, "invalid genesis")
ErrNoProposalHandlerExists = sdkerrors.Register(ModuleName, 9, "pubproposal has no corresponding handler")
ErrUnknownSubspace = sdkerrors.Register(ModuleName, 10, "subspace not found")
)
13 changes: 13 additions & 0 deletions x/committee/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
QueryVotes = "votes"
QueryVote = "vote"
QueryTally = "tally"
QueryRawParams = "raw_params"
)

type QueryCommitteeParams struct {
Expand Down Expand Up @@ -46,3 +47,15 @@ func NewQueryVoteParams(proposalID uint64, voter sdk.AccAddress) QueryVoteParams
Voter: voter,
}
}

type QueryRawParamsParams struct {
Subspace string
Key string
}

func NewQueryRawParamsParams(subspace, key string) QueryRawParamsParams {
return QueryRawParamsParams{
Subspace: subspace,
Key: key,
}
}

0 comments on commit b539820

Please sign in to comment.