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.

558 lines
13 KiB

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