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.

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