Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions @worldsibu/convector-core-chaincode/src/chaincode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Chaincode as CC, StubHelper, ChaincodeError } from '@theledger/fabric-c
import {
BaseStorage,
getInvokables,
BaseStorageNamespace,
ChaincodeInitializationError,
ChaincodeInvokationError,
ConfigurationInvalidError,
Expand Down Expand Up @@ -54,11 +55,15 @@ export class Chaincode extends CC {
* it first calls this function
*/
public async Invoke(stub: ChaincodeStub): Promise<ChaincodeResponse> {
BaseStorage.current = new StubStorage(stub);

try {
await this.initControllers(new StubHelper(stub), [, 'true']);
const invokeRes = await super.Invoke(stub);
let invokeRes;

await BaseStorageNamespace.runAndReturn(async () => {
BaseStorage.current = new StubStorage(stub);
await this.initControllers(new StubHelper(stub), [, 'true']);
invokeRes = await super.Invoke(stub);
});

if (invokeRes.status === 500) {
const err = (invokeRes.message as any) instanceof Buffer ?
invokeRes.message.toString() : JSON.stringify(invokeRes.message);
Expand Down
2 changes: 2 additions & 0 deletions @worldsibu/convector-core-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"docs:serve": "http-server docs"
},
"dependencies": {
"@types/cls-hooked": "^4.3.1",
"@worldsibu/convector-core-errors": "^1.3.8",
"cls-hooked": "git+https://github.com/bringg/cls-hooked.git",
"tslib": "^1.9.0",
"window-or-global": "^1.0.1"
},
Expand Down
13 changes: 10 additions & 3 deletions @worldsibu/convector-core-storage/src/base-storage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/** @module @worldsibu/convector-core-storage */

import * as g from 'window-or-global';
import { createNamespace, Namespace } from 'cls-hooked';

export const BaseStorageNamespace: Namespace = createNamespace('@worldsibu/convector-core-storage');

export abstract class BaseStorage {
private static _currentStorage: BaseStorage;
/**
* Current storage implementation
*/
public static get current(): BaseStorage {
return g.ConvectorBaseStorageCurrent;
return BaseStorageNamespace.active ? BaseStorageNamespace.get('current') : this._currentStorage;
}
public static set current(storage: BaseStorage) {
g.ConvectorBaseStorageCurrent = storage;
if (BaseStorageNamespace.active) {
BaseStorageNamespace.set('current', storage);
} else {
this._currentStorage = storage;
}
}

public async abstract get(id: string, storageOptions?: any): Promise<any>;
Expand Down
31 changes: 27 additions & 4 deletions examples/token/tests/token.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,29 @@ describe('Token e2e', () => {
txTimeout: 300000,
user: 'admin',
channel: 'ch1',
chaincode: 'token',
chaincode: 'token1',
keyStore,
networkProfile,
userMspPath,
// userMsp: 'org1MSP'
});
tokenCtrl = ClientFactory(TokenController, adapter);

(adapter as ClientHelper).cleanUp();

BaseStorage.current = new CouchDBStorage({
host: 'localhost',
protocol: 'http',
port: '5084'
}, 'ch1_token');
}, 'ch1_token1');

await adapter.init();

certificate = await tokenCtrl.whoAmI();
});

after(() => {
(adapter as ClientHelper).close();
});

it('should initialize the token', async () => {
await tokenCtrl.init(new Token({
id: tokenId,
Expand Down Expand Up @@ -112,4 +114,25 @@ describe('Token e2e', () => {

expect(response.txId).to.exist;
});

it('should preserve the context in parallel invokes', async () => {
await Array(10).fill('').reduce(async (result, _, n) => {
await result;
console.log(`Processing batch ${n+1}/10`);
await Promise.all(Array(10).fill('').map(async (_, m) => {
console.log(`Processing item ${m+1}/10`);
await tokenCtrl.init(new Token({
id: uuid(),
name: 'Token',
symbol: 'TKN',
totalSupply: totalSupply,
balances: { [certificate]: totalSupply },
complex: {
name: 'Test',
value: 5
}
}));
}));
});
});
});
Loading