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.

261 lines
7.8 KiB

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