aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-07-19 06:11:29 +0800
committerkumavis <aaron@kumavis.me>2017-07-19 06:11:29 +0800
commitaa48ed34c458874914c44400fb68885069625a6f (patch)
treeaed40656054d1d31cc23d4534c032cc32f5e546a /app
parent67fdba5e42d8deac1dcbb4a82fd3d22b944e639a (diff)
downloadtangerine-wallet-browser-aa48ed34c458874914c44400fb68885069625a6f.tar
tangerine-wallet-browser-aa48ed34c458874914c44400fb68885069625a6f.tar.gz
tangerine-wallet-browser-aa48ed34c458874914c44400fb68885069625a6f.tar.bz2
tangerine-wallet-browser-aa48ed34c458874914c44400fb68885069625a6f.tar.lz
tangerine-wallet-browser-aa48ed34c458874914c44400fb68885069625a6f.tar.xz
tangerine-wallet-browser-aa48ed34c458874914c44400fb68885069625a6f.tar.zst
tangerine-wallet-browser-aa48ed34c458874914c44400fb68885069625a6f.zip
nonce-tracker - fix lock mechanism to be a real mutex
Diffstat (limited to 'app')
-rw-r--r--app/scripts/lib/nonce-tracker.js26
1 files changed, 15 insertions, 11 deletions
diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js
index ba05fd124..7a450cb78 100644
--- a/app/scripts/lib/nonce-tracker.js
+++ b/app/scripts/lib/nonce-tracker.js
@@ -1,5 +1,6 @@
const EthQuery = require('eth-query')
const assert = require('assert')
+const Mutex = require('await-semaphore').Mutex
class NonceTracker {
@@ -13,10 +14,8 @@ class NonceTracker {
// releaseLock must be called
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
async getNonceLock (address) {
- // await lock free
- await this.lockMap[address]
- // take lock
- const releaseLock = this._takeLock(address)
+ // 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
@@ -41,13 +40,18 @@ class NonceTracker {
})
}
- _takeLock (lockId) {
- let releaseLock = null
- // create and store lock
- const lock = new Promise((resolve, reject) => { releaseLock = resolve })
- this.lockMap[lockId] = lock
- // setup lock teardown
- lock.then(() => delete this.lockMap[lockId])
+ _lookupMutex (lockId) {
+ let mutex = this.lockMap[lockId]
+ if (!mutex) {
+ mutex = new Mutex()
+ this.lockMap[lockId] = mutex
+ }
+ return mutex
+ }
+
+ async _takeMutex (lockId) {
+ const mutex = this._lookupMutex(lockId)
+ const releaseLock = await mutex.acquire()
return releaseLock
}