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.

548 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
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
9 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() {
  84. addGlobalFlags()
  85. addCommands()
  86. 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()
  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()
  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. resLog, err := client.SetOptionSync(args[0], args[1])
  316. if err != nil {
  317. return err
  318. }
  319. printResponse(cmd, args, response{
  320. Log: resLog,
  321. })
  322. return nil
  323. }
  324. // Append a new tx to application
  325. func cmdDeliverTx(cmd *cobra.Command, args []string) error {
  326. txBytes, err := stringOrHexToBytes(args[0])
  327. if err != nil {
  328. return err
  329. }
  330. res := client.DeliverTxSync(txBytes)
  331. printResponse(cmd, args, response{
  332. Code: res.Code,
  333. Data: res.Data,
  334. Log: res.Log,
  335. })
  336. return nil
  337. }
  338. // Validate a tx
  339. func cmdCheckTx(cmd *cobra.Command, args []string) error {
  340. txBytes, err := stringOrHexToBytes(args[0])
  341. if err != nil {
  342. return err
  343. }
  344. res := client.CheckTxSync(txBytes)
  345. printResponse(cmd, args, response{
  346. Code: res.Code,
  347. Data: res.Data,
  348. Log: res.Log,
  349. })
  350. return nil
  351. }
  352. // Get application Merkle root hash
  353. func cmdCommit(cmd *cobra.Command, args []string) error {
  354. res := client.CommitSync()
  355. printResponse(cmd, args, response{
  356. Code: res.Code,
  357. Data: res.Data,
  358. Log: res.Log,
  359. })
  360. return nil
  361. }
  362. // Query application state
  363. func cmdQuery(cmd *cobra.Command, args []string) error {
  364. queryBytes, err := stringOrHexToBytes(args[0])
  365. if err != nil {
  366. return err
  367. }
  368. resQuery, err := client.QuerySync(types.RequestQuery{
  369. Data: queryBytes,
  370. Path: path,
  371. Height: uint64(height),
  372. Prove: prove,
  373. })
  374. if err != nil {
  375. return err
  376. }
  377. printResponse(cmd, args, response{
  378. Code: resQuery.Code,
  379. Log: resQuery.Log,
  380. Query: &queryResponse{
  381. Key: resQuery.Key,
  382. Value: resQuery.Value,
  383. Height: resQuery.Height,
  384. Proof: resQuery.Proof,
  385. },
  386. })
  387. return nil
  388. }
  389. func cmdCounter(cmd *cobra.Command, args []string) error {
  390. app := counter.NewCounterApplication(serial)
  391. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  392. // Start the listener
  393. srv, err := server.NewServer(addrC, abci, app)
  394. if err != nil {
  395. return err
  396. }
  397. srv.SetLogger(logger.With("module", "abci-server"))
  398. if _, err := srv.Start(); err != nil {
  399. return err
  400. }
  401. // Wait forever
  402. cmn.TrapSignal(func() {
  403. // Cleanup
  404. srv.Stop()
  405. })
  406. return nil
  407. }
  408. func cmdDummy(cmd *cobra.Command, args []string) error {
  409. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  410. // Create the application - in memory or persisted to disk
  411. var app types.Application
  412. if persist == "" {
  413. app = dummy.NewDummyApplication()
  414. } else {
  415. app = dummy.NewPersistentDummyApplication(persist)
  416. app.(*dummy.PersistentDummyApplication).SetLogger(logger.With("module", "dummy"))
  417. }
  418. // Start the listener
  419. srv, err := server.NewServer(addrD, abci, app)
  420. if err != nil {
  421. return err
  422. }
  423. srv.SetLogger(logger.With("module", "abci-server"))
  424. if _, err := srv.Start(); err != nil {
  425. return err
  426. }
  427. // Wait forever
  428. cmn.TrapSignal(func() {
  429. // Cleanup
  430. srv.Stop()
  431. })
  432. return nil
  433. }
  434. //--------------------------------------------------------------------------------
  435. func printResponse(cmd *cobra.Command, args []string, rsp response) {
  436. if verbose {
  437. fmt.Println(">", cmd.Use, strings.Join(args, " "))
  438. }
  439. // Always print the status code.
  440. fmt.Printf("-> code: %s\n", rsp.Code.String())
  441. if len(rsp.Data) != 0 {
  442. // Do no print this line when using the commit command
  443. // because the string comes out as gibberish
  444. if cmd.Use != "commit" {
  445. fmt.Printf("-> data: %s\n", rsp.Data)
  446. }
  447. fmt.Printf("-> data.hex: 0x%X\n", rsp.Data)
  448. }
  449. if rsp.Log != "" {
  450. fmt.Printf("-> log: %s\n", rsp.Log)
  451. }
  452. if rsp.Query != nil {
  453. fmt.Printf("-> height: %d\n", rsp.Query.Height)
  454. if rsp.Query.Key != nil {
  455. fmt.Printf("-> key: %s\n", rsp.Query.Key)
  456. fmt.Printf("-> key.hex: %X\n", rsp.Query.Key)
  457. }
  458. if rsp.Query.Value != nil {
  459. fmt.Printf("-> value: %s\n", rsp.Query.Value)
  460. fmt.Printf("-> value.hex: %X\n", rsp.Query.Value)
  461. }
  462. if rsp.Query.Proof != nil {
  463. fmt.Printf("-> proof: %X\n", rsp.Query.Proof)
  464. }
  465. }
  466. }
  467. // NOTE: s is interpreted as a string unless prefixed with 0x
  468. func stringOrHexToBytes(s string) ([]byte, error) {
  469. if len(s) > 2 && strings.ToLower(s[:2]) == "0x" {
  470. b, err := hex.DecodeString(s[2:])
  471. if err != nil {
  472. err = fmt.Errorf("Error decoding hex argument: %s", err.Error())
  473. return nil, err
  474. }
  475. return b, nil
  476. }
  477. if !strings.HasPrefix(s, "\"") || !strings.HasSuffix(s, "\"") {
  478. err := fmt.Errorf("Invalid string arg: \"%s\". Must be quoted or a \"0x\"-prefixed hex string", s)
  479. return nil, err
  480. }
  481. return []byte(s[1 : len(s)-1]), nil
  482. }