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.

585 lines
19 KiB

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