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.

420 lines
16 KiB

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