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

feat: add query for getting all assigned keys #1546

Closed
Closed
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
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 {
"/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 @@ -126,6 +133,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
45 changes: 45 additions & 0 deletions x/ccv/provider/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func NewQueryCmd() *cobra.Command {
cmd.AddCommand(CmdConsumerStartProposals())
cmd.AddCommand(CmdConsumerStopProposals())
cmd.AddCommand(CmdConsumerValidatorKeyAssignment())
cmd.AddCommand(CmdConsumerAllValidatorKeysAssignment())
cmd.AddCommand(CmdProviderValidatorKey())
cmd.AddCommand(CmdThrottleState())
cmd.AddCommand(CmdRegisteredConsumerRewardDenoms())
Expand Down Expand Up @@ -230,6 +231,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 @@ -114,6 +114,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