登录
首页 >  Golang >  Go问答

Hyperledger Fabric registerChaincodeEvent() 方法未从链码获取事件

来源:stackoverflow

时间:2024-04-28 20:15:33 329浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《Hyperledger Fabric registerChaincodeEvent() 方法未从链码获取事件》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

成功部署我的链码并能够从其数据中执行一些获取(返回的所有数据都是正确的)后,我无法检查是否发出了事件。 invoke() 内部的函数是 queryallmachines() ,如果我只调用这个函数,它会返回一组机器(该组机器插入方法 initledger() 中)

我遵循文档,我的链代码(在 go 中)应该在其 queryallmachines() 函数内的此代码行中发出一个事件:

func (s *testcontract) queryallmachines(apistub shim.chaincodestubinterface) sc.response {
    fmt.println("inside queryallmachines\n")
    ...
    err = apistub.setevent("evtsender", []byte("adadsads"))
    if err != nil {
        return shim.error("event not set")
    }
    fmt.printf("no errors\n")
}

此代码始终打印“无错误”,因此我认为事件已正确发出。

然后在我的 .js 查询文件中我有请求:

let request = {
        chaincodeid: 'oraclize-test-integration',
        fcn: 'queryallmachines',
        args: [''],
        txid: tx_object
    };

然后这是我的 .js 文件中出现问题的部分,我想在其中启动 registerchaincodeevent()

let event_monitor = new promise((resolve, reject) => {
        let regid = null;
        let handle = settimeout(() => {
            if (regid) {
                // might need to do the clean up this listener
                channel_event_hub.unregisterchaincodeevent(regid);
                console.log('timeout - failed to receive the chaincode event');
            }
            reject(new error('timed out waiting for chaincode event'));
        }, 20000);

        regid = channel_event_hub.registerchaincodeevent('oraclize-test-integration', '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
                    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) => {
                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
        );
    });

    // build the promise to send the proposals to the orderer
    let send_trans = channel.sendtransaction({
        proposalresponses: query_responses[0],
        proposal: query_responses[1]
    });

    // now that we have two promises all set to go... execute them
    return promise.all([event_monitor, send_trans]);

出现的错误是:

超时 - 无法接收链码事件(节点:9180) unhandledpromiserejectionwarning:未处理的承诺拒绝 (拒绝 id:3):错误:等待链码事件超时 (节点:9180)[dep0018] deprecationwarning:未处理的承诺拒绝 已弃用。以后承诺拒绝不处理的 将使用非零退出代码终止 node.js 进程。

因此,即使我启动另一个成功执行对 queryallmachines() 函数的调用的另一个 .js 文件,函数也会在这部分代码中超时:

let handle = setTimeout(() => {
            if (regid) {
                // might need to do the clean up this listener
                channel_event_hub.unregisterChaincodeEvent(regid);
                console.log('Timeout - Failed to receive the chaincode event');
            }
            reject(new Error('Timed out waiting for chaincode event'));
        }, 20000);

解决方案


最新的高级 api 让一切变得简单,

const listener = await contract.addContractListener('asset creation-listener', 'saveAsset123456789', (err, event, blockNumber, transactionId, status) => {
            if (err) {
                console.error(err);
                return;
            }
            console.log(`The event payload is :${event.payload}`)
            console.log(`Block Number: ${blockNumber} Transaction ID: ${transactionId} Status: ${status}`);
        })

查看 this 文档以获取更多信息

今天关于《Hyperledger Fabric registerChaincodeEvent() 方法未从链码获取事件》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>