事件订阅
背景
在3.5版本,Tron开发团队提供了事件订阅机制,因此开发者可以通过事件插件来获取链上触发的事件。
服务
Tron提供了以下方式来获取事件信息。
1、TronGrid封装了事件插件接口,并且提供了公共的、友好的https事件查询接口。
https://cn.developers.tron.network/reference/get-events-by-transaction-id
2、TronWeb也封装了相应的js方法,以获取事件。
3、本地搭建事件插件服务,提供事件查询。
- Tron开发团队发布了两个事件插件kafka和mongodb。
- 支持链上数据的订阅,例如区块,交易,合约、合约日志等。开发者还可以通过设置过滤条件来订阅指定的数据。
- 事件查询服务tron-eventquery,提供在线事件查询服务。
插件
插件的功能是实现事件转储,开发人员可以根据需求定制自己的插件,例如消息队列,Kafka,MongoDB或写入本地文件。
使用事件插件,需要在节点配置文件中将useNativeQueue设置为false,设置如下:
event.subscribe = {
native = {
useNativeQueue = false // if true, use native message queue, else use event plugin.
bindport = 5555 // bind port
sendqueuelength = 1000 //max length of send queue
}
...
}
Tron开发团队提供的插件独立于java-tron,默认情况下不加载。可以通过配置命令行参数来启用。默认情况下,仅支持订阅智能合约事件。开发人员可以通过修改配置文件来订阅其他触发器。还可以灵活地定义插件配置文件,包括消息队列服务器地址,定义的触发器类型等。
事件类型
事件订阅支持四种类型的事件:
1.交易事件
transactionId: 交易哈希
blockHash: 区块哈希
blockNumber: 区块高度
energyUsage: 此次调用中,合约调用者消耗的Energy的总量
energyFee: 此次调用中,合约调用者消耗的Energy中,需要TRX支付的数目(SUN为单位)
originEnergyUsage: 此次调用中,合约开发者消耗的Energy的总量
energyUsageTotal: 此次调用中,合约调用者和合约开发者消耗的Energy的总量
2.区块事件
blockHash: 区块哈希
blockNumber: 区块高度
transactionSize: 区块中包含的交易的数目
latestSolidifiedBlockNumber: 最新的固化块的高度
transactionList: 交易哈希列表
3.合约事件
transactionId: 交易哈希
contractAddress: 合约地址
callerAddress: 合约调用者地址
blockNumber: 合约事件所在的区块高度
blockTimestamp: 区块时间戳
eventSignature: 事件签名
topicMap: the map of topic in solidity language
data: the data information in solidity language
4.合约日志事件
transactionId: 交易哈希
contractAddress: 合约地址
callerAddress: 合约调用者地址
blockNumber: 合约事件所在的区块高度
blockTimestamp: 区块时间戳
contractTopics: the list of topic in solidity language
data: the data information in solidity language
removed: 'true'代表日志已经被移除
合约事件与合约日志事件订阅支持过滤功能:
fromBlock: 起始区块索引
toBlock: 结束区块索引
contractAddress: 合约地址
contractTopics: contract topics list
注意: 不支持历史数据查询
更详细的信息参考下面链接:
https://github.com/tronprotocol/TIPs/issues/12
Updated 2 months ago