Improved gateway.disconnect usage in js/ts app, Improved comments

Signed-off-by: Sijo Cherian <sijo@ibm.com>
This commit is contained in:
Sijo Cherian 2020-07-07 09:51:07 -04:00
parent 2f58fb29c4
commit d5abf2b3a0
4 changed files with 63 additions and 50 deletions

View file

@ -31,6 +31,7 @@ async function main() {
// Create a new gateway for connecting to our peer node. // Create a new gateway for connecting to our peer node.
const gateway = new Gateway(); const gateway = new Gateway();
try {
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });
// Get the network (channel) our contract is deployed to. // Get the network (channel) our contract is deployed to.
@ -39,14 +40,16 @@ async function main() {
// Get the contract from the network. // Get the contract from the network.
const contract = network.getContract('fabcar'); const contract = network.getContract('fabcar');
// Submit the specified transaction. // 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') // createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave')
await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom');
console.log('Transaction has been submitted'); console.log('Transaction has been submitted');
} finally {
// Disconnect from the gateway. // Disconnect from the gateway.
await gateway.disconnect(); gateway.disconnect();
}
} catch (error) { } catch (error) {
console.error(`Failed to submit transaction: ${error}`); console.error(`Failed to submit transaction: ${error}`);

View file

@ -32,6 +32,7 @@ async function main() {
// Create a new gateway for connecting to our peer node. // Create a new gateway for connecting to our peer node.
const gateway = new Gateway(); const gateway = new Gateway();
try {
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });
// Get the network (channel) our contract is deployed to. // Get the network (channel) our contract is deployed to.
@ -40,15 +41,17 @@ async function main() {
// Get the contract from the network. // Get the contract from the network.
const contract = network.getContract('fabcar'); const contract = network.getContract('fabcar');
// Evaluate the specified transaction. // 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') // queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4')
// queryAllCars transaction - requires no arguments, ex: ('queryAllCars') // queryAllCars transaction - requires no arguments, ex: ('queryAllCars')
const result = await contract.evaluateTransaction('queryAllCars'); const result = await contract.evaluateTransaction('queryAllCars');
console.log(`Transaction has been evaluated, result is: ${result.toString()}`); console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
} finally {
// Disconnect from the gateway. // Disconnect from the gateway.
await gateway.disconnect(); gateway.disconnect();
}
} catch (error) { } catch (error) {
console.error(`Failed to evaluate transaction: ${error}`); console.error(`Failed to evaluate transaction: ${error}`);
process.exit(1); process.exit(1);

View file

@ -27,6 +27,7 @@ async function main() {
// Create a new gateway for connecting to our peer node. // Create a new gateway for connecting to our peer node.
const gateway = new Gateway(); const gateway = new Gateway();
try {
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });
// Get the network (channel) our contract is deployed to. // Get the network (channel) our contract is deployed to.
@ -40,9 +41,10 @@ async function main() {
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave') // changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave')
await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom'); await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom');
console.log(`Transaction has been submitted`); console.log(`Transaction has been submitted`);
} finally {
// Disconnect from the gateway. // Disconnect from the gateway.
await gateway.disconnect(); gateway.disconnect();
}
} catch (error) { } catch (error) {
console.error(`Failed to submit transaction: ${error}`); console.error(`Failed to submit transaction: ${error}`);

View file

@ -28,6 +28,7 @@ async function main() {
// Create a new gateway for connecting to our peer node. // Create a new gateway for connecting to our peer node.
const gateway = new Gateway(); const gateway = new Gateway();
try {
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } }); await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });
// Get the network (channel) our contract is deployed to. // Get the network (channel) our contract is deployed to.
@ -41,6 +42,10 @@ async function main() {
// queryAllCars transaction - requires no arguments, ex: ('queryAllCars') // queryAllCars transaction - requires no arguments, ex: ('queryAllCars')
const result = await contract.evaluateTransaction('queryAllCars'); const result = await contract.evaluateTransaction('queryAllCars');
console.log(`Transaction has been evaluated, result is: ${result.toString()}`); console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
} finally {
// Disconnect from the gateway.
gateway.disconnect();
}
} catch (error) { } catch (error) {
console.error(`Failed to evaluate transaction: ${error}`); console.error(`Failed to evaluate transaction: ${error}`);