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.

351 lines
9.1 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
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. # Using ABCI-CLI
  2. To facilitate testing and debugging of ABCI servers and simple apps, we
  3. built a CLI, the `abci-cli`, for sending ABCI messages from the command
  4. line.
  5. ## Install
  6. Make sure you [have Go installed](https://golang.org/doc/install).
  7. Next, install the `abci-cli` tool and example applications:
  8. ```
  9. mkdir -p $GOPATH/src/github.com/tendermint
  10. cd $GOPATH/src/github.com/tendermint
  11. git clone https://github.com/tendermint/tendermint.git
  12. cd tendermint
  13. make get_tools
  14. make get_vendor_deps
  15. make install_abci
  16. ```
  17. Now run `abci-cli` to see the list of commands:
  18. ```
  19. Usage:
  20. abci-cli [command]
  21. Available Commands:
  22. batch Run a batch of abci commands against an application
  23. check_tx Validate a tx
  24. commit Commit the application state and return the Merkle root hash
  25. console Start an interactive abci console for multiple commands
  26. counter ABCI demo example
  27. deliver_tx Deliver a new tx to the application
  28. kvstore ABCI demo example
  29. echo Have the application echo a message
  30. help Help about any command
  31. info Get some info about the application
  32. query Query the application state
  33. set_option Set an options on the application
  34. Flags:
  35. --abci string socket or grpc (default "socket")
  36. --address string address of application socket (default "tcp://127.0.0.1:26658")
  37. -h, --help help for abci-cli
  38. -v, --verbose print the command and results as if it were a console session
  39. Use "abci-cli [command] --help" for more information about a command.
  40. ```
  41. ## KVStore - First Example
  42. The `abci-cli` tool lets us send ABCI messages to our application, to
  43. help build and debug them.
  44. The most important messages are `deliver_tx`, `check_tx`, and `commit`,
  45. but there are others for convenience, configuration, and information
  46. purposes.
  47. We'll start a kvstore application, which was installed at the same time
  48. as `abci-cli` above. The kvstore just stores transactions in a merkle
  49. tree.
  50. Its code can be found
  51. [here](https://github.com/tendermint/tendermint/blob/develop/abci/cmd/abci-cli/abci-cli.go)
  52. and looks like:
  53. ```
  54. func cmdKVStore(cmd *cobra.Command, args []string) error {
  55. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  56. // Create the application - in memory or persisted to disk
  57. var app types.Application
  58. if flagPersist == "" {
  59. app = kvstore.NewKVStoreApplication()
  60. } else {
  61. app = kvstore.NewPersistentKVStoreApplication(flagPersist)
  62. app.(*kvstore.PersistentKVStoreApplication).SetLogger(logger.With("module", "kvstore"))
  63. }
  64. // Start the listener
  65. srv, err := server.NewServer(flagAddrD, flagAbci, app)
  66. if err != nil {
  67. return err
  68. }
  69. srv.SetLogger(logger.With("module", "abci-server"))
  70. if err := srv.Start(); err != nil {
  71. return err
  72. }
  73. // Wait forever
  74. cmn.TrapSignal(func() {
  75. // Cleanup
  76. srv.Stop()
  77. })
  78. return nil
  79. }
  80. ```
  81. Start by running:
  82. ```
  83. abci-cli kvstore
  84. ```
  85. And in another terminal, run
  86. ```
  87. abci-cli echo hello
  88. abci-cli info
  89. ```
  90. You'll see something like:
  91. ```
  92. -> data: hello
  93. -> data.hex: 68656C6C6F
  94. ```
  95. and:
  96. ```
  97. -> data: {"size":0}
  98. -> data.hex: 7B2273697A65223A307D
  99. ```
  100. An ABCI application must provide two things:
  101. - a socket server
  102. - a handler for ABCI messages
  103. When we run the `abci-cli` tool we open a new connection to the
  104. application's socket server, send the given ABCI message, and wait for a
  105. response.
  106. The server may be generic for a particular language, and we provide a
  107. [reference implementation in
  108. Golang](https://github.com/tendermint/tendermint/tree/develop/abci/server). See the
  109. [list of other ABCI implementations](./ecosystem.md) for servers in
  110. other languages.
  111. The handler is specific to the application, and may be arbitrary, so
  112. long as it is deterministic and conforms to the ABCI interface
  113. specification.
  114. So when we run `abci-cli info`, we open a new connection to the ABCI
  115. server, which calls the `Info()` method on the application, which tells
  116. us the number of transactions in our Merkle tree.
  117. Now, since every command opens a new connection, we provide the
  118. `abci-cli console` and `abci-cli batch` commands, to allow multiple ABCI
  119. messages to be sent over a single connection.
  120. Running `abci-cli console` should drop you in an interactive console for
  121. speaking ABCI messages to your application.
  122. Try running these commands:
  123. ```
  124. > echo hello
  125. -> code: OK
  126. -> data: hello
  127. -> data.hex: 0x68656C6C6F
  128. > info
  129. -> code: OK
  130. -> data: {"size":0}
  131. -> data.hex: 0x7B2273697A65223A307D
  132. > commit
  133. -> code: OK
  134. -> data.hex: 0x0000000000000000
  135. > deliver_tx "abc"
  136. -> code: OK
  137. > info
  138. -> code: OK
  139. -> data: {"size":1}
  140. -> data.hex: 0x7B2273697A65223A317D
  141. > commit
  142. -> code: OK
  143. -> data.hex: 0x0200000000000000
  144. > query "abc"
  145. -> code: OK
  146. -> log: exists
  147. -> height: 0
  148. -> value: abc
  149. -> value.hex: 616263
  150. > deliver_tx "def=xyz"
  151. -> code: OK
  152. > commit
  153. -> code: OK
  154. -> data.hex: 0x0400000000000000
  155. > query "def"
  156. -> code: OK
  157. -> log: exists
  158. -> height: 0
  159. -> value: xyz
  160. -> value.hex: 78797A
  161. ```
  162. Note that if we do `deliver_tx "abc"` it will store `(abc, abc)`, but if
  163. we do `deliver_tx "abc=efg"` it will store `(abc, efg)`.
  164. Similarly, you could put the commands in a file and run
  165. `abci-cli --verbose batch < myfile`.
  166. ## Counter - Another Example
  167. Now that we've got the hang of it, let's try another application, the
  168. "counter" app.
  169. Like the kvstore app, its code can be found
  170. [here](https://github.com/tendermint/tendermint/blob/master/abci/cmd/abci-cli/abci-cli.go)
  171. and looks like:
  172. ```
  173. func cmdCounter(cmd *cobra.Command, args []string) error {
  174. app := counter.NewCounterApplication(flagSerial)
  175. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  176. // Start the listener
  177. srv, err := server.NewServer(flagAddrC, flagAbci, app)
  178. if err != nil {
  179. return err
  180. }
  181. srv.SetLogger(logger.With("module", "abci-server"))
  182. if err := srv.Start(); err != nil {
  183. return err
  184. }
  185. // Wait forever
  186. cmn.TrapSignal(func() {
  187. // Cleanup
  188. srv.Stop()
  189. })
  190. return nil
  191. }
  192. ```
  193. The counter app doesn't use a Merkle tree, it just counts how many times
  194. we've sent a transaction, asked for a hash, or committed the state. The
  195. result of `commit` is just the number of transactions sent.
  196. This application has two modes: `serial=off` and `serial=on`.
  197. When `serial=on`, transactions must be a big-endian encoded incrementing
  198. integer, starting at 0.
  199. If `serial=off`, there are no restrictions on transactions.
  200. We can toggle the value of `serial` using the `set_option` ABCI message.
  201. When `serial=on`, some transactions are invalid. In a live blockchain,
  202. transactions collect in memory before they are committed into blocks. To
  203. avoid wasting resources on invalid transactions, ABCI provides the
  204. `check_tx` message, which application developers can use to accept or
  205. reject transactions, before they are stored in memory or gossipped to
  206. other peers.
  207. In this instance of the counter app, `check_tx` only allows transactions
  208. whose integer is greater than the last committed one.
  209. Let's kill the console and the kvstore application, and start the
  210. counter app:
  211. ```
  212. abci-cli counter
  213. ```
  214. In another window, start the `abci-cli console`:
  215. ```
  216. > set_option serial on
  217. -> code: OK
  218. -> log: OK (SetOption doesn't return anything.)
  219. > check_tx 0x00
  220. -> code: OK
  221. > check_tx 0xff
  222. -> code: OK
  223. > deliver_tx 0x00
  224. -> code: OK
  225. > check_tx 0x00
  226. -> code: BadNonce
  227. -> log: Invalid nonce. Expected >= 1, got 0
  228. > deliver_tx 0x01
  229. -> code: OK
  230. > deliver_tx 0x04
  231. -> code: BadNonce
  232. -> log: Invalid nonce. Expected 2, got 4
  233. > info
  234. -> code: OK
  235. -> data: {"hashes":0,"txs":2}
  236. -> data.hex: 0x7B22686173686573223A302C22747873223A327D
  237. ```
  238. This is a very simple application, but between `counter` and `kvstore`,
  239. its easy to see how you can build out arbitrary application states on
  240. top of the ABCI. [Hyperledger's
  241. Burrow](https://github.com/hyperledger/burrow) also runs atop ABCI,
  242. bringing with it Ethereum-like accounts, the Ethereum virtual-machine,
  243. Monax's permissioning scheme, and native contracts extensions.
  244. But the ultimate flexibility comes from being able to write the
  245. application easily in any language.
  246. We have implemented the counter in a number of languages [see the
  247. example directory](https://github.com/tendermint/tendermint/tree/develop/abci/example).
  248. To run the Node JS version, `cd` to `example/js` and run
  249. ```
  250. node app.js
  251. ```
  252. (you'll have to kill the other counter application process). In another
  253. window, run the console and those previous ABCI commands. You should get
  254. the same results as for the Go version.
  255. ## Bounties
  256. Want to write the counter app in your favorite language?! We'd be happy
  257. to add you to our [ecosystem](https://tendermint.com/ecosystem)! We're
  258. also offering [bounties](https://hackerone.com/tendermint/) for
  259. implementations in new languages!
  260. The `abci-cli` is designed strictly for testing and debugging. In a real
  261. deployment, the role of sending messages is taken by Tendermint, which
  262. connects to the app using three separate connections, each with its own
  263. pattern of messages.
  264. For more information, see the [application developers
  265. guide](./app-development.md). For examples of running an ABCI app with
  266. Tendermint, see the [getting started guide](./getting-started.md).
  267. Next is the ABCI specification.