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

nonce-tracker - make nonce strategy api and naming more symmetical

This commit is contained in:
kumavis 2017-08-22 13:52:15 -07:00
parent 604c91f7b2
commit 98bc9b6656

View File

@ -26,14 +26,16 @@ 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)
const localNextNonce = this._getLocalNextNonce(address) // evaluate multiple nextNonce strategies
const nonceDetails = await this._getNetworkNonceAndDetails(address) const nonceDetails = {}
const networkNonce = nonceDetails.networkNonce const localNonceResult = await this._getlocalNextNonce(address)
const nextNonce = Math.max(networkNonce, localNextNonce) nonceDetails.local = localNonceResult.details
const currentPendingNonce = this._getLocalPendingNonce(address) const networkNonceResult = await this._getNetworkNextNonce(address)
nonceDetails.network = networkNonceResult.details
const nextNonce = Math.max(networkNonceResult.nonce, localNonceResult.nonce)
assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`) assert(Number.isInteger(nextNonce), `nonce-tracker - nextNonce is not an integer - got: (${typeof nextNonce}) "${nextNonce}"`)
// collect the numbers used to calculate the nonce for debugging // collect the numbers used to calculate the nonce for debugging
nonceDetails.localNextNonce = localNextNonce const currentPendingNonce = this._getLocalPendingNonce(address)
nonceDetails.currentPendingNonce = currentPendingNonce nonceDetails.currentPendingNonce = currentPendingNonce
// return nonce and release cb // return nonce and release cb
return { nextNonce, nonceDetails, releaseLock } return { nextNonce, nonceDetails, releaseLock }
@ -78,7 +80,7 @@ class NonceTracker {
return mutex return mutex
} }
async _getNetworkNonceAndDetails (address) { async _getNetworkNextNonce (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
@ -87,28 +89,29 @@ class NonceTracker {
const baseCountHex = await this._getTxCount(address, currentBlock) const baseCountHex = await this._getTxCount(address, currentBlock)
const baseCount = parseInt(baseCountHex, 16) const baseCount = parseInt(baseCountHex, 16)
assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`)
// if the nonce provided by the network is higher then a pending tx const nonceDetails = { blockNumber, baseCountHex, baseCount }
// toss out the pending txCount return { name: 'network', nonce: baseCount, details: nonceDetails }
const networkNonce = baseCount
return {networkNonce, blockNumber, baseCountHex, baseCount}
} }
_getLocalNextNonce (address) { async _getlocalNextNonce (address) {
const confirmedTransactions = this._reduceTxListToUniqueNonces(this.getConfirmedTransactions(address)) const confirmedTransactions = this._reduceTxListToUniqueNonces(this.getConfirmedTransactions(address))
const pendingTransactions = this._reduceTxListToUniqueNonces(this.getPendingTransactions(address)) const pendingTransactions = this._reduceTxListToUniqueNonces(this.getPendingTransactions(address))
const transactions = this._reduceTxListToUniqueNonces(confirmedTransactions.concat(pendingTransactions)) const transactions = this._reduceTxListToUniqueNonces(confirmedTransactions.concat(pendingTransactions))
let localNonce = this._getHighestNonce(transactions) const highestNonce = this._getHighestNonce(transactions)
let localNonce = highestNonce
// throw out localNonce if not a number // throw out localNonce if not a number
if (!Number.isInteger(localNonce)) localNonce = 0 if (!Number.isInteger(highestNonce)) localNonce = 0
const pendingCount = this._getPendingTransactionCount(address)
const confirmedCount = confirmedTransactions.length
if ( if (
// the local nonce is not 0 // the local nonce is not 0
localNonce || localNonce ||
// or their are pending or confirmed transactions // or their are pending or confirmed transactions
this._getPendingTransactionCount(address) || pendingCount ||
confirmedTransactions.length confirmedCount
) ++localNonce ) ++localNonce
return localNonce const nonceDetails = { highestNonce, localNonce, pendingCount, confirmedCount }
return { name: 'local', nonce: localNonce, details: nonceDetails }
} }
_getLocalPendingNonce (address) { _getLocalPendingNonce (address) {
@ -122,7 +125,6 @@ class NonceTracker {
return this._reduceTxListToUniqueNonces(pendingTransactions).length return this._reduceTxListToUniqueNonces(pendingTransactions).length
} }
_reduceTxListToUniqueNonces (txList) { _reduceTxListToUniqueNonces (txList) {
const reducedTxList = txList.reduce((reducedList, txMeta, index) => { const reducedTxList = txList.reduce((reducedList, txMeta, index) => {
if (!index) return [txMeta] if (!index) return [txMeta]