From d386958d906897a5e86651e6c645da0ffc2b756c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 7 Feb 2018 17:19:00 -0500 Subject: [PATCH 1/6] move runTx code and related functions to an async waterfall --- src/universal-dapp.js | 76 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 9e14263018..2e57ab9f3e 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -1,6 +1,7 @@ /* global */ 'use strict' +var async = require('async') var ethJSUtil = require('ethereumjs-util') var BN = ethJSUtil.BN var remixLib = require('remix-lib') @@ -201,14 +202,63 @@ UniversalDApp.prototype.getInputs = function (funABI) { return txHelper.inputParametersDeclarationToString(funABI.inputs) } -function execute (pipeline, env, callback) { - function next (err, env) { - if (err) return callback(err) - var step = pipeline.shift() - if (step) step(env, next) - else callback(null, env.result) - } - next(null, env) +UniversalDApp.prototype.runTx = function (args, cb) { + const self = this + + var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: args.from, value: args.value } + var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName } // contains decoded parameters + + async.waterfall([ + function queryGasLimit (next) { + tx.gasLimit = 3000000 + if (self.transactionContextAPI.getGasLimit) { + self.transactionContextAPI.getGasLimit(function (err, ret) { + if (err) return next(err) + tx.gasLimit = ret + next() + }) + } else next() + }, + function queryValue (next) { + if (self.transactionContextAPI.getAddress) { + self.transactionContextAPI.getAddress(function (err, ret) { + if (err) return next(err) + tx.from = ret + next() + }) + } else { + self.getAccounts(function (err, ret) { + if (err) return next(err) + if (ret.length === 0) return next('No accounts available') + if (executionContext.isVM() && !self.accounts[ret[0]]) { + return next('Invalid account selected') + } + tx.from = ret[0] + next() + }) + } + }, + function runTransaction (next) { + var timestamp = Date.now() + self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) + self.txRunner.rawRun(tx, function (error, result) { + if (!tx.useCall) { + self.event.trigger('transactionExecuted', [error, tx.from, tx.to, tx.data, false, result, timestamp, payLoad]) + } else { + self.event.trigger('callExecuted', [error, tx.from, tx.to, tx.data, true, result, timestamp, payLoad]) + } + if (error) { + if (typeof (error) !== 'string') { + if (error.message) error = error.message + else { + try { error = 'error: ' + JSON.stringify(error) } catch (e) {} + } + } + } + next(error, result) + }) + } + ], cb) } UniversalDApp.prototype.runTx = function (args, cb) { @@ -227,6 +277,16 @@ UniversalDApp.prototype.runTx = function (args, cb) { execute(pipeline, env, cb) } +function execute (pipeline, env, callback) { + function next (err, env) { + if (err) return callback(err) + var step = pipeline.shift() + if (step) step(env, next) + else callback(null, env.result) + } + next(null, env) +} + function queryGasLimit (env, next) { var { self, tx } = env tx.gasLimit = 3000000 From 09fd25e3607517402fdcd68378480c944f85dce6 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 7 Feb 2018 17:20:03 -0500 Subject: [PATCH 2/6] remove old code --- src/universal-dapp.js | 95 ------------------------------------------- 1 file changed, 95 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 2e57ab9f3e..f4fe6b3743 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -261,99 +261,4 @@ UniversalDApp.prototype.runTx = function (args, cb) { ], cb) } -UniversalDApp.prototype.runTx = function (args, cb) { - var self = this - var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: args.from, value: args.value } - var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName } // contains decoded parameters - var pipeline = [queryGasLimit] - if (!args.value) { - pipeline.push(queryValue) - } - if (!args.from) { - pipeline.push(queryAddress) - } - pipeline.push(runTransaction) - var env = { self, tx, payLoad } - execute(pipeline, env, cb) -} - -function execute (pipeline, env, callback) { - function next (err, env) { - if (err) return callback(err) - var step = pipeline.shift() - if (step) step(env, next) - else callback(null, env.result) - } - next(null, env) -} - -function queryGasLimit (env, next) { - var { self, tx } = env - tx.gasLimit = 3000000 - if (self.transactionContextAPI.getGasLimit) { - self.transactionContextAPI.getGasLimit(function (err, ret) { - if (err) return next(err) - tx.gasLimit = ret - next(null, env) - }) - } else next(null, env) -} - -function queryValue (env, next) { - var { self, tx } = env - tx.value = 0 - if (tx.useCall) return next(null, env) - if (self.transactionContextAPI.getValue) { - self.transactionContextAPI.getValue(function (err, ret) { - if (err) return next(err) - tx.value = ret - next(null, env) - }) - } else next(null, env) -} - -function queryAddress (env, next) { - var { self, tx } = env - if (self.transactionContextAPI.getAddress) { - self.transactionContextAPI.getAddress(function (err, ret) { - if (err) return next(err) - tx.from = ret - next(null, env) - }) - } else { - self.getAccounts(function (err, ret) { - if (err) return next(err) - if (ret.length === 0) return next('No accounts available') - if (executionContext.isVM() && !self.accounts[ret[0]]) { - return next('Invalid account selected') - } - tx.from = ret[0] - next(null, env) - }) - } -} - -function runTransaction (env, next) { - var { self, tx, payLoad } = env - var timestamp = Date.now() - self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) - self.txRunner.rawRun(tx, function (error, result) { - if (!tx.useCall) { - self.event.trigger('transactionExecuted', [error, tx.from, tx.to, tx.data, false, result, timestamp, payLoad]) - } else { - self.event.trigger('callExecuted', [error, tx.from, tx.to, tx.data, true, result, timestamp, payLoad]) - } - if (error) { - if (typeof (error) !== 'string') { - if (error.message) error = error.message - else { - try { error = 'error: ' + JSON.stringify(error) } catch (e) {} - } - } - } - env.result = result - next(error, env) - }) -} - module.exports = UniversalDApp From 8cc7c89761a7aa7d82358d216c79053f77a13a38 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 7 Feb 2018 18:49:20 -0500 Subject: [PATCH 3/6] refactor queryGasLimit and queryValue --- src/universal-dapp.js | 49 +++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index f4fe6b3743..1a01a5bc26 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -204,41 +204,36 @@ UniversalDApp.prototype.getInputs = function (funABI) { UniversalDApp.prototype.runTx = function (args, cb) { const self = this - - var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: args.from, value: args.value } - var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName } // contains decoded parameters - async.waterfall([ function queryGasLimit (next) { - tx.gasLimit = 3000000 if (self.transactionContextAPI.getGasLimit) { - self.transactionContextAPI.getGasLimit(function (err, ret) { - if (err) return next(err) - tx.gasLimit = ret - next() - }) - } else next() + return self.transactionContextAPI.getGasLimit(next) + } + next(null, 3000000) }, - function queryValue (next) { + function queryValue (gasLimit, next) { if (self.transactionContextAPI.getAddress) { - self.transactionContextAPI.getAddress(function (err, ret) { - if (err) return next(err) - tx.from = ret - next() - }) - } else { - self.getAccounts(function (err, ret) { - if (err) return next(err) - if (ret.length === 0) return next('No accounts available') - if (executionContext.isVM() && !self.accounts[ret[0]]) { - return next('Invalid account selected') - } - tx.from = ret[0] - next() + return self.transactionContextAPI.getAddress(function (err, address) { + next(err, address, gasLimit) }) } + self.getAccounts(function (err, accounts) { + let address = accounts[0] + + if (err) return next(err) + if (!address) return next('No accounts available') + if (executionContext.isVM() && !self.accounts[address]) { + return next('Invalid account selected') + } + next(null, address, gasLimit) + }) }, - function runTransaction (next) { + function runTransaction (fromAddress, gasLimit, next) { + var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: args.from, value: args.value } + var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName } + + tx.gasLimit = gasLimit + tx.from = fromAddress var timestamp = Date.now() self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) self.txRunner.rawRun(tx, function (error, result) { From f9342769221eea7d78afd31186063bd00c117ea1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 7 Feb 2018 19:01:18 -0500 Subject: [PATCH 4/6] refactor runTransaction --- src/universal-dapp.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 1a01a5bc26..d69ce152c3 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -205,13 +205,13 @@ UniversalDApp.prototype.getInputs = function (funABI) { UniversalDApp.prototype.runTx = function (args, cb) { const self = this async.waterfall([ - function queryGasLimit (next) { + function getGasLimit (next) { if (self.transactionContextAPI.getGasLimit) { return self.transactionContextAPI.getGasLimit(next) } next(null, 3000000) }, - function queryValue (gasLimit, next) { + function getAccount (gasLimit, next) { if (self.transactionContextAPI.getAddress) { return self.transactionContextAPI.getAddress(function (err, address) { next(err, address, gasLimit) @@ -229,25 +229,19 @@ UniversalDApp.prototype.runTx = function (args, cb) { }) }, function runTransaction (fromAddress, gasLimit, next) { - var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: args.from, value: args.value } + var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: args.value, gasLimit: gasLimit } var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName } - - tx.gasLimit = gasLimit - tx.from = fromAddress var timestamp = Date.now() + self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) self.txRunner.rawRun(tx, function (error, result) { - if (!tx.useCall) { - self.event.trigger('transactionExecuted', [error, tx.from, tx.to, tx.data, false, result, timestamp, payLoad]) - } else { - self.event.trigger('callExecuted', [error, tx.from, tx.to, tx.data, true, result, timestamp, payLoad]) - } - if (error) { - if (typeof (error) !== 'string') { - if (error.message) error = error.message - else { - try { error = 'error: ' + JSON.stringify(error) } catch (e) {} - } + let eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted') + self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad]) + + if (error && (typeof (error) !== 'string')) { + if (error.message) error = error.message + else { + try { error = 'error: ' + JSON.stringify(error) } catch (e) {} } } next(error, result) From 2b113e8df3fb9b114a229434731c0a4fb0f69c24 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 8 Feb 2018 11:08:52 -0500 Subject: [PATCH 5/6] re-add queryValue function to the waterfall; update to take into account value and gasLimit --- src/universal-dapp.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index d69ce152c3..502b668e21 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -211,10 +211,25 @@ UniversalDApp.prototype.runTx = function (args, cb) { } next(null, 3000000) }, - function getAccount (gasLimit, next) { + function queryValue (gasLimit, next) { + if (args.value) { + return next(null, args.value, gasLimit) + } + if (args.useCall) return next(null, 0, gasLimit) + if (self.transactionContextAPI.getValue) { + self.transactionContextAPI.getValue(function (err, value) { + next(err, value, gasLimit) + }) + } + next(null, 0, gasLimit) + }, + function getAccount (value, gasLimit, next) { + if (args.from) { + return next(null, args.from, value, gasLimit) + } if (self.transactionContextAPI.getAddress) { return self.transactionContextAPI.getAddress(function (err, address) { - next(err, address, gasLimit) + next(err, address, value, gasLimit) }) } self.getAccounts(function (err, accounts) { @@ -225,11 +240,11 @@ UniversalDApp.prototype.runTx = function (args, cb) { if (executionContext.isVM() && !self.accounts[address]) { return next('Invalid account selected') } - next(null, address, gasLimit) + next(null, address, value, gasLimit) }) }, - function runTransaction (fromAddress, gasLimit, next) { - var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: args.value, gasLimit: gasLimit } + function runTransaction (fromAddress, value, gasLimit, next) { + var tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit } var payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName } var timestamp = Date.now() From f684e12f750d869d4ec6aabd863bd0b0754a2d7d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 8 Feb 2018 11:11:42 -0500 Subject: [PATCH 6/6] simplify queryValue; remove one level of complexity --- src/universal-dapp.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/universal-dapp.js b/src/universal-dapp.js index 502b668e21..21c8f32de0 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -215,13 +215,12 @@ UniversalDApp.prototype.runTx = function (args, cb) { if (args.value) { return next(null, args.value, gasLimit) } - if (args.useCall) return next(null, 0, gasLimit) - if (self.transactionContextAPI.getValue) { - self.transactionContextAPI.getValue(function (err, value) { - next(err, value, gasLimit) - }) + if (args.useCall || !self.transactionContextAPI.getValue) { + return next(null, 0, gasLimit) } - next(null, 0, gasLimit) + self.transactionContextAPI.getValue(function (err, value) { + next(err, value, gasLimit) + }) }, function getAccount (value, gasLimit, next) { if (args.from) {