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

Add Param Querying to x/auth #5702

Merged
merged 6 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (modules) [\#5572](https://github.com/cosmos/cosmos-sdk/pull/5572) The `/bank/balances/{address}` endpoint now returns all account
balances or a single balance by denom when the `denom` query parameter is present.
* (client) [\#5640](https://github.com/cosmos/cosmos-sdk/pull/5640) The rest server endpoint `/swagger-ui/` is replaced by
´/´.
* (client) [\#5640](https://github.com/cosmos/cosmos-sdk/pull/5640) The rest server endpoint `/swagger-ui/` is replaced by ´/´.
* (x/auth) [\#5702](https://github.com/cosmos/cosmos-sdk/pull/5702) The `x/auth` querier route has changed from `"acc"` to `"auth"`.

### API Breaking Changes

Expand Down Expand Up @@ -133,6 +133,7 @@ Buffers for state serialization instead of Amino.

### Improvements

* (x/auth) [\#5702](https://github.com/cosmos/cosmos-sdk/pull/5702) Add parameter querying support for `x/auth`.
* (types) [\#5581](https://github.com/cosmos/cosmos-sdk/pull/5581) Add convenience functions {,Must}Bech32ifyAddressBytes.
* (staking) [\#5584](https://github.com/cosmos/cosmos-sdk/pull/5584) Add util function `ToTmValidator` that converts a `staking.Validator` type to `*tmtypes.Validator`.
* (client) [\#5585](https://github.com/cosmos/cosmos-sdk/pull/5585) IBC additions:
Expand Down
8 changes: 5 additions & 3 deletions simapp/codec/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
var _ eviexported.MsgSubmitEvidence = MsgSubmitEvidence{}

// NewMsgSubmitEvidence returns a new MsgSubmitEvidence.
func NewMsgSubmitEvidence(evidenceI eviexported.Evidence, s sdk.AccAddress) MsgSubmitEvidence {
func NewMsgSubmitEvidence(evidenceI eviexported.Evidence, s sdk.AccAddress) (MsgSubmitEvidence, error) {
e := &Evidence{}
e.SetEvidence(evidenceI)
if err := e.SetEvidence(evidenceI); err != nil {
return MsgSubmitEvidence{}, err
}

return MsgSubmitEvidence{Evidence: e, MsgSubmitEvidenceBase: evidence.NewMsgSubmitEvidenceBase(s)}
return MsgSubmitEvidence{Evidence: e, MsgSubmitEvidenceBase: evidence.NewMsgSubmitEvidenceBase(s)}, nil
}

// ValidateBasic performs basic (non-state-dependant) validation on a
Expand Down
1 change: 1 addition & 0 deletions x/auth/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
DefaultSigVerifyCostED25519 = types.DefaultSigVerifyCostED25519
DefaultSigVerifyCostSecp256k1 = types.DefaultSigVerifyCostSecp256k1
QueryAccount = types.QueryAccount
QueryParams = types.QueryParams
)

var (
Expand Down
36 changes: 35 additions & 1 deletion x/auth/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,45 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(GetAccountCmd(cdc))
cmd.AddCommand(
flags.GetCommands(
GetAccountCmd(cdc),
QueryParamsCmd(cdc),
)...,
)

return cmd
}

// QueryParamsCmd returns the command handler for evidence parameter querying.
func QueryParamsCmd(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Short: "Query the current auth parameters",
Args: cobra.NoArgs,
Long: strings.TrimSpace(`Query the current auth parameters:

$ <appcli> query auth params
`),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}

var params types.Params
if err := cdc.UnmarshalJSON(res, &params); err != nil {
return fmt.Errorf("failed to unmarshal params: %w", err)
}

return cliCtx.PrintOutput(params)
},
}
}

// GetAccountCmd returns a query account that will display the state of the
// account at a given address.
func GetAccountCmd(cdc *codec.Codec) *cobra.Command {
Expand Down
19 changes: 19 additions & 0 deletions x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,22 @@ func QueryTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
rest.PostProcessResponseBare(w, cliCtx, output)
}
}

func queryParamsHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
12 changes: 11 additions & 1 deletion x/auth/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
)

// REST query and parameter values
const (
MethodGet = "GET"
)

// RegisterRoutes registers the auth module REST routes.
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) {
r.HandleFunc(
"/auth/accounts/{address}", QueryAccountRequestHandlerFn(storeName, cliCtx),
).Methods("GET")
).Methods(MethodGet)

r.HandleFunc(
"/auth/params",
queryParamsHandler(cliCtx),
).Methods(MethodGet)
}

// RegisterTxRoutes registers all transaction routes on the provided router.
Expand Down
27 changes: 21 additions & 6 deletions x/auth/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,47 @@ import (
)

// NewQuerier creates a querier for auth REST endpoints
func NewQuerier(keeper AccountKeeper) sdk.Querier {
func NewQuerier(k AccountKeeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryAccount:
return queryAccount(ctx, req, keeper)
return queryAccount(ctx, req, k)

case types.QueryParams:
return queryParams(ctx, k)

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

func queryAccount(ctx sdk.Context, req abci.RequestQuery, keeper AccountKeeper) ([]byte, error) {
func queryAccount(ctx sdk.Context, req abci.RequestQuery, k AccountKeeper) ([]byte, error) {
var params types.QueryAccountParams
if err := keeper.cdc.UnmarshalJSON(req.Data, &params); err != nil {
if err := k.cdc.UnmarshalJSON(req.Data, &params); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

account := keeper.GetAccount(ctx, params.Address)
account := k.GetAccount(ctx, params.Address)
if account == nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", params.Address)
}

bz, err := codec.MarshalJSONIndent(keeper.cdc, account)
bz, err := codec.MarshalJSONIndent(k.cdc, account)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

return bz, nil
}

func queryParams(ctx sdk.Context, k AccountKeeper) ([]byte, error) {
params := k.GetParams(ctx)

res, err := codec.MarshalJSONIndent(k.cdc, params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

return res, nil
}
4 changes: 2 additions & 2 deletions x/auth/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
// FeeCollectorName the root string for the fee collector account address
FeeCollectorName = "fee_collector"

// QuerierRoute is the querier route for acc
QuerierRoute = StoreKey
// QuerierRoute is the querier route for auth
QuerierRoute = ModuleName
)

var (
Expand Down
1 change: 1 addition & 0 deletions x/auth/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
// query endpoints supported by the auth Querier
const (
QueryAccount = "account"
QueryParams = "params"
)

// QueryAccountParams defines the params for querying accounts.
Expand Down
13 changes: 11 additions & 2 deletions x/evidence/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
Expand All @@ -24,6 +25,12 @@ type HandlerTestSuite struct {
app *simapp.SimApp
}

func testMsgSubmitEvidence(r *require.Assertions, e *types.Equivocation, s sdk.AccAddress) simappcodec.MsgSubmitEvidence {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
msg, err := simappcodec.NewMsgSubmitEvidence(e, s)
r.NoError(err)
return msg
}

func testEquivocationHandler(k interface{}) types.Handler {
return func(ctx sdk.Context, e exported.Evidence) error {
if err := e.ValidateBasic(); err != nil {
Expand Down Expand Up @@ -70,7 +77,8 @@ func (suite *HandlerTestSuite) TestMsgSubmitEvidence() {
expectErr bool
}{
{
simappcodec.NewMsgSubmitEvidence(
testMsgSubmitEvidence(
suite.Require(),
&types.Equivocation{
Height: 11,
Time: time.Now().UTC(),
Expand All @@ -82,7 +90,8 @@ func (suite *HandlerTestSuite) TestMsgSubmitEvidence() {
false,
},
{
simappcodec.NewMsgSubmitEvidence(
testMsgSubmitEvidence(
suite.Require(),
&types.Equivocation{
Height: 10,
Time: time.Now().UTC(),
Expand Down
10 changes: 8 additions & 2 deletions x/evidence/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
)

func testMsgSubmitEvidence(t *testing.T, e *types.Equivocation, s sdk.AccAddress) simappcodec.MsgSubmitEvidence {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
msg, err := simappcodec.NewMsgSubmitEvidence(e, s)
require.NoError(t, err)
return msg
}

func TestMsgSubmitEvidence(t *testing.T) {
pk := ed25519.GenPrivKey()
submitter := sdk.AccAddress("test")
Expand All @@ -27,7 +33,7 @@ func TestMsgSubmitEvidence(t *testing.T) {
false,
},
{
simappcodec.NewMsgSubmitEvidence(&types.Equivocation{
testMsgSubmitEvidence(t, &types.Equivocation{
Height: 0,
Power: 100,
Time: time.Now().UTC(),
Expand All @@ -37,7 +43,7 @@ func TestMsgSubmitEvidence(t *testing.T) {
true,
},
{
simappcodec.NewMsgSubmitEvidence(&types.Equivocation{
testMsgSubmitEvidence(t, &types.Equivocation{
Height: 10,
Power: 100,
Time: time.Now().UTC(),
Expand Down