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.

296 lines
7.7 KiB

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