监听网络事件

用户可能注意到,发往网络的请求是异步处理模式,这就意味着客户端无法获知提交的交易是否最终接受。Fabric 在 Peer 节点上提供了事件 gRPC 服务,用户可以通过客户端来监听。

下面通过 eventsclient 工具来监听网络中的事件。

首先通过如下命令安装 eventsclient 工具。

  1. $ cd $GOPATH/src/hyperledger/fabric/examples/events/eventsclient
  2. $ go install && go clean

该工具自动封装对 Peer 事件的 gRPC 请求,支持的选项主要包括如下几个:

  • -server “localhost:7053”:监听服务地址,一般指定为 Peer 节点的 7053 端口;
  • -channelID string:监听指定通道信息,默认为 testchainid;
  • -seek int:指定从哪个区块开始监听。-2代表从初始区块(默认),-1代表从当前最新区块;
  • -filtered=true:只获取过滤的区块内容,不显示完整内容。
  • -quiet:不打印区块内容,只显示区块号;
  • -tls:是否启用 TLS,默认关闭;
  • -rootCert string:启用 TLS 时指定信任的根 CA 证书路径;
  • -mTls:是否开启双向验证(即服务端也同时验证客户端身份),默认关闭。
  • -clientCert string::启用 TLS 时候客户端证书路径;
  • -clientKey string:启用 TLS 时候客户端私钥路径;

典型地,用户可以通过环境变量指定所需的参数值,使用如下命令启动监听。

  1. $ eventsclient \
  2. -server=${PEER_URL} \
  3. -channelID=${APP_CHANNEL} \
  4. -filtered=true \
  5. -tls=true \
  6. -clientKey=${TLS_CLIENT_KEY} \
  7. -clientCert=${TLS_CLIENT_CERT} \
  8. -rootCert=${TLS_CA_CERT}

启动后,该工具会持续监听来自指定通道的事件,并打印出来。

例如,监听 businesschannel 通道内区块信息,并对结果进行过滤输出,命令和结果如下所示。

  1. $ CORE_PEER_LOCALMSPID=Org1MSP \
  2. CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp \
  3. eventsclient \
  4. -server=peer0.org1.example.com:7051 \
  5. -channelID=businesschannel \
  6. -filtered=true \
  7. -tls=true \
  8. -clientKey=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@Org1.example.com/tls/client.key \
  9. -clientCert=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@Org1.example.com/tls/client.crt \
  10. -rootCert=/etc/hyperledger/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@Org1.example.com/tls/ca.crt
  11. UTC [eventsclient] readEventsStream -> INFO 001 Received filtered block:
  12. {
  13. "channel_id": "businesschannel",
  14. "filtered_transactions": [
  15. {
  16. "tx_validation_code": "VALID",
  17. "txid": "",
  18. "type": "CONFIG"
  19. }
  20. ],
  21. "number": "0"
  22. }
  23. UTC [eventsclient] readEventsStream -> INFO 002 Received filtered block:
  24. {
  25. "channel_id": "businesschannel",
  26. "filtered_transactions": [
  27. {
  28. "tx_validation_code": "VALID",
  29. "txid": "",
  30. "type": "CONFIG"
  31. }
  32. ],
  33. "number": "1"
  34. }
  35. UTC [eventsclient] readEventsStream -> INFO 003 Received filtered block:
  36. {
  37. "channel_id": "businesschannel",
  38. "filtered_transactions": [
  39. {
  40. "tx_validation_code": "VALID",
  41. "txid": "",
  42. "type": "CONFIG"
  43. }
  44. ],
  45. "number": "2"
  46. }
  47. UTC [eventsclient] readEventsStream -> INFO 004 Received filtered block:
  48. {
  49. "channel_id": "businesschannel",
  50. "filtered_transactions": [
  51. {
  52. "transaction_actions": {
  53. "chaincode_actions": []
  54. },
  55. "tx_validation_code": "VALID",
  56. "txid": "2832892094f612237b06950b77a6afc13ca9226176e99c2a8577cf4be2074c0a",
  57. "type": "ENDORSER_TRANSACTION"
  58. }
  59. ],
  60. "number": "3"
  61. }
  62. UTC [eventsclient] readEventsStream -> INFO 005 Received filtered block:
  63. {
  64. "channel_id": "businesschannel",
  65. "filtered_transactions": [
  66. {
  67. "transaction_actions": {
  68. "chaincode_actions": []
  69. },
  70. "tx_validation_code": "VALID",
  71. "txid": "fec547335060bb324e8e4a08067c7fa24092e1295cb62dffb14a93bc77b2fbcf",
  72. "type": "ENDORSER_TRANSACTION"
  73. }
  74. ],
  75. "number": "4"
  76. }
  77. ...