Skip to content

Commit

Permalink
basecli refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Mar 1, 2018
1 parent 9494874 commit 65f27f2
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 80 deletions.
1 change: 1 addition & 0 deletions client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import "github.com/spf13/cobra"

// nolint
const (
FlagChainID = "chain-id"
FlagNode = "node"
Expand Down
71 changes: 71 additions & 0 deletions client/helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
8 changes: 0 additions & 8 deletions client/node.go

This file was deleted.

7 changes: 4 additions & 3 deletions client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions client/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
50 changes: 19 additions & 31 deletions x/bank/commands/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <address>",
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")
}
Expand All @@ -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
}
32 changes: 10 additions & 22 deletions x/bank/commands/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 65f27f2

Please sign in to comment.