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.

292 lines
7.9 KiB

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