You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
6.3 KiB
190 lines
6.3 KiB
7 years ago
|
const EventEmitter = require('events')
|
||
|
const EthQuery = require('ethjs-query')
|
||
|
/*
|
||
|
|
||
|
Utility class for tracking the transactions as they
|
||
|
go from a pending state to a confirmed (mined in a block) state
|
||
|
|
||
|
As well as continues broadcast while in the pending state
|
||
|
|
||
|
~config is not optional~
|
||
|
requires a: {
|
||
|
provider: //,
|
||
|
nonceTracker: //see nonce tracker,
|
||
|
getPendingTransactions: //() a function for getting an array of transactions,
|
||
|
publishTransaction: //(rawTx) a async function for publishing raw transactions,
|
||
|
}
|
||
|
|
||
|
*/
|
||
|
|
||
7 years ago
|
module.exports = class PendingTransactionTracker extends EventEmitter {
|
||
7 years ago
|
constructor (config) {
|
||
|
super()
|
||
|
this.query = new EthQuery(config.provider)
|
||
|
this.nonceTracker = config.nonceTracker
|
||
7 years ago
|
// default is one day
|
||
7 years ago
|
this.getPendingTransactions = config.getPendingTransactions
|
||
7 years ago
|
this.getCompletedTransactions = config.getCompletedTransactions
|
||
7 years ago
|
this.publishTransaction = config.publishTransaction
|
||
7 years ago
|
this._checkPendingTxs()
|
||
7 years ago
|
}
|
||
|
|
||
|
// checks if a signed tx is in a block and
|
||
|
// if included sets the tx status as 'confirmed'
|
||
|
checkForTxInBlock (block) {
|
||
|
const signedTxList = this.getPendingTransactions()
|
||
|
if (!signedTxList.length) return
|
||
|
signedTxList.forEach((txMeta) => {
|
||
|
const txHash = txMeta.hash
|
||
|
const txId = txMeta.id
|
||
|
|
||
|
if (!txHash) {
|
||
|
const noTxHashErr = new Error('We had an error while submitting this transaction, please try again.')
|
||
|
noTxHashErr.name = 'NoTxHashError'
|
||
7 years ago
|
this.emit('tx:failed', txId, noTxHashErr)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
|
||
|
block.transactions.forEach((tx) => {
|
||
7 years ago
|
if (tx.hash === txHash) this.emit('tx:confirmed', txId)
|
||
7 years ago
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
7 years ago
|
queryPendingTxs ({ oldBlock, newBlock }) {
|
||
7 years ago
|
// check pending transactions on start
|
||
|
if (!oldBlock) {
|
||
|
this._checkPendingTxs()
|
||
|
return
|
||
|
}
|
||
|
// if we synced by more than one block, check for missed pending transactions
|
||
|
const diff = Number.parseInt(newBlock.number, 16) - Number.parseInt(oldBlock.number, 16)
|
||
|
if (diff > 1) this._checkPendingTxs()
|
||
|
}
|
||
|
|
||
|
|
||
7 years ago
|
resubmitPendingTxs (block) {
|
||
7 years ago
|
const pending = this.getPendingTransactions()
|
||
7 years ago
|
// only try resubmitting if their are transactions to resubmit
|
||
|
if (!pending.length) return
|
||
7 years ago
|
pending.forEach((txMeta) => this._resubmitTx(txMeta, block.number).catch((err) => {
|
||
7 years ago
|
/*
|
||
|
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"
|
||
7 years ago
|
|
||
|
Also don't mark as failed if it has ever been broadcast successfully.
|
||
|
A successful broadcast means it may still be mined.
|
||
7 years ago
|
*/
|
||
|
const errorMessage = err.message.toLowerCase()
|
||
|
const isKnownTx = (
|
||
|
// geth
|
||
7 years ago
|
errorMessage.includes('replacement transaction underpriced') ||
|
||
|
errorMessage.includes('known transaction') ||
|
||
7 years ago
|
// parity
|
||
7 years ago
|
errorMessage.includes('gas price too low to replace') ||
|
||
|
errorMessage.includes('transaction with the same hash was already imported') ||
|
||
7 years ago
|
// other
|
||
7 years ago
|
errorMessage.includes('gateway timeout') ||
|
||
|
errorMessage.includes('nonce too low')
|
||
7 years ago
|
)
|
||
|
// ignore resubmit warnings, return early
|
||
|
if (isKnownTx) return
|
||
|
// encountered real error - transition to error state
|
||
7 years ago
|
txMeta.warning = {
|
||
|
error: errorMessage,
|
||
|
message: 'There was an error when resubmitting this transaction.',
|
||
|
}
|
||
7 years ago
|
this.emit('tx:warning', txMeta, err)
|
||
7 years ago
|
}))
|
||
|
}
|
||
|
|
||
7 years ago
|
async _resubmitTx (txMeta, latestBlockNumber) {
|
||
|
if (!txMeta.firstRetryBlockNumber) {
|
||
|
this.emit('tx:block-update', txMeta, latestBlockNumber)
|
||
|
}
|
||
|
|
||
7 years ago
|
const firstRetryBlockNumber = txMeta.firstRetryBlockNumber || latestBlockNumber
|
||
7 years ago
|
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
|
||
|
|
||
7 years ago
|
// Only auto-submit already-signed txs:
|
||
|
if (!('rawTx' in txMeta)) return
|
||
|
|
||
|
const rawTx = txMeta.rawTx
|
||
7 years ago
|
const txHash = await this.publishTransaction(rawTx)
|
||
|
|
||
|
// Increment successful tries:
|
||
7 years ago
|
this.emit('tx:retry', txMeta)
|
||
7 years ago
|
return txHash
|
||
7 years ago
|
}
|
||
|
|
||
|
async _checkPendingTx (txMeta) {
|
||
|
const txHash = txMeta.hash
|
||
|
const txId = txMeta.id
|
||
7 years ago
|
|
||
7 years ago
|
// 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'
|
||
7 years ago
|
this.emit('tx:failed', txId, noTxHashErr)
|
||
7 years ago
|
return
|
||
|
}
|
||
7 years ago
|
|
||
|
// 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)
|
||
|
}
|
||
|
|
||
7 years ago
|
// get latest transaction status
|
||
|
let txParams
|
||
|
try {
|
||
|
txParams = await this.query.getTransactionByHash(txHash)
|
||
|
if (!txParams) return
|
||
|
if (txParams.blockNumber) {
|
||
7 years ago
|
this.emit('tx:confirmed', txId)
|
||
7 years ago
|
}
|
||
|
} catch (err) {
|
||
|
txMeta.warning = {
|
||
7 years ago
|
error: err.message,
|
||
7 years ago
|
message: 'There was a problem loading this transaction.',
|
||
|
}
|
||
7 years ago
|
this.emit('tx:warning', txMeta, err)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// checks the network for signed txs and
|
||
|
// if confirmed sets the tx status as 'confirmed'
|
||
|
async _checkPendingTxs () {
|
||
|
const signedTxList = this.getPendingTransactions()
|
||
|
// in order to keep the nonceTracker accurate we block it while updating pending transactions
|
||
|
const nonceGlobalLock = await this.nonceTracker.getGlobalLock()
|
||
|
try {
|
||
|
await Promise.all(signedTxList.map((txMeta) => this._checkPendingTx(txMeta)))
|
||
|
} catch (err) {
|
||
|
console.error('PendingTransactionWatcher - Error updating pending transactions')
|
||
|
console.error(err)
|
||
|
}
|
||
|
nonceGlobalLock.releaseLock()
|
||
|
}
|
||
7 years ago
|
|
||
|
async _checkIfNonceIsTaken (txMeta) {
|
||
7 years ago
|
const address = txMeta.txParams.from
|
||
|
const completed = this.getCompletedTransactions(address)
|
||
7 years ago
|
const sameNonce = completed.filter((otherMeta) => {
|
||
|
return otherMeta.txParams.nonce === txMeta.txParams.nonce
|
||
|
})
|
||
|
return sameNonce.length > 0
|
||
|
}
|
||
|
|
||
7 years ago
|
}
|