Skip to content

Commit 00ba140

Browse files
sainoempokeinsumity
authored
feat: add consumer genesis time query (#2366)
* add query handler for consumer genesis time * add consumer genesis time command to provider CLI * update docs * typo * typo #2 * typo #3 * add godoc * nit * add entry to changelog * fix tests after main merge * Update x/ccv/provider/keeper/grpc_query.go Co-authored-by: insumity <[email protected]> --------- Co-authored-by: Marius Poke <[email protected]> Co-authored-by: insumity <[email protected]>
1 parent b3a2781 commit 00ba140

File tree

8 files changed

+950
-158
lines changed

8 files changed

+950
-158
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `[x/provider]` Add query for consumer genesis time,
2+
which corresponds to creation time of consumer clients.
3+
([\#2366](https://github.com/cosmos/interchain-security/pull/2366))

docs/docs/build/modules/02-provider.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,29 @@ power_shaping_params:
15661566

15671567
</details>
15681568

1569+
##### Consumer Genesis Time
1570+
1571+
The `consumer-genesis-time` command allows to query the genesis time of the consumer chain associated with the consumer id.
1572+
1573+
```bash
1574+
interchain-security-pd query provider consumer-genesis-time [consumer-id] [flags]
1575+
```
1576+
1577+
<details>
1578+
<summary>Example</summary>
1579+
1580+
```bash
1581+
interchain-security-pd query provider consumer-genesis-time 0
1582+
```
1583+
1584+
Output:
1585+
1586+
```bash
1587+
genesis_time: "2024-10-18T08:13:23.507178095Z"
1588+
```
1589+
1590+
</details>
1591+
15691592
#### Transactions
15701593

15711594
The `tx` commands allows users to interact with the `provider` module.
@@ -2409,7 +2432,7 @@ Output:
24092432

24102433
#### Throttle State
24112434

2412-
The `QueryThrottleState` queries the main on-chain state relevant to slash packet throttling.
2435+
The `QueryThrottleState` endpoint queries the main on-chain state relevant to slash packet throttling.
24132436

24142437
```bash
24152438
interchain_security.ccv.provider.v1.Query/QueryThrottleState
@@ -2801,7 +2824,7 @@ Output:
28012824

28022825
#### Consumer Chain
28032826

2804-
The `QueryConsumerChain` command allows to query the consumer chain associated with the consumer id.
2827+
The `QueryConsumerChain` endpoint allows to query the consumer chain associated with the consumer id.
28052828

28062829
```bash
28072830
interchain_security.ccv.provider.v1.Query/QueryConsumerChain
@@ -2850,6 +2873,29 @@ grpcurl -plaintext -d '{"consumer_id": "0"}' localhost:9090 interchain_security.
28502873

28512874
</details>
28522875

2876+
#### Consumer Genesis Time
2877+
2878+
The `QueryConsumerGenesisTime` endpoint allows to query the genesis time of the consumer chain associated with the consumer id.
2879+
2880+
```bash
2881+
interchain_security.ccv.provider.v1.Query/QueryConsumerGenesisTime
2882+
```
2883+
2884+
<details>
2885+
<summary>Example</summary>
2886+
2887+
```bash
2888+
grpcurl -plaintext -d '{"consumer_id": "0"}' localhost:9090 interchain_security.ccv.provider.v1.Query/QueryConsumerGenesisTime
2889+
```
2890+
2891+
```json
2892+
{
2893+
"genesisTime": "2024-10-18T08:13:23.507178095Z"
2894+
}
2895+
```
2896+
2897+
</details>
2898+
28532899
### REST
28542900

28552901
A user can query the `provider` module using REST endpoints.
@@ -3524,3 +3570,28 @@ Output:
35243570
```
35253571

35263572
</details>
3573+
3574+
#### Consumer Genesis Time
3575+
3576+
The `consumer_genesis_time` endpoint allows to query the genesis time of the consumer chain associated with the consumer id.
3577+
3578+
```bash
3579+
interchain_security/ccv/provider/consumer_genesis_time/{consumer_id}
3580+
```
3581+
3582+
<details>
3583+
<summary>Example</summary>
3584+
3585+
```bash
3586+
curl http://localhost:1317/interchain_security/ccv/provider/consumer_genesis_time/0
3587+
```
3588+
3589+
Output:
3590+
3591+
```json
3592+
{
3593+
"genesis_time":"2024-10-18T08:29:46.153234Z"
3594+
}
3595+
```
3596+
3597+
</details>

proto/interchain_security/ccv/provider/v1/query.proto

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ service Query {
144144
option (google.api.http).get =
145145
"/interchain_security/ccv/provider/consumer_chain/{consumer_id}";
146146
}
147+
148+
// QueryConsumerGenesisTime returns the genesis time
149+
// of the consumer chain associated with the provided consumer id
150+
rpc QueryConsumerGenesisTime(QueryConsumerGenesisTimeRequest)
151+
returns (QueryConsumerGenesisTimeResponse) {
152+
option (google.api.http).get =
153+
"/interchain_security/ccv/provider/consumer_genesis_time/{consumer_id}";
154+
}
147155
}
148156

149157
message QueryConsumerGenesisRequest {
@@ -390,3 +398,12 @@ message QueryConsumerChainResponse {
390398
ConsumerInitializationParameters init_params = 6;
391399
PowerShapingParameters power_shaping_params = 7;
392400
}
401+
402+
message QueryConsumerGenesisTimeRequest {
403+
string consumer_id = 1;
404+
}
405+
406+
message QueryConsumerGenesisTimeResponse {
407+
google.protobuf.Timestamp genesis_time = 1
408+
[ (gogoproto.stdtime) = true, (gogoproto.nullable) = false ];
409+
}

x/ccv/provider/client/cli/query.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func NewQueryCmd() *cobra.Command {
4141
cmd.AddCommand(CmdBlocksUntilNextEpoch())
4242
cmd.AddCommand(CmdConsumerIdFromClientId())
4343
cmd.AddCommand(CmdConsumerChain())
44+
cmd.AddCommand(CmdConsumerGenesisTime())
4445
return cmd
4546
}
4647

@@ -584,3 +585,30 @@ func CmdConsumerChain() *cobra.Command {
584585

585586
return cmd
586587
}
588+
589+
func CmdConsumerGenesisTime() *cobra.Command {
590+
cmd := &cobra.Command{
591+
Use: "consumer-genesis-time [consumer-id]",
592+
Short: "Query the genesis time of the consumer chain associated with the consumer id",
593+
Args: cobra.ExactArgs(1),
594+
RunE: func(cmd *cobra.Command, args []string) error {
595+
clientCtx, err := client.GetClientQueryContext(cmd)
596+
if err != nil {
597+
return err
598+
}
599+
queryClient := types.NewQueryClient(clientCtx)
600+
601+
req := &types.QueryConsumerGenesisTimeRequest{ConsumerId: args[0]}
602+
res, err := queryClient.QueryConsumerGenesisTime(cmd.Context(), req)
603+
if err != nil {
604+
return err
605+
}
606+
607+
return clientCtx.PrintProto(res)
608+
},
609+
}
610+
611+
flags.AddQueryFlagsToCmd(cmd)
612+
613+
return cmd
614+
}

x/ccv/provider/keeper/grpc_query.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/binary"
77
"fmt"
88
"sort"
9+
"time"
910

1011
"google.golang.org/grpc/codes"
1112
"google.golang.org/grpc/status"
@@ -627,3 +628,59 @@ func (k Keeper) QueryConsumerChain(goCtx context.Context, req *types.QueryConsum
627628
PowerShapingParams: &powerParams,
628629
}, nil
629630
}
631+
632+
// QueryConsumerGenesisTime returns the genesis time
633+
// of the consumer chain associated with the provided consumer id
634+
func (k Keeper) QueryConsumerGenesisTime(goCtx context.Context, req *types.QueryConsumerGenesisTimeRequest) (*types.QueryConsumerGenesisTimeResponse, error) {
635+
if req == nil {
636+
return nil, status.Errorf(codes.InvalidArgument, "empty request")
637+
}
638+
639+
consumerId := req.ConsumerId
640+
if err := ccvtypes.ValidateConsumerId(consumerId); err != nil {
641+
return nil, status.Error(codes.InvalidArgument, err.Error())
642+
}
643+
ctx := sdk.UnwrapSDKContext(goCtx)
644+
645+
// Get consumer initialization params. If they aren't found,
646+
// it means that there is no consumer for that consumerId.
647+
params, err := k.GetConsumerInitializationParameters(ctx, consumerId)
648+
if err != nil {
649+
return nil, status.Errorf(
650+
codes.InvalidArgument,
651+
"cannot get consumer genesis time for consumer Id: %s: %s",
652+
consumerId, types.ErrUnknownConsumerId,
653+
)
654+
}
655+
656+
// Get the consumer clientId. If it isn't found, it means
657+
// that the consumer hasn't been launched or has been stopped and deleted.
658+
clientID, ok := k.GetConsumerClientId(ctx, consumerId)
659+
if !ok {
660+
return nil, status.Errorf(
661+
codes.InvalidArgument,
662+
"cannot get consumer genesis time for consumer Id: %s: consumer hasn't been launched or has been stopped and deleted",
663+
consumerId,
664+
)
665+
}
666+
667+
// Get the consensus state of the consumer client at the initial height,
668+
// for which the timestamps corresponds to the consumer genesis time
669+
cs, ok := k.clientKeeper.GetClientConsensusState(
670+
ctx,
671+
clientID,
672+
params.InitialHeight,
673+
)
674+
if !ok {
675+
return nil, status.Errorf(
676+
codes.InvalidArgument,
677+
"cannot get consumer genesis time for consumer Id: %s: cannot find consensus state for initial height: %s",
678+
consumerId,
679+
params.InitialHeight,
680+
)
681+
}
682+
683+
return &types.QueryConsumerGenesisTimeResponse{
684+
GenesisTime: time.Unix(0, int64(cs.GetTimestamp())),
685+
}, nil
686+
}

0 commit comments

Comments
 (0)