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.
149 lines
4.9 KiB
149 lines
4.9 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
|
||
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
|
async getGlobalLock () {
|
||
|
const globalMutex = this._lookupMutex('global')
|
||
|
// await global mutex free
|
||
|
const releaseLock = await globalMutex.acquire()
|
||
|
return { releaseLock }
|
||
|
}
|
||
|
|
||
8 years ago
|
// releaseLock must be called
|
||
|
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
|
||
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
|
_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
|
||
|
_getBlockTracker () {
|
||
|
return this.provider._blockTracker
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
module.exports = NonceTracker
|