概述

这是一份快速入门指南。如果你已经大致了解 CometBFT 的工作方式,并希望立即开始使用,请继续阅读。

安装

请参阅安装指南。

初始化

运行:
cometbft init
将会为单个本地节点创建所需文件。 这些文件位于 $HOME/.cometbft:
$ ls $HOME/.cometbft

config  data

$ ls $HOME/.cometbft/config/

config.toml  genesis.json  node_key.json  priv_validator.json
对于单个本地节点,不需要进一步配置。 关于集群配置的说明见下文。

本地节点

使用一个简单的进程内应用启动 CometBFT:
cometbft node --proxy_app=kvstore
注意:kvstore 是一个非持久化应用。如果你希望运行带持久化能力的应用,请使用 --proxy_app=persistent_kvstore。
随后区块将开始持续输出:
I[01-06|01:45:15.592] Executed block                               module=state height=1 validTxs=0 invalidTxs=0
I[01-06|01:45:15.624] Committed state                              module=state height=1 txs=0 appHash=
使用以下命令检查状态:
curl -s localhost:26657/status

发送交易

在 KVstore 应用运行后,我们可以发送交易:
curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"'
然后通过以下命令检查是否成功:
curl -s 'localhost:26657/abci_query?data="abcd"'
我们也可以发送带键和值的交易:
curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"'
然后查询这个键:
curl -s 'localhost:26657/abci_query?data="name"'
返回的值为十六进制格式。

节点集群

首先,创建四台 Ubuntu 云主机。以下步骤已在 Digital Ocean Ubuntu 16.04 x64(3GB/1CPU,20GB SSD)上测试通过。下文将它们各自的 IP 地址分别记为 IP1、IP2、IP3、IP4。 然后,ssh 登录每台机器,并按照说明安装 CometBFT。 接下来,使用 cometbft testnet 命令创建四个配置文件目录(位于 ./mytestnet),并将每个目录复制到云中的对应机器上,使每台机器都拥有一个 $HOME/mytestnet/node[0-3] 目录。 在启动网络之前,你需要获取各节点的对等节点标识符(仅有 IP 不够,而且 IP 可能会变化)。下文将它们分别记为 ID1、ID2、ID3、ID4。
cometbft show_node_id --home ./mytestnet/node0
cometbft show_node_id --home ./mytestnet/node1
cometbft show_node_id --home ./mytestnet/node2
cometbft show_node_id --home ./mytestnet/node3
下面是一段方便的 Bash 脚本,用于拼接 persistent peers 字符串,下一步会用到它:
#!/bin/bash

# Check if the required argument is provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <ip1> <ip2> <ip3> ..."
    exit 1
fi

# Command to run on each IP
BASE_COMMAND="cometbft show_node_id --home ./mytestnet/node"

# Initialize an array to store results
PERSISTENT_PEERS=""

# Iterate through provided IPs
for i in "${!@}"; do
    IP="${!i}"
    NODE_IDX=$((i - 1))  # Adjust for zero-based indexing

    echo "Getting ID of $IP (node $NODE_IDX)..."

    # Run the command on the current IP and capture the result
    ID=$($BASE_COMMAND$NODE_IDX)

    # Store the result in the array
    PERSISTENT_PEERS+="$ID@$IP:26656"

    # Add a comma if not the last IP
    if [ $i -lt $# ]; then
        PERSISTENT_PEERS+="," 
    fi
done

echo "$PERSISTENT_PEERS"
最后,在每台机器上分别运行:
cometbft node --home ./mytestnet/node0 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
cometbft node --home ./mytestnet/node1 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
cometbft node --home ./mytestnet/node2 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
cometbft node --home ./mytestnet/node3 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
注意,在第三个节点启动之后,区块就会开始持续输出,因为超过 2/3 的验证者(定义在 genesis.json 中)已经上线。 也可以在 config.toml 中指定 persistent peers。有关配置选项的更多信息,请参阅这里。 之后,你就可以像上面单个本地节点示例中那样发送交易。

Overview

This is a quick start guide. If you have a general idea about how CometBFT works and want to get started right away, continue.

Install

See the install guide.

Initialization

Running:
cometbft init
will create the required files for a single, local node. These files are found in $HOME/.cometbft:
$ ls $HOME/.cometbft

config  data

$ ls $HOME/.cometbft/config/

config.toml  genesis.json  node_key.json  priv_validator.json
For a single, local node, no further configuration is required. Configuring a cluster is covered further below.

Local Node

Start CometBFT with a simple in-process application:
cometbft node --proxy_app=kvstore
Note: kvstore is a non-persistent app. If you would like to run an application with persistence, run --proxy_app=persistent_kvstore.
and blocks will start to stream in:
I[01-06|01:45:15.592] Executed block                               module=state height=1 validTxs=0 invalidTxs=0
I[01-06|01:45:15.624] Committed state                              module=state height=1 txs=0 appHash=
Check the status with:
curl -s localhost:26657/status

Sending Transactions

With the KVstore app running, we can send transactions:
curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"'
and check that it worked with:
curl -s 'localhost:26657/abci_query?data="abcd"'
We can send transactions with a key and value too:
curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"'
and query the key:
curl -s 'localhost:26657/abci_query?data="name"'
where the value is returned in hex.

Cluster of Nodes

First, create four Ubuntu cloud machines. The following was tested on Digital Ocean Ubuntu 16.04 x64 (3GB/1CPU, 20GB SSD). We’ll refer to their respective IP addresses below as IP1, IP2, IP3, IP4. Then, ssh into each machine and install CometBFT following the instructions. Next, use the cometbft testnet command to create four directories of config files (found in ./mytestnet) and copy each directory to the relevant machine in the cloud, so that each machine has a $HOME/mytestnet/node[0-3] directory. Before you can start the network, you’ll need peer identifiers (IPs are not enough and can change). We’ll refer to them as ID1, ID2, ID3, ID4.
cometbft show_node_id --home ./mytestnet/node0
cometbft show_node_id --home ./mytestnet/node1
cometbft show_node_id --home ./mytestnet/node2
cometbft show_node_id --home ./mytestnet/node3
Here’s a handy Bash script to compile the persistent peers string, which will be needed for our next step:
#!/bin/bash

# Check if the required argument is provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <ip1> <ip2> <ip3> ..."
    exit 1
fi

# Command to run on each IP
BASE_COMMAND="cometbft show_node_id --home ./mytestnet/node"

# Initialize an array to store results
PERSISTENT_PEERS=""

# Iterate through provided IPs
for i in "${!@}"; do
    IP="${!i}"
    NODE_IDX=$((i - 1))  # Adjust for zero-based indexing

    echo "Getting ID of $IP (node $NODE_IDX)..."

    # Run the command on the current IP and capture the result
    ID=$($BASE_COMMAND$NODE_IDX)

    # Store the result in the array
    PERSISTENT_PEERS+="$ID@$IP:26656"

    # Add a comma if not the last IP
    if [ $i -lt $# ]; then
        PERSISTENT_PEERS+=","
    fi
done

echo "$PERSISTENT_PEERS"
Finally, from each machine, run:
cometbft node --home ./mytestnet/node0 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
cometbft node --home ./mytestnet/node1 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
cometbft node --home ./mytestnet/node2 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
cometbft node --home ./mytestnet/node3 --proxy_app=kvstore --p2p.persistent_peers="ID1@IP1:26656,ID2@IP2:26656,ID3@IP3:26656,ID4@IP4:26656"
Note that after the third node is started, blocks will start to stream in because >2/3 of validators (defined in the genesis.json) have come online. Persistent peers can also be specified in the config.toml. See here for more information about configuration options. Transactions can then be sent as covered in the single, local node example above.