|
|
|
@ -12,15 +12,14 @@ class NonceTracker { |
|
|
|
|
// releaseLock must be called
|
|
|
|
|
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
|
|
|
|
|
async getNonceLock (address) { |
|
|
|
|
const pendingTransactions = this.getPendingTransactions(address) |
|
|
|
|
// await lock free
|
|
|
|
|
await this.lockMap[address] |
|
|
|
|
if (pendingTransactions.length) await this.lockMap[address] |
|
|
|
|
else if (this.lockMap[address]) await this.lockMap[address]() |
|
|
|
|
// take lock
|
|
|
|
|
const releaseLock = this._takeLock(address) |
|
|
|
|
// calculate next nonce
|
|
|
|
|
const currentBlock = await this._getCurrentBlock() |
|
|
|
|
const blockNumber = currentBlock.number |
|
|
|
|
const pendingTransactions = this.getPendingTransactions(address) |
|
|
|
|
const baseCount = await this._getTxCount(address, blockNumber) |
|
|
|
|
const baseCount = await this._getTxCount(address) |
|
|
|
|
const nextNonce = parseInt(baseCount) + pendingTransactions.length |
|
|
|
|
// return next nonce and release cb
|
|
|
|
|
return { nextNonce: nextNonce.toString(16), releaseLock } |
|
|
|
@ -44,7 +43,9 @@ class NonceTracker { |
|
|
|
|
return releaseLock |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_getTxCount (address, blockNumber) { |
|
|
|
|
async _getTxCount (address) { |
|
|
|
|
const currentBlock = await this._getCurrentBlock() |
|
|
|
|
const blockNumber = currentBlock.number |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
this.ethQuery.getTransactionCount(address, blockNumber, (err, result) => { |
|
|
|
|
err ? reject(err) : resolve(result) |
|
|
|
|