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.

230 lines
5.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. ---
  2. order: 2
  3. ---
  4. # Using ABCI-CLI
  5. To facilitate testing and debugging of ABCI servers and simple apps, we
  6. built a CLI, the `abci-cli`, for sending ABCI messages from the command
  7. line.
  8. ## Install
  9. Make sure you [have Go installed](https://golang.org/doc/install).
  10. Next, install the `abci-cli` tool and example applications:
  11. ```sh
  12. git clone https://github.com/tendermint/tendermint.git
  13. cd tendermint
  14. make install_abci
  15. ```
  16. Now run `abci-cli` to see the list of commands:
  17. ```sh
  18. Usage:
  19. abci-cli [command]
  20. Available Commands:
  21. batch Run a batch of abci commands against an application
  22. check_tx Validate a tx
  23. commit Commit the application state and return the Merkle root hash
  24. console Start an interactive abci console for multiple commands
  25. deliver_tx Deliver a new tx to the application
  26. kvstore ABCI demo example
  27. echo Have the application echo a message
  28. help Help about any command
  29. info Get some info about the application
  30. query Query the application state
  31. set_option Set an options on the application
  32. Flags:
  33. --abci string socket or grpc (default "socket")
  34. --address string address of application socket (default "tcp://127.0.0.1:26658")
  35. -h, --help help for abci-cli
  36. -v, --verbose print the command and results as if it were a console session
  37. Use "abci-cli [command] --help" for more information about a command.
  38. ```
  39. ## KVStore - First Example
  40. The `abci-cli` tool lets us send ABCI messages to our application, to
  41. help build and debug them.
  42. The most important messages are `deliver_tx`, `check_tx`, and `commit`,
  43. but there are others for convenience, configuration, and information
  44. purposes.
  45. We'll start a kvstore application, which was installed at the same time
  46. as `abci-cli` above. The kvstore just stores transactions in a merkle
  47. tree.
  48. Its code can be found
  49. [here](https://github.com/tendermint/tendermint/blob/master/abci/cmd/abci-cli/abci-cli.go)
  50. and looks like:
  51. ```go
  52. func cmdKVStore(cmd *cobra.Command, args []string) error {
  53. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  54. // Create the application - in memory or persisted to disk
  55. var app types.Application
  56. if flagPersist == "" {
  57. app = kvstore.NewKVStoreApplication()
  58. } else {
  59. app = kvstore.NewPersistentKVStoreApplication(flagPersist)
  60. app.(*kvstore.PersistentKVStoreApplication).SetLogger(logger.With("module", "kvstore"))
  61. }
  62. // Start the listener
  63. srv, err := server.NewServer(flagAddrD, flagAbci, app)
  64. if err != nil {
  65. return err
  66. }
  67. srv.SetLogger(logger.With("module", "abci-server"))
  68. if err := srv.Start(); err != nil {
  69. return err
  70. }
  71. // Stop upon receiving SIGTERM or CTRL-C.
  72. tmos.TrapSignal(logger, func() {
  73. // Cleanup
  74. srv.Stop()
  75. })
  76. // Run forever.
  77. select {}
  78. }
  79. ```
  80. Start by running:
  81. ```sh
  82. abci-cli kvstore
  83. ```
  84. And in another terminal, run
  85. ```sh
  86. abci-cli echo hello
  87. abci-cli info
  88. ```
  89. You'll see something like:
  90. ```sh
  91. -> data: hello
  92. -> data.hex: 68656C6C6F
  93. ```
  94. and:
  95. ```sh
  96. -> data: {"size":0}
  97. -> data.hex: 7B2273697A65223A307D
  98. ```
  99. An ABCI application must provide two things:
  100. - a socket server
  101. - a handler for ABCI messages
  102. When we run the `abci-cli` tool we open a new connection to the
  103. application's socket server, send the given ABCI message, and wait for a
  104. response.
  105. The server may be generic for a particular language, and we provide a
  106. [reference implementation in
  107. Golang](https://github.com/tendermint/tendermint/tree/master/abci/server). See the
  108. [list of other ABCI implementations](https://github.com/tendermint/awesome#ecosystem) for servers in
  109. other languages.
  110. The handler is specific to the application, and may be arbitrary, so
  111. long as it is deterministic and conforms to the ABCI interface
  112. specification.
  113. So when we run `abci-cli info`, we open a new connection to the ABCI
  114. server, which calls the `Info()` method on the application, which tells
  115. us the number of transactions in our Merkle tree.
  116. Now, since every command opens a new connection, we provide the
  117. `abci-cli console` and `abci-cli batch` commands, to allow multiple ABCI
  118. messages to be sent over a single connection.
  119. Running `abci-cli console` should drop you in an interactive console for
  120. speaking ABCI messages to your application.
  121. Try running these commands:
  122. ```sh
  123. > echo hello
  124. -> code: OK
  125. -> data: hello
  126. -> data.hex: 0x68656C6C6F
  127. > info
  128. -> code: OK
  129. -> data: {"size":0}
  130. -> data.hex: 0x7B2273697A65223A307D
  131. > commit
  132. -> code: OK
  133. -> data.hex: 0x0000000000000000
  134. > deliver_tx "abc"
  135. -> code: OK
  136. > info
  137. -> code: OK
  138. -> data: {"size":1}
  139. -> data.hex: 0x7B2273697A65223A317D
  140. > commit
  141. -> code: OK
  142. -> data.hex: 0x0200000000000000
  143. > query "abc"
  144. -> code: OK
  145. -> log: exists
  146. -> height: 2
  147. -> value: abc
  148. -> value.hex: 616263
  149. > deliver_tx "def=xyz"
  150. -> code: OK
  151. > commit
  152. -> code: OK
  153. -> data.hex: 0x0400000000000000
  154. > query "def"
  155. -> code: OK
  156. -> log: exists
  157. -> height: 3
  158. -> value: xyz
  159. -> value.hex: 78797A
  160. ```
  161. Note that if we do `deliver_tx "abc"` it will store `(abc, abc)`, but if
  162. we do `deliver_tx "abc=efg"` it will store `(abc, efg)`.
  163. Similarly, you could put the commands in a file and run
  164. `abci-cli --verbose batch < myfile`.
  165. ## Bounties
  166. Want to write an app in your favorite language?! We'd be happy
  167. to add you to our [ecosystem](https://github.com/tendermint/awesome#ecosystem)!
  168. See [funding](https://github.com/interchainio/funding) opportunities from the
  169. [Interchain Foundation](https://interchain.io/) for implementations in new languages and more.
  170. The `abci-cli` is designed strictly for testing and debugging. In a real
  171. deployment, the role of sending messages is taken by Tendermint, which
  172. connects to the app using three separate connections, each with its own
  173. pattern of messages.
  174. For examples of running an ABCI app with
  175. Tendermint, see the [getting started guide](./getting-started.md).
  176. Next is the ABCI specification.