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.

265 lines
7.9 KiB

  1. # Getting Started
  2. ## First Tendermint App
  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
  8. ABCI](introduction.html#ABCI-Overview) that Tendermint Core handles all
  9. the p2p and consensus stuff, and just forwards transactions to the
  10. application when they need to be validated, or when they're ready to be
  11. committed to a block.
  12. In this guide, we show you some examples of how to run an application
  13. using Tendermint.
  14. ### Install
  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 `$PATH`; see
  18. [here](https://github.com/tendermint/tendermint/wiki/Setting-GOPATH) for
  19. more info.
  20. Then run
  21. go get -u github.com/tendermint/abci/cmd/abci-cli
  22. If there is an error, install and run the
  23. [dep](https://github.com/golang/dep) tool to pin the dependencies:
  24. cd $GOPATH/src/github.com/tendermint/abci
  25. make get_tools
  26. make get_vendor_deps
  27. make install
  28. Now you should have the `abci-cli` installed; you'll see a couple of
  29. commands (`counter` and `kvstore`) that are example applications written
  30. in Go. See below for an application written in JavaScript.
  31. Now, let's run some apps!
  32. ## KVStore - A First Example
  33. The kvstore app is a [Merkle
  34. tree](https://en.wikipedia.org/wiki/Merkle_tree) that just stores all
  35. transactions. If the transaction contains an `=`, e.g. `key=value`, then
  36. the `value` is stored under the `key` in the Merkle tree. Otherwise, the
  37. full transaction bytes are stored as the key and the value.
  38. Let's start a kvstore application.
  39. abci-cli kvstore
  40. In another terminal, we can start Tendermint. If you have never run
  41. Tendermint before, use:
  42. tendermint init
  43. tendermint node
  44. If you have used Tendermint, you may want to reset the data for a new
  45. blockchain by running `tendermint unsafe_reset_all`. Then you can run
  46. `tendermint node` to start Tendermint, and connect to the app. For more
  47. details, see [the guide on using Tendermint](./using-tendermint.html).
  48. You should see Tendermint making blocks! We can get the status of our
  49. Tendermint node as follows:
  50. curl -s localhost:26657/status
  51. The `-s` just silences `curl`. For nicer output, pipe the result into a
  52. tool like [jq](https://stedolan.github.io/jq/) or `json_pp`.
  53. Now let's send some transactions to the kvstore.
  54. curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"'
  55. Note the single quote (`'`) around the url, which ensures that the
  56. double quotes (`"`) are not escaped by bash. This command sent a
  57. transaction with bytes `abcd`, so `abcd` will be stored as both the key
  58. and the value in the Merkle tree. The response should look something
  59. like:
  60. {
  61. "jsonrpc": "2.0",
  62. "id": "",
  63. "result": {
  64. "check_tx": {
  65. "fee": {}
  66. },
  67. "deliver_tx": {
  68. "tags": [
  69. {
  70. "key": "YXBwLmNyZWF0b3I=",
  71. "value": "amFl"
  72. },
  73. {
  74. "key": "YXBwLmtleQ==",
  75. "value": "YWJjZA=="
  76. }
  77. ],
  78. "fee": {}
  79. },
  80. "hash": "9DF66553F98DE3C26E3C3317A3E4CED54F714E39",
  81. "height": 14
  82. }
  83. }
  84. We can confirm that our transaction worked and the value got stored by
  85. querying the app:
  86. curl -s 'localhost:26657/abci_query?data="abcd"'
  87. The result should look like:
  88. {
  89. "jsonrpc": "2.0",
  90. "id": "",
  91. "result": {
  92. "response": {
  93. "log": "exists",
  94. "index": "-1",
  95. "key": "YWJjZA==",
  96. "value": "YWJjZA=="
  97. }
  98. }
  99. }
  100. Note the `value` in the result (`YWJjZA==`); this is the base64-encoding
  101. of the ASCII of `abcd`. You can verify this in a python 2 shell by
  102. running `"61626364".decode('base64')` or in python 3 shell by running
  103. `import codecs; codecs.decode("61626364", 'base64').decode('ascii')`.
  104. Stay tuned for a future release that [makes this output more
  105. human-readable](https://github.com/tendermint/abci/issues/32).
  106. Now let's try setting a different key and value:
  107. curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"'
  108. Now if we query for `name`, we should get `satoshi`, or `c2F0b3NoaQ==`
  109. in base64:
  110. curl -s 'localhost:26657/abci_query?data="name"'
  111. Try some other transactions and queries to make sure everything is
  112. working!
  113. ## Counter - Another Example
  114. Now that we've got the hang of it, let's try another application, the
  115. `counter` app.
  116. The counter app doesn't use a Merkle tree, it just counts how many times
  117. we've sent a transaction, or committed the state.
  118. This application has two modes: `serial=off` and `serial=on`.
  119. When `serial=on`, transactions must be a big-endian encoded incrementing
  120. integer, starting at 0.
  121. If `serial=off`, there are no restrictions on transactions.
  122. In a live blockchain, transactions collect in memory before they are
  123. committed into blocks. To avoid wasting resources on invalid
  124. transactions, ABCI provides the `CheckTx` message, which application
  125. developers can use to accept or reject transactions, before they are
  126. stored in memory or gossipped to other peers.
  127. In this instance of the counter app, with `serial=on`, `CheckTx` only
  128. allows transactions whose integer is greater than the last committed
  129. one.
  130. Let's kill the previous instance of `tendermint` and the `kvstore`
  131. application, and start the counter app. We can enable `serial=on` with a
  132. flag:
  133. abci-cli counter --serial
  134. In another window, reset then start Tendermint:
  135. tendermint unsafe_reset_all
  136. tendermint node
  137. Once again, you can see the blocks streaming by. Let's send some
  138. transactions. Since we have set `serial=on`, the first transaction must
  139. be the number `0`:
  140. curl localhost:26657/broadcast_tx_commit?tx=0x00
  141. Note the empty (hence successful) response. The next transaction must be
  142. the number `1`. If instead, we try to send a `5`, we get an error:
  143. > curl localhost:26657/broadcast_tx_commit?tx=0x05
  144. {
  145. "jsonrpc": "2.0",
  146. "id": "",
  147. "result": {
  148. "check_tx": {
  149. "fee": {}
  150. },
  151. "deliver_tx": {
  152. "code": 2,
  153. "log": "Invalid nonce. Expected 1, got 5",
  154. "fee": {}
  155. },
  156. "hash": "33B93DFF98749B0D6996A70F64071347060DC19C",
  157. "height": 34
  158. }
  159. }
  160. But if we send a `1`, it works again:
  161. > curl localhost:26657/broadcast_tx_commit?tx=0x01
  162. {
  163. "jsonrpc": "2.0",
  164. "id": "",
  165. "result": {
  166. "check_tx": {
  167. "fee": {}
  168. },
  169. "deliver_tx": {
  170. "fee": {}
  171. },
  172. "hash": "F17854A977F6FA7EEA1BD758E296710B86F72F3D",
  173. "height": 60
  174. }
  175. }
  176. For more details on the `broadcast_tx` API, see [the guide on using
  177. Tendermint](./using-tendermint.html).
  178. ## CounterJS - Example in Another Language
  179. We also want to run applications in another language - in this case,
  180. we'll run a Javascript version of the `counter`. To run it, you'll need
  181. to [install node](https://nodejs.org/en/download/).
  182. You'll also need to fetch the relevant repository, from
  183. [here](https://github.com/tendermint/js-abci) then install it. As go
  184. devs, we keep all our code under the `$GOPATH`, so run:
  185. go get github.com/tendermint/js-abci &> /dev/null
  186. cd $GOPATH/src/github.com/tendermint/js-abci/example
  187. npm install
  188. cd ..
  189. Kill the previous `counter` and `tendermint` processes. Now run the app:
  190. node example/app.js
  191. In another window, reset and start `tendermint`:
  192. tendermint unsafe_reset_all
  193. tendermint node
  194. Once again, you should see blocks streaming by - but now, our
  195. application is written in javascript! Try sending some transactions, and
  196. like before - the results should be the same:
  197. curl localhost:26657/broadcast_tx_commit?tx=0x00 # ok
  198. curl localhost:26657/broadcast_tx_commit?tx=0x05 # invalid nonce
  199. curl localhost:26657/broadcast_tx_commit?tx=0x01 # ok
  200. Neat, eh?