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.

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