diff --git a/composer-logs/trace_20171102-104326-214.log b/composer-logs/trace_20171102-104326-214.log new file mode 100644 index 0000000..fd8ebc2 --- /dev/null +++ b/composer-logs/trace_20171102-104326-214.log @@ -0,0 +1,2 @@ +2017-11-01T16:43:33.681Z ERROR HLFConnectionManager :fabric-client() [client-utils.js]: sendPeersProposal - Promise is rejected: Error: chaincode error (status: 500, message: Error installing chaincode code lyra-cli:0.14.1(chaincode /var/hyperledger/production/chaincodes/lyra-cli.0.14.1 exists)) + at /usr/local/lib/node_modules/composer-cli/node_modules/fabric-client/node_modules/grpc/src/node/src/client.js:434:17 {}$ diff --git a/composer-logs/trace_20171102-104350-957.log b/composer-logs/trace_20171102-104350-957.log new file mode 100644 index 0000000..51cfdf6 --- /dev/null +++ b/composer-logs/trace_20171102-104350-957.log @@ -0,0 +1,4 @@ +2017-11-01T16:43:52.582Z ERROR HLFConnectionManager :fabric-client() [client-utils.js]: sendPeersProposal - Promise is rejected: Error: chaincode error (status: 500, message: Error: The current identity must be activated (ACTIVATION_REQUIRED)) + at /usr/local/lib/node_modules/composer-cli/node_modules/fabric-client/node_modules/grpc/src/node/src/client.js:434:17 {}$ +2017-11-01T16:43:52.583Z ERROR HLFConnection :queryChainCode() {"message":"Error trying to query business network. Error: chaincode error (status: 500, message: Error: The current identity must be activated (ACTIVATION_REQUIRED))","stack":"Error: Error trying to query business network. Error: chaincode error (status: 500, message: Error: The current identity must be activated (ACTIVATION_REQUIRED))\n at channel.queryByChaincode.then.catch (/usr/local/lib/node_modules/composer-cli/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:749:34)\n at "}$ +2017-11-01T16:43:52.583Z ERROR HLFConnection :ping() {"message":"Error trying to ping. Error: Error trying to query business network. Error: chaincode error (status: 500, message: Error: The current identity must be activated (ACTIVATION_REQUIRED))","stack":"Error: Error trying to ping. Error: Error trying to query business network. Error: chaincode error (status: 500, message: Error: The current identity must be activated (ACTIVATION_REQUIRED))\n at _checkRuntimeVersions.then.catch (/usr/local/lib/node_modules/composer-cli/node_modules/composer-connector-hlfv1/lib/hlfconnection.js:663:34)\n at "}$ diff --git a/index.js b/index.js index e02914e..0fb62b1 100755 --- a/index.js +++ b/index.js @@ -81,6 +81,13 @@ var yargs = require('yargs') alias: 't' } }) + .command('ledger', 'Checks if ledger is synced', { + transactions: { + description: 'Amount of transactions to launch into the ledger', + require: true, + alias: 't' + } + }) .help() .argv; @@ -183,13 +190,33 @@ switch (yargs._[0]) { case 'cannon': console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017')); hyper.transactionCannon(yargs.transactions) - .then(()=>{ - process.exit(0); - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - process.exit(1); - }) + .then(() => { + process.exit(0); + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + process.exit(1); + }) + break; + + case 'ledger': + console.log(chalk.bold.cyan('Lyra CLI App'), chalk.bold.green('Made by Aabo Technologies © 2017')); + hyper.getTransactionSchedule(yargs.transactions) + .then((schedule) => { + return hyper.isLedgerStateCorrect(schedule); + }) + .then((result) => { + if (result) { + console.log(chalk.green('Success!')); + } else { + console.log(chalk.red('Not Success')); + } + process.exit(0); + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + process.exit(1); + }); break; default: @@ -227,7 +254,7 @@ function author() { /**@description Shows the menu with the main options * @returns {Nothing} -*/ + */ function mainMenu() { /** Displays the main menu */ @@ -313,7 +340,7 @@ function debugMenu() { .then(() => { debugMenu(); }) - .catch(function(error){ + .catch(function (error) { console.log('An error occured: ', chalk.bold.red(error)); process.exit(1); }); @@ -323,7 +350,7 @@ function debugMenu() { .then(() => { debugMenu(); }) - .catch(function(error){ + .catch(function (error) { console.log('An error occured: ', chalk.bold.red(error)); process.exit(1); }); @@ -336,7 +363,7 @@ function debugMenu() { .then(() => { debugMenu(); }) - .catch(function(error){ + .catch(function (error) { console.log('An error occured: ', chalk.bold.red(error)); process.exit(1); }); @@ -346,7 +373,7 @@ function debugMenu() { .then(() => { debugMenu(); }) - .catch(function(error){ + .catch(function (error) { console.log('An error occured: ', chalk.bold.red(error)); process.exit(1); }); @@ -373,7 +400,7 @@ function testingMenu() { }) ) ); - + var questions = [{ type: "list", name: "initial", @@ -388,19 +415,19 @@ function testingMenu() { }]; inquirer.prompt(questions).then(function (answers) { - switch (answers.initial){ + switch (answers.initial) { case 'Create batch accounts': batchCreation('test'); - break; + break; case 'Start the transaction cannon': startTheCannon(); - break; + break; case 'Go back to the main menu': - clear(); - mainMenu(); - break; + clear(); + mainMenu(); + break; } }); } @@ -409,7 +436,7 @@ function testingMenu() { * @returns {Nothing} */ -function startTheCannon(){ +function startTheCannon() { var questions = [{ type: 'input', name: 'transactions', @@ -418,13 +445,13 @@ function startTheCannon(){ }]; inquirer.prompt(questions).then(function (answers) { hyper.transactionCannon(answers.transactions) - .then(()=>{ - testingMenu(); - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - process.exit(1); - }); + .then(() => { + testingMenu(); + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + process.exit(1); + }); }); } @@ -455,18 +482,18 @@ function batchCreation(whereTo) { ]; inquirer.prompt(questions).then(function (answers) { - hyper.batchAccount(answers.clientNumber,answers.bottom,answers.top) - .then(()=>{ - if('debug'){ - debugMenu(); - }else{ - testingMenu(); - } - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - process.exit(1); - }); + hyper.batchAccount(answers.clientNumber, answers.bottom, answers.top) + .then(() => { + if ('debug') { + debugMenu(); + } else { + testingMenu(); + } + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + process.exit(1); + }); }); } @@ -495,7 +522,7 @@ function makeTransaction() { .then(() => { mainMenu(); }) - .catch(function(error){ + .catch(function (error) { console.log('An error occured: ', chalk.bold.red(error)); process.exit(1); }); diff --git a/lib/blockchainManager.js b/lib/blockchainManager.js index 230760e..f92eece 100644 --- a/lib/blockchainManager.js +++ b/lib/blockchainManager.js @@ -139,7 +139,7 @@ class BlockchainManager { client = factory.newResource('org.aabo', 'Client', md5(clientSeed)); client.id = md5(clientSeed); /**Save to MDB */ - // mongo.savePnt(client); + mongo.savePnt(client); /** Create a new relationship for the owner */ ownerRelation = factory.newRelationship('org.aabo', 'Client', md5(clientSeed)); /** Create a new wallet for the owner */ @@ -148,7 +148,7 @@ class BlockchainManager { wallet.balance = (Math.random() * top) + bottom; wallet.owner = ownerRelation; /**Save to MDB */ - // mongo.saveAst(wallet); + mongo.saveAst(wallet); /** Save the new state of this relationship to the Blockchain */ return this.walletRegistry.add(wallet); }) @@ -203,6 +203,28 @@ class BlockchainManager { }); } + /**@name rawAssetsOnLedger + * @author Aabo Technologies © 2017 - Server's team + * @description Returns all assets on the ledger in raw form + * @returns {Promise} A promise whose fullfillment means all wallets have succesfully been returned + */ + + rawAssetsOnLedger() { + const METHOD = 'rawAssetsOnLedger'; + let walletRegistry; + + return this.businessNetworkConnection.getAssetRegistry('org.aabo.Wallet') + .then((registry) => { + return registry.resolveAll(); + }) + .then((result) => { + return result; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); + } + /**@name ShowCurrentParticipants * @author Aabo Technologies © 2017 - Server's team * @description Lists all current Participants on the ledger @@ -286,7 +308,7 @@ class BlockchainManager { "owner": "resource:org.aabo.Client#" + to.owner.getIdentifier() } }); - // mongo.saveTx(resource); + // mongo.saveTx(resource); return this.businessNetworkConnection.submitTransaction(resource);; }) .then(() => { @@ -313,8 +335,8 @@ class BlockchainManager { .then((aResources) => { let schedule = []; for (let i = 1; i <= simTrax; i++) { - var fromRandomUser = (2*i)-1; - var toRandomUser = (2*i); + var fromRandomUser = (2 * i) - 1; + var toRandomUser = (2 * i); var fundsRandom = (Math.random() * 1000) + 100; schedule.push({ from: aResources[fromRandomUser].id, @@ -341,18 +363,18 @@ class BlockchainManager { * @returns {Number} opsPerDay is the amount of operations that could be done in one day */ - profilingTime(timeStart,timeEnd,simTrax,whereFrom){ + profilingTime(timeStart, timeEnd, simTrax, whereFrom) { const METHOD = 'profilingTime'; - let executedTime = (timeEnd-timeStart).toFixed(0); + let executedTime = (timeEnd - timeStart).toFixed(0); let opTime = (executedTime / simTrax).toFixed(0); let opsPerDay = (86400000 / opTime).toFixed(0); - let exp = (opsPerDay.toString().length)-1; - console.log(chalk.bold.yellow('EXECUTION TIME:'),chalk.white(executedTime),'ms'); - console.log(chalk.bold.yellow('OPERATION TIME:'),chalk.white(opTime),'ms'); - console.log(chalk.bold.yellow('OPS PER DAY:'),chalk.white(opsPerDay),'or:',chalk.bold.yellow('1E'+exp)); - if(whereFrom === 'tx'){ - if(exp < 9 ){ + let exp = (opsPerDay.toString().length) - 1; + console.log(chalk.bold.yellow('EXECUTION TIME:'), chalk.white(executedTime), 'ms'); + console.log(chalk.bold.yellow('OPERATION TIME:'), chalk.white(opTime), 'ms'); + console.log(chalk.bold.yellow('OPS PER DAY:'), chalk.white(opsPerDay), 'or:', chalk.bold.yellow('1E' + exp)); + if (whereFrom === 'tx') { + if (exp < 9) { console.log( chalk.yellow( figlet.textSync('Not yet...', { @@ -361,7 +383,7 @@ class BlockchainManager { }) ) ); - }else{ + } else { console.log( chalk.green( figlet.textSync('DONE~!', { @@ -535,7 +557,7 @@ class BlockchainManager { }) .then((arr) => { timeEnd = now().toFixed(0); - bm.profilingTime(timeStart,timeEnd,amount,'acc'); + bm.profilingTime(timeStart, timeEnd, amount, 'acc'); console.log('Accounts created successfully!'); }) .catch(function (error) { @@ -572,33 +594,33 @@ class BlockchainManager { * @returns {Promise} that it will try it's best, don't sweat it */ - static transactionCannon(simTrax){ + static transactionCannon(simTrax) { let bm = new BlockchainManager(); //**Set up the time start */ let timeStart; let timeEnd; //**Set up the time end */ return bm.init() - .then(()=>{ - return bm.transactionSchedule(simTrax); - }) - .then((result)=>{ - let cannonBalls = []; - for (let i = 0; i < result.length ; i++){ - cannonBalls.push(bm.makeTransaction(result[i].from,result[i].to,result[i].funds)); - } - timeStart = now().toFixed(0); - return Promise.all(cannonBalls); - }) - .then(()=>{ - timeEnd = now().toFixed(0); - bm.profilingTime(timeStart,timeEnd,simTrax,'tx'); - console.log('Transaction Cannon Finished'); - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - process.exit(1); - }); + .then(() => { + return bm.transactionSchedule(simTrax); + }) + .then((result) => { + let cannonBalls = []; + for (let i = 0; i < result.length; i++) { + cannonBalls.push(bm.makeTransaction(result[i].from, result[i].to, result[i].funds)); + } + timeStart = now().toFixed(0); + return Promise.all(cannonBalls); + }) + .then(() => { + timeEnd = now().toFixed(0); + bm.profilingTime(timeStart, timeEnd, simTrax, 'tx'); + console.log('Transaction Cannon Finished'); + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + process.exit(1); + }); } /**@name isLedgerStateCorrect @@ -608,10 +630,42 @@ class BlockchainManager { * @returns {Boolean} if the ledger is indeed synced or not */ - static isLedgerStateCorrect(schedule){ - - } - + static isLedgerStateCorrect(schedule) { + let bm = new BlockchainManager(); + let modState; + /** Get the state of the database */ + console.log(schedule); + return mdb.getAllAst() + .then((result) => { + for (let i = 0; i < schedule.length; i++) { + for (let x = 0; x < result.length; x++) { + if (result[x].id == schedule[i].from) { + result[x].balance -= schedule[i].funds; + } + if (result[x].id == schedule[i].to) { + result[x].balance += schedule[i].funds; + } + } + } + modState = result; + return bm.rawAssetsOnLedger(); + }) + .then((rawLedger) => { + let state = true; + for (let i = 0; i < rawLedger.length; i++) { + for (let x = 0; x < modState.length; x++) { + if (rawLedger[i].id == modState[x].id && rawLedger[i].balance != modState[x].balance) { + state = false; + } + } + } + return state; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + process.exit(1); + }); + } } module.exports = BlockchainManager; \ No newline at end of file diff --git a/lib/mongoManager.js b/lib/mongoManager.js index 61c97b0..241da97 100644 --- a/lib/mongoManager.js +++ b/lib/mongoManager.js @@ -22,8 +22,6 @@ var config = require('config').get('mongo-connection'); /** Get data from the configuration files */ let connectionURI = config.get('mongoURI'); -let databaseName = config.get('mongoDatabase'); - /** Get the Schemas */ var { Participant @@ -35,17 +33,25 @@ var { Wallet } = require('../Schemas/wallet'); -/** Get the configuration for the database location */ -var config = require('config').get('mongo-connection'); -let mongoURI = config.get("mongoURI"); - //Start the connection to Mongoose mongoose.Promise = global.Promise; -mongoose.connect(mongoURI, { +mongoose.connect(connectionURI, { useMongoClient: true }); class MongoManager { + constructor() { + mongoose.connect(connectionURI, { + useMongoClient: true + }) + .then((result)=>{ + console.log('Connected to MongoDB'); + }) + .catch(function(error){ + console.log('an error ocurred'); + }); + } + /**@description Persists a transaction into MongoDB * @param {JSON} a JSON Document with transaction information * @return {Promise} whose fulfillment means the transaction has been saved @@ -103,7 +109,6 @@ class MongoManager { saveAsset(jsonDoc, idOwner) { const METHOD = 'saveAsset'; - console.log(jsonDoc); var wallet = new Wallet({ id: jsonDoc.id, balance: jsonDoc.balance, @@ -153,7 +158,7 @@ class MongoManager { */ getAllAssets() { - return Wallet.find({}) + return Wallet.find({}) .then((result) => { return result; }) @@ -168,7 +173,7 @@ class MongoManager { */ getOneAsset(identifier) { - return Wallet.findOne({ + return Wallet.findOne({ id: identifier }) .then((result) => { @@ -185,7 +190,7 @@ class MongoManager { */ getOneParticipant(identifier) { - return Participant.findOne({ + return Participant.findOne({ id: identifier }) .then((result) => { @@ -234,7 +239,7 @@ class MongoManager { static saveAst(jsonDoc, idOwner) { let mong = new MongoManager(); - return mong.saveAsset(jsonDoc,idOwner); + return mong.saveAsset(jsonDoc, idOwner); } /**@description Executes the Get All Transaction command @@ -244,12 +249,12 @@ class MongoManager { static getAllTx() { let mong = new MongoManager(); return mong.getAllTransactions() - .then((result)=>{ - return result; - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - }); + .then((result) => { + return result; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); } /**@description Executes the Get All Participants command @@ -259,12 +264,12 @@ class MongoManager { static getAllPnt() { let mong = new MongoManager(); return mong.getAllParticipants() - .then((result)=>{ - return result; - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - }); + .then((result) => { + return result; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); } /**@description Executes the Get All Assets command @@ -274,12 +279,12 @@ class MongoManager { static getAllAst() { let mong = new MongoManager(); return mong.getAllAssets() - .then((result)=>{ - return result; - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - }); + .then((result) => { + return result; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); } /**@description Executes the Get One Asset command @@ -290,12 +295,12 @@ class MongoManager { static getOneAst(identifier) { let mong = new MongoManager(); return mong.getOneAsset(identifier) - .then((result)=>{ - return result; - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - }); + .then((result) => { + return result; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); } /**@description Executes the Get One Participant command @@ -306,28 +311,30 @@ class MongoManager { static getOnePnt(identifier) { let mong = new MongoManager(); return mong.getOneParticipant(identifier) - .then((result)=>{ - return result; - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - }); + .then((result) => { + return result; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); } /**@description Executes the All Asset's id command * @returns {Promise} whose fullfilment means either all assets were retrieved or none existed + * @deprecated Should be taken out by release 1.0 */ static getAllAstID() { let mong = new MongoManager(); return mong.getAllAssetsID() - .then((result)=>{ - return result; - }) - .catch(function(error){ - console.log('An error occured: ', chalk.bold.red(error)); - }); + .then((result) => { + return result; + }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); } + } module.exports = MongoManager; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0fb411a..07ef757 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "lyra-cli", - "version": "0.8.7", + "version": "0.8.8", "lockfileVersion": 1, "requires": true, "dependencies": {