From 3fbd282f2ed2c1091750ada28f9ace52e4a0e8c6 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 30 Jan 2017 15:56:47 +0100 Subject: [PATCH] Allow registering plugin subcommand to apptx --- cmd/basecoin/commands/counter.go | 49 ++++++++++++++++++++++++++++++++ cmd/basecoin/commands/ibc.go | 8 +++--- cmd/basecoin/commands/tx.go | 49 +++++++------------------------- 3 files changed, 63 insertions(+), 43 deletions(-) create mode 100644 cmd/basecoin/commands/counter.go diff --git a/cmd/basecoin/commands/counter.go b/cmd/basecoin/commands/counter.go new file mode 100644 index 0000000000..d05d08c424 --- /dev/null +++ b/cmd/basecoin/commands/counter.go @@ -0,0 +1,49 @@ +package commands + +import ( + "fmt" + + "github.com/tendermint/basecoin/plugins/counter" + "github.com/tendermint/basecoin/types" + wire "github.com/tendermint/go-wire" + "github.com/urfave/cli" +) + +var ( + CounterTxCmd = cli.Command{ + Name: "counter", + Usage: "Craft a transaction to the counter plugin", + Action: func(c *cli.Context) error { + return cmdCounterTx(c) + }, + Flags: []cli.Flag{ + ValidFlag, + }, + } +) + +func init() { + RegisterPlugin(CounterTxCmd) +} + +func cmdCounterTx(c *cli.Context) error { + valid := c.Bool("valid") + parent := c.Parent() + + counterTx := counter.CounterTx{ + Valid: valid, + Fee: types.Coins{ + { + Denom: parent.String("coin"), + Amount: int64(parent.Int("fee")), + }, + }, + } + + fmt.Println("CounterTx:", string(wire.JSONBytes(counterTx))) + + data := wire.BinaryBytes(counterTx) + name := "counter" + + return AppTx(parent, name, data) +} diff --git a/cmd/basecoin/commands/ibc.go b/cmd/basecoin/commands/ibc.go index 68ad4feba9..bdb7ccc0e7 100644 --- a/cmd/basecoin/commands/ibc.go +++ b/cmd/basecoin/commands/ibc.go @@ -132,7 +132,7 @@ func cmdIBCRegisterTx(c *cli.Context) error { }{ibcTx})) name := "IBC" - return appTx(parent, name, data) + return AppTx(parent, name, data) } func cmdIBCUpdateTx(c *cli.Context) error { @@ -167,7 +167,7 @@ func cmdIBCUpdateTx(c *cli.Context) error { }{ibcTx})) name := "IBC" - return appTx(c.Parent(), name, data) + return AppTx(c.Parent(), name, data) } func cmdIBCPacketCreateTx(c *cli.Context) error { @@ -200,7 +200,7 @@ func cmdIBCPacketCreateTx(c *cli.Context) error { ibc.IBCTx `json:"unwrap"` }{ibcTx})) - return appTx(c.Parent().Parent(), "IBC", data) + return AppTx(c.Parent().Parent(), "IBC", data) } func cmdIBCPacketPostTx(c *cli.Context) error { @@ -238,7 +238,7 @@ func cmdIBCPacketPostTx(c *cli.Context) error { ibc.IBCTx `json:"unwrap"` }{ibcTx})) - return appTx(c.Parent().Parent(), "IBC", data) + return AppTx(c.Parent().Parent(), "IBC", data) } func getIBCSequence(c *cli.Context) (uint64, error) { diff --git a/cmd/basecoin/commands/tx.go b/cmd/basecoin/commands/tx.go index 239d2c842d..745c450323 100644 --- a/cmd/basecoin/commands/tx.go +++ b/cmd/basecoin/commands/tx.go @@ -7,7 +7,6 @@ import ( "github.com/urfave/cli" - "github.com/tendermint/basecoin/plugins/counter" "github.com/tendermint/basecoin/types" cmn "github.com/tendermint/go-common" @@ -63,23 +62,17 @@ var ( NameFlag, DataFlag, }, - Subcommands: []cli.Command{ - CounterTxCmd, - }, - } - - CounterTxCmd = cli.Command{ - Name: "counter", - Usage: "Craft a transaction to the counter plugin", - Action: func(c *cli.Context) error { - return cmdCounterTx(c) - }, - Flags: []cli.Flag{ - ValidFlag, - }, + // Subcommands are dynamically registered with plugins as needed + Subcommands: []cli.Command{}, } ) +// RegisterPlugin is used to add another subcommand and create a custom +// apptx encoding. Look at counter.go for an example +func RegisterPlugin(cmd cli.Command) { + AppTxCmd.Subcommands = append(AppTxCmd.Subcommands, cmd) +} + func cmdSendTx(c *cli.Context) error { toHex := c.String("to") fromFile := c.String("from") @@ -136,10 +129,10 @@ func cmdAppTx(c *cli.Context) error { data, _ = hex.DecodeString(dataString) } name := c.String("name") - return appTx(c, name, data) + return AppTx(c, name, data) } -func appTx(c *cli.Context, name string, data []byte) error { +func AppTx(c *cli.Context, name string, data []byte) error { fromFile := c.String("from") amount := int64(c.Int("amount")) coin := c.String("coin") @@ -174,28 +167,6 @@ func appTx(c *cli.Context, name string, data []byte) error { return nil } -func cmdCounterTx(c *cli.Context) error { - valid := c.Bool("valid") - parent := c.Parent() - - counterTx := counter.CounterTx{ - Valid: valid, - Fee: types.Coins{ - { - Denom: parent.String("coin"), - Amount: int64(parent.Int("fee")), - }, - }, - } - - fmt.Println("CounterTx:", string(wire.JSONBytes(counterTx))) - - data := wire.BinaryBytes(counterTx) - name := "counter" - - return appTx(parent, name, data) -} - // broadcast the transaction to tendermint func broadcastTx(c *cli.Context, tx types.Tx) ([]byte, error) { tmResult := new(ctypes.TMResult)