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.

297 lines
7.7 KiB

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