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.

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