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.

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