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