From 65f27f2daa24e017ced272859dd565ff8959173a Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 28 Feb 2018 23:26:39 +0000 Subject: [PATCH] basecli refactor --- client/flags.go | 1 + client/helpers.go | 71 ++++++++++++++++++++++++++++++++++++++ client/keys/add.go | 3 +- client/node.go | 8 ----- client/rpc/block.go | 7 ++-- client/rpc/status.go | 7 ++-- client/rpc/validators.go | 7 ++-- client/tx/search.go | 7 ++-- client/tx/tx.go | 8 ++--- server/init.go | 2 +- x/bank/commands/account.go | 50 ++++++++++----------------- x/bank/commands/sendtx.go | 32 ++++++----------- 12 files changed, 123 insertions(+), 80 deletions(-) create mode 100644 client/helpers.go delete mode 100644 client/node.go diff --git a/client/flags.go b/client/flags.go index 1f55c29217..843cb52d1e 100644 --- a/client/flags.go +++ b/client/flags.go @@ -2,6 +2,7 @@ package client import "github.com/spf13/cobra" +// nolint const ( FlagChainID = "chain-id" FlagNode = "node" diff --git a/client/helpers.go b/client/helpers.go new file mode 100644 index 0000000000..10ffcc88e6 --- /dev/null +++ b/client/helpers.go @@ -0,0 +1,71 @@ +package client + +import ( + "fmt" + + "github.com/pkg/errors" + "github.com/spf13/viper" + + rpcclient "github.com/tendermint/tendermint/rpc/client" + ctypes "github.com/tendermint/tendermint/rpc/core/types" + cmn "github.com/tendermint/tmlibs/common" +) + +// GetNode prepares a simple rpc.Client from the flags +func GetNode() (rpcclient.Client, error) { + uri := viper.GetString(FlagNode) + if uri == "" { + return nil, errors.New("Must define node using --node") + } + return rpcclient.NewHTTP(uri, "/websocket"), nil +} + +// Broadcast the transaction bytes to Tendermint +func BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) { + + node, err := GetNode() + if err != nil { + return nil, err + } + + res, err := node.BroadcastTxCommit(tx) + if err != nil { + return res, err + } + + if res.CheckTx.Code != uint32(0) { + return res, errors.Errorf("CheckTx failed: (%d) %s", + res.CheckTx.Code, + res.CheckTx.Log) + } + if res.DeliverTx.Code != uint32(0) { + return res, errors.Errorf("DeliverTx failed: (%d) %s", + res.DeliverTx.Code, + res.DeliverTx.Log) + } + return res, err +} + +// Query from Tendermint with the provided key and storename +func Query(key cmn.HexBytes, storeName string) (res []byte, err error) { + + path := fmt.Sprintf("/%s/key", storeName) + node, err := GetNode() + if err != nil { + return res, err + } + + opts := rpcclient.ABCIQueryOptions{ + Height: viper.GetInt64(FlagHeight), + Trusted: viper.GetBool(FlagTrustNode), + } + result, err := node.ABCIQueryWithOptions(path, key, opts) + if err != nil { + return res, err + } + resp := result.Response + if resp.Code != uint32(0) { + return res, errors.Errorf("Query failed: (%d) %s", resp.Code, resp.Log) + } + return resp.Value, nil +} diff --git a/client/keys/add.go b/client/keys/add.go index 40bb0d103e..7472dce27f 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -102,7 +102,8 @@ func printCreate(info keys.Info, seed string) { // print seed unless requested not to. if !viper.GetBool(flagNoBackup) { fmt.Println("**Important** write this seed phrase in a safe place.") - fmt.Println("It is the only way to recover your account if you ever forget your password.\n") + fmt.Println("It is the only way to recover your account if you ever forget your password.") + fmt.Println() fmt.Println(seed) } case "json": diff --git a/client/node.go b/client/node.go deleted file mode 100644 index 5328f49d84..0000000000 --- a/client/node.go +++ /dev/null @@ -1,8 +0,0 @@ -package client - -import rpcclient "github.com/tendermint/tendermint/rpc/client" - -// GetNode prepares a simple rpc.Client from the flags -func GetNode(uri string) rpcclient.Client { - return rpcclient.NewHTTP(uri, "/websocket") -} diff --git a/client/rpc/block.go b/client/rpc/block.go index 3ae4e76156..c701061a20 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -5,7 +5,6 @@ import ( "strconv" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" tmwire "github.com/tendermint/tendermint/wire" @@ -43,8 +42,10 @@ func getBlock(cmd *cobra.Command, args []string) error { } // get the node - uri := viper.GetString(client.FlagNode) - node := client.GetNode(uri) + node, err := client.GetNode() + if err != nil { + return err + } // TODO: actually honor the --select flag! // header -> BlockchainInfo diff --git a/client/rpc/status.go b/client/rpc/status.go index 9436a240a4..c5888d99f5 100644 --- a/client/rpc/status.go +++ b/client/rpc/status.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" tmwire "github.com/tendermint/tendermint/wire" @@ -22,8 +21,10 @@ func statusCommand() *cobra.Command { func checkStatus(cmd *cobra.Command, args []string) error { // get the node - uri := viper.GetString(client.FlagNode) - node := client.GetNode(uri) + node, err := client.GetNode() + if err != nil { + return err + } res, err := node.Status() if err != nil { return err diff --git a/client/rpc/validators.go b/client/rpc/validators.go index cb554e7c6b..8eebda8dd6 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -5,7 +5,6 @@ import ( "strconv" "github.com/spf13/cobra" - "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" tmwire "github.com/tendermint/tendermint/wire" @@ -38,8 +37,10 @@ func getValidators(cmd *cobra.Command, args []string) error { } // get the node - uri := viper.GetString(client.FlagNode) - node := client.GetNode(uri) + node, err := client.GetNode() + if err != nil { + return err + } res, err := node.Validators(height) if err != nil { diff --git a/client/tx/search.go b/client/tx/search.go index 51256470a3..adb3df32a4 100644 --- a/client/tx/search.go +++ b/client/tx/search.go @@ -41,11 +41,10 @@ func searchTx(cmd *cobra.Command, args []string) error { query := strings.Join(tags, " AND ") // get the node - uri := viper.GetString(client.FlagNode) - if uri == "" { - return errors.New("Must define which node to query with --node") + node, err := client.GetNode() + if err != nil { + return err } - node := client.GetNode(uri) prove := !viper.GetBool(client.FlagTrustNode) res, err := node.TxSearch(query, prove) diff --git a/client/tx/tx.go b/client/tx/tx.go index 98d72b84d7..d79c341e60 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -28,6 +28,7 @@ func txCommand() *cobra.Command { return cmd } +// command to query for a transaction func queryTx(cmd *cobra.Command, args []string) error { if len(args) != 1 || len(args[0]) == 0 { return errors.New("You must provide a tx hash") @@ -41,11 +42,10 @@ func queryTx(cmd *cobra.Command, args []string) error { } // get the node - uri := viper.GetString(client.FlagNode) - if uri == "" { - return errors.New("Must define which node to query with --node") + node, err := client.GetNode() + if err != nil { + return err } - node := client.GetNode(uri) prove := !viper.GetBool(client.FlagTrustNode) res, err := node.Tx(hash, prove) diff --git a/server/init.go b/server/init.go index a56ad5c37d..4dc7245873 100644 --- a/server/init.go +++ b/server/init.go @@ -178,5 +178,5 @@ func GetGenesisJSON(pubkey, chainID, denom, addr string, options string) string "plugin_options": [ "coin/issuer", {"app": "sigs", "addr": "%s"}%s ] -}`, chainID, pubkey, addr, denom, addr, options) +}`, addr, denom, addr, options) } diff --git a/x/bank/commands/account.go b/x/bank/commands/account.go index 2f7f9132ef..ebe76e3edd 100644 --- a/x/bank/commands/account.go +++ b/x/bank/commands/account.go @@ -7,33 +7,37 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" - "github.com/spf13/viper" crypto "github.com/tendermint/go-crypto" - rpcclient "github.com/tendermint/tendermint/rpc/client" + wire "github.com/tendermint/go-wire" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/examples/basecoin/app" // XXX: not good "github.com/cosmos/cosmos-sdk/examples/basecoin/types" // XXX: not good + + "github.com/cosmos/cosmos-sdk/x/bank" ) // GetAccountCmd returns a query account that will display the // state of the account at a given address func GetAccountCmd(storeName string) *cobra.Command { - cmd := acctCmd{storeName} - return &cobra.Command{ Use: "account
", Short: "Query account balance", - RunE: cmd.get, + RunE: newRunner(storeName).cmd, } } -type acctCmd struct { +type runner struct { storeName string } -func (a acctCmd) get(cmd *cobra.Command, args []string) error { +func newRunner(storeName string) runner { + return runner{ + storeName: storeName, + } +} + +func (r runner) cmd(cmd *cobra.Command, args []string) error { if len(args) != 1 || len(args[0]) == 0 { return errors.New("You must provide an account name") } @@ -45,41 +49,25 @@ func (a acctCmd) get(cmd *cobra.Command, args []string) error { return err } key := crypto.Address(bz) - path := fmt.Sprintf("/%s/key", a.storeName) - uri := viper.GetString(client.FlagNode) - if uri == "" { - return errors.New("Must define which node to query with --node") - } - node := client.GetNode(uri) - - opts := rpcclient.ABCIQueryOptions{ - Height: viper.GetInt64(client.FlagHeight), - Trusted: viper.GetBool(client.FlagTrustNode), - } - result, err := node.ABCIQueryWithOptions(path, key, opts) - if err != nil { - return err - } - resp := result.Response - if resp.Code != uint32(0) { - return errors.Errorf("Query failed: (%d) %s", resp.Code, resp.Log) - } + res, err := client.Query(key, r.storeName) // parse out the value acct := new(types.AppAccount) - cdc := app.MakeTxCodec() - err = cdc.UnmarshalBinary(resp.Value, acct) + cdc := wire.NewCodec() + bank.RegisterWire(cdc) + + err = cdc.UnmarshalBinary(res, acct) if err != nil { return err } - // print out whole account or just coins? + // print out whole account output, err := json.MarshalIndent(acct, "", " ") - // output, err := json.MarshalIndent(acct.BaseAccount.Coins, "", " ") if err != nil { return err } fmt.Println(string(output)) + return nil } diff --git a/x/bank/commands/sendtx.go b/x/bank/commands/sendtx.go index b6d3ed31fc..65b8fb1963 100644 --- a/x/bank/commands/sendtx.go +++ b/x/bank/commands/sendtx.go @@ -8,12 +8,14 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + crypto "github.com/tendermint/go-crypto" + wire "github.com/tendermint/go-wire" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/keys" - "github.com/cosmos/cosmos-sdk/examples/basecoin/app" // XXX: not good sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/bank" - crypto "github.com/tendermint/go-crypto" ) const ( @@ -43,29 +45,12 @@ func sendTx(cmd *cobra.Command, args []string) error { return err } - uri := viper.GetString(client.FlagNode) - if uri == "" { - return errors.New("Must define which node to query with --node") - } - node := client.GetNode(uri) - - result, err := node.BroadcastTxCommit(txBytes) + res, err := client.BroadcastTx(txBytes) if err != nil { return err } - if result.CheckTx.Code != uint32(0) { - return errors.Errorf("CheckTx failed: (%d) %s", - result.CheckTx.Code, - result.CheckTx.Log) - } - if result.DeliverTx.Code != uint32(0) { - return errors.Errorf("DeliverTx failed: (%d) %s", - result.DeliverTx.Code, - result.DeliverTx.Log) - } - - fmt.Printf("Committed at block %d. Hash: %s\n", result.Height, result.Hash.String()) + fmt.Printf("Committed at block %d. Hash: %s\n", res.Height, res.Hash.String()) return nil } @@ -107,7 +92,9 @@ func buildTx() ([]byte, error) { // marshal bytes tx := sdk.NewStdTx(msg, sigs) - cdc := app.MakeTxCodec() + cdc := wire.NewCodec() + bank.RegisterWire(cdc) + txBytes, err := cdc.MarshalBinary(tx) if err != nil { return nil, err @@ -116,6 +103,7 @@ func buildTx() ([]byte, error) { } func buildMsg(from crypto.Address) (sdk.Msg, error) { + // parse coins amount := viper.GetString(flagAmount) coins, err := sdk.ParseCoins(amount)