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.

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