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.

257 lines
7.2 KiB

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