Skip to content

Commit

Permalink
restructure to remove deps on example
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Mar 1, 2018
1 parent 5bc5135 commit 3be4639
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 118 deletions.
13 changes: 10 additions & 3 deletions client/tx/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package tx

import (
"github.com/spf13/cobra"
wire "github.com/tendermint/go-wire"
)

// type used to pass around the provided cdc
type commander struct {
cdc *wire.Codec
}

// AddCommands adds a number of tx-query related subcommands
func AddCommands(cmd *cobra.Command) {
func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
cmdr := commander{cdc}
cmd.AddCommand(
txSearchCommand(),
txCommand(),
SearchTxCmd(cmdr),
QueryTxCmd(cmdr),
)
}
20 changes: 10 additions & 10 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
wire "github.com/tendermint/go-wire"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)

Expand All @@ -18,21 +18,22 @@ const (
flagAny = "any"
)

func txSearchCommand() *cobra.Command {
// default client command to search through tagged transactions
func SearchTxCmd(cmdr commander) *cobra.Command {
cmd := &cobra.Command{
Use: "txs",
Short: "Search for all transactions that match the given tags",
RunE: searchTx,
RunE: cmdr.searchTxCmd,
}
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")
// TODO: change this to false when we can
// TODO: change this to false once proofs built in
cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses")
cmd.Flags().StringSlice(flagTags, nil, "Tags that must match (may provide multiple)")
cmd.Flags().Bool(flagAny, false, "Return transactions that match ANY tag, rather than ALL")
return cmd
}

func searchTx(cmd *cobra.Command, args []string) error {
func (c commander) searchTxCmd(cmd *cobra.Command, args []string) error {
tags := viper.GetStringSlice(flagTags)
if len(tags) == 0 {
return errors.New("Must declare at least one tag to search")
Expand All @@ -52,13 +53,12 @@ func searchTx(cmd *cobra.Command, args []string) error {
return err
}

info, err := formatTxResults(res)
info, err := formatTxResults(c.cdc, res)
if err != nil {
return err
}

cdc := app.MakeTxCodec()
output, err := cdc.MarshalJSON(info)
output, err := c.cdc.MarshalJSON(info)
if err != nil {
return err
}
Expand All @@ -67,11 +67,11 @@ func searchTx(cmd *cobra.Command, args []string) error {
return nil
}

func formatTxResults(res []*ctypes.ResultTx) ([]txInfo, error) {
func formatTxResults(cdc *wire.Codec, res []*ctypes.ResultTx) ([]txInfo, error) {
var err error
out := make([]txInfo, len(res))
for i := range res {
out[i], err = formatTxResult(res[i])
out[i], err = formatTxResult(cdc, res[i])
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/examples/basecoin/app" // XXX: not good
"github.com/cosmos/cosmos-sdk/client" // XXX: not good
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types"
wire "github.com/tendermint/go-wire"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)

func txCommand() *cobra.Command {
// Get the default command for a tx query
func QueryTxCmd(cmdr commander) *cobra.Command {
cmd := &cobra.Command{
Use: "tx <hash>",
Use: "tx [hash]",
Short: "Matches this txhash over all committed blocks",
RunE: queryTx,
RunE: cmdr.queryTxCmd,
}
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to")
// TODO: change this to false when we can
Expand All @@ -29,7 +30,7 @@ func txCommand() *cobra.Command {
}

// command to query for a transaction
func queryTx(cmd *cobra.Command, args []string) error {
func (c commander) queryTxCmd(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 @@ -52,7 +53,7 @@ func queryTx(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
info, err := formatTxResult(res)
info, err := formatTxResult(c.cdc, res)
if err != nil {
return err
}
Expand All @@ -66,9 +67,9 @@ func queryTx(cmd *cobra.Command, args []string) error {
return nil
}

func formatTxResult(res *ctypes.ResultTx) (txInfo, error) {
func formatTxResult(cdc *wire.Codec, res *ctypes.ResultTx) (txInfo, error) {
// TODO: verify the proof if requested
tx, err := parseTx(res.Tx)
tx, err := parseTx(cdc, res.Tx)
if err != nil {
return txInfo{}, err
}
Expand All @@ -88,9 +89,8 @@ type txInfo struct {
Result abci.ResponseDeliverTx `json:"result"`
}

func parseTx(txBytes []byte) (sdk.Tx, error) {
func parseTx(cdc *wire.Codec, txBytes []byte) (sdk.Tx, error) {
var tx sdk.StdTx
cdc := app.MakeTxCodec()
err := cdc.UnmarshalBinary(txBytes, &tx)
if err != nil {
return nil, err
Expand Down
9 changes: 6 additions & 3 deletions examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
Expand Down Expand Up @@ -39,7 +40,7 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
// create your application object
var app = &BasecoinApp{
BaseApp: bam.NewBaseApp(appName, logger, db),
cdc: MakeTxCodec(),
cdc: MakeCodec(),
capKeyMainStore: sdk.NewKVStoreKey("main"),
capKeyIBCStore: sdk.NewKVStoreKey("ibc"),
}
Expand Down Expand Up @@ -70,9 +71,11 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
}

// custom tx codec
func MakeTxCodec() *wire.Codec {
func MakeCodec() *wire.Codec {
cdc := wire.NewCodec()
bank.RegisterWire(cdc) // Register bank.[SendMsg,IssueMsg] types.
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
bank.RegisterWire(cdc) // Register bank.[SendMsg,IssueMsg] types.
crypto.RegisterWire(cdc) // Register crypto.[PubKey,PrivKey,Signature] types.
return cdc
}

Expand Down
2 changes: 1 addition & 1 deletion examples/basecoin/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestSendMsg(t *testing.T) {
}})

// just marshal/unmarshal!
cdc := MakeTxCodec()
cdc := MakeCodec()
txBytes, err := cdc.MarshalBinary(tx)
require.NoError(t, err)

Expand Down
13 changes: 10 additions & 3 deletions examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import (
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/commands"

"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
)

// gaiacliCmd is the entry point for this binary
Expand All @@ -33,20 +37,23 @@ func main() {
// disable sorting
cobra.EnableCommandSorting = false

// get the codec
cdc := app.MakeCodec()

// add standard rpc, and tx commands
rpc.AddCommands(basecliCmd)
basecliCmd.AddCommand(client.LineBreak)
tx.AddCommands(basecliCmd)
tx.AddCommands(basecliCmd, cdc)
basecliCmd.AddCommand(client.LineBreak)

// add query/post commands (custom to binary)
basecliCmd.AddCommand(
client.GetCommands(
bankcmd.GetAccountCmd("main"),
authcmd.GetAccountCmd("main", cdc, types.GetParseAccount(cdc)),
)...)
basecliCmd.AddCommand(
client.PostCommands(
bankcmd.SendTxCommand(),
bankcmd.SendTxCmd(cdc),
)...)

// add proxy, version and key info
Expand Down
10 changes: 10 additions & 0 deletions examples/basecoin/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
crypto "github.com/tendermint/go-crypto"
wire "github.com/tendermint/go-wire"
)

var _ sdk.Account = (*AppAccount)(nil)
Expand All @@ -22,6 +23,15 @@ type AppAccount struct {
func (acc AppAccount) GetName() string { return acc.Name }
func (acc *AppAccount) SetName(name string) { acc.Name = name }

// Get the ParseAccount function for the custom AppAccount
func GetParseAccount(cdc *wire.Codec) sdk.ParseAccount {
return func(accBytes []byte) (res sdk.Account, err error) {
acct := new(AppAccount)
err = cdc.UnmarshalBinary(accBytes, acct)
return acct, err
}
}

//___________________________________________________________________________________

// State to Unmarshal
Expand Down
3 changes: 3 additions & 0 deletions types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ type AccountMapper interface {
GetAccount(ctx Context, addr crypto.Address) Account
SetAccount(ctx Context, acc Account)
}

// Application function variable used to unmarshal account
type ParseAccount func([]byte) (Account, error)
82 changes: 82 additions & 0 deletions x/auth/commands/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package commands

import (
"encoding/hex"
"encoding/json"
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"

crypto "github.com/tendermint/go-crypto"
wire "github.com/tendermint/go-wire"

"github.com/cosmos/cosmos-sdk/client" // XXX: not good
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)

// GetAccountCmd for the auth.BaseAccount type
func GetAccountCmdDefault(storeName string, cdc *wire.Codec) *cobra.Command {
return GetAccountCmd(storeName, cdc, getParseAccount(cdc))
}

func getParseAccount(cdc *wire.Codec) sdk.ParseAccount {
return func(accBytes []byte) (sdk.Account, error) {
acct := new(auth.BaseAccount)
err := cdc.UnmarshalBinary(accBytes, acct)
return acct, err
}
}

// GetAccountCmd returns a query account that will display the
// state of the account at a given address
func GetAccountCmd(storeName string, cdc *wire.Codec, parser sdk.ParseAccount) *cobra.Command {
cmdr := commander{
storeName,
cdc,
parser,
}
return &cobra.Command{
Use: "account <address>",
Short: "Query account balance",
RunE: cmdr.getAccountCmd,
}
}

type commander struct {
storeName string
cdc *wire.Codec
parser sdk.ParseAccount
}

func (c commander) getAccountCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 || len(args[0]) == 0 {
return errors.New("You must provide an account name")
}

// find the key to look up the account
addr := args[0]
bz, err := hex.DecodeString(addr)
if err != nil {
return err
}
key := crypto.Address(bz)

res, err := client.Query(key, c.storeName)

// parse out the value
account, err := c.parser(res)
if err != nil {
return err
}

// print out whole account
output, err := json.MarshalIndent(account, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))

return nil
}
Loading

0 comments on commit 3be4639

Please sign in to comment.