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.

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