1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

nonce-tracker - wrap nonce calculations in try-catch and release lock on error

This commit is contained in:
kumavis 2018-06-12 10:55:54 -07:00
parent 030fea7136
commit c86f935889

View File

@ -50,29 +50,35 @@ class NonceTracker {
await this._globalMutexFree() 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)
// evaluate multiple nextNonce strategies try {
const nonceDetails = {} // evaluate multiple nextNonce strategies
const networkNonceResult = await this._getNetworkNextNonce(address) const nonceDetails = {}
const highestLocallyConfirmed = this._getHighestLocallyConfirmed(address) const networkNonceResult = await this._getNetworkNextNonce(address)
const nextNetworkNonce = networkNonceResult.nonce const highestLocallyConfirmed = this._getHighestLocallyConfirmed(address)
const highestSuggested = Math.max(nextNetworkNonce, highestLocallyConfirmed) const nextNetworkNonce = networkNonceResult.nonce
const highestSuggested = Math.max(nextNetworkNonce, highestLocallyConfirmed)
const pendingTxs = this.getPendingTransactions(address) const pendingTxs = this.getPendingTransactions(address)
const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestSuggested) || 0 const localNonceResult = this._getHighestContinuousFrom(pendingTxs, highestSuggested) || 0
nonceDetails.params = { nonceDetails.params = {
highestLocallyConfirmed, highestLocallyConfirmed,
highestSuggested, highestSuggested,
nextNetworkNonce, 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}"`)
// return nonce and release cb
return { nextNonce, nonceDetails, releaseLock }
} catch (err) {
// release lock if we encounter an error
releaseLock()
throw err
} }
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}"`)
// return nonce and release cb
return { nextNonce, nonceDetails, releaseLock }
} }
async _globalMutexFree () { async _globalMutexFree () {