Skip to content

Commit

Permalink
Add tx commands for modules execution, instance, ownership, process, …
Browse files Browse the repository at this point in the history
…runner and service
  • Loading branch information
NicolasMahe committed May 3, 2020
1 parent 4863ee9 commit f60d843
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 44 deletions.
73 changes: 71 additions & 2 deletions x/execution/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package cli

import (
"bufio"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/mesg-foundation/engine/x/execution/internal/types"
"github.com/spf13/cobra"
)
Expand All @@ -14,12 +20,75 @@ import (
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
executionTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
Short: fmt.Sprintf("%s transactions subcommands", strings.Title(types.ModuleName)),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

executionTxCmd.AddCommand(flags.PostCommands()...)
executionTxCmd.AddCommand(flags.PostCommands(
GetCmdCreate(cdc),
GetCmdUpdate(cdc),
)...)
return executionTxCmd
}

// GetCmdCreate is the command to create a execution.
func GetCmdCreate(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "create [definition]",
Short: "Creates a new execution",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

rawMsg := fmt.Sprintf(`{"type":"execution/create","value":%s}`, args[0])
var msg types.MsgCreate
if err := cdc.UnmarshalJSON([]byte(rawMsg), &msg); err != nil {
return err
}
if !msg.Signer.Empty() && !msg.Signer.Equals(cliCtx.FromAddress) {
return fmt.Errorf("the signer set in the definition is not the same as the from flag")
}
msg.Signer = cliCtx.FromAddress
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}

// GetCmdUpdate is the command to update a execution.
func GetCmdUpdate(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "update [definition]",
Short: "Updates an execution",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

rawMsg := fmt.Sprintf(`{"type":"execution/update","value":%s}`, args[0])
var msg types.MsgUpdate
if err := cdc.UnmarshalJSON([]byte(rawMsg), &msg); err != nil {
return err
}
if !msg.Executor.Empty() && !msg.Executor.Equals(cliCtx.FromAddress) {
return fmt.Errorf("the executor set in the definition is not the same as the from flag")
}
msg.Executor = cliCtx.FromAddress
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
16 changes: 1 addition & 15 deletions x/instance/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
package cli

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/mesg-foundation/engine/x/instance/internal/types"
"github.com/spf13/cobra"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
ownershipTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

ownershipTxCmd.AddCommand(flags.PostCommands()...)
return ownershipTxCmd
return nil
}
13 changes: 7 additions & 6 deletions x/ownership/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"bufio"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
Expand All @@ -20,7 +21,7 @@ import (
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
ownershipTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
Short: fmt.Sprintf("%s transactions subcommands", strings.Title(types.ModuleName)),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
Expand All @@ -35,26 +36,26 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
// GetCmdWithdraw is the CLI command for sending a Withdraw transaction
func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "withdraw-coins [resource] [amount]",
Short: "withdraw amount from resource address",
Use: "withdraw-coins [resourceHash] [amount]",
Short: "withdraw coins from a resource",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

h, err := hash.Decode(args[0])
resourceHash, err := hash.Decode(args[0])
if err != nil {
return err
}

msg := types.MsgWithdraw{
Owner: cliCtx.GetFromAddress(),
ResourceHash: h,
ResourceHash: resourceHash,
Amount: args[1],
}
if err = msg.ValidateBasic(); err != nil {
if err := msg.ValidateBasic(); err != nil {
return err
}

Expand Down
63 changes: 53 additions & 10 deletions x/process/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package cli

import (
"bufio"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/x/process/internal/types"
"github.com/spf13/cobra"
)
Expand All @@ -14,7 +21,7 @@ import (
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
processTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
Short: fmt.Sprintf("%s transactions subcommands", strings.Title(types.ModuleName)),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
Expand All @@ -28,26 +35,62 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
return processTxCmd
}

// GetCmdCreate is the command to create a process.
func GetCmdCreate(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "create [serviceHash] [key=val]...",
Use: "create [definition]",
Short: "Creates a new process",
Args: cobra.MinimumNArgs(1),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO:
return nil
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

rawMsg := fmt.Sprintf(`{"type":"process/create","value":%s}`, args[0])
var msg types.MsgCreate
if err := cdc.UnmarshalJSON([]byte(rawMsg), &msg); err != nil {
return err
}
if !msg.Owner.Empty() && !msg.Owner.Equals(cliCtx.FromAddress) {
return fmt.Errorf("the owner set in the definition is not the same as the from flag")
}
msg.Owner = cliCtx.FromAddress
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}

// GetCmdDelete is the command to delete a process.
func GetCmdDelete(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "create [serviceHash] [key=val]...",
Short: "Creates a new process",
Args: cobra.MinimumNArgs(1),
Use: "delete [processHash]",
Short: "Deletes a process",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO:
return nil
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

processHash, err := hash.Decode(args[0])
if err != nil {
return fmt.Errorf("arg processHash error: %w", err)
}

msg := types.MsgDelete{
Hash: processHash,
Owner: cliCtx.FromAddress,
}
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
69 changes: 60 additions & 9 deletions x/runner/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package cli

import (
"bufio"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/x/runner/internal/types"
"github.com/spf13/cobra"
)
Expand All @@ -14,7 +21,7 @@ import (
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
runnerTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("%s transactions subcommands", types.ModuleName),
Short: fmt.Sprintf("%s transactions subcommands", strings.Title(types.ModuleName)),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
Expand All @@ -28,26 +35,70 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
return runnerTxCmd
}

// GetCmdCreate is the command to create a runner.
func GetCmdCreate(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "create [runnerHash] [key=val]...",
Use: "create [serviceHash] [envHash]",
Short: "Creates a new runner",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO:
return nil
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

serviceHash, err := hash.Decode(args[0])
if err != nil {
return fmt.Errorf("arg serviceHash error: %w", err)
}

var envHash hash.Hash
if len(args) >= 2 && args[1] != "" {
if envHash, err = hash.Decode(args[1]); err != nil {
return fmt.Errorf("arg envHash error: %w", err)
}
}

msg := types.MsgCreate{
ServiceHash: serviceHash,
EnvHash: envHash,
Owner: cliCtx.FromAddress,
}
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}

// GetCmdDelete is the command to delete a runner.
func GetCmdDelete(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "delete [runnerHash] [key=val]...",
Short: "Creates a new runner",
Args: cobra.MinimumNArgs(1),
Use: "delete [runnerHash]",
Short: "Deletes a runner",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO:
return nil
inBuf := bufio.NewReader(cmd.InOrStdin())
cliCtx := context.NewCLIContext().WithCodec(cdc)

txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

runHash, err := hash.Decode(args[0])
if err != nil {
return fmt.Errorf("arg runHash error: %w", err)
}

msg := types.MsgDelete{
Hash: runHash,
Owner: cliCtx.FromAddress,
}
if err := msg.ValidateBasic(); err != nil {
return err
}

return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
},
}
}
Loading

0 comments on commit f60d843

Please sign in to comment.