aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/migrations/019.js
blob: 9dfe96050c1f1932e759e618f655110fa523d09f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

const version = 19

/*

This migration sets transactions as failed
whos nonce is too high

*/

const clone = require('clone')

module.exports = {
  version,

  migrate: function (originalVersionedData) {
    const versionedData = clone(originalVersionedData)
    versionedData.meta.version = version
    try {
      const state = versionedData.data
      const newState = transformState(state)
      versionedData.data = newState
    } catch (err) {
      console.warn(`MetaMask Migration #${version}` + err.stack)
    }
    return Promise.resolve(versionedData)
  },
}

function transformState (state) {
  const newState = state
  const transactions = newState.TransactionController.transactions
  newState.TransactionController.transactions = transactions.map((txMeta, _, txList) => {
    if (txMeta.status !== 'submitted') return txMeta

    const confirmedTxs = txList.filter((tx) => tx.status === 'confirmed')
    .filter((tx) => tx.txParams.from === txMeta.txParams.from)
    .filter((tx) => tx.metamaskNetworkId.from === txMeta.metamaskNetworkId.from)
    const highestConfirmedNonce = getHighestNonce(confirmedTxs)

    if (parseInt(txMeta.txParams.nonce, 16) > highestConfirmedNonce + 1) {
      txMeta.status = 'failed'
      txMeta.err = {
        message: 'nonce too high',
        note: 'migration 019 custom error',
      }
    }
    return txMeta
  })
  return newState
}

function getHighestNonce (txList) {
  const nonces = txList.map((txMeta) => {
  const nonce = txMeta.txParams.nonce
    return parseInt(nonce || '0x0', 16)
  })
  const highestNonce = Math.max.apply(null, nonces)
  return highestNonce
}