|
|
@ -11,9 +11,18 @@ 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 global mutex free
|
|
|
|
|
|
|
|
await this._globalMutexFree() |
|
|
|
// await lock free, then take lock
|
|
|
|
// await lock free, then take lock
|
|
|
|
const releaseLock = await this._takeMutex(address) |
|
|
|
const releaseLock = await this._takeMutex(address) |
|
|
|
// calculate next nonce
|
|
|
|
// calculate next nonce
|
|
|
@ -40,13 +49,19 @@ class NonceTracker { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_lookupMutex (lockId) { |
|
|
|
async _getTxCount (address, currentBlock) { |
|
|
|
let mutex = this.lockMap[lockId] |
|
|
|
const blockNumber = currentBlock.number |
|
|
|
if (!mutex) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
mutex = new Mutex() |
|
|
|
this.ethQuery.getTransactionCount(address, blockNumber, (err, result) => { |
|
|
|
this.lockMap[lockId] = mutex |
|
|
|
err ? reject(err) : resolve(result) |
|
|
|
} |
|
|
|
}) |
|
|
|
return mutex |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _globalMutexFree () { |
|
|
|
|
|
|
|
const globalMutex = this._lookupMutex('global') |
|
|
|
|
|
|
|
const release = await globalMutex.acquire() |
|
|
|
|
|
|
|
release() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async _takeMutex (lockId) { |
|
|
|
async _takeMutex (lockId) { |
|
|
@ -55,13 +70,13 @@ class NonceTracker { |
|
|
|
return releaseLock |
|
|
|
return releaseLock |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async _getTxCount (address, currentBlock) { |
|
|
|
_lookupMutex (lockId) { |
|
|
|
const blockNumber = currentBlock.number |
|
|
|
let mutex = this.lockMap[lockId] |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
if (!mutex) { |
|
|
|
this.ethQuery.getTransactionCount(address, blockNumber, (err, result) => { |
|
|
|
mutex = new Mutex() |
|
|
|
err ? reject(err) : resolve(result) |
|
|
|
this.lockMap[lockId] = mutex |
|
|
|
}) |
|
|
|
} |
|
|
|
}) |
|
|
|
return mutex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|