Skip to content

Commit

Permalink
check if cc is compaitible
Browse files Browse the repository at this point in the history
  • Loading branch information
dshuffma-ibm committed Apr 3, 2017
1 parent 0f245d1 commit 2c3c5c4
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 37 deletions.
43 changes: 15 additions & 28 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,6 @@ console.log('------------------------------------------ Server Up - ' + host + '
if (process.env.PRODUCTION) logger.debug('Running using Production settings');
else logger.debug('Running using Developer settings');

if (helper.getNetworkId() === 'FakeNetworkId') {
console.log('\n\n');
logger.warn('----------------------------------------------------------------------');
logger.warn('----------------------------- Hey Buddy! -----------------------------');
logger.warn('------------------------ It looks like you did -----------------------');
logger.error('------------------------------- not --------------------------------');
logger.warn('------------------------- follow my instructions ---------------------');
logger.warn('----------------------------------------------------------------------');
logger.warn('Your network config JSON has a network ID of "FakeNetworkID"...');
logger.warn('You likely have other settings that are wrong too!');
logger.warn('----------------------------------------------------------------------');
logger.error('Fix this file: ' + helper.getNetworkCredFileName());
logger.warn('It must have credentials/hostnames/ports/channels/etc for YOUR network');
logger.warn('How/where would I get that info? Using the Bluemix service? Then look at these instructions(near the end): ');
logger.warn(' https://github.com/IBM-Blockchain/marbles/blob/v3.0/docs/install_chaincode.md');
logger.warn('----------------------------------------------------------------------');
console.log('\n\n');
}

// ============================================================================================================================
// Warning
// ============================================================================================================================
Expand All @@ -141,6 +122,7 @@ if (helper.getNetworkId() === 'FakeNetworkId') {
// ------------------------------------------------------------------------------------------------------------------------------
process.env.app_state = 'starting';
process.env.app_first_setup = 'yes';
helper.checkConfig();
setupWebSocket();

var hash = helper.getMarbleStartUpHash();
Expand Down Expand Up @@ -193,12 +175,17 @@ function setup_marbles_lib() {
broadcast_state('no_chaincode');
}
else { //else we already deployed
console.log('\n------------------------------------------ Chaincode Found ------------------------------------------\n');
broadcast_state('found_chaincode');

var user_base = null;
if (process.env.app_first_setup === 'yes') user_base = helper.getMarbleUsernames();
create_assets(user_base); //builds marbles, then starts webapp
console.log('\n----------------------------- Chaincode Found on Channel ' + helper.getChannelId() + ' -----------------------------\n');

// --- Check Chaincode Compatibility --- //
marbles_lib.check_version(options, function (err, resp) {
if (!helper.errorWithVersions(resp)) {
broadcast_state('found_chaincode');
var user_base = null;
if (process.env.app_first_setup === 'yes') user_base = helper.getMarbleUsernames();
create_assets(user_base); //builds marbles, then starts webapp
}
});
}
});
}
Expand Down Expand Up @@ -273,7 +260,7 @@ function create_assets(build_marbles_users) {

// --- Create Each User --- //
create_owners(0, username, function (errCode, resp) {
owners.push({id: resp.id, username: username});
owners.push({ id: resp.id, username: username });
owner_cb();
});

Expand All @@ -283,8 +270,8 @@ function create_assets(build_marbles_users) {

var marbles = [];
var marblesEach = 3; //number of marbles each owner gets
for(var i in owners){
for(var x=0; x < marblesEach; x++){
for (var i in owners) {
for (var x = 0; x < marblesEach; x++) {
marbles.push(owners[i]);
}
}
Expand Down
6 changes: 6 additions & 0 deletions chaincode/src/marbles/marbles.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Error("Expecting a numeric string argument to Init()")
}

// store compaitible marbles application version
err = stub.PutState("marbles_ui", []byte("4.0.0"))
if err != nil {
return shim.Error(err.Error())
}

// this is a very simple dumb test. let's write to the ledger and error on any errors
err = stub.PutState("selftest", []byte(strconv.Itoa(Aval))) //making a test var "selftest", its handy to read this right away to test the network
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "marbles",
"version": "3.1.0",
"version": "4.0.0",
"description": "A demonstration Node.js web application on Hyperledger Fabric.",
"main": "app.js",
"scripts": {
Expand Down
16 changes: 10 additions & 6 deletions utils/fc_wrangler/query_cc.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,22 @@ module.exports = function (logger) {
}

try {
if (as_string === '') {
if (as_string === '') { //if its empty, thats okay... well its not great
as_obj = '';
} else {
as_obj = JSON.parse(as_string); //if we can parse it, its great
}
else {
as_obj = JSON.parse(as_string);
}
//logger.debug('[fcw] Peer ' + i, 'payload as self:', as_obj, 'type', typeof as_obj);
logger.debug('[fcw] Peer ' + i, 'type', typeof as_obj);
if (ret.parsed === null) ret.parsed = as_obj; //store the first one here
}
catch (e) {
logger.warn('[fcw] warning - could not json parse query response');
if (as_string.indexOf('Error: failed to obtain') >= 0) {
logger.error('[fcw] query resp looks like an error', typeof as_string, as_string);
ret.parsed = null;
} else {
logger.warn('[fcw] warning - query resp is not json, might be okay.', typeof as_string, as_string);
ret.parsed = as_string;
}
}
}
return ret;
Expand Down
46 changes: 46 additions & 0 deletions utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function (config_filename, logger) {
helper.config = require(config_path);
var creds_path = path.join(__dirname, '../config/' + helper.config.cred_filename);
helper.creds = require(creds_path);
var packagejson = require(path.join(__dirname, '../package.json'));

logger.info('Loaded config file', config_path);
logger.info('Loaded creds file', creds_path);
Expand Down Expand Up @@ -319,5 +320,50 @@ module.exports = function (config_filename, logger) {
helper.config = config_file; //replace old copy
};



// check if user has changed the settings from the default ones
helper.checkConfig = function () {
if (helper.getNetworkId() === 'FakeNetworkId') {
console.log('\n\n');
logger.warn('----------------------------------------------------------------------');
logger.warn('----------------------------- Hey Buddy! -----------------------------');
logger.warn('------------------------ It looks like you did -----------------------');
logger.error('------------------------------- not --------------------------------');
logger.warn('------------------------- follow my instructions ---------------------');
logger.warn('----------------------------------------------------------------------');
logger.warn('Your network config JSON has a network ID of "FakeNetworkID"...');
logger.warn('You likely have other settings that are wrong too!');
logger.warn('----------------------------------------------------------------------');
logger.error('Fix this file: ' + helper.getNetworkCredFileName());
logger.warn('It must have credentials/hostnames/ports/channels/etc for YOUR network');
logger.warn('How/where would I get that info? Using the Bluemix service? Then look at these instructions(near the end): ');
logger.warn(' https://github.com/IBM-Blockchain/marbles/blob/v3.0/docs/install_chaincode.md');
logger.warn('----------------------------------------------------------------------');
console.log('\n\n');
}
};

// check if marbles UI and marbles chaincode work together
helper.errorWithVersions = function (v) {
var version = packagejson.version;
if (!v || !v.parsed) v = { parsed: '3.x.x' }; //default
if (v.parsed[0] !== version[0]) { //only check the major version
console.log('\n\n');
logger.warn('---------------------------------------------------------------');
logger.warn('----------------------------- Ah! -----------------------------');
logger.warn('---------------------------------------------------------------');
logger.error('Looks like you are using an old version of marbles chaincode: v' + v.parsed);
logger.warn('This UI is expecting chaincode version: v' + version[0] + '.x.x');
logger.warn('Install and instantiate v' + version[0] + '.x.x' + ' chaincode on channel ' + helper.getChannelId());
logger.warn('----------------------------------------------------------------------');
console.log('\n\n');
console.log('killing self');
process.exit(); //we die here
return true;
}
return false;
};

return helper;
};
31 changes: 29 additions & 2 deletions utils/marbles_cc_lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (enrollObj, g_options, fcw, logger) {

// Chaincode -------------------------------------------------------------------------------

//check chaincode
//check if chaincode exists
marbles_chaincode.check_if_already_deployed = function (options, cb) {
console.log('');
logger.info('Checking for chaincode...');
Expand All @@ -17,7 +17,34 @@ module.exports = function (enrollObj, g_options, fcw, logger) {
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
cc_function: 'read',
cc_args: ['abc']
cc_args: ['selftest']
};
fcw.query_chaincode(enrollObj, opts, function (err, resp) {
if (err != null) {
if (cb) return cb(err, resp);
}
else {
if (resp.parsed == null || isNaN(resp.parsed)) { //if nothing is here, no chaincode
if (cb) return cb({ error: 'chaincode not found' }, resp);
}
else {
if (cb) return cb(null, resp);
}
}
});
};

//check chaincode version
marbles_chaincode.check_version = function (options, cb) {
console.log('');
logger.info('Checking chaincode and ui compatibility...');

var opts = {
channel_id: g_options.channel_id,
chaincode_id: g_options.chaincode_id,
chaincode_version: g_options.chaincode_version,
cc_function: 'read',
cc_args: ['marbles_ui']
};
fcw.query_chaincode(enrollObj, opts, function (err, resp) {
if (err != null) {
Expand Down

0 comments on commit 2c3c5c4

Please sign in to comment.