From 9b14d5bfe56d564df7227711b43ef23863459510 Mon Sep 17 00:00:00 2001 From: Sijo Cherian Date: Tue, 7 Jul 2020 09:51:07 -0400 Subject: [PATCH] Improved gateway.disconnect usage, Improved comments Signed-off-by: Sijo Cherian --- fabcar/javascript/invoke.js | 30 +++++++++++++++++------------- fabcar/javascript/query.js | 32 ++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/fabcar/javascript/invoke.js b/fabcar/javascript/invoke.js index 228aceb7..65500988 100644 --- a/fabcar/javascript/invoke.js +++ b/fabcar/javascript/invoke.js @@ -31,22 +31,26 @@ async function main() { // Create a new gateway for connecting to our peer node. const gateway = new Gateway(); - await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); + try { + await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); + // Get the network (channel) our contract is deployed to. + const network = await gateway.getNetwork('mychannel'); - // Get the contract from the network. - const contract = network.getContract('fabcar'); + // Get the contract from the network. + const contract = network.getContract('fabcar'); - // Submit the specified transaction. - // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') - // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') - await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); - console.log('Transaction has been submitted'); - - // Disconnect from the gateway. - await gateway.disconnect(); + // Submit the specified transaction: The transaction function name will be evaluated on the + // endorsing peers and then submitted to the ordering service for committing to the ledger. + + // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom') + // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') + await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); + console.log('Transaction has been submitted'); + } finally { + // Disconnect from the gateway. + gateway.disconnect(); + } } catch (error) { console.error(`Failed to submit transaction: ${error}`); diff --git a/fabcar/javascript/query.js b/fabcar/javascript/query.js index 82f5367f..caeec3f4 100644 --- a/fabcar/javascript/query.js +++ b/fabcar/javascript/query.js @@ -32,23 +32,27 @@ async function main() { // Create a new gateway for connecting to our peer node. const gateway = new Gateway(); - await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); + try { + await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); + // Get the network (channel) our contract is deployed to. + const network = await gateway.getNetwork('mychannel'); - // Get the contract from the network. - const contract = network.getContract('fabcar'); + // Get the contract from the network. + const contract = network.getContract('fabcar'); - // Evaluate the specified transaction. - // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') - // queryAllCars transaction - requires no arguments, ex: ('queryAllCars') - const result = await contract.evaluateTransaction('queryAllCars'); - console.log(`Transaction has been evaluated, result is: ${result.toString()}`); - - // Disconnect from the gateway. - await gateway.disconnect(); - + // Evaluate the specified transaction. This is used for querying the world state. + // The transaction function name will be evaluated on the endorsing peers but the responses + // will not be sent to the ordering service and hence will not be committed to the ledger. + + // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4') + // queryAllCars transaction - requires no arguments, ex: ('queryAllCars') + const result = await contract.evaluateTransaction('queryAllCars'); + console.log(`Transaction has been evaluated, result is: ${result.toString()}`); + } finally { + // Disconnect from the gateway. + gateway.disconnect(); + } } catch (error) { console.error(`Failed to evaluate transaction: ${error}`); process.exit(1);