aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/inpage-provider.js3
-rw-r--r--app/scripts/lib/nonce-tracker.js28
-rw-r--r--app/scripts/lib/obj-multiplex.js16
-rw-r--r--app/scripts/lib/tx-utils.js61
-rw-r--r--app/scripts/lib/util.js8
5 files changed, 53 insertions, 63 deletions
diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js
index 8b8623974..fd032a673 100644
--- a/app/scripts/lib/inpage-provider.js
+++ b/app/scripts/lib/inpage-provider.js
@@ -26,6 +26,9 @@ function MetamaskInpageProvider (connectionStream) {
(err) => logStreamDisconnectWarning('MetaMask PublicConfigStore', err)
)
+ // ignore phishing warning message (handled elsewhere)
+ multiStream.ignoreStream('phishing')
+
// connect to async provider
const asyncProvider = self.asyncProvider = new StreamProvider()
pipe(
diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js
index b76dac4e8..8328e81ec 100644
--- a/app/scripts/lib/nonce-tracker.js
+++ b/app/scripts/lib/nonce-tracker.js
@@ -4,8 +4,8 @@ const Mutex = require('await-semaphore').Mutex
class NonceTracker {
- constructor ({ blockTracker, provider, getPendingTransactions }) {
- this.blockTracker = blockTracker
+ constructor ({ provider, getPendingTransactions }) {
+ this.provider = provider
this.ethQuery = new EthQuery(provider)
this.getPendingTransactions = getPendingTransactions
this.lockMap = {}
@@ -31,21 +31,25 @@ class NonceTracker {
const currentBlock = await this._getCurrentBlock()
const pendingTransactions = this.getPendingTransactions(address)
const pendingCount = pendingTransactions.length
- assert(Number.isInteger(pendingCount), 'nonce-tracker - pendingCount is an integer')
+ assert(Number.isInteger(pendingCount), `nonce-tracker - pendingCount is not an integer - got: (${typeof pendingCount}) "${pendingCount}"`)
const baseCountHex = await this._getTxCount(address, currentBlock)
const baseCount = parseInt(baseCountHex, 16)
- assert(Number.isInteger(baseCount), 'nonce-tracker - baseCount is an integer')
+ assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`)
const nextNonce = baseCount + pendingCount
- assert(Number.isInteger(nextNonce), 'nonce-tracker - nextNonce is an integer')
- // return next nonce and release cb
- return { nextNonce, releaseLock }
+ assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`)
+ // collect the numbers used to calculate the nonce for debugging
+ const blockNumber = currentBlock.number
+ const nonceDetails = { blockNumber, baseCount, baseCountHex, pendingCount }
+ // return nonce and release cb
+ return { nextNonce, nonceDetails, releaseLock }
}
async _getCurrentBlock () {
- const currentBlock = this.blockTracker.getCurrentBlock()
+ const blockTracker = this._getBlockTracker()
+ const currentBlock = blockTracker.getCurrentBlock()
if (currentBlock) return currentBlock
return await Promise((reject, resolve) => {
- this.blockTracker.once('latest', resolve)
+ blockTracker.once('latest', resolve)
})
}
@@ -79,6 +83,12 @@ class NonceTracker {
return mutex
}
+ // this is a hotfix for the fact that the blockTracker will
+ // change when the network changes
+ _getBlockTracker () {
+ return this.provider._blockTracker
+ }
+
}
module.exports = NonceTracker
diff --git a/app/scripts/lib/obj-multiplex.js b/app/scripts/lib/obj-multiplex.js
index bd114c394..0034febe0 100644
--- a/app/scripts/lib/obj-multiplex.js
+++ b/app/scripts/lib/obj-multiplex.js
@@ -5,12 +5,16 @@ module.exports = ObjectMultiplex
function ObjectMultiplex (opts) {
opts = opts || {}
// create multiplexer
- var mx = through.obj(function (chunk, enc, cb) {
- var name = chunk.name
- var data = chunk.data
- var substream = mx.streams[name]
+ const mx = through.obj(function (chunk, enc, cb) {
+ const name = chunk.name
+ const data = chunk.data
+ if (!name) {
+ console.warn(`ObjectMultiplex - Malformed chunk without name "${chunk}"`)
+ return cb()
+ }
+ const substream = mx.streams[name]
if (!substream) {
- console.warn(`orphaned data for stream "${name}"`)
+ console.warn(`ObjectMultiplex - orphaned data for stream "${name}"`)
} else {
if (substream.push) substream.push(data)
}
@@ -19,7 +23,7 @@ function ObjectMultiplex (opts) {
mx.streams = {}
// create substreams
mx.createStream = function (name) {
- var substream = mx.streams[name] = through.obj(function (chunk, enc, cb) {
+ const substream = mx.streams[name] = through.obj(function (chunk, enc, cb) {
mx.push({
name: name,
data: chunk,
diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js
index 8f6943937..3687a9652 100644
--- a/app/scripts/lib/tx-utils.js
+++ b/app/scripts/lib/tx-utils.js
@@ -1,4 +1,3 @@
-const async = require('async')
const ethUtil = require('ethereumjs-util')
const Transaction = require('ethereumjs-tx')
const normalize = require('eth-sig-util').normalize
@@ -10,24 +9,19 @@ its passed ethquery
and used to do things like calculate gas of a tx.
*/
-module.exports = class txProviderUtils {
-
+module.exports = class txProvideUtils {
constructor (ethQuery) {
this.query = ethQuery
}
- analyzeGasUsage (txMeta, cb) {
- var self = this
- this.query.getBlockByNumber('latest', true, (err, block) => {
- if (err) return cb(err)
- async.waterfall([
- self.estimateTxGas.bind(self, txMeta, block.gasLimit),
- self.setTxGas.bind(self, txMeta, block.gasLimit),
- ], cb)
- })
+ async analyzeGasUsage (txMeta) {
+ const block = await this.query.getBlockByNumber('latest', true)
+ const estimatedGasHex = await this.estimateTxGas(txMeta, block.gasLimit)
+ this.setTxGas(txMeta, block.gasLimit, estimatedGasHex)
+ return txMeta
}
- estimateTxGas (txMeta, blockGasLimitHex, cb) {
+ async estimateTxGas (txMeta, blockGasLimitHex) {
const txParams = txMeta.txParams
// check if gasLimit is already specified
txMeta.gasLimitSpecified = Boolean(txParams.gas)
@@ -38,10 +32,10 @@ module.exports = class txProviderUtils {
txParams.gas = bnToHex(saferGasLimitBN)
}
// run tx, see if it will OOG
- this.query.estimateGas(txParams, cb)
+ return this.query.estimateGas(txParams)
}
- setTxGas (txMeta, blockGasLimitHex, estimatedGasHex, cb) {
+ setTxGas (txMeta, blockGasLimitHex, estimatedGasHex) {
txMeta.estimatedGas = estimatedGasHex
const txParams = txMeta.txParams
@@ -49,14 +43,12 @@ module.exports = class txProviderUtils {
// use original specified amount
if (txMeta.gasLimitSpecified) {
txMeta.estimatedGas = txParams.gas
- cb()
return
}
// if gasLimit not originally specified,
// try adding an additional gas buffer to our estimation for safety
const recommendedGasHex = this.addGasBuffer(txMeta.estimatedGas, blockGasLimitHex)
txParams.gas = recommendedGasHex
- cb()
return
}
@@ -74,22 +66,6 @@ module.exports = class txProviderUtils {
return bnToHex(upperGasLimitBn)
}
- fillInTxParams (txParams, cb) {
- const fromAddress = txParams.from
- const reqs = {}
-
- if (isUndef(txParams.gas)) reqs.gas = (cb) => this.query.estimateGas(txParams, cb)
- if (isUndef(txParams.gasPrice)) reqs.gasPrice = (cb) => this.query.gasPrice(cb)
- if (isUndef(txParams.nonce)) reqs.nonce = (cb) => this.query.getTransactionCount(fromAddress, 'pending', cb)
-
- async.parallel(reqs, function (err, result) {
- if (err) return cb(err)
- // write results to txParams obj
- Object.assign(txParams, result)
- cb()
- })
- }
-
// builds ethTx from txParams object
buildEthTxFromParams (txParams) {
// normalize values
@@ -106,20 +82,13 @@ module.exports = class txProviderUtils {
return ethTx
}
- publishTransaction (rawTx) {
- return new Promise((resolve, reject) => {
- this.query.sendRawTransaction(rawTx, (err, ress) => {
- if (err) reject(err)
- else resolve(ress)
- })
- })
+ async publishTransaction (rawTx) {
+ return await this.query.sendRawTransaction(rawTx)
}
- validateTxParams (txParams, cb) {
+ async validateTxParams (txParams) {
if (('value' in txParams) && txParams.value.indexOf('-') === 0) {
- cb(new Error(`Invalid transaction value of ${txParams.value} not a positive number.`))
- } else {
- cb()
+ throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`)
}
}
@@ -137,10 +106,6 @@ module.exports = class txProviderUtils {
// util
-function isUndef (value) {
- return value === undefined
-}
-
function bnToHex (inputBn) {
return ethUtil.addHexPrefix(inputBn.toString(16))
}
diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js
new file mode 100644
index 000000000..bddd60ee8
--- /dev/null
+++ b/app/scripts/lib/util.js
@@ -0,0 +1,8 @@
+module.exports = {
+ getStack,
+}
+
+function getStack () {
+ const stack = new Error('Stack trace generator - not an error').stack
+ return stack
+}