mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
transactions - block nonce-tracker while updating pending transactions
This commit is contained in:
parent
aa48ed34c4
commit
12d6f21627
@ -484,12 +484,15 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
// if confirmed sets the tx status as 'confirmed'
|
// if confirmed sets the tx status as 'confirmed'
|
||||||
async _checkPendingTxs () {
|
async _checkPendingTxs () {
|
||||||
const signedTxList = this.getFilteredTxList({status: 'submitted'})
|
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 {
|
try {
|
||||||
await Promise.all(signedTxList.map((txMeta) => this._checkPendingTx(txMeta)))
|
await Promise.all(signedTxList.map((txMeta) => this._checkPendingTx(txMeta)))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('TransactionController - Error updating pending transactions')
|
console.error('TransactionController - Error updating pending transactions')
|
||||||
console.error(err)
|
console.error(err)
|
||||||
}
|
}
|
||||||
|
nonceGlobalLock.releaseLock()
|
||||||
}
|
}
|
||||||
|
|
||||||
async _checkPendingTx (txMeta) {
|
async _checkPendingTx (txMeta) {
|
||||||
|
@ -11,9 +11,18 @@ class NonceTracker {
|
|||||||
this.lockMap = {}
|
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
|
||||||
// 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 global mutex free
|
||||||
|
await this._globalMutexFree()
|
||||||
// await lock free, then take lock
|
// await lock free, then take lock
|
||||||
const releaseLock = await this._takeMutex(address)
|
const releaseLock = await this._takeMutex(address)
|
||||||
// calculate next nonce
|
// calculate next nonce
|
||||||
@ -40,21 +49,6 @@ class NonceTracker {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
_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
|
|
||||||
}
|
|
||||||
|
|
||||||
async _getTxCount (address, currentBlock) {
|
async _getTxCount (address, currentBlock) {
|
||||||
const blockNumber = currentBlock.number
|
const blockNumber = currentBlock.number
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -64,6 +58,27 @@ class NonceTracker {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _globalMutexFree () {
|
||||||
|
const globalMutex = this._lookupMutex('global')
|
||||||
|
const release = await globalMutex.acquire()
|
||||||
|
release()
|
||||||
|
}
|
||||||
|
|
||||||
|
async _takeMutex (lockId) {
|
||||||
|
const mutex = this._lookupMutex(lockId)
|
||||||
|
const releaseLock = await mutex.acquire()
|
||||||
|
return releaseLock
|
||||||
|
}
|
||||||
|
|
||||||
|
_lookupMutex (lockId) {
|
||||||
|
let mutex = this.lockMap[lockId]
|
||||||
|
if (!mutex) {
|
||||||
|
mutex = new Mutex()
|
||||||
|
this.lockMap[lockId] = mutex
|
||||||
|
}
|
||||||
|
return mutex
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = NonceTracker
|
module.exports = NonceTracker
|
||||||
|
Loading…
Reference in New Issue
Block a user