Skip to content

Commit

Permalink
x/staking: gRPC query Service (#6490)
Browse files Browse the repository at this point in the history
* Add types for staking grpc

* Update module.go

* Update staking query types

* Add grpc query methods

* Add delegation response to proto

* Add queriers for delegations

* Add queriers for unbonding

* Add queriers for redelegations

* Add cases for redelegations

* Add test for grpc validators

* Update staking types to proto

* Update staking query proto

* Add tests for grpc

* Add tests for grpc pool, parameters

* Fix lint issues

* Add grpc redelegation tests

* Add more tests

* Add docs for query proto

* Add docs for query types

* Modify redel querier

* Add debugging statements

* Revert debugging

* Fix proto lint errors

* Add wrapper for keeper

* Embed keeper in querier

* Add more tests

* Add tests for validator unbondings

* Add redel tests

* fix queryRedelegationsFromSrcValidator

* Fix Redelegation tests

* update godoc

* Update args

* Update tests with suite

* Fix lint

* Remove redundant types

* Refactor tests

* fix test

* refactor query proto

* Fix tests

* address review comments

* lint staking proto

* add godoc

* Update tests to table driven tests

* add debugging

* Fix grpc tests

* address comments

* address whitespace suggestions

* Add more tests

* add tests for invalid redels

* update error messages

* address review suggestions

* add tests

* move suite to keeper_test

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 14, 2020
1 parent b9f86dd commit 5656e86
Show file tree
Hide file tree
Showing 19 changed files with 10,156 additions and 286 deletions.
228 changes: 228 additions & 0 deletions proto/cosmos/staking/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
syntax = "proto3";
package cosmos.staking;

import "cosmos/query/pagination.proto";
import "gogoproto/gogo.proto";
import "cosmos/staking/staking.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/staking/types";

// Query defines the gRPC querier service
service Query {
// Validators queries all validators that match the given status
rpc Validators (QueryValidatorsRequest) returns (QueryValidatorsResponse) {}

// Validator queries validator info for given validator addr
rpc Validator (QueryValidatorRequest) returns (QueryValidatorResponse) {}

// ValidatorDelegations queries delegate info for given validator
rpc ValidatorDelegations (QueryValidatorDelegationsRequest) returns (QueryValidatorDelegationsResponse) {}

// ValidatorUnbondingDelegations queries unbonding delegations of a validator
rpc ValidatorUnbondingDelegations (QueryValidatorUnbondingDelegationsRequest) returns (QueryValidatorUnbondingDelegationsResponse) {}

// Delegation queries delegate info for given validator delegator pair
rpc Delegation (QueryDelegationRequest) returns (QueryDelegationResponse) {}

// UnbondingDelegation queries unbonding info for give validator delegator pair
rpc UnbondingDelegation (QueryUnbondingDelegationRequest) returns (QueryUnbondingDelegationResponse) {}

// DelegatorDelegations queries all delegations of a give delegator address
rpc DelegatorDelegations (QueryDelegatorDelegationsRequest) returns (QueryDelegatorDelegationsResponse) {}

// DelegatorUnbondingDelegations queries all unbonding delegations of a give delegator address
rpc DelegatorUnbondingDelegations (QueryDelegatorUnbondingDelegationsRequest) returns (QueryDelegatorUnbondingDelegationsResponse) {}

// Redelegations queries redelegations of given address
rpc Redelegations (QueryRedelegationsRequest) returns (QueryRedelegationsResponse) {}

// DelegatorValidators queries all validator info for given delegator address
rpc DelegatorValidators (QueryDelegatorValidatorsRequest) returns (QueryDelegatorValidatorsResponse) {}

// DelegatorValidator queries validator info for given delegator validator pair
rpc DelegatorValidator (QueryDelegatorValidatorRequest) returns (QueryDelegatorValidatorResponse) {}

// HistoricalInfo queries the historical info for given height
rpc HistoricalInfo (QueryHistoricalInfoRequest) returns (QueryHistoricalInfoResponse) {}

// Pool queries the pool info
rpc Pool (QueryPoolRequest) returns (QueryPoolResponse) {}

// Parameters queries the staking parameters
rpc Params (QueryParamsRequest) returns (QueryParamsResponse) {}
}

// QueryValidatorsRequest is request type for Query/Validators RPC method
message QueryValidatorsRequest{
string status = 1;

cosmos.query.PageRequest req = 2;
}

// QueryValidatorsResponse is response type for the Query/Validators RPC method
message QueryValidatorsResponse {
repeated cosmos.staking.Validator validators = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

// QueryValidatorRequest is response type for the Query/Validator RPC method
message QueryValidatorRequest {
bytes validator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];
}

// QueryValidatorResponse is response type for the Query/Validator RPC method
message QueryValidatorResponse {
Validator validator = 1 [(gogoproto.nullable) = false];
}

// QueryValidatorDelegationsRequest is request type for the Query/ValidatorDelegations RPC method
message QueryValidatorDelegationsRequest {
bytes validator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];

cosmos.query.PageRequest req = 2;
}

// QueryValidatorDelegationsRequest is response type for the Query/ValidatorDelegations RPC method
message QueryValidatorDelegationsResponse {
repeated DelegationResponse delegation_responses = 1 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "DelegationResponses"];

cosmos.query.PageResponse res = 2;
}

// QueryValidatorUnbondingDelegationsRequest is required type for the Query/ValidatorUnbondingDelegations RPC method
message QueryValidatorUnbondingDelegationsRequest {
bytes validator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];

cosmos.query.PageRequest req = 2;
}

// QueryValidatorUnbondingDelegationsResponse is response type for the Query/ValidatorUnbondingDelegations RPC method
message QueryValidatorUnbondingDelegationsResponse {
repeated UnbondingDelegation unbonding_responses = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

// QueryDelegationRequest is request type for the Query/Delegation RPC method
message QueryDelegationRequest {
bytes delegator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

bytes validator_addr = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];
}

// QueryDelegationResponse is response type for the Query/Delegation RPC method
message QueryDelegationResponse {
DelegationResponse delegation_response = 1 [(gogoproto.casttype) = "DelegationResponse"];
}

// QueryUnbondingDelegationRequest is request type for the Query/UnbondingDelegation RPC method
message QueryUnbondingDelegationRequest {
bytes delegator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

bytes validator_addr = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];
}

// QueryDelegationResponse is response type for the Query/UnbondingDelegation RPC method
message QueryUnbondingDelegationResponse {
UnbondingDelegation unbond =1 [(gogoproto.nullable) = false];
}

// QueryDelegatorDelegationsRequest is request type for the Query/DelegatorDelegations RPC method
message QueryDelegatorDelegationsRequest {
bytes delegator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

cosmos.query.PageRequest req = 2;
}

// QueryDelegatorDelegationsResponse is response type for the Query/DelegatorDelegations RPC method
message QueryDelegatorDelegationsResponse {
repeated DelegationResponse delegation_responses = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

// QueryDelegatorUnbondingDelegationsRequest is request type for the Query/DelegatorUnbondingDelegations RPC method
message QueryDelegatorUnbondingDelegationsRequest {
bytes delegator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

cosmos.query.PageRequest req = 2;
}
// QueryUnbondingDelegatorDelegationsResponse is response type for the Query/UnbondingDelegatorDelegations RPC method
message QueryDelegatorUnbondingDelegationsResponse {
repeated UnbondingDelegation unbonding_responses = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

// QueryRedelegationsRequest is request type for the Query/Redelegations RPC method
message QueryRedelegationsRequest {
bytes delegator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

bytes src_validator_addr = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];

bytes dst_validator_addr = 3 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];

cosmos.query.PageRequest req = 4;
}

// QueryRedelegationsResponse is response type for the Query/Redelegations RPC method
message QueryRedelegationsResponse {
repeated RedelegationResponse redelegation_responses = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

// QueryDelegatorValidatorsRequest is request type for the Query/DelegatorValidators RPC method
message QueryDelegatorValidatorsRequest {
bytes delegator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

cosmos.query.PageRequest req = 2;
}

// QueryDelegatorValidatorsResponse is response type for the Query/DelegatorValidators RPC method
message QueryDelegatorValidatorsResponse {
repeated Validator validators = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

// QueryDelegatorValidatorRequest is request type for the Query/DelegatorValidator RPC method
message QueryDelegatorValidatorRequest {
bytes delegator_addr = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

bytes validator_addr = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ValAddress"];
}

// QueryDelegatorValidatorResponse response type for the Query/DelegatorValidator RPC method
message QueryDelegatorValidatorResponse {
Validator validator = 1 [(gogoproto.nullable) = false];
}

// QueryHistoricalInfoRequest is request type for the Query/HistoricalInfo RPC method
message QueryHistoricalInfoRequest {
int64 height = 1;
}

// QueryHistoricalInfoResponse is response type for the Query/HistoricalInfo RPC method
message QueryHistoricalInfoResponse {
HistoricalInfo hist = 1;
}

// QueryPoolRequest is request type for the Query/Pool RPC method
message QueryPoolRequest { }

// QueryPoolResponse is response type for the Query/Pool RPC method
message QueryPoolResponse {
Pool pool = 1 [(gogoproto.nullable) = false];
}

// QueryParametersRequest is request type for the Query/Parameters RPC method
message QueryParamsRequest { }

// QueryParametersResponse is response type for the Query/Parameters RPC method
message QueryParamsResponse {
Params params = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}
48 changes: 48 additions & 0 deletions proto/cosmos/staking/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,51 @@ message Params {
uint32 historical_entries = 4 [(gogoproto.moretags) = "yaml:\"historical_entries\""];
string bond_denom = 5 [(gogoproto.moretags) = "yaml:\"bond_denom\""];
}

// DelegationResponse is equivalent to Delegation except that it contains a balance
// in addition to shares which is more suitable for client responses.
message DelegationResponse {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

Delegation delegation = 1 [(gogoproto.nullable) = false];

cosmos.Coin balance = 2 [(gogoproto.nullable) = false];
}

// RedelegationEntryResponse is equivalent to a RedelegationEntry except that it
// contains a balance in addition to shares which is more suitable for client
// responses.
message RedelegationEntryResponse {
option (gogoproto.equal) = true;

RedelegationEntry redelegation_entry = 1 [(gogoproto.nullable) = false];
string balance = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

// RedelegationResponse is equivalent to a Redelegation except that its entries
// contain a balance in addition to shares which is more suitable for client
// responses.
message RedelegationResponse {
option (gogoproto.equal) = true;

Redelegation redelegation = 1 [(gogoproto.nullable) = false];
repeated RedelegationEntryResponse entries = 2 [(gogoproto.nullable) = false];
}

// Pool - tracking bonded and not-bonded token supply of the bond denomination
message Pool {
option (gogoproto.description) = true;
option (gogoproto.equal) = true;
string not_bonded_tokens = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.jsontag) = "not_bonded_tokens",
(gogoproto.nullable) = false];
string bonded_tokens = 2 [(gogoproto.jsontag) = "bonded_tokens",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"bonded_tokens\""];
}

2 changes: 1 addition & 1 deletion x/slashing/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func signingInfoHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

params := types.NewQuerySigningInfoParams(sdk.ConsAddress(pk.Address()))
params := types.QuerySigningInfoRequest{ConsAddress: sdk.ConsAddress(pk.Address())}

bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func queryParams(ctx sdk.Context, k Keeper) ([]byte, error) {
}

func querySigningInfo(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
var params types.QuerySigningInfoParams
var params types.QuerySigningInfoRequest

err := types.ModuleCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
Expand Down
15 changes: 0 additions & 15 deletions x/slashing/types/querier.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// DONTCOVER

// Query endpoints supported by the slashing querier
Expand All @@ -13,17 +9,6 @@ const (
QuerySigningInfos = "signingInfos"
)

// QuerySigningInfoParams defines the params for the following queries:
// - 'custom/slashing/signingInfo'
type QuerySigningInfoParams struct {
ConsAddress sdk.ConsAddress
}

// NewQuerySigningInfoParams creates a new QuerySigningInfoParams instance
func NewQuerySigningInfoParams(consAddr sdk.ConsAddress) QuerySigningInfoParams {
return QuerySigningInfoParams{consAddr}
}

// QuerySigningInfosParams defines the params for the following queries:
// - 'custom/slashing/signingInfos'
type QuerySigningInfosParams struct {
Expand Down
2 changes: 1 addition & 1 deletion x/staking/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestCLICreateValidator(t *testing.T) {
// Query delegations to the validator
validatorDelegations := testutil.QueryStakingDelegationsTo(f, barVal)
require.Len(t, validatorDelegations, 1)
require.NotZero(t, validatorDelegations[0].Shares)
require.NotNil(t, validatorDelegations[0].Shares)

// Edit validator
// params to be changed in edit validator (NOTE: a validator can only change its commission once per day)
Expand Down
6 changes: 3 additions & 3 deletions x/staking/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ $ %s query staking delegation cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p cosm
return err
}

bz, err := cdc.MarshalJSON(types.NewQueryBondsParams(delAddr, valAddr))
bz, err := cdc.MarshalJSON(types.QueryDelegatorValidatorRequest{DelegatorAddr: delAddr, ValidatorAddr: valAddr})
if err != nil {
return err
}
Expand Down Expand Up @@ -414,7 +414,7 @@ $ %s query staking unbonding-delegation cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld7
return err
}

bz, err := cdc.MarshalJSON(types.NewQueryBondsParams(delAddr, valAddr))
bz, err := cdc.MarshalJSON(types.QueryDelegatorValidatorRequest{DelegatorAddr: delAddr, ValidatorAddr: valAddr})
if err != nil {
return err
}
Expand Down Expand Up @@ -619,7 +619,7 @@ $ %s query staking historical-info 5
return fmt.Errorf("height argument provided must be a non-negative-integer: %v", err)
}

bz, err := cdc.MarshalJSON(types.QueryHistoricalInfoParams{Height: height})
bz, err := cdc.MarshalJSON(types.QueryHistoricalInfoRequest{Height: height})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/staking/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func historicalInfoHandlerFn(clientCtx client.Context) http.HandlerFunc {
return
}

params := types.NewQueryHistoricalInfoParams(height)
params := types.QueryHistoricalInfoRequest{Height: height}

bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if rest.CheckInternalServerError(w, err) {
Expand Down
2 changes: 1 addition & 1 deletion x/staking/client/rest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func queryBonds(clientCtx client.Context, endpoint string) http.HandlerFunc {
return
}

params := types.NewQueryBondsParams(delegatorAddr, validatorAddr)
params := types.QueryDelegatorValidatorRequest{DelegatorAddr: delegatorAddr, ValidatorAddr: validatorAddr}

bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
Expand Down
Loading

0 comments on commit 5656e86

Please sign in to comment.