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

766 lines
19 KiB
Go
Raw Normal View History

2015-11-02 07:39:53 -08:00
package main
import (
2015-11-30 17:56:36 -08:00
"bufio"
2016-07-01 17:22:58 -07:00
"encoding/hex"
"errors"
2015-11-02 07:39:53 -08:00
"fmt"
2015-11-30 22:49:54 -08:00
"io"
2015-11-02 07:39:53 -08:00
"os"
2015-11-30 17:56:36 -08:00
"strings"
2015-11-02 07:39:53 -08:00
"github.com/spf13/cobra"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
2018-06-21 21:59:02 -07:00
abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/example/code"
"github.com/tendermint/tendermint/abci/example/counter"
"github.com/tendermint/tendermint/abci/example/kvstore"
"github.com/tendermint/tendermint/abci/server"
servertest "github.com/tendermint/tendermint/abci/tests/server"
"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/abci/version"
2015-11-02 07:39:53 -08:00
)
2016-05-24 18:51:28 -07:00
// client is a global variable so it can be reused by the console
var (
client abcicli.Client
logger log.Logger
)
2017-05-01 05:43:52 -07:00
2017-10-18 09:48:19 -07:00
// flags
var (
// global
flagAddress string
flagAbci string
flagVerbose bool // for the println output
flagLogLevel string // for the logger
2016-12-22 16:00:22 -08:00
// query
flagPath string
flagHeight int
flagProve bool
// counter
flagSerial bool
2018-02-19 12:34:51 -08:00
// kvstore
flagPersist string
2017-10-18 09:48:19 -07:00
)
2016-12-22 16:00:22 -08:00
2017-10-18 09:48:19 -07:00
var RootCmd = &cobra.Command{
Use: "abci-cli",
2018-01-01 08:21:36 -08:00
Short: "the ABCI CLI tool wraps an ABCI client",
Long: "the ABCI CLI tool wraps an ABCI client and is used for testing ABCI servers",
2017-10-18 09:48:19 -07:00
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
switch cmd.Use {
2018-02-19 12:34:51 -08:00
case "counter", "kvstore", "dummy": // for the examples apps, don't pre-run
2017-11-08 14:18:42 -08:00
return nil
case "version": // skip running for version command
return nil
}
2017-10-18 09:48:19 -07:00
if logger == nil {
allowLevel, err := log.AllowLevel(flagLogLevel)
if err != nil {
return err
}
logger = log.NewFilter(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), allowLevel)
2017-10-18 09:48:19 -07:00
}
if client == nil {
var err error
client, err = abcicli.NewClient(flagAddress, flagAbci, false)
2017-10-18 09:48:19 -07:00
if err != nil {
2017-10-26 05:37:42 -07:00
return err
2017-10-18 09:48:19 -07:00
}
client.SetLogger(logger.With("module", "abci-client"))
2018-01-05 19:19:37 -08:00
if err := client.Start(); err != nil {
2017-10-18 09:48:19 -07:00
return err
}
}
return nil
},
}
2015-11-02 07:39:53 -08:00
// Structure for data passed to print response.
type response struct {
// generic abci response
Data []byte
Code uint32
Info string
Log string
Query *queryResponse
}
type queryResponse struct {
Key []byte
Value []byte
Height int64
Proof []byte
}
2017-11-27 11:52:06 -08:00
func Execute() error {
2017-10-18 09:48:19 -07:00
addGlobalFlags()
addCommands()
2017-11-27 11:52:06 -08:00
return RootCmd.Execute()
2015-11-02 07:39:53 -08:00
}
2017-10-18 09:48:19 -07:00
func addGlobalFlags() {
2018-05-30 21:15:34 -07:00
RootCmd.PersistentFlags().StringVarP(&flagAddress, "address", "", "tcp://0.0.0.0:26658", "address of application socket")
2018-01-01 08:21:36 -08:00
RootCmd.PersistentFlags().StringVarP(&flagAbci, "abci", "", "socket", "either socket or grpc")
RootCmd.PersistentFlags().BoolVarP(&flagVerbose, "verbose", "v", false, "print the command and results as if it were a console session")
RootCmd.PersistentFlags().StringVarP(&flagLogLevel, "log_level", "", "debug", "set the logger level")
2017-10-18 09:48:19 -07:00
}
func addQueryFlags() {
2018-01-01 08:21:36 -08:00
queryCmd.PersistentFlags().StringVarP(&flagPath, "path", "", "/store", "path to prefix query with")
queryCmd.PersistentFlags().IntVarP(&flagHeight, "height", "", 0, "height to query the blockchain at")
queryCmd.PersistentFlags().BoolVarP(&flagProve, "prove", "", false, "whether or not to return a merkle proof of the query result")
2017-10-18 09:48:19 -07:00
}
func addCounterFlags() {
2018-01-01 08:21:36 -08:00
counterCmd.PersistentFlags().BoolVarP(&flagSerial, "serial", "", false, "enforce incrementing (serial) transactions")
}
func addDummyFlags() {
2018-01-01 08:21:36 -08:00
dummyCmd.PersistentFlags().StringVarP(&flagPersist, "persist", "", "", "directory to use for a database")
}
2018-02-19 12:34:51 -08:00
func addKVStoreFlags() {
kvstoreCmd.PersistentFlags().StringVarP(&flagPersist, "persist", "", "", "directory to use for a database")
}
2017-10-18 09:48:19 -07:00
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)
2017-11-08 14:18:42 -08:00
RootCmd.AddCommand(versionCmd)
RootCmd.AddCommand(testCmd)
2017-10-18 09:48:19 -07:00
addQueryFlags()
RootCmd.AddCommand(queryCmd)
// examples
addCounterFlags()
RootCmd.AddCommand(counterCmd)
2018-02-19 12:34:51 -08:00
// deprecated, left for backwards compatibility
addDummyFlags()
RootCmd.AddCommand(dummyCmd)
2018-02-19 12:34:51 -08:00
// replaces dummy, see issue #196
addKVStoreFlags()
RootCmd.AddCommand(kvstoreCmd)
2015-11-30 17:56:36 -08:00
}
2017-10-18 09:48:19 -07:00
var batchCmd = &cobra.Command{
Use: "batch",
2018-01-01 08:21:36 -08:00
Short: "run a batch of abci commands against an application",
Long: `run a batch of abci commands against an application
This command is run by piping in a file containing a series of commands
you'd like to run:
abci-cli batch < example.file
where example.file looks something like:
set_option serial on
check_tx 0x00
check_tx 0xff
deliver_tx 0x00
check_tx 0x00
deliver_tx 0x01
deliver_tx 0x04
info
`,
Args: cobra.ExactArgs(0),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdBatch(cmd, args)
},
2016-09-12 01:21:20 -07:00
}
2017-10-18 09:48:19 -07:00
var consoleCmd = &cobra.Command{
2018-01-01 08:21:36 -08:00
Use: "console",
Short: "start an interactive ABCI console for multiple commands",
Long: `start an interactive ABCI console for multiple commands
2018-01-05 19:19:37 -08:00
2018-01-01 08:21:36 -08:00
This command opens an interactive console for running any of the other commands
without opening a new connection each time
`,
Args: cobra.ExactArgs(0),
2017-12-20 12:41:28 -08:00
ValidArgs: []string{"echo", "info", "set_option", "deliver_tx", "check_tx", "commit", "query"},
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdConsole(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
var echoCmd = &cobra.Command{
Use: "echo",
2018-01-01 08:21:36 -08:00
Short: "have the application echo a message",
Long: "have the application echo a message",
Args: cobra.ExactArgs(1),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdEcho(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
var infoCmd = &cobra.Command{
Use: "info",
2018-01-01 08:21:36 -08:00
Short: "get some info about the application",
Long: "get some info about the application",
Args: cobra.ExactArgs(0),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdInfo(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
var setOptionCmd = &cobra.Command{
Use: "set_option",
2018-01-01 08:21:36 -08:00
Short: "set an option on the application",
Long: "set an option on the application",
Args: cobra.ExactArgs(2),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdSetOption(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
var deliverTxCmd = &cobra.Command{
Use: "deliver_tx",
2018-01-01 08:21:36 -08:00
Short: "deliver a new transaction to the application",
Long: "deliver a new transaction to the application",
Args: cobra.ExactArgs(1),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdDeliverTx(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
var checkTxCmd = &cobra.Command{
Use: "check_tx",
2018-01-01 08:21:36 -08:00
Short: "validate a transaction",
Long: "validate a transaction",
Args: cobra.ExactArgs(1),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdCheckTx(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
var commitCmd = &cobra.Command{
Use: "commit",
2018-01-01 08:21:36 -08:00
Short: "commit the application state and return the Merkle root hash",
Long: "commit the application state and return the Merkle root hash",
Args: cobra.ExactArgs(0),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdCommit(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
2017-11-08 14:18:42 -08:00
var versionCmd = &cobra.Command{
Use: "version",
2018-01-01 08:21:36 -08:00
Short: "print ABCI console version",
Long: "print ABCI console version",
2017-11-08 14:18:42 -08:00
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(version.Version)
return nil
},
}
2017-10-18 09:48:19 -07:00
var queryCmd = &cobra.Command{
Use: "query",
2018-01-01 08:21:36 -08:00
Short: "query the application state",
Long: "query the application state",
Args: cobra.ExactArgs(1),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdQuery(cmd, args)
},
2017-10-18 09:48:19 -07:00
}
var counterCmd = &cobra.Command{
Use: "counter",
Short: "ABCI demo example",
2018-01-01 08:21:36 -08:00
Long: "ABCI demo example",
Args: cobra.ExactArgs(0),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
return cmdCounter(cmd, args)
},
}
2018-02-19 12:34:51 -08:00
// deprecated, left for backwards compatibility
var dummyCmd = &cobra.Command{
2018-02-19 12:34:51 -08:00
Use: "dummy",
Deprecated: "use: [abci-cli kvstore] instead",
Short: "ABCI demo example",
Long: "ABCI demo example",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return cmdKVStore(cmd, args)
},
}
var kvstoreCmd = &cobra.Command{
Use: "kvstore",
Short: "ABCI demo example",
2018-01-01 08:21:36 -08:00
Long: "ABCI demo example",
Args: cobra.ExactArgs(0),
2017-10-26 05:37:42 -07:00
RunE: func(cmd *cobra.Command, args []string) error {
2018-02-19 12:34:51 -08:00
return cmdKVStore(cmd, args)
2017-10-26 05:37:42 -07:00
},
}
var testCmd = &cobra.Command{
2017-10-20 09:44:37 -07:00
Use: "test",
2018-01-01 08:21:36 -08:00
Short: "run integration tests",
Long: "run integration tests",
2017-10-20 09:44:37 -07:00
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return cmdTest(cmd, args)
},
}
2017-10-18 09:48:19 -07:00
// Generates new Args array based off of previous call args to maintain flag persistence
2017-01-11 22:03:51 -08:00
func persistentArgs(line []byte) []string {
2017-09-21 12:26:43 -07:00
// generate the arguments to run from original os.Args
2017-01-11 22:03:51 -08:00
// to maintain flag arguments
args := os.Args
args = args[:len(args)-1] // remove the previous command argument
2017-09-21 12:26:43 -07:00
if len(line) > 0 { // prevents introduction of extra space leading to argument parse errors
2017-01-11 22:03:51 -08:00
args = append(args, strings.Split(string(line), " ")...)
}
return args
}
2015-11-02 07:39:53 -08:00
//--------------------------------------------------------------------------------
func compose(fs []func() error) error {
if len(fs) == 0 {
return nil
2017-10-20 09:44:37 -07:00
} else {
err := fs[0]()
if err == nil {
return compose(fs[1:])
} else {
return err
}
2017-10-20 09:44:37 -07:00
}
}
func cmdTest(cmd *cobra.Command, args []string) error {
return compose(
[]func() error{
func() error { return servertest.InitChain(client) },
func() error { return servertest.SetOption(client, "serial", "on") },
func() error { return servertest.Commit(client, nil) },
func() error { return servertest.DeliverTx(client, []byte("abc"), code.CodeTypeBadNonce, nil) },
func() error { return servertest.Commit(client, nil) },
func() error { return servertest.DeliverTx(client, []byte{0x00}, code.CodeTypeOK, nil) },
func() error { return servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1}) },
func() error { return servertest.DeliverTx(client, []byte{0x00}, code.CodeTypeBadNonce, nil) },
func() error { return servertest.DeliverTx(client, []byte{0x01}, code.CodeTypeOK, nil) },
func() error { return servertest.DeliverTx(client, []byte{0x00, 0x02}, code.CodeTypeOK, nil) },
func() error { return servertest.DeliverTx(client, []byte{0x00, 0x03}, code.CodeTypeOK, nil) },
func() error { return servertest.DeliverTx(client, []byte{0x00, 0x00, 0x04}, code.CodeTypeOK, nil) },
2018-01-01 08:21:36 -08:00
func() error {
return servertest.DeliverTx(client, []byte{0x00, 0x00, 0x06}, code.CodeTypeBadNonce, nil)
},
func() error { return servertest.Commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5}) },
})
}
2017-10-26 05:37:42 -07:00
func cmdBatch(cmd *cobra.Command, args []string) error {
2015-11-30 22:49:54 -08:00
bufReader := bufio.NewReader(os.Stdin)
for {
2015-11-30 22:49:54 -08:00
line, more, err := bufReader.ReadLine()
if more {
2017-10-26 05:37:42 -07:00
return errors.New("Input line is too long")
2015-11-30 22:49:54 -08:00
} else if err == io.EOF {
break
2015-12-14 15:00:18 -08:00
} else if len(line) == 0 {
continue
2015-11-30 22:49:54 -08:00
} else if err != nil {
2017-10-26 05:37:42 -07:00
return err
2015-11-30 22:49:54 -08:00
}
2017-12-10 13:39:30 -08:00
cmdArgs := persistentArgs(line)
if err := muxOnCommands(cmd, cmdArgs); err != nil {
2017-12-10 13:39:30 -08:00
return err
}
fmt.Println()
2015-11-30 22:49:54 -08:00
}
2017-10-26 05:37:42 -07:00
return nil
2015-11-30 22:49:54 -08:00
}
2017-10-26 05:37:42 -07:00
func cmdConsole(cmd *cobra.Command, args []string) error {
2015-11-30 17:56:36 -08:00
for {
2017-10-23 16:35:02 -07:00
fmt.Printf("> ")
2015-11-30 17:56:36 -08:00
bufReader := bufio.NewReader(os.Stdin)
line, more, err := bufReader.ReadLine()
if more {
2017-10-26 05:37:42 -07:00
return errors.New("Input is too long")
2015-11-30 17:56:36 -08:00
} else if err != nil {
2017-10-26 05:37:42 -07:00
return err
2015-11-30 17:56:36 -08:00
}
2017-10-18 09:48:19 -07:00
pArgs := persistentArgs(line)
if err := muxOnCommands(cmd, pArgs); err != nil {
2017-10-26 05:37:42 -07:00
return err
2017-10-18 09:48:19 -07:00
}
2015-11-30 17:56:36 -08:00
}
2017-10-26 05:37:42 -07:00
return nil
2015-11-30 17:56:36 -08:00
}
func muxOnCommands(cmd *cobra.Command, pArgs []string) error {
if len(pArgs) < 2 {
return errors.New("expecting persistent args of the form: abci-cli [command] <...>")
}
2017-12-20 12:41:28 -08:00
// TODO: this parsing is fragile
args := []string{}
for i := 0; i < len(pArgs); i++ {
arg := pArgs[i]
// check for flags
if strings.HasPrefix(arg, "-") {
// if it has an equal, we can just skip
if strings.Contains(arg, "=") {
continue
}
// if its a boolean, we can just skip
_, err := cmd.Flags().GetBool(strings.TrimLeft(arg, "-"))
if err == nil {
continue
}
// otherwise, we need to skip the next one too
i += 1
continue
}
// append the actual arg
args = append(args, arg)
}
var subCommand string
var actualArgs []string
if len(args) > 1 {
subCommand = args[1]
}
if len(args) > 2 {
actualArgs = args[2:]
}
cmd.Use = subCommand // for later print statements ...
2017-12-10 13:39:30 -08:00
switch strings.ToLower(subCommand) {
case "check_tx":
return cmdCheckTx(cmd, actualArgs)
case "commit":
return cmdCommit(cmd, actualArgs)
case "deliver_tx":
return cmdDeliverTx(cmd, actualArgs)
case "echo":
return cmdEcho(cmd, actualArgs)
case "info":
return cmdInfo(cmd, actualArgs)
case "query":
return cmdQuery(cmd, actualArgs)
case "set_option":
return cmdSetOption(cmd, actualArgs)
default:
return cmdUnimplemented(cmd, pArgs)
}
}
func cmdUnimplemented(cmd *cobra.Command, args []string) error {
// TODO: Print out all the sub-commands available
msg := "unimplemented command"
if err := cmd.Help(); err != nil {
msg = err.Error()
}
if len(args) > 0 {
msg += fmt.Sprintf(" args: [%s]", strings.Join(args, " "))
}
printResponse(cmd, args, response{
Code: codeBad,
Log: msg,
})
return nil
}
2015-12-06 15:18:13 -08:00
// Have the application echo a message
2017-10-26 05:37:42 -07:00
func cmdEcho(cmd *cobra.Command, args []string) error {
msg := ""
if len(args) > 0 {
msg = args[0]
}
res, err := client.EchoSync(msg)
if err != nil {
return err
}
2017-10-18 09:48:19 -07:00
printResponse(cmd, args, response{
Data: []byte(res.Message),
})
2017-10-26 05:37:42 -07:00
return nil
2015-12-06 15:18:13 -08:00
}
// Get some info from the application
2017-10-26 05:37:42 -07:00
func cmdInfo(cmd *cobra.Command, args []string) error {
2017-09-22 08:10:39 -07:00
var version string
if len(args) == 1 {
version = args[0]
}
res, err := client.InfoSync(types.RequestInfo{version})
2016-12-26 17:44:36 -08:00
if err != nil {
2017-10-26 05:37:42 -07:00
return err
2016-12-26 17:44:36 -08:00
}
2017-10-18 09:48:19 -07:00
printResponse(cmd, args, response{
Data: []byte(res.Data),
})
2017-10-26 05:37:42 -07:00
return nil
}
const codeBad uint32 = 10
// Set an option on the application
2017-10-26 05:37:42 -07:00
func cmdSetOption(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
printResponse(cmd, args, response{
Code: codeBad,
Log: "want at least arguments of the form: <key> <value>",
})
return nil
}
key, val := args[0], args[1]
_, err := client.SetOptionSync(types.RequestSetOption{key, val})
if err != nil {
return err
}
printResponse(cmd, args, response{Log: "OK (SetOption doesn't return anything.)"}) // NOTE: Nothing to show...
2017-10-26 05:37:42 -07:00
return nil
}
2015-11-02 07:39:53 -08:00
// Append a new tx to application
2017-10-26 05:37:42 -07:00
func cmdDeliverTx(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
printResponse(cmd, args, response{
Code: codeBad,
Log: "want the tx",
})
return nil
}
2017-10-18 09:48:19 -07:00
txBytes, err := stringOrHexToBytes(args[0])
if err != nil {
2017-10-26 05:37:42 -07:00
return err
}
res, err := client.DeliverTxSync(txBytes)
if err != nil {
return err
}
2017-10-18 09:48:19 -07:00
printResponse(cmd, args, response{
Code: res.Code,
Data: res.Data,
Info: res.Info,
Log: res.Log,
})
2017-10-26 05:37:42 -07:00
return nil
2015-11-02 07:39:53 -08:00
}
// Validate a tx
2017-10-26 05:37:42 -07:00
func cmdCheckTx(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
printResponse(cmd, args, response{
Code: codeBad,
Info: "want the tx",
})
return nil
}
2017-10-18 09:48:19 -07:00
txBytes, err := stringOrHexToBytes(args[0])
if err != nil {
2017-10-26 05:37:42 -07:00
return err
}
res, err := client.CheckTxSync(txBytes)
if err != nil {
return err
}
2017-10-18 09:48:19 -07:00
printResponse(cmd, args, response{
Code: res.Code,
Data: res.Data,
Info: res.Info,
Log: res.Log,
})
2017-10-26 05:37:42 -07:00
return nil
2015-11-02 07:39:53 -08:00
}
// Get application Merkle root hash
2017-10-26 05:37:42 -07:00
func cmdCommit(cmd *cobra.Command, args []string) error {
res, err := client.CommitSync()
if err != nil {
return err
}
2017-10-18 09:48:19 -07:00
printResponse(cmd, args, response{
Data: res.Data,
})
2017-10-26 05:37:42 -07:00
return nil
2015-11-02 07:39:53 -08:00
}
2016-01-22 15:50:11 -08:00
// Query application state
2017-10-26 05:37:42 -07:00
func cmdQuery(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
printResponse(cmd, args, response{
Code: codeBad,
Info: "want the query",
Log: "",
})
return nil
}
2017-05-13 09:37:00 -07:00
queryBytes, err := stringOrHexToBytes(args[0])
if err != nil {
2017-10-26 05:37:42 -07:00
return err
}
2017-05-13 09:37:00 -07:00
resQuery, err := client.QuerySync(types.RequestQuery{
2017-02-14 13:53:21 -08:00
Data: queryBytes,
Path: flagPath,
Height: int64(flagHeight),
Prove: flagProve,
})
if err != nil {
2017-10-26 05:37:42 -07:00
return err
2017-01-10 06:49:26 -08:00
}
2017-10-18 09:48:19 -07:00
printResponse(cmd, args, response{
2017-03-03 15:39:10 -08:00
Code: resQuery.Code,
Info: resQuery.Info,
2017-03-03 15:39:10 -08:00
Log: resQuery.Log,
Query: &queryResponse{
Key: resQuery.Key,
Value: resQuery.Value,
Height: resQuery.Height,
Proof: resQuery.Proof,
},
})
2017-10-26 05:37:42 -07:00
return nil
2016-01-22 15:50:11 -08:00
}
2017-10-26 05:37:42 -07:00
func cmdCounter(cmd *cobra.Command, args []string) error {
app := counter.NewCounterApplication(flagSerial)
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
// Start the listener
srv, err := server.NewServer(flagAddress, flagAbci, app)
if err != nil {
2017-10-26 05:37:42 -07:00
return err
}
srv.SetLogger(logger.With("module", "abci-server"))
2018-01-05 19:19:37 -08:00
if err := srv.Start(); err != nil {
2017-10-26 05:37:42 -07:00
return err
}
// Wait forever
cmn.TrapSignal(func() {
// Cleanup
srv.Stop()
})
2017-10-26 05:37:42 -07:00
return nil
}
2018-02-19 12:34:51 -08:00
func cmdKVStore(cmd *cobra.Command, args []string) error {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
// Create the application - in memory or persisted to disk
var app types.Application
if flagPersist == "" {
2018-02-19 12:39:36 -08:00
app = kvstore.NewKVStoreApplication()
} else {
2018-02-19 12:39:36 -08:00
app = kvstore.NewPersistentKVStoreApplication(flagPersist)
app.(*kvstore.PersistentKVStoreApplication).SetLogger(logger.With("module", "kvstore"))
}
// Start the listener
srv, err := server.NewServer(flagAddress, flagAbci, app)
if err != nil {
2017-10-26 05:37:42 -07:00
return err
}
srv.SetLogger(logger.With("module", "abci-server"))
2018-01-05 19:19:37 -08:00
if err := srv.Start(); err != nil {
2017-10-26 05:37:42 -07:00
return err
}
// Wait forever
cmn.TrapSignal(func() {
// Cleanup
srv.Stop()
})
2017-10-26 05:37:42 -07:00
return nil
}
2015-11-02 07:39:53 -08:00
//--------------------------------------------------------------------------------
2017-10-18 09:48:19 -07:00
func printResponse(cmd *cobra.Command, args []string, rsp response) {
2017-01-11 22:03:51 -08:00
if flagVerbose {
2017-10-18 09:48:19 -07:00
fmt.Println(">", cmd.Use, strings.Join(args, " "))
}
2017-10-23 16:35:02 -07:00
// Always print the status code.
2017-11-30 14:49:53 -08:00
if rsp.Code == types.CodeTypeOK {
fmt.Printf("-> code: OK\n")
} else {
fmt.Printf("-> code: %d\n", rsp.Code)
}
2017-10-23 16:35:02 -07:00
if len(rsp.Data) != 0 {
2017-10-23 16:35:02 -07:00
// Do no print this line when using the commit command
// because the string comes out as gibberish
if cmd.Use != "commit" {
fmt.Printf("-> data: %s\n", rsp.Data)
}
fmt.Printf("-> data.hex: 0x%X\n", rsp.Data)
}
if rsp.Log != "" {
fmt.Printf("-> log: %s\n", rsp.Log)
}
2017-03-03 15:39:10 -08: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 17:22:58 -07: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 17:22:58 -07: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 17:22:58 -07:00
}
return b, nil
2016-07-01 17:22:58 -07: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 17:22:58 -07:00
}