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

那年仲夏 提交于 2019-12-12 07:24:18

问题


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

async addRequestNode(ctx, sampleAssetId, sampleData) {
    //console.info('============= Adding Sample Asset ===========');

    await ctx.stub.putState(sampleAssetId,sampleData);
    //console.info('============= Sample Asset Added ===========');
    ctx.stub.setEvent('sampleAssetAdded', 'sampleData');
}

Here as you can see, I am emiiting an event 'sampleAssetAdded'. I want to capture this event on my client application.

I have a server setup and is listening at port 8080. In the server I have instantiated the channelEventHub and have given my chaincode code ID and the event name.

    const channelEventHub = new ChannelEventHub('mychannel','peer0.org1.example.com');
    let eventCapture = channelEventHub.registerChaincodeEvent('fabcar','sampleAssetAdded',(event, block_num, txnid, status) => {



        console.log('Successfully got a chaincode event with transid:'+ txnid + ' with status:'+status);


        storeBlockNumForLater(block_num);


        let event_payload = event.payload.toString('utf8');
        if(event_payload.indexOf('CHAINCODE') > -1) {
            clearTimeout(handle);

            channel_event_hub.unregisterChaincodeEvent(regid);
            console.log('Successfully received the chaincode event on block number '+ block_num);
            resolve('RECEIVED');
        } else {
            console.log('Successfully got chaincode event ... just not the one we are looking for on block number '+ block_num);
        }
    },(error) => {

        console.log('Got Some Error'+error);

    })

But When I add the asset, the event is not captured. I don't know what is going wrong.

I even console logged eventCapture and got this

Event Capture ----> ChaincodeRegistration {
  ccid: 'fabcar',
  eventNameFilter: /sampleAssetAdded/,
  event_reg: 
   EventRegistration {
     _onEventFn: [Function],
     _onErrorFn: [Function],
     unregister: false,
     disconnect: false,
     unregister_action: [Function] } }

I am not sure if it's actually registering to the event or not.

I am using the Node SDK and I have referred this site https://fabric-sdk-node.github.io/release-1.4/tutorial-channel-events.html, but it is not at all helpful as they don't have the complete code and instead have vague snippets with incorrect or not working code.

Can someone please help on how to set this entire thing up and test it too.


回答1:


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.




回答2:


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());
  }

}



来源:https://stackoverflow.com/questions/55062756/hyperledger-fabric-1-4-how-to-test-and-verify-registerchaincodeevent-function

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