CometBFT 会发出不同的事件,你可以通过 WebSocket 订阅这些事件。这对于第三方应用程序(用于分析)或检查状态都很有用。 事件列表 要通过 CLI 使用 websocket 连接到节点,你可以使用诸如 wscat 之类的工具,并运行:
wscat -c ws://127.0.0.1:26657/websocket
注意:如果你的节点 RPC 端点启用了 TLS,请使用 wss 协议而不是 ws。 你可以通过 WebSocket 调用 subscribe RPC 方法,并提供一个有效查询,来订阅上述任意事件。
{
    "jsonrpc": "2.0",
    "method": "subscribe",
    "id": 0,
    "params": {
        "query": "tm.event='NewBlock'"
    }
}
查看API 文档了解有关查询语法和其他选项的更多信息。 你还可以使用标签来查询交易结果,前提是你已在 FinalizeBlock 响应中包含这些标签。详见交易索引。

查询参数与事件类型限制

虽然 CometBFT 对应用程序输出的事件类型没有任何限制,但在查询数值型事件时,仍有几个注意事项需要考虑。
  • 查询会将所有数值型事件值转换为 math/big 提供的 big.Float。整数会被转换为精度等于表示该整数所需位数的浮点数。这样做是为了避免在使用默认精度(64)转换大整数时发生精度损失。
  • 比较两个值时,如果其中任意一个是浮点数,另一个也会被表示为大浮点数。整数同样会被解析为大浮点数,其精度等于表示该整数所需的位数。
  • 与所有浮点比较一样,比较带小数的浮点数可能导致结果不精确。
  • 查询中不能包含负数。
在 v0.38.x 版本之前,查询参数不支持浮点数。

ValidatorSetUpdates

当验证者集合发生变化时,会发布 ValidatorSetUpdates 事件。该事件携带一个公钥/投票权重对列表。这个列表与 CometBFT 从 ABCI 应用程序接收到的内容相同(参见 ABCI 规范中的 EndBlock 部分)。 响应:
{
    "jsonrpc": "2.0",
    "id": 0,
    "result": {
        "query": "tm.event='ValidatorSetUpdates'",
        "data": {
            "type": "tendermint/event/ValidatorSetUpdates",
            "value": {
              "validator_updates": [
                {
                  "address": "09EAD022FD25DE3A02E64B0FE9610B1417183EE4",
                  "pub_key": {
                    "type": "tendermint/PubKeyEd25519",
                    "value": "ww0z4WaZ0Xg+YI10w43wTWbBmM3dpVza4mmSQYsd0ck="
                  },
                  "voting_power": "10",
                  "proposer_priority": "0"
                }
              ]
            }
        }
    }
}

CometBFT emits different events, which you can subscribe to via Websocket. This can be useful for third-party applications (for analysis) or for inspecting state. List of events To connect to a node via websocket from the CLI, you can use a tool such as wscat and run:
wscat -c ws://127.0.0.1:26657/websocket
NOTE: If your node’s RPC endpoint is TLS-enabled, use the scheme wss instead of ws. You can subscribe to any of the events above by calling the subscribe RPC method via Websocket along with a valid query.
{
    "jsonrpc": "2.0",
    "method": "subscribe",
    "id": 0,
    "params": {
        "query": "tm.event='NewBlock'"
    }
}
Check out the API docs for more information on query syntax and other options. You can also use tags, given you have included them in the FinalizeBlock response, to query transaction results. See Indexing transactions for details.

Query parameter and event type restrictions

While CometBFT imposes no restrictions on the application with regard to the type of the event output, there are several considerations that need to be taken into account when querying events with numeric values.
  • Queries convert all numeric event values to big.Float, provided by math/big. Integers are converted into a float with a precision equal to the number of bits needed to represent this integer. This is done to avoid precision loss for big integers when they are converted with the default precision (64).
  • When comparing two values, if either one of them is a float, the other one will be represented as a big float. Integers are again parsed as big floats with a precision equal to the number of bits required to represent them.
  • As with all floating point comparisons, comparing floats with decimal values can lead to imprecise results.
  • Queries cannot include negative numbers.
Prior to version v0.38.x, floats were not supported as query parameters.

ValidatorSetUpdates

When the validator set changes, the ValidatorSetUpdates event is published. The event carries a list of pubkey/power pairs. The list is the same as what CometBFT receives from the ABCI application (see the EndBlock section in the ABCI spec). Response:
{
    "jsonrpc": "2.0",
    "id": 0,
    "result": {
        "query": "tm.event='ValidatorSetUpdates'",
        "data": {
            "type": "tendermint/event/ValidatorSetUpdates",
            "value": {
              "validator_updates": [
                {
                  "address": "09EAD022FD25DE3A02E64B0FE9610B1417183EE4",
                  "pub_key": {
                    "type": "tendermint/PubKeyEd25519",
                    "value": "ww0z4WaZ0Xg+YI10w43wTWbBmM3dpVza4mmSQYsd0ck="
                  },
                  "voting_power": "10",
                  "proposer_priority": "0"
                }
              ]
            }
        }
    }
}