2017-01-30 06:16:51 -08:00
|
|
|
package commands
|
2017-01-29 13:34:48 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
|
2017-01-29 18:41:37 -08:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2017-01-29 13:34:48 -08:00
|
|
|
"github.com/tendermint/basecoin/types"
|
|
|
|
|
2017-01-29 18:41:37 -08:00
|
|
|
abci "github.com/tendermint/abci/types"
|
2017-01-29 13:34:48 -08:00
|
|
|
cmn "github.com/tendermint/go-common"
|
|
|
|
client "github.com/tendermint/go-rpc/client"
|
|
|
|
"github.com/tendermint/go-wire"
|
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2017-01-29 18:41:37 -08:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2017-01-29 13:34:48 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Returns true for non-empty hex-string prefixed with "0x"
|
|
|
|
func isHex(s string) bool {
|
|
|
|
if len(s) > 2 && s[:2] == "0x" {
|
|
|
|
_, err := hex.DecodeString(s[2:])
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-30 08:16:00 -08:00
|
|
|
func StripHex(s string) string {
|
2017-01-29 13:34:48 -08:00
|
|
|
if isHex(s) {
|
|
|
|
return s[2:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-01-31 06:33:34 -08:00
|
|
|
func Query(tmAddr string, key []byte) (*abci.ResponseQuery, error) {
|
2017-01-29 13:34:48 -08:00
|
|
|
clientURI := client.NewClientURI(tmAddr)
|
|
|
|
tmResult := new(ctypes.TMResult)
|
|
|
|
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"path": "/key",
|
2017-01-29 18:41:37 -08:00
|
|
|
"data": key,
|
|
|
|
"prove": true,
|
2017-01-29 13:34:48 -08:00
|
|
|
}
|
|
|
|
_, err := clientURI.Call("abci_query", params, tmResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(cmn.Fmt("Error calling /abci_query: %v", err))
|
|
|
|
}
|
|
|
|
res := (*tmResult).(*ctypes.ResultABCIQuery)
|
|
|
|
if !res.Response.Code.IsOK() {
|
|
|
|
return nil, errors.New(cmn.Fmt("Query got non-zero exit code: %v. %s", res.Response.Code, res.Response.Log))
|
|
|
|
}
|
2017-01-29 18:41:37 -08:00
|
|
|
return &res.Response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the account by querying the app
|
|
|
|
func getAcc(tmAddr string, address []byte) (*types.Account, error) {
|
|
|
|
|
|
|
|
key := append([]byte("base/a/"), address...)
|
2017-01-31 06:33:34 -08:00
|
|
|
response, err := Query(tmAddr, key)
|
2017-01-29 18:41:37 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
accountBytes := response.Value
|
2017-01-29 13:34:48 -08:00
|
|
|
|
|
|
|
if len(accountBytes) == 0 {
|
2017-01-29 18:41:37 -08:00
|
|
|
return nil, errors.New(cmn.Fmt("Account bytes are empty for address: %X ", address))
|
2017-01-29 13:34:48 -08:00
|
|
|
}
|
2017-01-29 18:41:37 -08:00
|
|
|
|
2017-01-29 13:34:48 -08:00
|
|
|
var acc *types.Account
|
|
|
|
err = wire.ReadBinaryBytes(accountBytes, &acc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(cmn.Fmt("Error reading account %X error: %v",
|
|
|
|
accountBytes, err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc, nil
|
|
|
|
}
|
2017-01-29 18:41:37 -08:00
|
|
|
|
|
|
|
func getBlock(c *cli.Context, height int) (*tmtypes.Block, error) {
|
|
|
|
tmResult := new(ctypes.TMResult)
|
|
|
|
tmAddr := c.String("node")
|
|
|
|
clientURI := client.NewClientURI(tmAddr)
|
|
|
|
|
|
|
|
_, err := clientURI.Call("block", map[string]interface{}{"height": height}, tmResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(cmn.Fmt("Error on broadcast tx: %v", err))
|
|
|
|
}
|
|
|
|
res := (*tmResult).(*ctypes.ResultBlock)
|
|
|
|
return res.Block, nil
|
|
|
|
}
|