Skip to content

Commit

Permalink
add consumer commission rate query
Browse files Browse the repository at this point in the history
  • Loading branch information
sainoe committed Mar 26, 2024
1 parent 24be8b2 commit b311f35
Show file tree
Hide file tree
Showing 5 changed files with 743 additions and 104 deletions.
28 changes: 27 additions & 1 deletion proto/interchain_security/ccv/provider/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "interchain_security/ccv/provider/v1/provider.proto";
import "interchain_security/ccv/v1/shared_consumer.proto";
import "interchain_security/ccv/v1/wire.proto";
import "tendermint/crypto/keys.proto";
import "cosmos_proto/cosmos.proto";

service Query {
// ConsumerGenesis queries the genesis state needed to start a consumer chain
Expand Down Expand Up @@ -117,6 +118,15 @@ service Query {
option (google.api.http).get =
"/interchain_security/ccv/provider/consumer_chains_per_validator";
}

// QueryValidatorConsumerCommissionRate returns the commission rate a given
// validator charges on a given consumer chain
rpc QueryValidatorConsumerCommissionRate(
QueryValidatorConsumerCommissionRateRequest)
returns (QueryValidatorConsumerCommissionRateResponse) {
option (google.api.http).get =
"/interchain_security/ccv/provider/consumer_commission_rate";
}
}

message QueryConsumerGenesisRequest { string chain_id = 1; }
Expand Down Expand Up @@ -251,4 +261,20 @@ message QueryConsumerChainsByValidatorAddressRequest {

message QueryConsumerChainsByValidatorAddressResponse {
repeated string validator_consumer_chains = 1;
}
}

message QueryValidatorConsumerCommissionRateRequest {
string chain_id = 1;
// The consensus address of the validator on the provider chain
string provider_address = 2 [ (gogoproto.moretags) = "yaml:\"address\"" ];
}

message QueryValidatorConsumerCommissionRateResponse {
// The rate to charge delegators on the consumer chain, as a fraction
string rate = 1 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

49 changes: 47 additions & 2 deletions x/ccv/provider/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewQueryCmd() *cobra.Command {
cmd.AddCommand(CmdProviderParameters())
cmd.AddCommand(CmdOptedInValidatorsByConsumerChainID())
cmd.AddCommand(CmdConsumerChainsByValidatorAddress())
cmd.AddCommand(CmdValidatorConsumerCommissionRate())
return cmd
}

Expand Down Expand Up @@ -415,7 +416,7 @@ $ %s query provider params
// Command to query opted-in validators by consumer chain ID
func CmdOptedInValidatorsByConsumerChainID() *cobra.Command {
cmd := &cobra.Command{
Use: "opted-in-validators",
Use: "opted-in-validators [chainid]",
Short: "Query opted-in validators for a given consumer chain",
Long: strings.TrimSpace(
fmt.Sprintf(`Query opted-in validators for a given consumer chain.
Expand Down Expand Up @@ -450,7 +451,7 @@ $ %s opted-in-validators foochain
func CmdConsumerChainsByValidatorAddress() *cobra.Command {
bech32PrefixConsAddr := sdk.GetConfig().GetBech32ConsensusAddrPrefix()
cmd := &cobra.Command{
Use: "has-to-validate",
Use: "has-to-validate [provider-validator-address]",
Short: "Query the consumer chains list a given validator has to validate",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the consumer chains list a given validator has to validate.
Expand Down Expand Up @@ -487,3 +488,47 @@ $ %s has-to-validate %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj

return cmd
}

// Command to query the consumer commission rate a validator charges
// on a consumer chain
func CmdValidatorConsumerCommissionRate() *cobra.Command {
bech32PrefixConsAddr := sdk.GetConfig().GetBech32ConsensusAddrPrefix()
cmd := &cobra.Command{
Use: "validator-consumer-commission-rate [chainid] [provider-validator-address]",
Short: "Query the consumer commission rate a validator charges on a consumer chain",
Long: strings.TrimSpace(
fmt.Sprintf(`Query the consumer commission rate a validator charges on a consumer chain.
Example:
$ %s validator-consumer-commission-rate foochain %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj
`, version.AppName, bech32PrefixConsAddr),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

addr, err := sdk.ConsAddressFromBech32(args[1])
if err != nil {
return err
}

res, err := queryClient.QueryValidatorConsumerCommissionRate(cmd.Context(),
&types.QueryValidatorConsumerCommissionRateRequest{
ChainId: args[0],
ProviderAddress: addr.String(),
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
39 changes: 39 additions & 0 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,42 @@ func (k Keeper) QueryConsumerChainsByValidatorAddress(goCtx context.Context, req
ValidatorConsumerChains: consumersToValidate,
}, nil
}

// QueryValidatorConsumerCommissionRate returns the commission rate a given
// validator charges on a given consumer chain
func (k Keeper) QueryValidatorConsumerCommissionRate(goCtx context.Context, req *types.QueryValidatorConsumerCommissionRateRequest) (*types.QueryValidatorConsumerCommissionRateResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

consumerChainID := req.ChainId
if consumerChainID == "" {
return nil, status.Error(codes.InvalidArgument, "empty chainId")
}

consAddr, err := sdk.ConsAddressFromBech32(req.ProviderAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid provider address")
}

ctx := sdk.UnwrapSDKContext(goCtx)

if !k.IsConsumerProposedOrRegistered(ctx, consumerChainID) {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("unknown consumer chain: %s", consumerChainID))
}

res := &types.QueryValidatorConsumerCommissionRateResponse{}

consumerRate, found := k.GetConsumerCommissionRate(ctx, consumerChainID, types.NewProviderConsAddress(consAddr))
if found {
res.Rate = consumerRate
} else {
v, ok := k.stakingKeeper.GetValidatorByConsAddr(ctx, consAddr)
if !ok {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("unknown validator: %s", consAddr.String()))
}
res.Rate = v.Commission.Rate
}

return res, nil
}
Loading

0 comments on commit b311f35

Please sign in to comment.