首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在hyperledger-fabric中生成事件吗?

我可以在hyperledger-fabric中生成事件吗?
EN

Stack Overflow用户
提问于 2017-12-26 12:54:50
回答 2查看 999关注 0票数 3

我之前在Ethereum上工作过,它可以在函数被调用或状态改变时生成“事件”,这些事件可以通过观察状态变化从应用程序中捕获。Hyperledger有这个功能吗?我可以在hyperledger-fabric中看到“事件”,但是当状态发生变化时,我如何生成自己的事件并在节点应用程序中捕获它们?

EN

回答 2

Stack Overflow用户

发布于 2017-12-26 18:38:19

在Hyperledger Fabric中,有一个名为的shim.ChaincodeStubInterface应用编程接口方法:

代码语言:javascript
复制
// SetEvent allows the chaincode to set an event on the response to the
// proposal to be included as part of a transaction. The event will be
// available within the transaction in the committed block regardless of the
// validity of the transaction.
SetEvent(name string, payload []byte) error

它允许您在调用链代码(也称为smartcontract)期间指定事件。稍后,您可以注册到event hub以获取这些事件。

票数 4
EN

Stack Overflow用户

发布于 2018-07-17 20:16:23

您可以在链代码中设置事件,如以下代码所示:

APIstub.SetEvent("evtsender", []byte("abcdef"))

然后,在您用来执行查询的文件中,您应该使用registerChaincodeEvent();请参阅以下官方文档:

https://fabric-sdk-node.github.io/EventHub.html#registerChaincodeEvent__anchor

这可能是一个示例:

代码语言:javascript
复制
let event_monitor = new Promise((resolve, reject) => {
        let regid = null;
        let handle = setTimeout(() => {
            if (regid) {
                // might need to do the clean up this listener
                eh.unregisterChaincodeEvent(regid);
                console.log('Timeout - Failed to receive the chaincode event');
            }
            reject(new Error('Timed out waiting for chaincode event'));
        }, 20000);
        eh.connect();
        regid = eh.registerChaincodeEvent('fabcar', 'evtsender',
            (event, block_num, txnid, status) => {
                // This callback will be called when there is a chaincode event name
                // within a block that will match on the second parameter in the registration
                // from the chaincode with the ID of the first parameter.
                console.log('Successfully got a chaincode event with transid:' + txnid + ' with status:' + status);

                // might be good to store the block number to be able to resume if offline
                storeBlockNumForLater(block_num);

                // to see the event payload, the channel_event_hub must be connected(true)
                let event_payload = event.payload.toString('utf8');
                if (event_payload.indexOf('CHAINCODE') > -1) {
                    clearTimeout(handle);
                    // Chaincode event listeners are meant to run continuously
                    // Therefore the default to automatically unregister is false
                    // So in this case we want to shutdown the event listener once
                    // we see the event with the correct payload
                    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) => {
                clearTimeout(handle);


                   console.log('Failed to receive the chaincode event ::' + error);
                    reject(error);
                }
                // no options specified
                // startBlock will default to latest
                // endBlock will default to MAX
                // unregister will default to false
                // disconnect will default to false
            );
})

无论如何,我在测试它时遇到了一些问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47974012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档