2020-01-09 04:34:58 +01:00
|
|
|
import EventEmitter from 'safe-event-emitter'
|
|
|
|
import log from 'loglevel'
|
|
|
|
import EthQuery from '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>
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {Object} config - 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()
|
2019-05-16 07:36:53 +02:00
|
|
|
this.droppedBuffer = {}
|
2020-04-23 17:49:04 +02:00
|
|
|
this.query = config.query || (new EthQuery(config.provider))
|
2017-08-04 20:41:35 +02:00
|
|
|
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-11-14 22:34:07 +01:00
|
|
|
this.approveTransaction = config.approveTransaction
|
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
|
|
|
/**
|
2020-04-23 17:49:04 +02:00
|
|
|
* Resubmits each pending transaction
|
|
|
|
* @param {string} blockNumber - the latest block number in hex
|
|
|
|
* @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
|
2019-11-20 01:03:20 +01:00
|
|
|
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
|
2019-11-20 01:03:20 +01:00
|
|
|
if (isKnownTx) {
|
|
|
|
return
|
|
|
|
}
|
2017-08-04 20:41:35 +02:00
|
|
|
// 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
|
|
|
/**
|
2020-04-23 17:49:04 +02:00
|
|
|
* Attempts to resubmit the given transaction with exponential backoff
|
|
|
|
*
|
|
|
|
* Will only attempt to retry the given tx every {@code 2**(txMeta.retryCount)} blocks.
|
|
|
|
*
|
|
|
|
* @param {Object} txMeta - the transaction metadata
|
|
|
|
* @param {string} latestBlockNumber - the latest block number in hex
|
|
|
|
* @returns {Promise<string|undefined>} the tx hash if retried
|
|
|
|
* @emits tx:block-update
|
|
|
|
* @emits tx:retry
|
|
|
|
* @private
|
|
|
|
*/
|
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
|
2019-11-20 01:03:20 +01:00
|
|
|
if (txBlockDistance <= Math.pow(2, retryCount) - 1) {
|
|
|
|
return
|
|
|
|
}
|
2017-12-05 21:51:14 +01:00
|
|
|
|
2017-08-04 20:41:35 +02:00
|
|
|
// Only auto-submit already-signed txs:
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!('rawTx' in txMeta)) {
|
|
|
|
return this.approveTransaction(txMeta.id)
|
|
|
|
}
|
2017-08-04 20:41:35 +02:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2020-04-23 17:49:04 +02:00
|
|
|
* Query the network to see if the given {@code txMeta} has been included in a block
|
|
|
|
* @param {Object} txMeta - the transaction metadata
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
* @emits tx:confirmed
|
|
|
|
* @emits tx:dropped
|
|
|
|
* @emits tx:failed
|
|
|
|
* @emits tx:warning
|
|
|
|
* @private
|
|
|
|
*/
|
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
|
|
|
|
2018-11-14 22:34:07 +01:00
|
|
|
// Only check submitted txs
|
2019-11-20 01:03:20 +01:00
|
|
|
if (txMeta.status !== 'submitted') {
|
|
|
|
return
|
|
|
|
}
|
2018-11-14 22:34:07 +01: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)
|
2019-05-16 07:36:53 +02:00
|
|
|
|
2017-08-04 20:41:35 +02:00
|
|
|
return
|
|
|
|
}
|
2019-12-05 20:34:10 +01:00
|
|
|
// *note to self* hard failure point
|
|
|
|
const transactionReceipt = await this.query.getTransactionReceipt(txHash)
|
|
|
|
|
2017-10-06 21:51:13 +02:00
|
|
|
|
2019-05-16 07:36:53 +02:00
|
|
|
// If another tx with the same nonce is mined, set as dropped.
|
2017-10-06 21:51:13 +02:00
|
|
|
const taken = await this._checkIfNonceIsTaken(txMeta)
|
2019-05-16 07:36:53 +02:00
|
|
|
let dropped
|
|
|
|
try {
|
|
|
|
// check the network if the nonce is ahead the tx
|
|
|
|
// and the tx has not been mined into a block
|
2020-04-22 21:33:59 +02:00
|
|
|
dropped = await this._checkIfTxWasDropped(txMeta, transactionReceipt)
|
2020-04-17 18:53:46 +02:00
|
|
|
|
2019-05-16 07:36:53 +02:00
|
|
|
// the dropped buffer is in case we ask a node for the tx
|
|
|
|
// that is behind the node we asked for tx count
|
|
|
|
// IS A SECURITY FOR HITTING NODES IN INFURA THAT COULD GO OUT
|
|
|
|
// OF SYNC.
|
|
|
|
// on the next block event it will return fire as dropped
|
2019-12-05 20:34:10 +01:00
|
|
|
if (typeof this.droppedBuffer[txHash] !== 'number') {
|
|
|
|
this.droppedBuffer[txHash] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3 block count buffer
|
|
|
|
if (dropped && this.droppedBuffer[txHash] < 3) {
|
2019-05-16 07:36:53 +02:00
|
|
|
dropped = false
|
2019-12-05 20:34:10 +01:00
|
|
|
++this.droppedBuffer[txHash]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dropped && this.droppedBuffer[txHash] === 3) {
|
2019-05-16 07:36:53 +02:00
|
|
|
// clean up
|
|
|
|
delete this.droppedBuffer[txHash]
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.error(e)
|
|
|
|
}
|
2020-04-17 18:53:46 +02:00
|
|
|
|
2019-05-16 07:36:53 +02:00
|
|
|
if (taken || dropped) {
|
|
|
|
return this.emit('tx:dropped', txId)
|
2017-10-06 21:51:13 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 20:41:35 +02:00
|
|
|
// get latest transaction status
|
2020-04-17 18:53:46 +02:00
|
|
|
if (transactionReceipt?.blockNumber) {
|
|
|
|
this.emit('tx:confirmed', txId, transactionReceipt)
|
|
|
|
} else {
|
|
|
|
const err = new Error('Missing transaction receipt or block number.')
|
2017-08-04 20:41:35 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2019-05-16 07:36:53 +02:00
|
|
|
|
2020-04-23 17:49:04 +02:00
|
|
|
/**
|
|
|
|
* Checks whether the nonce in the given {@code txMeta} is correct against the network
|
|
|
|
* @param {Object} txMeta - the transaction metadata
|
|
|
|
* @param {Object} [transactionReceipt] - the transaction receipt
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
* @private
|
|
|
|
*/
|
2020-04-22 21:33:59 +02:00
|
|
|
async _checkIfTxWasDropped (txMeta, transactionReceipt) {
|
2019-12-05 20:34:10 +01:00
|
|
|
const { txParams: { nonce, from } } = txMeta
|
2019-05-16 07:36:53 +02:00
|
|
|
const nextNonce = await this.query.getTransactionCount(from)
|
2020-04-17 18:53:46 +02:00
|
|
|
if (
|
|
|
|
!transactionReceipt?.blockNumber &&
|
|
|
|
parseInt(nextNonce) > parseInt(nonce)
|
|
|
|
) {
|
2019-07-31 22:17:11 +02:00
|
|
|
return true
|
2019-05-16 07:36:53 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-08-04 20:41:35 +02:00
|
|
|
|
2018-04-13 22:18:45 +02:00
|
|
|
/**
|
2020-04-23 17:49:04 +02:00
|
|
|
* Checks whether the nonce in the given {@code txMeta} is correct against the local set of transactions
|
|
|
|
* @param {Object} txMeta - the transaction metadata
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
* @private
|
|
|
|
*/
|
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) => {
|
2019-12-05 20:34:10 +01:00
|
|
|
if (otherMeta.id === txMeta.id) {
|
|
|
|
return false
|
|
|
|
}
|
2017-10-06 21:51:13 +02:00
|
|
|
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
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default PendingTransactionTracker
|