mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
nonce-tracker - fix lock mechanism to be a real mutex
This commit is contained in:
parent
67fdba5e42
commit
aa48ed34c4
@ -1,5 +1,6 @@
|
|||||||
const EthQuery = require('eth-query')
|
const EthQuery = require('eth-query')
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
|
const Mutex = require('await-semaphore').Mutex
|
||||||
|
|
||||||
class NonceTracker {
|
class NonceTracker {
|
||||||
|
|
||||||
@ -13,10 +14,8 @@ class NonceTracker {
|
|||||||
// releaseLock must be called
|
// releaseLock must be called
|
||||||
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
|
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
|
||||||
async getNonceLock (address) {
|
async getNonceLock (address) {
|
||||||
// await lock free
|
// await lock free, then take lock
|
||||||
await this.lockMap[address]
|
const releaseLock = await this._takeMutex(address)
|
||||||
// take lock
|
|
||||||
const releaseLock = this._takeLock(address)
|
|
||||||
// calculate next nonce
|
// calculate next nonce
|
||||||
// we need to make sure our base count
|
// we need to make sure our base count
|
||||||
// and pending count are from the same block
|
// and pending count are from the same block
|
||||||
@ -41,13 +40,18 @@ class NonceTracker {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
_takeLock (lockId) {
|
_lookupMutex (lockId) {
|
||||||
let releaseLock = null
|
let mutex = this.lockMap[lockId]
|
||||||
// create and store lock
|
if (!mutex) {
|
||||||
const lock = new Promise((resolve, reject) => { releaseLock = resolve })
|
mutex = new Mutex()
|
||||||
this.lockMap[lockId] = lock
|
this.lockMap[lockId] = mutex
|
||||||
// setup lock teardown
|
}
|
||||||
lock.then(() => delete this.lockMap[lockId])
|
return mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
async _takeMutex (lockId) {
|
||||||
|
const mutex = this._lookupMutex(lockId)
|
||||||
|
const releaseLock = await mutex.acquire()
|
||||||
return releaseLock
|
return releaseLock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +47,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "^1.5.2",
|
"async": "^1.5.2",
|
||||||
|
"await-semaphore": "^0.1.1",
|
||||||
"babel-runtime": "^6.23.0",
|
"babel-runtime": "^6.23.0",
|
||||||
"bip39": "^2.2.0",
|
"bip39": "^2.2.0",
|
||||||
"bluebird": "^3.5.0",
|
"bluebird": "^3.5.0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user