Minor Code Cleanup - gRPC queries (#6862)

* add missing RegisterQueryService

* Update generated proto files

* Update grpc tests for auth,bank

* Make format

* fix godoc

* Address suggestions

* Update godoc
This commit is contained in:
SaReN 2020-07-28 18:23:35 +05:30 committed by GitHub
parent 282afcdb12
commit 919e906866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 67 additions and 72 deletions

View File

@ -186,7 +186,7 @@ func (app *BaseApp) MountKVStores(keys map[string]*sdk.KVStoreKey) {
}
}
// MountTransientStores mounts all IAVL or DB stores to the provided keys in
// MountTransientStores mounts all transient stores to the provided keys in
// the BaseApp multistore.
func (app *BaseApp) MountTransientStores(keys map[string]*sdk.TransientStoreKey) {
for _, key := range keys {

View File

@ -5,7 +5,7 @@ import (
"io"
"path/filepath"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
cryptokeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
)

View File

@ -1,7 +1,7 @@
package hd
import (
"github.com/cosmos/go-bip39"
bip39 "github.com/cosmos/go-bip39"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"
)

View File

@ -12,7 +12,7 @@ import (
"strings"
"github.com/99designs/keyring"
"github.com/cosmos/go-bip39"
bip39 "github.com/cosmos/go-bip39"
"github.com/pkg/errors"
"github.com/tendermint/crypto/bcrypt"
tmcrypto "github.com/tendermint/tendermint/crypto"

View File

@ -6,7 +6,7 @@ import (
"testing"
"github.com/99designs/keyring"
"github.com/cosmos/go-bip39"
bip39 "github.com/cosmos/go-bip39"
"github.com/stretchr/testify/require"
tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"

View File

@ -1,7 +1,7 @@
package multisig
import (
"github.com/tendermint/go-amino"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"

View File

@ -6,7 +6,7 @@ import (
"fmt"
"time"
"github.com/armon/go-metrics"
metrics "github.com/armon/go-metrics"
metricsprom "github.com/armon/go-metrics/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"

View File

@ -6,7 +6,7 @@ import (
"testing"
"time"
"github.com/armon/go-metrics"
metrics "github.com/armon/go-metrics"
"github.com/prometheus/common/expfmt"
"github.com/stretchr/testify/require"
)

View File

@ -3,7 +3,7 @@ package telemetry
import (
"time"
"github.com/armon/go-metrics"
metrics "github.com/armon/go-metrics"
)
// Common metric key constants

View File

@ -1,7 +1,7 @@
package testdata
import (
"github.com/tendermint/go-amino"
amino "github.com/tendermint/go-amino"
"github.com/cosmos/cosmos-sdk/codec/types"
)

View File

@ -17,17 +17,17 @@ import (
var _ types.QueryServer = AccountKeeper{}
// Account returns account details based on address
func (k AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
if req.Address.Empty() {
return nil, status.Errorf(codes.InvalidArgument, "invalid request")
return nil, status.Error(codes.InvalidArgument, "Address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(c)
account := k.GetAccount(ctx, req.Address)
account := ak.GetAccount(ctx, req.Address)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
}
@ -41,12 +41,12 @@ func (k AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest
}
// Params returns parameters of auth module
func (k AccountKeeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (ak AccountKeeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
params := ak.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}

View File

@ -183,15 +183,15 @@ func (ak AccountKeeper) decodeAccount(bz []byte) types.AccountI {
return acc
}
// MarshalEvidence marshals an Evidence interface. If the given type implements
// MarshalAccount marshals an Account interface. If the given type implements
// the Marshaler interface, it is treated as a Proto-defined message and
// serialized that way. Otherwise, it falls back on the internal Amino codec.
func (ak AccountKeeper) MarshalAccount(accountI types.AccountI) ([]byte, error) {
return codec.MarshalAny(ak.cdc, accountI)
}
// UnmarshalEvidence returns an Evidence interface from raw encoded evidence
// bytes of a Proto-based Evidence type. An error is returned upon decoding
// UnmarshalAccount returns an Account interface from raw encoded account
// bytes of a Proto-based Account type. An error is returned upon decoding
// failure.
func (ak AccountKeeper) UnmarshalAccount(bz []byte) (types.AccountI, error) {
var acc types.AccountI

View File

@ -4,7 +4,7 @@ import (
"fmt"
"strings"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)
// Periods stores all vesting periods passed as part of a PeriodicVestingAccount

View File

@ -4,13 +4,14 @@ import (
"errors"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec/legacy"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
"github.com/tendermint/tendermint/crypto"
"gopkg.in/yaml.v2"
)
// Compile-time type assertions

View File

@ -1,7 +1,7 @@
package bank
import (
"github.com/armon/go-metrics"
metrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

View File

@ -17,15 +17,15 @@ var _ types.QueryServer = BaseKeeper{}
// Balance implements the Query/Balance gRPC method
func (q BaseKeeper) Balance(c context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if len(req.Address) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "invalid address")
if req.Address.Empty() {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
if req.Denom == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid denom")
return nil, status.Error(codes.InvalidArgument, "invalid denom")
}
ctx := sdk.UnwrapSDKContext(c)
@ -37,12 +37,12 @@ func (q BaseKeeper) Balance(c context.Context, req *types.QueryBalanceRequest) (
// AllBalances implements the Query/AllBalances gRPC method
func (q BaseKeeper) AllBalances(c context.Context, req *types.QueryAllBalancesRequest) (*types.QueryAllBalancesResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
return nil, status.Error(codes.InvalidArgument, "empty request")
}
addr := req.Address
if len(addr) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "invalid address")
if addr.Empty() {
return nil, status.Errorf(codes.InvalidArgument, "address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(c)
@ -80,11 +80,11 @@ func (q BaseKeeper) TotalSupply(c context.Context, _ *types.QueryTotalSupplyRequ
// SupplyOf implements the Query/SupplyOf gRPC method
func (q BaseKeeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest) (*types.QuerySupplyOfResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.Denom == "" {
return nil, status.Errorf(codes.InvalidArgument, "invalid denom")
return nil, status.Error(codes.InvalidArgument, "invalid denom")
}
ctx := sdk.UnwrapSDKContext(c)

View File

@ -5,20 +5,15 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
func (suite *IntegrationTestSuite) TestQueryBalance() {
app, ctx := suite.app, suite.ctx
app, ctx, queryClient := suite.app, suite.ctx, suite.queryClient
_, _, addr := testdata.KeyTestPubAddr()
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
queryClient := types.NewQueryClient(queryHelper)
_, err := queryClient.Balance(gocontext.Background(), &types.QueryBalanceRequest{})
suite.Require().Error(err)
@ -44,13 +39,8 @@ func (suite *IntegrationTestSuite) TestQueryBalance() {
}
func (suite *IntegrationTestSuite) TestQueryAllBalances() {
app, ctx := suite.app, suite.ctx
app, ctx, queryClient := suite.app, suite.ctx, suite.queryClient
_, _, addr := testdata.KeyTestPubAddr()
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
queryClient := types.NewQueryClient(queryHelper)
_, err := queryClient.AllBalances(gocontext.Background(), &types.QueryAllBalancesRequest{})
suite.Require().Error(err)
@ -93,14 +83,10 @@ func (suite *IntegrationTestSuite) TestQueryAllBalances() {
}
func (suite *IntegrationTestSuite) TestQueryTotalSupply() {
app, ctx := suite.app, suite.ctx
app, ctx, queryClient := suite.app, suite.ctx, suite.queryClient
expectedTotalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin("test", 400000000)))
app.BankKeeper.SetSupply(ctx, expectedTotalSupply)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
queryClient := types.NewQueryClient(queryHelper)
res, err := queryClient.TotalSupply(gocontext.Background(), &types.QueryTotalSupplyRequest{})
suite.Require().NoError(err)
suite.Require().NotNil(res)
@ -109,17 +95,13 @@ func (suite *IntegrationTestSuite) TestQueryTotalSupply() {
}
func (suite *IntegrationTestSuite) TestQueryTotalSupplyOf() {
app, ctx := suite.app, suite.ctx
app, ctx, queryClient := suite.app, suite.ctx, suite.queryClient
test1Supply := sdk.NewInt64Coin("test1", 4000000)
test2Supply := sdk.NewInt64Coin("test2", 700000000)
expectedTotalSupply := types.NewSupply(sdk.NewCoins(test1Supply, test2Supply))
app.BankKeeper.SetSupply(ctx, expectedTotalSupply)
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
queryClient := types.NewQueryClient(queryHelper)
_, err := queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{})
suite.Require().Error(err)

View File

@ -9,6 +9,7 @@ import (
tmkv "github.com/tendermint/tendermint/libs/kv"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
@ -60,8 +61,9 @@ func getCoinsByName(ctx sdk.Context, bk keeper.Keeper, ak types.AccountKeeper, m
type IntegrationTestSuite struct {
suite.Suite
app *simapp.SimApp
ctx sdk.Context
app *simapp.SimApp
ctx sdk.Context
queryClient types.QueryClient
}
func (suite *IntegrationTestSuite) SetupTest() {
@ -71,8 +73,13 @@ func (suite *IntegrationTestSuite) SetupTest() {
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams())
app.BankKeeper.SetParams(ctx, types.DefaultParams())
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
queryClient := types.NewQueryClient(queryHelper)
suite.app = app
suite.ctx = ctx
suite.queryClient = queryClient
}
func (suite *IntegrationTestSuite) TestSupply() {

View File

@ -4,7 +4,7 @@ import (
"fmt"
"sort"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

View File

@ -1,7 +1,7 @@
package distribution
import (
"github.com/armon/go-metrics"
metrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

View File

@ -3,7 +3,7 @@ package types
import (
"fmt"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

View File

@ -144,7 +144,9 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc codec.JSONMarshaler) s
// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
}
// RegisterInvariants registers the evidence module's invariants.
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {}

View File

@ -4,13 +4,13 @@ import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
)
// Evidence type constants

View File

@ -4,7 +4,7 @@ import (
"fmt"
"strconv"
"github.com/armon/go-metrics"
metrics "github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

View File

@ -3,7 +3,7 @@ package types
import (
"fmt"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)

View File

@ -4,7 +4,7 @@ import (
"fmt"
"time"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

View File

@ -1,7 +1,7 @@
package types
import (
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)

View File

@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)

View File

@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"strings"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

View File

@ -3,7 +3,7 @@ package staking
import (
"time"
"github.com/armon/go-metrics"
metrics "github.com/armon/go-metrics"
gogotypes "github.com/gogo/protobuf/types"
tmstrings "github.com/tendermint/tendermint/libs/strings"
tmtypes "github.com/tendermint/tendermint/types"

View File

@ -129,7 +129,10 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc codec.JSONMarshaler) s
// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}
func (am AppModule) RegisterQueryService(server grpc.Server) {
querier := keeper.Querier{Keeper: am.keeper}
types.RegisterQueryServer(server, querier)
}
// InitGenesis performs genesis initialization for the staking module. It returns
// no validator updates.