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.

292 lines
7.4 KiB

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