Skip to content

Commit

Permalink
Merge pull request #41 from GeoDB-Limited/feature/add-query-validator
Browse files Browse the repository at this point in the history
add query validator by consaddr
  • Loading branch information
artemskriabin authored Feb 3, 2022
2 parents d90958f + 2b4890d commit 634f44c
Show file tree
Hide file tree
Showing 8 changed files with 631 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ vendor
**/target/
/pkg/owasm/res/*.wasm
#/docker-config/genesis.json
config.yaml
15 changes: 15 additions & 0 deletions proto/telemetry/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ service Query {
rpc ValidatorBlocks(QueryValidatorBlocksRequest) returns (QueryValidatorBlocksResponse) {
option (google.api.http).get = "/telemetry/validator_blocks";
}

rpc ValidatorByConsAddr(QueryValidatorByConsAddrRequest) returns (QueryValidatorByConsAddrResponse) {
option (google.api.http).get = "/telemetry/validator_by_cons_addr";
}
}

// QueryTopBalancesRequest is request type for the Query/TopBalances RPC method.
Expand Down Expand Up @@ -160,4 +164,15 @@ message QueryValidatorBlocksRequest {
message QueryValidatorBlocksResponse {
repeated ValidatorBlock blocks = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryValidatorByConsAddrRequest {
string consensus_address = 1;
}

message QueryValidatorByConsAddrResponse {
cosmos.base.v1beta1.Validator validator = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/x/staking/types.Validator",
(gogoproto.nullable) = false
];
}
32 changes: 30 additions & 2 deletions x/telemetry/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ func GetQueryCmd() *cobra.Command {
/*GetQueryCmdAvgBlockSize(),
GetQueryCmdAvgBlockTime(),
GetQueryCmdAvgTxFee(),
GetQueryCmdTxVolume(),*/
GetQueryCmdValidatorBlocks(),
GetQueryCmdTxVolume(),
GetQueryCmdValidatorBlocks(),*/
GetQueryCmdTopValidators(),
GetQueryCmdValidatorByConsAddr(),
)
return coinswapCmd
}
Expand Down Expand Up @@ -397,3 +398,30 @@ func ParseDateInterval(startDateArg, endDateArg string) (*time.Time, *time.Time,

return startDate, endDate, nil
}

func GetQueryCmdValidatorByConsAddr() *cobra.Command {
cmd := &cobra.Command{
Use: "validator-by-consaddr [status]",
Short: "Query for validator by consensus address",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := telemetrytypes.NewQueryClient(clientCtx)
res, err := queryClient.ValidatorByConsAddr(cmd.Context(), &telemetrytypes.QueryValidatorByConsAddrRequest{
ConsensusAddress: args[0],
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
33 changes: 32 additions & 1 deletion x/telemetry/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ func RegisterRoutes(clientCtx client.Context, rtr *mux.Router) {
getTopValidatorsHandler(clientCtx),
).Methods("GET")

rtr.HandleFunc(
/*rtr.HandleFunc(
fmt.Sprintf("/%s/%s", telemetrytypes.ModuleName, telemetrytypes.QueryValidatorBlocks),
getValidatorBlocksHandler(clientCtx),
).Methods("GET")*/

rtr.HandleFunc(
fmt.Sprintf("/%s/%s", telemetrytypes.ModuleName, telemetrytypes.QueryValidatorByConsAddress),
getValidatorByConsAddressHandler(clientCtx),
).Methods("GET")
}

Expand Down Expand Up @@ -267,3 +272,29 @@ func getValidatorBlocksHandler(clientCtx client.Context) http.HandlerFunc {
rest.PostProcessResponse(w, clientCtx, res)
}
}

func getValidatorByConsAddressHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}

var request telemetrytypes.QueryValidatorByConsAddrRequest
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &request) {
return
}
bin := clientCtx.LegacyAmino.MustMarshalJSON(request)

res, height, err := clientCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", telemetrytypes.QuerierRoute, telemetrytypes.QueryValidatorByConsAddress),
bin,
)
if rest.CheckInternalServerError(w, err) {
return
}

clientCtx = clientCtx.WithHeight(height)
rest.PostProcessResponse(w, clientCtx, res)
}
}
14 changes: 14 additions & 0 deletions x/telemetry/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import (

var _ telemetrytypes.QueryServer = Keeper{}

func (k Keeper) ValidatorByConsAddr(c context.Context, request *telemetrytypes.QueryValidatorByConsAddrRequest) (*telemetrytypes.QueryValidatorByConsAddrResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
consAddr, err := sdk.ConsAddressFromHex(request.ConsensusAddress)
if err != nil {
return nil, err
}
val, ok := k.stakingQuerier.Keeper.GetValidatorByConsAddr(ctx, consAddr)
if !ok {
return nil, sdkerrors.ErrNotFound
}

return &telemetrytypes.QueryValidatorByConsAddrResponse{Validator: val}, nil
}

func (k Keeper) TopBalances(
c context.Context,
request *telemetrytypes.QueryTopBalancesRequest,
Expand Down
17 changes: 9 additions & 8 deletions x/telemetry/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ const (
RouterKey = ModuleName
QuerierRoute = ModuleName

QueryTopBalances = "top_balances"
QueryExtendedValidators = "extended_validators"
QueryAvgBlockSize = "avg_block_size"
QueryAvgBlockTime = "avg_block_time"
QueryAvgTxFee = "avg_tx_fee"
QueryTxVolume = "tx_volume"
QueryValidatorBlocks = "validator_blocks"
QueryTopValidators = "top_validators"
QueryTopBalances = "top_balances"
QueryExtendedValidators = "extended_validators"
QueryAvgBlockSize = "avg_block_size"
QueryAvgBlockTime = "avg_block_time"
QueryAvgTxFee = "avg_tx_fee"
QueryTxVolume = "tx_volume"
QueryValidatorBlocks = "validator_blocks"
QueryTopValidators = "top_validators"
QueryValidatorByConsAddress = "validator_by_cons_addr"

DenomTag = "denom"
StatusTag = "status"
Expand Down
Loading

0 comments on commit 634f44c

Please sign in to comment.