Skip to content

Commit

Permalink
add chaincodePackage
Browse files Browse the repository at this point in the history
  • Loading branch information
horeaporutiu committed May 8, 2019
1 parent 668241e commit e67ae93
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 105 deletions.
Binary file added chaincodePackage/insurance@1.0.6.cds
Binary file not shown.
22 changes: 16 additions & 6 deletions web/www/blockchain/insurancePeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import uuidV4 from 'uuid/v4';

import network from './invoke';


import * as util from 'util' // has no default export

export async function getContractTypes() {
Expand Down Expand Up @@ -170,25 +169,36 @@ export const once = client.once.bind(client);
export const addListener = client.addListener.bind(client);
export const prependListener = client.prependListener.bind(client);
export const removeListener = client.removeListener.bind(client);
const peerType = 'insuranceUser'

//identity to use for submitting transactions to smart contract
const peerType = 'insuranceUser'
let isQuery = false;
let isCloud = true;

async function invoke(fcn, ...args) {

isQuery = false;

console.log(`args in insurancePeer invoke: ${util.inspect(...args)}`)
console.log(`func in insurancePeer invoke: ${util.inspect(fcn)}`)

await network.invokeCC(fcn, ...args);
console.log('after calling await network.invokeCC(fcn, ...args)')
if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.invoke(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
}

async function query(fcn, ...args) {

// await network.invokeCC(fcn, ...args);
console.log('after calling await network.queryCC(fcn, ...args)')
isQuery = true;
console.log(`args in insurancePeer query: ${util.inspect(...args)}`)
console.log(`func in insurancePeer query: ${util.inspect(fcn)}`)

if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.query(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
Expand Down
121 changes: 37 additions & 84 deletions web/www/blockchain/invoke.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,78 @@
//Import Hyperledger Fabric 1.4 programming model - fabric-network
const { FileSystemWallet, Gateway } = require('fabric-network');
const path = require('path');
const fs = require('fs');
import { wrapError, marshalArgs } from './utils';


const path = require('path');
//function used to prepare args for smart contract invokation
import { marshalArgs } from './utils';
import * as util from 'util' // has no default export

//connect to the config file
const configPath = path.join(process.cwd(), './www/blockchain/config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configJSON);
var userName = config.userName;
var gatewayDiscovery = config.gatewayDiscovery;
var connection_file = config.connection_file;

import * as util from 'util' // has no default export

// connect to the connection file
const ccpPath = path.join(process.cwd(), './www/blockchain/ibpConnection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
const connectionProfile = JSON.parse(ccpJSON);

// A wallet stores a collection of identities for use
const walletPath = path.join(process.cwd(), './www/blockchain/wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);

exports.invokeCC = async function (fcn, args) {
exports.invokeCC = async function (isQuery, peerIdentity, fcn, args) {

try {

let response;


console.log('invokeCC called, which is using the new HLF 1.4 mechanism')
console.log(`function: ${fcn} and the args:`)
console.log(`invokeCC called, function: ${fcn} and the args: `)
console.log(args)

// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(userName);
const userExists = await wallet.exists(peerIdentity);
if (!userExists) {
console.log('An identity for the user ' + userName + ' does not exist in the wallet');
console.log('An identity for the user ' + peerIdentity + ' does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
response.error = 'An identity for the user ' + userName + ' does not exist in the wallet. Register ' + userName + ' first';
response.error = 'An identity for the user ' + peerIdentity + ' does not exist in the wallet. Register ' + peerIdentity + ' first';
return response;
}
// console.log(`ccp: ${util.inspect(ccp)}`)

console.log('before gateway NewGateWay')
//connect to Fabric Network, but starting a new gateway
const gateway = new Gateway();
console.log('before gateway.connect')
await gateway.connect(ccp, { wallet, identity: userName, discovery: gatewayDiscovery });

// console.log(`gateway: ${util.inspect(gateway)}`)
//use our config file, our peerIdentity, and our discovery options to connect to Fabric network.
await gateway.connect(connectionProfile, { wallet, identity: peerIdentity, discovery: config.gatewayDiscovery });

console.log('after gateway.connect, before getting network')
//connect to our channel that has been created on IBM Blockchain Platform
const network = await gateway.getNetwork('mychannel-all-ca-as-operators');

console.log('before getting contract')
//connect to our insurance contract that has been installed / instantiated on IBM Blockchain Platform
const contract = await network.getContract('insurance');
console.log('after getting contract')

if (!args) {
console.log('!no args!!')
response = await contract.submitTransaction(fcn);
//prepare our arguments for smart contract
args = marshalArgs(args)

//submit transaction to the smart contract that is installed / instnatiated on the peers
if (isQuery) {
console.log('calling contract.evaluateTransaction, with args');
if (args) {
response = await contract.evaluateTransaction(fcn, args[0]);
response = JSON.parse(response.toString());
console.log(`response from evaluateTransaction: ${(response)}`)
} else {
console.log('calling contract.evaluateTransaction, with no args');
response = await contract.evaluateTransaction(fcn);
response = JSON.parse(response.toString());
console.log(`response from evaluateTransaction: ${(response)}`)
}
} else {
console.log(`func in insurancePeer invoke: ${util.inspect(fcn)}`)

console.log(`before calling marshal, args: ${util.inspect(args)}`)

args = marshalArgs(args)
console.log(`after marshalArgs, args[0]: ${util.inspect(args[0])}`)

console.log('calling contract.submitTransaction');
response = await contract.submitTransaction(fcn, args[0]);
console.log('after contract.submitTransaction')
}

console.log('Transaction has been submitted');

// Disconnect from the gateway.
await gateway.disconnect();

} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
// response.error = error.message;
// return response;
}
}

exports.queryCC = async function (fcn, args) {
try {

console.log('invokeCC called, which is using the new HLF 1.4 mechanism')
console.log(`function: ${fcn} and the args:`)
console.log(args)

// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(userName);
if (!userExists) {
console.log('An identity for the user ' + userName + ' does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
response.error = 'An identity for the user ' + userName + ' does not exist in the wallet. Register ' + userName + ' first';
return response;
}

// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: userName, discovery: gatewayDiscovery });

// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel1');

// Get the contract from the network.
const contract = await network.getContract('insurance');

let response;
if (!args) {
response = await contract.evaluateTransaction(fcn);
} else {
args = JSON.stringify(args)
console.log(`after calling args.stringify(), args: ${args}`)
response = await contract.evaluateTransaction(fcn, args);
// response = JSON.parse(response.toString());
console.log(`response from submitTransaction: ${(response)}`)
}

console.log('Transaction has been submitted');
Expand All @@ -127,7 +82,5 @@ exports.queryCC = async function (fcn, args) {

} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
response.error = error.message;
return response;
}
}
24 changes: 20 additions & 4 deletions web/www/blockchain/policePeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { policeClient as client, isReady } from './setup';

import network from './invoke';

import * as util from 'util' // has no default export

export async function listTheftClaims() {
if (!isReady()) {
Expand Down Expand Up @@ -37,22 +38,37 @@ export const once = client.once.bind(client);
export const addListener = client.addListener.bind(client);
export const prependListener = client.prependListener.bind(client);
export const removeListener = client.removeListener.bind(client);
const peerType = 'insuranceUser'

//identity to use for submitting transactions to smart contract
const peerType = 'policeApp-admin'
let isQuery = false;
let isCloud = true;

async function invoke(fcn, ...args) {

isQuery = false;

await network.invokeCC(fcn, ...args);
console.log(`args in policePeer invoke: ${util.inspect(...args)}`)
console.log(`func in policePeer invoke: ${util.inspect(fcn)}`)

console.log('after calling await network.invokeCC(fcn, ...args)')
if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.invoke(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
}

async function query(fcn, ...args) {

console.log('after calling await network.queryCC(fcn, ...args)')
isQuery = true;

console.log(`args in policePeer query: ${util.inspect(...args)}`)
console.log(`func in policePeer query: ${util.inspect(fcn)}`)

if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.query(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
Expand Down
30 changes: 25 additions & 5 deletions web/www/blockchain/repairShopPeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { repairShopClient as client, isReady } from './setup';

import network from './invoke';

import * as util from 'util' // has no default export

export async function getRepairOrders() {
if (!isReady()) {
return;
Expand Down Expand Up @@ -41,21 +43,39 @@ export const once = client.once.bind(client);
export const addListener = client.addListener.bind(client);
export const prependListener = client.prependListener.bind(client);
export const removeListener = client.removeListener.bind(client);
const peerType = 'insuranceUser'

//identity to use for submitting transactions to smart contract
const peerType = 'repairApp-admin3'
let isQuery = false;
let isCloud = true;


async function invoke(fcn, ...args) {
await network.invokeCC(fcn, ...args);

console.log('after calling await network.invokeCC(fcn, ...args)')

isQuery = false;

console.log(`args in repairPeer invoke: ${util.inspect(...args)}`)
console.log(`func in repairPeer invoke: ${util.inspect(fcn)}`)

if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.invoke(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
}

async function query(fcn, ...args) {

console.log('after calling await network.queryCC(fcn, ...args)')
isQuery = true;

console.log(`args in repairPeer query: ${util.inspect(...args)}`)
console.log(`func in repairPeer query: ${util.inspect(fcn)}`)

if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.query(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
}
28 changes: 22 additions & 6 deletions web/www/blockchain/shopPeer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import uuidV4 from 'uuid/v4';

import network from './invoke';

import * as util from 'util' // has no default export


export async function getContractTypes(shopType) {
if (!isReady()) {
return;
Expand Down Expand Up @@ -95,13 +98,21 @@ export const addListener = client.addListener.bind(client);
export const prependListener = client.prependListener.bind(client);
export const removeListener = client.removeListener.bind(client);

const peerType = 'insuranceUser'
//identity to use for submitting transactions to smart contract
const peerType = 'shopApp-admin'
let isQuery = false;
let isCloud = true;

async function invoke(fcn, ...args) {
//using the new programming model
await network.invokeCC(fcn, ...args);

console.log('after calling await network.invokeCC(fcn, ...args)')
isQuery = false;

console.log(`args in shopPeer invoke: ${util.inspect(...args)}`)
console.log(`func in shopPeer invoke: ${util.inspect(fcn)}`)

if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.invoke(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
Expand All @@ -110,9 +121,14 @@ async function invoke(fcn, ...args) {

async function query(fcn, ...args) {

// await network.invokeCC(fcn, ...args);
isQuery = true;

console.log(`args in shopPeer query: ${util.inspect(...args)}`)
console.log(`func in shopPeer query: ${util.inspect(fcn)}`)

console.log('after calling await network.queryCC(fcn, ...args)')
if (isCloud) {
await network.invokeCC(isQuery, peerType, fcn, ...args);
}

return client.query(
config.chaincodeId, config.chaincodeVersion, fcn, ...args);
Expand Down

0 comments on commit e67ae93

Please sign in to comment.