关于 Hyperledger Fabric 上下文
On Hyperledger Fabric Context
对于这个小问题,我将不胜感激。
根据 Fabric-samples 中的 comercial paper smart contract,可以定义一个自定义 Context,它允许处理不同交易的逻辑。
我们可以在 Context 中定义一个变量,初始化为 0,并在每次交易时递增它吗?我似乎无法增加它,即计数器总是在每次交易时重置:
class CustomContext extends Context {
constructor() {
super();
this.comercialPaperList = new PaperList(this);
this.counter = 0;
}
generateLogId() {
return this.numberLogs++;
}
getLatestLogId() {
return this.numberLogs;
}
}
class MyContract extends Contract {
constructor() {
// Unique namespace when multiple contracts per chaincode file
super('...');
}
/**
* Define a custom context for a citius log
*/
createContext() {
return new CustomContext();
我有一个增加计数器的测试交易:
async incrementC (ctx) {
let before = await ctx.getLatestLogId();
console.log(before);
let new = await ctx.generateLogId();
console.log(new);
console.log("============== after inc")
let after = await ctx.getLatestLogId();
console.log(after);
}
第一次执行incrementC事务时,我得到了0, 1, 1,如我所料。
在接下来的时间里,我得到的结果完全一样,就好像上下文没有存储更新一样。
有什么见解吗?
自定义上下文与非自定义上下文具有相同的范围,它只是当前正在执行的事务的上下文。它无法跨越多个事务请求。如果文档暗示了这一点,那么我会建议文档有问题,应该在 https://jira.hyperledger.org 提出一个 jira 来提出这个问题。
很遗憾,您尝试做的是行不通的。
对于这个小问题,我将不胜感激。 根据 Fabric-samples 中的 comercial paper smart contract,可以定义一个自定义 Context,它允许处理不同交易的逻辑。 我们可以在 Context 中定义一个变量,初始化为 0,并在每次交易时递增它吗?我似乎无法增加它,即计数器总是在每次交易时重置:
class CustomContext extends Context {
constructor() {
super();
this.comercialPaperList = new PaperList(this);
this.counter = 0;
}
generateLogId() {
return this.numberLogs++;
}
getLatestLogId() {
return this.numberLogs;
}
}
class MyContract extends Contract {
constructor() {
// Unique namespace when multiple contracts per chaincode file
super('...');
}
/**
* Define a custom context for a citius log
*/
createContext() {
return new CustomContext();
我有一个增加计数器的测试交易:
async incrementC (ctx) {
let before = await ctx.getLatestLogId();
console.log(before);
let new = await ctx.generateLogId();
console.log(new);
console.log("============== after inc")
let after = await ctx.getLatestLogId();
console.log(after);
}
第一次执行incrementC事务时,我得到了0, 1, 1,如我所料。 在接下来的时间里,我得到的结果完全一样,就好像上下文没有存储更新一样。
有什么见解吗?
自定义上下文与非自定义上下文具有相同的范围,它只是当前正在执行的事务的上下文。它无法跨越多个事务请求。如果文档暗示了这一点,那么我会建议文档有问题,应该在 https://jira.hyperledger.org 提出一个 jira 来提出这个问题。 很遗憾,您尝试做的是行不通的。