gpt4 book ai didi

node.js - Hyperledger Fabric registerChaincodeEvent() 方法未从链代码获取事件

转载 作者:IT王子 更新时间:2023-10-29 02:20:38 25 4
gpt4 key购买 nike

在成功部署我的链代码并能够从其数据中执行某些操作后(返回的所有数据都是正确的),我无法检查是否发出了事件。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]);

出现的错误是:

Timeout - Failed to receive the chaincode event (node:9180) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: Timed out waiting for chaincode event (node:9180) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with anon-zero exit code.

因此函数在这部分代码中超时,即使我启动另一个成功执行调用 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 文档以获取更多信息

关于node.js - Hyperledger Fabric registerChaincodeEvent() 方法未从链代码获取事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51363178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com