Skip to content

Commit 167b53d

Browse files
committed
[FAB-15580] Update types for async iterators
This patch updates type definitions for some methods returning iterators which support async iterators. It also refines the defintions for CommonIterator and NextResult. FAB-15580 #done Change-Id: I25664a411c6b00662ae3875a3b39264e3aec6cac Signed-off-by: Taku Shimosawa <taku.shimosawa@hal.hitachi.com>
1 parent 08dd598 commit 167b53d

6 files changed

Lines changed: 57 additions & 28 deletions

File tree

fabric-contract-api/test/typescript/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"compilerOptions": {
99
"module": "commonjs",
1010
"target": "es2017",
11-
"sourceMap": true
11+
"sourceMap": true,
12+
"lib": [
13+
"esnext"
14+
]
1215
},
1316
"exclude": [
1417
"node_modules"

fabric-shim/test/typescript/chaincode.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class TestTS implements ChaincodeInterface {
7777
this.testCompositeKey(stub);
7878
await this.testState(stub);
7979
await this.testOtherIteratorCalls(stub);
80+
await this.testAsyncIterators(stub);
8081
await this.testPrivateData(stub);
8182
await this.testOtherStubCalls(stub);
8283
this.testClientIdentity(stub);
@@ -147,7 +148,7 @@ class TestTS implements ChaincodeInterface {
147148
this.testStateQueryIterator(queryResult);
148149
}
149150

150-
async testIterator(iterator: Iterators.CommonIterator) {
151+
async testIterator(iterator: Iterators.CommonIterator<any>) {
151152
const historyNext: Promise<any> = iterator.next();
152153
const nextVal: any = await historyNext;
153154
const historyClose: Promise<void> = iterator.close();
@@ -170,7 +171,7 @@ class TestTS implements ChaincodeInterface {
170171
}
171172

172173
async testStateQueryIterator(stateQuery: Iterators.StateQueryIterator) {
173-
const stateNext: Iterators.NextResult = await stateQuery.next();
174+
const stateNext: Iterators.NextResult<Iterators.KV> = await stateQuery.next();
174175
await stateQuery.close();
175176
const done: boolean = stateNext.done;
176177
const keyVal: Iterators.KV = stateNext.value;
@@ -197,6 +198,21 @@ class TestTS implements ChaincodeInterface {
197198

198199
}
199200

201+
async testAsyncIterators(stub: ChaincodeStub): Promise<void> {
202+
const iterator = stub.getStateByRange('key2', 'key6');
203+
for await (const res of iterator) {
204+
const value = res.value;
205+
}
206+
const iteratorPage = stub.getStateByRangeWithPagination('key2', 'key6', 3);
207+
for await (const res of iteratorPage) {
208+
const value = res.value;
209+
}
210+
const iteratorHistory = stub.getHistoryForKey('key1');
211+
for await (const res of iteratorHistory) {
212+
const tx_id = res.tx_id;
213+
}
214+
}
215+
200216
async testOtherStubCalls(stub: ChaincodeStub): Promise<void> {
201217
const binding: string = stub.getBinding();
202218
const channelID: string = stub.getChannelID();

fabric-shim/test/typescript/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"compilerOptions": {
99
"module": "commonjs",
1010
"target": "es2017",
11-
"sourceMap": true
11+
"sourceMap": true,
12+
"lib": [
13+
"esnext"
14+
]
1215
},
1316
"exclude": [
1417
"node_modules"

fabric-shim/types/index.d.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ declare module 'fabric-shim' {
7171
ERROR: number;
7272
}
7373

74+
interface AsyncIterable<T> {
75+
[Symbol.asyncIterator]: () => AsyncIterator<T>;
76+
}
77+
7478
export class ChaincodeStub {
7579
getArgs(): string[];
7680
getStringArgs(): string[];
@@ -90,14 +94,14 @@ declare module 'fabric-shim' {
9094
deleteState(key: string): Promise<void>;
9195
setStateValidationParameter(key: string, ep: Buffer): Promise<void>;
9296
getStateValidationParameter(key: string): Promise<Buffer>;
93-
getStateByRange(startKey: string, endKey: string): Promise<Iterators.StateQueryIterator>;
94-
getStateByRangeWithPagination(startKey: string, endKey: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>>;
95-
getStateByPartialCompositeKey(objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator>;
96-
getStateByPartialCompositeKeyWithPagination(objectType: string, attributes: string[], pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>>;
97+
getStateByRange(startKey: string, endKey: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
98+
getStateByRangeWithPagination(startKey: string, endKey: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;
99+
getStateByPartialCompositeKey(objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
100+
getStateByPartialCompositeKeyWithPagination(objectType: string, attributes: string[], pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;
97101

98-
getQueryResult(query: string): Promise<Iterators.StateQueryIterator>;
99-
getQueryResultWithPagination(query: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>>;
100-
getHistoryForKey(key: string): Promise<Iterators.HistoryQueryIterator>;
102+
getQueryResult(query: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
103+
getQueryResultWithPagination(query: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;
104+
getHistoryForKey(key: string): Promise<Iterators.HistoryQueryIterator> & AsyncIterable<Iterators.KeyModification>;
101105

102106
invokeChaincode(chaincodeName: string, args: string[], channel: string): Promise<ChaincodeResponse>;
103107
setEvent(name: string, payload: Buffer): void;
@@ -111,9 +115,9 @@ declare module 'fabric-shim' {
111115
deletePrivateData(collection: string, key: string): Promise<void>;
112116
setPrivateDataValidationParameter(collection: string, key: string, ep: Buffer): Promise<void>;
113117
getPrivateDataValidationParameter(collection: string, key: string): Promise<Buffer>;
114-
getPrivateDataByRange(collection: string, startKey: string, endKey: string): Promise<Iterators.StateQueryIterator>;
115-
getPrivateDataByPartialCompositeKey(collection: string, objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator>;
116-
getPrivateDataQueryResult(collection: string, query: string): Promise<Iterators.StateQueryIterator>;
118+
getPrivateDataByRange(collection: string, startKey: string, endKey: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
119+
getPrivateDataByPartialCompositeKey(collection: string, objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
120+
getPrivateDataQueryResult(collection: string, query: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
117121

118122
static RESPONSE_CODE: ResponseCode;
119123
}
@@ -130,32 +134,29 @@ declare module 'fabric-shim' {
130134

131135
export namespace Iterators {
132136

133-
interface CommonIterator extends EventEmitter {
137+
interface CommonIterator<T> {
134138
close(): Promise<void>;
135-
next(): Promise<any>;
136-
}
137-
138-
interface HistoryQueryIterator extends CommonIterator {
139-
next(): Promise<NextKeyModificationResult>;
139+
next(): Promise<NextResult<T>>;
140140
}
141141

142-
interface StateQueryIterator extends CommonIterator {
143-
next(): Promise<NextResult>;
144-
}
145-
146-
interface NextResult {
147-
value: KV;
142+
interface NextResult<T> {
143+
value: T;
148144
done: boolean;
149145
}
150146

147+
type HistoryQueryIterator = CommonIterator<KeyModification>;
148+
type StateQueryIterator = CommonIterator<KV>;
149+
151150
interface NextKeyModificationResult {
152151
value: KeyModification;
153152
done: boolean;
154153
}
155154

156155
interface KV {
156+
namespace: string;
157157
key: string;
158158
value: ProtobufBytes;
159+
getNamespace(): string;
159160
getKey(): string;
160161
getValue(): ProtobufBytes;
161162
}

fabric-shim/types/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"*": [
2323
"node_modules/*"
2424
]
25-
}
25+
},
26+
"lib": [
27+
"esnext"
28+
]
2629
},
2730
"files": [
2831
"index.d.ts"

test/fv/annotations/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"module": "commonjs",
77
"declaration": true,
88
"experimentalDecorators": true,
9-
"emitDecoratorMetadata": true
9+
"emitDecoratorMetadata": true,
10+
"lib": [
11+
"esnext"
12+
]
1013
}
1114
}

0 commit comments

Comments
 (0)