Ripped out query, tx send, account, keys, and more

This commit is contained in:
Ethan Frey 2017-06-16 17:20:45 +02:00
parent f75ebca3ae
commit b15f882ff4
5 changed files with 0 additions and 367 deletions

View File

@ -20,11 +20,6 @@ func main() {
commands.StartCmd, commands.StartCmd,
commands.RelayCmd, commands.RelayCmd,
commands.TxCmd, commands.TxCmd,
commands.QueryCmd,
commands.KeyCmd,
commands.VerifyCmd,
commands.BlockCmd,
commands.AccountCmd,
commands.UnsafeResetAllCmd, commands.UnsafeResetAllCmd,
commands.VersionCmd, commands.VersionCmd,
) )

View File

@ -19,11 +19,6 @@ func RegisterStartPlugin(name string, newPlugin func() types.Plugin) {
plugins = append(plugins, plugin{name: name, newPlugin: newPlugin}) plugins = append(plugins, plugin{name: name, newPlugin: newPlugin})
} }
// Register a subcommand of QueryCmd for plugin specific query functionality
func RegisterQuerySubcommand(cmd *cobra.Command) {
QueryCmd.AddCommand(cmd)
}
// Register a subcommand of TxCmd to craft transactions for plugins // Register a subcommand of TxCmd to craft transactions for plugins
func RegisterTxSubcommand(cmd *cobra.Command) { func RegisterTxSubcommand(cmd *cobra.Command) {
TxCmd.AddCommand(cmd) TxCmd.AddCommand(cmd)

View File

@ -1,234 +0,0 @@
package commands
import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/merkleeyes/iavl"
"github.com/tendermint/tendermint/rpc/client"
tmtypes "github.com/tendermint/tendermint/types"
)
//commands
var (
QueryCmd = &cobra.Command{
Use: "query [key]",
Short: "Query the merkle tree",
RunE: queryCmd,
}
AccountCmd = &cobra.Command{
Use: "account [address]",
Short: "Get details of an account",
RunE: accountCmd,
}
BlockCmd = &cobra.Command{
Use: "block [height]",
Short: "Get the header and commit of a block",
RunE: blockCmd,
}
VerifyCmd = &cobra.Command{
Use: "verify",
Short: "Verify the IAVL proof",
RunE: verifyCmd,
}
)
//flags
var (
nodeFlag string
proofFlag string
keyFlag string
valueFlag string
rootFlag string
)
func init() {
commonFlags := []Flag2Register{
{&nodeFlag, "node", "tcp://localhost:46657", "Tendermint RPC address"},
}
verifyFlags := []Flag2Register{
{&proofFlag, "proof", "", "hex-encoded IAVL proof"},
{&keyFlag, "key", "", "key to the IAVL tree"},
{&valueFlag, "value", "", "value in the IAVL tree"},
{&rootFlag, "root", "", "root hash of the IAVL tree"},
}
RegisterFlags(QueryCmd, commonFlags)
RegisterFlags(AccountCmd, commonFlags)
RegisterFlags(BlockCmd, commonFlags)
RegisterFlags(VerifyCmd, verifyFlags)
}
func queryCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("query command requires an argument ([key])") //never stack trace
}
keyString := args[0]
key := []byte(keyString)
if isHex(keyString) {
// convert key to bytes
var err error
key, err = hex.DecodeString(StripHex(keyString))
if err != nil {
return errors.Errorf("Query key (%v) is invalid hex: %v\n", keyString, err)
}
}
resp, err := Query(nodeFlag, key)
if err != nil {
return errors.Errorf("Query returns error: %v\n", err)
}
if !resp.Code.IsOK() {
return errors.Errorf("Query for key (%v) returned non-zero code (%v): %v", keyString, resp.Code, resp.Log)
}
val := resp.Value
proof := resp.Proof
height := resp.Height
out, err := json.Marshal(struct {
Value data.Bytes `json:"value"`
Proof data.Bytes `json:"proof"`
Height uint64 `json:"height"`
}{val, proof, height})
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
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)
}
httpClient := client.NewHTTP(nodeFlag, "/websocket")
acc, err := getAccWithClient(httpClient, addr)
if err != nil {
return err
}
out, err := json.Marshal(acc)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
func blockCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("block command requires an argument ([height])") //never stack trace
}
heightString := args[0]
height, err := strconv.Atoi(heightString)
if err != nil {
return errors.Errorf("Height must be an int, got %v: %v\n", heightString, err)
}
header, commit, err := getHeaderAndCommit(nodeFlag, height)
if err != nil {
return err
}
out, err := json.Marshal(struct {
Hex BlockHex `json:"hex"`
JSON BlockJSON `json:"json"`
}{
BlockHex{
Header: wire.BinaryBytes(header),
Commit: wire.BinaryBytes(commit),
},
BlockJSON{
Header: header,
Commit: commit,
},
})
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}
type BlockHex struct {
Header data.Bytes `json:"header"`
Commit data.Bytes `json:"commit"`
}
type BlockJSON struct {
Header *tmtypes.Header `json:"header"`
Commit *tmtypes.Commit `json:"commit"`
}
func verifyCmd(cmd *cobra.Command, args []string) error {
keyString, valueString := keyFlag, valueFlag
var err error
key := []byte(keyString)
if isHex(keyString) {
key, err = hex.DecodeString(StripHex(keyString))
if err != nil {
return errors.Errorf("Key (%v) is invalid hex: %v\n", keyString, err)
}
}
value := []byte(valueString)
if isHex(valueString) {
value, err = hex.DecodeString(StripHex(valueString))
if err != nil {
return errors.Errorf("Value (%v) is invalid hex: %v\n", valueString, err)
}
}
root, err := hex.DecodeString(StripHex(rootFlag))
if err != nil {
return errors.Errorf("Root (%v) is invalid hex: %v\n", rootFlag, err)
}
proofBytes, err := hex.DecodeString(StripHex(proofFlag))
if err != nil {
return errors.Errorf("Proof (%v) is invalid hex: %v\n", proofFlag, err)
}
proof, err := iavl.ReadProof(proofBytes)
if err != nil {
return errors.Errorf("Error unmarshalling proof: %v\n", err)
}
if proof.Verify(key, value, root) {
fmt.Println("OK")
} else {
return errors.New("Proof does not verify")
}
return nil
}

View File

@ -1,9 +1,7 @@
package commands package commands
import ( import (
"encoding/hex"
"fmt" "fmt"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -20,18 +18,6 @@ var (
Use: "tx", Use: "tx",
Short: "Create, sign, and broadcast a transaction", Short: "Create, sign, and broadcast a transaction",
} }
SendTxCmd = &cobra.Command{
Use: "send",
Short: "A SendTx transaction, for sending tokens around",
RunE: sendTxCmd,
}
AppTxCmd = &cobra.Command{
Use: "app",
Short: "An AppTx transaction, for sending raw data to plugins",
RunE: appTxCmd,
}
) )
var ( var (
@ -43,11 +29,6 @@ var (
gasFlag int gasFlag int
feeFlag string feeFlag string
chainIDFlag string chainIDFlag string
//non-persistent flags
toFlag string
dataFlag string
nameFlag string
) )
func init() { func init() {
@ -62,106 +43,7 @@ func init() {
{&feeFlag, "fee", "0coin", "Coins for the transaction fee of the format <amt><coin>"}, {&feeFlag, "fee", "0coin", "Coins for the transaction fee of the format <amt><coin>"},
{&seqFlag, "sequence", -1, "Sequence number for the account (-1 to autocalculate)"}, {&seqFlag, "sequence", -1, "Sequence number for the account (-1 to autocalculate)"},
} }
sendTxFlags := []Flag2Register{
{&toFlag, "to", "", "Destination address for the transaction"},
}
appTxFlags := []Flag2Register{
{&nameFlag, "name", "", "Plugin to send the transaction to"},
{&dataFlag, "data", "", "Data to send with the transaction"},
}
RegisterPersistentFlags(TxCmd, cmdTxFlags) RegisterPersistentFlags(TxCmd, cmdTxFlags)
RegisterFlags(SendTxCmd, sendTxFlags)
RegisterFlags(AppTxCmd, appTxFlags)
//register commands
TxCmd.AddCommand(SendTxCmd, AppTxCmd)
}
func sendTxCmd(cmd *cobra.Command, args []string) error {
var toHex string
var chainPrefix string
spl := strings.Split(toFlag, "/")
switch len(spl) {
case 1:
toHex = spl[0]
case 2:
chainPrefix = spl[0]
toHex = spl[1]
default:
return errors.Errorf("To address has too many slashes")
}
// convert destination address to bytes
to, err := hex.DecodeString(StripHex(toHex))
if err != nil {
return errors.Errorf("To address is invalid hex: %v\n", err)
}
if chainPrefix != "" {
to = []byte(chainPrefix + "/" + string(to))
}
// load the priv key
privKey, err := LoadKey(fromFlag)
if err != nil {
return err
}
// get the sequence number for the tx
sequence, err := getSeq(privKey.Address[:])
if err != nil {
return err
}
//parse the fee and amounts into coin types
feeCoin, err := types.ParseCoin(feeFlag)
if err != nil {
return err
}
amountCoins, err := types.ParseCoins(amountFlag)
if err != nil {
return err
}
// craft the tx
input := types.NewTxInput(privKey.PubKey, amountCoins, sequence)
output := newOutput(to, amountCoins)
tx := &types.SendTx{
Gas: int64(gasFlag),
Fee: feeCoin,
Inputs: []types.TxInput{input},
Outputs: []types.TxOutput{output},
}
// sign that puppy
signBytes := tx.SignBytes(chainIDFlag)
tx.Inputs[0].Signature = privKey.Sign(signBytes)
out := wire.BinaryBytes(tx)
fmt.Println("Signed SendTx:")
fmt.Printf("%X\n", out)
// broadcast the transaction to tendermint
data, log, err := broadcastTx(tx)
if err != nil {
return err
}
fmt.Printf("Response: %X ; %s\n", data, log)
return nil
}
func appTxCmd(cmd *cobra.Command, args []string) error {
// convert data to bytes
data := []byte(dataFlag)
if isHex(dataFlag) {
data, _ = hex.DecodeString(dataFlag)
}
name := nameFlag
return AppTx(name, data)
} }
func AppTx(name string, data []byte) error { func AppTx(name string, data []byte) error {

View File

@ -19,11 +19,6 @@ func main() {
commands.InitCmd, commands.InitCmd,
commands.StartCmd, commands.StartCmd,
commands.TxCmd, commands.TxCmd,
commands.QueryCmd,
commands.KeyCmd,
commands.VerifyCmd,
commands.BlockCmd,
commands.AccountCmd,
commands.UnsafeResetAllCmd, commands.UnsafeResetAllCmd,
commands.VersionCmd, commands.VersionCmd,
) )