2017-06-22 02:28:19 +02:00
|
|
|
const EthQuery = require('eth-query')
|
2017-07-18 21:48:16 +02:00
|
|
|
const assert = require('assert')
|
2017-07-19 00:11:29 +02:00
|
|
|
const Mutex = require('await-semaphore').Mutex
|
2017-06-15 07:16:14 +02:00
|
|
|
|
|
|
|
class NonceTracker {
|
|
|
|
|
2017-07-26 20:37:00 +02:00
|
|
|
constructor ({ provider, getPendingTransactions }) {
|
|
|
|
this.provider = provider
|
2017-06-15 07:16:14 +02:00
|
|
|
this.ethQuery = new EthQuery(provider)
|
|
|
|
this.getPendingTransactions = getPendingTransactions
|
|
|
|
this.lockMap = {}
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:27:15 +02:00
|
|
|
async getGlobalLock () {
|
|
|
|
const globalMutex = this._lookupMutex('global')
|
|
|
|
// await global mutex free
|
|
|
|
const releaseLock = await globalMutex.acquire()
|
|
|
|
return { releaseLock }
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:16:14 +02:00
|
|
|
// releaseLock must be called
|
|
|
|
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
|
2017-06-22 04:51:00 +02:00
|
|
|
async getNonceLock (address) {
|
2017-07-19 00:27:15 +02:00
|
|
|
// await global mutex free
|
|
|
|
await this._globalMutexFree()
|
2017-07-19 00:11:29 +02:00
|
|
|
// await lock free, then take lock
|
|
|
|
const releaseLock = await this._takeMutex(address)
|
2017-06-15 07:16:14 +02:00
|
|
|
// calculate next nonce
|
2017-07-05 21:00:42 +02:00
|
|
|
// we need to make sure our base count
|
|
|
|
// and pending count are from the same block
|
|
|
|
const currentBlock = await this._getCurrentBlock()
|
|
|
|
const pendingTransactions = this.getPendingTransactions(address)
|
2017-07-18 21:48:16 +02:00
|
|
|
const pendingCount = pendingTransactions.length
|
2017-07-26 19:16:08 +02:00
|
|
|
assert(Number.isInteger(pendingCount), `nonce-tracker - pendingCount is not an integer - got: (${typeof pendingCount}) "${pendingCount}"`)
|
2017-07-18 21:48:16 +02:00
|
|
|
const baseCountHex = await this._getTxCount(address, currentBlock)
|
|
|
|
const baseCount = parseInt(baseCountHex, 16)
|
2017-07-26 19:16:08 +02:00
|
|
|
assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`)
|
2017-07-18 21:48:16 +02:00
|
|
|
const nextNonce = baseCount + pendingCount
|
2017-07-26 19:16:08 +02:00
|
|
|
assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`)
|
2017-07-26 19:40:08 +02:00
|
|
|
// collect the numbers used to calculate the nonce for debugging
|
|
|
|
const blockNumber = currentBlock.number
|
2017-07-26 20:09:02 +02:00
|
|
|
const nonceDetails = { blockNumber, baseCount, baseCountHex, pendingCount }
|
2017-07-26 19:40:08 +02:00
|
|
|
// return nonce and release cb
|
|
|
|
return { nextNonce, nonceDetails, releaseLock }
|
2017-06-15 07:16:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 04:51:00 +02:00
|
|
|
async _getCurrentBlock () {
|
2017-07-26 20:37:00 +02:00
|
|
|
const blockTracker = this._getBlockTracker()
|
|
|
|
const currentBlock = blockTracker.getCurrentBlock()
|
2017-06-15 07:16:14 +02:00
|
|
|
if (currentBlock) return currentBlock
|
|
|
|
return await Promise((reject, resolve) => {
|
2017-07-26 20:37:00 +02:00
|
|
|
blockTracker.once('latest', resolve)
|
2017-06-15 07:16:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:27:15 +02:00
|
|
|
async _getTxCount (address, currentBlock) {
|
|
|
|
const blockNumber = currentBlock.number
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.ethQuery.getTransactionCount(address, blockNumber, (err, result) => {
|
|
|
|
err ? reject(err) : resolve(result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async _globalMutexFree () {
|
|
|
|
const globalMutex = this._lookupMutex('global')
|
|
|
|
const release = await globalMutex.acquire()
|
|
|
|
release()
|
2017-07-19 00:11:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async _takeMutex (lockId) {
|
|
|
|
const mutex = this._lookupMutex(lockId)
|
|
|
|
const releaseLock = await mutex.acquire()
|
2017-06-15 07:16:14 +02:00
|
|
|
return releaseLock
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:27:15 +02:00
|
|
|
_lookupMutex (lockId) {
|
|
|
|
let mutex = this.lockMap[lockId]
|
|
|
|
if (!mutex) {
|
|
|
|
mutex = new Mutex()
|
|
|
|
this.lockMap[lockId] = mutex
|
|
|
|
}
|
|
|
|
return mutex
|
2017-06-22 02:28:19 +02:00
|
|
|
}
|
|
|
|
|
2017-07-26 20:37:00 +02:00
|
|
|
// this is a hotfix for the fact that the blockTracker will
|
|
|
|
// change when the network changes
|
|
|
|
_getBlockTracker () {
|
|
|
|
return this.provider._blockTracker
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = NonceTracker
|