Skip to content

Commit

Permalink
feat: add orda/cc CLI (#1041)
Browse files Browse the repository at this point in the history
* add `orda/cc` query cli

* add missing error handlings

* add `orda/cc` tx cli
  • Loading branch information
tkxkd0159 authored Aug 9, 2023
1 parent a785359 commit fa85f8a
Show file tree
Hide file tree
Showing 3 changed files with 326 additions and 5 deletions.
228 changes: 224 additions & 4 deletions x/or/da/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cli

import (
"context"
"fmt"
"strconv"

"github.com/spf13/cobra"

Expand All @@ -12,7 +12,7 @@ import (
)

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(queryRoute string) *cobra.Command {
func GetQueryCmd() *cobra.Command {

cmd := &cobra.Command{
Use: types.ModuleName,
Expand All @@ -22,7 +22,16 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(
CmdQueryParams(),
CmdQueryCCState(),
CmdCCRef(),
CmdCCRefs(),
CmdQueueTxState(),
CmdQueueTx(),
CmdQueueTxs(),
CmdMappedBatch(),
)
return cmd
}

Expand All @@ -36,7 +45,218 @@ func CmdQueryParams() *cobra.Command {

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdQueryCCState() *cobra.Command {
cmd := &cobra.Command{
Use: "cc-state [rollup-name]",
Short: "shows the state of the specific rollup's canonical chain",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.CCState(cmd.Context(), &types.QueryCCStateRequest{
RollupName: args[0],
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdCCRef() *cobra.Command {
cmd := &cobra.Command{
Use: "cc-ref [rollup-name] [batch-height]",
Short: "shows the reference of the specific batch in the specific rollup's canonical chain",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

h, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}
res, err := queryClient.CCRef(cmd.Context(), &types.QueryCCRefRequest{
RollupName: args[0],
BatchHeight: h,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdCCRefs() *cobra.Command {
cmd := &cobra.Command{
Use: "cc-refs [rollup-name]",
Short: "shows all references of the specific rollup's canonical chain",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

res, err := queryClient.CCRefs(cmd.Context(), &types.QueryCCRefsRequest{
RollupName: args[0],
Pagination: pageReq,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "canonical chain references")

return cmd
}

func CmdQueueTxState() *cobra.Command {
cmd := &cobra.Command{
Use: "queue-tx-state [rollup-name]",
Short: "shows the state of the specific rollup's tx queue",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.QueueTxState(cmd.Context(), &types.QueryQueueTxStateRequest{
RollupName: args[0],
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdQueueTx() *cobra.Command {
cmd := &cobra.Command{
Use: "queue-tx [rollup-name] [tx-index]",
Short: "shows the specific tx in the specific rollup's tx queue",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

idx, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

res, err := queryClient.QueueTx(cmd.Context(), &types.QueryQueueTxRequest{
RollupName: args[0],
QueueIndex: idx,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdQueueTxs() *cobra.Command {
cmd := &cobra.Command{
Use: "queue-txs [rollup-name]",
Short: "shows all txs in the specific rollup's tx queue",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

res, err := queryClient.QueueTxs(cmd.Context(), &types.QueryQueueTxsRequest{
RollupName: args[0],
Pagination: pageReq,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "rollup queue transactions")

return cmd
}

func CmdMappedBatch() *cobra.Command {
cmd := &cobra.Command{
Use: "mapped-batch [rollup-name] [rollup-height]",
Short: "shows the specific batch reference which is mapped to the specific rollup height",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

l2h, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

res, err := queryClient.MappedBatch(cmd.Context(), &types.QueryMappedBatchRequest{
RollupName: args[0],
L2Height: l2h,
})
if err != nil {
return err
}
Expand Down
101 changes: 101 additions & 0 deletions x/or/da/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import (
"github.com/spf13/cobra"

"github.com/Finschia/finschia-sdk/client"
"github.com/Finschia/finschia-sdk/client/flags"
"github.com/Finschia/finschia-sdk/client/tx"
"github.com/Finschia/finschia-sdk/x/or/da/types"
)

const (
FlagCompression = "compression"
FlagL2GasLimit = "l2-gas-limit"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -19,5 +26,99 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(
CmdTxAppendCCBatch(),
CmdTxEnqueue(),
)

return cmd
}

func CmdTxAppendCCBatch() *cobra.Command {
cmd := &cobra.Command{
Use: "append-cc-batch [from_key_or_address] [rollup_name] [compressed_batch_bytes]",
Short: "append a batch of rollup transactions",
Long: `append a batch of rollup transactions.
Note, the '--from' flag is ignored as it is implied from [from_key_or_address].
When using '--dry-run' a key name cannot be used, only a bech32 address.`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Flags().Set(flags.FlagFrom, args[0])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

compressOpt, err := cmd.Flags().GetInt32(FlagCompression)
if err != nil {
return err
}
msg := &types.MsgAppendCCBatch{
FromAddress: clientCtx.GetFromAddress().String(),
RollupName: args[1],
Batch: types.CompressedCCBatch{
Data: []byte(args[2]),
Compression: types.CompressionOption(compressOpt),
},
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Int32(FlagCompression, 0, "compression algorithm to use for the batch")
return cmd
}

func CmdTxEnqueue() *cobra.Command {
cmd := &cobra.Command{
Use: "enqueue [from_key_or_address] [rollup_name] [tx_bytes]",
Short: "enqueue a rollup transaction on L1",
Long: `enqueue a rollup transaction on L1.
Note, the '--from' flag is ignored as it is implied from [from_key_or_address].
When using '--dry-run' a key name cannot be used, only a bech32 address.
`,
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Flags().Set(flags.FlagFrom, args[0])
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

l2gasLimit, err := cmd.Flags().GetUint64(FlagL2GasLimit)
if err != nil {
return err
}

msg := &types.MsgEnqueue{
FromAddress: clientCtx.GetFromAddress().String(),
RollupName: args[1],
GasLimit: l2gasLimit,
Txraw: []byte(args[2]),
}
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Uint64(FlagL2GasLimit, 0, "gas limit for the transaction on L2")
err := cmd.MarkFlagRequired(FlagL2GasLimit)
if err != nil {
return nil
}

return cmd
}
2 changes: 1 addition & 1 deletion x/or/da/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a AppModuleBasic) GetTxCmd() *cobra.Command {

// GetQueryCmd returns the root query command for the module. The subcommands of this root command are used by end-users to generate new queries to the subset of the state defined by the module
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd(types.StoreKey)
return cli.GetQueryCmd()
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit fa85f8a

Please sign in to comment.