How do I notify the client application when a chaincode is invoked?

前提是你 提交于 2021-02-10 07:53:22

问题


When a chaincode is invoked, is there a way to call a REST API (external) so that the client application can be notified on the new transaction.

Apart from REST, is there any other option?


回答1:


It's better to use events https://github.com/hyperledger/fabric/blob/master/docs/protocol-spec.md#35-events

Validating peers and chaincodes can emit events on the network that applications may listen for and take actions on. There is a set of pre-defined events, and chaincodes can generate custom events. Events are consumed by 1 or more event adapters. Adapters may further deliver events using other vehicles such as Web hooks or Kafka.

Application can subscribe for events stream from Fabric and listen for messages generate by your chaincode.

An example for how to work with Events can be found here: https://github.com/hyperledger/fabric/tree/master/examples/events/block-listener




回答2:


To add to Sergey's answer, there are 3 types of events.

  1. BLOCK EVENTs, which are created when the ledger changes.
  2. REJECTION EVENTs, which are created when any error occur( either in user chain code or in system chain code )
  3. CHAINCODE EVENTs, which are user handles which lets user chain code create events. [ Weird thing I noticed is, only one CHAINCODE EVENT per invoke is allowed as per current design ]

You can have an event listener/client running at your end, listening on the gRPC port, ( you can get the port from the core.yaml file ) Or you can even refer to the example Sergey has mentioned.

In your case, I am guessing that you are looking for a successful transaction. In that case, you should listen on BLOCK events and REJECTION Events. The Transaction UUID which you received when your invoke was triggered, can be used to scan the events and trigger an action when it matches. Also note that if a transaction results in REJECTION EVENT, then it would not have a BLOCK EVENT. Hope this helps.



来源:https://stackoverflow.com/questions/40769741/how-do-i-notify-the-client-application-when-a-chaincode-is-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!