You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

571 lines
13 KiB

9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
7 years ago
7 years ago
9 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "os"
  9. "os/exec"
  10. "strings"
  11. "github.com/spf13/cobra"
  12. cmn "github.com/tendermint/tmlibs/common"
  13. "github.com/tendermint/tmlibs/log"
  14. abcicli "github.com/tendermint/abci/client"
  15. "github.com/tendermint/abci/example/counter"
  16. "github.com/tendermint/abci/example/dummy"
  17. "github.com/tendermint/abci/server"
  18. "github.com/tendermint/abci/types"
  19. "github.com/tendermint/abci/version"
  20. )
  21. // client is a global variable so it can be reused by the console
  22. var (
  23. client abcicli.Client
  24. logger log.Logger
  25. )
  26. // flags
  27. var (
  28. // global
  29. address string
  30. abci string
  31. verbose bool // for the println output
  32. logLevel string // for the logger
  33. // query
  34. path string
  35. height int
  36. prove bool
  37. // counter
  38. addrC string
  39. serial bool
  40. // dummy
  41. addrD string
  42. persist string
  43. )
  44. var RootCmd = &cobra.Command{
  45. Use: "abci-cli",
  46. Short: "",
  47. Long: "",
  48. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  49. switch cmd.Use {
  50. case "counter", "dummy": // for the examples apps, don't pre-run
  51. return nil
  52. case "version": // skip running for version command
  53. return nil
  54. }
  55. if logger == nil {
  56. allowLevel, err := log.AllowLevel(logLevel)
  57. if err != nil {
  58. return err
  59. }
  60. logger = log.NewFilter(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), allowLevel)
  61. }
  62. if client == nil {
  63. var err error
  64. client, err = abcicli.NewClient(address, abci, false)
  65. if err != nil {
  66. return err
  67. }
  68. client.SetLogger(logger.With("module", "abci-client"))
  69. if err := client.Start(); err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. },
  75. }
  76. // Structure for data passed to print response.
  77. type response struct {
  78. // generic abci response
  79. Data []byte
  80. Code uint32
  81. Log string
  82. Query *queryResponse
  83. }
  84. type queryResponse struct {
  85. Key []byte
  86. Value []byte
  87. Height uint64
  88. Proof []byte
  89. }
  90. func Execute() error {
  91. addGlobalFlags()
  92. addCommands()
  93. return RootCmd.Execute()
  94. }
  95. func addGlobalFlags() {
  96. RootCmd.PersistentFlags().StringVarP(&address, "address", "", "tcp://0.0.0.0:46658", "Address of application socket")
  97. RootCmd.PersistentFlags().StringVarP(&abci, "abci", "", "socket", "Either socket or grpc")
  98. RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print the command and results as if it were a console session")
  99. RootCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "debug", "Set the logger level")
  100. }
  101. func addQueryFlags() {
  102. queryCmd.PersistentFlags().StringVarP(&path, "path", "", "/store", "Path to prefix query with")
  103. queryCmd.PersistentFlags().IntVarP(&height, "height", "", 0, "Height to query the blockchain at")
  104. queryCmd.PersistentFlags().BoolVarP(&prove, "prove", "", false, "Whether or not to return a merkle proof of the query result")
  105. }
  106. func addCounterFlags() {
  107. counterCmd.PersistentFlags().StringVarP(&addrC, "addr", "", "tcp://0.0.0.0:46658", "Listen address")
  108. counterCmd.PersistentFlags().BoolVarP(&serial, "serial", "", false, "Enforce incrementing (serial) transactions")
  109. }
  110. func addDummyFlags() {
  111. dummyCmd.PersistentFlags().StringVarP(&addrD, "addr", "", "tcp://0.0.0.0:46658", "Listen address")
  112. dummyCmd.PersistentFlags().StringVarP(&persist, "persist", "", "", "Directory to use for a database")
  113. }
  114. func addCommands() {
  115. RootCmd.AddCommand(batchCmd)
  116. RootCmd.AddCommand(consoleCmd)
  117. RootCmd.AddCommand(echoCmd)
  118. RootCmd.AddCommand(infoCmd)
  119. RootCmd.AddCommand(setOptionCmd)
  120. RootCmd.AddCommand(deliverTxCmd)
  121. RootCmd.AddCommand(checkTxCmd)
  122. RootCmd.AddCommand(commitCmd)
  123. RootCmd.AddCommand(versionCmd)
  124. addQueryFlags()
  125. RootCmd.AddCommand(queryCmd)
  126. // examples
  127. addCounterFlags()
  128. RootCmd.AddCommand(counterCmd)
  129. addDummyFlags()
  130. RootCmd.AddCommand(dummyCmd)
  131. }
  132. var batchCmd = &cobra.Command{
  133. Use: "batch",
  134. Short: "Run a batch of abci commands against an application",
  135. Long: "",
  136. Args: cobra.ExactArgs(0),
  137. RunE: func(cmd *cobra.Command, args []string) error {
  138. return cmdBatch(cmd, args)
  139. },
  140. }
  141. var consoleCmd = &cobra.Command{
  142. Use: "console",
  143. Short: "Start an interactive abci console for multiple commands",
  144. Long: "",
  145. Args: cobra.ExactArgs(0),
  146. ValidArgs: []string{"batch", "echo", "info", "set_option", "deliver_tx", "check_tx", "commit", "query"},
  147. RunE: func(cmd *cobra.Command, args []string) error {
  148. return cmdConsole(cmd, args)
  149. },
  150. }
  151. var echoCmd = &cobra.Command{
  152. Use: "echo",
  153. Short: "Have the application echo a message",
  154. Long: "",
  155. Args: cobra.ExactArgs(1),
  156. RunE: func(cmd *cobra.Command, args []string) error {
  157. return cmdEcho(cmd, args)
  158. },
  159. }
  160. var infoCmd = &cobra.Command{
  161. Use: "info",
  162. Short: "Get some info about the application",
  163. Long: "",
  164. Args: cobra.ExactArgs(0),
  165. RunE: func(cmd *cobra.Command, args []string) error {
  166. return cmdInfo(cmd, args)
  167. },
  168. }
  169. var setOptionCmd = &cobra.Command{
  170. Use: "set_option",
  171. Short: "Set an option on the application",
  172. Long: "",
  173. Args: cobra.ExactArgs(2),
  174. RunE: func(cmd *cobra.Command, args []string) error {
  175. return cmdSetOption(cmd, args)
  176. },
  177. }
  178. var deliverTxCmd = &cobra.Command{
  179. Use: "deliver_tx",
  180. Short: "Deliver a new transaction to the application",
  181. Long: "",
  182. Args: cobra.ExactArgs(1),
  183. RunE: func(cmd *cobra.Command, args []string) error {
  184. return cmdDeliverTx(cmd, args)
  185. },
  186. }
  187. var checkTxCmd = &cobra.Command{
  188. Use: "check_tx",
  189. Short: "Validate a transaction",
  190. Long: "",
  191. Args: cobra.ExactArgs(1),
  192. RunE: func(cmd *cobra.Command, args []string) error {
  193. return cmdCheckTx(cmd, args)
  194. },
  195. }
  196. var commitCmd = &cobra.Command{
  197. Use: "commit",
  198. Short: "Commit the application state and return the Merkle root hash",
  199. Long: "",
  200. Args: cobra.ExactArgs(0),
  201. RunE: func(cmd *cobra.Command, args []string) error {
  202. return cmdCommit(cmd, args)
  203. },
  204. }
  205. var versionCmd = &cobra.Command{
  206. Use: "version",
  207. Short: "Print abci console version",
  208. Long: "",
  209. Args: cobra.ExactArgs(0),
  210. RunE: func(cmd *cobra.Command, args []string) error {
  211. fmt.Println(version.Version)
  212. return nil
  213. },
  214. }
  215. var queryCmd = &cobra.Command{
  216. Use: "query",
  217. Short: "Query the application state",
  218. Long: "",
  219. Args: cobra.ExactArgs(1),
  220. RunE: func(cmd *cobra.Command, args []string) error {
  221. return cmdQuery(cmd, args)
  222. },
  223. }
  224. var counterCmd = &cobra.Command{
  225. Use: "counter",
  226. Short: "ABCI demo example",
  227. Long: "",
  228. Args: cobra.ExactArgs(0),
  229. RunE: func(cmd *cobra.Command, args []string) error {
  230. return cmdCounter(cmd, args)
  231. },
  232. }
  233. var dummyCmd = &cobra.Command{
  234. Use: "dummy",
  235. Short: "ABCI demo example",
  236. Long: "",
  237. Args: cobra.ExactArgs(0),
  238. RunE: func(cmd *cobra.Command, args []string) error {
  239. return cmdDummy(cmd, args)
  240. },
  241. }
  242. // Generates new Args array based off of previous call args to maintain flag persistence
  243. func persistentArgs(line []byte) []string {
  244. // generate the arguments to run from original os.Args
  245. // to maintain flag arguments
  246. args := os.Args
  247. args = args[:len(args)-1] // remove the previous command argument
  248. if len(line) > 0 { // prevents introduction of extra space leading to argument parse errors
  249. args = append(args, strings.Split(string(line), " ")...)
  250. }
  251. return args
  252. }
  253. //--------------------------------------------------------------------------------
  254. func cmdBatch(cmd *cobra.Command, args []string) error {
  255. bufReader := bufio.NewReader(os.Stdin)
  256. for {
  257. line, more, err := bufReader.ReadLine()
  258. if more {
  259. return errors.New("Input line is too long")
  260. } else if err == io.EOF {
  261. break
  262. } else if len(line) == 0 {
  263. continue
  264. } else if err != nil {
  265. return err
  266. }
  267. pArgs := persistentArgs(line)
  268. out, err := exec.Command(pArgs[0], pArgs[1:]...).Output() // nolint: gas
  269. if err != nil {
  270. return err
  271. }
  272. fmt.Println(string(out))
  273. }
  274. return nil
  275. }
  276. func cmdConsole(cmd *cobra.Command, args []string) error {
  277. for {
  278. fmt.Printf("> ")
  279. bufReader := bufio.NewReader(os.Stdin)
  280. line, more, err := bufReader.ReadLine()
  281. if more {
  282. return errors.New("Input is too long")
  283. } else if err != nil {
  284. return err
  285. }
  286. pArgs := persistentArgs(line)
  287. out, err := exec.Command(pArgs[0], pArgs[1:]...).Output() // nolint: gas
  288. if err != nil {
  289. return err
  290. }
  291. fmt.Println(string(out))
  292. }
  293. return nil
  294. }
  295. // Have the application echo a message
  296. func cmdEcho(cmd *cobra.Command, args []string) error {
  297. res, err := client.EchoSync(args[0])
  298. if err != nil {
  299. return err
  300. }
  301. printResponse(cmd, args, response{
  302. Data: []byte(res.Message),
  303. })
  304. return nil
  305. }
  306. // Get some info from the application
  307. func cmdInfo(cmd *cobra.Command, args []string) error {
  308. var version string
  309. if len(args) == 1 {
  310. version = args[0]
  311. }
  312. res, err := client.InfoSync(types.RequestInfo{version})
  313. if err != nil {
  314. return err
  315. }
  316. printResponse(cmd, args, response{
  317. Data: []byte(res.Data),
  318. })
  319. return nil
  320. }
  321. // Set an option on the application
  322. func cmdSetOption(cmd *cobra.Command, args []string) error {
  323. key, val := args[0], args[1]
  324. res, err := client.SetOptionSync(types.RequestSetOption{key, val})
  325. if err != nil {
  326. return err
  327. }
  328. printResponse(cmd, args, response{
  329. Log: res.Log,
  330. })
  331. return nil
  332. }
  333. // Append a new tx to application
  334. func cmdDeliverTx(cmd *cobra.Command, args []string) error {
  335. txBytes, err := stringOrHexToBytes(args[0])
  336. if err != nil {
  337. return err
  338. }
  339. res, err := client.DeliverTxSync(txBytes)
  340. if err != nil {
  341. return err
  342. }
  343. printResponse(cmd, args, response{
  344. Code: res.Code,
  345. Data: res.Data,
  346. Log: res.Log,
  347. })
  348. return nil
  349. }
  350. // Validate a tx
  351. func cmdCheckTx(cmd *cobra.Command, args []string) error {
  352. txBytes, err := stringOrHexToBytes(args[0])
  353. if err != nil {
  354. return err
  355. }
  356. res, err := client.CheckTxSync(txBytes)
  357. if err != nil {
  358. return err
  359. }
  360. printResponse(cmd, args, response{
  361. Code: res.Code,
  362. Data: res.Data,
  363. Log: res.Log,
  364. })
  365. return nil
  366. }
  367. // Get application Merkle root hash
  368. func cmdCommit(cmd *cobra.Command, args []string) error {
  369. res, err := client.CommitSync()
  370. if err != nil {
  371. return err
  372. }
  373. printResponse(cmd, args, response{
  374. Code: res.Code,
  375. Data: res.Data,
  376. Log: res.Log,
  377. })
  378. return nil
  379. }
  380. // Query application state
  381. func cmdQuery(cmd *cobra.Command, args []string) error {
  382. queryBytes, err := stringOrHexToBytes(args[0])
  383. if err != nil {
  384. return err
  385. }
  386. resQuery, err := client.QuerySync(types.RequestQuery{
  387. Data: queryBytes,
  388. Path: path,
  389. Height: uint64(height),
  390. Prove: prove,
  391. })
  392. if err != nil {
  393. return err
  394. }
  395. printResponse(cmd, args, response{
  396. Code: resQuery.Code,
  397. Log: resQuery.Log,
  398. Query: &queryResponse{
  399. Key: resQuery.Key,
  400. Value: resQuery.Value,
  401. Height: resQuery.Height,
  402. Proof: resQuery.Proof,
  403. },
  404. })
  405. return nil
  406. }
  407. func cmdCounter(cmd *cobra.Command, args []string) error {
  408. app := counter.NewCounterApplication(serial)
  409. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  410. // Start the listener
  411. srv, err := server.NewServer(addrC, abci, app)
  412. if err != nil {
  413. return err
  414. }
  415. srv.SetLogger(logger.With("module", "abci-server"))
  416. if err := srv.Start(); err != nil {
  417. return err
  418. }
  419. // Wait forever
  420. cmn.TrapSignal(func() {
  421. // Cleanup
  422. srv.Stop()
  423. })
  424. return nil
  425. }
  426. func cmdDummy(cmd *cobra.Command, args []string) error {
  427. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  428. // Create the application - in memory or persisted to disk
  429. var app types.Application
  430. if persist == "" {
  431. app = dummy.NewDummyApplication()
  432. } else {
  433. app = dummy.NewPersistentDummyApplication(persist)
  434. app.(*dummy.PersistentDummyApplication).SetLogger(logger.With("module", "dummy"))
  435. }
  436. // Start the listener
  437. srv, err := server.NewServer(addrD, abci, app)
  438. if err != nil {
  439. return err
  440. }
  441. srv.SetLogger(logger.With("module", "abci-server"))
  442. if err := srv.Start(); err != nil {
  443. return err
  444. }
  445. // Wait forever
  446. cmn.TrapSignal(func() {
  447. // Cleanup
  448. srv.Stop()
  449. })
  450. return nil
  451. }
  452. //--------------------------------------------------------------------------------
  453. func printResponse(cmd *cobra.Command, args []string, rsp response) {
  454. if verbose {
  455. fmt.Println(">", cmd.Use, strings.Join(args, " "))
  456. }
  457. // Always print the status code.
  458. if rsp.Code == types.CodeTypeOK {
  459. fmt.Printf("-> code: OK\n")
  460. } else {
  461. fmt.Printf("-> code: %d\n", rsp.Code)
  462. }
  463. if len(rsp.Data) != 0 {
  464. // Do no print this line when using the commit command
  465. // because the string comes out as gibberish
  466. if cmd.Use != "commit" {
  467. fmt.Printf("-> data: %s\n", rsp.Data)
  468. }
  469. fmt.Printf("-> data.hex: 0x%X\n", rsp.Data)
  470. }
  471. if rsp.Log != "" {
  472. fmt.Printf("-> log: %s\n", rsp.Log)
  473. }
  474. if rsp.Query != nil {
  475. fmt.Printf("-> height: %d\n", rsp.Query.Height)
  476. if rsp.Query.Key != nil {
  477. fmt.Printf("-> key: %s\n", rsp.Query.Key)
  478. fmt.Printf("-> key.hex: %X\n", rsp.Query.Key)
  479. }
  480. if rsp.Query.Value != nil {
  481. fmt.Printf("-> value: %s\n", rsp.Query.Value)
  482. fmt.Printf("-> value.hex: %X\n", rsp.Query.Value)
  483. }
  484. if rsp.Query.Proof != nil {
  485. fmt.Printf("-> proof: %X\n", rsp.Query.Proof)
  486. }
  487. }
  488. }
  489. // NOTE: s is interpreted as a string unless prefixed with 0x
  490. func stringOrHexToBytes(s string) ([]byte, error) {
  491. if len(s) > 2 && strings.ToLower(s[:2]) == "0x" {
  492. b, err := hex.DecodeString(s[2:])
  493. if err != nil {
  494. err = fmt.Errorf("Error decoding hex argument: %s", err.Error())
  495. return nil, err
  496. }
  497. return b, nil
  498. }
  499. if !strings.HasPrefix(s, "\"") || !strings.HasSuffix(s, "\"") {
  500. err := fmt.Errorf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string", s)
  501. return nil, err
  502. }
  503. return []byte(s[1 : len(s)-1]), nil
  504. }