From 4d728aebe8d16904e5b7f741b463c37cd53f0204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Mart=C3=ADn=20Garc=C3=ADa=20Del=20Angel?= Date: Wed, 1 Nov 2017 17:41:34 -0600 Subject: [PATCH] Release Candidate 3 Most of the changes from the previous version but now: + You can choose between persisting transactions or not into MongoDB + Some documentation has been cleansed --- blockchainManager.js | 76 ++++++++++++++++++++++++++++++++++++++++---- index.js | 23 ++++++++++++-- package.json | 2 +- 3 files changed, 90 insertions(+), 11 deletions(-) diff --git a/blockchainManager.js b/blockchainManager.js index c919f68..9af4afd 100644 --- a/blockchainManager.js +++ b/blockchainManager.js @@ -307,11 +307,64 @@ class BlockchainManager { "owner": "resource:org.aabo.Client#" + to.owner.getIdentifier() } }); - // mongo.saveTx(resource); return this.businessNetworkConnection.submitTransaction(resource);; }) + .catch(function (error) { + console.log('An error occured: ', chalk.bold.red(error)); + }); + } + + /** + * @name makeReplicatedTransaction + * @author Aabo Technologies © 2017 - Server's team + * @description This method makes a single transaction over the ledger + * @param {String} fromID is the md5 related to a Client's wallet on the ledger who's sending money + * @param {String} toID is the md5 related to a Client's wallet on the ledger who's receiving money + * @param {Number} funds is an amount of money to be sent from one wallet to the other + * @returns {Promise} whose fullfiment means a transaction was made succesfully + */ + + makeReplicatedTransaction(fromID,toID,funds){ + const METHOD = 'makeReplicatedTransaction'; + let from; + let walletRegistry; + let to; + let resource; + + return this.businessNetworkConnection.getAssetRegistry('org.aabo.Wallet') + .then((registry) => { + walletRegistry = registry; + return walletRegistry.get(fromID); + }) + .then((fromm) => { + from = fromm; + return walletRegistry.get(toID); + }) + .then((too) => { + to = too; + }) .then(() => { - //return mongo.saveTx(resource); + let serializer = this.businessNetworkDefinition.getSerializer(); + resource = serializer.fromJSON({ + "$class": "org.aabo.Transfer", + "amount": funds, + "from": { + "$class": "org.aabo.Wallet", + "id": from.getIdentifier(), + "balance": from.balance, + "owner": "resource:org.aabo.Client#" + from.owner.getIdentifier() + }, + "to": { + "$class": "org.aabo.Wallet", + "id": to.getIdentifier(), + "balance": to.balance, + "owner": "resource:org.aabo.Client#" + to.owner.getIdentifier() + } + }); + return this.businessNetworkConnection.submitTransaction(resource);; + }) + .then(() => { + return mongo.saveTx(resource); }) .catch(function (error) { console.log('An error occured: ', chalk.bold.red(error)); @@ -631,10 +684,11 @@ class BlockchainManager { * @author Aabo Technologies © 2017 - Server's team * @description This is it. This launches as many transactions as possible, it doesn't care about your feelings * @param {Number} amount of transactions to make + * @param {String} replica if the transaction information will be replicated * @returns {Promise} that it will try it's best, don't sweat it */ - static transactionCannon(simTrax) { + static transactionCannon(simTrax,replica) { /**Start the spinner */ const spinner = new ora({ text: 'Connecting to Hyperledger...', @@ -655,12 +709,20 @@ class BlockchainManager { return bm.transactionSchedule(simTrax); }) .then((result) => { - spinner.text = 'Breaking into the chain...'; - spinner.color = 'green'; let cannonBalls = []; schedule = result; - for (let i = 0; i < result.length; i++) { - cannonBalls.push(bm.makeTransaction(result[i].from, result[i].to, result[i].funds)); + if(replica.toLowerCase() == 'n'){ + for (let i = 0; i < result.length; i++) { + spinner.text = 'Breaking into the chain...'; + spinner.color = 'green'; + cannonBalls.push(bm.makeTransaction(result[i].from, result[i].to, result[i].funds)); + } + }else{ + for (let i = 0; i < result.length; i++) { + spinner.text = 'Merely tapping into the chain...'; + spinner.color = 'red'; + cannonBalls.push(bm.makeReplicatedTransaction(result[i].from, result[i].to, result[i].funds)); + } } timeStart = now().toFixed(0); return Promise.all(cannonBalls); diff --git a/index.js b/index.js index 187fd34..5697ef8 100755 --- a/index.js +++ b/index.js @@ -80,6 +80,12 @@ var yargs = require('yargs') description: 'Amount of transactions to launch into the ledger', require: true, alias: 't' + }, + replication: { + description : 'Replication of transactions into MongoDB', + require: false, + default : 'n', + alias :'r' } }) .command('ledger', 'Checks if ledger is synced', { @@ -200,7 +206,7 @@ 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) + hyper.transactionCannon(yargs.transactions,yargs.replication) .then((result) => { return hyper.isLedgerStateCorrect(result); }) @@ -513,14 +519,25 @@ function startTheCannon() { name: 'transactions', message: 'Amount of transactions to launch into the ledger', default: 1, + },{ + type: 'confirm', + name: 'replica', + message: 'Do you want the transactions to be replicated?', + default : false }]; inquirer.prompt(questions).then(function (answers) { - hyper.transactionCannon(answers.transactions) + let rep; + if(answers.replica){ + rep = 'y'; + } else{ + rep = 'n'; + } + hyper.transactionCannon(answers.transactions,rep) .then((schedule) => { return hyper.isLedgerStateCorrect(schedule); }) .then(() => { - debugMenu(); + testingMenu(); }) .catch(function (error) { console.log('An error occured: ', chalk.bold.red(error)); diff --git a/package.json b/package.json index ee264a6..3376786 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lyra-cli", - "version": "0.8.8", + "version": "1.0.0", "description": "Hyperledger Composer Super Fast Network", "author": "Aabo Technologies© 2017 - Server's Team", "main": "index.js",