Hyperledger Fabric 1.4 : How to test and verify registerChaincodeEvent function from the Fabric Node SDK?

后端 未结 2 928
甜味超标
甜味超标 2021-01-03 15:27

I am currently emitting an event from my chaincode when adding an asset.

async addRequestNode(ctx, sampleAssetId, sampleData) {
    //console.info(\'========         


        
相关标签:
2条回答
  • 2021-01-03 15:42

    You need to convert payload data into bytes ex.

    async addRequestNode(ctx, sampleAssetId, sampleData) {
       await ctx.stub.putState(sampleAssetId,sampleData);
       ctx.stub.setEvent('sampleAssetAdded', Buffer.from('payload Data'));
    }
    

    and in client sdk you need to convert into string

    await contract.addContractListener('sampleAssetAdded', 'sampleAssetAdded', (err, event, blkNum, txid, status, options) => {
      console.log('event received', status, event, blkNum, txid);  
      if (err) {
         this.emit('error', err);
      } else if (status && status === 'VALID') {
         console.log("payload ",event.payload.toString());
      }
    

    }

    0 讨论(0)
  • 2021-01-03 15:42

    You shouldn't be instantiating a ChannelEventHub directly as it requires a user context in order to connect. You should get a channel event hub from the channel object using either

    channel.getChannelEventHub(peer)
    

    or

    channel.newChannelEventHub(peer)
    

    depending on whether you want the event hub cached in the channel object or not. Suggest reading the at https://fabric-sdk-node.github.io for each of these apis to explain more.

    Next you need to connect it and specify true for full blocks rather than filtered blocks. Also you need to wait for the eventhub to be connected before you try committing a transaction that emits an event otherwise you won't receive it.

    eventHub.connect(true, (err, eventHub) => {
       if (err) {
          // Error connecting
       } else {
          // connected successfully
       });
    

    Once connected you then register for chaincode events.

    0 讨论(0)
提交回复
热议问题