Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to byfn.sh exec, fix bug in networkUp function #3

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
70bff28
[FAB-5197]Check all prop-responses in balance-transfer
yuki-kon Jul 6, 2017
e0db341
[FAB-5287] NodeSDK - fix sample
harrisob Jul 13, 2017
6422fc3
[FAB-5311] Fix spelling error
glindsell Jul 14, 2017
0611471
Merge "[FAB-5197]Check all prop-responses in balance-transfer"
christo4ferris Jul 17, 2017
d9e2d5c
Merge "[FAB-5287] NodeSDK - fix sample"
christo4ferris Jul 17, 2017
1153988
Merge "[FAB-5311] Fix spelling error"
christo4ferris Jul 19, 2017
6cce07c
[FAB-5155]Fix README.md in "balance-transfer" example
yuki-kon Jul 3, 2017
a7e83fc
FAB-5392 Fix path issue for Git Bash users
mastersingh24 Jul 20, 2017
134549a
FAB-5412 add fabric-preload.sh script
christo4ferris Jul 22, 2017
7778837
[FAB-5394] Introduce Delay as configurable variable
krishvoor Jul 20, 2017
2f4e9b8
[FAB-5130] Invalid syntax in docker compose files
Jul 24, 2017
79cb041
[FAB-5303] Further balance-transfer code optimization
jimthematrix Jul 18, 2017
6b99925
Merge "[FAB-5394] Introduce Delay as configurable variable"
christo4ferris Aug 2, 2017
5921140
[FAB-5576] -f flag to choose docker-compose on byfn.sh
dsanchezseco Aug 3, 2017
be773d2
[FAB-5603] fixed missing f option on switch
dsanchezseco Aug 3, 2017
cb3e7f6
Merge "[FAB-5130] Invalid syntax in docker compose files"
mastersingh24 Aug 5, 2017
2571c96
[FAB-56431]: Add dependency for couchdb into peer.
C0rWin Aug 6, 2017
6e09679
Merge "[FAB-5603] fixed missing f option on switch"
C0rWin Aug 7, 2017
7f1c2f4
FAB-5056 enable couchdb test in byfn e2e script
rameshthoomu Jun 29, 2017
9cd3f54
Merge "[FAB-5303] Further balance-transfer code optimization"
yacovm Aug 9, 2017
bb84299
Merge "[FAB-5155]Fix README.md in "balance-transfer" example"
jimthematrix Aug 9, 2017
419a083
Merge "FAB-5056 enable couchdb test in byfn e2e script"
binhn Aug 10, 2017
b19816b
updated byfn.sh to pass DELAY to docker compose in networkUp exec (fi…
Aug 15, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions balance-transfer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A sample Node.js app to demonstrate **__fabric-client__** & **__fabric-ca-client
* Download docker images

```
cd fabric-sdk-node/examples/balance-transfer/
cd fabric-samples/balance-transfer/
docker-compose -f artifacts/docker-compose.yaml pull
```

Expand Down Expand Up @@ -40,15 +40,29 @@ docker-compose -f artifacts/docker-compose.yaml up
```
##### Terminal Window 2

* Execute the REST APIs from the section [Sample REST APIs Requests](https://github.com/hyperledger/fabric-sdk-node/tree/master/examples/balance-transfer#running-the-sample-program)
* Install the fabric-client and fabric-ca-client node modules

```
npm install
```

* Start the node app on PORT 4000

```
PORT=4000 node app
```

##### Terminal Window 3

* Execute the REST APIs from the section [Sample REST APIs Requests](https://github.com/hyperledger/fabric-samples/tree/master/balance-transfer#sample-rest-apis-requests)


### Option 2:

##### Terminal Window 1

```
cd fabric-sdk-node/examples/balance-transfer
cd fabric-samples/balance-transfer

./runApp.sh

Expand All @@ -67,7 +81,7 @@ instructions [https://stedolan.github.io/jq/](https://stedolan.github.io/jq/)

With the application started in terminal 1, next, test the APIs by executing the script - **testAPIs.sh**:
```
cd fabric-sdk-node/examples/balance-transfer
cd fabric-samples/balance-transfer

./testAPIs.sh

Expand Down Expand Up @@ -117,7 +131,7 @@ curl -s -X POST \
-H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTQ4NjU1OTEsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTQ4NjE5OTF9.yWaJhFDuTvMQRaZIqg20Is5t-JJ_1BP58yrNLOKxtNI" \
-H "content-type: application/json" \
-d '{
"peers": ["localhost:7051","localhost:7056"]
"peers": ["peer1","peer2"]
}'
```
### Install chaincode
Expand All @@ -128,7 +142,7 @@ curl -s -X POST \
-H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTQ4NjU1OTEsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTQ4NjE5OTF9.yWaJhFDuTvMQRaZIqg20Is5t-JJ_1BP58yrNLOKxtNI" \
-H "content-type: application/json" \
-d '{
"peers": ["localhost:7051","localhost:7056"],
"peers": ["peer1","peer2"],
"chaincodeName":"mycc",
"chaincodePath":"github.com/example_cc",
"chaincodeVersion":"v0"
Expand All @@ -143,10 +157,8 @@ curl -s -X POST \
-H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTQ4NjU1OTEsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTQ4NjE5OTF9.yWaJhFDuTvMQRaZIqg20Is5t-JJ_1BP58yrNLOKxtNI" \
-H "content-type: application/json" \
-d '{
"peers": ["localhost:7051"],
"chaincodeName":"mycc",
"chaincodeVersion":"v0",
"functionName":"init",
"args":["a","100","b","200"]
}'
```
Expand All @@ -159,7 +171,6 @@ curl -s -X POST \
-H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0OTQ4NjU1OTEsInVzZXJuYW1lIjoiSmltIiwib3JnTmFtZSI6Im9yZzEiLCJpYXQiOjE0OTQ4NjE5OTF9.yWaJhFDuTvMQRaZIqg20Is5t-JJ_1BP58yrNLOKxtNI" \
-H "content-type: application/json" \
-d '{
"peers": ["localhost:7051", "localhost:7056"],
"fcn":"move",
"args":["a","b","10"]
}'
Expand Down Expand Up @@ -232,7 +243,7 @@ curl -s -X GET \

### Network configuration considerations

You have the ability to change configuration parameters by editing the network-config.json file.
You have the ability to change configuration parameters by either directly editing the network-config.json file or provide an additional file for an alternative target network. The app uses an optional environment variable "TARGET_NETWORK" to control the configuration files to use. For example, if you deployed the target network on Amazon Web Services EC2, you can add a file "network-config-aws.json", and set the "TARGET_NETWORK" environment to 'aws'. The app will pick up the settings inside the "network-config-aws.json" file.

#### IP Address** and PORT information

Expand Down
25 changes: 10 additions & 15 deletions balance-transfer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ var expressJWT = require('express-jwt');
var jwt = require('jsonwebtoken');
var bearerToken = require('express-bearer-token');
var cors = require('cors');
var config = require('./config.json');

require('./config.js');
var hfc = require('fabric-client');

var helper = require('./app/helper.js');
var channels = require('./app/create-channel.js');
var join = require('./app/join-channel.js');
var install = require('./app/install-chaincode.js');
var instantiate = require('./app/instantiate-chaincode.js');
var invoke = require('./app/invoke-transaction.js');
var query = require('./app/query.js');
var host = process.env.HOST || config.host;
var port = process.env.PORT || config.port;
var host = process.env.HOST || hfc.getConfigSetting('host');
var port = process.env.PORT || hfc.getConfigSetting('port');
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// SET CONFIGURATONS ////////////////////////////
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -118,7 +121,7 @@ app.post('/users', function(req, res) {
return;
}
var token = jwt.sign({
exp: Math.floor(Date.now() / 1000) + parseInt(config.jwt_expiretime),
exp: Math.floor(Date.now() / 1000) + parseInt(hfc.getConfigSetting('jwt_expiretime')),
username: username,
orgName: orgName
}, app.get('secret'));
Expand Down Expand Up @@ -216,12 +219,12 @@ app.post('/channels/:channelName/chaincodes', function(req, res) {
var chaincodeName = req.body.chaincodeName;
var chaincodeVersion = req.body.chaincodeVersion;
var channelName = req.params.channelName;
var functionName = req.body.functionName;
var fcn = req.body.fcn;
var args = req.body.args;
logger.debug('channelName : ' + channelName);
logger.debug('chaincodeName : ' + chaincodeName);
logger.debug('chaincodeVersion : ' + chaincodeVersion);
logger.debug('functionName : ' + functionName);
logger.debug('fcn : ' + fcn);
logger.debug('args : ' + args);
if (!chaincodeName) {
res.json(getErrorMessage('\'chaincodeName\''));
Expand All @@ -235,15 +238,11 @@ app.post('/channels/:channelName/chaincodes', function(req, res) {
res.json(getErrorMessage('\'channelName\''));
return;
}
if (!functionName) {
res.json(getErrorMessage('\'functionName\''));
return;
}
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
instantiate.instantiateChaincode(channelName, chaincodeName, chaincodeVersion, functionName, args, req.username, req.orgname)
instantiate.instantiateChaincode(channelName, chaincodeName, chaincodeVersion, fcn, args, req.username, req.orgname)
.then(function(message) {
res.send(message);
});
Expand All @@ -260,10 +259,6 @@ app.post('/channels/:channelName/chaincodes/:chaincodeName', function(req, res)
logger.debug('chaincodeName : ' + chaincodeName);
logger.debug('fcn : ' + fcn);
logger.debug('args : ' + args);
if (!peers || peers.length == 0) {
res.json(getErrorMessage('\'peers\''));
return;
}
if (!chaincodeName) {
res.json(getErrorMessage('\'chaincodeName\''));
return;
Expand Down
123 changes: 45 additions & 78 deletions balance-transfer/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ var fs = require('fs-extra');
var User = require('fabric-client/lib/User.js');
var crypto = require('crypto');
var copService = require('fabric-ca-client');
var config = require('../config.json');

var hfc = require('fabric-client');
hfc.addConfigFile(path.join(__dirname, 'network-config.json'));
hfc.setLogger(logger);
var ORGS = hfc.getConfigSetting('network-config');

Expand All @@ -44,7 +42,7 @@ for (let key in ORGS) {
cryptoSuite.setCryptoKeyStore(hfc.newCryptoKeyStore({path: getKeyStoreForOrg(ORGS[key].name)}));
client.setCryptoSuite(cryptoSuite);

let channel = client.newChannel(config.channelName);
let channel = client.newChannel(hfc.getConfigSetting('channelName'));
channel.addOrderer(newOrderer(client));

clients[key] = client;
Expand All @@ -58,27 +56,26 @@ for (let key in ORGS) {
}

function setupPeers(channel, org, client) {
for (let key in ORGS[org]) {
if (key.indexOf('peer') === 0) {
let data = fs.readFileSync(path.join(__dirname, ORGS[org][key]['tls_cacerts']));
let peer = client.newPeer(
ORGS[org][key].requests,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[org][key]['server-hostname']
}
);

channel.addPeer(peer);
}
for (let key in ORGS[org].peers) {
let data = fs.readFileSync(path.join(__dirname, ORGS[org].peers[key]['tls_cacerts']));
let peer = client.newPeer(
ORGS[org].peers[key].requests,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[org].peers[key]['server-hostname']
}
);
peer.setName(key);

channel.addPeer(peer);
}
}

function newOrderer(client) {
var caRootsPath = ORGS.orderer.tls_cacerts;
let data = fs.readFileSync(path.join(__dirname, caRootsPath));
let caroots = Buffer.from(data).toString();
return client.newOrderer(config.orderer, {
return client.newOrderer(ORGS.orderer.url, {
'pem': caroots,
'ssl-target-name-override': ORGS.orderer['server-hostname']
});
Expand All @@ -100,60 +97,36 @@ function getOrgName(org) {
}

function getKeyStoreForOrg(org) {
return config.keyValueStore + '_' + org;
return hfc.getConfigSetting('keyValueStore') + '_' + org;
}

function newRemotes(urls, forPeers, userOrg) {
var targets = [];
// find the peer that match the urls
outer:
for (let index in urls) {
let peerUrl = urls[index];

let found = false;
for (let key in ORGS) {
if (key.indexOf('org') === 0) {
// if looking for event hubs, an app can only connect to
// event hubs in its own org
if (!forPeers && key !== userOrg) {
continue;
}

let org = ORGS[key];
let client = getClientForOrg(key);

for (let prop in org) {
if (prop.indexOf('peer') === 0) {
if (org[prop]['requests'].indexOf(peerUrl) >= 0) {
// found a peer matching the subject url
if (forPeers) {
let data = fs.readFileSync(path.join(__dirname, org[prop]['tls_cacerts']));
targets.push(client.newPeer('grpcs://' + peerUrl, {
pem: Buffer.from(data).toString(),
'ssl-target-name-override': org[prop]['server-hostname']
}));

continue outer;
} else {
let eh = client.newEventHub();
let data = fs.readFileSync(path.join(__dirname, org[prop]['tls_cacerts']));
eh.setPeerAddr(org[prop]['events'], {
pem: Buffer.from(data).toString(),
'ssl-target-name-override': org[prop]['server-hostname']
});
targets.push(eh);

continue outer;
}
}
}
}
function newRemotes(names, forPeers, userOrg) {
let client = getClientForOrg(userOrg);

let targets = [];
// find the peer that match the names
for (let idx in names) {
let peerName = names[idx];
if (ORGS[userOrg].peers[peerName]) {
// found a peer matching the name
let data = fs.readFileSync(path.join(__dirname, ORGS[userOrg].peers[peerName]['tls_cacerts']));
let grpcOpts = {
pem: Buffer.from(data).toString(),
'ssl-target-name-override': ORGS[userOrg].peers[peerName]['server-hostname']
};

if (forPeers) {
targets.push(client.newPeer(ORGS[userOrg].peers[peerName].requests, grpcOpts));
} else {
let eh = client.newEventHub();
eh.setPeerAddr(ORGS[userOrg].peers[peerName].events, grpcOpts);
targets.push(eh);
}
}
}

if (!found) {
logger.error(util.format('Failed to find a peer matching the url %s', peerUrl));
}
if (targets.length === 0) {
logger.error(util.format('Failed to find peers matching the names %s', names));
}

return targets;
Expand All @@ -170,12 +143,12 @@ var getClientForOrg = function(org) {
return clients[org];
};

var newPeers = function(urls) {
return newRemotes(urls, true);
var newPeers = function(names, org) {
return newRemotes(names, true, org);
};

var newEventHubs = function(urls, org) {
return newRemotes(urls, false, org);
var newEventHubs = function(names, org) {
return newRemotes(names, false, org);
};

var getMspID = function(org) {
Expand All @@ -184,7 +157,7 @@ var getMspID = function(org) {
};

var getAdminUser = function(userOrg) {
var users = config.users;
var users = hfc.getConfigSetting('admins');
var username = users[0].username;
var password = users[0].secret;
var member;
Expand Down Expand Up @@ -325,7 +298,7 @@ var getOrgAdmin = function(userOrg) {
};

var setupChaincodeDeploy = function() {
process.env.GOPATH = path.join(__dirname, config.GOPATH);
process.env.GOPATH = path.join(__dirname, hfc.getConfigSetting('CC_SRC_PATH'));
};

var getLogger = function(moduleName) {
Expand All @@ -334,11 +307,6 @@ var getLogger = function(moduleName) {
return logger;
};

var getPeerAddressByName = function(org, peer) {
var address = ORGS[org][peer].requests;
return address.split('grpcs://')[1];
};

exports.getChannelForOrg = getChannelForOrg;
exports.getClientForOrg = getClientForOrg;
exports.getLogger = getLogger;
Expand All @@ -347,6 +315,5 @@ exports.getMspID = getMspID;
exports.ORGS = ORGS;
exports.newPeers = newPeers;
exports.newEventHubs = newEventHubs;
exports.getPeerAddressByName = getPeerAddressByName;
exports.getRegisteredUsers = getRegisteredUsers;
exports.getOrgAdmin = getOrgAdmin;
6 changes: 3 additions & 3 deletions balance-transfer/app/install-chaincode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var installChaincode = function(peers, chaincodeName, chaincodePath,

return helper.getOrgAdmin(org).then((user) => {
var request = {
targets: helper.newPeers(peers),
targets: helper.newPeers(peers, org),
chaincodePath: chaincodePath,
chaincodeId: chaincodeName,
chaincodeVersion: chaincodeVersion
Expand All @@ -47,8 +47,8 @@ var installChaincode = function(peers, chaincodeName, chaincodePath,
var all_good = true;
for (var i in proposalResponses) {
let one_good = false;
if (proposalResponses && proposalResponses[0].response &&
proposalResponses[0].response.status === 200) {
if (proposalResponses && proposalResponses[i].response &&
proposalResponses[i].response.status === 200) {
one_good = true;
logger.info('install proposal was good');
} else {
Expand Down
Loading