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.

577 lines
19 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
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. ---
  2. order: 2
  3. ---
  4. # Using Tendermint
  5. This is a guide to using the `tendermint` program from the command line.
  6. It assumes only that you have the `tendermint` binary installed and have
  7. some rudimentary idea of what Tendermint and ABCI are.
  8. You can see the help menu with `tendermint --help`, and the version
  9. number with `tendermint version`.
  10. ## Directory Root
  11. The default directory for blockchain data is `~/.tendermint`. Override
  12. this by setting the `TMHOME` environment variable.
  13. ## Initialize
  14. Initialize the root directory by running:
  15. ```sh
  16. tendermint init
  17. ```
  18. This will create a new private key (`priv_validator_key.json`), and a
  19. genesis file (`genesis.json`) containing the associated public key, in
  20. `$TMHOME/config`. This is all that's necessary to run a local testnet
  21. with one validator.
  22. For more elaborate initialization, see the testnet command:
  23. ```sh
  24. tendermint testnet --help
  25. ```
  26. ### Genesis
  27. The `genesis.json` file in `$TMHOME/config/` defines the initial
  28. TendermintCore state upon genesis of the blockchain ([see
  29. definition](https://github.com/tendermint/tendermint/blob/master/types/genesis.go)).
  30. #### Fields
  31. - `genesis_time`: Official time of blockchain start.
  32. - `chain_id`: ID of the blockchain. **This must be unique for
  33. every blockchain.** If your testnet blockchains do not have unique
  34. chain IDs, you will have a bad time. The ChainID must be less than 50 symbols.
  35. - `initial_height`: Height at which Tendermint should begin at.
  36. - `consensus_params` [spec](https://github.com/tendermint/spec/blob/master/spec/core/state.md#consensusparams)
  37. - `block`
  38. - `max_bytes`: Max block size, in bytes.
  39. - `max_gas`: Max gas per block.
  40. - `time_iota_ms`: Minimum time increment between consecutive blocks (in
  41. milliseconds). If the block header timestamp is ahead of the system clock,
  42. decrease this value.
  43. - `evidence`
  44. - `max_age_num_blocks`: Max age of evidence, in blocks. The basic formula
  45. for calculating this is: MaxAgeDuration / {average block time}.
  46. - `max_age_duration`: Max age of evidence, in time. It should correspond
  47. with an app's "unbonding period" or other similar mechanism for handling
  48. [Nothing-At-Stake
  49. attacks](https://github.com/ethereum/wiki/wiki/Proof-of-Stake-FAQ#what-is-the-nothing-at-stake-problem-and-how-can-it-be-fixed).
  50. - `max_num`: This sets the maximum number of evidence that can be committed
  51. in a single block. and should fall comfortably under the max block
  52. bytes when we consider the size of each evidence.
  53. - `proof_trial_period`: Proof trial period dictates the time given for
  54. nodes accused of amnesia evidence, incorrectly voting twice in two
  55. different rounds to respond with their respective proofs.
  56. - `validator`
  57. - `pub_key_types`: Public key types validators can use.
  58. - `version`
  59. - `app_version`: ABCI application version.
  60. - `validators`: List of initial validators. Note this may be overridden entirely by the
  61. application, and may be left empty to make explicit that the
  62. application will initialize the validator set with ResponseInitChain.
  63. - `pub_key`: The first element specifies the `pub_key` type. 1
  64. == Ed25519. The second element are the pubkey bytes.
  65. - `power`: The validator's voting power.
  66. - `name`: Name of the validator (optional).
  67. - `app_hash`: The expected application hash (as returned by the
  68. `ResponseInfo` ABCI message) upon genesis. If the app's hash does
  69. not match, Tendermint will panic.
  70. - `app_state`: The application state (e.g. initial distribution
  71. of tokens).
  72. > :warning: **ChainID must be unique to every blockchain. Reusing old chainID can cause issues**
  73. #### Sample genesis.json
  74. ```json
  75. {
  76. "genesis_time": "2020-04-21T11:17:42.341227868Z",
  77. "chain_id": "test-chain-ROp9KF",
  78. "initial_height": "0",
  79. "consensus_params": {
  80. "block": {
  81. "max_bytes": "22020096",
  82. "max_gas": "-1",
  83. "time_iota_ms": "1000"
  84. },
  85. "evidence": {
  86. "max_age_num_blocks": "100000",
  87. "max_age_duration": "172800000000000",
  88. "max_num": 50,
  89. "proof_trial_period": "5000000"
  90. },
  91. "validator": {
  92. "pub_key_types": [
  93. "ed25519"
  94. ]
  95. }
  96. },
  97. "validators": [
  98. {
  99. "address": "B547AB87E79F75A4A3198C57A8C2FDAF8628CB47",
  100. "pub_key": {
  101. "type": "tendermint/PubKeyEd25519",
  102. "value": "P/V6GHuZrb8rs/k1oBorxc6vyXMlnzhJmv7LmjELDys="
  103. },
  104. "power": "10",
  105. "name": ""
  106. }
  107. ],
  108. "app_hash": ""
  109. }
  110. ```
  111. ## Run
  112. To run a Tendermint node, use:
  113. ```bash
  114. tendermint node
  115. ```
  116. By default, Tendermint will try to connect to an ABCI application on
  117. `127.0.0.1:26658`. If you have the `kvstore` ABCI app installed, run it in
  118. another window. If you don't, kill Tendermint and run an in-process version of
  119. the `kvstore` app:
  120. ```bash
  121. tendermint node --proxy_app=kvstore
  122. ```
  123. After a few seconds, you should see blocks start streaming in. Note that blocks
  124. are produced regularly, even if there are no transactions. See _No Empty
  125. Blocks_, below, to modify this setting.
  126. Tendermint supports in-process versions of the `counter`, `kvstore`, and `noop`
  127. apps that ship as examples with `abci-cli`. It's easy to compile your app
  128. in-process with Tendermint if it's written in Go. If your app is not written in
  129. Go, run it in another process, and use the `--proxy_app` flag to specify the
  130. address of the socket it is listening on, for instance:
  131. ```bash
  132. tendermint node --proxy_app=/var/run/abci.sock
  133. ```
  134. You can find out what flags are supported by running `tendermint node --help`.
  135. ## Transactions
  136. To send a transaction, use `curl` to make requests to the Tendermint RPC
  137. server, for example:
  138. ```sh
  139. curl http://localhost:26657/broadcast_tx_commit?tx=\"abcd\"
  140. ```
  141. We can see the chain's status at the `/status` end-point:
  142. ```sh
  143. curl http://localhost:26657/status | json_pp
  144. ```
  145. and the `latest_app_hash` in particular:
  146. ```sh
  147. curl http://localhost:26657/status | json_pp | grep latest_app_hash
  148. ```
  149. Visit `http://localhost:26657` in your browser to see the list of other
  150. endpoints. Some take no arguments (like `/status`), while others specify
  151. the argument name and use `_` as a placeholder.
  152. > TIP: Find the RPC Documentation [here](https://docs.tendermint.com/master/rpc/)
  153. ### Formatting
  154. The following nuances when sending/formatting transactions should be
  155. taken into account:
  156. With `GET`:
  157. To send a UTF8 string byte array, quote the value of the tx parameter:
  158. ```sh
  159. curl 'http://localhost:26657/broadcast_tx_commit?tx="hello"'
  160. ```
  161. which sends a 5 byte transaction: "h e l l o" \[68 65 6c 6c 6f\].
  162. Note the URL must be wrapped with single quotes, else bash will ignore
  163. the double quotes. To avoid the single quotes, escape the double quotes:
  164. ```sh
  165. curl http://localhost:26657/broadcast_tx_commit?tx=\"hello\"
  166. ```
  167. Using a special character:
  168. ```sh
  169. curl 'http://localhost:26657/broadcast_tx_commit?tx="€5"'
  170. ```
  171. sends a 4 byte transaction: "€5" (UTF8) \[e2 82 ac 35\].
  172. To send as raw hex, omit quotes AND prefix the hex string with `0x`:
  173. ```sh
  174. curl http://localhost:26657/broadcast_tx_commit?tx=0x01020304
  175. ```
  176. which sends a 4 byte transaction: \[01 02 03 04\].
  177. With `POST` (using `json`), the raw hex must be `base64` encoded:
  178. ```sh
  179. curl --data-binary '{"jsonrpc":"2.0","id":"anything","method":"broadcast_tx_commit","params": {"tx": "AQIDBA=="}}' -H 'content-type:text/plain;' http://localhost:26657
  180. ```
  181. which sends the same 4 byte transaction: \[01 02 03 04\].
  182. Note that raw hex cannot be used in `POST` transactions.
  183. ## Reset
  184. > :warning: **UNSAFE** Only do this in development and only if you can
  185. afford to lose all blockchain data!
  186. To reset a blockchain, stop the node and run:
  187. ```sh
  188. tendermint unsafe_reset_all
  189. ```
  190. This command will remove the data directory and reset private validator and
  191. address book files.
  192. ## Configuration
  193. Tendermint uses a `config.toml` for configuration. For details, see [the
  194. config specification](./configuration.md).
  195. Notable options include the socket address of the application
  196. (`proxy_app`), the listening address of the Tendermint peer
  197. (`p2p.laddr`), and the listening address of the RPC server
  198. (`rpc.laddr`).
  199. Some fields from the config file can be overwritten with flags.
  200. ## No Empty Blocks
  201. While the default behavior of `tendermint` is still to create blocks
  202. approximately once per second, it is possible to disable empty blocks or
  203. set a block creation interval. In the former case, blocks will be
  204. created when there are new transactions or when the AppHash changes.
  205. To configure Tendermint to not produce empty blocks unless there are
  206. transactions or the app hash changes, run Tendermint with this
  207. additional flag:
  208. ```sh
  209. tendermint node --consensus.create_empty_blocks=false
  210. ```
  211. or set the configuration via the `config.toml` file:
  212. ```toml
  213. [consensus]
  214. create_empty_blocks = false
  215. ```
  216. Remember: because the default is to _create empty blocks_, avoiding
  217. empty blocks requires the config option to be set to `false`.
  218. The block interval setting allows for a delay (in time.Duration format [ParseDuration](https://golang.org/pkg/time/#ParseDuration)) between the
  219. creation of each new empty block. It can be set with this additional flag:
  220. ```sh
  221. --consensus.create_empty_blocks_interval="5s"
  222. ```
  223. or set the configuration via the `config.toml` file:
  224. ```toml
  225. [consensus]
  226. create_empty_blocks_interval = "5s"
  227. ```
  228. With this setting, empty blocks will be produced every 5s if no block
  229. has been produced otherwise, regardless of the value of
  230. `create_empty_blocks`.
  231. ## Broadcast API
  232. Earlier, we used the `broadcast_tx_commit` endpoint to send a
  233. transaction. When a transaction is sent to a Tendermint node, it will
  234. run via `CheckTx` against the application. If it passes `CheckTx`, it
  235. will be included in the mempool, broadcasted to other peers, and
  236. eventually included in a block.
  237. Since there are multiple phases to processing a transaction, we offer
  238. multiple endpoints to broadcast a transaction:
  239. ```md
  240. /broadcast_tx_async
  241. /broadcast_tx_sync
  242. /broadcast_tx_commit
  243. ```
  244. These correspond to no-processing, processing through the mempool, and
  245. processing through a block, respectively. That is, `broadcast_tx_async`,
  246. will return right away without waiting to hear if the transaction is
  247. even valid, while `broadcast_tx_sync` will return with the result of
  248. running the transaction through `CheckTx`. Using `broadcast_tx_commit`
  249. will wait until the transaction is committed in a block or until some
  250. timeout is reached, but will return right away if the transaction does
  251. not pass `CheckTx`. The return value for `broadcast_tx_commit` includes
  252. two fields, `check_tx` and `deliver_tx`, pertaining to the result of
  253. running the transaction through those ABCI messages.
  254. The benefit of using `broadcast_tx_commit` is that the request returns
  255. after the transaction is committed (i.e. included in a block), but that
  256. can take on the order of a second. For a quick result, use
  257. `broadcast_tx_sync`, but the transaction will not be committed until
  258. later, and by that point its effect on the state may change.
  259. Note the mempool does not provide strong guarantees - just because a tx passed
  260. CheckTx (ie. was accepted into the mempool), doesn't mean it will be committed,
  261. as nodes with the tx in their mempool may crash before they get to propose.
  262. For more information, see the [mempool
  263. write-ahead-log](../tendermint-core/running-in-production.md#mempool-wal)
  264. ## Tendermint Networks
  265. When `tendermint init` is run, both a `genesis.json` and
  266. `priv_validator_key.json` are created in `~/.tendermint/config`. The
  267. `genesis.json` might look like:
  268. ```json
  269. {
  270. "validators" : [
  271. {
  272. "pub_key" : {
  273. "value" : "h3hk+QE8c6QLTySp8TcfzclJw/BG79ziGB/pIA+DfPE=",
  274. "type" : "tendermint/PubKeyEd25519"
  275. },
  276. "power" : 10,
  277. "name" : ""
  278. }
  279. ],
  280. "app_hash" : "",
  281. "chain_id" : "test-chain-rDlYSN",
  282. "genesis_time" : "0001-01-01T00:00:00Z"
  283. }
  284. ```
  285. And the `priv_validator_key.json`:
  286. ```json
  287. {
  288. "last_step" : 0,
  289. "last_round" : "0",
  290. "address" : "B788DEDE4F50AD8BC9462DE76741CCAFF87D51E2",
  291. "pub_key" : {
  292. "value" : "h3hk+QE8c6QLTySp8TcfzclJw/BG79ziGB/pIA+DfPE=",
  293. "type" : "tendermint/PubKeyEd25519"
  294. },
  295. "last_height" : "0",
  296. "priv_key" : {
  297. "value" : "JPivl82x+LfVkp8i3ztoTjY6c6GJ4pBxQexErOCyhwqHeGT5ATxzpAtPJKnxNx/NyUnD8Ebv3OIYH+kgD4N88Q==",
  298. "type" : "tendermint/PrivKeyEd25519"
  299. }
  300. }
  301. ```
  302. The `priv_validator_key.json` actually contains a private key, and should
  303. thus be kept absolutely secret; for now we work with the plain text.
  304. Note the `last_` fields, which are used to prevent us from signing
  305. conflicting messages.
  306. Note also that the `pub_key` (the public key) in the
  307. `priv_validator_key.json` is also present in the `genesis.json`.
  308. The genesis file contains the list of public keys which may participate
  309. in the consensus, and their corresponding voting power. Greater than 2/3
  310. of the voting power must be active (i.e. the corresponding private keys
  311. must be producing signatures) for the consensus to make progress. In our
  312. case, the genesis file contains the public key of our
  313. `priv_validator_key.json`, so a Tendermint node started with the default
  314. root directory will be able to make progress. Voting power uses an int64
  315. but must be positive, thus the range is: 0 through 9223372036854775807.
  316. Because of how the current proposer selection algorithm works, we do not
  317. recommend having voting powers greater than 10\^12 (ie. 1 trillion).
  318. If we want to add more nodes to the network, we have two choices: we can
  319. add a new validator node, who will also participate in the consensus by
  320. proposing blocks and voting on them, or we can add a new non-validator
  321. node, who will not participate directly, but will verify and keep up
  322. with the consensus protocol.
  323. ### Peers
  324. #### Seed
  325. A seed node is a node who relays the addresses of other peers which they know
  326. of. These nodes constantly crawl the network to try to get more peers. The
  327. addresses which the seed node relays get saved into a local address book. Once
  328. these are in the address book, you will connect to those addresses directly.
  329. Basically the seed nodes job is just to relay everyones addresses. You won't
  330. connect to seed nodes once you have received enough addresses, so typically you
  331. only need them on the first start. The seed node will immediately disconnect
  332. from you after sending you some addresses.
  333. #### Persistent Peer
  334. Persistent peers are people you want to be constantly connected with. If you
  335. disconnect you will try to connect directly back to them as opposed to using
  336. another address from the address book. On restarts you will always try to
  337. connect to these peers regardless of the size of your address book.
  338. All peers relay peers they know of by default. This is called the peer exchange
  339. protocol (PeX). With PeX, peers will be gossiping about known peers and forming
  340. a network, storing peer addresses in the addrbook. Because of this, you don't
  341. have to use a seed node if you have a live persistent peer.
  342. #### Connecting to Peers
  343. To connect to peers on start-up, specify them in the
  344. `$TMHOME/config/config.toml` or on the command line. Use `seeds` to
  345. specify seed nodes, and
  346. `persistent_peers` to specify peers that your node will maintain
  347. persistent connections with.
  348. For example,
  349. ```sh
  350. tendermint node --p2p.seeds "f9baeaa15fedf5e1ef7448dd60f46c01f1a9e9c4@1.2.3.4:26656,0491d373a8e0fcf1023aaf18c51d6a1d0d4f31bd@5.6.7.8:26656"
  351. ```
  352. Alternatively, you can use the `/dial_seeds` endpoint of the RPC to
  353. specify seeds for a running node to connect to:
  354. ```sh
  355. curl 'localhost:26657/dial_seeds?seeds=\["f9baeaa15fedf5e1ef7448dd60f46c01f1a9e9c4@1.2.3.4:26656","0491d373a8e0fcf1023aaf18c51d6a1d0d4f31bd@5.6.7.8:26656"\]'
  356. ```
  357. Note, with PeX enabled, you
  358. should not need seeds after the first start.
  359. If you want Tendermint to connect to specific set of addresses and
  360. maintain a persistent connection with each, you can use the
  361. `--p2p.persistent_peers` flag or the corresponding setting in the
  362. `config.toml` or the `/dial_peers` RPC endpoint to do it without
  363. stopping Tendermint core instance.
  364. ```sh
  365. tendermint node --p2p.persistent_peers "429fcf25974313b95673f58d77eacdd434402665@10.11.12.13:26656,96663a3dd0d7b9d17d4c8211b191af259621c693@10.11.12.14:26656"
  366. curl 'localhost:26657/dial_peers?persistent=true&peers=\["429fcf25974313b95673f58d77eacdd434402665@10.11.12.13:26656","96663a3dd0d7b9d17d4c8211b191af259621c693@10.11.12.14:26656"\]'
  367. ```
  368. ### Adding a Non-Validator
  369. Adding a non-validator is simple. Just copy the original `genesis.json`
  370. to `~/.tendermint/config` on the new machine and start the node,
  371. specifying seeds or persistent peers as necessary. If no seeds or
  372. persistent peers are specified, the node won't make any blocks, because
  373. it's not a validator, and it won't hear about any blocks, because it's
  374. not connected to the other peer.
  375. ### Adding a Validator
  376. The easiest way to add new validators is to do it in the `genesis.json`,
  377. before starting the network. For instance, we could make a new
  378. `priv_validator_key.json`, and copy it's `pub_key` into the above genesis.
  379. We can generate a new `priv_validator_key.json` with the command:
  380. ```sh
  381. tendermint gen_validator
  382. ```
  383. Now we can update our genesis file. For instance, if the new
  384. `priv_validator_key.json` looks like:
  385. ```json
  386. {
  387. "address" : "5AF49D2A2D4F5AD4C7C8C4CC2FB020131E9C4902",
  388. "pub_key" : {
  389. "value" : "l9X9+fjkeBzDfPGbUM7AMIRE6uJN78zN5+lk5OYotek=",
  390. "type" : "tendermint/PubKeyEd25519"
  391. },
  392. "priv_key" : {
  393. "value" : "EDJY9W6zlAw+su6ITgTKg2nTZcHAH1NMTW5iwlgmNDuX1f35+OR4HMN88ZtQzsAwhETq4k3vzM3n6WTk5ii16Q==",
  394. "type" : "tendermint/PrivKeyEd25519"
  395. },
  396. "last_step" : 0,
  397. "last_round" : "0",
  398. "last_height" : "0"
  399. }
  400. ```
  401. then the new `genesis.json` will be:
  402. ```json
  403. {
  404. "validators" : [
  405. {
  406. "pub_key" : {
  407. "value" : "h3hk+QE8c6QLTySp8TcfzclJw/BG79ziGB/pIA+DfPE=",
  408. "type" : "tendermint/PubKeyEd25519"
  409. },
  410. "power" : 10,
  411. "name" : ""
  412. },
  413. {
  414. "pub_key" : {
  415. "value" : "l9X9+fjkeBzDfPGbUM7AMIRE6uJN78zN5+lk5OYotek=",
  416. "type" : "tendermint/PubKeyEd25519"
  417. },
  418. "power" : 10,
  419. "name" : ""
  420. }
  421. ],
  422. "app_hash" : "",
  423. "chain_id" : "test-chain-rDlYSN",
  424. "genesis_time" : "0001-01-01T00:00:00Z"
  425. }
  426. ```
  427. Update the `genesis.json` in `~/.tendermint/config`. Copy the genesis
  428. file and the new `priv_validator_key.json` to the `~/.tendermint/config` on
  429. a new machine.
  430. Now run `tendermint node` on both machines, and use either
  431. `--p2p.persistent_peers` or the `/dial_peers` to get them to peer up.
  432. They should start making blocks, and will only continue to do so as long
  433. as both of them are online.
  434. To make a Tendermint network that can tolerate one of the validators
  435. failing, you need at least four validator nodes (e.g., 2/3).
  436. Updating validators in a live network is supported but must be
  437. explicitly programmed by the application developer. See the [application
  438. developers guide](../app-dev/app-development.md) for more details.
  439. ### Local Network
  440. To run a network locally, say on a single machine, you must change the `_laddr`
  441. fields in the `config.toml` (or using the flags) so that the listening
  442. addresses of the various sockets don't conflict. Additionally, you must set
  443. `addr_book_strict=false` in the `config.toml`, otherwise Tendermint's p2p
  444. library will deny making connections to peers with the same IP address.
  445. ### Upgrading
  446. See the
  447. [UPGRADING.md](https://github.com/tendermint/tendermint/blob/master/UPGRADING.md)
  448. guide. You may need to reset your chain between major breaking releases.
  449. Although, we expect Tendermint to have fewer breaking releases in the future
  450. (especially after 1.0 release).