diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-08-19 07:05:21 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-08-19 07:05:21 +0800 |
commit | 1ffb40648066189cd9e3abffc94299bbeecb6334 (patch) | |
tree | 3d6c3c9aa19fd0b305c09a1011b891ab19b93530 /app | |
parent | 37f86e874f8c3c3d5f6d78cd8abeb0c835f20ae2 (diff) | |
download | tangerine-wallet-browser-1ffb40648066189cd9e3abffc94299bbeecb6334.tar tangerine-wallet-browser-1ffb40648066189cd9e3abffc94299bbeecb6334.tar.gz tangerine-wallet-browser-1ffb40648066189cd9e3abffc94299bbeecb6334.tar.bz2 tangerine-wallet-browser-1ffb40648066189cd9e3abffc94299bbeecb6334.tar.lz tangerine-wallet-browser-1ffb40648066189cd9e3abffc94299bbeecb6334.tar.xz tangerine-wallet-browser-1ffb40648066189cd9e3abffc94299bbeecb6334.tar.zst tangerine-wallet-browser-1ffb40648066189cd9e3abffc94299bbeecb6334.zip |
break out network nonce calc.
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/lib/nonce-tracker.js | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 3063209ee..01d8e5f19 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -26,23 +26,13 @@ class NonceTracker { await this._globalMutexFree() // await lock free, then take lock const releaseLock = await this._takeMutex(address) - // calculate next nonce - // we need to make sure our base count - // and pending count are from the same block const localNextNonce = this._getLocalNextNonce(address) - // throw out localNonce if not a number - const currentBlock = await this._getCurrentBlock() - const pendingTransactions = this.getPendingTransactions(address) - const pendingCount = pendingTransactions.length - assert(Number.isInteger(pendingCount), `nonce-tracker - pendingCount is not an integer - got: (${typeof pendingCount}) "${pendingCount}"`) - const baseCountHex = await this._getTxCount(address, currentBlock) - const baseCount = parseInt(baseCountHex, 16) - assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) - const nextNonce = Math.max(baseCount + pendingCount, localNextNonce) + const nonceDetails = await this._getNetworkNonceAndDetails(address) + const networkNonce = nonceDetails.networkNonce + const nextNonce = Math.max(networkNonce, localNextNonce) assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`) // collect the numbers used to calculate the nonce for debugging - const blockNumber = currentBlock.number - const nonceDetails = { blockNumber, baseCount, baseCountHex, pendingCount, localNextNonce } + nonceDetails.localNextNonce = localNextNonce // return nonce and release cb return { nextNonce, nonceDetails, releaseLock } } @@ -86,9 +76,21 @@ class NonceTracker { return mutex } - // _getNetworkNonce (address) { - - // } + async _getNetworkNonceAndDetails (address) { + // 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 + const pendingTransactions = this.getPendingTransactions(address) + const pendingCount = pendingTransactions.length + assert(Number.isInteger(pendingCount), `nonce-tracker - pendingCount is not an integer - got: (${typeof pendingCount}) "${pendingCount}"`) + const baseCountHex = await this._getTxCount(address, currentBlock) + const baseCount = parseInt(baseCountHex, 16) + assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) + const networkNonce = baseCount + pendingCount + return {networkNonce, blockNumber, baseCountHex, baseCount, pendingCount} + } _getLocalNextNonce (address) { const confirmedTransactions = this.getConfirmedTransactions(address) @@ -99,6 +101,8 @@ class NonceTracker { return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce }, '0x0') let localNonce = parseInt(localNonceHex, 16) + // throw out localNonce if not a number + if (!Number.isInteger(localNonce)) localNonce = 0 if ( // the local nonce is not 0 localNonce || |