为了便于对 ABCI 服务器和简单应用进行测试与调试,我们构建了一个 CLI 工具 abci-cli,用于从命令行发送 ABCI 消息。

安装

请确保你已经安装 Go。 接下来,安装 abci-cli 工具和示例应用:
git clone https://github.com/cometbft/cometbft.git
cd cometbft
make install_abci
现在运行 abci-cli 以查看命令列表:
Usage:
  abci-cli [command]

Available Commands:
  batch            run a batch of abci commands against an application
  check_tx         validate a transaction
  commit           commit the application state and return the Merkle root hash
  completion       Generate the autocompletion script for the specified shell
  console          start an interactive ABCI console for multiple commands
  echo             have the application echo a message
  finalize_block   deliver a block of transactions to the application
  help             Help about any command
  info             get some info about the application
  kvstore          ABCI demo example
  prepare_proposal prepare proposal
  process_proposal process proposal
  query            query the application state
  test             run integration tests
  version          print ABCI console version

Flags:
      --abci string        either socket or grpc (default "socket")
      --address string     address of application socket (default "tcp://0.0.0.0:26658")
  -h, --help               help for abci-cli
      --log_level string   set the logger level (default "debug")
  -v, --verbose            print the command and results as if it were a console session

Use "abci-cli [command] --help" for more information about a command.

KVStore - 第一个示例

abci-cli 工具让我们能够向应用发送 ABCI 消息,从而帮助构建和调试应用。 最重要的消息是 deliver_tx、check_tx 和 commit,此外还有一些消息用于便捷操作、配置和信息查询。 我们先启动一个 kvstore 应用,它与上面的 abci-cli 一起安装。kvstore 只是将交易存储在一棵 Merkle 树中。它的代码可在这里查看。 运行以下命令启动应用:
abci-cli kvstore
然后在另一个终端中运行
abci-cli echo hello
abci-cli info
你会看到类似如下内容:
-> data: hello
-> data.hex: 68656C6C6F
以及:
-> data: {"size":0}
-> data.hex: 7B2273697A65223A307D
一个 ABCI 应用必须提供两样东西:
  • 一个 socket 服务器
  • 一个 ABCI 消息处理器
当我们运行 abci-cli 工具时,会打开一个到应用 socket 服务器的新连接,发送给定的 ABCI 消息,然后等待响应。 服务器对于某种特定语言来说可以是通用的,我们提供了一个 Golang 参考实现。关于其他语言的服务器,请参阅其他 ABCI 实现列表。 处理器则是应用特定的,可以采用任意实现方式,只要它是确定性的并符合 ABCI 接口规范即可。 因此,当我们运行 abci-cli info 时,会打开一个到 ABCI 服务器的新连接,服务器会调用应用上的 Info() 方法,并告诉我们 Merkle 树中有多少笔交易。 由于每条命令都会打开一个新连接,我们提供了 abci-cli console 和 abci-cli batch 命令,以便通过单个连接发送多条 ABCI 消息。 运行 abci-cli console 后,你应该会进入一个交互式控制台,用来向应用发送 ABCI 消息。 尝试运行以下命令:
> echo hello
-> code: OK
-> data: hello
-> data.hex: 0x68656C6C6F

> info
-> code: OK
-> data: {"size":0}
-> data.hex: 0x7B2273697A65223A307D

> prepare_proposal "abc=123"
-> code: OK
-> log: Succeeded. Tx: abc=123

> process_proposal "abc==456"
-> code: OK
-> status: REJECT

> process_proposal "abc=123"
-> code: OK
-> status: ACCEPT

> finalize_block "abc=123"
-> code: OK
-> code: OK
-> data.hex: 0x0200000000000000

> commit
-> code: OK

> info
-> code: OK
-> data: {"size":1}
-> data.hex: 0x7B2273697A65223A317D

> query "abc"
-> code: OK
-> log: exists
-> height: 0
-> key: abc
-> key.hex: 616263
-> value: 123
-> value.hex: 313233

> finalize_block "def=xyz" "ghi=123"
-> code: OK
-> code: OK
-> code: OK
-> data.hex: 0x0600000000000000

> commit
-> code: OK

> query "def"
-> code: OK
-> log: exists
-> height: 0
-> key: def
-> key.hex: 646566
-> value: xyz
-> value.hex: 78797A
请注意,如果执行 finalize_block "abc" ...,它会存储 (abc, abc);但如果执行 finalize_block "abc=efg" ...,它会存储 (abc, efg)。 你也可以把这些命令放进一个文件中,然后运行 abci-cli --verbose batch < myfile。 请注意,abci-cli 严格来说只是为测试和调试而设计的。在真实部署中,发送消息这一角色由 CometBFT 承担,它会通过四条独立连接接入应用,并且每条连接都有各自的消息模式。 有关将 ABCI 应用与 CometBFT 一起运行的示例,请参阅入门指南。

悬赏

想用你最喜欢的语言编写一个应用吗?我们很乐意将你加入我们的生态系统! 请查看 Interchain Foundation 提供的资助机会,以支持新语言实现及更多相关工作。
To facilitate testing and debugging of ABCI servers and simple apps, we built a CLI, the abci-cli, for sending ABCI messages from the command line.

Install

Make sure you have Go installed. Next, install the abci-cli tool and example applications:
git clone https://github.com/cometbft/cometbft.git
cd cometbft
make install_abci
Now run abci-cli to see the list of commands:
Usage:
  abci-cli [command]

Available Commands:
  batch            run a batch of abci commands against an application
  check_tx         validate a transaction
  commit           commit the application state and return the Merkle root hash
  completion       Generate the autocompletion script for the specified shell
  console          start an interactive ABCI console for multiple commands
  echo             have the application echo a message
  finalize_block   deliver a block of transactions to the application
  help             Help about any command
  info             get some info about the application
  kvstore          ABCI demo example
  prepare_proposal prepare proposal
  process_proposal process proposal
  query            query the application state
  test             run integration tests
  version          print ABCI console version

Flags:
      --abci string        either socket or grpc (default "socket")
      --address string     address of application socket (default "tcp://0.0.0.0:26658")
  -h, --help               help for abci-cli
      --log_level string   set the logger level (default "debug")
  -v, --verbose            print the command and results as if it were a console session

Use "abci-cli [command] --help" for more information about a command.

KVStore - First Example

The abci-cli tool lets us send ABCI messages to our application to help build and debug them. The most important messages are deliver_tx, check_tx, and commit, but there are others for convenience, configuration, and information purposes. We’ll start a kvstore application, which was installed at the same time as abci-cli above. The kvstore just stores transactions in a Merkle tree. Its code can be found here. Start the application by running:
abci-cli kvstore
And in another terminal, run
abci-cli echo hello
abci-cli info
You’ll see something like:
-> data: hello
-> data.hex: 68656C6C6F
and:
-> data: {"size":0}
-> data.hex: 7B2273697A65223A307D
An ABCI application must provide two things:
  • a socket server
  • a handler for ABCI messages
When we run the abci-cli tool, we open a new connection to the application’s socket server, send the given ABCI message, and wait for a response. The server may be generic for a particular language, and we provide a reference implementation in Golang. See the list of other ABCI implementations for servers in other languages. The handler is specific to the application, and may be arbitrary, so long as it is deterministic and conforms to the ABCI interface specification. So when we run abci-cli info, we open a new connection to the ABCI server, which calls the Info() method on the application, which tells us the number of transactions in our Merkle tree. Now, since every command opens a new connection, we provide the abci-cli console and abci-cli batch commands to allow multiple ABCI messages to be sent over a single connection. Running abci-cli console should drop you into an interactive console for speaking ABCI messages to your application. Try running these commands:
> echo hello
-> code: OK
-> data: hello
-> data.hex: 0x68656C6C6F

> info
-> code: OK
-> data: {"size":0}
-> data.hex: 0x7B2273697A65223A307D

> prepare_proposal "abc=123"
-> code: OK
-> log: Succeeded. Tx: abc=123

> process_proposal "abc==456"
-> code: OK
-> status: REJECT

> process_proposal "abc=123"
-> code: OK
-> status: ACCEPT

> finalize_block "abc=123"
-> code: OK
-> code: OK
-> data.hex: 0x0200000000000000

> commit
-> code: OK

> info
-> code: OK
-> data: {"size":1}
-> data.hex: 0x7B2273697A65223A317D

> query "abc"
-> code: OK
-> log: exists
-> height: 0
-> key: abc
-> key.hex: 616263
-> value: 123
-> value.hex: 313233

> finalize_block "def=xyz" "ghi=123"
-> code: OK
-> code: OK
-> code: OK
-> data.hex: 0x0600000000000000

> commit
-> code: OK

> query "def"
-> code: OK
-> log: exists
-> height: 0
-> key: def
-> key.hex: 646566
-> value: xyz
-> value.hex: 78797A
Note that if we do finalize_block "abc" ... it will store (abc, abc), but if we do finalize_block "abc=efg" ... it will store (abc, efg). You could put the commands in a file and run abci-cli --verbose batch < myfile. Note that the abci-cli is designed strictly for testing and debugging. In a real deployment, the role of sending messages is taken by CometBFT, which connects to the app using four separate connections, each with its own pattern of messages. For examples of running an ABCI app with CometBFT, see the getting started guide.

Bounties

Want to write an app in your favorite language?! We’d be happy to add you to our ecosystem! See funding opportunities from the Interchain Foundation for implementations in new languages and more.