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