diff options
author | frankiebee <frankie.diamond@gmail.com> | 2017-08-22 08:04:47 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2017-08-22 08:04:47 +0800 |
commit | 7d34b22d78713aa1a12dbdab0559ccaf4c1be93c (patch) | |
tree | 744b0de1011f7ff17e280d0bc2b276a806f3902a | |
parent | 5c74f316a8adbcbcca642f9ba0734bd1fc181bc1 (diff) | |
download | tangerine-wallet-browser-7d34b22d78713aa1a12dbdab0559ccaf4c1be93c.tar tangerine-wallet-browser-7d34b22d78713aa1a12dbdab0559ccaf4c1be93c.tar.gz tangerine-wallet-browser-7d34b22d78713aa1a12dbdab0559ccaf4c1be93c.tar.bz2 tangerine-wallet-browser-7d34b22d78713aa1a12dbdab0559ccaf4c1be93c.tar.lz tangerine-wallet-browser-7d34b22d78713aa1a12dbdab0559ccaf4c1be93c.tar.xz tangerine-wallet-browser-7d34b22d78713aa1a12dbdab0559ccaf4c1be93c.tar.zst tangerine-wallet-browser-7d34b22d78713aa1a12dbdab0559ccaf4c1be93c.zip |
clean up code
-rw-r--r-- | app/scripts/lib/nonce-tracker.js | 82 |
1 files changed, 43 insertions, 39 deletions
diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 92a9cf4f1..c20459650 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -37,25 +37,6 @@ class NonceTracker { return { nextNonce, nonceDetails, releaseLock } } - getPendingTransactionCount (address) { - const pendingTransactions = this.getPendingTransactions(address) - return this.reduceTxListToUniqueNonces(pendingTransactions).length - } - - - reduceTxListToUniqueNonces (txList) { - const reducedTxList = txList.reduce((reducedList, txMeta, index) => { - if (!index) return [txMeta] - const nonceMatches = txList.filter((txData) => { - return txMeta.txParams.nonce === txData.txParams.nonce - }) - if (nonceMatches.length > 1) return reducedList - reducedList.push(txMeta) - return reducedList - }, []) - return reducedTxList - } - async _getCurrentBlock () { const blockTracker = this._getBlockTracker() const currentBlock = blockTracker.getCurrentBlock() @@ -102,45 +83,68 @@ class NonceTracker { const currentBlock = await this._getCurrentBlock() const blockNumber = currentBlock.blockNumber const pendingNonce = this._getLocalPendingNonce(address) - const pendingCount = this.getPendingTransactionCount(address) + const pendingCount = this._getPendingTransactionCount(address) 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}"`) - let networkNonce = pendingNonce > baseCount ? baseCount + pendingCount : baseCount - return {networkNonce, blockNumber, baseCountHex, baseCount, pendingCount, pendingNonce} - } + // if the nonce provided by the network is higher then a pending tx + // toss out the pending txCount + const networkNonce = pendingNonce > baseCount ? baseCount + pendingCount : baseCount - _getLocalPendingNonce (address) { - const pendingTransactions = this.reduceTxListToUniqueNonces(this.getPendingTransactions(address)) - const localNonces = pendingTransactions.map((txMeta) => txMeta.txParams.nonce) - const localNonceHex = localNonces.reduce((highestNonce, nonce) => { - return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce - }, '0x0') - return parseInt(localNonceHex, 16) + return {networkNonce, blockNumber, baseCountHex, baseCount, pendingCount, pendingNonce} } _getLocalNextNonce (address) { - const confirmedTransactions = this.reduceTxListToUniqueNonces(this.getConfirmedTransactions(address)) - const pendingTransactions = this.reduceTxListToUniqueNonces(this.getPendingTransactions(address)) - const transactions = this.reduceTxListToUniqueNonces(confirmedTransactions.concat(pendingTransactions)) - const localNonces = transactions.map((txMeta) => txMeta.txParams.nonce) - const localNonceHex = localNonces.reduce((highestNonce, nonce) => { - return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce - }, '0x0') - let localNonce = parseInt(localNonceHex, 16) + const confirmedTransactions = this._reduceTxListToUniqueNonces(this.getConfirmedTransactions(address)) + const pendingTransactions = this._reduceTxListToUniqueNonces(this.getPendingTransactions(address)) + const transactions = this._reduceTxListToUniqueNonces(confirmedTransactions.concat(pendingTransactions)) + let localNonce = this._getHighestNonce(transactions) // throw out localNonce if not a number if (!Number.isInteger(localNonce)) localNonce = 0 if ( // the local nonce is not 0 localNonce || // or their are pending or confirmed transactions - this.getPendingTransactionCount(address) || + this._getPendingTransactionCount(address) || confirmedTransactions.length ) ++localNonce return localNonce } + _getLocalPendingNonce (address) { + const pendingTransactions = this._reduceTxListToUniqueNonces(this.getPendingTransactions(address)) + const localNonce = this._getHighestNonce(pendingTransactions) + return localNonce + } + + _getPendingTransactionCount (address) { + const pendingTransactions = this.getPendingTransactions(address) + return this._reduceTxListToUniqueNonces(pendingTransactions).length + } + + + _reduceTxListToUniqueNonces (txList) { + const reducedTxList = txList.reduce((reducedList, txMeta, index) => { + if (!index) return [txMeta] + const nonceMatches = txList.filter((txData) => { + return txMeta.txParams.nonce === txData.txParams.nonce + }) + if (nonceMatches.length > 1) return reducedList + reducedList.push(txMeta) + return reducedList + }, []) + return reducedTxList + } + + _getHighestNonce (txList) { + const nonces = txList.map((txMeta) => txMeta.txParams.nonce) + const nonceHex = nonces.reduce((highestNonce, nonce) => { + return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce + }, '0x0') + return parseInt(nonceHex, 16) + } + // this is a hotfix for the fact that the blockTracker will // change when the network changes _getBlockTracker () { |