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.

202 lines
5.3 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 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
7 years ago
6 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
7 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
  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 install_abci
  29. ```
  30. Now you should have the `abci-cli` installed; you'll notice the `kvstore`
  31. command, an example application written
  32. in Go. See below for an application written in JavaScript.
  33. Now, let's run some apps!
  34. ## KVStore - A First Example
  35. The kvstore app is a [Merkle
  36. tree](https://en.wikipedia.org/wiki/Merkle_tree) that just stores all
  37. transactions. If the transaction contains an `=`, e.g. `key=value`, then
  38. the `value` is stored under the `key` in the Merkle tree. Otherwise, the
  39. full transaction bytes are stored as the key and the value.
  40. Let's start a kvstore application.
  41. ```sh
  42. abci-cli kvstore
  43. ```
  44. In another terminal, we can start Tendermint. You should already have the
  45. Tendermint binary installed. If not, follow the steps from
  46. [here](../introduction/install.md). If you have never run Tendermint
  47. before, use:
  48. ```sh
  49. tendermint init validator
  50. tendermint start
  51. ```
  52. If you have used Tendermint, you may want to reset the data for a new
  53. blockchain by running `tendermint unsafe-reset-all`. Then you can run
  54. `tendermint start` to start Tendermint, and connect to the app. For more
  55. details, see [the guide on using Tendermint](../tendermint-core/using-tendermint.md).
  56. You should see Tendermint making blocks! We can get the status of our
  57. Tendermint node as follows:
  58. ```sh
  59. curl -s localhost:26657/status
  60. ```
  61. The `-s` just silences `curl`. For nicer output, pipe the result into a
  62. tool like [jq](https://stedolan.github.io/jq/) or `json_pp`.
  63. Now let's send some transactions to the kvstore.
  64. ```sh
  65. curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"'
  66. ```
  67. Note the single quote (`'`) around the url, which ensures that the
  68. double quotes (`"`) are not escaped by bash. This command sent a
  69. transaction with bytes `abcd`, so `abcd` will be stored as both the key
  70. and the value in the Merkle tree. The response should look something
  71. like:
  72. ```json
  73. {
  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. We can confirm that our transaction worked and the value got stored by
  92. querying the app:
  93. ```sh
  94. curl -s 'localhost:26657/abci_query?data="abcd"'
  95. ```
  96. The result should look like:
  97. ```json
  98. {
  99. "response": {
  100. "log": "exists",
  101. "index": "-1",
  102. "key": "YWJjZA==",
  103. "value": "YWJjZA=="
  104. }
  105. }
  106. ```
  107. Note the `value` in the result (`YWJjZA==`); this is the base64-encoding
  108. of the ASCII of `abcd`. You can verify this in a python 2 shell by
  109. running `"YWJjZA==".decode('base64')` or in python 3 shell by running
  110. `import codecs; codecs.decode(b"YWJjZA==", 'base64').decode('ascii')`.
  111. Stay tuned for a future release that [makes this output more
  112. human-readable](https://github.com/tendermint/tendermint/issues/1794).
  113. Now let's try setting a different key and value:
  114. ```sh
  115. curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"'
  116. ```
  117. Now if we query for `name`, we should get `satoshi`, or `c2F0b3NoaQ==`
  118. in base64:
  119. ```sh
  120. curl -s 'localhost:26657/abci_query?data="name"'
  121. ```
  122. Try some other transactions and queries to make sure everything is
  123. working!
  124. ## CounterJS - Example in Another Language
  125. We also want to run applications in another language - in this case,
  126. we'll run a Javascript version of the `counter`. To run it, you'll need
  127. to [install node](https://nodejs.org/en/download/).
  128. You'll also need to fetch the relevant repository, from
  129. [here](https://github.com/tendermint/js-abci), then install it:
  130. ```sh
  131. git clone https://github.com/tendermint/js-abci.git
  132. cd js-abci
  133. npm install abci
  134. ```
  135. Kill the previous `counter` and `tendermint` processes. Now run the app:
  136. ```sh
  137. node example/counter.js
  138. ```
  139. In another window, reset and start `tendermint`:
  140. ```sh
  141. tendermint unsafe-reset-all
  142. tendermint start
  143. ```
  144. Once again, you should see blocks streaming by - but now, our
  145. application is written in Javascript! Try sending some transactions, and
  146. like before - the results should be the same:
  147. ```sh
  148. # ok
  149. curl localhost:26657/broadcast_tx_commit?tx=0x00
  150. # invalid nonce
  151. curl localhost:26657/broadcast_tx_commit?tx=0x05
  152. # ok
  153. curl localhost:26657/broadcast_tx_commit?tx=0x01
  154. ```
  155. Neat, eh?