1

I would appreciate some help with this small issue. According to the comercial paper smart contract, present in Fabric-samples, one can define a custom Context, which allows handling logic across different transactions. Can we define a variable in a Context, initialized as 0, and increment it at each transaction? I do not seem able to increment it, i.e., the counter always resets at each transation:

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();

I have a test transaction which increments the counter:

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);
}

On the first time I execute the incrementC transaction, I obtain 0, 1, 1, as expected. On the following times, I obtain exacly the same, as if context did not store the updates.

Any insights?

raf
  • 42
  • 11

1 Answers1

1

A Custom context has the same scope as a non custom context, it is the context for the currently executing transaction only. It is not able to span across multiple transaction requests. If the documentation implies this then I would suggest that there is something wrong with the documentation and a jira should be raised at https://jira.hyperledger.org to raise this as an issue. So unfortunately what you are trying to do will not work.

david_k
  • 5,843
  • 2
  • 9
  • 16