|
|
@ -1,4 +1,6 @@ |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
const EthQuery = require('eth-query') |
|
|
|
|
|
|
|
const assert = require('assert') |
|
|
|
|
|
|
|
const Mutex = require('await-semaphore').Mutex |
|
|
|
|
|
|
|
|
|
|
|
class NonceTracker { |
|
|
|
class NonceTracker { |
|
|
|
|
|
|
|
|
|
|
@ -9,22 +11,34 @@ class NonceTracker { |
|
|
|
this.lockMap = {} |
|
|
|
this.lockMap = {} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getGlobalLock () { |
|
|
|
|
|
|
|
const globalMutex = this._lookupMutex('global') |
|
|
|
|
|
|
|
// await global mutex free
|
|
|
|
|
|
|
|
const releaseLock = await globalMutex.acquire() |
|
|
|
|
|
|
|
return { releaseLock } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// releaseLock must be called
|
|
|
|
// releaseLock must be called
|
|
|
|
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
|
|
|
|
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
|
|
|
|
async getNonceLock (address) { |
|
|
|
async getNonceLock (address) { |
|
|
|
// await lock free
|
|
|
|
// await global mutex free
|
|
|
|
await this.lockMap[address] |
|
|
|
await this._globalMutexFree() |
|
|
|
// take lock
|
|
|
|
// await lock free, then take lock
|
|
|
|
const releaseLock = this._takeLock(address) |
|
|
|
const releaseLock = await this._takeMutex(address) |
|
|
|
// calculate next nonce
|
|
|
|
// calculate next nonce
|
|
|
|
// we need to make sure our base count
|
|
|
|
// we need to make sure our base count
|
|
|
|
// and pending count are from the same block
|
|
|
|
// and pending count are from the same block
|
|
|
|
const currentBlock = await this._getCurrentBlock() |
|
|
|
const currentBlock = await this._getCurrentBlock() |
|
|
|
const pendingTransactions = this.getPendingTransactions(address) |
|
|
|
const pendingTransactions = this.getPendingTransactions(address) |
|
|
|
const baseCount = await this._getTxCount(address, currentBlock) |
|
|
|
const pendingCount = pendingTransactions.length |
|
|
|
const nextNonce = parseInt(baseCount) + pendingTransactions.length |
|
|
|
assert(Number.isInteger(pendingCount), 'nonce-tracker - pendingCount is an integer') |
|
|
|
|
|
|
|
const baseCountHex = await this._getTxCount(address, currentBlock) |
|
|
|
|
|
|
|
const baseCount = parseInt(baseCountHex, 16) |
|
|
|
|
|
|
|
assert(Number.isInteger(baseCount), 'nonce-tracker - baseCount is an integer') |
|
|
|
|
|
|
|
const nextNonce = baseCount + pendingCount |
|
|
|
|
|
|
|
assert(Number.isInteger(nextNonce), 'nonce-tracker - nextNonce is an integer') |
|
|
|
// return next nonce and release cb
|
|
|
|
// return next nonce and release cb
|
|
|
|
return { nextNonce: nextNonce.toString(16), releaseLock } |
|
|
|
return { nextNonce, releaseLock } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async _getCurrentBlock () { |
|
|
|
async _getCurrentBlock () { |
|
|
@ -35,16 +49,6 @@ class NonceTracker { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_takeLock (lockId) { |
|
|
|
|
|
|
|
let releaseLock = null |
|
|
|
|
|
|
|
// create and store lock
|
|
|
|
|
|
|
|
const lock = new Promise((resolve, reject) => { releaseLock = resolve }) |
|
|
|
|
|
|
|
this.lockMap[lockId] = lock |
|
|
|
|
|
|
|
// setup lock teardown
|
|
|
|
|
|
|
|
lock.then(() => delete this.lockMap[lockId]) |
|
|
|
|
|
|
|
return releaseLock |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _getTxCount (address, currentBlock) { |
|
|
|
async _getTxCount (address, currentBlock) { |
|
|
|
const blockNumber = currentBlock.number |
|
|
|
const blockNumber = currentBlock.number |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
@ -54,6 +58,27 @@ class NonceTracker { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _globalMutexFree () { |
|
|
|
|
|
|
|
const globalMutex = this._lookupMutex('global') |
|
|
|
|
|
|
|
const release = await globalMutex.acquire() |
|
|
|
|
|
|
|
release() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _takeMutex (lockId) { |
|
|
|
|
|
|
|
const mutex = this._lookupMutex(lockId) |
|
|
|
|
|
|
|
const releaseLock = await mutex.acquire() |
|
|
|
|
|
|
|
return releaseLock |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_lookupMutex (lockId) { |
|
|
|
|
|
|
|
let mutex = this.lockMap[lockId] |
|
|
|
|
|
|
|
if (!mutex) { |
|
|
|
|
|
|
|
mutex = new Mutex() |
|
|
|
|
|
|
|
this.lockMap[lockId] = mutex |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return mutex |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = NonceTracker |
|
|
|
module.exports = NonceTracker |
|
|
|