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.

289 lines
7.5 KiB

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