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-05-29 00:54:47 +02:00
|
|
|
|
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
|
2017-10-06 21:51:13 +02:00
|
|
|
this.getCompletedTransactions = config.getCompletedTransactions
|
2017-08-04 20:41:35 +02:00
|
|
|
this.publishTransaction = config.publishTransaction
|
2018-05-23 01:41:00 +02:00
|
|
|
this.confirmTransaction = config.confirmTransaction
|
2017-08-04 20:41:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 22:18:45 +02:00
|
|
|
/**
|
2018-05-28 23:29:31 +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
|
|
|
*/
|
2018-05-28 23:29:31 +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 {
|
2018-05-29 09:53:44 +02:00
|
|
|
const pendingTxs = this.getPendingTransactions()
|
2018-05-28 23:29:31 +02:00
|
|
|
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
|
|
|
}
|
2018-05-28 23:29:31 +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
|
|
|
|
*/
|
2018-05-23 01:41:00 +02:00
|
|
|
resubmitPendingTxs (blockNumber) {
|
2017-08-09 03:49:48 +02:00
|
|
|
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
|
2018-05-23 01:41:00 +02:00
|
|
|
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"
|
2017-09-18 23:34:25 +02:00
|
|
|
|
|
|
|
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
|
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
|
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
|
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
|
2017-10-03 00:00:23 +02:00
|
|
|
txMeta.warning = {
|
|
|
|
error: errorMessage,
|
|
|
|
message: 'There was an error when resubmitting this transaction.',
|
|
|
|
}
|
2017-10-03 00:20:01 +02:00
|
|
|
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}
|
|
|
|
*/
|
2017-12-05 21:51:14 +01:00
|
|
|
async _resubmitTx (txMeta, latestBlockNumber) {
|
|
|
|
if (!txMeta.firstRetryBlockNumber) {
|
|
|
|
this.emit('tx:block-update', txMeta, latestBlockNumber)
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:51:09 +01:00
|
|
|
const firstRetryBlockNumber = txMeta.firstRetryBlockNumber || latestBlockNumber
|
2017-12-05 21:51:14 +01:00
|
|
|
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
|
2017-09-18 23:34:25 +02:00
|
|
|
const txHash = await this.publishTransaction(rawTx)
|
|
|
|
|
|
|
|
// Increment successful tries:
|
2017-09-27 07:42:59 +02:00
|
|
|
this.emit('tx:retry', txMeta)
|
2017-09-18 23:34:25 +02:00
|
|
|
return txHash
|
2017-08-04 20:41:35 +02:00
|
|
|
}
|
2018-05-28 23:29:31 +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-10-06 21:51:13 +02:00
|
|
|
|
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'
|
2017-09-27 01:55:11 +02:00
|
|
|
this.emit('tx:failed', txId, noTxHashErr)
|
2017-08-04 20:41:35 +02:00
|
|
|
return
|
|
|
|
}
|
2017-10-06 21:51:13 +02:00
|
|
|
|
|
|
|
// 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 {
|
2018-05-28 23:29:31 +02:00
|
|
|
const txParams = await this.query.getTransactionByHash(txHash)
|
2017-08-04 20:41:35 +02:00
|
|
|
if (!txParams) return
|
|
|
|
if (txParams.blockNumber) {
|
2018-05-29 09:41:28 +02:00
|
|
|
this.emit('tx:confirmed', txId)
|
2017-08-04 20:41:35 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
txMeta.warning = {
|
2017-10-03 00:39:11 +02:00
|
|
|
error: err.message,
|
2017-08-04 20:41:35 +02:00
|
|
|
message: 'There was a problem loading this transaction.',
|
|
|
|
}
|
2017-10-03 00:20:01 +02:00
|
|
|
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}
|
|
|
|
*/
|
2018-05-23 01:41:00 +02:00
|
|
|
|
|
|
|
|
2017-10-06 21:51:13 +02:00
|
|
|
async _checkIfNonceIsTaken (txMeta) {
|
2017-12-28 01:50:15 +01:00
|
|
|
const address = txMeta.txParams.from
|
|
|
|
const completed = this.getCompletedTransactions(address)
|
2017-10-06 21:51:13 +02:00
|
|
|
const sameNonce = completed.filter((otherMeta) => {
|
|
|
|
return otherMeta.txParams.nonce === txMeta.txParams.nonce
|
|
|
|
})
|
|
|
|
return sameNonce.length > 0
|
|
|
|
}
|
2017-08-24 07:29:08 +02:00
|
|
|
}
|
2018-04-13 22:18:45 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
module.exports = PendingTransactionTracker
|