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.

337 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. Again, the ``112`` is the type-byte. Note the ``value`` in the result
  115. (``61626364``); this is the hex-encoding of the ASCII of ``abcd``. You
  116. can verify this in a python shell by running
  117. ``"61626364".decode('hex')``. Stay tuned for a future release that makes
  118. this output more human-readable ;).
  119. Now let's try setting a different key and value:
  120. ::
  121. curl -s 'localhost:46657/broadcast_tx_commit?tx="name=satoshi"'
  122. Now if we query for ``name``, we should get ``satoshi``, or
  123. ``7361746F736869`` in hex:
  124. ::
  125. curl -s 'localhost:46657/abci_query?data="name"'
  126. Try some other transactions and queries to make sure everything is
  127. working!
  128. Counter - Another Example
  129. -------------------------
  130. Now that we've got the hang of it, let's try another application, the
  131. "counter" app.
  132. The counter app doesn't use a Merkle tree, it just counts how many times
  133. we've sent a transaction, or committed the state.
  134. This application has two modes: ``serial=off`` and ``serial=on``.
  135. When ``serial=on``, transactions must be a big-endian encoded
  136. incrementing integer, starting at 0.
  137. If ``serial=off``, there are no restrictions on transactions.
  138. In a live blockchain, transactions collect in memory before they are
  139. committed into blocks. To avoid wasting resources on invalid
  140. transactions, ABCI provides the ``CheckTx`` message, which application
  141. developers can use to accept or reject transactions, before they are
  142. stored in memory or gossipped to other peers.
  143. In this instance of the counter app, with ``serial=on``, ``CheckTx``
  144. only allows transactions whose integer is greater than the last
  145. committed one.
  146. Let's kill the previous instance of ``tendermint`` and the ``dummy``
  147. application, and start the counter app. We can enable ``serial=on`` with
  148. a flag:
  149. ::
  150. counter --serial
  151. In another window, reset then start Tendermint:
  152. ::
  153. tendermint unsafe_reset_all
  154. tendermint node
  155. Once again, you can see the blocks streaming by. Let's send some
  156. transactions. Since we have set ``serial=on``, the first transaction
  157. must be the number ``0``:
  158. ::
  159. curl localhost:46657/broadcast_tx_commit?tx=0x00
  160. Note the empty (hence successful) response. The next transaction must be
  161. the number ``1``. If instead, we try to send a ``5``, we get an error:
  162. ::
  163. > curl localhost:46657/broadcast_tx_commit?tx=0x05
  164. {
  165. "jsonrpc": "2.0",
  166. "id": "",
  167. "result": {
  168. "check_tx": {
  169. "code": 0,
  170. "data": "",
  171. "log": ""
  172. },
  173. "deliver_tx": {
  174. "code": 3,
  175. "data": "",
  176. "log": "Invalid nonce. Expected 1, got 5"
  177. },
  178. "hash": "33B93DFF98749B0D6996A70F64071347060DC19C",
  179. "height": 38
  180. }
  181. }
  182. But if we send a ``1``, it works again:
  183. ::
  184. > curl localhost:46657/broadcast_tx_commit?tx=0x01
  185. {
  186. "jsonrpc": "2.0",
  187. "id": "",
  188. "result": {
  189. "check_tx": {
  190. "code": 0,
  191. "data": "",
  192. "log": ""
  193. },
  194. "deliver_tx": {
  195. "code": 0,
  196. "data": "",
  197. "log": ""
  198. },
  199. "hash": "F17854A977F6FA7EEA1BD758E296710B86F72F3D",
  200. "height": 87
  201. }
  202. }
  203. For more details on the ``broadcast_tx`` API, see `the guide on using
  204. Tendermint <./using-tendermint.html>`__.
  205. CounterJS - Example in Another Language
  206. ---------------------------------------
  207. We also want to run applications in another language - in this case,
  208. we'll run a Javascript version of the ``counter``. To run it, you'll
  209. need to `install node <https://nodejs.org/en/download/>`__.
  210. You'll also need to fetch the relevant repository, from `here <https://github.com/tendermint/js-abci>`__ then install it. As go devs, we
  211. keep all our code under the ``$GOPATH``, so run:
  212. ::
  213. go get github.com/tendermint/js-abci &> /dev/null
  214. cd $GOPATH/src/github.com/tendermint/js-abci/example
  215. npm install
  216. Kill the previous ``counter`` and ``tendermint`` processes. Now run the
  217. app:
  218. ::
  219. node example/app.js
  220. In another window, reset and start ``tendermint``:
  221. ::
  222. tendermint unsafe_reset_all
  223. tendermint node
  224. Once again, you should see blocks streaming by - but now, our
  225. application is written in javascript! Try sending some transactions, and
  226. like before - the results should be the same:
  227. ::
  228. curl localhost:46657/broadcast_tx_commit?tx=0x00 # ok
  229. curl localhost:46657/broadcast_tx_commit?tx=0x05 # invalid nonce
  230. curl localhost:46657/broadcast_tx_commit?tx=0x01 # ok
  231. Neat, eh?
  232. Basecoin - A More Interesting Example
  233. -------------------------------------
  234. 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.
  235. The default ``basecoin`` application is a multi-asset cryptocurrency
  236. that supports inter-blockchain communication. For more details on how
  237. basecoin works and how to use it, see our `basecoin
  238. guide <https://github.com/cosmos/cosmos-sdk/blob/develop/docs/guide/basecoin-basics.md>`__
  239. In this tutorial you learned how to run applications using Tendermint
  240. on a single node. You saw how applications could be written in different
  241. languages, and how to send transactions and query for the latest state.
  242. But the true power of Tendermint comes from its ability to securely and
  243. efficiently run an application across a distributed network of nodes,
  244. while keeping them all in sync using its state-of-the-art consensus
  245. protocol. Next, we show you how to deploy Tendermint testnets.