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
+11 -23
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())
}