1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/app/scripts/controllers/transactions/pending-tx-tracker.js

183 lines
6.1 KiB
JavaScript
Raw Normal View History

2017-08-04 20:41:35 +02:00
const EventEmitter = require('events')
2018-04-19 20:56:34 +02:00
const log = require('loglevel')
2017-08-04 20:41:35 +02:00
const EthQuery = require('ethjs-query')
2018-04-13 22:18:45 +02:00
/**
2017-08-04 20:41:35 +02:00
2018-04-13 22:18:45 +02:00
Event emitter utility class for tracking the transactions as they<br>
go from a pending state to a confirmed (mined in a block) state<br>
<br>
2017-08-04 20:41:35 +02:00
As well as continues broadcast while in the pending state
2018-04-13 22:18:45 +02:00
<br>
@param config {object} - non optional configuration object consists of:
2018-04-25 20:13:51 +02:00
@param {Object} config.provider - A network provider.
@param {Object} config.nonceTracker see nonce tracker
@param {function} config.getPendingTransactions a function for getting an array of transactions,
@param {function} config.publishTransaction a async function for publishing raw transactions,
2017-08-04 20:41:35 +02:00
2018-04-13 22:18:45 +02:00
@class
2017-08-04 20:41:35 +02:00
*/
2018-04-13 22:18:45 +02:00
class PendingTransactionTracker extends EventEmitter {
2017-08-04 20:41:35 +02:00
constructor (config) {
super()
this.query = new EthQuery(config.provider)
this.nonceTracker = config.nonceTracker
this.getPendingTransactions = config.getPendingTransactions
this.getCompletedTransactions = config.getCompletedTransactions
2017-08-04 20:41:35 +02:00
this.publishTransaction = config.publishTransaction
this.confirmTransaction = config.confirmTransaction
2017-08-04 20:41:35 +02:00
}
2018-04-13 22:18:45 +02:00
/**
checks the network for signed txs and releases the nonce global lock if it is
2018-04-13 22:18:45 +02:00
*/
async updatePendingTxs () {
// in order to keep the nonceTracker accurate we block it while updating pending transactions
const nonceGlobalLock = await this.nonceTracker.getGlobalLock()
try {
const pendingTxs = this.getPendingTransactions()
await Promise.all(pendingTxs.map((txMeta) => this._checkPendingTx(txMeta)))
} catch (err) {
log.error('PendingTransactionTracker - Error updating pending transactions')
log.error(err)
2017-08-04 20:41:35 +02:00
}
nonceGlobalLock.releaseLock()
2017-08-04 20:41:35 +02:00
}
2018-04-13 22:18:45 +02:00
/**
Will resubmit any transactions who have not been confirmed in a block
@param block {object} - a block object
@emits tx:warning
*/
resubmitPendingTxs (blockNumber) {
const pending = this.getPendingTransactions()
2017-08-04 20:41:35 +02:00
// only try resubmitting if their are transactions to resubmit
if (!pending.length) return
pending.forEach((txMeta) => this._resubmitTx(txMeta, blockNumber).catch((err) => {
2017-08-04 20:41:35 +02:00
/*
Dont marked as failed if the error is a "known" transaction warning
"there is already a transaction with the same sender-nonce
but higher/same gas price"
Also don't mark as failed if it has ever been broadcast successfully.
A successful broadcast means it may still be mined.
2017-08-04 20:41:35 +02:00
*/
const errorMessage = err.message.toLowerCase()
const isKnownTx = (
// geth
Fix lint warnings Fixed warnings: ```md app/scripts/controllers/computed-balances.js + 35:27 warning Missing space before function parentheses space-before-function-paren + 41:14 warning 'address' is never reassigned. Use 'const' instead prefer-const + 61:9 warning 'updater' is never reassigned. Use 'const' instead prefer-const + 68:11 warning 'newState' is never reassigned. Use 'const' instead prefer-const app/scripts/controllers/network.js + 104:29 warning Missing space before function parentheses space-before-function-paren app/scripts/lib/createLoggerMiddleware.js + 4:32 warning Missing space before function parentheses space-before-function-paren + 15:2 warning Newline required at end of file but not found eol-last app/scripts/lib/createOriginMiddleware.js + 4:32 warning Missing space before function parentheses space-before-function-paren + 9:2 warning Newline required at end of file but not found eol-last app/scripts/lib/createProviderMiddleware.js + 5:34 warning Missing space before function parentheses space-before-function-paren + 13:2 warning Newline required at end of file but not found eol-last app/scripts/lib/events-proxy.js + 1:50 warning Missing space before function parentheses space-before-function-paren + 31:2 warning Newline required at end of file but not found eol-last app/scripts/lib/nodeify.js + 2:22 warning Missing space before function parentheses space-before-function-paren + 2:24 warning Missing space before opening brace space-before-blocks + 5:18 warning Missing space before function parentheses space-before-function-paren + 5:20 warning Missing space before opening brace space-before-blocks app/scripts/lib/pending-balance-calculator.js + 16:19 warning Missing space before function parentheses space-before-function-paren app/scripts/lib/pending-tx-tracker.js + 85:11 warning '||' should be placed at the end of the line operator-linebreak + 87:11 warning '||' should be placed at the end of the line operator-linebreak + 88:11 warning '||' should be placed at the end of the line operator-linebreak + 90:11 warning '||' should be placed at the end of the line operator-linebreak + 91:11 warning '||' should be placed at the end of the line operator-linebreak app/scripts/lib/port-stream.js + 3:22 warning Missing space before function parentheses space-before-function-paren + 3:24 warning Missing space before opening brace space-before-blocks app/scripts/lib/tx-gas-utils.js + 84:2 warning Newline required at end of file but not found eol-last app/scripts/lib/tx-state-history-helper.js + 12:37 warning Missing space before function parentheses space-before-function-paren + 23:30 warning Missing space before function parentheses space-before-function-paren + 30:23 warning Missing space before function parentheses space-before-function-paren + 35:28 warning Missing space before function parentheses space-before-function-paren + 41:2 warning Newline required at end of file but not found eol-last app/scripts/lib/tx-state-manager.js + 94:13 warning 'value' is never reassigned. Use 'const' instead prefer-const ui/app/reducers.js + 45:7 warning 'state' is never reassigned. Use 'const' instead prefer-const + 53:7 warning 'stateString' is never reassigned. Use 'const' instead prefer-const ui/lib/tx-helper.js + 27:2 warning Newline required at end of file but not found eol-last ui/app/components/account-dropdowns.js + 163:1 warning More than 2 blank lines not allowed no-multiple-empty-lines ui/app/components/menu-droppo.js + 22:7 warning 'style' is never reassigned. Use 'const' instead prefer-const ui/app/components/shapeshift-form.js + 135:11 warning '&&' should be placed at the end of the line operator-linebreak ui/app/components/typed-message-renderer.js + 35:25 warning Missing space before function parentheses space-before-function-paren + 42:2 warning Newline required at end of file but not found eol-last mascara/server/index.js + 11:42 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 12:36 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 13:33 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 14:40 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 20:29 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 21:29 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 26:40 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat ```
2017-10-21 21:06:39 +02:00
errorMessage.includes('replacement transaction underpriced') ||
errorMessage.includes('known transaction') ||
2017-08-04 20:41:35 +02:00
// parity
Fix lint warnings Fixed warnings: ```md app/scripts/controllers/computed-balances.js + 35:27 warning Missing space before function parentheses space-before-function-paren + 41:14 warning 'address' is never reassigned. Use 'const' instead prefer-const + 61:9 warning 'updater' is never reassigned. Use 'const' instead prefer-const + 68:11 warning 'newState' is never reassigned. Use 'const' instead prefer-const app/scripts/controllers/network.js + 104:29 warning Missing space before function parentheses space-before-function-paren app/scripts/lib/createLoggerMiddleware.js + 4:32 warning Missing space before function parentheses space-before-function-paren + 15:2 warning Newline required at end of file but not found eol-last app/scripts/lib/createOriginMiddleware.js + 4:32 warning Missing space before function parentheses space-before-function-paren + 9:2 warning Newline required at end of file but not found eol-last app/scripts/lib/createProviderMiddleware.js + 5:34 warning Missing space before function parentheses space-before-function-paren + 13:2 warning Newline required at end of file but not found eol-last app/scripts/lib/events-proxy.js + 1:50 warning Missing space before function parentheses space-before-function-paren + 31:2 warning Newline required at end of file but not found eol-last app/scripts/lib/nodeify.js + 2:22 warning Missing space before function parentheses space-before-function-paren + 2:24 warning Missing space before opening brace space-before-blocks + 5:18 warning Missing space before function parentheses space-before-function-paren + 5:20 warning Missing space before opening brace space-before-blocks app/scripts/lib/pending-balance-calculator.js + 16:19 warning Missing space before function parentheses space-before-function-paren app/scripts/lib/pending-tx-tracker.js + 85:11 warning '||' should be placed at the end of the line operator-linebreak + 87:11 warning '||' should be placed at the end of the line operator-linebreak + 88:11 warning '||' should be placed at the end of the line operator-linebreak + 90:11 warning '||' should be placed at the end of the line operator-linebreak + 91:11 warning '||' should be placed at the end of the line operator-linebreak app/scripts/lib/port-stream.js + 3:22 warning Missing space before function parentheses space-before-function-paren + 3:24 warning Missing space before opening brace space-before-blocks app/scripts/lib/tx-gas-utils.js + 84:2 warning Newline required at end of file but not found eol-last app/scripts/lib/tx-state-history-helper.js + 12:37 warning Missing space before function parentheses space-before-function-paren + 23:30 warning Missing space before function parentheses space-before-function-paren + 30:23 warning Missing space before function parentheses space-before-function-paren + 35:28 warning Missing space before function parentheses space-before-function-paren + 41:2 warning Newline required at end of file but not found eol-last app/scripts/lib/tx-state-manager.js + 94:13 warning 'value' is never reassigned. Use 'const' instead prefer-const ui/app/reducers.js + 45:7 warning 'state' is never reassigned. Use 'const' instead prefer-const + 53:7 warning 'stateString' is never reassigned. Use 'const' instead prefer-const ui/lib/tx-helper.js + 27:2 warning Newline required at end of file but not found eol-last ui/app/components/account-dropdowns.js + 163:1 warning More than 2 blank lines not allowed no-multiple-empty-lines ui/app/components/menu-droppo.js + 22:7 warning 'style' is never reassigned. Use 'const' instead prefer-const ui/app/components/shapeshift-form.js + 135:11 warning '&&' should be placed at the end of the line operator-linebreak ui/app/components/typed-message-renderer.js + 35:25 warning Missing space before function parentheses space-before-function-paren + 42:2 warning Newline required at end of file but not found eol-last mascara/server/index.js + 11:42 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 12:36 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 13:33 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 14:40 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 20:29 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 21:29 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 26:40 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat ```
2017-10-21 21:06:39 +02:00
errorMessage.includes('gas price too low to replace') ||
errorMessage.includes('transaction with the same hash was already imported') ||
2017-08-04 20:41:35 +02:00
// other
Fix lint warnings Fixed warnings: ```md app/scripts/controllers/computed-balances.js + 35:27 warning Missing space before function parentheses space-before-function-paren + 41:14 warning 'address' is never reassigned. Use 'const' instead prefer-const + 61:9 warning 'updater' is never reassigned. Use 'const' instead prefer-const + 68:11 warning 'newState' is never reassigned. Use 'const' instead prefer-const app/scripts/controllers/network.js + 104:29 warning Missing space before function parentheses space-before-function-paren app/scripts/lib/createLoggerMiddleware.js + 4:32 warning Missing space before function parentheses space-before-function-paren + 15:2 warning Newline required at end of file but not found eol-last app/scripts/lib/createOriginMiddleware.js + 4:32 warning Missing space before function parentheses space-before-function-paren + 9:2 warning Newline required at end of file but not found eol-last app/scripts/lib/createProviderMiddleware.js + 5:34 warning Missing space before function parentheses space-before-function-paren + 13:2 warning Newline required at end of file but not found eol-last app/scripts/lib/events-proxy.js + 1:50 warning Missing space before function parentheses space-before-function-paren + 31:2 warning Newline required at end of file but not found eol-last app/scripts/lib/nodeify.js + 2:22 warning Missing space before function parentheses space-before-function-paren + 2:24 warning Missing space before opening brace space-before-blocks + 5:18 warning Missing space before function parentheses space-before-function-paren + 5:20 warning Missing space before opening brace space-before-blocks app/scripts/lib/pending-balance-calculator.js + 16:19 warning Missing space before function parentheses space-before-function-paren app/scripts/lib/pending-tx-tracker.js + 85:11 warning '||' should be placed at the end of the line operator-linebreak + 87:11 warning '||' should be placed at the end of the line operator-linebreak + 88:11 warning '||' should be placed at the end of the line operator-linebreak + 90:11 warning '||' should be placed at the end of the line operator-linebreak + 91:11 warning '||' should be placed at the end of the line operator-linebreak app/scripts/lib/port-stream.js + 3:22 warning Missing space before function parentheses space-before-function-paren + 3:24 warning Missing space before opening brace space-before-blocks app/scripts/lib/tx-gas-utils.js + 84:2 warning Newline required at end of file but not found eol-last app/scripts/lib/tx-state-history-helper.js + 12:37 warning Missing space before function parentheses space-before-function-paren + 23:30 warning Missing space before function parentheses space-before-function-paren + 30:23 warning Missing space before function parentheses space-before-function-paren + 35:28 warning Missing space before function parentheses space-before-function-paren + 41:2 warning Newline required at end of file but not found eol-last app/scripts/lib/tx-state-manager.js + 94:13 warning 'value' is never reassigned. Use 'const' instead prefer-const ui/app/reducers.js + 45:7 warning 'state' is never reassigned. Use 'const' instead prefer-const + 53:7 warning 'stateString' is never reassigned. Use 'const' instead prefer-const ui/lib/tx-helper.js + 27:2 warning Newline required at end of file but not found eol-last ui/app/components/account-dropdowns.js + 163:1 warning More than 2 blank lines not allowed no-multiple-empty-lines ui/app/components/menu-droppo.js + 22:7 warning 'style' is never reassigned. Use 'const' instead prefer-const ui/app/components/shapeshift-form.js + 135:11 warning '&&' should be placed at the end of the line operator-linebreak ui/app/components/typed-message-renderer.js + 35:25 warning Missing space before function parentheses space-before-function-paren + 42:2 warning Newline required at end of file but not found eol-last mascara/server/index.js + 11:42 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 12:36 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 13:33 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 14:40 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 20:29 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 21:29 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat + 26:40 warning Use path.join() or path.resolve() instead of + to create paths no-path-concat ```
2017-10-21 21:06:39 +02:00
errorMessage.includes('gateway timeout') ||
errorMessage.includes('nonce too low')
2017-08-04 20:41:35 +02:00
)
// ignore resubmit warnings, return early
if (isKnownTx) return
// encountered real error - transition to error state
txMeta.warning = {
error: errorMessage,
message: 'There was an error when resubmitting this transaction.',
}
this.emit('tx:warning', txMeta, err)
2017-08-04 20:41:35 +02:00
}))
}
2018-04-13 22:18:45 +02:00
/**
resubmits the individual txMeta used in resubmitPendingTxs
2018-04-25 20:13:51 +02:00
@param txMeta {Object} - txMeta object
2018-04-13 22:18:45 +02:00
@param latestBlockNumber {string} - hex string for the latest block number
@emits tx:retry
@returns txHash {string}
*/
async _resubmitTx (txMeta, latestBlockNumber) {
if (!txMeta.firstRetryBlockNumber) {
this.emit('tx:block-update', txMeta, latestBlockNumber)
}
const firstRetryBlockNumber = txMeta.firstRetryBlockNumber || latestBlockNumber
const txBlockDistance = Number.parseInt(latestBlockNumber, 16) - Number.parseInt(firstRetryBlockNumber, 16)
const retryCount = txMeta.retryCount || 0
// Exponential backoff to limit retries at publishing
if (txBlockDistance <= Math.pow(2, retryCount) - 1) return
2017-08-04 20:41:35 +02:00
// Only auto-submit already-signed txs:
if (!('rawTx' in txMeta)) return
const rawTx = txMeta.rawTx
const txHash = await this.publishTransaction(rawTx)
// Increment successful tries:
this.emit('tx:retry', txMeta)
return txHash
2017-08-04 20:41:35 +02:00
}
2018-04-13 22:18:45 +02:00
/**
Ask the network for the transaction to see if it has been include in a block
2018-04-25 20:13:51 +02:00
@param txMeta {Object} - the txMeta object
2018-04-13 22:18:45 +02:00
@emits tx:failed
@emits tx:confirmed
@emits tx:warning
*/
2017-08-04 20:41:35 +02:00
async _checkPendingTx (txMeta) {
const txHash = txMeta.hash
const txId = txMeta.id
2017-08-04 20:41:35 +02:00
// extra check in case there was an uncaught error during the
// signature and submission process
if (!txHash) {
const noTxHashErr = new Error('We had an error while submitting this transaction, please try again.')
noTxHashErr.name = 'NoTxHashError'
this.emit('tx:failed', txId, noTxHashErr)
2017-08-04 20:41:35 +02:00
return
}
// If another tx with the same nonce is mined, set as failed.
const taken = await this._checkIfNonceIsTaken(txMeta)
if (taken) {
const nonceTakenErr = new Error('Another transaction with this nonce has been mined.')
nonceTakenErr.name = 'NonceTakenErr'
return this.emit('tx:failed', txId, nonceTakenErr)
}
2017-08-04 20:41:35 +02:00
// get latest transaction status
try {
const txParams = await this.query.getTransactionByHash(txHash)
2017-08-04 20:41:35 +02:00
if (!txParams) return
if (txParams.blockNumber) {
this.emit('tx:confirmed', txId)
2017-08-04 20:41:35 +02:00
}
} catch (err) {
txMeta.warning = {
error: err.message,
2017-08-04 20:41:35 +02:00
message: 'There was a problem loading this transaction.',
}
this.emit('tx:warning', txMeta, err)
2017-08-04 20:41:35 +02:00
}
}
2018-04-13 22:18:45 +02:00
/**
checks to see if a confirmed txMeta has the same nonce
2018-04-25 20:13:51 +02:00
@param txMeta {Object} - txMeta object
2018-04-13 22:18:45 +02:00
@returns {boolean}
*/
async _checkIfNonceIsTaken (txMeta) {
const address = txMeta.txParams.from
const completed = this.getCompletedTransactions(address)
const sameNonce = completed.filter((otherMeta) => {
return otherMeta.txParams.nonce === txMeta.txParams.nonce
})
return sameNonce.length > 0
}
}
2018-04-13 22:18:45 +02:00
2018-04-19 20:29:26 +02:00
module.exports = PendingTransactionTracker