Skip to content

Commit

Permalink
feat: add query for getting all assigned keys
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 16, 2023
1 parent 0b636da commit 4322232
Show file tree
Hide file tree
Showing 5 changed files with 952 additions and 102 deletions.
27 changes: 27 additions & 0 deletions proto/interchain_security/ccv/provider/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ service Query {
option (google.api.http).get = "/interchain_security/ccv/provider/validator_consumer_addr";
}

// QueryValidatorConsumerAddr queries the address
// assigned by a validator for a consumer chain.
rpc QueryAllValidatorsConsumerAddr(QueryAllValidatorsConsumerAddrRequest)
returns (QueryAllValidatorsConsumerAddrResponse) {
option (google.api.http).get = "/interchain_security/ccv/provider/all_validator_consumer_addr";
}

// QueryProviderAddr returns the provider chain validator
// given a consumer chain validator address
rpc QueryValidatorProviderAddr(QueryValidatorProviderAddrRequest)
Expand Down Expand Up @@ -119,6 +126,26 @@ message QueryValidatorConsumerAddrResponse {
string consumer_address = 1;
}

message QueryAllValidatorsConsumerAddrRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
// The id of the consumer chain, optional
string chain_id = 1;
}

message ValidatorWithConsumerAddr {
// The id of the consumer chain, optional
string chain_id = 1;
// The address of the validator on the consumer chain
string consumer_address = 2;
// The address of the validator on the provider chain
string provider_address = 3;
}

message QueryAllValidatorsConsumerAddrResponse {
repeated ValidatorWithConsumerAddr validators = 1;
}

message QueryValidatorProviderAddrRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
Expand Down
44 changes: 44 additions & 0 deletions x/ccv/provider/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,50 @@ $ %s query provider validator-consumer-key foochain %s1gghjut3ccd8ay0zduzj64hwre
return cmd
}

// TODO: fix naming
func CmdConsumerAllValidatorKeysAssignment() *cobra.Command {
cmd := &cobra.Command{
Use: "validators-consumer-keys [chainid]",
Short: "Query all assigned validators consensus public keys",
Long: strings.TrimSpace(
fmt.Sprintf(`Returns the currently assigned validators consensus public keys for a
specified consumer chain (or all chains), if one has been assigned.
Example:
$ %s query provider validators-consumer-keys foochain
`,
version.AppName,
),
),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

consumerChainID := ""
if len(args) > 0 {
consumerChainID = args[0]
}

req := &types.QueryAllValidatorsConsumerAddrRequest{
ChainId: consumerChainID,
}
res, err := queryClient.QueryAllValidatorsConsumerAddr(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// TODO: fix naming
func CmdProviderValidatorKey() *cobra.Command {
bech32PrefixConsAddr := sdk.GetConfig().GetBech32ConsensusAddrPrefix()
Expand Down
28 changes: 28 additions & 0 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ func (k Keeper) QueryValidatorConsumerAddr(goCtx context.Context, req *types.Que
}, nil
}

func (k Keeper) QueryAllValidatorsConsumerAddr(goCtx context.Context, req *types.QueryAllValidatorsConsumerAddrRequest) (*types.QueryAllValidatorsConsumerAddrResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

var chainID *string
if req.ChainId != "" {
chainID = &req.ChainId
}

keys := k.GetAllValidatorConsumerPubKeys(ctx, chainID)

validators := make([]*types.ValidatorWithConsumerAddr, len(keys))
for index, validatorInfo := range validators {
validators[index] = &types.ValidatorWithConsumerAddr{
ChainId: validatorInfo.ChainId,
ConsumerAddress: validatorInfo.ConsumerAddress,
ProviderAddress: validatorInfo.ProviderAddress,
}
}

return &types.QueryAllValidatorsConsumerAddrResponse{
Validators: validators,
}, nil
}

func (k Keeper) QueryValidatorProviderAddr(goCtx context.Context, req *types.QueryValidatorProviderAddrRequest) (*types.QueryValidatorProviderAddrResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down
Loading

0 comments on commit 4322232

Please sign in to comment.