Blockchain 我可以在hyperledger结构中生成事件吗?
我以前在以太坊工作过,它可以在调用函数或状态更改时生成“事件”,并且可以通过观察状态更改从应用程序捕获这些事件。 Hyperledger是否具有该功能?Blockchain 我可以在hyperledger结构中生成事件吗?,blockchain,hyperledger-fabric,hyperledger,Blockchain,Hyperledger Fabric,Hyperledger,我以前在以太坊工作过,它可以在调用函数或状态更改时生成“事件”,并且可以通过观察状态更改从应用程序捕获这些事件。 Hyperledger是否具有该功能? 我可以在hyperledger结构中看到“事件”,但当状态发生更改时,如何生成自己的事件并在节点应用程序中捕获它们?在hyperledger结构中有一个垫片。ChaincodeStubInterfaceAPI方法名为: // SetEvent allows the chaincode to set an event on the respons
我可以在hyperledger结构中看到“事件”,但当状态发生更改时,如何生成自己的事件并在节点应用程序中捕获它们?在hyperledger结构中有一个
垫片。ChaincodeStubInterface
API方法名为:
// 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
它允许您在调用chaincode(也称为smartcontract)期间指定事件。稍后,您可以注册到事件中心以获取这些事件。您可以在链码中设置事件,如以下代码所示:
APIstub.SetEvent(“evtsender”,[]字节(“abcdef”))
然后在用于执行查询的文件中,应使用注册表ChaincodeEvent();见以下正式文件:
这可以是一个例子:
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
);
})
无论如何,我在测试它时遇到了一些问题 那你怎么能赶上这场比赛呢?我尝试了registerChaincodeEvent()方法,但似乎效果不好。。。