From ac9e84ff40c0331cda6233be31241d68f05565f3 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 11 Aug 2017 15:52:03 -0700 Subject: add deprecation warning for web3 --- app/scripts/lib/auto-reload.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index 534047330..dabf4278e 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -5,7 +5,10 @@ function setupDappAutoReload (web3, observable) { global.web3 = new Proxy(web3, { get: (_web3, name) => { // get the time of use - if (name !== '_used') _web3._used = Date.now() + if (name !== '_used') { + console.warn('MetaMask: web3 will be deprecated in the future in favor of the ethereumProvider \nhttps://github.com/ethereum/mist/releases/tag/v0.9.0') + _web3._used = Date.now() + } return _web3[name] }, set: (_web3, name, value) => { -- cgit From 66a012550edb8b8bb328f2315109bbbab48d3d3f Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 11 Aug 2017 15:54:52 -0700 Subject: fix wording --- app/scripts/lib/auto-reload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index dabf4278e..6abce73ea 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -6,7 +6,7 @@ function setupDappAutoReload (web3, observable) { get: (_web3, name) => { // get the time of use if (name !== '_used') { - console.warn('MetaMask: web3 will be deprecated in the future in favor of the ethereumProvider \nhttps://github.com/ethereum/mist/releases/tag/v0.9.0') + console.warn('MetaMask: web3 will be deprecated in the near future in favor of the ethereumProvider \nhttps://github.com/ethereum/mist/releases/tag/v0.9.0') _web3._used = Date.now() } return _web3[name] -- cgit From fdffb6fedc6a29a2f952f7db669bdc2907afb854 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 14 Aug 2017 18:46:04 -0700 Subject: introduce tx-state-history-helper and diff-based history --- app/scripts/lib/tx-state-history-helper.js | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 app/scripts/lib/tx-state-history-helper.js (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/tx-state-history-helper.js b/app/scripts/lib/tx-state-history-helper.js new file mode 100644 index 000000000..87b9a1d63 --- /dev/null +++ b/app/scripts/lib/tx-state-history-helper.js @@ -0,0 +1,37 @@ +const jsonDiffer = require('fast-json-patch') +const clone = require('clone') + +module.exports = { + generateHistoryEntry, + replayHistory, + snapshotFromTxMeta, + migrateFromSnapshotsToDiffs, +} + + +function migrateFromSnapshotsToDiffs(longHistory) { + return ( + longHistory + // convert non-initial history entries into diffs + .map((entry, index) => { + if (index === 0) return entry + return generateHistoryEntry(longHistory[index-1], entry) + }) + ) +} + +function generateHistoryEntry(previousState, newState) { + return jsonDiffer.compare(previousState, newState) +} + +function replayHistory(shortHistory) { + return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument) +} + +function snapshotFromTxMeta(txMeta) { + // create txMeta snapshot for history + const snapshot = clone(txMeta) + // dont include previous history in this snapshot + delete snapshot.history + return snapshot +} \ No newline at end of file -- cgit From 1af797b1b3a37d8fe00afbd0d84cb3349b8fd471 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 14 Aug 2017 19:15:36 -0700 Subject: tx controller - tx state history various small fixes --- app/scripts/lib/tx-state-history-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/tx-state-history-helper.js b/app/scripts/lib/tx-state-history-helper.js index 87b9a1d63..304069d57 100644 --- a/app/scripts/lib/tx-state-history-helper.js +++ b/app/scripts/lib/tx-state-history-helper.js @@ -15,7 +15,7 @@ function migrateFromSnapshotsToDiffs(longHistory) { // convert non-initial history entries into diffs .map((entry, index) => { if (index === 0) return entry - return generateHistoryEntry(longHistory[index-1], entry) + return generateHistoryEntry(longHistory[index - 1], entry) }) ) } -- cgit From a5a32f3d5742972586893741847ea4516afa19ac Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 18 Aug 2017 13:54:16 -0700 Subject: use "localNonce" when the network returns a nonce that is lower then a known confirmed tx --- app/scripts/lib/nonce-tracker.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 8328e81ec..3a26c374c 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -4,10 +4,11 @@ const Mutex = require('await-semaphore').Mutex class NonceTracker { - constructor ({ provider, getPendingTransactions }) { + constructor ({ provider, getPendingTransactions, getConfirmedTransactions }) { this.provider = provider this.ethQuery = new EthQuery(provider) this.getPendingTransactions = getPendingTransactions + this.getConfirmedTransactions = getConfirmedTransactions this.lockMap = {} } @@ -28,6 +29,14 @@ class NonceTracker { // calculate next nonce // we need to make sure our base count // and pending count are from the same block + const localNonceHex = this._getLocalNonce(address) + let localNonce = parseInt(localNonceHex, 16) + try { + assert(Number.isInteger(localNonce), `nonce-tracker - localNonce is not an integer - got: (${typeof localNonce}) "${localNonce}"`) + } catch (e) { + // throw out localNonce if not a number + localNonce = 0 + } const currentBlock = await this._getCurrentBlock() const pendingTransactions = this.getPendingTransactions(address) const pendingCount = pendingTransactions.length @@ -35,11 +44,11 @@ class NonceTracker { 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 = baseCount + pendingCount + const nextNonce = Math.max(baseCount, localNonce + 1) + pendingCount 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 } + const nonceDetails = { blockNumber, baseCount, baseCountHex, pendingCount, localNonceHex, localNonce } // return nonce and release cb return { nextNonce, nonceDetails, releaseLock } } @@ -83,6 +92,14 @@ class NonceTracker { return mutex } + _getLocalNonce (address) { + const confirmedTransactions = this.getConfirmedTransactions(address) + const localNonces = confirmedTransactions.map((txMeta) => txMeta.txParams.nonce) + return localNonces.reduce((nonce, highestNonce) => { + return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce + }, '0x0') + } + // this is a hotfix for the fact that the blockTracker will // change when the network changes _getBlockTracker () { -- cgit From f8eca95ca513c6d8af8e599ce143ae0e78b77e26 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 18 Aug 2017 15:01:05 -0700 Subject: include pendingTxs in localNonce --- app/scripts/lib/nonce-tracker.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 3a26c374c..a4ee5c38f 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -29,14 +29,10 @@ class NonceTracker { // calculate next nonce // we need to make sure our base count // and pending count are from the same block - const localNonceHex = this._getLocalNonce(address) + const localNonceHex = this._getHighestLocalNonce(address) let localNonce = parseInt(localNonceHex, 16) - try { - assert(Number.isInteger(localNonce), `nonce-tracker - localNonce is not an integer - got: (${typeof localNonce}) "${localNonce}"`) - } catch (e) { - // throw out localNonce if not a number - localNonce = 0 - } + // throw out localNonce if not a number + if (!Number.isInteger(localNonce)) localNonce = 0 const currentBlock = await this._getCurrentBlock() const pendingTransactions = this.getPendingTransactions(address) const pendingCount = pendingTransactions.length @@ -44,7 +40,8 @@ class NonceTracker { 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, localNonce + 1) + pendingCount + if (localNonce) ++localNonce + const nextNonce = Math.max(baseCount + pendingCount, localNonce) 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 @@ -92,9 +89,11 @@ class NonceTracker { return mutex } - _getLocalNonce (address) { + _getHighestLocalNonce (address) { const confirmedTransactions = this.getConfirmedTransactions(address) - const localNonces = confirmedTransactions.map((txMeta) => txMeta.txParams.nonce) + const pendingTransactions = this.getPendingTransactions(address) + const transactions = confirmedTransactions.concat(pendingTransactions) + const localNonces = transactions.map((txMeta) => txMeta.txParams.nonce) return localNonces.reduce((nonce, highestNonce) => { return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce }, '0x0') -- cgit From 37f86e874f8c3c3d5f6d78cd8abeb0c835f20ae2 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 18 Aug 2017 15:44:32 -0700 Subject: fix 0x0 nonce calc. --- app/scripts/lib/nonce-tracker.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index a4ee5c38f..3063209ee 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -29,10 +29,8 @@ class NonceTracker { // calculate next nonce // we need to make sure our base count // and pending count are from the same block - const localNonceHex = this._getHighestLocalNonce(address) - let localNonce = parseInt(localNonceHex, 16) + const localNextNonce = this._getLocalNextNonce(address) // throw out localNonce if not a number - if (!Number.isInteger(localNonce)) localNonce = 0 const currentBlock = await this._getCurrentBlock() const pendingTransactions = this.getPendingTransactions(address) const pendingCount = pendingTransactions.length @@ -40,12 +38,11 @@ class NonceTracker { 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}"`) - if (localNonce) ++localNonce - const nextNonce = Math.max(baseCount + pendingCount, localNonce) + const nextNonce = Math.max(baseCount + pendingCount, 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, localNonceHex, localNonce } + const nonceDetails = { blockNumber, baseCount, baseCountHex, pendingCount, localNextNonce } // return nonce and release cb return { nextNonce, nonceDetails, releaseLock } } @@ -89,14 +86,27 @@ class NonceTracker { return mutex } - _getHighestLocalNonce (address) { + // _getNetworkNonce (address) { + + // } + + _getLocalNextNonce (address) { const confirmedTransactions = this.getConfirmedTransactions(address) const pendingTransactions = this.getPendingTransactions(address) const transactions = confirmedTransactions.concat(pendingTransactions) const localNonces = transactions.map((txMeta) => txMeta.txParams.nonce) - return localNonces.reduce((nonce, highestNonce) => { + const localNonceHex = localNonces.reduce((nonce, highestNonce) => { return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce }, '0x0') + let localNonce = parseInt(localNonceHex, 16) + if ( + // the local nonce is not 0 + localNonce || + // or their are pending or confirmed transactions + pendingTransactions.length || + confirmedTransactions.length + ) ++localNonce + return localNonce } // this is a hotfix for the fact that the blockTracker will -- cgit From 1ffb40648066189cd9e3abffc94299bbeecb6334 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 18 Aug 2017 16:05:21 -0700 Subject: break out network nonce calc. --- app/scripts/lib/nonce-tracker.js | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'app/scripts/lib') 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 || -- cgit From 5c74f316a8adbcbcca642f9ba0734bd1fc181bc1 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Mon, 21 Aug 2017 16:45:10 -0700 Subject: nonce-tracker - pass tests --- app/scripts/lib/nonce-tracker.js | 49 +++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 01d8e5f19..92a9cf4f1 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -37,6 +37,25 @@ 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() @@ -82,22 +101,31 @@ class NonceTracker { // 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 + const pendingNonce = this._getLocalPendingNonce(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}"`) - const networkNonce = baseCount + pendingCount - return {networkNonce, blockNumber, baseCountHex, baseCount, pendingCount} + let networkNonce = pendingNonce > baseCount ? baseCount + pendingCount : baseCount + return {networkNonce, blockNumber, baseCountHex, baseCount, pendingCount, pendingNonce} + } + + _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) } _getLocalNextNonce (address) { - const confirmedTransactions = this.getConfirmedTransactions(address) - const pendingTransactions = this.getPendingTransactions(address) - const transactions = confirmedTransactions.concat(pendingTransactions) + 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((nonce, highestNonce) => { + const localNonceHex = localNonces.reduce((highestNonce, nonce) => { return parseInt(nonce, 16) > parseInt(highestNonce, 16) ? nonce : highestNonce }, '0x0') let localNonce = parseInt(localNonceHex, 16) @@ -107,9 +135,9 @@ class NonceTracker { // the local nonce is not 0 localNonce || // or their are pending or confirmed transactions - pendingTransactions.length || + this.getPendingTransactionCount(address) || confirmedTransactions.length - ) ++localNonce + ) ++localNonce return localNonce } @@ -118,7 +146,6 @@ class NonceTracker { _getBlockTracker () { return this.provider._blockTracker } - } module.exports = NonceTracker -- cgit From 7d34b22d78713aa1a12dbdab0559ccaf4c1be93c Mon Sep 17 00:00:00 2001 From: frankiebee Date: Mon, 21 Aug 2017 17:04:47 -0700 Subject: clean up code --- app/scripts/lib/nonce-tracker.js | 82 +++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 39 deletions(-) (limited to 'app/scripts/lib') 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 () { -- cgit From 604c91f7b2158b09acfcf0e56ea77d0fad0d9dec Mon Sep 17 00:00:00 2001 From: frankiebee Date: Mon, 21 Aug 2017 18:04:05 -0700 Subject: nonce-tracker - pass tests --- app/scripts/lib/nonce-tracker.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index c20459650..ee36e3d5d 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -30,9 +30,11 @@ class NonceTracker { const nonceDetails = await this._getNetworkNonceAndDetails(address) const networkNonce = nonceDetails.networkNonce const nextNonce = Math.max(networkNonce, localNextNonce) + const currentPendingNonce = this._getLocalPendingNonce(address) 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 nonceDetails.localNextNonce = localNextNonce + nonceDetails.currentPendingNonce = currentPendingNonce // return nonce and release cb return { nextNonce, nonceDetails, releaseLock } } @@ -82,17 +84,14 @@ class NonceTracker { // and pending count are from the same block const currentBlock = await this._getCurrentBlock() const blockNumber = currentBlock.blockNumber - const pendingNonce = this._getLocalPendingNonce(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}"`) // 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 + const networkNonce = baseCount - return {networkNonce, blockNumber, baseCountHex, baseCount, pendingCount, pendingNonce} + return {networkNonce, blockNumber, baseCountHex, baseCount} } _getLocalNextNonce (address) { -- cgit From 98bc9b6656a7fd712085df76252030ddadc653dd Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 13:52:15 -0700 Subject: nonce-tracker - make nonce strategy api and naming more symmetical --- app/scripts/lib/nonce-tracker.js | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index ee36e3d5d..0a87f040d 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -26,14 +26,16 @@ class NonceTracker { await this._globalMutexFree() // await lock free, then take lock const releaseLock = await this._takeMutex(address) - const localNextNonce = this._getLocalNextNonce(address) - const nonceDetails = await this._getNetworkNonceAndDetails(address) - const networkNonce = nonceDetails.networkNonce - const nextNonce = Math.max(networkNonce, localNextNonce) - const currentPendingNonce = this._getLocalPendingNonce(address) + // evaluate multiple nextNonce strategies + const nonceDetails = {} + const localNonceResult = await this._getlocalNextNonce(address) + nonceDetails.local = localNonceResult.details + const networkNonceResult = await this._getNetworkNextNonce(address) + nonceDetails.network = networkNonceResult.details + const nextNonce = Math.max(networkNonceResult.nonce, localNonceResult.nonce) 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 - nonceDetails.localNextNonce = localNextNonce + const currentPendingNonce = this._getLocalPendingNonce(address) nonceDetails.currentPendingNonce = currentPendingNonce // return nonce and release cb return { nextNonce, nonceDetails, releaseLock } @@ -78,7 +80,7 @@ class NonceTracker { return mutex } - async _getNetworkNonceAndDetails (address) { + async _getNetworkNextNonce (address) { // calculate next nonce // we need to make sure our base count // and pending count are from the same block @@ -87,28 +89,29 @@ class NonceTracker { 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}"`) - // if the nonce provided by the network is higher then a pending tx - // toss out the pending txCount - const networkNonce = baseCount - - return {networkNonce, blockNumber, baseCountHex, baseCount} + const nonceDetails = { blockNumber, baseCountHex, baseCount } + return { name: 'network', nonce: baseCount, details: nonceDetails } } - _getLocalNextNonce (address) { + async _getlocalNextNonce (address) { 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) + const highestNonce = this._getHighestNonce(transactions) + let localNonce = highestNonce // throw out localNonce if not a number - if (!Number.isInteger(localNonce)) localNonce = 0 + if (!Number.isInteger(highestNonce)) localNonce = 0 + const pendingCount = this._getPendingTransactionCount(address) + const confirmedCount = confirmedTransactions.length if ( // the local nonce is not 0 localNonce || // or their are pending or confirmed transactions - this._getPendingTransactionCount(address) || - confirmedTransactions.length + pendingCount || + confirmedCount ) ++localNonce - return localNonce + const nonceDetails = { highestNonce, localNonce, pendingCount, confirmedCount } + return { name: 'local', nonce: localNonce, details: nonceDetails } } _getLocalPendingNonce (address) { @@ -122,7 +125,6 @@ class NonceTracker { return this._reduceTxListToUniqueNonces(pendingTransactions).length } - _reduceTxListToUniqueNonces (txList) { const reducedTxList = txList.reduce((reducedList, txMeta, index) => { if (!index) return [txMeta] -- cgit From b191649ef5ee952c636b224658181fdf2fd73b97 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 13:58:26 -0700 Subject: nonce-tracker - getHighestNonce doesnt need uniqued input --- app/scripts/lib/nonce-tracker.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 0a87f040d..d44f9c858 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -94,9 +94,9 @@ class NonceTracker { } async _getlocalNextNonce (address) { - const confirmedTransactions = this._reduceTxListToUniqueNonces(this.getConfirmedTransactions(address)) - const pendingTransactions = this._reduceTxListToUniqueNonces(this.getPendingTransactions(address)) - const transactions = this._reduceTxListToUniqueNonces(confirmedTransactions.concat(pendingTransactions)) + const confirmedTransactions = this.getConfirmedTransactions(address) + const pendingTransactions = this.getPendingTransactions(address) + const transactions = confirmedTransactions.concat(pendingTransactions) const highestNonce = this._getHighestNonce(transactions) let localNonce = highestNonce // throw out localNonce if not a number @@ -115,7 +115,7 @@ class NonceTracker { } _getLocalPendingNonce (address) { - const pendingTransactions = this._reduceTxListToUniqueNonces(this.getPendingTransactions(address)) + const pendingTransactions = this.getPendingTransactions(address) const localNonce = this._getHighestNonce(pendingTransactions) return localNonce } -- cgit From e43da3e4aa703cf49543df7726b8cbeea987943d Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 14:04:31 -0700 Subject: nonce-tracker - simplify getHighestNonce --- app/scripts/lib/nonce-tracker.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index d44f9c858..dab6ace1f 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -139,11 +139,9 @@ class NonceTracker { } _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) + const nonces = txList.map((txMeta) => parseInt(txMeta.txParams.nonce, 16)) + const highestNonce = Math.max.apply(null, nonces) + return highestNonce } // this is a hotfix for the fact that the blockTracker will -- cgit From a7e3dc83272d795a48ee7e10c2588a18affbe72e Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 14:15:56 -0700 Subject: nonce-tracker - simplify _getlocalNextNonce --- app/scripts/lib/nonce-tracker.js | 49 ++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index dab6ace1f..0fb245f7b 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -94,30 +94,39 @@ class NonceTracker { } async _getlocalNextNonce (address) { - const confirmedTransactions = this.getConfirmedTransactions(address) - const pendingTransactions = this.getPendingTransactions(address) - const transactions = confirmedTransactions.concat(pendingTransactions) - const highestNonce = this._getHighestNonce(transactions) - let localNonce = highestNonce - // throw out localNonce if not a number - if (!Number.isInteger(highestNonce)) localNonce = 0 - const pendingCount = this._getPendingTransactionCount(address) - const confirmedCount = confirmedTransactions.length - if ( - // the local nonce is not 0 - localNonce || - // or their are pending or confirmed transactions - pendingCount || - confirmedCount - ) ++localNonce - const nonceDetails = { highestNonce, localNonce, pendingCount, confirmedCount } - return { name: 'local', nonce: localNonce, details: nonceDetails } + let nextNonce + // check our local tx history for the highest nonce (if any) + const highestNonce = this._getLocalHighestNonce(address) + const haveHighestNonce = Number.isInteger(highestNonce) + if (haveHighestNonce) { + // next nonce is the nonce after our last + nextNonce = highestNonce + 1 + } else { + // no local tx history so next must be first (zero) + nextNonce = 0 + } + const nonceDetails = { highestNonce } + return { name: 'local', nonce: nextNonce, details: nonceDetails } } _getLocalPendingNonce (address) { const pendingTransactions = this.getPendingTransactions(address) - const localNonce = this._getHighestNonce(pendingTransactions) - return localNonce + const highestNonce = this._getHighestNonce(pendingTransactions) + return highestNonce + } + + _getLocalConfirmedNonce (address) { + const pendingTransactions = this.getConfirmedTransactions(address) + const highestNonce = this._getHighestNonce(pendingTransactions) + return highestNonce + } + + _getLocalHighestNonce (address) { + const confirmedTransactions = this.getConfirmedTransactions(address) + const pendingTransactions = this.getPendingTransactions(address) + const transactions = confirmedTransactions.concat(pendingTransactions) + const highestNonce = this._getHighestNonce(transactions) + return highestNonce } _getPendingTransactionCount (address) { -- cgit From 6d596bd9e6921b92de62befadebe32b270f2b784 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 14:17:00 -0700 Subject: nonce-tracker - getlocalNextNonce - add entry to nonceDetails --- app/scripts/lib/nonce-tracker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 0fb245f7b..1c51ef647 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -105,7 +105,7 @@ class NonceTracker { // no local tx history so next must be first (zero) nextNonce = 0 } - const nonceDetails = { highestNonce } + const nonceDetails = { highestNonce, haveHighestNonce } return { name: 'local', nonce: nextNonce, details: nonceDetails } } -- cgit From dc3e8d60ef22b268b4146e28e9dcb262aaa63304 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 14:24:09 -0700 Subject: nonce-tracker - fix var name --- app/scripts/lib/nonce-tracker.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 1c51ef647..13fdf27eb 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -110,14 +110,14 @@ class NonceTracker { } _getLocalPendingNonce (address) { - const pendingTransactions = this.getPendingTransactions(address) - const highestNonce = this._getHighestNonce(pendingTransactions) + const transactions = this.getPendingTransactions(address) + const highestNonce = this._getHighestNonce(transactions) return highestNonce } _getLocalConfirmedNonce (address) { - const pendingTransactions = this.getConfirmedTransactions(address) - const highestNonce = this._getHighestNonce(pendingTransactions) + const transactions = this.getConfirmedTransactions(address) + const highestNonce = this._getHighestNonce(transactions) return highestNonce } -- cgit From fb2c6cc8acacfbdf134877beb68090a6fc127b94 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 14:33:54 -0700 Subject: nonce-tracker - use ethjs-query --- app/scripts/lib/nonce-tracker.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 13fdf27eb..427c09508 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -1,4 +1,4 @@ -const EthQuery = require('eth-query') +const EthQuery = require('ethjs-query') const assert = require('assert') const Mutex = require('await-semaphore').Mutex @@ -50,15 +50,6 @@ class NonceTracker { }) } - async _getTxCount (address, currentBlock) { - const blockNumber = currentBlock.number - return new Promise((resolve, reject) => { - this.ethQuery.getTransactionCount(address, blockNumber, (err, result) => { - err ? reject(err) : resolve(result) - }) - }) - } - async _globalMutexFree () { const globalMutex = this._lookupMutex('global') const release = await globalMutex.acquire() @@ -86,7 +77,7 @@ class NonceTracker { // and pending count are from the same block const currentBlock = await this._getCurrentBlock() const blockNumber = currentBlock.blockNumber - const baseCountHex = await this._getTxCount(address, currentBlock) + const baseCountHex = await this.ethQuery.getTransactionCount(address, blockNumber) const baseCount = parseInt(baseCountHex, 16) assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) const nonceDetails = { blockNumber, baseCountHex, baseCount } -- cgit From 0a93b65a3dea32a85a73674fb40ee40257ed18d7 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 15:06:54 -0700 Subject: remove unused code --- app/scripts/lib/nonce-tracker.js | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 427c09508..03bc3c278 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -34,9 +34,6 @@ class NonceTracker { nonceDetails.network = networkNonceResult.details const nextNonce = Math.max(networkNonceResult.nonce, localNonceResult.nonce) 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 currentPendingNonce = this._getLocalPendingNonce(address) - nonceDetails.currentPendingNonce = currentPendingNonce // return nonce and release cb return { nextNonce, nonceDetails, releaseLock } } @@ -100,18 +97,6 @@ class NonceTracker { return { name: 'local', nonce: nextNonce, details: nonceDetails } } - _getLocalPendingNonce (address) { - const transactions = this.getPendingTransactions(address) - const highestNonce = this._getHighestNonce(transactions) - return highestNonce - } - - _getLocalConfirmedNonce (address) { - const transactions = this.getConfirmedTransactions(address) - const highestNonce = this._getHighestNonce(transactions) - return highestNonce - } - _getLocalHighestNonce (address) { const confirmedTransactions = this.getConfirmedTransactions(address) const pendingTransactions = this.getPendingTransactions(address) -- cgit From 247965ebbe99bd611aaa6b7a5c6e86f33dcdea1d Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 22 Aug 2017 15:34:30 -0700 Subject: nonce-tracker - more debugging numbers for nonceDetails --- app/scripts/lib/nonce-tracker.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 03bc3c278..30c59fa46 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -84,7 +84,13 @@ class NonceTracker { async _getlocalNextNonce (address) { let nextNonce // check our local tx history for the highest nonce (if any) - const highestNonce = this._getLocalHighestNonce(address) + const confirmedTransactions = this.getConfirmedTransactions(address) + const pendingTransactions = this.getPendingTransactions(address) + const transactions = confirmedTransactions.concat(pendingTransactions) + const highestConfirmedNonce = this._getHighestNonce(confirmedTransactions) + const highestPendingNonce = this._getHighestNonce(pendingTransactions) + const highestNonce = this._getHighestNonce(transactions) + const haveHighestNonce = Number.isInteger(highestNonce) if (haveHighestNonce) { // next nonce is the nonce after our last @@ -93,18 +99,10 @@ class NonceTracker { // no local tx history so next must be first (zero) nextNonce = 0 } - const nonceDetails = { highestNonce, haveHighestNonce } + const nonceDetails = { highestNonce, haveHighestNonce, highestConfirmedNonce, highestPendingNonce } return { name: 'local', nonce: nextNonce, details: nonceDetails } } - _getLocalHighestNonce (address) { - const confirmedTransactions = this.getConfirmedTransactions(address) - const pendingTransactions = this.getPendingTransactions(address) - const transactions = confirmedTransactions.concat(pendingTransactions) - const highestNonce = this._getHighestNonce(transactions) - return highestNonce - } - _getPendingTransactionCount (address) { const pendingTransactions = this.getPendingTransactions(address) return this._reduceTxListToUniqueNonces(pendingTransactions).length -- cgit From 53e410167b25b8914fa927efc822515d6b22e0ee Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 23 Aug 2017 16:13:33 -0700 Subject: inpage provider - sync rpc - default to null values --- app/scripts/lib/inpage-provider.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js index fd032a673..8b36b4433 100644 --- a/app/scripts/lib/inpage-provider.js +++ b/app/scripts/lib/inpage-provider.js @@ -27,7 +27,7 @@ function MetamaskInpageProvider (connectionStream) { ) // ignore phishing warning message (handled elsewhere) - multiStream.ignoreStream('phishing') + multiStream.ignoreStream('phishing') // connect to async provider const asyncProvider = self.asyncProvider = new StreamProvider() @@ -80,7 +80,7 @@ MetamaskInpageProvider.prototype.send = function (payload) { case 'eth_coinbase': // read from localStorage selectedAddress = self.publicConfigStore.getState().selectedAddress - result = selectedAddress + result = selectedAddress || null break case 'eth_uninstallFilter': @@ -90,7 +90,7 @@ MetamaskInpageProvider.prototype.send = function (payload) { case 'net_version': const networkVersion = self.publicConfigStore.getState().networkVersion - result = networkVersion + result = networkVersion || null break // throw not-supported Error -- cgit From 44dc2ba7128704f26bf2a530aa24c2a62b862b75 Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 23 Aug 2017 16:26:55 -0700 Subject: inpage provider - only warn web3 usage once per session --- app/scripts/lib/auto-reload.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index 6abce73ea..f3566cff5 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -2,17 +2,22 @@ module.exports = setupDappAutoReload function setupDappAutoReload (web3, observable) { // export web3 as a global, checking for usage + let hasBeenWarned = false global.web3 = new Proxy(web3, { - get: (_web3, name) => { - // get the time of use - if (name !== '_used') { + get: (_web3, key) => { + // show warning once on web3 access + if (!hasBeenWarned && key !== 'currentProvider') { console.warn('MetaMask: web3 will be deprecated in the near future in favor of the ethereumProvider \nhttps://github.com/ethereum/mist/releases/tag/v0.9.0') + hasBeenWarned = true + } + // get the time of use + if (key !== '_used') { _web3._used = Date.now() } - return _web3[name] + return _web3[key] }, - set: (_web3, name, value) => { - _web3[name] = value + set: (_web3, key, value) => { + _web3[key] = value }, }) var networkVersion -- cgit From 4019f318fe48e85ac7325ac1cb942c230644547c Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 23 Aug 2017 16:44:57 -0700 Subject: inpage provider - autoreload - improve readability --- app/scripts/lib/auto-reload.js | 47 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index f3566cff5..62aaf405c 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -3,6 +3,10 @@ module.exports = setupDappAutoReload function setupDappAutoReload (web3, observable) { // export web3 as a global, checking for usage let hasBeenWarned = false + let reloadInProgress = false + let lastTimeUsed + let lastSeenNetwork + global.web3 = new Proxy(web3, { get: (_web3, key) => { // show warning once on web3 access @@ -11,29 +15,42 @@ function setupDappAutoReload (web3, observable) { hasBeenWarned = true } // get the time of use - if (key !== '_used') { - _web3._used = Date.now() - } + lastTimeUsed = Date.now() + // return value normally return _web3[key] }, set: (_web3, key, value) => { + // set value normally _web3[key] = value }, }) - var networkVersion observable.subscribe(function (state) { - // get the initial network - const curentNetVersion = state.networkVersion - if (!networkVersion) networkVersion = curentNetVersion - - if (curentNetVersion !== networkVersion && web3._used) { - const timeSinceUse = Date.now() - web3._used - // if web3 was recently used then delay the reloading of the page - timeSinceUse > 500 ? triggerReset() : setTimeout(triggerReset, 500) - // prevent reentry into if statement if state updates again before - // reload - networkVersion = curentNetVersion + // if reload in progress, no need to check reload logic + if (reloadInProgress) return + + const currentNetwork = state.networkVersion + + // set the initial network + if (!lastSeenNetwork) { + lastSeenNetwork = currentNetwork + return + } + + // skip reload logic if web3 not used + if (!lastTimeUsed) return + + // if network did not change, exit + if (currentNetwork === lastSeenNetwork) return + + // initiate page reload + reloadInProgress = true + const timeSinceUse = Date.now() - lastTimeUsed + // if web3 was recently used then delay the reloading of the page + if (timeSinceUse > 500) { + triggerReset() + } else { + setTimeout(triggerReset, 500) } }) } -- cgit From 1f0223d0a0b617ffaf1704210b7ed328d12c48d1 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 19:34:20 -0700 Subject: Simplify nonce calculation --- app/scripts/lib/nonce-tracker.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 30c59fa46..db5a5327f 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -28,9 +28,9 @@ class NonceTracker { const releaseLock = await this._takeMutex(address) // evaluate multiple nextNonce strategies const nonceDetails = {} - const localNonceResult = await this._getlocalNextNonce(address) - nonceDetails.local = localNonceResult.details const networkNonceResult = await this._getNetworkNextNonce(address) + const localNonceResult = await this._getLocalNextNonce(address, networkNonceResult) + nonceDetails.local = localNonceResult.details nonceDetails.network = networkNonceResult.details const nextNonce = Math.max(networkNonceResult.nonce, localNonceResult.nonce) assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`) @@ -81,15 +81,16 @@ class NonceTracker { return { name: 'network', nonce: baseCount, details: nonceDetails } } - async _getlocalNextNonce (address) { + async _getLocalNextNonce (address, networkNonce) { let nextNonce // check our local tx history for the highest nonce (if any) const confirmedTransactions = this.getConfirmedTransactions(address) const pendingTransactions = this.getPendingTransactions(address) const transactions = confirmedTransactions.concat(pendingTransactions) + const highestConfirmedNonce = this._getHighestNonce(confirmedTransactions) const highestPendingNonce = this._getHighestNonce(pendingTransactions) - const highestNonce = this._getHighestNonce(transactions) + const highestNonce = Math.max(highestConfirmedNonce, highestPendingNonce) const haveHighestNonce = Number.isInteger(highestNonce) if (haveHighestNonce) { -- cgit From c4ab7a57799167904b863b9cb9423885a9c1cc66 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 19:35:49 -0700 Subject: Linted --- app/scripts/lib/nonce-tracker.js | 1 - 1 file changed, 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index db5a5327f..d3c76c230 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -86,7 +86,6 @@ class NonceTracker { // check our local tx history for the highest nonce (if any) const confirmedTransactions = this.getConfirmedTransactions(address) const pendingTransactions = this.getPendingTransactions(address) - const transactions = confirmedTransactions.concat(pendingTransactions) const highestConfirmedNonce = this._getHighestNonce(confirmedTransactions) const highestPendingNonce = this._getHighestNonce(pendingTransactions) -- cgit From 221575a191a0a8b8c4c17465a0530561e1905297 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 20:04:03 -0700 Subject: Fix new test, break an older maybe wrong one --- app/scripts/lib/nonce-tracker.js | 50 +++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index d3c76c230..4f2473cf5 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -29,11 +29,16 @@ class NonceTracker { // evaluate multiple nextNonce strategies const nonceDetails = {} const networkNonceResult = await this._getNetworkNextNonce(address) - const localNonceResult = await this._getLocalNextNonce(address, networkNonceResult) + const highestLocallyConfirmed = this._getHighestLocallyConfirmed(address) + const highestConfirmed = Math.max(networkNonceResult.nonce, highestLocallyConfirmed) + const pendingTxs = this.getPendingTransactions(address) + const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestConfirmed) || 0 + nonceDetails.local = localNonceResult.details nonceDetails.network = networkNonceResult.details const nextNonce = Math.max(networkNonceResult.nonce, localNonceResult.nonce) assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`) + // return nonce and release cb return { nextNonce, nonceDetails, releaseLock } } @@ -77,35 +82,14 @@ class NonceTracker { const baseCountHex = await this.ethQuery.getTransactionCount(address, blockNumber) const baseCount = parseInt(baseCountHex, 16) assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) - const nonceDetails = { blockNumber, baseCountHex, baseCount } + const nonceDetails = { blockNumber, baseCount } return { name: 'network', nonce: baseCount, details: nonceDetails } } - async _getLocalNextNonce (address, networkNonce) { - let nextNonce - // check our local tx history for the highest nonce (if any) + _getHighestLocallyConfirmed (address) { const confirmedTransactions = this.getConfirmedTransactions(address) - const pendingTransactions = this.getPendingTransactions(address) - - const highestConfirmedNonce = this._getHighestNonce(confirmedTransactions) - const highestPendingNonce = this._getHighestNonce(pendingTransactions) - const highestNonce = Math.max(highestConfirmedNonce, highestPendingNonce) - - const haveHighestNonce = Number.isInteger(highestNonce) - if (haveHighestNonce) { - // next nonce is the nonce after our last - nextNonce = highestNonce + 1 - } else { - // no local tx history so next must be first (zero) - nextNonce = 0 - } - const nonceDetails = { highestNonce, haveHighestNonce, highestConfirmedNonce, highestPendingNonce } - return { name: 'local', nonce: nextNonce, details: nonceDetails } - } - - _getPendingTransactionCount (address) { - const pendingTransactions = this.getPendingTransactions(address) - return this._reduceTxListToUniqueNonces(pendingTransactions).length + const highest = this._getHighestNonce(confirmedTransactions) + return highest } _reduceTxListToUniqueNonces (txList) { @@ -127,6 +111,20 @@ class NonceTracker { return highestNonce } + _getHighestContinuousFrom (txList, startPoint) { + const nonces = txList.map((txMeta) => parseInt(txMeta.txParams.nonce, 16)) + + let highest = startPoint + while (nonces.includes(highest + 1)) { + highest++ + } + + const haveHighestNonce = Number.isInteger(highest) && highest > 0 + const nonce = haveHighestNonce ? highest + 1 : 0 + + return { name: 'local', nonce } + } + // this is a hotfix for the fact that the blockTracker will // change when the network changes _getBlockTracker () { -- cgit From 04d40b114dd12237710f605fe2f0a5f2c337d2cb Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 20:11:37 -0700 Subject: Got all tests but one passing --- app/scripts/lib/nonce-tracker.js | 1 + 1 file changed, 1 insertion(+) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 4f2473cf5..6fcd716f2 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -30,6 +30,7 @@ class NonceTracker { const nonceDetails = {} const networkNonceResult = await this._getNetworkNextNonce(address) const highestLocallyConfirmed = this._getHighestLocallyConfirmed(address) + const highestConfirmed = Math.max(networkNonceResult.nonce, highestLocallyConfirmed) const pendingTxs = this.getPendingTransactions(address) const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestConfirmed) || 0 -- cgit From 855f4eeacbcf7b3e056cf7956edea2c84fa256d5 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 20:31:03 -0700 Subject: Pass nonce tests --- app/scripts/lib/nonce-tracker.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 6fcd716f2..8c2568c3f 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -30,10 +30,12 @@ class NonceTracker { const nonceDetails = {} const networkNonceResult = await this._getNetworkNextNonce(address) const highestLocallyConfirmed = this._getHighestLocallyConfirmed(address) + const nextNetworkNonce = networkNonceResult.nonce + const highestLocalNonce = highestLocallyConfirmed + const highestSuggested = Math.max(nextNetworkNonce, highestLocalNonce) - const highestConfirmed = Math.max(networkNonceResult.nonce, highestLocallyConfirmed) const pendingTxs = this.getPendingTransactions(address) - const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestConfirmed) || 0 + const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestSuggested) || 0 nonceDetails.local = localNonceResult.details nonceDetails.network = networkNonceResult.details @@ -90,7 +92,7 @@ class NonceTracker { _getHighestLocallyConfirmed (address) { const confirmedTransactions = this.getConfirmedTransactions(address) const highest = this._getHighestNonce(confirmedTransactions) - return highest + return Number.isInteger(highest) ? highest + 1 : 0 } _reduceTxListToUniqueNonces (txList) { @@ -116,14 +118,11 @@ class NonceTracker { const nonces = txList.map((txMeta) => parseInt(txMeta.txParams.nonce, 16)) let highest = startPoint - while (nonces.includes(highest + 1)) { + while (nonces.includes(highest)) { highest++ } - const haveHighestNonce = Number.isInteger(highest) && highest > 0 - const nonce = haveHighestNonce ? highest + 1 : 0 - - return { name: 'local', nonce } + return { name: 'local', nonce: highest, details: { startPoint, highest } } } // this is a hotfix for the fact that the blockTracker will -- cgit From 55c1a259b1ac01e8a8f6b241bac7a3b1cd16f3ec Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 21:14:46 -0700 Subject: Fix network nonce parsing --- app/scripts/lib/nonce-tracker.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 8c2568c3f..d49c7f205 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -37,8 +37,14 @@ class NonceTracker { const pendingTxs = this.getPendingTransactions(address) const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestSuggested) || 0 - nonceDetails.local = localNonceResult.details - nonceDetails.network = networkNonceResult.details + nonceDetails.params = { + highestLocalNonce, + highestSuggested, + nextNetworkNonce, + } + nonceDetails.local = localNonceResult + nonceDetails.network = networkNonceResult + const nextNonce = Math.max(networkNonceResult.nonce, localNonceResult.nonce) assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`) @@ -82,8 +88,9 @@ class NonceTracker { // and pending count are from the same block const currentBlock = await this._getCurrentBlock() const blockNumber = currentBlock.blockNumber - const baseCountHex = await this.ethQuery.getTransactionCount(address, blockNumber) - const baseCount = parseInt(baseCountHex, 16) + const baseCountBN = await this.ethQuery.getTransactionCount(address, blockNumber) + const baseString = baseCountBN.toString() + const baseCount = parseInt(baseString) assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) const nonceDetails = { blockNumber, baseCount } return { name: 'network', nonce: baseCount, details: nonceDetails } -- cgit From a122ec1f8ba0935d26a45ce0b26be991d222aaad Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 21:37:07 -0700 Subject: Use toNumber method --- app/scripts/lib/nonce-tracker.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index d49c7f205..e0e065d82 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -89,8 +89,7 @@ class NonceTracker { const currentBlock = await this._getCurrentBlock() const blockNumber = currentBlock.blockNumber const baseCountBN = await this.ethQuery.getTransactionCount(address, blockNumber) - const baseString = baseCountBN.toString() - const baseCount = parseInt(baseString) + const baseCount = baseCountBN.toNumber() assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) const nonceDetails = { blockNumber, baseCount } return { name: 'network', nonce: baseCount, details: nonceDetails } -- cgit From c620123fab9e1eac8d3038204c9cfb04ca85afb1 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 21:50:28 -0700 Subject: Enforce nonces as type string --- app/scripts/lib/nonce-tracker.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index e0e065d82..08f1e1e86 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -115,13 +115,21 @@ class NonceTracker { } _getHighestNonce (txList) { - const nonces = txList.map((txMeta) => parseInt(txMeta.txParams.nonce, 16)) + const nonces = txList.map((txMeta) => { + const nonce = txMeta.txParams.nonce + assert(typeof nonce, 'string', 'nonces should be hex strings') + return parseInt(nonce, 16) + }) const highestNonce = Math.max.apply(null, nonces) return highestNonce } _getHighestContinuousFrom (txList, startPoint) { - const nonces = txList.map((txMeta) => parseInt(txMeta.txParams.nonce, 16)) + const nonces = txList.map((txMeta) => { + const nonce = txMeta.txParams.nonce + assert(typeof nonce, 'string', 'nonces should be hex strings') + return parseInt(nonce, 16) + }) let highest = startPoint while (nonces.includes(highest)) { -- cgit From 0ad310e096fe8c13553dc40f65363d296a7e84ce Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 23 Aug 2017 22:29:08 -0700 Subject: Fail transactions after a day of retries --- app/scripts/lib/pending-tx-tracker.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/pending-tx-tracker.js b/app/scripts/lib/pending-tx-tracker.js index 19720db3f..b90851b58 100644 --- a/app/scripts/lib/pending-tx-tracker.js +++ b/app/scripts/lib/pending-tx-tracker.js @@ -1,6 +1,7 @@ const EventEmitter = require('events') const EthQuery = require('ethjs-query') const sufficientBalance = require('./util').sufficientBalance +const RETRY_LIMIT = 3500 // Retry 3500 blocks, or about 1 day. /* Utility class for tracking the transactions as they @@ -28,6 +29,7 @@ module.exports = class PendingTransactionTracker extends EventEmitter { this.getBalance = config.getBalance this.getPendingTransactions = config.getPendingTransactions this.publishTransaction = config.publishTransaction + this.giveUpOnTransaction = config.giveUpOnTransaction } // checks if a signed tx is in a block and @@ -100,6 +102,10 @@ module.exports = class PendingTransactionTracker extends EventEmitter { if (balance === undefined) return if (!('retryCount' in txMeta)) txMeta.retryCount = 0 + if (txMeta.retryCount > RETRY_LIMIT) { + return this.giveUpOnTransaction(txMeta.id) + } + // if the value of the transaction is greater then the balance, fail. if (!sufficientBalance(txMeta.txParams, balance)) { const insufficientFundsError = new Error('Insufficient balance during rebroadcast.') @@ -160,4 +166,4 @@ module.exports = class PendingTransactionTracker extends EventEmitter { } nonceGlobalLock.releaseLock() } -} \ No newline at end of file +} -- cgit From c2624dd1a071ea4388df23400f4319e863d061c6 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Thu, 24 Aug 2017 00:02:06 -0700 Subject: fall back to `latest` if blockNumber is null --- app/scripts/lib/nonce-tracker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js index 08f1e1e86..0029ac953 100644 --- a/app/scripts/lib/nonce-tracker.js +++ b/app/scripts/lib/nonce-tracker.js @@ -88,7 +88,7 @@ class NonceTracker { // and pending count are from the same block const currentBlock = await this._getCurrentBlock() const blockNumber = currentBlock.blockNumber - const baseCountBN = await this.ethQuery.getTransactionCount(address, blockNumber) + const baseCountBN = await this.ethQuery.getTransactionCount(address, blockNumber || 'latest') const baseCount = baseCountBN.toNumber() assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) const nonceDetails = { blockNumber, baseCount } -- cgit From e294aa7e0dd4c773c7724f0fb152cfafece431fe Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 28 Aug 2017 10:41:01 -0700 Subject: inpage - lint fix --- app/scripts/lib/inpage-provider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts/lib') diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js index 8b36b4433..7c74a62d3 100644 --- a/app/scripts/lib/inpage-provider.js +++ b/app/scripts/lib/inpage-provider.js @@ -90,7 +90,7 @@ MetamaskInpageProvider.prototype.send = function (payload) { case 'net_version': const networkVersion = self.publicConfigStore.getState().networkVersion - result = networkVersion || null + result = networkVersion || null break // throw not-supported Error -- cgit