Tendermint RPC API文档

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

Subscribe - 订阅事件通知

Subscribe接口用来通过WebSocket订阅区块链事件通知。需要提供 一个查询来声明感兴趣的事件。查询字符串具有如下格式:

condition AND condition ...

目前不支持OR操作。

condition的格式如下:

key operation operand

其中,

  • key:键,一个字符串,不能包含\t\n\r()"'=><
  • operation: 操作符,可以是 "=", "<", "<=", ">", ">=", "CONTAINS"
  • operand:操作数,可以是字符串、数值、日期或时间

例如:

tm.event = 'NewBlock' # new blocks 
tm.event = 'CompleteProposal' # node got a complete proposal 
tm.event = 'Tx' AND tx.hash = 'XYZ' # single transaction 
tm.event = 'Tx' AND tx.height = 5 # all txs of the fifth block 
tx.height = 5 # all txs of the fifth block

Tendermint提供了一些预定义的key:

  • tm.event
  • tx.hash
  • tx.height

对于交易而言,你可以用tags定义额外的键:

DeliverTx{ Tags: []*KVPair{ "agent.name": "K", } }

tm.event = 'Tx' AND agent.name = 'K' 
tm.event = 'Tx' AND account.created_at >= TIME 2013-05-03T14:45:00Z 
tm.event = 'Tx' AND contract.sign_date = DATE 2017-01-01 
tm.event = 'Tx' AND account.owner CONTAINS 'Igor'

API请求

仅用于websocket

参数:

  • query:查询字符串

API响应

返回JSON对象,结构如下:

  • jsonrpc:JSONRPC版本,固定为"2.0"
  • id:API请求的ID编号
  • error:错误描述信息
  • result:结果空对象

示例代码

使用go订阅事件通知:

import "github.com/tendermint/tendermint/libs/pubsub/query"
import "github.com/tendermint/tendermint/types"

client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
query := query.MustParse("tm.event = 'Tx' AND tx.height = 3")
txs := make(chan interface{})
err := client.Subscribe(ctx, "test-client", query, txs)

go func() {


      for e := range txs {
        fmt.Println("got ", e.(types.EventDataTx))
         }

}()

响应结果:

{
"jsonrpc": "2.0",
"id": "",
"result": {
    "node_info": {
            "protocol_version": {
                "p2p": "4",
                "block": "7",
                "app": "0"
            },
            "id": "53729852020041b956e86685e24394e0bee4373f",
            "listen_addr": "10.0.2.15:26656",
            "network": "test-chain-Y1OHx6",
            "version": "0.24.0-2ce1abc2",
            "channels": "4020212223303800",
            "moniker": "ubuntu-xenial",
            "other": {
                "tx_index": "on",
                "rpc_addr": "tcp://0.0.0.0:26657"
            }
        },
        "sync_info": {
            "latest_block_hash": "F51538DA498299F4C57AC8162AAFA0254CE08286",
            "latest_app_hash": "0000000000000000",
            "latest_block_height": "18",
            "latest_block_time": "2018-09-17T11:42:19.149920551Z",
            "catching_up": false
        },
        "validator_info": {
            "address": "D9F56456D7C5793815D0E9AF07C3A355D0FC64FD",
            "pub_key": {
                "type": "tendermint/PubKeyEd25519",
                "value": "wVxKNtEsJmR4vvh651LrVoRguPs+6yJJ9Bz174gw9DM="
            },
            "voting_power": "10"
        }
    }
}