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.
187 lines
6.4 KiB
187 lines
6.4 KiB
7 years ago
|
const EthQuery = require('ethjs-query')
|
||
7 years ago
|
const assert = require('assert')
|
||
7 years ago
|
const Mutex = require('await-semaphore').Mutex
|
||
7 years ago
|
/**
|
||
|
@param opts {Object}
|
||
|
@param {Object} opts.provider a ethereum provider
|
||
|
@param {Function} opts.getPendingTransactions a function that returns an array of txMeta
|
||
|
whosee status is `submitted`
|
||
|
@param {Function} opts.getConfirmedTransactions a function that returns an array of txMeta
|
||
|
whose status is `confirmed`
|
||
|
@class
|
||
|
*/
|
||
8 years ago
|
class NonceTracker {
|
||
|
|
||
7 years ago
|
constructor ({ provider, getPendingTransactions, getConfirmedTransactions }) {
|
||
7 years ago
|
this.provider = provider
|
||
8 years ago
|
this.ethQuery = new EthQuery(provider)
|
||
|
this.getPendingTransactions = getPendingTransactions
|
||
7 years ago
|
this.getConfirmedTransactions = getConfirmedTransactions
|
||
8 years ago
|
this.lockMap = {}
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
@returns {Promise<Object>} with the key releaseLock (the gloabl mutex)
|
||
|
*/
|
||
7 years ago
|
async getGlobalLock () {
|
||
|
const globalMutex = this._lookupMutex('global')
|
||
|
// await global mutex free
|
||
|
const releaseLock = await globalMutex.acquire()
|
||
|
return { releaseLock }
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* @typedef NonceDetails
|
||
|
* @property {number} highestLocallyConfirmed - A hex string of the highest nonce on a confirmed transaction.
|
||
|
* @property {number} nextNetworkNonce - The next nonce suggested by the eth_getTransactionCount method.
|
||
|
* @property {number} highetSuggested - The maximum between the other two, the number returned.
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
this will return an object with the `nextNonce` `nonceDetails` of type NonceDetails, and the releaseLock
|
||
|
Note: releaseLock must be called after adding a signed tx to pending transactions (or discarding).
|
||
|
|
||
|
@param address {string} the hex string for the address whose nonce we are calculating
|
||
|
@returns {Promise<NonceDetails>}
|
||
|
*/
|
||
8 years ago
|
async getNonceLock (address) {
|
||
7 years ago
|
// await global mutex free
|
||
|
await this._globalMutexFree()
|
||
7 years ago
|
// await lock free, then take lock
|
||
|
const releaseLock = await this._takeMutex(address)
|
||
7 years ago
|
// evaluate multiple nextNonce strategies
|
||
|
const nonceDetails = {}
|
||
|
const networkNonceResult = await this._getNetworkNextNonce(address)
|
||
7 years ago
|
const highestLocallyConfirmed = this._getHighestLocallyConfirmed(address)
|
||
7 years ago
|
const nextNetworkNonce = networkNonceResult.nonce
|
||
7 years ago
|
const highestSuggested = Math.max(nextNetworkNonce, highestLocallyConfirmed)
|
||
7 years ago
|
|
||
7 years ago
|
const pendingTxs = this.getPendingTransactions(address)
|
||
7 years ago
|
const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestSuggested) || 0
|
||
7 years ago
|
|
||
7 years ago
|
nonceDetails.params = {
|
||
7 years ago
|
highestLocallyConfirmed,
|
||
7 years ago
|
highestSuggested,
|
||
|
nextNetworkNonce,
|
||
|
}
|
||
|
nonceDetails.local = localNonceResult
|
||
|
nonceDetails.network = networkNonceResult
|
||
|
|
||
7 years ago
|
const nextNonce = Math.max(networkNonceResult.nonce, localNonceResult.nonce)
|
||
7 years ago
|
assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`)
|
||
7 years ago
|
|
||
7 years ago
|
// return nonce and release cb
|
||
|
return { nextNonce, nonceDetails, releaseLock }
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
async _getCurrentBlock () {
|
||
7 years ago
|
const blockTracker = this._getBlockTracker()
|
||
7 years ago
|
const currentBlock = blockTracker.getCurrentBlock()
|
||
8 years ago
|
if (currentBlock) return currentBlock
|
||
7 years ago
|
return await new Promise((reject, resolve) => {
|
||
7 years ago
|
blockTracker.once('latest', resolve)
|
||
8 years ago
|
})
|
||
|
}
|
||
|
|
||
7 years ago
|
async _globalMutexFree () {
|
||
|
const globalMutex = this._lookupMutex('global')
|
||
|
const release = await globalMutex.acquire()
|
||
|
release()
|
||
7 years ago
|
}
|
||
|
|
||
|
async _takeMutex (lockId) {
|
||
|
const mutex = this._lookupMutex(lockId)
|
||
|
const releaseLock = await mutex.acquire()
|
||
8 years ago
|
return releaseLock
|
||
|
}
|
||
|
|
||
7 years ago
|
_lookupMutex (lockId) {
|
||
|
let mutex = this.lockMap[lockId]
|
||
|
if (!mutex) {
|
||
|
mutex = new Mutex()
|
||
|
this.lockMap[lockId] = mutex
|
||
|
}
|
||
|
return mutex
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
async _getNetworkNextNonce (address) {
|
||
7 years ago
|
// calculate next nonce
|
||
|
// we need to make sure our base count
|
||
|
// and pending count are from the same block
|
||
|
const currentBlock = await this._getCurrentBlock()
|
||
|
const blockNumber = currentBlock.blockNumber
|
||
7 years ago
|
const baseCountBN = await this.ethQuery.getTransactionCount(address, blockNumber || 'latest')
|
||
7 years ago
|
const baseCount = baseCountBN.toNumber()
|
||
7 years ago
|
assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`)
|
||
7 years ago
|
const nonceDetails = { blockNumber, baseCount }
|
||
7 years ago
|
return { name: 'network', nonce: baseCount, details: nonceDetails }
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
_getHighestLocallyConfirmed (address) {
|
||
7 years ago
|
const confirmedTransactions = this.getConfirmedTransactions(address)
|
||
7 years ago
|
const highest = this._getHighestNonce(confirmedTransactions)
|
||
7 years ago
|
return Number.isInteger(highest) ? highest + 1 : 0
|
||
7 years ago
|
}
|
||
|
|
||
|
_reduceTxListToUniqueNonces (txList) {
|
||
|
const reducedTxList = txList.reduce((reducedList, txMeta, index) => {
|
||
|
if (!index) return [txMeta]
|
||
|
const nonceMatches = txList.filter((txData) => {
|
||
|
return txMeta.txParams.nonce === txData.txParams.nonce
|
||
|
})
|
||
|
if (nonceMatches.length > 1) return reducedList
|
||
|
reducedList.push(txMeta)
|
||
|
return reducedList
|
||
|
}, [])
|
||
|
return reducedTxList
|
||
|
}
|
||
|
|
||
|
_getHighestNonce (txList) {
|
||
7 years ago
|
const nonces = txList.map((txMeta) => {
|
||
|
const nonce = txMeta.txParams.nonce
|
||
|
assert(typeof nonce, 'string', 'nonces should be hex strings')
|
||
|
return parseInt(nonce, 16)
|
||
|
})
|
||
7 years ago
|
const highestNonce = Math.max.apply(null, nonces)
|
||
|
return highestNonce
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
@typedef {object} highestContinuousFrom
|
||
|
@property {string} - name the name for how the nonce was calculated based on the data used
|
||
|
@property {number} - nonce the next suggested nonce
|
||
|
@property {object} - details the provided starting nonce that was used (for debugging)
|
||
|
*/
|
||
|
/**
|
||
|
@param txList {array} - list of txMeta's
|
||
|
@param startPoint {number} - the highest known locally confirmed nonce
|
||
|
@returns {highestContinuousFrom}
|
||
|
*/
|
||
7 years ago
|
_getHighestContinuousFrom (txList, startPoint) {
|
||
7 years ago
|
const nonces = txList.map((txMeta) => {
|
||
|
const nonce = txMeta.txParams.nonce
|
||
|
assert(typeof nonce, 'string', 'nonces should be hex strings')
|
||
|
return parseInt(nonce, 16)
|
||
|
})
|
||
7 years ago
|
|
||
|
let highest = startPoint
|
||
7 years ago
|
while (nonces.includes(highest)) {
|
||
7 years ago
|
highest++
|
||
|
}
|
||
|
|
||
7 years ago
|
return { name: 'local', nonce: highest, details: { startPoint, highest } }
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
// this is a hotfix for the fact that the blockTracker will
|
||
|
// change when the network changes
|
||
7 years ago
|
|
||
|
/**
|
||
|
@returns {Object} the current blockTracker
|
||
|
*/
|
||
7 years ago
|
_getBlockTracker () {
|
||
|
return this.provider._blockTracker
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
module.exports = NonceTracker
|