tendermint/cmd/abci-cli/abci-cli.go

404 lines
9.1 KiB
Go
Raw Normal View History

2015-11-02 07:39:53 -08:00
package main
import (
2015-11-30 20:56:36 -05:00
"bufio"
2016-07-01 20:22:58 -04:00
"encoding/hex"
"errors"
2015-11-02 07:39:53 -08:00
"fmt"
2015-12-01 01:49:54 -05:00
"io"
2015-11-02 07:39:53 -08:00
"os"
2017-10-18 12:48:19 -04:00
"os/exec"
2015-11-30 20:56:36 -05:00
"strings"
2015-11-02 07:39:53 -08:00
2017-04-28 00:37:18 +04:00
abcicli "github.com/tendermint/abci/client"
2017-01-12 15:47:55 -05:00
"github.com/tendermint/abci/types"
2017-10-18 12:48:19 -04:00
//"github.com/tendermint/abci/version"
2017-04-28 00:37:18 +04:00
"github.com/tendermint/tmlibs/log"
2017-10-18 12:48:19 -04:00
"github.com/spf13/cobra"
2015-11-02 07:39:53 -08:00
)
// Structure for data passed to print response.
2017-01-12 01:03:51 -05:00
type response struct {
2017-03-03 18:39:10 -05:00
// generic abci response
Data []byte
Code types.CodeType
Log string
Query *queryResponse
}
type queryResponse struct {
2017-02-14 16:53:21 -05:00
Key []byte
Value []byte
2017-03-03 18:39:10 -05:00
Height uint64
2017-02-14 16:53:21 -05:00
Proof []byte
2017-01-12 01:03:51 -05:00
}
2016-05-24 21:51:28 -04:00
// client is a global variable so it can be reused by the console
2017-01-12 15:47:55 -05:00
var client abcicli.Client
2015-11-30 20:56:36 -05:00
2017-05-01 16:43:52 +04:00
var logger log.Logger
2017-10-18 12:48:19 -04:00
// flags
var (
address string
abci string
verbose bool
2016-12-22 19:00:22 -05:00
2017-10-18 12:48:19 -04:00
path string
height int
prove bool
)
2016-12-22 19:00:22 -05:00
2017-10-18 12:48:19 -04:00
var RootCmd = &cobra.Command{
Use: "abci-cli",
Short: "",
Long: "",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if logger == nil {
logger = log.NewFilter(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), log.AllowError())
}
if client == nil {
var err error
client, err = abcicli.NewClient(address, abci, false)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
client.SetLogger(logger.With("module", "abci-client"))
if _, err := client.Start(); err != nil {
return err
}
}
return nil
},
}
2015-11-02 07:39:53 -08:00
2017-10-18 12:48:19 -04:00
func Execute() {
addGlobalFlags()
addCommands()
RootCmd.Execute() //err?
2015-11-02 07:39:53 -08:00
}
2017-10-18 12:48:19 -04:00
func addGlobalFlags() {
RootCmd.PersistentFlags().StringVarP(&address, "address", "", "tcp://127.0.0.1:46658", "address of application socket")
RootCmd.PersistentFlags().StringVarP(&abci, "abci", "", "socket", "socket or grpc")
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "print the command and results as if it were a console session")
}
func addQueryFlags() {
queryCmd.PersistentFlags().StringVarP(&path, "path", "", "/store", "Path to prefix query with")
queryCmd.PersistentFlags().IntVarP(&height, "height", "", 0, "Height to query the blockchain at")
queryCmd.PersistentFlags().BoolVarP(&prove, "prove", "", false, "Whether or not to return a merkle proof of the query result")
}
func addCommands() {
RootCmd.AddCommand(batchCmd)
RootCmd.AddCommand(consoleCmd)
RootCmd.AddCommand(echoCmd)
RootCmd.AddCommand(infoCmd)
RootCmd.AddCommand(setOptionCmd)
RootCmd.AddCommand(deliverTxCmd)
RootCmd.AddCommand(checkTxCmd)
RootCmd.AddCommand(commitCmd)
addQueryFlags()
RootCmd.AddCommand(queryCmd)
2015-11-30 20:56:36 -05:00
}
2017-10-18 12:48:19 -04:00
var batchCmd = &cobra.Command{
Use: "batch",
Short: "Run a batch of abci commands against an application",
Long: "",
Run: cmdBatch,
2016-09-12 10:21:20 +02:00
}
2017-10-18 12:48:19 -04:00
var consoleCmd = &cobra.Command{
Use: "console",
Short: "Start an interactive abci console for multiple commands",
Long: "",
ValidArgs: []string{"batch", "echo", "info", "set_option", "deliver_tx", "check_tx", "commit", "query"},
Run: cmdConsole,
}
var echoCmd = &cobra.Command{
Use: "echo",
Short: "",
Long: "Have the application echo a message",
Run: cmdEcho,
}
var infoCmd = &cobra.Command{
Use: "info",
Short: "Get some info about the application",
Long: "",
Run: cmdInfo,
}
var setOptionCmd = &cobra.Command{
Use: "set_option",
Short: "Set an options on the application",
Long: "",
Run: cmdSetOption,
}
var deliverTxCmd = &cobra.Command{
Use: "deliver_tx",
Short: "Deliver a new tx to the application",
Long: "",
Run: cmdDeliverTx,
}
var checkTxCmd = &cobra.Command{
Use: "check_tx",
Short: "Validate a tx",
Long: "",
Run: cmdCheckTx,
}
var commitCmd = &cobra.Command{
Use: "commit",
Short: "Commit the application state and return the Merkle root hash",
Long: "",
Run: cmdCommit,
}
var queryCmd = &cobra.Command{
Use: "query",
Short: "Query the application state",
Long: "",
Run: cmdQuery,
}
// Generates new Args array based off of previous call args to maintain flag persistence
2017-01-12 01:03:51 -05:00
func persistentArgs(line []byte) []string {
2017-09-21 15:26:43 -04:00
// generate the arguments to run from original os.Args
2017-01-12 01:03:51 -05:00
// to maintain flag arguments
args := os.Args
args = args[:len(args)-1] // remove the previous command argument
2017-09-21 15:26:43 -04:00
if len(line) > 0 { // prevents introduction of extra space leading to argument parse errors
2017-01-12 01:03:51 -05:00
args = append(args, strings.Split(string(line), " ")...)
}
return args
}
2015-11-02 07:39:53 -08:00
//--------------------------------------------------------------------------------
2017-10-18 12:48:19 -04:00
func cmdBatch(cmd *cobra.Command, args []string) {
2015-12-01 01:49:54 -05:00
bufReader := bufio.NewReader(os.Stdin)
for {
line, more, err := bufReader.ReadLine()
if more {
2017-10-18 12:48:19 -04:00
ifExit(errors.New("Input line is too long"))
2015-12-01 01:49:54 -05:00
} else if err == io.EOF {
break
2015-12-14 18:00:18 -05:00
} else if len(line) == 0 {
continue
2015-12-01 01:49:54 -05:00
} else if err != nil {
2017-10-18 12:48:19 -04:00
ifExit(err)
2015-12-01 01:49:54 -05:00
}
2017-01-12 01:03:51 -05:00
2017-10-18 12:48:19 -04:00
pArgs := persistentArgs(line)
out, err := exec.Command(pArgs[0], pArgs[1:]...).Output()
if err != nil {
panic(err)
}
fmt.Println(string(out))
2015-12-01 01:49:54 -05:00
}
}
2017-10-18 12:48:19 -04:00
func cmdConsole(cmd *cobra.Command, args []string) {
2017-01-12 01:03:51 -05:00
2015-11-30 20:56:36 -05:00
for {
fmt.Printf("\n> ")
2015-11-30 20:56:36 -05:00
bufReader := bufio.NewReader(os.Stdin)
line, more, err := bufReader.ReadLine()
if more {
2017-10-18 12:48:19 -04:00
ifExit(errors.New("Input is too long"))
2015-11-30 20:56:36 -05:00
} else if err != nil {
2017-10-18 12:48:19 -04:00
ifExit(err)
2015-11-30 20:56:36 -05:00
}
2017-10-18 12:48:19 -04:00
pArgs := persistentArgs(line)
out, err := exec.Command(pArgs[0], pArgs[1:]...).Output()
if err != nil {
panic(err)
}
fmt.Println(string(out))
2015-11-30 20:56:36 -05:00
}
}
2015-12-06 18:18:13 -05:00
// Have the application echo a message
2017-10-18 12:48:19 -04:00
func cmdEcho(cmd *cobra.Command, args []string) {
2015-12-06 18:18:13 -05:00
if len(args) != 1 {
2017-10-18 12:48:19 -04:00
ifExit(errors.New("Command echo takes 1 argument"))
2015-12-06 18:18:13 -05:00
}
resEcho := client.EchoSync(args[0])
2017-10-18 12:48:19 -04:00
printResponse(cmd, args, response{
Data: resEcho.Data,
})
2015-12-06 18:18:13 -05:00
}
// Get some info from the application
2017-10-18 12:48:19 -04:00
func cmdInfo(cmd *cobra.Command, args []string) {
2017-09-22 11:10:39 -04:00
var version string
if len(args) == 1 {
version = args[0]
}
resInfo, err := client.InfoSync(types.RequestInfo{version})
2016-12-26 17:44:36 -08:00
if err != nil {
2017-10-18 12:48:19 -04:00
ifExit(err)
2016-12-26 17:44:36 -08:00
}
2017-10-18 12:48:19 -04:00
printResponse(cmd, args, response{
Data: []byte(resInfo.Data),
})
}
// Set an option on the application
2017-10-18 12:48:19 -04:00
func cmdSetOption(cmd *cobra.Command, args []string) {
if len(args) != 2 {
2017-10-18 12:48:19 -04:00
ifExit(errors.New("Command set_option takes 2 arguments (key, value)"))
}
resSetOption := client.SetOptionSync(args[0], args[1])
2017-10-18 12:48:19 -04:00
printResponse(cmd, args, response{
Log: resSetOption.Log,
})
}
2015-11-02 07:39:53 -08:00
// Append a new tx to application
2017-10-18 12:48:19 -04:00
func cmdDeliverTx(cmd *cobra.Command, args []string) {
if len(args) != 1 {
2017-10-18 12:48:19 -04:00
ifExit(errors.New("Command deliver_tx takes 1 argument"))
}
2017-10-18 12:48:19 -04:00
txBytes, err := stringOrHexToBytes(args[0])
if err != nil {
2017-10-18 12:48:19 -04:00
ifExit(err)
}
2017-01-12 15:27:08 -05:00
res := client.DeliverTxSync(txBytes)
2017-10-18 12:48:19 -04:00
printResponse(cmd, args, response{
Code: res.Code,
Data: res.Data,
Log: res.Log,
})
2015-11-02 07:39:53 -08:00
}
// Validate a tx
2017-10-18 12:48:19 -04:00
func cmdCheckTx(cmd *cobra.Command, args []string) {
if len(args) != 1 {
2017-10-18 12:48:19 -04:00
ifExit(errors.New("Command check_tx takes 1 argument"))
}
2017-10-18 12:48:19 -04:00
txBytes, err := stringOrHexToBytes(args[0])
if err != nil {
2017-10-18 12:48:19 -04:00
ifExit(err)
}
2016-05-14 03:09:47 -04:00
res := client.CheckTxSync(txBytes)
2017-10-18 12:48:19 -04:00
printResponse(cmd, args, response{
Code: res.Code,
Data: res.Data,
Log: res.Log,
})
2015-11-02 07:39:53 -08:00
}
// Get application Merkle root hash
2017-10-18 12:48:19 -04:00
func cmdCommit(cmd *cobra.Command, args []string) {
2016-05-14 03:09:47 -04:00
res := client.CommitSync()
2017-10-18 12:48:19 -04:00
printResponse(cmd, args, response{
2017-03-03 18:39:10 -05:00
Code: res.Code,
Data: res.Data,
Log: res.Log,
})
2015-11-02 07:39:53 -08:00
}
2016-01-22 15:50:11 -08:00
// Query application state
2017-10-18 12:48:19 -04:00
func cmdQuery(cmd *cobra.Command, args []string) {
2016-01-22 15:50:11 -08:00
if len(args) != 1 {
2017-10-18 12:48:19 -04:00
ifExit(errors.New("Command query takes 1 argument, the query bytes"))
2016-01-22 15:50:11 -08:00
}
2017-05-13 18:37:00 +02:00
queryBytes, err := stringOrHexToBytes(args[0])
if err != nil {
2017-10-18 12:48:19 -04:00
ifExit(err)
}
2017-05-13 18:37:00 +02:00
resQuery, err := client.QuerySync(types.RequestQuery{
2017-02-14 16:53:21 -05:00
Data: queryBytes,
2017-05-13 18:37:00 +02:00
Path: path,
2017-05-15 12:28:37 -04:00
Height: uint64(height),
2017-05-13 18:37:00 +02:00
Prove: prove,
})
if err != nil {
2017-10-18 12:48:19 -04:00
ifExit(err)
2017-01-10 15:49:26 +01:00
}
2017-10-18 12:48:19 -04:00
printResponse(cmd, args, response{
2017-03-03 18:39:10 -05:00
Code: resQuery.Code,
Log: resQuery.Log,
Query: &queryResponse{
Key: resQuery.Key,
Value: resQuery.Value,
Height: resQuery.Height,
Proof: resQuery.Proof,
},
})
2016-01-22 15:50:11 -08:00
}
2015-11-02 07:39:53 -08:00
//--------------------------------------------------------------------------------
2017-10-18 12:48:19 -04:00
func printResponse(cmd *cobra.Command, args []string, rsp response) {
2017-01-12 01:03:51 -05:00
if verbose {
2017-10-18 12:48:19 -04:00
fmt.Println(">", cmd.Use, strings.Join(args, " "))
}
2017-03-03 18:39:10 -05:00
if !rsp.Code.IsOK() {
fmt.Printf("-> code: %s\n", rsp.Code.String())
2016-02-07 19:59:19 -08:00
}
if len(rsp.Data) != 0 {
2017-01-12 01:03:51 -05:00
fmt.Printf("-> data: %s\n", rsp.Data)
fmt.Printf("-> data.hex: %X\n", rsp.Data)
}
if rsp.Log != "" {
fmt.Printf("-> log: %s\n", rsp.Log)
}
2017-03-03 18:39:10 -05:00
if rsp.Query != nil {
fmt.Printf("-> height: %d\n", rsp.Query.Height)
if rsp.Query.Key != nil {
fmt.Printf("-> key: %s\n", rsp.Query.Key)
fmt.Printf("-> key.hex: %X\n", rsp.Query.Key)
}
if rsp.Query.Value != nil {
fmt.Printf("-> value: %s\n", rsp.Query.Value)
fmt.Printf("-> value.hex: %X\n", rsp.Query.Value)
}
if rsp.Query.Proof != nil {
fmt.Printf("-> proof: %X\n", rsp.Query.Proof)
}
2016-02-08 13:47:47 -08:00
}
2016-02-07 19:59:19 -08:00
}
2016-07-01 20:22:58 -04:00
// NOTE: s is interpreted as a string unless prefixed with 0x
func stringOrHexToBytes(s string) ([]byte, error) {
if len(s) > 2 && strings.ToLower(s[:2]) == "0x" {
2016-07-01 20:22:58 -04:00
b, err := hex.DecodeString(s[2:])
if err != nil {
err = fmt.Errorf("Error decoding hex argument: %s", err.Error())
return nil, err
2016-07-01 20:22:58 -04:00
}
return b, nil
2016-07-01 20:22:58 -04:00
}
if !strings.HasPrefix(s, "\"") || !strings.HasSuffix(s, "\"") {
err := fmt.Errorf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string", s)
return nil, err
}
return []byte(s[1 : len(s)-1]), nil
2016-07-01 20:22:58 -04:00
}
2017-10-18 12:48:19 -04:00
func ifExit(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}