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.

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