Skip to content

Commit

Permalink
feat(rollapp): refactor rollapp cli to be more useful (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored and omritoptix committed Apr 14, 2024
1 parent b2f8955 commit cffe421
Show file tree
Hide file tree
Showing 10 changed files with 829 additions and 864 deletions.
139 changes: 69 additions & 70 deletions proto/dymension/rollapp/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,53 @@ import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "dymension/rollapp/params.proto";
import "dymension/rollapp/rollapp.proto";
// this line is used by starport scaffolding # 1
import "dymension/rollapp/state_info.proto";

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

// Query defines the gRPC querier service.
service Query {
// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/params";
}
// Queries a Rollapp by index.
rpc Rollapp(QueryGetRollappRequest) returns (QueryGetRollappResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/rollapp/{rollappId}";
}

// Queries a Rollapp by index.
rpc RollappByEIP155(QueryGetRollappByEIP155Request) returns (QueryGetRollappResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/eip155/{eip155}";
}

// Queries a list of Rollapp items.
rpc RollappAll(QueryAllRollappRequest) returns (QueryAllRollappResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/rollapp";
}

// Queries a LatestStateIndex by rollapp-id.
rpc LatestStateIndex(QueryGetLatestStateIndexRequest) returns (QueryGetLatestStateIndexResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/latest_state_index/{rollappId}";
}

// Queries a StateInfo by index.
rpc StateInfo(QueryGetStateInfoRequest) returns (QueryGetStateInfoResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_info/{rollappId}/{index}";
}

// Queries a list of StateInfo items.
rpc StateInfoAll(QueryAllStateInfoRequest) returns (QueryAllStateInfoResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_info";
}

// this line is used by starport scaffolding # 2
// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/params";
}
// Queries a Rollapp by index.
rpc Rollapp(QueryGetRollappRequest) returns (QueryGetRollappResponse) {
option (google.api.http).get =
"/dymensionxyz/dymension/rollapp/rollapp/{rollappId}";
}

// Queries a Rollapp by index.
rpc RollappByEIP155(QueryGetRollappByEIP155Request)
returns (QueryGetRollappResponse) {
option (google.api.http).get =
"/dymensionxyz/dymension/rollapp/eip155/{eip155}";
}

// Queries a list of Rollapp items.
rpc RollappAll(QueryAllRollappRequest) returns (QueryAllRollappResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/rollapp";
}

// Queries a LatestHeight by rollapp-id.
rpc LatestHeight(QueryGetLatestHeightRequest)
returns (QueryGetLatestHeightResponse) {
option (google.api.http).get =
"/dymensionxyz/dymension/rollapp/latest_height/{rollappId}";
}

// Queries a LatestStateIndex by rollapp-id.
rpc LatestStateIndex(QueryGetLatestStateIndexRequest)
returns (QueryGetLatestStateIndexResponse) {
option (google.api.http).get =
"/dymensionxyz/dymension/rollapp/latest_state_index/{rollappId}";
}

// Queries a StateInfo by index.
rpc StateInfo(QueryGetStateInfoRequest) returns (QueryGetStateInfoResponse) {
option (google.api.http).get =
"/dymensionxyz/dymension/rollapp/state_info/{rollappId}/{index}";
}
}

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

message QueryGetRollappRequest {
string rollappId = 1;
}
message QueryGetRollappRequest { string rollappId = 1; }

message QueryGetRollappByEIP155Request { uint64 eip155 = 1; }

message QueryGetRollappByEIP155Request {
uint64 eip155 = 1;
message QueryGetLatestHeightRequest {
string rollappId = 1;
bool finalized = 2;
}

message QueryGetLatestHeightResponse { uint64 height = 1; }

message QueryGetLatestStateIndexRequest {
string rollappId = 1;
bool finalized = 2;
string rollappId = 1;
bool finalized = 2;
}

message QueryGetLatestStateIndexResponse {
StateInfoIndex stateIndex = 1 [(gogoproto.nullable) = false];
StateInfoIndex stateIndex = 1 [ (gogoproto.nullable) = false ];
}

message QueryGetRollappResponse {
Rollapp rollapp = 1 [(gogoproto.nullable) = false];
// Defines the index of the last rollapp UpdateState.
StateInfoIndex latestStateIndex = 2;
// Defines the index of the last rollapp UpdateState that was finalized.
StateInfoIndex latestFinalizedStateIndex = 3;
Rollapp rollapp = 1 [ (gogoproto.nullable) = false ];
// Defines the index of the last rollapp UpdateState.
StateInfoIndex latestStateIndex = 2;
// Defines the index of the last rollapp UpdateState that was finalized.
StateInfoIndex latestFinalizedStateIndex = 3;

uint64 latestHeight = 4;
uint64 latestFinalizedHeight = 5;
}

message QueryAllRollappRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryAllRollappResponse {
repeated RollappSummary rollapp = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
repeated RollappSummary rollapp = 1 [ (gogoproto.nullable) = false ];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryGetStateInfoRequest {
string rollappId = 1;
uint64 index = 2;
uint64 height = 3;
bool finalized = 4;
string rollappId = 1;
uint64 index = 2;
uint64 height = 3;
bool finalized = 4;
}

message QueryGetStateInfoResponse {
StateInfo stateInfo = 1 [(gogoproto.nullable) = false];
}

message QueryAllStateInfoRequest {
string rollappId = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryAllStateInfoResponse {
repeated StateInfoSummary stateInfo = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
StateInfo stateInfo = 1 [ (gogoproto.nullable) = false ];
}

// this line is used by starport scaffolding # 3
4 changes: 1 addition & 3 deletions x/rollapp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdListRollapp())
cmd.AddCommand(CmdShowRollapp())
cmd.AddCommand(CmdListStateInfo())
cmd.AddCommand(CmdShowStateInfo())
cmd.AddCommand(CmdShowLatestHeight())
cmd.AddCommand(CmdShowLatestStateIndex())

// this line is used by starport scaffolding # 1

return cmd
}
48 changes: 48 additions & 0 deletions x/rollapp/client/cli/query_latest_height.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"context"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"github.com/spf13/cobra"
)

func CmdShowLatestHeight() *cobra.Command {
cmd := &cobra.Command{
Use: "latest-height [rollapp-id]",
Short: "Query the last height of the last UpdateState associated with the specified rollapp-id.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

argRollappId := args[0]

argFinalized, err := cmd.Flags().GetBool(FlagFinalized)
if err != nil {
return err
}

req := &types.QueryGetLatestHeightRequest{
RollappId: argRollappId,
Finalized: argFinalized,
}

res, err := queryClient.LatestHeight(context.Background(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

cmd.Flags().Bool(FlagFinalized, false, "Indicates whether to return the latest finalized state index")

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
36 changes: 0 additions & 36 deletions x/rollapp/client/cli/query_state_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,6 @@ const (
FlagFinalized = "finalized"
)

func CmdListStateInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "states [rollapp-id]",
Short: "Query all states associated with the specified rollapp-id",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

argRollappId := args[0]
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryAllStateInfoRequest{
RollappId: argRollappId,
Pagination: pageReq,
}

res, err := queryClient.StateInfoAll(context.Background(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdShowStateInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "state [rollapp-id]",
Expand Down
40 changes: 40 additions & 0 deletions x/rollapp/keeper/grpc_query_latest_height.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) LatestHeight(c context.Context, req *types.QueryGetLatestHeightRequest) (*types.QueryGetLatestHeightResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)

var val types.StateInfoIndex
var found bool
if req.Finalized {
val, found = k.GetLatestFinalizedStateIndex(
ctx,
req.RollappId,
)
} else {
val, found = k.GetLatestStateInfoIndex(
ctx,
req.RollappId,
)
}
if !found {
return nil, status.Error(codes.NotFound, "not found")
}

state := k.MustGetStateInfo(ctx, req.RollappId, val.Index)

return &types.QueryGetLatestHeightResponse{
Height: state.GetLatestHeight(),
}, nil
}
41 changes: 4 additions & 37 deletions x/rollapp/keeper/grpc_query_state_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,13 @@ package keeper
import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/dymensionxyz/dymension/v3/x/rollapp/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) StateInfoAll(c context.Context, req *types.QueryAllStateInfoRequest) (*types.QueryAllStateInfoResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var stateInfos []types.StateInfoSummary
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(k.storeKey)
stateInfoStore := prefix.NewStore(store, types.KeyPrefix(types.StateInfoKeyPrefix))

pageRes, err := query.Paginate(stateInfoStore, req.Pagination, func(key []byte, value []byte) error {
var stateInfo types.StateInfo
if err := k.cdc.Unmarshal(value, &stateInfo); err != nil {
return err
}
if stateInfo.StateInfoIndex.RollappId == req.RollappId {
stateInfoSummary := types.StateInfoSummary{
StateInfoIndex: stateInfo.StateInfoIndex,
Status: stateInfo.Status,
CreationHeight: stateInfo.CreationHeight,
}
stateInfos = append(stateInfos, stateInfoSummary)
}
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryAllStateInfoResponse{StateInfo: stateInfos, Pagination: pageRes}, nil
}

func (k Keeper) StateInfo(c context.Context, req *types.QueryGetStateInfoRequest) (*types.QueryGetStateInfoResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
Expand All @@ -63,8 +28,10 @@ func (k Keeper) StateInfo(c context.Context, req *types.QueryGetStateInfoRequest
} else {
latestStateIndex, found := k.GetLatestStateInfoIndex(ctx, req.RollappId)
if !found {
return nil, sdkerrors.Wrapf(sdkerrors.ErrNotFound,
"LatestStateInfoIndex wasn't found for rollappId=%s", req.RollappId)
if _, exists := k.GetRollapp(ctx, req.RollappId); !exists {
return nil, types.ErrRollappNotRegistered
}
return nil, status.Error(codes.NotFound, "not found")
}
req.Index = latestStateIndex.Index
}
Expand Down
Loading

0 comments on commit cffe421

Please sign in to comment.