# 订阅

Events 是包含有关应用程序执行信息的对象。他们是主要由区块浏览器和钱包等服务提供商用于跟踪各种执行情况消息和索引事务。

# 先决条件

# SDK 和 Tendermint 事件

可以通过 Tendermint 的 Websocket (opens new window) 订阅events )。 这是通过 Websocket 调用 subscribe RPC 方法来完成的:

{
    "jsonrpc": "2.0",
    "method": "subscribe",
    "id": "0",
    "params": {
        "query": "tm.event='eventCategory' AND eventType.eventAttribute='attributeValue'"
    }
}

您可以订阅的主要 eventCategory 是:

  • NewBlock:包含在BeginBlock和EndBlock期间触发的events。
  • Tx:包含在DeliverTx(即事务处理)期间触发的events。
  • ValidatorSetUpdates:包含块的验证器集更新。

这些事件是在提交块后从state包中触发的。你可以获得完整的events类别列表。[这里](https://godoc.org/github.com/tendermint/tendermint/types#pkg-constants)。

query 的 type 和 attribute 值允许您过滤您所在的特定 event 寻找。例如,MsgEthereumTx交易会触发ethermint类型的event,并且 具有sender和recipient作为attributes。订阅此event将按如下方式完成:

{
    "jsonrpc": "2.0",
    "method": "subscribe",
    "id": "0",
    "params": {
        "query": "tm.event='Tx' AND ethereum.recipient='hexAddress'"
    }
}

其中“hexAddress”是一个EIP-55地址(例如:“0x1122334455667788990011223344556677889900”)。

# Tendermint Websocket

要开始与 Tendermint websocket 的连接,您需要使用 --rpc.laddr 定义地址 启动节点时的标志(默认tcp://127.0.0.1:26657):

plugchaind start --rpc.laddr="tcp://127.0.0.1:26657"

然后,使用 ws (opens new window) 开始一个 websocket 订阅

# connect to tendermint websocket at port 8080 as defined above
ws ws://localhost:8080/websocket

# subscribe to new Tendermint block headers
> { "jsonrpc": "2.0", "method": "subscribe", "params": ["tm.event='NewBlockHeader'"], "id": 1 }

# Tendermint 事件列表

您可以订阅的主要事件是:

  • NewBlock:包含在BeginBlock和EndBlock期间触发的events。
  • Tx:包含在DeliverTx(即事务处理)期间触发的events。
  • ValidatorSetUpdates:包含块的验证器集更新。

所有 Tendermint 事件键的列表:

活动类型 分类
订阅特定事件 "tm.event" 块
订阅特定交易 "tx.hash" 块
订阅特定区块高度的交易 "tx.height" 块
索引 BeginBlock 和 Endblock 事件 "block.height" 块
订阅 ABCI BeginBlock 事件 "begin_block" 块
订阅 ABCI EndBlock 事件 "end_block" 共识

以下是可用于订阅 tm.event 类型的值列表:

事件价值 分类
新区块 "NewBlock" block
新区块头 "NewBlockHeader" block
新拜占庭证据 "NewEvidence" block
新交易 "Tx" block
验证器集已更新 "ValidatorSetUpdates" block
块同步状态 "BlockSyncStatus" consensus
锁 "Lock" consensus
新一轮共识 "NewRound" consensus
波尔卡 "Polka" consensus
重新锁定 "Relock" consensus
状态同步状态 "StateSyncStatus" consensus
超时建议 "TimeoutPropose" consensus
超时等待 "TimeoutWait" consensus
解锁 "Unlock" consensus
块有效 "ValidBlock" consensus
共识投票 "Vote" consensus

# Ethereum JSON-RPC Events

Plug Chain 还支持 Ethereum JSON-RPC (opens new window) 过滤器调用订阅 状态日志 (opens new window),区块 (opens new window) 或 [待定交易](https://eth.wiki/json-rpc/API#eth_newpendingtransactionfilter)更改。

在底层,它使用 Tendermint RPC 客户端的事件系统来处理订阅 然后格式化为与以太坊兼容的事件。

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_newBlockFilter","params":[],"id":1}' -H "Content-Type: application/json" http://localhost:8545

{"jsonrpc":"2.0","id":1,"result":"0x3503de5f0c766c68f78a03a3b05036a5"}

然后您可以使用 eth_getFilterChanges (opens new window) 调用检查状态是否发生变化:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getFilterChanges","params":["0x3503de5f0c766c68f78a03a3b05036a5"],"id":1}' -H "Content-Type: application/json" http://localhost:8545

{"jsonrpc":"2.0","id":1,"result":["0x7d44dceff05d5963b5bc81df7e9f79b27e777b0a03a6feca09f3447b99c6fa71","0x3961e4050c27ce0145d375255b3cb829a5b4e795ac475c05a219b3733723d376","0xd7a497f95167d63e6feca70f344d9f6e843d097b62729b8f43bdcd5febf142ab","0x55d80a4ba6ef54f2a8c0b99589d017b810ed13a1fda6a111e1b87725bc8ceb0e","0x9e8b92c17280dd05f2562af6eea3285181c562ebf41fc758527d4c30364bcbc4","0x7353a4b9d6b35c9eafeccaf9722dd293c46ae2ffd4093b2367165c3620a0c7c9","0x026d91bda61c8789c59632c349b38fd7e7557e6b598b94879654a644cfa75f30","0x73e3245d4ddc3bba48fa67633f9993c6e11728a36401fa1206437f8be94ef1d3"]}

# Ethereum Websocket

由于 Plug Chain 运行使用 Tendermint Core 作为它的共识引擎,并且它是使用 Cosmos 构建的 SDK 框架,它继承了它们的事件格式。但是为了支持原生的Web3 Ethereum's websockets的兼容性 PubSubAPI (opens new window),Plug Chain 需要转换 Tendermint 检索到以太坊类型的响应。

您可以在启动时使用 --json-rpc.ws-address 标志启动与以太坊 websocket 的连接 节点(默认“0.0.0.0:8546”):

plugchaind start  --json-rpc.address"0.0.0.0:8545" --json-rpc.ws-address="0.0.0.0:8546" --evm.rpc.api="eth,web3,net,txpool,debug" --json-rpc.enable

然后,使用 ws (opens new window) 开始一个 websocket 订阅

# connect to tendermint websocet at port 8546 as defined above
ws ws://localhost:8546/

# subscribe to new Ethereum-formatted block Headers
> {"id": 1, "method": "eth_subscribe", "params": ["newHeads", {}]}
< {"jsonrpc":"2.0","result":"0x44e010cb2c3161e9c02207ff172166ef","id":1}