Clean up queries, part 1

This commit is contained in:
Ethan Frey 2017-06-15 12:27:55 +02:00
parent 863e3725fe
commit 65837cf952
3 changed files with 35 additions and 42 deletions

View File

@ -1,10 +1,6 @@
package commands
import (
"encoding/hex"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
wire "github.com/tendermint/go-wire"
@ -12,44 +8,36 @@ import (
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)
var AccountQueryCmd = &cobra.Command{
Use: "account [address]",
Short: "Get details of an account, with proof",
RunE: doAccountQuery,
}
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)
func doAccountQuery(cmd *cobra.Command, args []string) error {
height := proofcmd.GetHeight()
addr, err := proofcmd.ParseHexKey(args, "address")
if err != nil {
return errors.Errorf("Account address (%v) is invalid hex: %v\n", addrHex, err)
return err
}
key := btypes.AccountKey(addr)
// 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
acc := new(btypes.Account)
err = wire.ReadBinaryBytes(proof.Data(), &acc)
if err != nil {
return err
}
return proofcmd.OutputProof(&acc, proof.BlockHeight())
return proofcmd.OutputProof(acc, proof.BlockHeight())
}

View File

@ -11,24 +11,18 @@ import (
"github.com/tendermint/basecoin/plugins/counter"
)
var CounterTxCmd = &cobra.Command{
var CounterQueryCmd = &cobra.Command{
Use: "counter",
Short: "query counter state",
RunE: counterTxCmd,
Short: "Query counter state, with proof",
RunE: doCounterQuery,
}
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
func doCounterQuery(cmd *cobra.Command, args []string) error {
height := proofcmd.GetHeight()
key := counter.New().StateKey()
node := commands.GetNode()
prover := proofs.NewAppProver(node)
key := counter.New().StateKey()
proof, err := proofcmd.GetProof(node, prover, key, height)
if err != nil {
return err

View File

@ -32,8 +32,19 @@ tmcli to work for any custom abci app.
func main() {
commands.AddBasicFlags(BaseCli)
//initialize proofs and txs
//proofs.StatePresenters.Register("account", bcmd.AccountPresenter{})
// prepare queries
pr := proofs.RootCmd
// these are default parsers, but you optional in your app
pr.AddCommand(proofs.TxCmd)
pr.AddCommand(proofs.KeyCmd)
pr.AddCommand(bcmd.AccountQueryCmd)
pr.AddCommand(bcount.CounterQueryCmd)
// here is how you would add the custom txs... but don't really add demo in your app
tr := txs.RootCmd
// tr.AddCommand(txs.DemoCmd)
// TODO
proofs.TxPresenters.Register("base", bcmd.BaseTxPresenter{})
//proofs.StatePresenters.Register("counter", bcount.CounterPresenter{})
@ -42,13 +53,13 @@ func main() {
// set up the various commands to use
BaseCli.AddCommand(
keycmd.RootCmd,
commands.InitCmd,
commands.ResetCmd,
keycmd.RootCmd,
seeds.RootCmd,
proofs.RootCmd,
txs.RootCmd,
proxy.RootCmd,
)
pr,
tr,
proxy.RootCmd)
cmd := cli.PrepareMainCmd(BaseCli, "BC", os.ExpandEnv("$HOME/.basecli"))
cmd.Execute()