Hyperledger Fabric registerChaincodeEvent() 方法未从链码获取事件
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《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学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
使用 cgo 构建时如何调试/转储 Go 变量?
- 上一篇
- 使用 cgo 构建时如何调试/转储 Go 变量?
- 下一篇
- WIN8查看本机端口的操作步骤
-
- Golang · Go问答 | 6小时前 | sync.Pool · reset · 对象复用 · Go问答 · 线上排查 · Go sync.Pool Go问答 sync.Pool串数据 Reset清理 对象池复用
- Go sync.Pool 为什么会串数据:Reset 和 Put 的顺序怎么定
- 342浏览 收藏
-
- Golang · Go问答 | 9小时前 | [] · []
- Go time.After 写在循环里会泄漏吗:定时器堆积、Stop 和 NewTimer 复用怎么判断
- 181浏览 收藏
-
- Golang · Go问答 | 11小时前 | [] · []
- Go 解析 JSON 用 struct 还是 map[string]any:RawMessage 和严格校验怎么选
- 250浏览 收藏
-
- Golang · Go问答 | 11小时前 | [] · []
- Go 1.22 后 for range 闭包变量还要手动复制吗:循环变量语义变化和迁移检查
- 348浏览 收藏
-
- Golang · Go问答 | 11小时前 | select · 并发控制 · Go问答 · nil channel · select Go 并发控制 channel阻塞 Go问答 nil channel
- Go select 里的 nil channel 有什么用:为什么能动态关闭一个分支
- 467浏览 收藏
-
- Golang · Go问答 | 1天前 | channel · goroutine · pprof · Go问答 · 线上排查 · pprof Go问答 Go channel阻塞 goroutine堆积 channel发送
- Go channel 发送阻塞怎么排查:goroutine 堆积的线上处理手册
- 205浏览 收藏
-
- Golang · Go问答 | 1天前 | 连接池 · database/sql · Go问答 · 线上排查 · DBStats · MaxOpenConns Go数据库连接池 DBStats Go问答 sql.DB WaitCount
- Go 数据库连接池被打满怎么办:sql.DB 等待数上涨的排查手册
- 355浏览 收藏
-
- Golang · Go问答 | 1天前 | EventSource · sse · Go问答 · http.Flusher · 实时推送 · EventSource FLUSH text/event-stream Go问答 http.Flusher Go SSE
- Go 写 SSE 接口为什么浏览器收不到消息:EventSource、Flush 和超时怎么排查
- 333浏览 收藏
-
- Golang · Go问答 | 1天前 | 并发安全 · RWMutex · sync.Map · Go问答 · map并发 · RWMutex sync.Map Go问答 Go map并发读写 concurrent map writes
- Go map 并发读写为什么会 panic:从 fatal error 到三种修法
- 102浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ljg-skills
- ljg-skills 是李继刚开源的 AI 技能与提示词集合,面向大模型使用者整理了一批可复用的 prompt、角色设定和任务技能模板,适合用于学习提示词设计、搭建个人 AI 工作流和沉淀团队常用智能体能力。
- 4381次使用
-
- MELO音乐
- MELO音乐是一站式AI视频与音乐制作助手,对标suno, udio的高品质体验。提供伴奏生成、原创写词、无损导出、哼唱识曲、混音变声等全套音频与短视频编辑工具。无论是流行Kpop、电音说唱、民谣古风、摇滚儿歌还是商用轻音乐,MELO为你免费谱曲,轻松做同款!
- 4062次使用
-
- UniScribe
- UniScribe 是一款 AI 音视频转文字与内容整理工具,支持上传音频、视频文件或粘贴 YouTube 链接,自动生成转写文本、摘要、思维导图和关键问题,并支持多格式导出,适合会议记录、课程学习、访谈整理和内容创作复盘。
- 4042次使用
-
- 剧云
- 剧云是专业中文剧本创作平台,安全稳定运行十余年,集成AI编剧、剧本医生审核、人物小传、剧情关系图、大纲编写、多人协作、Word导入导出、版权管控功能,数据安全防护,轻松高效创作剧本。
- 4226次使用
-
- 万象有声
- 万象有声,一个专为有声创作者打造的新一代智能有声内容创作平台。平台提供专业的智能拆章、智能画本编辑、AI配音、AI生成音效、后期制作、智能对轨、智能审听等有声创作全流程工具,可以帮助创作者高效、低成本创作出引人入胜的有声作品。立即体验,让有声书制作更简单!
- 4195次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 从不同的 go 例程将数据写入同一通道无需等待组即可正常工作
- 2024-04-29 501浏览
-
- Golang rsa-oaep解密失败,前端使用webcrypto
- 2024-04-26 501浏览
-
- 如何从用户输入以惰性方式初始化包的全局变量?
- 2024-04-24 501浏览

