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.

336 lines
9.4 KiB

  1. First Tendermint App
  2. ====================
  3. As a general purpose blockchain engine, Tendermint is agnostic to the
  4. application you want to run. So, to run a complete blockchain that does
  5. something useful, you must start two programs: one is Tendermint Core,
  6. the other is your application, which can be written in any programming
  7. language. Recall from `the intro to ABCI <introduction.rst#ABCI-Overview>`__ that
  8. Tendermint Core handles all the p2p and consensus stuff, and just
  9. forwards transactions to the application when they need to be validated,
  10. or when they're ready to be committed to a block.
  11. In this guide, we show you some examples of how to run an application
  12. using Tendermint.
  13. Install
  14. -------
  15. The first apps we will work with are written in Go. To install them, you
  16. need to `install Go <https://golang.org/doc/install>`__ and put
  17. ``$GOPATH/bin`` in your
  18. ``$PATH``; see `here <https://github.com/tendermint/tendermint/wiki/Setting-GOPATH>`__ for more info.
  19. Then run
  20. ::
  21. go get -u github.com/tendermint/abci/cmd/...
  22. If there is an error, install and run the ``glide`` tool to pin the
  23. dependencies:
  24. ::
  25. go get github.com/Masterminds/glide
  26. cd $GOPATH/src/github.com/tendermint/abci
  27. glide install
  28. go install ./cmd/...
  29. Now you should have the ``abci-cli`` plus two apps installed:
  30. ::
  31. dummy --help
  32. counter --help
  33. These binaries are installed on ``$GOPATH/bin`` and all come from within
  34. the ``./cmd/...`` directory of the abci repository.
  35. Both of these example applications are in Go. See below for an
  36. application written in Javascript.
  37. Now, let's run some apps!
  38. Dummy - A First Example
  39. -----------------------
  40. The dummy app is a `Merkle
  41. tree <https://en.wikipedia.org/wiki/Merkle_tree>`__ that just stores all
  42. transactions. If the transaction contains an ``=``, eg. ``key=value``,
  43. then the ``value`` is stored under the ``key`` in the Merkle tree.
  44. Otherwise, the full transaction bytes are stored as the key and the
  45. value.
  46. Let's start a dummy application.
  47. ::
  48. dummy
  49. In another terminal, we can start Tendermint. If you have never run
  50. Tendermint before, use:
  51. ::
  52. tendermint init
  53. tendermint node
  54. If you have used Tendermint, you may want to reset the data for a new
  55. blockchain by running ``tendermint unsafe_reset_all``. Then you can run
  56. ``tendermint node`` to start Tendermint, and connect to the app. For
  57. more details, see `the guide on using
  58. Tendermint <./using-tendermint.html>`__.
  59. You should see Tendermint making blocks! We can get the status of our
  60. Tendermint node as follows:
  61. ::
  62. curl -s localhost:46657/status
  63. The ``-s`` just silences ``curl``. For nicer output, pipe the result
  64. into a tool like `jq <https://stedolan.github.io/jq/>`__ or
  65. `jsonpp <https://github.com/jmhodges/jsonpp>`__.
  66. Now let's send some transactions to the dummy.
  67. ::
  68. curl -s 'localhost:46657/broadcast_tx_commit?tx="abcd"'
  69. Note the single quote (``'``) around the url, which ensures that the
  70. double quotes (``"``) are not escaped by bash. This command sent a
  71. transaction with bytes ``abcd``, so ``abcd`` will be stored as both the
  72. key and the value in the Merkle tree. The response should look something
  73. like:
  74. ::
  75. {
  76. "jsonrpc": "2.0",
  77. "id": "",
  78. "result": {
  79. "check_tx": {
  80. "code": 0,
  81. "data": "",
  82. "log": ""
  83. },
  84. "deliver_tx": {
  85. "code": 0,
  86. "data": "",
  87. "log": ""
  88. },
  89. "hash": "2B8EC32BA2579B3B8606E42C06DE2F7AFA2556EF",
  90. "height": 154
  91. }
  92. }
  93. We can confirm that our transaction worked and the value got stored by
  94. querying the app:
  95. ::
  96. curl -s 'localhost:46657/abci_query?data="abcd"'
  97. The result should look like:
  98. ::
  99. {
  100. "jsonrpc": "2.0",
  101. "id": "",
  102. "result": {
  103. "response": {
  104. "code": 0,
  105. "index": 0,
  106. "key": "",
  107. "value": "61626364",
  108. "proof": "",
  109. "height": 0,
  110. "log": "exists"
  111. }
  112. }
  113. }
  114. Note the ``value`` in the result (``61626364``); this is the
  115. hex-encoding of the ASCII of ``abcd``. You can verify this in
  116. a python shell by running ``"61626364".decode('hex')``. Stay
  117. tuned for a future release that makes this output more human-readable.
  118. Now let's try setting a different key and value:
  119. ::
  120. curl -s 'localhost:46657/broadcast_tx_commit?tx="name=satoshi"'
  121. Now if we query for ``name``, we should get ``satoshi``, or
  122. ``7361746F736869`` in hex:
  123. ::
  124. curl -s 'localhost:46657/abci_query?data="name"'
  125. Try some other transactions and queries to make sure everything is
  126. working!
  127. Counter - Another Example
  128. -------------------------
  129. Now that we've got the hang of it, let's try another application, the
  130. "counter" app.
  131. The counter app doesn't use a Merkle tree, it just counts how many times
  132. we've sent a transaction, or committed the state.
  133. This application has two modes: ``serial=off`` and ``serial=on``.
  134. When ``serial=on``, transactions must be a big-endian encoded
  135. incrementing integer, starting at 0.
  136. If ``serial=off``, there are no restrictions on transactions.
  137. In a live blockchain, transactions collect in memory before they are
  138. committed into blocks. To avoid wasting resources on invalid
  139. transactions, ABCI provides the ``CheckTx`` message, which application
  140. developers can use to accept or reject transactions, before they are
  141. stored in memory or gossipped to other peers.
  142. In this instance of the counter app, with ``serial=on``, ``CheckTx``
  143. only allows transactions whose integer is greater than the last
  144. committed one.
  145. Let's kill the previous instance of ``tendermint`` and the ``dummy``
  146. application, and start the counter app. We can enable ``serial=on`` with
  147. a flag:
  148. ::
  149. counter --serial
  150. In another window, reset then start Tendermint:
  151. ::
  152. tendermint unsafe_reset_all
  153. tendermint node
  154. Once again, you can see the blocks streaming by. Let's send some
  155. transactions. Since we have set ``serial=on``, the first transaction
  156. must be the number ``0``:
  157. ::
  158. curl localhost:46657/broadcast_tx_commit?tx=0x00
  159. Note the empty (hence successful) response. The next transaction must be
  160. the number ``1``. If instead, we try to send a ``5``, we get an error:
  161. ::
  162. > curl localhost:46657/broadcast_tx_commit?tx=0x05
  163. {
  164. "jsonrpc": "2.0",
  165. "id": "",
  166. "result": {
  167. "check_tx": {
  168. "code": 0,
  169. "data": "",
  170. "log": ""
  171. },
  172. "deliver_tx": {
  173. "code": 3,
  174. "data": "",
  175. "log": "Invalid nonce. Expected 1, got 5"
  176. },
  177. "hash": "33B93DFF98749B0D6996A70F64071347060DC19C",
  178. "height": 38
  179. }
  180. }
  181. But if we send a ``1``, it works again:
  182. ::
  183. > curl localhost:46657/broadcast_tx_commit?tx=0x01
  184. {
  185. "jsonrpc": "2.0",
  186. "id": "",
  187. "result": {
  188. "check_tx": {
  189. "code": 0,
  190. "data": "",
  191. "log": ""
  192. },
  193. "deliver_tx": {
  194. "code": 0,
  195. "data": "",
  196. "log": ""
  197. },
  198. "hash": "F17854A977F6FA7EEA1BD758E296710B86F72F3D",
  199. "height": 87
  200. }
  201. }
  202. For more details on the ``broadcast_tx`` API, see `the guide on using
  203. Tendermint <./using-tendermint.html>`__.
  204. CounterJS - Example in Another Language
  205. ---------------------------------------
  206. We also want to run applications in another language - in this case,
  207. we'll run a Javascript version of the ``counter``. To run it, you'll
  208. need to `install node <https://nodejs.org/en/download/>`__.
  209. You'll also need to fetch the relevant repository, from `here <https://github.com/tendermint/js-abci>`__ then install it. As go devs, we
  210. keep all our code under the ``$GOPATH``, so run:
  211. ::
  212. go get github.com/tendermint/js-abci &> /dev/null
  213. cd $GOPATH/src/github.com/tendermint/js-abci/example
  214. npm install
  215. Kill the previous ``counter`` and ``tendermint`` processes. Now run the
  216. app:
  217. ::
  218. node example/app.js
  219. In another window, reset and start ``tendermint``:
  220. ::
  221. tendermint unsafe_reset_all
  222. tendermint node
  223. Once again, you should see blocks streaming by - but now, our
  224. application is written in javascript! Try sending some transactions, and
  225. like before - the results should be the same:
  226. ::
  227. curl localhost:46657/broadcast_tx_commit?tx=0x00 # ok
  228. curl localhost:46657/broadcast_tx_commit?tx=0x05 # invalid nonce
  229. curl localhost:46657/broadcast_tx_commit?tx=0x01 # ok
  230. Neat, eh?
  231. Basecoin - A More Interesting Example
  232. -------------------------------------
  233. We saved the best for last; the `Cosmos SDK <https://github.com/cosmos/cosmos-sdk>`__ is a general purpose framework for building cryptocurrencies. Unlike the``dummy`` and ``counter``, which are strictly for example purposes. The reference implementation of Cosmos SDK is ``basecoin``, which demonstrates how to use the building blocks of the Cosmos SDK.
  234. The default ``basecoin`` application is a multi-asset cryptocurrency
  235. that supports inter-blockchain communication. For more details on how
  236. basecoin works and how to use it, see our `basecoin
  237. guide <https://github.com/cosmos/cosmos-sdk/blob/develop/docs/guide/basecoin-basics.md>`__
  238. In this tutorial you learned how to run applications using Tendermint
  239. on a single node. You saw how applications could be written in different
  240. languages, and how to send transactions and query for the latest state.
  241. But the true power of Tendermint comes from its ability to securely and
  242. efficiently run an application across a distributed network of nodes,
  243. while keeping them all in sync using its state-of-the-art consensus
  244. protocol. Next, we show you how to deploy Tendermint testnets.