moved try catch from contract wrapper to app

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
sapthasurendran 2022-05-20 15:18:01 +05:30
parent 52a3e655a4
commit 7792806482
2 changed files with 184 additions and 195 deletions

View file

@ -8,6 +8,7 @@ import { connect } from '@hyperledger/fabric-gateway';
import { newGrpcConnection, newIdentity, newSigner, tlsCertPathOrg1, peerEndpointOrg1, peerNameOrg1, certPathOrg1, mspIdOrg1, keyDirectoryPathOrg1, tlsCertPathOrg2, peerEndpointOrg2, peerNameOrg2, certPathOrg2, mspIdOrg2, keyDirectoryPathOrg2 } from './connect'; import { newGrpcConnection, newIdentity, newSigner, tlsCertPathOrg1, peerEndpointOrg1, peerNameOrg1, certPathOrg1, mspIdOrg1, keyDirectoryPathOrg1, tlsCertPathOrg2, peerEndpointOrg2, peerNameOrg2, certPathOrg2, mspIdOrg2, keyDirectoryPathOrg2 } from './connect';
import { ContractWrapper } from './contractWrapper'; import { ContractWrapper } from './contractWrapper';
import { RED, RESET } from './utils';
const channelName = 'mychannel'; const channelName = 'mychannel';
const chaincodeName = 'secured'; const chaincodeName = 'secured';
@ -70,7 +71,11 @@ async function main(): Promise<void> {
await contractWrapperOrg1.getAssetPrivateProperties(assetKey, mspIdOrg1); await contractWrapperOrg1.getAssetPrivateProperties(assetKey, mspIdOrg1);
// Org2 is not the owner and does not have the private details, read expected to fail. // Org2 is not the owner and does not have the private details, read expected to fail.
try {
await contractWrapperOrg2.getAssetPrivateProperties(assetKey, mspIdOrg1); await contractWrapperOrg2.getAssetPrivateProperties(assetKey, mspIdOrg1);
} catch(e) {
console.log(`${RED}*** Failed: getAssetPrivateProperties - ${e}${RESET}`);
}
// Org1 updates the assets public description. // Org1 updates the assets public description.
await contractWrapperOrg1.changePublicDescription({AssetId: assetKey, await contractWrapperOrg1.changePublicDescription({AssetId: assetKey,
@ -84,10 +89,14 @@ async function main(): Promise<void> {
await contractWrapperOrg2.readAsset(assetKey, mspIdOrg1); await contractWrapperOrg2.readAsset(assetKey, mspIdOrg1);
// This is an update to the public state and requires the owner(Org1) to endorse and sent by the owner org client (Org1). // This is an update to the public state and requires the owner(Org1) to endorse and sent by the owner org client (Org1).
// Since the client is from Org2, which is not the owner, this will fail // Since the client is from Org2, which is not the owner, this will fail.
try{
await contractWrapperOrg2.changePublicDescription({AssetId: assetKey, await contractWrapperOrg2.changePublicDescription({AssetId: assetKey,
OwnerOrg: mspIdOrg1, OwnerOrg: mspIdOrg1,
PublicDescription: `Asset ${assetKey} owned by ${mspIdOrg2} is NOT for sale`}); PublicDescription: `Asset ${assetKey} owned by ${mspIdOrg2} is NOT for sale`});
} catch(e) {
console.log(`${RED}*** Failed: changePublicDescription - ${e}${RESET}`);
}
// Read the public details by org1. // Read the public details by org1.
await contractWrapperOrg1.readAsset(assetKey, mspIdOrg1); await contractWrapperOrg1.readAsset(assetKey, mspIdOrg1);
@ -115,18 +124,28 @@ async function main(): Promise<void> {
await contractWrapperOrg1.getAssetSalesPrice(assetKey, mspIdOrg1); await contractWrapperOrg1.getAssetSalesPrice(assetKey, mspIdOrg1);
// Org2 has not set a sale price and this should fail. // Org2 has not set a sale price and this should fail.
try{
await contractWrapperOrg2.getAssetSalesPrice(assetKey, mspIdOrg1); await contractWrapperOrg2.getAssetSalesPrice(assetKey, mspIdOrg1);
} catch(e) {
console.log(`${RED}*** Failed: getAssetSalesPrice - ${e}${RESET}`);
}
// Org1 has not agreed to buy so this should fail. // Org1 has not agreed to buy so this should fail.
try{
await contractWrapperOrg1.getAssetBidPrice(assetKey, mspIdOrg2); await contractWrapperOrg1.getAssetBidPrice(assetKey, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: getAssetBidPrice - ${e}${RESET}`);
}
// Org2 should be able to see the price it has agreed. // Org2 should be able to see the price it has agreed.
await contractWrapperOrg2.getAssetBidPrice(assetKey, mspIdOrg2); await contractWrapperOrg2.getAssetBidPrice(assetKey, mspIdOrg2);
// Org1 will try to transfer the asset to Org2 // Org1 will try to transfer the asset to Org2
// This will fail due to the sell price and the bid price are not the same. // This will fail due to the sell price and the bid price are not the same.
try{
await contractWrapperOrg1.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 110, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2); await contractWrapperOrg1.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 110, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: transferAsset - ${e}${RESET}`);
}
// Agree to a sell by Org1, the seller will agree to the bid price of Org2. // Agree to a sell by Org1, the seller will agree to the bid price of Org2.
await contractWrapperOrg1.agreeToSell({AssetId:assetKey, Price:100, TradeId:now}); await contractWrapperOrg1.agreeToSell({AssetId:assetKey, Price:100, TradeId:now});
@ -147,7 +166,11 @@ async function main(): Promise<void> {
// Org2 user will try to transfer the asset to Org1. // Org2 user will try to transfer the asset to Org1.
// This will fail as the owner is Org1. // This will fail as the owner is Org1.
try{
await contractWrapperOrg2.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 100, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2); await contractWrapperOrg2.transferAsset({ObjectType: 'asset_properties', Color: 'blue', Size: 35}, { AssetId: assetKey, Price: 100, TradeId: now}, [ mspIdOrg1, mspIdOrg2 ], mspIdOrg1, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: transferAsset - ${e}${RESET}`);
}
// Org1 will transfer the asset to Org2. // Org1 will transfer the asset to Org2.
// This will now complete as the sell price and the bid price are the same. // This will now complete as the sell price and the bid price are the same.
@ -163,7 +186,11 @@ async function main(): Promise<void> {
await contractWrapperOrg2.getAssetPrivateProperties(assetKey, mspIdOrg2); await contractWrapperOrg2.getAssetPrivateProperties(assetKey, mspIdOrg2);
// Org1 should not be able to read the private data details of this asset, expected to fail. // Org1 should not be able to read the private data details of this asset, expected to fail.
try{
await contractWrapperOrg1.getAssetPrivateProperties(assetKey, mspIdOrg2); await contractWrapperOrg1.getAssetPrivateProperties(assetKey, mspIdOrg2);
} catch(e) {
console.log(`${RED}*** Failed: getAssetPrivateProperties - ${e}${RESET}`);
}
// This is an update to the public state and requires only the owner to endorse. // This is an update to the public state and requires only the owner to endorse.
// Org2 wants to indicate that the items is no longer for sale. // Org2 wants to indicate that the items is no longer for sale.

View file

@ -24,12 +24,12 @@ export class ContractWrapper {
public async createAsset(asset: Asset, privateData: AssetPrivateData): Promise<void> { public async createAsset(asset: Asset, privateData: AssetPrivateData): Promise<void> {
console.log(`${GREEN}--> Submit Transaction: CreateAsset, ${asset.AssetId} as ${asset.OwnerOrg} - endorsed by Org1${RESET}`); console.log(`${GREEN}--> Submit Transaction: CreateAsset, ${asset.AssetId} as ${asset.OwnerOrg} - endorsed by Org1${RESET}`);
try { const assetPropertiesJSON: AssetPropertiesJSON = {
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties', objectType: 'asset_properties',
assetID: asset.AssetId, assetID: asset.AssetId,
color: privateData.Color, color: privateData.Color,
size: privateData.Size, size: privateData.Size,
salt: this.#randomBytes }); salt: this.#randomBytes };
await this.#contract.submit('CreateAsset', { await this.#contract.submit('CreateAsset', {
arguments: [asset.AssetId, asset.PublicDescription], arguments: [asset.AssetId, asset.PublicDescription],
@ -37,14 +37,10 @@ export class ContractWrapper {
}); });
console.log(`*** Result: committed, asset ${asset.AssetId} is owned by Org1`); console.log(`*** Result: committed, asset ${asset.AssetId} is owned by Org1`);
}catch (e) {
console.log(`${RED}*** Failed: createAsset - ${e}${RESET}`);
}
} }
public async readAsset(assetKey: string, ownerOrg: string): Promise<void> { public async readAsset(assetKey: string, ownerOrg: string): Promise<void> {
console.log(`${GREEN}--> Evaluate Transactions: ReadAsset as ${this.#org}, - ${assetKey} should be owned by ${ownerOrg}${RESET}`); console.log(`${GREEN}--> Evaluate Transactions: ReadAsset as ${this.#org}, - ${assetKey} should be owned by ${ownerOrg}${RESET}`);
try {
const resultBytes = await this.#contract.evaluateTransaction('ReadAsset', assetKey); const resultBytes = await this.#contract.evaluateTransaction('ReadAsset', assetKey);
@ -59,16 +55,12 @@ export class ContractWrapper {
} else { } else {
throw new Error('No Asset Found'); throw new Error('No Asset Found');
} }
}catch (e) {
console.log(`${RED}*** Failed evaluateTransaction readAsset - ${e}${RESET}`);
}
} }
public async getAssetPrivateProperties(assetKey: string, ownerOrg: string): Promise<void> { public async getAssetPrivateProperties(assetKey: string, ownerOrg: string): Promise<void> {
try{
console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${this.#org}${RESET}`); console.log(`${GREEN}--> Evaluate Transaction: GetAssetPrivateProperties, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== ownerOrg) { if(this.#org !== ownerOrg) {
console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner and does not have the private details${RESET}`); console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner and does not have the private details.${RESET}`);
} }
const resultBytes = await this.#contract.evaluateTransaction('GetAssetPrivateProperties', assetKey); const resultBytes = await this.#contract.evaluateTransaction('GetAssetPrivateProperties', assetKey);
@ -82,16 +74,12 @@ export class ContractWrapper {
}; };
console.log('*** Result:', result); console.log('*** Result:', result);
} }
catch(e){
console.log(`${RED}*** Failed evaluateTransaction readPrivateAsset: ${e}${RESET}`);
}
}
public async changePublicDescription(asset: Asset): Promise<void> { public async changePublicDescription(asset: Asset): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: ChangePublicDescription ${asset.AssetId}, as ${this.#org} - endorse by ${this.#org} ${RESET}`); console.log(`${GREEN}--> Submit Transaction: ChangePublicDescription ${asset.AssetId}, as ${this.#org} - endorse by ${this.#org} ${RESET}`);
if (asset.OwnerOrg !== this.#org) { if (asset.OwnerOrg !== this.#org) {
console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner${RESET}`); console.log(`${GREEN}* Expected to fail as ${this.#org} is not the owner.${RESET}`);
} }
await this.#contract.submit('ChangePublicDescription', { await this.#contract.submit('ChangePublicDescription', {
@ -99,14 +87,10 @@ export class ContractWrapper {
}); });
console.log(`*** Result: committed, Desc: ${asset.PublicDescription}`); console.log(`*** Result: committed, Desc: ${asset.PublicDescription}`);
} catch (e) {
console.log(`${RED}*** Failed: ChangePublicDescription - ${e}${RESET}`);
}
} }
public async agreeToSell(assetPrice: AssetPrice): Promise<void> { public async agreeToSell(assetPrice: AssetPrice): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: AgreeToSell, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`); console.log(`${GREEN}--> Submit Transaction: AgreeToSell, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPriceJSON: AssetPriceJSON = { const assetPriceJSON: AssetPriceJSON = {
assetID:assetPrice.AssetId, assetID:assetPrice.AssetId,
@ -120,13 +104,9 @@ export class ContractWrapper {
}); });
console.log(`*** Result: committed, ${this.#org} has agreed to sell asset ${assetPrice.AssetId} for ${assetPrice.Price}`); console.log(`*** Result: committed, ${this.#org} has agreed to sell asset ${assetPrice.AssetId} for ${assetPrice.Price}`);
} catch (e) {
console.log(`${RED}*** Failed: AgreeToSell - ${e}${RESET}`);
}
} }
public async verifyAssetProperties(assetProperties: AssetProperties): Promise<void> { public async verifyAssetProperties(assetProperties: AssetProperties): Promise<void> {
try {
console.log(`${GREEN}--> Evalute: VerifyAssetProperties, ${assetProperties.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`); console.log(`${GREEN}--> Evalute: VerifyAssetProperties, ${assetProperties.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties', const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties',
assetID: assetProperties.AssetId, assetID: assetProperties.AssetId,
@ -156,16 +136,10 @@ export class ContractWrapper {
} else { } else {
throw new Error(`Private information about asset ${assetProperties.AssetId} has not been verified by ${this.#org}`); throw new Error(`Private information about asset ${assetProperties.AssetId} has not been verified by ${this.#org}`);
} }
} catch (e) {
console.log(`${RED}*** Failed: VerifyAssetProperties - ${e}${RESET}`);
}
} }
public async agreeToBuy(assetPrice: AssetPrice, ): Promise<void> { public async agreeToBuy(assetPrice: AssetPrice, ): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: AgreeToBuy, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`); console.log(`${GREEN}--> Submit Transaction: AgreeToBuy, ${assetPrice.AssetId} as ${this.#org} - endorsed by ${this.#org}${RESET}`);
const assetPriceJSON: AssetPriceJSON = { const assetPriceJSON: AssetPriceJSON = {
assetID: assetPrice.AssetId, assetID: assetPrice.AssetId,
@ -179,17 +153,14 @@ export class ContractWrapper {
}); });
console.log(`*** Result: committed, ${this.#org} has agreed to buy asset ${assetPrice.AssetId} for 100`); console.log(`*** Result: committed, ${this.#org} has agreed to buy asset ${assetPrice.AssetId} for 100`);
} catch (e) {
console.log(`${RED}*** Failed: AgreeToBuy - ${e}${RESET}`);
}
} }
public async getAssetSalesPrice(assetKey: string, ownerOrg: string): Promise<void> { public async getAssetSalesPrice(assetKey: string, ownerOrg: string): Promise<void> {
try {
console.log(`${GREEN}--> Evaluate Transaction: GetAssetSalesPrice, - ${assetKey} from organization ${this.#org}${RESET}`); console.log(`${GREEN}--> Evaluate Transaction: GetAssetSalesPrice, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== ownerOrg) { if(this.#org !== ownerOrg) {
console.log(`${GREEN}* Expected to fail as ${this.#org} has not set a sale price${RESET}`); console.log(`${GREEN}* Expected to fail as ${this.#org} has not set a sale price.${RESET}`);
} }
const resultBytes = await this.#contract.evaluateTransaction('GetAssetSalesPrice', assetKey); const resultBytes = await this.#contract.evaluateTransaction('GetAssetSalesPrice', assetKey);
@ -204,16 +175,13 @@ export class ContractWrapper {
}; };
console.log('*** Result: GetAssetSalesPrice', result); console.log('*** Result: GetAssetSalesPrice', result);
} catch (e) {
console.log(`${RED}*** Failed evaluateTransaction GetAssetSalesPrice: ${e}${RESET}`);
}
} }
public async getAssetBidPrice(assetKey: string, buyerOrgID: string): Promise<void> { public async getAssetBidPrice(assetKey: string, buyerOrgID: string): Promise<void> {
try{
console.log(`${GREEN}--> Evaluate Transaction: GetAssetBidPrice, - ${assetKey} from organization ${this.#org}${RESET}`); console.log(`${GREEN}--> Evaluate Transaction: GetAssetBidPrice, - ${assetKey} from organization ${this.#org}${RESET}`);
if(this.#org !== buyerOrgID){ if(this.#org !== buyerOrgID){
console.log(`${GREEN}* Expected to fail as ${this.#org} has not agreed to buy${RESET}`); console.log(`${GREEN}* Expected to fail as ${this.#org} has not agreed to buy.${RESET}`);
} }
const resultBytes = await this.#contract.evaluateTransaction('GetAssetBidPrice', assetKey); const resultBytes = await this.#contract.evaluateTransaction('GetAssetBidPrice', assetKey);
@ -227,19 +195,16 @@ export class ContractWrapper {
}; };
console.log('*** Result: GetAssetBidPrice', result); console.log('*** Result: GetAssetBidPrice', result);
} catch (e) {
console.log(`${RED}*** Failed evaluateTransaction GetAssetBidPrice: ${e}${RESET}`);
}
} }
public async transferAsset( privateData: AssetPrivateData, assetPrice: AssetPrice, endorsingOrganizations: string[], ownerOrgID: string, buyerOrgID: string): Promise<void> { public async transferAsset( privateData: AssetPrivateData, assetPrice: AssetPrice, endorsingOrganizations: string[], ownerOrgID: string, buyerOrgID: string): Promise<void> {
try {
console.log(`${GREEN}--> Submit Transaction: TransferAsset, ${assetPrice.AssetId} as ${this.#org } - endorsed by ${this.#org}${RESET}`); console.log(`${GREEN}--> Submit Transaction: TransferAsset, ${assetPrice.AssetId} as ${this.#org } - endorsed by ${this.#org}${RESET}`);
if (this.#org !== ownerOrgID) { if (this.#org !== ownerOrgID) {
console.log(`${GREEN}* Expected to fail as the owner is ${ownerOrgID}${RESET}`); console.log(`${GREEN}* Expected to fail as the owner is ${ownerOrgID}.${RESET}`);
} else if (assetPrice.Price === 110) { } else if (assetPrice.Price === 110) {
console.log(`${GREEN}* Expected to fail as sell price and the bid price are not the same${RESET}`); console.log(`${GREEN}* Expected to fail as sell price and the bid price are not the same.${RESET}`);
} }
const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties', const assetPropertiesJSON: AssetPropertiesJSON = Object.assign({}, {objectType: 'asset_properties',
@ -259,8 +224,5 @@ export class ContractWrapper {
}); });
console.log(`${GREEN}*** Result: committed, ${this.#org} has transfered the asset ${assetPrice.AssetId} to ${buyerOrgID} ${RESET}`); console.log(`${GREEN}*** Result: committed, ${this.#org} has transfered the asset ${assetPrice.AssetId} to ${buyerOrgID} ${RESET}`);
} catch (e) {
console.log(`${RED}*** Failed: TransferAsset - ${e}${RESET}`);
}
} }
} }