aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-07-19 06:27:15 +0800
committerkumavis <aaron@kumavis.me>2017-07-19 06:27:15 +0800
commit12d6f2162791b421bd51313b0063e144b47ed868 (patch)
tree079d48fff6729abd6689a10badaf99bc36230220 /app
parentaa48ed34c458874914c44400fb68885069625a6f (diff)
downloadtangerine-wallet-browser-12d6f2162791b421bd51313b0063e144b47ed868.tar
tangerine-wallet-browser-12d6f2162791b421bd51313b0063e144b47ed868.tar.gz
tangerine-wallet-browser-12d6f2162791b421bd51313b0063e144b47ed868.tar.bz2
tangerine-wallet-browser-12d6f2162791b421bd51313b0063e144b47ed868.tar.lz
tangerine-wallet-browser-12d6f2162791b421bd51313b0063e144b47ed868.tar.xz
tangerine-wallet-browser-12d6f2162791b421bd51313b0063e144b47ed868.tar.zst
tangerine-wallet-browser-12d6f2162791b421bd51313b0063e144b47ed868.zip
transactions - block nonce-tracker while updating pending transactions
Diffstat (limited to 'app')
-rw-r--r--app/scripts/controllers/transactions.js3
-rw-r--r--app/scripts/lib/nonce-tracker.js43
2 files changed, 32 insertions, 14 deletions
diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js
index 1fc48aadd..5f3d84ebe 100644
--- a/app/scripts/controllers/transactions.js
+++ b/app/scripts/controllers/transactions.js
@@ -484,12 +484,15 @@ module.exports = class TransactionController extends EventEmitter {
// if confirmed sets the tx status as 'confirmed'
async _checkPendingTxs () {
const signedTxList = this.getFilteredTxList({status: 'submitted'})
+ // in order to keep the nonceTracker accurate we block it while updating pending transactions
+ const nonceGlobalLock = await this.nonceTracker.getGlobalLock()
try {
await Promise.all(signedTxList.map((txMeta) => this._checkPendingTx(txMeta)))
} catch (err) {
console.error('TransactionController - Error updating pending transactions')
console.error(err)
}
+ nonceGlobalLock.releaseLock()
}
async _checkPendingTx (txMeta) {
diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js
index 7a450cb78..b76dac4e8 100644
--- a/app/scripts/lib/nonce-tracker.js
+++ b/app/scripts/lib/nonce-tracker.js
@@ -11,9 +11,18 @@ class NonceTracker {
this.lockMap = {}
}
+ async getGlobalLock () {
+ const globalMutex = this._lookupMutex('global')
+ // await global mutex free
+ const releaseLock = await globalMutex.acquire()
+ return { releaseLock }
+ }
+
// releaseLock must be called
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
async getNonceLock (address) {
+ // await global mutex free
+ await this._globalMutexFree()
// await lock free, then take lock
const releaseLock = await this._takeMutex(address)
// calculate next nonce
@@ -40,13 +49,19 @@ class NonceTracker {
})
}
- _lookupMutex (lockId) {
- let mutex = this.lockMap[lockId]
- if (!mutex) {
- mutex = new Mutex()
- this.lockMap[lockId] = mutex
- }
- return mutex
+ 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()
+ release()
}
async _takeMutex (lockId) {
@@ -55,13 +70,13 @@ class NonceTracker {
return releaseLock
}
- 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)
- })
- })
+ _lookupMutex (lockId) {
+ let mutex = this.lockMap[lockId]
+ if (!mutex) {
+ mutex = new Mutex()
+ this.lockMap[lockId] = mutex
+ }
+ return mutex
}
}