FIX replay transactions

pull/1/head
serapath 7 years ago committed by yann300
parent 07ec843256
commit 11c78ebe82
  1. 86
      src/universal-dapp.js

@ -458,82 +458,103 @@ UniversalDApp.prototype.pendingTransactions = function () {
return this.txRunner.pendingTxs return this.txRunner.pendingTxs
} }
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.replayTx = function (args, cb) {
var self = this
var tx = { to: args.to, data: args.data, useCall: args.useCall }
var pipeline = [runTransaction]
var env = { self, args, tx }
execute(pipeline, env, cb)
}
UniversalDApp.prototype.runTx = function (args, cb) { UniversalDApp.prototype.runTx = function (args, cb) {
var self = this var self = this
var tx = { var tx = { to: args.to, data: args.data, useCall: args.useCall }
to: args.to, var pipeline = [queryGasLimit, queryValue, queryAddress, runTransaction]
data: args.data, var env = { self, args, tx }
useCall: args.useCall execute(pipeline, env, cb)
} }
async.waterfall([
// query gas limit
function (callback) { function queryGasLimit (env, next) {
var { self, args, tx } = env
tx.gasLimit = 3000000 tx.gasLimit = 3000000
if (self.transactionContextAPI.getGasLimit) { if (self.transactionContextAPI.getGasLimit) {
self.transactionContextAPI.getGasLimit(function (err, ret) { self.transactionContextAPI.getGasLimit(function (err, ret) {
if (err) { if (err) {
return callback(err) return next(err)
} }
tx.gasLimit = ret tx.gasLimit = ret
callback() next(null, env)
}) })
} else { } else {
callback() next(null, env)
} }
}, }
// query value
function (callback) { function queryGasLimit (env, next) {
var { self, args, tx } = env
tx.value = 0 tx.value = 0
if (tx.useCall) return callback() if (tx.useCall) return next(null, env)
if (self.transactionContextAPI.getValue) { if (self.transactionContextAPI.getValue) {
self.transactionContextAPI.getValue(function (err, ret) { self.transactionContextAPI.getValue(function (err, ret) {
if (err) { if (err) {
return callback(err) return next(err)
} }
tx.value = ret tx.value = ret
callback() next(null, env)
}) })
} else { } else {
callback() next(null, env)
} }
}, }
// query address
function (callback) { function queryAddress (env, next) {
var { self, args, tx } = env
if (self.transactionContextAPI.getAddress) { if (self.transactionContextAPI.getAddress) {
self.transactionContextAPI.getAddress(function (err, ret) { self.transactionContextAPI.getAddress(function (err, ret) {
if (err) { if (err) {
return callback(err) return next(err)
} }
tx.from = ret tx.from = ret
callback() next(null, env)
}) })
} else { } else {
self.getAccounts(function (err, ret) { self.getAccounts(function (err, ret) {
if (err) { if (err) {
return callback(err) return next(err)
} }
if (ret.length === 0) { if (ret.length === 0) {
return callback('No accounts available') return next('No accounts available')
} }
if (executionContext.isVM() && !self.accounts[ret[0]]) { if (executionContext.isVM() && !self.accounts[ret[0]]) {
return callback('Invalid account selected') return next('Invalid account selected')
} }
tx.from = ret[0] tx.from = ret[0]
callback() next(null, env)
}) })
} }
}, }
// run transaction
function (callback) { function runTransaction (env, next) {
var { self, args, tx } = env
var timestamp = Date.now() var timestamp = Date.now()
self.event.trigger('initiatingTransaction', [timestamp, tx]) self.event.trigger('initiatingTransaction', [timestamp, tx])
self.txRunner.rawRun(tx, function (error, result) { self.txRunner.rawRun(tx, function (error, result) {
@ -553,10 +574,9 @@ UniversalDApp.prototype.runTx = function (args, cb) {
} }
} }
} }
callback(error, result) env.result = result
next(error, env)
}) })
} }
], cb)
}
module.exports = UniversalDApp module.exports = UniversalDApp

Loading…
Cancel
Save