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.

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