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 all 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
}
9 changes: 6 additions & 3 deletions x/auth/types/account_retriever_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types_test

import (
"errors"
"fmt"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -23,19 +24,21 @@ func TestAccountRetriever(t *testing.T) {
bs, err := appCodec.MarshalJSON(types.NewQueryAccountParams(addr))
require.NoError(t, err)

mockNodeQuerier.EXPECT().QueryWithData(gomock.Eq("custom/acc/account"),
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount)

mockNodeQuerier.EXPECT().QueryWithData(gomock.Eq(route),
gomock.Eq(bs)).Return(nil, int64(0), errFoo).Times(1)
_, err = accRetr.GetAccount(addr)
require.Error(t, err)

mockNodeQuerier.EXPECT().QueryWithData(gomock.Eq("custom/acc/account"),
mockNodeQuerier.EXPECT().QueryWithData(gomock.Eq(route),
gomock.Eq(bs)).Return(nil, int64(0), errFoo).Times(1)
n, s, err := accRetr.GetAccountNumberSequence(addr)
require.Error(t, err)
require.Equal(t, uint64(0), n)
require.Equal(t, uint64(0), s)

mockNodeQuerier.EXPECT().QueryWithData(gomock.Eq("custom/acc/account"),
mockNodeQuerier.EXPECT().QueryWithData(gomock.Eq(route),
gomock.Eq(bs)).Return(nil, int64(0), errFoo).Times(1)
require.Error(t, accRetr.EnsureExists(addr))
}
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 exported.Evidence, s sdk.AccAddress) simappcodec.MsgSubmitEvidence {
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
11 changes: 9 additions & 2 deletions x/evidence/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import (

simappcodec "github.com/cosmos/cosmos-sdk/simapp/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/types"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
)

func testMsgSubmitEvidence(t *testing.T, e exported.Evidence, s sdk.AccAddress) simappcodec.MsgSubmitEvidence {
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 +34,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 +44,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