From 521a7ffc4ae9c805e14aab36335daae5d90e2d99 Mon Sep 17 00:00:00 2001 From: Simon Stone Date: Wed, 18 Sep 2019 09:52:00 +0100 Subject: [PATCH] [FAB-16607] Update FabCar to reflect CC updates Node.js CC: As a result of using the new protobufjs library, certain external APIs have changed in the Node.js chaincode. The samples need to be updated to use these new APIs. Java CC: As a result of moving to Java 11, we hit this issue: https://github.com/gradle/gradle/issues/8286 The sample needs to be updated to specify the absolute path to checkstyle's suppression.xml. Signed-off-by: Simon Stone Change-Id: I4db886b3feff46d165e05a7eda624230b65f9cbe --- .../java/config/checkstyle/checkstyle.xml | 4 +-- chaincode/fabcar/javascript/lib/fabcar.js | 35 ++++++------------- chaincode/fabcar/typescript/src/fabcar.ts | 35 ++++++------------- 3 files changed, 24 insertions(+), 50 deletions(-) diff --git a/chaincode/fabcar/java/config/checkstyle/checkstyle.xml b/chaincode/fabcar/java/config/checkstyle/checkstyle.xml index 94317559e7..29fbde9b0c 100644 --- a/chaincode/fabcar/java/config/checkstyle/checkstyle.xml +++ b/chaincode/fabcar/java/config/checkstyle/checkstyle.xml @@ -28,9 +28,9 @@ --> - + - + diff --git a/chaincode/fabcar/javascript/lib/fabcar.js b/chaincode/fabcar/javascript/lib/fabcar.js index 53f2faefa1..ed6b520d30 100644 --- a/chaincode/fabcar/javascript/lib/fabcar.js +++ b/chaincode/fabcar/javascript/lib/fabcar.js @@ -108,33 +108,20 @@ class FabCar extends Contract { async queryAllCars(ctx) { const startKey = 'CAR0'; const endKey = 'CAR999'; - - const iterator = await ctx.stub.getStateByRange(startKey, endKey); - const allResults = []; - while (true) { - const res = await iterator.next(); - - if (res.value && res.value.value.toString()) { - console.log(res.value.value.toString('utf8')); - - const Key = res.value.key; - let Record; - try { - Record = JSON.parse(res.value.value.toString('utf8')); - } catch (err) { - console.log(err); - Record = res.value.value.toString('utf8'); - } - allResults.push({ Key, Record }); - } - if (res.done) { - console.log('end of data'); - await iterator.close(); - console.info(allResults); - return JSON.stringify(allResults); + for await (const {key, value} of ctx.stub.getStateByRange(startKey, endKey)) { + const strValue = Buffer.from(value).toString('utf8'); + let record; + try { + record = JSON.parse(strValue); + } catch (err) { + console.log(err); + record = strValue; } + allResults.push({ Key: key, Record: record }); } + console.info(allResults); + return JSON.stringify(allResults); } async changeCarOwner(ctx, carNumber, newOwner) { diff --git a/chaincode/fabcar/typescript/src/fabcar.ts b/chaincode/fabcar/typescript/src/fabcar.ts index de72b0394f..2f45f9efe9 100644 --- a/chaincode/fabcar/typescript/src/fabcar.ts +++ b/chaincode/fabcar/typescript/src/fabcar.ts @@ -107,33 +107,20 @@ export class FabCar extends Contract { public async queryAllCars(ctx: Context): Promise { const startKey = 'CAR0'; const endKey = 'CAR999'; - - const iterator = await ctx.stub.getStateByRange(startKey, endKey); - const allResults = []; - while (true) { - const res = await iterator.next(); - - if (res.value && res.value.value.toString()) { - console.log(res.value.value.toString('utf8')); - - const Key = res.value.key; - let Record; - try { - Record = JSON.parse(res.value.value.toString('utf8')); - } catch (err) { - console.log(err); - Record = res.value.value.toString('utf8'); - } - allResults.push({ Key, Record }); - } - if (res.done) { - console.log('end of data'); - await iterator.close(); - console.info(allResults); - return JSON.stringify(allResults); + for await (const {key, value} of ctx.stub.getStateByRange(startKey, endKey)) { + const strValue = Buffer.from(value).toString('utf8'); + let record; + try { + record = JSON.parse(strValue); + } catch (err) { + console.log(err); + record = strValue; } + allResults.push({ Key: key, Record: record }); } + console.info(allResults); + return JSON.stringify(allResults); } public async changeCarOwner(ctx: Context, carNumber: string, newOwner: string) {