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.

356 lines
11 KiB

7 years ago
  1. Using Tendermint
  2. ================
  3. This is a guide to using the ``tendermint`` program from the command
  4. line. It assumes only that you have the ``tendermint`` binary installed
  5. and have some rudimentary idea of what Tendermint and ABCI are.
  6. You can see the help menu with ``tendermint --help``, and the version
  7. number with ``tendermint version``.
  8. Directory Root
  9. --------------
  10. The default directory for blockchain data is ``~/.tendermint``. Override
  11. this by setting the ``TMROOT`` environment variable.
  12. Initialize
  13. ----------
  14. Initialize the root directory by running:
  15. ::
  16. tendermint init
  17. This will create a new private key (``priv_validator.json``), and a
  18. genesis file (``genesis.json``) containing the associated public key.
  19. This is all that's necessary to run a local testnet with one validator.
  20. For more elaborate initialization, see our `testnet deployment
  21. tool <https://github.com/tendermint/tools/tree/master/mintnet-kubernetes>`__.
  22. Run
  23. ---
  24. To run a tendermint node, use
  25. ::
  26. tendermint node
  27. By default, Tendermint will try to connect to a abci appliction on
  28. `127.0.0.1:46658 <127.0.0.1:46658>`__. If you have the ``dummy`` ABCI
  29. app installed, run it in another window. If you don't, kill tendermint
  30. and run an in-process version with
  31. ::
  32. tendermint node --proxy_app=dummy
  33. After a few seconds you should see blocks start streaming in. Note that
  34. blocks are produced regularly, even if there are no transactions. This
  35. changes `with this pull
  36. request <https://github.com/tendermint/tendermint/pull/584>`__.
  37. Tendermint supports in-process versions of the dummy, counter, and nil
  38. apps that ship as examples in the `ABCI
  39. repository <https://github.com/tendermint/abci>`__. It's easy to compile
  40. your own app in-process with tendermint if it's written in Go. If your
  41. app is not written in Go, simply run it in another process, and use the
  42. ``--proxy_app`` flag to specify the address of the socket it is
  43. listening on, for instance
  44. ::
  45. tendermint node --proxy_app=/var/run/abci.sock
  46. Transactions
  47. ------------
  48. To send a transaction, use ``curl`` to make requests to the Tendermint
  49. RPC server:
  50. ::
  51. curl http://localhost:46657/broadcast_tx_commit?tx=\"abcd\"
  52. For handling responses, we recommend you `install the jsonpp
  53. tool <http://jmhodges.github.io/jsonpp/>`__ to pretty print the JSON.
  54. We can see the chain's status at the ``/status`` end-point:
  55. ::
  56. curl http://localhost:46657/status | jsonpp
  57. and the ``latest_app_hash`` in particular:
  58. ::
  59. curl http://localhost:46657/status | jsonpp | grep app_hash
  60. Visit http://localhost:46657 in your browser to see the list of other
  61. endpoints. Some take no arguments (like ``/status``), while others
  62. specify the argument name and use ``_`` as a placeholder.
  63. Reset
  64. -----
  65. **WARNING: UNSAFE** Only do this in development and only if you can
  66. afford to lose all blockchain data!
  67. To reset a blockchain, stop the node, remove the ``~/.tendermint/data``
  68. directory and run
  69. ::
  70. tendermint unsafe_reset_priv_validator
  71. This final step is necessary to reset the ``priv_validator.json``, which
  72. otherwise prevents you from making conflicting votes in the consensus
  73. (something that could get you in trouble if you do it on a real
  74. blockchain). If you don't reset the ``priv_validator.json``, your fresh
  75. new blockchain will not make any blocks.
  76. Configuration
  77. -------------
  78. Tendermint uses a ``config.toml`` for configuration. For details, see
  79. `the documentation <./specification/configuration.html>`__.
  80. Notable options include the socket address of the application
  81. (``proxy_app``), the listenting address of the tendermint peer
  82. (``p2p.laddr``), and the listening address of the rpc server
  83. (``rpc.laddr``).
  84. Some fields from the config file can be overwritten with flags.
  85. Broadcast API
  86. -------------
  87. Earlier, we used the ``broadcast_tx_commit`` endpoint to send a
  88. transaction. When a transaction is sent to a tendermint node, it will
  89. run via ``CheckTx`` against the application. If it passes ``CheckTx``,
  90. it will be included in the mempool, broadcast to other peers, and
  91. eventually included in a block.
  92. Since there are multiple phases to processing a transaction, we offer
  93. multiple endpoints to broadcast a transaction:
  94. ::
  95. /broadcast_tx_async
  96. /broadcast_tx_sync
  97. /broadcast_tx_commit
  98. These correspond to no-processing, processing through the mempool, and
  99. processing through a block, respectively. That is,
  100. ``broadcast_tx_async``, will return right away without waiting to hear
  101. if the transaction is even valid, while ``broadcast_tx_sync`` will
  102. return with the result of running the transaction through ``CheckTx``.
  103. Using ``broadcast_tx_commit`` will wait until the transaction is
  104. committed in a block or until some timeout is reached, but will return
  105. right away if the transaction does not pass ``CheckTx``. The return
  106. value for ``broadcast_tx_commit`` includes two fields, ``check_tx`` and
  107. ``deliver_tx``, pertaining to the result of running the transaction
  108. through those ABCI messages.
  109. The benefit of using ``broadcast_tx_commit`` is that the request returns
  110. after the transaction is committed (ie. included in a block), but that
  111. can take on the order of a second. For a quick result, use
  112. ``broadcast_tx_sync``, but the transaction will not be committed until
  113. later, and by that point its effect on the state may change.
  114. Tendermint Networks
  115. -------------------
  116. When ``tendermint init`` is run, both a ``genesis.json`` and
  117. ``priv_validator.json`` are created in ``~/.tendermint``. The
  118. ``genesis.json`` might look like:
  119. ::
  120. {
  121. "app_hash": "",
  122. "chain_id": "test-chain-HZw6TB",
  123. "genesis_time": "0001-01-01T00:00:00.000Z",
  124. "validators": [
  125. {
  126. "power": 10,
  127. "name": "",
  128. "pub_key": [
  129. 1,
  130. "5770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  131. ]
  132. }
  133. ]
  134. }
  135. And the ``priv_validator.json``:
  136. ::
  137. {
  138. "address": "4F4D895F882A18E1D1FC608D102601DA8D3570E5",
  139. "last_height": 0,
  140. "last_round": 0,
  141. "last_signature": null,
  142. "last_signbytes": "",
  143. "last_step": 0,
  144. "priv_key": [
  145. 1,
  146. "F9FA3CD435BDAE54D0BCA8F1BC289D718C23D855C6DB21E8543F5E4F457E62805770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  147. ],
  148. "pub_key": [
  149. 1,
  150. "5770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  151. ]
  152. }
  153. The ``priv_validator.json`` actually contains a private key, and should
  154. thus be kept absolutely secret; for now we work with the plain text.
  155. Note the ``last_`` fields, which are used to prevent us from signing
  156. conflicting messages.
  157. Note also that the ``pub_key`` (the public key) in the
  158. ``priv_validator.json`` is also present in the ``genesis.json``.
  159. The genesis file contains the list of public keys which may participate
  160. in the consensus, and their corresponding voting power. Greater than 2/3
  161. of the voting power must be active (ie. the corresponding private keys
  162. must be producing signatures) for the consensus to make progress. In our
  163. case, the genesis file contains the public key of our
  164. ``priv_validator.json``, so a tendermint node started with the default
  165. root directory will be able to make new blocks, as we've already seen.
  166. If we want to add more nodes to the network, we have two choices: we can
  167. add a new validator node, who will also participate in the consensus by
  168. proposing blocks and voting on them, or we can add a new non-validator
  169. node, who will not participate directly, but will verify and keep up
  170. with the consensus protocol.
  171. Peers
  172. ~~~~~
  173. To connect to peers on start-up, specify them in the ``config.toml`` or
  174. on the command line.
  175. For instance,
  176. ::
  177. tendermint node --p2p.seeds "1.2.3.4:46656,5.6.7.8:46656"
  178. Alternatively, you can use the ``/dial_seeds`` endpoint of the RPC to
  179. specify peers for a running node to connect to:
  180. ::
  181. curl --data-urlencode "seeds=[\"1.2.3.4:46656\",\"5.6.7.8:46656\"]" localhost:46657/dial_seeds
  182. Additionally, the peer-exchange protocol can be enabled using the
  183. ``--pex`` flag, though this feature is `still under
  184. development <https://github.com/tendermint/tendermint/issues/598>`__ If
  185. ``--pex`` is enabled, peers will gossip about known peers and form a
  186. more resilient network.
  187. Adding a Non-Validator
  188. ~~~~~~~~~~~~~~~~~~~~~~
  189. Adding a non-validator is simple. Just copy the original
  190. ``genesis.json`` to ``~/.tendermint`` on the new machine and start the
  191. node, specifying seeds as necessary. If no seeds are specified, the node
  192. won't make any blocks, because it's not a validator, and it won't hear
  193. about any blocks, because it's not connected to the other peer.
  194. Adding a Validator
  195. ~~~~~~~~~~~~~~~~~~
  196. The easiest way to add new validators is to do it in the
  197. ``genesis.json``, before starting the network. For instance, we could
  198. make a new ``priv_validator.json``, and copy it's ``pub_key`` into the
  199. above genesis.
  200. We can generate a new ``priv_validator.json`` with the command:
  201. ::
  202. tendermint gen_validator
  203. Now we can update our genesis file. For instance, if the new
  204. ``priv_validator.json`` looks like:
  205. ::
  206. {
  207. "address": "AC379688105901436A34A65F185C115B8BB277A1",
  208. "last_height": 0,
  209. "last_round": 0,
  210. "last_signature": null,
  211. "last_signbytes": "",
  212. "last_step": 0,
  213. "priv_key": [
  214. 1,
  215. "0D2ED337D748ADF79BE28559B9E59EBE1ABBA0BAFE6D65FCB9797985329B950C8F2B5AACAACC9FCE41881349743B0CFDE190DF0177744568D4E82A18F0B7DF94"
  216. ],
  217. "pub_key": [
  218. 1,
  219. "8F2B5AACAACC9FCE41881349743B0CFDE190DF0177744568D4E82A18F0B7DF94"
  220. ]
  221. }
  222. then the new ``genesis.json`` will be:
  223. ::
  224. {
  225. "app_hash": "",
  226. "chain_id": "test-chain-HZw6TB",
  227. "genesis_time": "0001-01-01T00:00:00.000Z",
  228. "validators": [
  229. {
  230. "power": 10,
  231. "name": "",
  232. "pub_key": [
  233. 1,
  234. "5770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  235. ]
  236. },
  237. {
  238. "power": 10,
  239. "name": "",
  240. "pub_key": [
  241. 1,
  242. "8F2B5AACAACC9FCE41881349743B0CFDE190DF0177744568D4E82A18F0B7DF94"
  243. ]
  244. }
  245. ]
  246. }
  247. Update the ``genesis.json`` in ``~/.tendermint``. Copy the genesis file
  248. and the new ``priv_validator.json`` to the ``~/.tendermint`` on a new
  249. machine.
  250. Now run ``tendermint node`` on both machines, and use either
  251. ``--p2p.seeds`` or the ``/dial_seeds`` to get them to peer up. They
  252. should start making blocks, and will only continue to do so as long as
  253. both of them are online.
  254. To make a Tendermint network that can tolerate one of the validators
  255. failing, you need at least four validator nodes (> 2/3).
  256. Updating validators in a live network is supported but must be
  257. explicitly programmed by the application developer. See the `application
  258. developers guide <./app-development.html>`__ for more
  259. details.
  260. Local Network
  261. ~~~~~~~~~~~~~
  262. To run a network locally, say on a single machine, you must change the
  263. ``_laddr`` fields in the ``config.toml`` (or using the flags) so that
  264. the listening addresses of the various sockets don't conflict.
  265. Additionally, you must set ``addrbook_strict=false`` in the
  266. ``config.toml``, otherwise Tendermint's p2p library will deny making
  267. connections to peers with the same IP address.