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

424 lines
9.2 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"
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-03-03 18:43:05 -05:00
"github.com/tendermint/abci/version"
2017-04-28 00:37:18 +04:00
"github.com/tendermint/tmlibs/log"
2016-10-20 21:57:33 -04:00
"github.com/urfave/cli"
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
2015-11-02 07:39:53 -08:00
func main() {
2016-12-22 19:00:22 -05:00
//workaround for the cli library (https://github.com/urfave/cli/issues/565)
cli.OsExiter = func(_ int) {}
2015-11-02 07:39:53 -08:00
app := cli.NewApp()
2017-01-12 15:47:55 -05:00
app.Name = "abci-cli"
app.Usage = "abci-cli [command] [args...]"
2017-03-03 18:43:05 -05:00
app.Version = version.Version
2015-11-02 07:39:53 -08:00
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "address",
2015-12-01 15:27:50 -08:00
Value: "tcp://127.0.0.1:46658",
2015-11-02 07:39:53 -08:00
Usage: "address of application socket",
},
2016-05-18 18:30:38 -04:00
cli.StringFlag{
2017-01-12 15:47:55 -05:00
Name: "abci",
2016-05-18 18:30:38 -04:00
Value: "socket",
Usage: "socket or grpc",
},
cli.BoolFlag{
Name: "verbose",
Usage: "print the command and results as if it were a console session",
},
2016-05-18 18:30:38 -04:00
}
2015-11-02 07:39:53 -08:00
app.Commands = []cli.Command{
2015-12-01 01:49:54 -05:00
{
Name: "batch",
2017-01-12 15:47:55 -05:00
Usage: "Run a batch of abci commands against an application",
Action: func(c *cli.Context) error {
return cmdBatch(app, c)
2015-12-01 01:49:54 -05:00
},
},
2015-11-30 20:56:36 -05:00
{
Name: "console",
2017-01-12 15:47:55 -05:00
Usage: "Start an interactive abci console for multiple commands",
Action: func(c *cli.Context) error {
return cmdConsole(app, c)
2015-11-30 20:56:36 -05:00
},
},
2015-12-06 18:18:13 -05:00
{
Name: "echo",
Usage: "Have the application echo a message",
Action: func(c *cli.Context) error {
return cmdEcho(c)
2015-12-06 18:18:13 -05:00
},
},
{
Name: "info",
Usage: "Get some info about the application",
Action: func(c *cli.Context) error {
return cmdInfo(c)
},
},
{
Name: "set_option",
Usage: "Set an option on the application",
Action: func(c *cli.Context) error {
return cmdSetOption(c)
},
},
2015-11-02 07:39:53 -08:00
{
2017-01-12 15:27:08 -05:00
Name: "deliver_tx",
Usage: "Deliver a new tx to application",
Action: func(c *cli.Context) error {
2017-01-12 15:27:08 -05:00
return cmdDeliverTx(c)
2015-11-02 07:39:53 -08:00
},
},
{
Name: "check_tx",
Usage: "Validate a tx",
Action: func(c *cli.Context) error {
return cmdCheckTx(c)
2015-11-02 07:39:53 -08:00
},
},
{
Name: "commit",
Usage: "Commit the application state and return the Merkle root hash",
Action: func(c *cli.Context) error {
return cmdCommit(c)
2015-11-02 07:39:53 -08:00
},
},
2016-01-22 15:50:11 -08:00
{
Name: "query",
Usage: "Query application state",
Action: func(c *cli.Context) error {
return cmdQuery(c)
2016-01-22 15:50:11 -08:00
},
2017-05-15 12:28:37 -04:00
Flags: []cli.Flag{
cli.StringFlag{
Name: "path",
Value: "/store",
Usage: "Path to prefix the query with",
},
cli.IntFlag{
Name: "height",
Value: 0,
Usage: "Height to query the blockchain at",
},
cli.BoolFlag{
Name: "prove",
Usage: "Whether or not to return a merkle proof of the query result",
},
},
2016-01-22 15:50:11 -08:00
},
2015-11-02 07:39:53 -08:00
}
2015-11-30 20:56:36 -05:00
app.Before = before
err := app.Run(os.Args)
if err != nil {
2017-05-01 16:43:52 +04:00
logger.Error(err.Error())
os.Exit(1)
}
2015-11-02 07:39:53 -08:00
}
2015-11-30 20:56:36 -05:00
func before(c *cli.Context) error {
2017-05-01 16:43:52 +04:00
if logger == nil {
logger = log.NewFilter(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), log.AllowError())
2017-05-01 16:43:52 +04:00
}
2016-05-14 03:09:47 -04:00
if client == nil {
2015-11-30 20:56:36 -05:00
var err error
2017-01-12 15:47:55 -05:00
client, err = abcicli.NewClient(c.GlobalString("address"), c.GlobalString("abci"), false)
2015-11-30 20:56:36 -05:00
if err != nil {
2017-05-01 16:43:52 +04:00
logger.Error(err.Error())
os.Exit(1)
2015-11-30 20:56:36 -05:00
}
2017-05-04 22:43:54 +04:00
client.SetLogger(logger.With("module", "abci-client"))
if _, err := client.Start(); err != nil {
return err
}
2015-11-30 20:56:36 -05:00
}
return nil
}
2016-09-12 10:21:20 +02:00
// badCmd is called when we invoke with an invalid first argument (just for console for now)
func badCmd(c *cli.Context, cmd string) {
fmt.Println("Unknown command:", cmd)
fmt.Println("Please try one of the following:")
fmt.Println("")
cli.DefaultAppComplete(c)
}
2017-01-12 01:03:51 -05:00
//Generates new Args array based off of previous call args to maintain flag persistence
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
//--------------------------------------------------------------------------------
func cmdBatch(app *cli.App, c *cli.Context) error {
2015-12-01 01:49:54 -05:00
bufReader := bufio.NewReader(os.Stdin)
for {
line, more, err := bufReader.ReadLine()
if more {
return 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 {
return err
2015-12-01 01:49:54 -05:00
}
2017-01-12 01:03:51 -05:00
args := persistentArgs(line)
app.Run(args) //cli prints error within its func call
2015-12-01 01:49:54 -05:00
}
return nil
2015-12-01 01:49:54 -05:00
}
func cmdConsole(app *cli.App, c *cli.Context) error {
2016-09-12 10:21:20 +02:00
// don't hard exit on mistyped commands (eg. check vs check_tx)
app.CommandNotFound = badCmd
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 {
return errors.New("Input is too long")
2015-11-30 20:56:36 -05:00
} else if err != nil {
return err
2015-11-30 20:56:36 -05:00
}
2017-01-12 01:03:51 -05:00
args := persistentArgs(line)
app.Run(args) //cli prints error within its func call
2015-11-30 20:56:36 -05:00
}
}
2015-12-06 18:18:13 -05:00
// Have the application echo a message
func cmdEcho(c *cli.Context) error {
2015-12-06 18:18:13 -05:00
args := c.Args()
if len(args) != 1 {
return errors.New("Command echo takes 1 argument")
2015-12-06 18:18:13 -05:00
}
resEcho := client.EchoSync(args[0])
printResponse(c, response{
Data: resEcho.Data,
})
return nil
2015-12-06 18:18:13 -05:00
}
// Get some info from the application
func cmdInfo(c *cli.Context) error {
2016-12-26 17:44:36 -08:00
resInfo, err := client.InfoSync()
if err != nil {
return err
}
printResponse(c, response{
Data: []byte(resInfo.Data),
})
return nil
}
// Set an option on the application
func cmdSetOption(c *cli.Context) error {
args := c.Args()
if len(args) != 2 {
return errors.New("Command set_option takes 2 arguments (key, value)")
}
resSetOption := client.SetOptionSync(args[0], args[1])
printResponse(c, response{
Log: resSetOption.Log,
})
return nil
}
2015-11-02 07:39:53 -08:00
// Append a new tx to application
2017-01-12 15:27:08 -05:00
func cmdDeliverTx(c *cli.Context) error {
args := c.Args()
if len(args) != 1 {
2017-01-12 15:27:08 -05:00
return errors.New("Command deliver_tx takes 1 argument")
}
txBytes, err := stringOrHexToBytes(c.Args()[0])
if err != nil {
return err
}
2017-01-12 15:27:08 -05:00
res := client.DeliverTxSync(txBytes)
printResponse(c, response{
Code: res.Code,
Data: res.Data,
Log: res.Log,
})
return nil
2015-11-02 07:39:53 -08:00
}
// Validate a tx
func cmdCheckTx(c *cli.Context) error {
args := c.Args()
if len(args) != 1 {
return errors.New("Command check_tx takes 1 argument")
}
txBytes, err := stringOrHexToBytes(c.Args()[0])
if err != nil {
return err
}
2016-05-14 03:09:47 -04:00
res := client.CheckTxSync(txBytes)
printResponse(c, response{
Code: res.Code,
Data: res.Data,
Log: res.Log,
})
return nil
2015-11-02 07:39:53 -08:00
}
// Get application Merkle root hash
func cmdCommit(c *cli.Context) error {
2016-05-14 03:09:47 -04:00
res := client.CommitSync()
printResponse(c, response{
2017-03-03 18:39:10 -05:00
Code: res.Code,
Data: res.Data,
Log: res.Log,
})
return nil
2015-11-02 07:39:53 -08:00
}
2016-01-22 15:50:11 -08:00
// Query application state
func cmdQuery(c *cli.Context) error {
2016-01-22 15:50:11 -08:00
args := c.Args()
2017-05-13 21:08:34 +02:00
2016-01-22 15:50:11 -08:00
if len(args) != 1 {
2017-05-15 12:28:37 -04:00
return 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 {
return err
}
2017-05-13 18:37:00 +02:00
2017-05-15 12:28:37 -04:00
path := c.String("path")
height := c.Int("height")
prove := c.Bool("prove")
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 {
return err
2017-01-10 15:49:26 +01:00
}
printResponse(c, 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,
},
})
return nil
2016-01-22 15:50:11 -08:00
}
2015-11-02 07:39:53 -08:00
//--------------------------------------------------------------------------------
func printResponse(c *cli.Context, rsp response) {
2017-01-12 01:03:51 -05:00
verbose := c.GlobalBool("verbose")
if verbose {
fmt.Println(">", c.Command.Name, strings.Join(c.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
}
2017-01-12 01:03:51 -05:00
if verbose {
fmt.Println("")
}
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
}