Hyperledger Fabric 1.4:如何从Fabric Node SDK测试和验证registerChaincodeEvent功能?

问题描述 投票:1回答:1

我正在添加资产时从我的链代码中发出一个事件。

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

在这里,你可以看到,我发出了一个事件'样本资产添加'。我想在我的客户端应用程序上捕获此事件。

我有一个服务器设置,正在侦听端口8080.在服务器中,我已经实例化了channelEventHub,并给出了我的链代码ID和事件名称。

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

    })

但是当我添加资产时,事件不会被捕获。我不知道出了什么问题。

我甚至控制了记录的eventCapture并得到了这个

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

我不确定它是否实际注册到该事件。

我正在使用Node SDK并且我已经引用了这个网站https://fabric-sdk-node.github.io/release-1.4/tutorial-channel-events.html,但它没有任何帮助,因为他们没有完整的代码,而是含有不正确或不正常工作代码的模糊片段。

有人可以请求帮助如何设置这整个事情并测试它。

node.js hyperledger-fabric blockchain
1个回答
2
投票

您不应该直接实例化ChannelEventHub,因为它需要用户上下文才能连接。您应该使用其中一个从通道对象获取通道事件中心

channel.getChannelEventHub(peer)

要么

channel.newChannelEventHub(peer)

取决于您是否希望事件中心缓存在通道对象中。建议阅读https://fabric-sdk-node.github.io中的每一个api来解释更多。

接下来,您需要连接它并为完整块而不是过滤块指定true。此外,在尝试提交发出事件的事务之前,您需要等待eventhub连接,否则您将无法接收它。

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

连接后,您可以注册链码事件。

© www.soinside.com 2019 - 2024. All rights reserved.