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.

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