cometbft 程序。
它只假设你已经安装了 cometbft 二进制文件,并且
对 CometBFT 和 ABCI 有一些基础了解。
你可以使用 cometbft --help 查看帮助菜单,使用 cometbft version
查看版本号。
根目录
区块链数据的默认目录是~/.cometbft。你可以通过设置
环境变量 CMTHOME 来覆盖它。
初始化
运行以下命令初始化根目录:$CMTHOME/config 中创建一个新的私钥(priv_validator_key.json)
和一个包含对应公钥的创世文件(genesis.json)。
这些内容足以在本地运行一个仅含一个验证者的测试网。
如需更复杂的初始化,请查看 testnet 命令:
创世
$CMTHOME/config/ 中的 genesis.json 文件定义了
区块链创世时的初始 CometBFT 状态(参见定义)。
字段
genesis_time:区块链启动的正式时间。chain_id:区块链的 ID。它对每条区块链都必须唯一。 如果你的测试网区块链没有唯一的 chain ID,会遇到很多问题。 ChainID 的长度必须少于 50 个字符。initial_height:CometBFT 应开始运行的高度。如果区块链正在执行网络升级, 从停止时的高度重新开始可以让此前的高度保持唯一性。consensus_params(参见规范)blockmax_bytes:区块最大大小,单位为字节。max_gas:每个区块的最大 gas。
evidencemax_age_num_blocks:证据的最大保留区块数。其基本计算公式为: MaxAgeDuration /(平均出块时间)。max_age_duration:证据的最大保留时长。它应与应用的“解绑期” 或其他用于处理 Nothing-At-Stake 攻击 的类似机制相对应。max_bytes:设置单个区块中可提交证据的最大字节数, 应明显小于区块最大字节数上限。
validatorpub_key_types:验证者可使用的公钥类型。
versionapp_version:ABCI 应用版本。
validators:初始验证者列表。注意,这一项可能会被应用完全覆盖; 也可以显式留空,以表明应用会在InitChain时初始化验证者集合。pub_key:第一个元素指定密钥类型, 使用已采用 密钥类型 所声明的PubKeyName。 第二个元素是公钥字节。power:验证者的投票权重。name:验证者名称(可选)。
app_hash:创世时预期的应用哈希(由ResponseInfoABCI 消息返回)。如果应用返回的哈希 不匹配,CometBFT 会 panic。app_state:应用状态(例如代币的初始分配)。
:warning: ChainID 对每条区块链都必须唯一。重复使用旧的 chainID 可能导致问题
genesis.json 示例
运行
使用以下命令运行一个 CometBFT 节点:tcp://127.0.0.1:26658 的 ABCI 应用。如果你已经安装了 kvstore ABCI 应用,
请在另一个窗口中运行它。如果没有,请停止 CometBFT,然后运行
kvstore 应用的进程内版本:
counter、kvstore 和 noop 应用的进程内版本,
这些示例随 abci-cli 一起提供。如果你的应用是用 Go 编写的,
将其以内嵌进程方式与 CometBFT 一起编译会很容易。如果你的应用不是用
Go 编写的,请在另一个进程中运行它,并使用 --proxy_app 标志指定
它监听的 socket 地址,例如:
cometbft node --help 查看支持哪些标志。
交易
要发送交易,请使用curl 向 CometBFT RPC 服务器发起请求,
例如:
/status 端点查看链的状态:
latest_app_hash:
http://localhost:26657 可以查看其他端点列表。
有些端点不需要参数(例如 /status),而另一些会指定参数名,
并用 _ 作为占位符。
提示:RPC 文档见 这里
格式化
发送/格式化交易时,需要注意以下细节: 使用GET:
要发送 UTF8 字符串字节数组,请为 tx 参数的值加上引号:
0x:
POST(通过 json)时,原始十六进制必须经过 base64 编码:
POST 交易中不能直接使用原始十六进制。
重置
:warning: 不安全 仅在开发环境中这样做,并且仅当你可以承担丢失全部区块链数据的后果时才这样做!要重置区块链,请先停止节点,然后运行:
配置
CometBFT 使用config.toml 进行配置。详情请参见配置规范。
值得注意的选项包括应用的 socket 地址
(proxy_app)、CometBFT 对等节点的监听地址
(p2p.laddr),以及 RPC 服务器的监听地址
(rpc.laddr)。
配置文件中的某些字段可以通过标志覆盖。
禁用空区块
虽然cometbft 的默认行为仍然是大约每秒创建一个区块,
但你也可以禁用空区块,或者设置区块创建间隔。前一种情况下,
只有在有新交易或 AppHash 发生变化时才会创建区块。
如果你希望 CometBFT 仅在存在交易或应用哈希变化时才生成区块,
请使用以下附加标志运行 CometBFT:
config.toml 文件中设置:
false。
区块间隔设置允许你在每个新空区块创建之间增加延迟(使用 time.Duration 格式,参见 ParseDuration)。
可以通过以下附加标志设置:
config.toml 文件中设置:
create_empty_blocks 值的影响。
Broadcast API
前面我们使用了broadcast_tx_commit 端点发送交易。
当一笔交易被发送到 CometBFT 节点时,它会先通过应用执行 CheckTx。
如果通过了 CheckTx,它就会被加入 mempool、广播给其他对等节点,
并最终被打包进某个区块。
由于交易处理分为多个阶段,我们提供了多个广播交易的端点:
broadcast_tx_async 会立即返回,而不会等待确认该交易是否
有效;broadcast_tx_sync 会在交易通过 CheckTx 后返回对应结果。
使用 broadcast_tx_commit 时,会一直等待到交易被提交进区块,
或直到达到某个超时时间;但如果交易未通过 CheckTx,它会立即返回。
broadcast_tx_commit 的返回值包含两个字段:check_tx 和 deliver_tx,
分别对应交易经过这些 ABCI 消息处理后的结果。
使用 broadcast_tx_commit 的好处是,请求会在交易提交后
(即被包含进区块后)返回,但这通常需要大约一秒。
如果你想更快拿到结果,可以使用 broadcast_tx_sync,不过交易要稍后
才会被提交,到那时它对状态的影响可能已经发生变化。
请注意,mempool 不提供强保证。交易通过 CheckTx(即被 mempool 接收)
并不意味着它一定会被提交,因为持有该交易的节点可能会在轮到自己提议区块前崩溃。
更多信息请参见 mempool
预写日志。
CometBFT 网络
当运行cometbft init 时,~/.cometbft/config 中会同时创建
genesis.json 和 priv_validator_key.json。
genesis.json 可能如下所示:
priv_validator_key.json 如下:
priv_validator_key.json 实际上包含私钥,因此必须绝对保密;
这里只是暂时以明文形式讨论它。
注意其中的 last_ 字段,它们用于防止我们对相互冲突的消息签名。
还要注意,priv_validator_key.json 中的 pub_key
(公钥)同样也存在于 genesis.json 中。
创世文件包含允许参与共识的公钥列表,以及它们对应的投票权重。
超过 2/3 的投票权重必须处于活跃状态(即对应私钥必须能够产生签名),
共识才能继续推进。在我们的例子中,创世文件包含了
priv_validator_key.json 的公钥,因此使用默认根目录启动的
CometBFT 节点能够推进共识。投票权重使用 int64,但必须为正,
因此范围是 0 到 9223372036854775807。由于当前提议者选择算法的实现方式,
我们不建议将投票权重设置为大于 10^12(即 1 万亿)。
如果我们想向网络中添加更多节点,有两种选择:
可以添加新的验证者节点,它会通过提议区块和对区块投票来参与共识;
也可以添加新的非验证者节点,它不会直接参与,但会验证并跟进
共识协议。
对等节点
Seed
Seed 节点是会转发其所知道的其他对等节点地址的节点。 这些节点会持续爬取网络以获取更多对等节点。Seed 节点转发的地址 会被保存到本地地址簿中。一旦这些地址进入地址簿, 你就会直接连接到这些地址。基本上,Seed 节点的职责只是转发所有人的地址。 当你获取到足够多的地址后,就不会继续连接 Seed 节点了, 因此通常只在第一次启动时需要它们。Seed 节点在向你发送一些地址后, 会立即断开连接。持久对等节点
持久对等节点是你希望始终保持连接的节点。 如果连接断开,你会尝试直接重新连接它们, 而不是使用地址簿中的其他地址。重启后,无论地址簿大小如何, 你都会始终尝试连接这些对等节点。 默认情况下,所有对等节点都会转发它们所知道的其他对等节点。 这被称为对等交换协议(PEX)。有了 PEX,对等节点会持续传播已知的 对等节点信息并形成网络,同时把对等节点地址存储在 addrbook 中。 正因如此,如果你已经有一个在线的持久对等节点, 就不一定还需要使用 Seed 节点。连接到对等节点
要在启动时连接到对等节点,请在$CMTHOME/config/config.toml 或命令行中指定它们。使用 seeds
指定 Seed 节点,使用
persistent_peers 指定你的节点要维持持久连接的对等节点。
例如:
/dial_seeds 端点,
为正在运行的节点指定要连接的 Seed:
--p2p.persistent_peers 标志、config.toml 中对应的设置,
或 /dial_peers RPC 端点,而无需停止 CometBFT 实例。
添加非验证者节点
添加非验证者节点很简单。只需将原始的genesis.json
复制到新机器上的 ~/.cometbft/config,然后启动节点,
并根据需要指定 Seed 或持久对等节点。如果未指定 Seed 或
持久对等节点,该节点将不会生成任何区块,因为它不是验证者;
同时它也收不到任何区块,因为它没有连接到其他对等节点。
添加验证者
添加新验证者最简单的方法是在网络启动前, 直接在genesis.json 中完成。例如,我们可以生成一个新的
priv_validator_key.json,并把其中的 pub_key 复制到上面的创世文件中。
我们可以使用以下命令生成新的 priv_validator_key.json:
priv_validator_key.json 如下所示:
genesis.json 将会是:
~/.cometbft/config 中的 genesis.json。然后将该创世文件
和新的 priv_validator_key.json 复制到新机器上的 ~/.cometbft/config。
现在在两台机器上都运行 cometbft node,并使用
--p2p.persistent_peers 或 /dial_peers 让它们建立对等连接。
它们应该会开始生成区块,并且只有在两台机器都在线时才会继续。
如果你希望 CometBFT 网络在一个验证者失效时仍能容错,
则至少需要四个验证者节点(例如满足 2/3)。
支持在运行中的网络里更新验证者,但这必须由应用开发者
显式编程实现。更多细节请参见应用开发者指南。
本地网络
如果要在本地运行一个网络,例如在单台机器上运行, 你必须修改config.toml 中的 _laddr 字段(或使用对应标志),
以避免不同 socket 的监听地址发生冲突。此外,你还必须在
config.toml 中设置 addr_book_strict=false,否则 CometBFT 的 p2p
库会拒绝与具有相同 IP 地址的对等节点建立连接。
升级
请参见 UPGRADING.md 指南。在重大破坏性版本之间,你可能需要重置链。 不过,我们预计未来 CometBFT 的破坏性版本会更少, 尤其是在 1.0 发布之后。This is a guide to using the
cometbft program from the command line.
It assumes only that you have the cometbft binary installed and have
some rudimentary idea of what CometBFT and ABCI are.
You can see the help menu with cometbft --help, and the version
number with cometbft version.
Directory Root
The default directory for blockchain data is~/.cometbft. Override
this by setting the CMTHOME environment variable.
Initialize
Initialize the root directory by running:priv_validator_key.json) and a
genesis file (genesis.json) containing the associated public key in
$CMTHOME/config. This is all that’s necessary to run a local testnet
with one validator.
For more elaborate initialization, see the testnet command:
Genesis
Thegenesis.json file in $CMTHOME/config/ defines the initial
CometBFT state upon genesis of the blockchain (see
definition).
Fields
genesis_time: Official time of blockchain start.chain_id: ID of the blockchain. This must be unique for every blockchain. If your testnet blockchains do not have unique chain IDs, you will have a bad time. The ChainID must be less than 50 symbols.initial_height: Height at which CometBFT should begin. If a blockchain is conducting a network upgrade, starting from the stopped height brings uniqueness to previous heights.consensus_params(see spec)blockmax_bytes: Max block size, in bytes.max_gas: Max gas per block.
evidencemax_age_num_blocks: Max age of evidence, in blocks. The basic formula for calculating this is: MaxAgeDuration / (average block time).max_age_duration: Max age of evidence, in time. It should correspond with an app’s “unbonding period” or other similar mechanism for handling Nothing-At-Stake attacks.max_bytes: This sets the maximum size in bytes of evidence that can be committed in a single block and should fall comfortably under the max block bytes.
validatorpub_key_types: Public key types validators can use.
versionapp_version: ABCI application version.
validators: List of initial validators. Note this may be overridden entirely by the application, and may be left empty to make explicit that the application will initialize the validator set uponInitChain.pub_key: The first element specifies the key type, using the declaredPubKeyNamefor the adopted key type. The second element are the pubkey bytes.power: The validator’s voting power.name: Name of the validator (optional).
app_hash: The expected application hash (as returned by theResponseInfoABCI message) upon genesis. If the app’s hash does not match, CometBFT will panic.app_state: The application state (e.g., initial distribution of tokens).
:warning: ChainID must be unique to every blockchain. Reusing old chainID can cause issues
Sample genesis.json
Run
To run a CometBFT node, use:tcp://127.0.0.1:26658. If you have the kvstore ABCI app installed, run it in
another window. If you don’t, kill CometBFT and run an in-process version of
the kvstore app:
counter, kvstore, and noop
apps that ship as examples with abci-cli. It’s easy to compile your app
in-process with CometBFT if it’s written in Go. If your app is not written in
Go, run it in another process, and use the --proxy_app flag to specify the
address of the socket it is listening on, for instance:
cometbft node --help.
Transactions
To send a transaction, usecurl to make requests to the CometBFT RPC
server, for example:
/status endpoint:
latest_app_hash in particular:
http://localhost:26657 in your browser to see the list of other
endpoints. Some take no arguments (like /status), while others specify
the argument name and use _ as a placeholder.
TIP: Find the RPC Documentation here
Formatting
The following nuances when sending/formatting transactions should be taken into account: WithGET:
To send a UTF8 string byte array, quote the value of the tx parameter:
0x:
POST (using json), the raw hex must be base64 encoded:
POST transactions.
Reset
:warning: UNSAFE Only do this in development and only if you can afford to lose all blockchain data!To reset a blockchain, stop the node and run:
Configuration
CometBFT uses aconfig.toml for configuration. For details, see the
config specification.
Notable options include the socket address of the application
(proxy_app), the listening address of the CometBFT peer
(p2p.laddr), and the listening address of the RPC server
(rpc.laddr).
Some fields from the config file can be overwritten with flags.
No Empty Blocks
While the default behavior ofcometbft is still to create blocks
approximately once per second, it is possible to disable empty blocks or
set a block creation interval. In the former case, blocks will be
created when there are new transactions or when the AppHash changes.
To configure CometBFT to not produce empty blocks unless there are
transactions or the app hash changes, run CometBFT with this
additional flag:
config.toml file:
false.
The block interval setting allows for a delay (in time.Duration format ParseDuration) between the
creation of each new empty block. It can be set with this additional flag:
config.toml file:
create_empty_blocks.
Broadcast API
Earlier, we used thebroadcast_tx_commit endpoint to send a
transaction. When a transaction is sent to a CometBFT node, it will
run via CheckTx against the application. If it passes CheckTx, it
will be included in the mempool, broadcast to other peers, and
eventually included in a block.
Since there are multiple phases to processing a transaction, we offer
multiple endpoints to broadcast a transaction:
broadcast_tx_async
will return right away without waiting to hear if the transaction is
even valid, while broadcast_tx_sync will return with the result of
running the transaction through CheckTx. Using broadcast_tx_commit
will wait until the transaction is committed in a block or until some
timeout is reached, but will return right away if the transaction does
not pass CheckTx. The return value for broadcast_tx_commit includes
two fields, check_tx and deliver_tx, pertaining to the result of
running the transaction through those ABCI messages.
The benefit of using broadcast_tx_commit is that the request returns
after the transaction is committed (i.e., included in a block), but that
can take on the order of a second. For a quick result, use
broadcast_tx_sync, but the transaction will not be committed until
later, and by that point its effect on the state may change.
Note the mempool does not provide strong guarantees - just because a tx passed
CheckTx (i.e., was accepted into the mempool) doesn’t mean it will be committed,
as nodes with the tx in their mempool may crash before they get to propose.
For more information, see the mempool
write-ahead-log.
CometBFT Networks
Whencometbft init is run, both a genesis.json and
priv_validator_key.json are created in ~/.cometbft/config. The
genesis.json might look like:
priv_validator_key.json:
priv_validator_key.json actually contains a private key, and should
thus be kept absolutely secret; for now we work with the plain text.
Note the last_ fields, which are used to prevent us from signing
conflicting messages.
Note also that the pub_key (the public key) in the
priv_validator_key.json is also present in the genesis.json.
The genesis file contains the list of public keys which may participate
in the consensus, and their corresponding voting power. Greater than 2/3
of the voting power must be active (i.e., the corresponding private keys
must be producing signatures) for the consensus to make progress. In our
case, the genesis file contains the public key of our
priv_validator_key.json, so a CometBFT node started with the default
root directory will be able to make progress. Voting power uses an int64
but must be positive, thus the range is 0 through 9223372036854775807.
Because of how the current proposer selection algorithm works, we do not
recommend having voting powers greater than 10^12 (i.e., 1 trillion).
If we want to add more nodes to the network, we have two choices: we can
add a new validator node, who will also participate in the consensus by
proposing blocks and voting on them, or we can add a new non-validator
node, who will not participate directly, but will verify and keep up
with the consensus protocol.
Peers
Seed
A seed node is a node who relays the addresses of other peers which they know of. These nodes constantly crawl the network to try to get more peers. The addresses which the seed node relays get saved into a local address book. Once these are in the address book, you will connect to those addresses directly. Basically the seed node’s job is just to relay everyone’s addresses. You won’t connect to seed nodes once you have received enough addresses, so typically you only need them on the first start. The seed node will immediately disconnect from you after sending you some addresses.Persistent Peer
Persistent peers are people you want to be constantly connected with. If you disconnect you will try to connect directly back to them as opposed to using another address from the address book. On restarts you will always try to connect to these peers regardless of the size of your address book. All peers relay peers they know of by default. This is called the peer exchange protocol (PEX). With PEX, peers will be gossiping about known peers and forming a network, storing peer addresses in the addrbook. Because of this, you don’t have to use a seed node if you have a live persistent peer.Connecting to Peers
To connect to peers on start-up, specify them in the$CMTHOME/config/config.toml or on the command line. Use seeds to
specify seed nodes, and
persistent_peers to specify peers that your node will maintain
persistent connections with.
For example,
/dial_seeds endpoint of the RPC to
specify seeds for a running node to connect to:
--p2p.persistent_peers flag or the corresponding setting in the
config.toml or the /dial_peers RPC endpoint to do it without
stopping the CometBFT instance.
Adding a Non-Validator
Adding a non-validator is simple. Just copy the originalgenesis.json
to ~/.cometbft/config on the new machine and start the node,
specifying seeds or persistent peers as necessary. If no seeds or
persistent peers are specified, the node won’t make any blocks, because
it’s not a validator, and it won’t hear about any blocks, because it’s
not connected to the other peer.
Adding a Validator
The easiest way to add new validators is to do it in thegenesis.json
before starting the network. For instance, we could make a new
priv_validator_key.json and copy its pub_key into the above genesis.
We can generate a new priv_validator_key.json with the command:
priv_validator_key.json looks like:
genesis.json will be:
genesis.json in ~/.cometbft/config. Copy the genesis
file and the new priv_validator_key.json to the ~/.cometbft/config on
a new machine.
Now run cometbft node on both machines, and use either
--p2p.persistent_peers or the /dial_peers to get them to peer up.
They should start making blocks, and will only continue to do so as long
as both of them are online.
To make a CometBFT network that can tolerate one of the validators
failing, you need at least four validator nodes (e.g., 2/3).
Updating validators in a live network is supported but must be
explicitly programmed by the application developer. See the application
developers guide for more details.
Local Network
To run a network locally, say on a single machine, you must change the_laddr
fields in the config.toml (or using the flags) so that the listening
addresses of the various sockets don’t conflict. Additionally, you must set
addr_book_strict=false in the config.toml, otherwise CometBFT’s p2p
library will deny making connections to peers with the same IP address.