From 1ffb40648066189cd9e3abffc94299bbeecb6334 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 18 Aug 2017 16:05:21 -0700 Subject: [PATCH] break out network nonce calc. --- app/scripts/lib/nonce-tracker.js | 38 ++++++++++++++++++-------------- 1 file 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 ||