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.

413 lines
14 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 ``TMHOME`` 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. in ``$TMHOME/config``.
  20. This is all that's necessary to run a local testnet with one validator.
  21. For more elaborate initialization, see our `testnet deployment
  22. tool <https://github.com/tendermint/tools/tree/master/mintnet-kubernetes>`__.
  23. Run
  24. ---
  25. To run a tendermint node, use
  26. ::
  27. tendermint node
  28. By default, Tendermint will try to connect to an ABCI application on
  29. `127.0.0.1:46658 <127.0.0.1:46658>`__. If you have the ``dummy`` ABCI
  30. app installed, run it in another window. If you don't, kill tendermint
  31. and run an in-process version with
  32. ::
  33. tendermint node --proxy_app=dummy
  34. After a few seconds you should see blocks start streaming in. Note that
  35. blocks are produced regularly, even if there are no transactions. See *No Empty Blocks*, below, to modify this setting.
  36. Tendermint supports in-process versions of the dummy, counter, and nil
  37. apps that ship as examples in the `ABCI
  38. repository <https://github.com/tendermint/abci>`__. It's easy to compile
  39. your own app in-process with tendermint if it's written in Go. If your
  40. app is not written in Go, simply run it in another process, and use the
  41. ``--proxy_app`` flag to specify the address of the socket it is
  42. listening on, for instance:
  43. ::
  44. tendermint node --proxy_app=/var/run/abci.sock
  45. Transactions
  46. ------------
  47. To send a transaction, use ``curl`` to make requests to the Tendermint
  48. RPC server:
  49. ::
  50. curl http://localhost:46657/broadcast_tx_commit?tx=\"abcd\"
  51. For handling responses, we recommend you `install the jsonpp
  52. tool <http://jmhodges.github.io/jsonpp/>`__ to pretty print the JSON.
  53. We can see the chain's status at the ``/status`` end-point:
  54. ::
  55. curl http://localhost:46657/status | jsonpp
  56. and the ``latest_app_hash`` in particular:
  57. ::
  58. curl http://localhost:46657/status | jsonpp | grep app_hash
  59. Visit http://localhost:46657 in your browser to see the list of other
  60. endpoints. Some take no arguments (like ``/status``), while others
  61. specify the argument name and use ``_`` as a placeholder.
  62. Reset
  63. -----
  64. **WARNING: UNSAFE** Only do this in development and only if you can
  65. afford to lose all blockchain data!
  66. To reset a blockchain, stop the node, remove the ``~/.tendermint/data``
  67. directory and run
  68. ::
  69. tendermint unsafe_reset_priv_validator
  70. This final step is necessary to reset the ``priv_validator.json``, which
  71. otherwise prevents you from making conflicting votes in the consensus
  72. (something that could get you in trouble if you do it on a real
  73. blockchain). If you don't reset the ``priv_validator.json``, your fresh
  74. new blockchain will not make any blocks.
  75. Configuration
  76. -------------
  77. Tendermint uses a ``config.toml`` for configuration. For details, see
  78. `the config specification <./specification/configuration.html>`__.
  79. Notable options include the socket address of the application
  80. (``proxy_app``), the listenting address of the tendermint peer
  81. (``p2p.laddr``), and the listening address of the rpc server
  82. (``rpc.laddr``).
  83. Some fields from the config file can be overwritten with flags.
  84. No Empty Blocks
  85. ---------------
  86. This much requested feature was implemented in version 0.10.3. While the
  87. default behaviour of ``tendermint`` is still to create blocks approximately
  88. once per second, it is possible to disable empty blocks or set a block creation
  89. interval. In the former case, blocks will be created when there are new
  90. transactions or when the AppHash changes.
  91. To configure tendermint to not produce empty blocks unless there are
  92. transactions or the app hash changes, run tendermint with this additional flag:
  93. ::
  94. tendermint node --consensus.create_empty_blocks=false
  95. or set the configuration via the ``config.toml`` file:
  96. ::
  97. [consensus]
  98. create_empty_blocks = false
  99. Remember: because the default is to *create empty blocks*, avoiding empty blocks requires the config option to be set to ``false``.
  100. The block interval setting allows for a delay (in seconds) between the creation of each new empty block. It is set via the ``config.toml``:
  101. ::
  102. [consensus]
  103. create_empty_blocks_interval = 5
  104. With this setting, empty blocks will be produced every 5s if no block has been produced otherwise,
  105. regardless of the value of ``create_empty_blocks``.
  106. Broadcast API
  107. -------------
  108. Earlier, we used the ``broadcast_tx_commit`` endpoint to send a
  109. transaction. When a transaction is sent to a tendermint node, it will
  110. run via ``CheckTx`` against the application. If it passes ``CheckTx``,
  111. it will be included in the mempool, broadcast to other peers, and
  112. eventually included in a block.
  113. Since there are multiple phases to processing a transaction, we offer
  114. multiple endpoints to broadcast a transaction:
  115. ::
  116. /broadcast_tx_async
  117. /broadcast_tx_sync
  118. /broadcast_tx_commit
  119. These correspond to no-processing, processing through the mempool, and
  120. processing through a block, respectively. That is,
  121. ``broadcast_tx_async``, will return right away without waiting to hear
  122. if the transaction is even valid, while ``broadcast_tx_sync`` will
  123. return with the result of running the transaction through ``CheckTx``.
  124. Using ``broadcast_tx_commit`` will wait until the transaction is
  125. committed in a block or until some timeout is reached, but will return
  126. right away if the transaction does not pass ``CheckTx``. The return
  127. value for ``broadcast_tx_commit`` includes two fields, ``check_tx`` and
  128. ``deliver_tx``, pertaining to the result of running the transaction
  129. through those ABCI messages.
  130. The benefit of using ``broadcast_tx_commit`` is that the request returns
  131. after the transaction is committed (ie. included in a block), but that
  132. can take on the order of a second. For a quick result, use
  133. ``broadcast_tx_sync``, but the transaction will not be committed until
  134. later, and by that point its effect on the state may change.
  135. Tendermint Networks
  136. -------------------
  137. When ``tendermint init`` is run, both a ``genesis.json`` and
  138. ``priv_validator.json`` are created in ``~/.tendermint/config``. The
  139. ``genesis.json`` might look like:
  140. ::
  141. {
  142. "app_hash": "",
  143. "chain_id": "test-chain-HZw6TB",
  144. "genesis_time": "0001-01-01T00:00:00.000Z",
  145. "validators": [
  146. {
  147. "power": 10,
  148. "name": "",
  149. "pub_key": [
  150. 1,
  151. "5770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  152. ]
  153. }
  154. ]
  155. }
  156. And the ``priv_validator.json``:
  157. ::
  158. {
  159. "address": "4F4D895F882A18E1D1FC608D102601DA8D3570E5",
  160. "last_height": 0,
  161. "last_round": 0,
  162. "last_signature": null,
  163. "last_signbytes": "",
  164. "last_step": 0,
  165. "priv_key": [
  166. 1,
  167. "F9FA3CD435BDAE54D0BCA8F1BC289D718C23D855C6DB21E8543F5E4F457E62805770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  168. ],
  169. "pub_key": [
  170. 1,
  171. "5770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  172. ]
  173. }
  174. The ``priv_validator.json`` actually contains a private key, and should
  175. thus be kept absolutely secret; for now we work with the plain text.
  176. Note the ``last_`` fields, which are used to prevent us from signing
  177. conflicting messages.
  178. Note also that the ``pub_key`` (the public key) in the
  179. ``priv_validator.json`` is also present in the ``genesis.json``.
  180. The genesis file contains the list of public keys which may participate in the
  181. consensus, and their corresponding voting power. Greater than 2/3 of the voting
  182. power must be active (ie. the corresponding private keys must be producing
  183. signatures) for the consensus to make progress. In our case, the genesis file
  184. contains the public key of our ``priv_validator.json``, so a tendermint node
  185. started with the default root directory will be able to make progress. Voting
  186. power uses an `int64` but must be positive, thus the range is: 0 through
  187. 9223372036854775807. Because of how the current proposer selection algorithm works,
  188. we do not recommend having voting powers greater than 10^12 (ie. 1 trillion)
  189. (see `Proposals section of Byzantine Consensus Algorithm
  190. <./specification/byzantine-consensus-algorithm.html#proposals>`__ for details).
  191. If we want to add more nodes to the network, we have two choices: we can
  192. add a new validator node, who will also participate in the consensus by
  193. proposing blocks and voting on them, or we can add a new non-validator
  194. node, who will not participate directly, but will verify and keep up
  195. with the consensus protocol.
  196. Peers
  197. ~~~~~
  198. To connect to peers on start-up, specify them in the ``$TMHOME/config/config.toml`` or
  199. on the command line. Use `seeds` to specify seed nodes from which you can get many other
  200. peer addresses, and ``persistent_peers`` to specify peers that your node will maintain
  201. persistent connections with.
  202. For instance,
  203. ::
  204. tendermint node --p2p.seeds "1.2.3.4:46656,5.6.7.8:46656"
  205. Alternatively, you can use the ``/dial_seeds`` endpoint of the RPC to
  206. specify seeds for a running node to connect to:
  207. ::
  208. curl 'localhost:46657/dial_seeds?seeds=\["1.2.3.4:46656","5.6.7.8:46656"\]'
  209. Note, if the peer-exchange protocol (PEX) is enabled (default), you should not
  210. normally need seeds after the first start. Peers will be gossipping about known
  211. peers and forming a network, storing peer addresses in the addrbook.
  212. If you want Tendermint to connect to specific set of addresses and maintain a
  213. persistent connection with each, you can use the ``--p2p.persistent_peers``
  214. flag or the corresponding setting in the ``config.toml`` or the
  215. ``/dial_peers`` RPC endpoint to do it without stopping Tendermint
  216. core instance.
  217. ::
  218. tendermint node --p2p.persistent_peers "10.11.12.13:46656,10.11.12.14:46656"
  219. curl 'localhost:46657/dial_peers?persistent=true&peers=\["1.2.3.4:46656","5.6.7.8:46656"\]'
  220. Adding a Non-Validator
  221. ~~~~~~~~~~~~~~~~~~~~~~
  222. Adding a non-validator is simple. Just copy the original
  223. ``genesis.json`` to ``~/.tendermint/config`` on the new machine and start the
  224. node, specifying seeds or persistent peers as necessary. If no seeds or persistent
  225. peers are specified, the node won't make any blocks, because it's not a validator,
  226. and it won't hear about any blocks, because it's not connected to the other peer.
  227. Adding a Validator
  228. ~~~~~~~~~~~~~~~~~~
  229. The easiest way to add new validators is to do it in the
  230. ``genesis.json``, before starting the network. For instance, we could
  231. make a new ``priv_validator.json``, and copy it's ``pub_key`` into the
  232. above genesis.
  233. We can generate a new ``priv_validator.json`` with the command:
  234. ::
  235. tendermint gen_validator
  236. Now we can update our genesis file. For instance, if the new
  237. ``priv_validator.json`` looks like:
  238. ::
  239. {
  240. "address": "AC379688105901436A34A65F185C115B8BB277A1",
  241. "last_height": 0,
  242. "last_round": 0,
  243. "last_signature": null,
  244. "last_signbytes": "",
  245. "last_step": 0,
  246. "priv_key": [
  247. 1,
  248. "0D2ED337D748ADF79BE28559B9E59EBE1ABBA0BAFE6D65FCB9797985329B950C8F2B5AACAACC9FCE41881349743B0CFDE190DF0177744568D4E82A18F0B7DF94"
  249. ],
  250. "pub_key": [
  251. 1,
  252. "8F2B5AACAACC9FCE41881349743B0CFDE190DF0177744568D4E82A18F0B7DF94"
  253. ]
  254. }
  255. then the new ``genesis.json`` will be:
  256. ::
  257. {
  258. "app_hash": "",
  259. "chain_id": "test-chain-HZw6TB",
  260. "genesis_time": "0001-01-01T00:00:00.000Z",
  261. "validators": [
  262. {
  263. "power": 10,
  264. "name": "",
  265. "pub_key": [
  266. 1,
  267. "5770B4DD55B3E08B7F5711C48B516347D8C33F47C30C226315D21AA64E0DFF2E"
  268. ]
  269. },
  270. {
  271. "power": 10,
  272. "name": "",
  273. "pub_key": [
  274. 1,
  275. "8F2B5AACAACC9FCE41881349743B0CFDE190DF0177744568D4E82A18F0B7DF94"
  276. ]
  277. }
  278. ]
  279. }
  280. Update the ``genesis.json`` in ``~/.tendermint/config``. Copy the genesis file
  281. and the new ``priv_validator.json`` to the ``~/.tendermint/config`` on a new
  282. machine.
  283. Now run ``tendermint node`` on both machines, and use either
  284. ``--p2p.persistent_peers`` or the ``/dial_peers`` to get them to peer up. They
  285. should start making blocks, and will only continue to do so as long as
  286. both of them are online.
  287. To make a Tendermint network that can tolerate one of the validators
  288. failing, you need at least four validator nodes (> 2/3).
  289. Updating validators in a live network is supported but must be
  290. explicitly programmed by the application developer. See the `application
  291. developers guide <./app-development.html>`__ for more
  292. details.
  293. Local Network
  294. ~~~~~~~~~~~~~
  295. To run a network locally, say on a single machine, you must change the
  296. ``_laddr`` fields in the ``config.toml`` (or using the flags) so that
  297. the listening addresses of the various sockets don't conflict.
  298. Additionally, you must set ``addrbook_strict=false`` in the
  299. ``config.toml``, otherwise Tendermint's p2p library will deny making
  300. connections to peers with the same IP address.
  301. Upgrading
  302. ~~~~~~~~~
  303. The tendermint development cycle includes a lot of breaking changes. Upgrading from
  304. an old version to a new version usually means throwing away the chain data. Try out
  305. the `tm-migrate <https://github.com/hxzqlh/tm-tools>`__ tool written by @hxqlh if
  306. you are keen to preserve the state of your chain when upgrading to newer versions.