replace basecli state presenters with cmds

This commit is contained in:
rigel rozanski 2017-06-14 05:02:42 -04:00
parent 7624c66f98
commit 25e7a79174
6 changed files with 111 additions and 28 deletions

View File

@ -17,22 +17,6 @@ import (
btypes "github.com/tendermint/basecoin/types"
)
type AccountPresenter struct{}
func (_ AccountPresenter) MakeKey(str string) ([]byte, error) {
res, err := hex.DecodeString(str)
if err == nil {
res = btypes.AccountKey(res)
}
return res, err
}
func (_ AccountPresenter) ParseData(raw []byte) (interface{}, error) {
var acc *btypes.Account
err := wire.ReadBinaryBytes(raw, &acc)
return acc, err
}
type BaseTxPresenter struct {
proofs.RawPresenter // this handles MakeKey as hex bytes
}
@ -65,7 +49,7 @@ func (m SendTxMaker) Flags() (*flag.FlagSet, interface{}) {
fs.String("to", "", "Destination address for the bits")
fs.String("amount", "", "Coins to send in the format <amt><coin>,<amt><coin>...")
fs.String("fee", "", "Coins for the transaction fee of the format <amt><coin>")
fs.String("fee", "0mycoin", "Coins for the transaction fee of the format <amt><coin>")
fs.Int64("gas", 0, "Amount of gas for this transaction")
fs.Int("sequence", -1, "Sequence number for this transaction")
return fs, &SendFlags{}

View File

@ -0,0 +1,55 @@
package commands
import (
"encoding/hex"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/light-client/commands"
proofcmd "github.com/tendermint/light-client/commands/proofs"
"github.com/tendermint/light-client/proofs"
bccmd "github.com/tendermint/basecoin/cmd/commands"
btypes "github.com/tendermint/basecoin/types"
)
func init() {
//first modify the full node account query command for the light client
bccmd.AccountCmd.RunE = accountCmd
proofcmd.RootCmd.AddCommand(bccmd.AccountCmd)
}
func accountCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("account command requires an argument ([address])") //never stack trace
}
addrHex := StripHex(args[0])
// convert destination address to bytes
addr, err := hex.DecodeString(addrHex)
if err != nil {
return errors.Errorf("Account address (%v) is invalid hex: %v\n", addrHex, err)
}
// get the proof -> this will be used by all prover commands
height := proofcmd.GetHeight()
node := commands.GetNode()
prover := proofs.NewAppProver(node)
key := btypes.AccountKey(addr)
proof, err := proofcmd.GetProof(node, prover, key, height)
if err != nil {
return err
}
var acc *btypes.Account
err = wire.ReadBinaryBytes(proof.Data(), &acc)
if err != nil {
return err
}
return proofcmd.OutputProof(&acc, proof.BlockHeight())
}

View File

@ -0,0 +1,44 @@
package counter
import (
"github.com/spf13/cobra"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/light-client/commands"
proofcmd "github.com/tendermint/light-client/commands/proofs"
"github.com/tendermint/light-client/proofs"
"github.com/tendermint/basecoin/plugins/counter"
)
var CounterTxCmd = &cobra.Command{
Use: "counter",
Short: "query counter state",
RunE: counterTxCmd,
}
func init() {
//first modify the full node account query command for the light client
proofcmd.RootCmd.AddCommand(CounterTxCmd)
}
func counterTxCmd(cmd *cobra.Command, args []string) error {
// get the proof -> this will be used by all prover commands
height := proofcmd.GetHeight()
node := commands.GetNode()
prover := proofs.NewAppProver(node)
key := counter.New().StateKey()
proof, err := proofcmd.GetProof(node, prover, key, height)
if err != nil {
return err
}
var cp counter.CounterPluginState
err = wire.ReadBinaryBytes(proof.Data(), &cp)
if err != nil {
return err
}
return proofcmd.OutputProof(cp, proof.BlockHeight())
}

View File

@ -33,9 +33,9 @@ func main() {
commands.AddBasicFlags(BaseCli)
//initialize proofs and txs
proofs.StatePresenters.Register("account", bcmd.AccountPresenter{})
//proofs.StatePresenters.Register("account", bcmd.AccountPresenter{})
proofs.TxPresenters.Register("base", bcmd.BaseTxPresenter{})
proofs.StatePresenters.Register("counter", bcount.CounterPresenter{})
//proofs.StatePresenters.Register("counter", bcount.CounterPresenter{})
txs.Register("send", bcmd.SendTxMaker{})
txs.Register("counter", bcount.CounterTxMaker{})

View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/basecoin/cmd/commands"
@ -19,16 +20,15 @@ var CounterTxCmd = &cobra.Command{
RunE: counterTxCmd,
}
//flags
var (
validFlag bool
countFeeFlag string
const (
flagValid = "valid"
flagCountFee = "countfee"
)
func init() {
CounterTxCmd.Flags().BoolVar(&validFlag, "valid", false, "Set valid field in CounterTx")
CounterTxCmd.Flags().StringVar(&countFeeFlag, "countfee", "", "Coins for the counter fee of the format <amt><coin>")
CounterTxCmd.Flags().Bool(flagValid, false, "Set valid field in CounterTx")
CounterTxCmd.Flags().String(flagCountFee, "", "Coins for the counter fee of the format <amt><coin>")
commands.RegisterTxSubcommand(CounterTxCmd)
commands.RegisterStartPlugin("counter", func() types.Plugin { return counter.New() })
@ -36,13 +36,13 @@ func init() {
func counterTxCmd(cmd *cobra.Command, args []string) error {
countFee, err := types.ParseCoins(countFeeFlag)
countFee, err := types.ParseCoins(viper.GetString(flagCountFee))
if err != nil {
return err
}
counterTx := counter.CounterTx{
Valid: validFlag,
Valid: viper.GetBool(flagValid),
Fee: countFee,
}

2
glide.lock generated
View File

@ -127,7 +127,7 @@ imports:
- data
- data/base58
- name: github.com/tendermint/light-client
version: 424905d3813586ce7e64e18690676250a0595ad4
version: d993d8a9ba6cb51f05faf6e527016288e38c2853
subpackages:
- certifiers
- certifiers/client