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.

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