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.

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