Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit dbda42d

Browse files
committed
fix: preserve context with cls-hooked
Signed-off-by: Diego Barahona <[email protected]>
1 parent 002b8dd commit dbda42d

File tree

6 files changed

+1115
-2065
lines changed

6 files changed

+1115
-2065
lines changed

@worldsibu/convector-core-chaincode/src/chaincode.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Chaincode as CC, StubHelper, ChaincodeError } from '@theledger/fabric-c
66
import {
77
BaseStorage,
88
getInvokables,
9+
BaseStorageNamespace,
910
ChaincodeInitializationError,
1011
ChaincodeInvokationError,
1112
ConfigurationInvalidError,
@@ -54,11 +55,15 @@ export class Chaincode extends CC {
5455
* it first calls this function
5556
*/
5657
public async Invoke(stub: ChaincodeStub): Promise<ChaincodeResponse> {
57-
BaseStorage.current = new StubStorage(stub);
58-
5958
try {
60-
await this.initControllers(new StubHelper(stub), [, 'true']);
61-
const invokeRes = await super.Invoke(stub);
59+
let invokeRes;
60+
61+
await BaseStorageNamespace.runAndReturn(async () => {
62+
BaseStorage.current = new StubStorage(stub);
63+
await this.initControllers(new StubHelper(stub), [, 'true']);
64+
invokeRes = await super.Invoke(stub);
65+
});
66+
6267
if (invokeRes.status === 500) {
6368
const err = (invokeRes.message as any) instanceof Buffer ?
6469
invokeRes.message.toString() : JSON.stringify(invokeRes.message);

@worldsibu/convector-core-storage/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
"docs:serve": "http-server docs"
4141
},
4242
"dependencies": {
43+
"@types/cls-hooked": "^4.3.1",
4344
"@worldsibu/convector-core-errors": "^1.3.8",
45+
"cls-hooked": "git+ssh://[email protected]/bringg/cls-hooked.git",
4446
"tslib": "^1.9.0",
4547
"window-or-global": "^1.0.1"
4648
},

@worldsibu/convector-core-storage/src/base-storage.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
/** @module @worldsibu/convector-core-storage */
22

3-
import * as g from 'window-or-global';
3+
import { createNamespace, Namespace } from 'cls-hooked';
4+
5+
export const BaseStorageNamespace: Namespace = createNamespace('@worldsibu/convector-core-storage');
46

57
export abstract class BaseStorage {
8+
private static _currentStorage: BaseStorage;
69
/**
710
* Current storage implementation
811
*/
912
public static get current(): BaseStorage {
10-
return g.ConvectorBaseStorageCurrent;
13+
return BaseStorageNamespace.active ? BaseStorageNamespace.get('current') : this._currentStorage;
1114
}
1215
public static set current(storage: BaseStorage) {
13-
g.ConvectorBaseStorageCurrent = storage;
16+
if (BaseStorageNamespace.active) {
17+
BaseStorageNamespace.set('current', storage);
18+
} else {
19+
this._currentStorage = storage;
20+
}
1421
}
1522

1623
public async abstract get(id: string, storageOptions?: any): Promise<any>;

examples/token/tests/token.e2e.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,29 @@ describe('Token e2e', () => {
3333
txTimeout: 300000,
3434
user: 'admin',
3535
channel: 'ch1',
36-
chaincode: 'token',
36+
chaincode: 'token1',
3737
keyStore,
3838
networkProfile,
3939
userMspPath,
4040
// userMsp: 'org1MSP'
4141
});
4242
tokenCtrl = ClientFactory(TokenController, adapter);
4343

44-
(adapter as ClientHelper).cleanUp();
45-
4644
BaseStorage.current = new CouchDBStorage({
4745
host: 'localhost',
4846
protocol: 'http',
4947
port: '5084'
50-
}, 'ch1_token');
48+
}, 'ch1_token1');
5149

5250
await adapter.init();
5351

5452
certificate = await tokenCtrl.whoAmI();
5553
});
5654

55+
after(() => {
56+
(adapter as ClientHelper).close();
57+
});
58+
5759
it('should initialize the token', async () => {
5860
await tokenCtrl.init(new Token({
5961
id: tokenId,
@@ -112,4 +114,25 @@ describe('Token e2e', () => {
112114

113115
expect(response.txId).to.exist;
114116
});
117+
118+
it('should preserve the context in parallel invokes', async () => {
119+
await Array(10).fill('').reduce(async (result, _, n) => {
120+
await result;
121+
console.log(`Processing batch ${n+1}/10`);
122+
await Promise.all(Array(10).fill('').map(async (_, m) => {
123+
console.log(`Processing item ${m+1}/10`);
124+
await tokenCtrl.init(new Token({
125+
id: uuid(),
126+
name: 'Token',
127+
symbol: 'TKN',
128+
totalSupply: totalSupply,
129+
balances: { [certificate]: totalSupply },
130+
complex: {
131+
name: 'Test',
132+
value: 5
133+
}
134+
}));
135+
}));
136+
});
137+
});
115138
});

0 commit comments

Comments
 (0)