github.com/okex/exchain@v1.8.0/libs/tendermint/docs/tendermint-core/using-tendermint.md (about)

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