Hyperledger Fabric:如何使用 JS/Node 中的链代码捕获交易错误?
Hyperledger Fabric: How to catch transaction errors with chaincode in JS/Node?
我在基于 JS/Node 的链代码中使用了以下依赖项:
- "fabric-contract-api": "~1.4.0",
- "fabric-shim": "~1.4.0"
查询我的账本。代码类似于:
'use strict';
const { Contract } = require('fabric-contract-api');
const shim = require('fabric-shim');
...
class ThingsChainCode extends Contract {
async queryThing(ctx, id) {
const thingAsBytes = await ctx.stub.getState(id);
if (!thingAsBytes || thingAsBytes.length === 0) {
throw new NotFoundError(`${id} does not exist`);
}
return thingAsBytes.toString('utf8');
}
async updateThing(ctx, id, jsonData) {
await ctx.stub.putState(id, Buffer.from(jsonData));
var succMesg = `${id} updated`;
retVal = shim.success(succMesg);
}
}
不幸的是,有时(当一批中有并发 [=38=] 时)交易被状态验证器标记为无效。原因代码表示 "MVCC_READ_CONFLICT"。此时的问题是 而不是 这些错误的原因是什么。相反,我想捕捉错误。在基于 go 的链代码中,这似乎非常简单:
JS 中没有抛出异常或返回错误。我没有找到任何代码示例是否有进一步的错误处理:https://fabric-shim.github.io/release-1.4/index.html
所以问题是如何使用基于 JS/Node 的链代码捕获交易错误?
非常感谢!
MVCC_READ_CONFLICT 发生在验证时,而不是在您的链代码执行时的提案模拟期间。因此,在这方面,您使用哪种语言编写链码实现并不重要。
要确定交易是否未通过验证并因此被标记为无效,您需要侦听交易事件并检查试图提交的交易的状态。我假设您将在这里使用节点作为客户端语言,并建议使用 fabric-network npm 包,它为您内置了事务事件处理。你什么时候在合同实例上提交交易,如果交易由于说 MVCC_READ_CONFLICT 而未能提交,那么它会抛出一个错误。
建议您查看各种 hyperledger fabric 文档(例如 https://hyperledger-fabric.readthedocs.io/en/release-1.4/developapps/application.html)和使用 fabric-network api(有时称为高级 api)的 hyperledger fabric 示例而不是低水平 api)
我在基于 JS/Node 的链代码中使用了以下依赖项:
- "fabric-contract-api": "~1.4.0",
- "fabric-shim": "~1.4.0"
查询我的账本。代码类似于:
'use strict';
const { Contract } = require('fabric-contract-api');
const shim = require('fabric-shim');
...
class ThingsChainCode extends Contract {
async queryThing(ctx, id) {
const thingAsBytes = await ctx.stub.getState(id);
if (!thingAsBytes || thingAsBytes.length === 0) {
throw new NotFoundError(`${id} does not exist`);
}
return thingAsBytes.toString('utf8');
}
async updateThing(ctx, id, jsonData) {
await ctx.stub.putState(id, Buffer.from(jsonData));
var succMesg = `${id} updated`;
retVal = shim.success(succMesg);
}
}
不幸的是,有时(当一批中有并发 [=38=] 时)交易被状态验证器标记为无效。原因代码表示 "MVCC_READ_CONFLICT"。此时的问题是 而不是 这些错误的原因是什么。相反,我想捕捉错误。在基于 go 的链代码中,这似乎非常简单:
JS 中没有抛出异常或返回错误。我没有找到任何代码示例是否有进一步的错误处理:https://fabric-shim.github.io/release-1.4/index.html
所以问题是如何使用基于 JS/Node 的链代码捕获交易错误?
非常感谢!
MVCC_READ_CONFLICT 发生在验证时,而不是在您的链代码执行时的提案模拟期间。因此,在这方面,您使用哪种语言编写链码实现并不重要。
要确定交易是否未通过验证并因此被标记为无效,您需要侦听交易事件并检查试图提交的交易的状态。我假设您将在这里使用节点作为客户端语言,并建议使用 fabric-network npm 包,它为您内置了事务事件处理。你什么时候在合同实例上提交交易,如果交易由于说 MVCC_READ_CONFLICT 而未能提交,那么它会抛出一个错误。
建议您查看各种 hyperledger fabric 文档(例如 https://hyperledger-fabric.readthedocs.io/en/release-1.4/developapps/application.html)和使用 fabric-network api(有时称为高级 api)的 hyperledger fabric 示例而不是低水平 api)