Skip to content

Commit

Permalink
[FAB-15580] Update types for async iterators
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
shimos committed May 30, 2019
1 parent 08dd598 commit 167b53d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
5 changes: 4 additions & 1 deletion fabric-contract-api/test/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"sourceMap": true
"sourceMap": true,
"lib": [
"esnext"
]
},
"exclude": [
"node_modules"
Expand Down
20 changes: 18 additions & 2 deletions fabric-shim/test/typescript/chaincode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class TestTS implements ChaincodeInterface {
this.testCompositeKey(stub);
await this.testState(stub);
await this.testOtherIteratorCalls(stub);
await this.testAsyncIterators(stub);
await this.testPrivateData(stub);
await this.testOtherStubCalls(stub);
this.testClientIdentity(stub);
Expand Down Expand Up @@ -147,7 +148,7 @@ class TestTS implements ChaincodeInterface {
this.testStateQueryIterator(queryResult);
}

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

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

}

async testAsyncIterators(stub: ChaincodeStub): Promise<void> {
const iterator = stub.getStateByRange('key2', 'key6');
for await (const res of iterator) {
const value = res.value;
}
const iteratorPage = stub.getStateByRangeWithPagination('key2', 'key6', 3);
for await (const res of iteratorPage) {
const value = res.value;
}
const iteratorHistory = stub.getHistoryForKey('key1');
for await (const res of iteratorHistory) {
const tx_id = res.tx_id;
}
}

async testOtherStubCalls(stub: ChaincodeStub): Promise<void> {
const binding: string = stub.getBinding();
const channelID: string = stub.getChannelID();
Expand Down
5 changes: 4 additions & 1 deletion fabric-shim/test/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"sourceMap": true
"sourceMap": true,
"lib": [
"esnext"
]
},
"exclude": [
"node_modules"
Expand Down
45 changes: 23 additions & 22 deletions fabric-shim/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ declare module 'fabric-shim' {
ERROR: number;
}

interface AsyncIterable<T> {
[Symbol.asyncIterator]: () => AsyncIterator<T>;
}

export class ChaincodeStub {
getArgs(): string[];
getStringArgs(): string[];
Expand All @@ -90,14 +94,14 @@ declare module 'fabric-shim' {
deleteState(key: string): Promise<void>;
setStateValidationParameter(key: string, ep: Buffer): Promise<void>;
getStateValidationParameter(key: string): Promise<Buffer>;
getStateByRange(startKey: string, endKey: string): Promise<Iterators.StateQueryIterator>;
getStateByRangeWithPagination(startKey: string, endKey: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>>;
getStateByPartialCompositeKey(objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator>;
getStateByPartialCompositeKeyWithPagination(objectType: string, attributes: string[], pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>>;
getStateByRange(startKey: string, endKey: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getStateByRangeWithPagination(startKey: string, endKey: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;
getStateByPartialCompositeKey(objectType: string, attributes: string[]): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getStateByPartialCompositeKeyWithPagination(objectType: string, attributes: string[], pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;

getQueryResult(query: string): Promise<Iterators.StateQueryIterator>;
getQueryResultWithPagination(query: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>>;
getHistoryForKey(key: string): Promise<Iterators.HistoryQueryIterator>;
getQueryResult(query: string): Promise<Iterators.StateQueryIterator> & AsyncIterable<Iterators.KV>;
getQueryResultWithPagination(query: string, pageSize: number, bookmark?: string): Promise<StateQueryResponse<Iterators.StateQueryIterator>> & AsyncIterable<Iterators.KV>;
getHistoryForKey(key: string): Promise<Iterators.HistoryQueryIterator> & AsyncIterable<Iterators.KeyModification>;

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

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

export namespace Iterators {

interface CommonIterator extends EventEmitter {
interface CommonIterator<T> {
close(): Promise<void>;
next(): Promise<any>;
}

interface HistoryQueryIterator extends CommonIterator {
next(): Promise<NextKeyModificationResult>;
next(): Promise<NextResult<T>>;
}

interface StateQueryIterator extends CommonIterator {
next(): Promise<NextResult>;
}

interface NextResult {
value: KV;
interface NextResult<T> {
value: T;
done: boolean;
}

type HistoryQueryIterator = CommonIterator<KeyModification>;
type StateQueryIterator = CommonIterator<KV>;

interface NextKeyModificationResult {
value: KeyModification;
done: boolean;
}

interface KV {
namespace: string;
key: string;
value: ProtobufBytes;
getNamespace(): string;
getKey(): string;
getValue(): ProtobufBytes;
}
Expand Down
5 changes: 4 additions & 1 deletion fabric-shim/types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
"*": [
"node_modules/*"
]
}
},
"lib": [
"esnext"
]
},
"files": [
"index.d.ts"
Expand Down
5 changes: 4 additions & 1 deletion test/fv/annotations/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"module": "commonjs",
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"lib": [
"esnext"
]
}
}

0 comments on commit 167b53d

Please sign in to comment.