Skip to content

Commit cffe421

Browse files
mtsitrinomritoptix
authored andcommitted
feat(rollapp): refactor rollapp cli to be more useful (#842)
1 parent b2f8955 commit cffe421

File tree

10 files changed

+829
-864
lines changed

10 files changed

+829
-864
lines changed

proto/dymension/rollapp/query.proto

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,53 @@ import "google/api/annotations.proto";
66
import "cosmos/base/query/v1beta1/pagination.proto";
77
import "dymension/rollapp/params.proto";
88
import "dymension/rollapp/rollapp.proto";
9-
// this line is used by starport scaffolding # 1
109
import "dymension/rollapp/state_info.proto";
1110

1211
option go_package = "github.com/dymensionxyz/dymension/v3/x/rollapp/types";
1312

1413
// Query defines the gRPC querier service.
1514
service Query {
16-
// Parameters queries the parameters of the module.
17-
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
18-
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/params";
19-
}
20-
// Queries a Rollapp by index.
21-
rpc Rollapp(QueryGetRollappRequest) returns (QueryGetRollappResponse) {
22-
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/rollapp/{rollappId}";
23-
}
24-
25-
// Queries a Rollapp by index.
26-
rpc RollappByEIP155(QueryGetRollappByEIP155Request) returns (QueryGetRollappResponse) {
27-
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/eip155/{eip155}";
28-
}
29-
30-
// Queries a list of Rollapp items.
31-
rpc RollappAll(QueryAllRollappRequest) returns (QueryAllRollappResponse) {
32-
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/rollapp";
33-
}
34-
35-
// Queries a LatestStateIndex by rollapp-id.
36-
rpc LatestStateIndex(QueryGetLatestStateIndexRequest) returns (QueryGetLatestStateIndexResponse) {
37-
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/latest_state_index/{rollappId}";
38-
}
39-
40-
// Queries a StateInfo by index.
41-
rpc StateInfo(QueryGetStateInfoRequest) returns (QueryGetStateInfoResponse) {
42-
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_info/{rollappId}/{index}";
43-
}
44-
45-
// Queries a list of StateInfo items.
46-
rpc StateInfoAll(QueryAllStateInfoRequest) returns (QueryAllStateInfoResponse) {
47-
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_info";
48-
}
49-
50-
// this line is used by starport scaffolding # 2
15+
// Parameters queries the parameters of the module.
16+
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
17+
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/params";
18+
}
19+
// Queries a Rollapp by index.
20+
rpc Rollapp(QueryGetRollappRequest) returns (QueryGetRollappResponse) {
21+
option (google.api.http).get =
22+
"/dymensionxyz/dymension/rollapp/rollapp/{rollappId}";
23+
}
24+
25+
// Queries a Rollapp by index.
26+
rpc RollappByEIP155(QueryGetRollappByEIP155Request)
27+
returns (QueryGetRollappResponse) {
28+
option (google.api.http).get =
29+
"/dymensionxyz/dymension/rollapp/eip155/{eip155}";
30+
}
31+
32+
// Queries a list of Rollapp items.
33+
rpc RollappAll(QueryAllRollappRequest) returns (QueryAllRollappResponse) {
34+
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/rollapp";
35+
}
36+
37+
// Queries a LatestHeight by rollapp-id.
38+
rpc LatestHeight(QueryGetLatestHeightRequest)
39+
returns (QueryGetLatestHeightResponse) {
40+
option (google.api.http).get =
41+
"/dymensionxyz/dymension/rollapp/latest_height/{rollappId}";
42+
}
43+
44+
// Queries a LatestStateIndex by rollapp-id.
45+
rpc LatestStateIndex(QueryGetLatestStateIndexRequest)
46+
returns (QueryGetLatestStateIndexResponse) {
47+
option (google.api.http).get =
48+
"/dymensionxyz/dymension/rollapp/latest_state_index/{rollappId}";
49+
}
50+
51+
// Queries a StateInfo by index.
52+
rpc StateInfo(QueryGetStateInfoRequest) returns (QueryGetStateInfoResponse) {
53+
option (google.api.http).get =
54+
"/dymensionxyz/dymension/rollapp/state_info/{rollappId}/{index}";
55+
}
5156
}
5257

5358
// QueryParamsRequest is request type for the Query/Params RPC method.
@@ -56,62 +61,56 @@ message QueryParamsRequest {}
5661
// QueryParamsResponse is response type for the Query/Params RPC method.
5762
message QueryParamsResponse {
5863
// params holds all the parameters of this module.
59-
Params params = 1 [(gogoproto.nullable) = false];
64+
Params params = 1 [ (gogoproto.nullable) = false ];
6065
}
6166

62-
message QueryGetRollappRequest {
63-
string rollappId = 1;
64-
}
67+
message QueryGetRollappRequest { string rollappId = 1; }
68+
69+
message QueryGetRollappByEIP155Request { uint64 eip155 = 1; }
6570

66-
message QueryGetRollappByEIP155Request {
67-
uint64 eip155 = 1;
71+
message QueryGetLatestHeightRequest {
72+
string rollappId = 1;
73+
bool finalized = 2;
6874
}
6975

76+
message QueryGetLatestHeightResponse { uint64 height = 1; }
77+
7078
message QueryGetLatestStateIndexRequest {
71-
string rollappId = 1;
72-
bool finalized = 2;
79+
string rollappId = 1;
80+
bool finalized = 2;
7381
}
7482

7583
message QueryGetLatestStateIndexResponse {
76-
StateInfoIndex stateIndex = 1 [(gogoproto.nullable) = false];
84+
StateInfoIndex stateIndex = 1 [ (gogoproto.nullable) = false ];
7785
}
7886

7987
message QueryGetRollappResponse {
80-
Rollapp rollapp = 1 [(gogoproto.nullable) = false];
81-
// Defines the index of the last rollapp UpdateState.
82-
StateInfoIndex latestStateIndex = 2;
83-
// Defines the index of the last rollapp UpdateState that was finalized.
84-
StateInfoIndex latestFinalizedStateIndex = 3;
88+
Rollapp rollapp = 1 [ (gogoproto.nullable) = false ];
89+
// Defines the index of the last rollapp UpdateState.
90+
StateInfoIndex latestStateIndex = 2;
91+
// Defines the index of the last rollapp UpdateState that was finalized.
92+
StateInfoIndex latestFinalizedStateIndex = 3;
93+
94+
uint64 latestHeight = 4;
95+
uint64 latestFinalizedHeight = 5;
8596
}
8697

8798
message QueryAllRollappRequest {
88-
cosmos.base.query.v1beta1.PageRequest pagination = 1;
99+
cosmos.base.query.v1beta1.PageRequest pagination = 1;
89100
}
90101

91102
message QueryAllRollappResponse {
92-
repeated RollappSummary rollapp = 1 [(gogoproto.nullable) = false];
93-
cosmos.base.query.v1beta1.PageResponse pagination = 2;
103+
repeated RollappSummary rollapp = 1 [ (gogoproto.nullable) = false ];
104+
cosmos.base.query.v1beta1.PageResponse pagination = 2;
94105
}
95106

96107
message QueryGetStateInfoRequest {
97-
string rollappId = 1;
98-
uint64 index = 2;
99-
uint64 height = 3;
100-
bool finalized = 4;
108+
string rollappId = 1;
109+
uint64 index = 2;
110+
uint64 height = 3;
111+
bool finalized = 4;
101112
}
102113

103114
message QueryGetStateInfoResponse {
104-
StateInfo stateInfo = 1 [(gogoproto.nullable) = false];
105-
}
106-
107-
message QueryAllStateInfoRequest {
108-
string rollappId = 1;
109-
cosmos.base.query.v1beta1.PageRequest pagination = 2;
110-
}
111-
112-
message QueryAllStateInfoResponse {
113-
repeated StateInfoSummary stateInfo = 1 [(gogoproto.nullable) = false];
114-
cosmos.base.query.v1beta1.PageResponse pagination = 2;
115+
StateInfo stateInfo = 1 [ (gogoproto.nullable) = false ];
115116
}
116-
117-
// this line is used by starport scaffolding # 3

x/rollapp/client/cli/query.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
2727
cmd.AddCommand(CmdQueryParams())
2828
cmd.AddCommand(CmdListRollapp())
2929
cmd.AddCommand(CmdShowRollapp())
30-
cmd.AddCommand(CmdListStateInfo())
3130
cmd.AddCommand(CmdShowStateInfo())
31+
cmd.AddCommand(CmdShowLatestHeight())
3232
cmd.AddCommand(CmdShowLatestStateIndex())
3333

34-
// this line is used by starport scaffolding # 1
35-
3634
return cmd
3735
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
6+
"github.com/cosmos/cosmos-sdk/client"
7+
"github.com/cosmos/cosmos-sdk/client/flags"
8+
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func CmdShowLatestHeight() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "latest-height [rollapp-id]",
15+
Short: "Query the last height of the last UpdateState associated with the specified rollapp-id.",
16+
Args: cobra.ExactArgs(1),
17+
RunE: func(cmd *cobra.Command, args []string) (err error) {
18+
clientCtx := client.GetClientContextFromCmd(cmd)
19+
20+
queryClient := types.NewQueryClient(clientCtx)
21+
22+
argRollappId := args[0]
23+
24+
argFinalized, err := cmd.Flags().GetBool(FlagFinalized)
25+
if err != nil {
26+
return err
27+
}
28+
29+
req := &types.QueryGetLatestHeightRequest{
30+
RollappId: argRollappId,
31+
Finalized: argFinalized,
32+
}
33+
34+
res, err := queryClient.LatestHeight(context.Background(), req)
35+
if err != nil {
36+
return err
37+
}
38+
39+
return clientCtx.PrintProto(res)
40+
},
41+
}
42+
43+
cmd.Flags().Bool(FlagFinalized, false, "Indicates whether to return the latest finalized state index")
44+
45+
flags.AddQueryFlagsToCmd(cmd)
46+
47+
return cmd
48+
}

x/rollapp/client/cli/query_state_info.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,6 @@ const (
1818
FlagFinalized = "finalized"
1919
)
2020

21-
func CmdListStateInfo() *cobra.Command {
22-
cmd := &cobra.Command{
23-
Use: "states [rollapp-id]",
24-
Short: "Query all states associated with the specified rollapp-id",
25-
Args: cobra.ExactArgs(1),
26-
RunE: func(cmd *cobra.Command, args []string) error {
27-
clientCtx := client.GetClientContextFromCmd(cmd)
28-
29-
argRollappId := args[0]
30-
pageReq, err := client.ReadPageRequest(cmd.Flags())
31-
if err != nil {
32-
return err
33-
}
34-
35-
queryClient := types.NewQueryClient(clientCtx)
36-
37-
params := &types.QueryAllStateInfoRequest{
38-
RollappId: argRollappId,
39-
Pagination: pageReq,
40-
}
41-
42-
res, err := queryClient.StateInfoAll(context.Background(), params)
43-
if err != nil {
44-
return err
45-
}
46-
47-
return clientCtx.PrintProto(res)
48-
},
49-
}
50-
51-
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
52-
flags.AddQueryFlagsToCmd(cmd)
53-
54-
return cmd
55-
}
56-
5721
func CmdShowStateInfo() *cobra.Command {
5822
cmd := &cobra.Command{
5923
Use: "state [rollapp-id]",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package keeper
2+
3+
import (
4+
"context"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
8+
"google.golang.org/grpc/codes"
9+
"google.golang.org/grpc/status"
10+
)
11+
12+
func (k Keeper) LatestHeight(c context.Context, req *types.QueryGetLatestHeightRequest) (*types.QueryGetLatestHeightResponse, error) {
13+
if req == nil {
14+
return nil, status.Error(codes.InvalidArgument, "invalid request")
15+
}
16+
ctx := sdk.UnwrapSDKContext(c)
17+
18+
var val types.StateInfoIndex
19+
var found bool
20+
if req.Finalized {
21+
val, found = k.GetLatestFinalizedStateIndex(
22+
ctx,
23+
req.RollappId,
24+
)
25+
} else {
26+
val, found = k.GetLatestStateInfoIndex(
27+
ctx,
28+
req.RollappId,
29+
)
30+
}
31+
if !found {
32+
return nil, status.Error(codes.NotFound, "not found")
33+
}
34+
35+
state := k.MustGetStateInfo(ctx, req.RollappId, val.Index)
36+
37+
return &types.QueryGetLatestHeightResponse{
38+
Height: state.GetLatestHeight(),
39+
}, nil
40+
}

x/rollapp/keeper/grpc_query_state_info.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,13 @@ package keeper
33
import (
44
"context"
55

6-
"github.com/cosmos/cosmos-sdk/store/prefix"
76
sdk "github.com/cosmos/cosmos-sdk/types"
87
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
9-
"github.com/cosmos/cosmos-sdk/types/query"
108
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
119
"google.golang.org/grpc/codes"
1210
"google.golang.org/grpc/status"
1311
)
1412

15-
func (k Keeper) StateInfoAll(c context.Context, req *types.QueryAllStateInfoRequest) (*types.QueryAllStateInfoResponse, error) {
16-
if req == nil {
17-
return nil, status.Error(codes.InvalidArgument, "invalid request")
18-
}
19-
20-
var stateInfos []types.StateInfoSummary
21-
ctx := sdk.UnwrapSDKContext(c)
22-
23-
store := ctx.KVStore(k.storeKey)
24-
stateInfoStore := prefix.NewStore(store, types.KeyPrefix(types.StateInfoKeyPrefix))
25-
26-
pageRes, err := query.Paginate(stateInfoStore, req.Pagination, func(key []byte, value []byte) error {
27-
var stateInfo types.StateInfo
28-
if err := k.cdc.Unmarshal(value, &stateInfo); err != nil {
29-
return err
30-
}
31-
if stateInfo.StateInfoIndex.RollappId == req.RollappId {
32-
stateInfoSummary := types.StateInfoSummary{
33-
StateInfoIndex: stateInfo.StateInfoIndex,
34-
Status: stateInfo.Status,
35-
CreationHeight: stateInfo.CreationHeight,
36-
}
37-
stateInfos = append(stateInfos, stateInfoSummary)
38-
}
39-
return nil
40-
})
41-
if err != nil {
42-
return nil, status.Error(codes.Internal, err.Error())
43-
}
44-
45-
return &types.QueryAllStateInfoResponse{StateInfo: stateInfos, Pagination: pageRes}, nil
46-
}
47-
4813
func (k Keeper) StateInfo(c context.Context, req *types.QueryGetStateInfoRequest) (*types.QueryGetStateInfoResponse, error) {
4914
if req == nil {
5015
return nil, status.Error(codes.InvalidArgument, "invalid request")
@@ -63,8 +28,10 @@ func (k Keeper) StateInfo(c context.Context, req *types.QueryGetStateInfoRequest
6328
} else {
6429
latestStateIndex, found := k.GetLatestStateInfoIndex(ctx, req.RollappId)
6530
if !found {
66-
return nil, sdkerrors.Wrapf(sdkerrors.ErrNotFound,
67-
"LatestStateInfoIndex wasn't found for rollappId=%s", req.RollappId)
31+
if _, exists := k.GetRollapp(ctx, req.RollappId); !exists {
32+
return nil, types.ErrRollappNotRegistered
33+
}
34+
return nil, status.Error(codes.NotFound, "not found")
6835
}
6936
req.Index = latestStateIndex.Index
7037
}

0 commit comments

Comments
 (0)