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.

256 lines
8.9 KiB

  1. # First Tendermint App
  2. As a general purpose blockchain engine, Tendermint is agnostic to the application you want to run.
  3. So, to run a complete blockchain that does something useful, you must start two programs:
  4. one is Tendermint Core, the other is your application, which can be written in any programming language.
  5. Recall from [the intro to ABCI](/intro/abci-overview) that Tendermint Core handles all the p2p and consensus stuff,
  6. and just forwards transactions to the application when they need to be validated, or when they're ready to be committed to a block.
  7. In this guide, we show you some examples of how to run an application using Tendermint.
  8. ## Install
  9. First, make sure you have [installed Tendermint](/download).
  10. The first apps we will work with are written in Go.
  11. To install them, you need to [install Go](https://golang.org/doc/install) and
  12. [put `$GOPATH/bin` in your `$PATH`](https://github.com/tendermint/tendermint/wiki/Setting-GOPATH).
  13. Then run
  14. ```
  15. go get -u github.com/tendermint/abci/cmd/...
  16. ```
  17. If there is an error, download the `glide` tool to pin the dependencies:
  18. ```
  19. go get github.com/Masterminds/glide
  20. cd $GOPATH/src/github.com/tendermint/abci
  21. glide install
  22. go install ./cmd/...
  23. ```
  24. Now you should have two apps installed:
  25. ```
  26. dummy --help
  27. counter --help
  28. ```
  29. Both of these applications are in Go.
  30. But we also want to run an application in another language -
  31. in this case, we'll run a Javascript version of the `counter`.
  32. To run it, you'll need to [install node](https://nodejs.org/en/download/).
  33. You'll also need to fetch the relevant repository, from https://github.com/tendermint/js-abci.
  34. Since I keep all my code under the `$GOPATH`, I just `go get github.com/tendermint/js-abci &> /dev/null`.
  35. Then `cd` into the `example` directory within that repository and run `npm install`.
  36. For instance, if you used `go get`,
  37. ```
  38. cd $GOPATH/src/github.com/tendermint/js-abci/example
  39. npm install
  40. ```
  41. Now, let's run some apps!
  42. ## A First Example - Dummy
  43. The dummy app is a [Merkle tree](https://en.wikipedia.org/wiki/Merkle_tree) that just stores all transactions.
  44. If the transaction contains an `=`, eg. `key=value`,
  45. then the `value` is stored under the `key` in the Merkle tree.
  46. Otherwise, the full transaction bytes are stored as the key and the value.
  47. Let's start a dummy application.
  48. ```
  49. dummy
  50. ```
  51. In another terminal, we can start Tendermint.
  52. If you have never run Tendermint before, use:
  53. ```
  54. tendermint init
  55. tendermint node
  56. ```
  57. If you have used Tendermint, you may want to reset the data for a new blockchain by running `tendermint unsafe_reset_all`.
  58. Then you can run `tendermint node` to start Tendermint, and connect to the app.
  59. For more details, see [the guide on using Tendermint](/docs/guides/using-tendermint).
  60. You should see Tendermint making blocks!
  61. We can get the status of our Tendermint node as follows:
  62. ```
  63. curl -s localhost:46657/status
  64. ```
  65. The `-s` just silences `curl`. For nicer output, pipe the result into a tool like [jq](https://stedolan.github.io/jq/)
  66. or [jsonpp](https://github.com/jmhodges/jsonpp).
  67. Now let's send some transactions to the dummy.
  68. ```
  69. curl -s 'localhost:46657/broadcast_tx_commit?tx="abcd"'
  70. ```
  71. Note the single quote (`'`) around the url, which ensures that the double quotes (`"`) are not escaped by bash.
  72. This command sent a transaction with bytes `abcd`, so `abcd` will be stored as both the key and the value in the Merkle tree.
  73. The response should look something like:
  74. ```
  75. {"jsonrpc":"2.0","id":"","result":[98,{"check_tx":{},"deliver_tx":{}}],"error":""}
  76. ```
  77. The `98` is a type-byte, and can be ignored (it's useful for serializing and deserializing arbitrary json).
  78. Otherwise, this result is empty - there's nothing to report on and everything is OK.
  79. We can confirm that our transaction worked and the value got stored by querying the app:
  80. ```
  81. curl -s 'localhost:46657/abci_query?data="abcd"&path=""&prove=false'
  82. ```
  83. The `path` and `prove` arguments can be ignored for now, and in a future release can be left out.
  84. The result should look like:
  85. ```
  86. {"jsonrpc":"2.0","id":"","result":[112,{"response":{"value":"61626364","log":"exists"}}],"error":""}
  87. ```
  88. Again, the `112` is the type-byte. Note the `value` in the result (`61626364`); this is the hex-encoding of the ASCII of `abcd`.
  89. You can verify this in a python shell by running `"61626364".decode('hex')`.
  90. Stay tuned for a future release that makes this output more human-readable ;).
  91. Now let's try setting a different key and value:
  92. ```
  93. curl -s 'localhost:46657/broadcast_tx_commit?tx="name=satoshi"'
  94. ```
  95. Now if we query for `name`, we should get `satoshi`, or `7361746F736869` in hex:
  96. ```
  97. curl -s 'localhost:46657/abci_query?data="name"&path=""&prove=false'
  98. ```
  99. Try some other transactions and queries to make sure everything is working!
  100. ## Another Example - Counter
  101. Now that we've got the hang of it, let's try another application, the "counter" app.
  102. The counter app doesn't use a Merkle tree, it just counts how many times we've sent a transaction,
  103. or committed the state.
  104. This application has two modes: `serial=off` and `serial=on`.
  105. When `serial=on`, transactions must be a big-endian encoded incrementing integer, starting at 0.
  106. If `serial=off`, there are no restrictions on transactions.
  107. In a live blockchain, transactions collect in memory before they are committed into blocks.
  108. To avoid wasting resources on invalid transactions,
  109. ABCI provides the `CheckTx` message,
  110. which application developers can use to accept or reject transactions,
  111. before they are stored in memory or gossipped to other peers.
  112. In this instance of the counter app, with `serial=on`, `CheckTx` only allows transactions whose integer is greater than the last committed one.
  113. Let's kill the previous instance of tendermint and the dummy application, and start the counter app.
  114. We can enable `serial=on` with a flag:
  115. ```
  116. counter --serial
  117. ```
  118. In another window, reset and start Tendermint:
  119. ```
  120. tendermint unsafe_reset_all
  121. tendermint node
  122. ```
  123. Once again, you can see the blocks streaming by. Let's send some transactions.
  124. Since we have set `serial=on`, the first transaction must be the number `0`:
  125. ```
  126. curl localhost:46657/broadcast_tx_commit?tx=0x00
  127. ```
  128. Note the empty, hence successful, response.
  129. The next transaction must be the number `1`. If instead, we try to send a `5`, we get an error:
  130. ```
  131. > curl localhost:46657/broadcast_tx_commit?tx=0x05
  132. {"jsonrpc":"2.0","id":"","result":[98,{"check_tx":{},"deliver_tx":{"code":3,"log":"Invalid nonce. Expected 1, got 5"}}],"error":""}
  133. ```
  134. But if we send a `1`, it works again:
  135. ```
  136. > curl localhost:46657/broadcast_tx_commit?tx=0x01
  137. {"jsonrpc":"2.0","id":"","result":[98,{"check_tx":{},"deliver_tx":{}}],"error":""}
  138. ```
  139. For more details on the `broadcast_tx` API,
  140. see [the guide on using Tendermint](/docs/guides/using-tendermint).
  141. ## Example in Another Language - CounterJS
  142. The ultimate flexibility in Tendermint comes from being able to easily write the application in any language.
  143. While we already used the implementation written in Go,
  144. let's now try the Counter application written in Javascript!
  145. Kill the previous `counter` and `tendermint` processes.
  146. Change directory to the location of the `github.com/tendermint/js-abci`.
  147. If you fetched the repository with `go get`, it would be
  148. ```
  149. cd $GOPATH/src/github.com/tendermint/js-abci
  150. ```
  151. Now run the app:
  152. ```
  153. node example/app.js
  154. ```
  155. In another window, reset and start `tendermint`:
  156. ```
  157. tendermint unsafe_reset_all
  158. tendermint node
  159. ```
  160. Once again, you should see blocks streaming by - but now, our application is written in javascript!
  161. Try sending some transasctions, like before - the results should be the same:
  162. ```
  163. curl localhost:46657/broadcast_tx_commit?tx=0x00 # ok
  164. curl localhost:46657/broadcast_tx_commit?tx=0x05 # invalid nonce
  165. curl localhost:46657/broadcast_tx_commit?tx=0x01 # ok
  166. ```
  167. Neat, eh?
  168. ## A More Interesting Example - Basecoin
  169. Before concluding, we'd like to introduce you to our star application, [Basecoin](https://github.com/tendermint/basecoin).
  170. Unlike the `dummy` and `counter`, which are strictly for example purposes,
  171. `basecoin` is designed to be actually useful - it's a general purpose framework for building cryptocurrencies.
  172. The default `basecoin` application is a multi-asset cryptocurrency that supports inter-blockchain communication.
  173. For more details on how basecoin works and how to use it, see our [basecoin guide](https://github.com/tendermint/basecoin/blob/develop/docs/guide/basecoin-basics.md)
  174. ## Next Step
  175. In this tutorial you learned how to run applications using Tendermint on a single node.
  176. You saw how applications could be written in different languages,
  177. and how to send transactions and query for the latest state.
  178. But the true power of Tendermint comes from its ability to securely and efficiently run an application
  179. across a distributed network of nodes, while keeping them all in sync using its state-of-the-art consensus protocol.
  180. This is the subject of the next tutorial, where we show you [how to deploy Tendermint networks](/docs/getting-started/deploy-testnet).