Merge pull request #3907 from MetaMask/fix-all-migrations

migrations - back fixes
feature/default_network_editable
kumavis 7 years ago committed by GitHub
commit 90bcf9f7d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/scripts/migrations/013.js
  2. 15
      app/scripts/migrations/015.js
  3. 22
      app/scripts/migrations/016.js
  4. 21
      app/scripts/migrations/017.js
  5. 39
      app/scripts/migrations/018.js
  6. 44
      app/scripts/migrations/019.js
  7. 17
      app/scripts/migrations/022.js
  8. 38
      app/scripts/migrations/023.js
  9. 10
      test/unit/migrator-test.js

@ -27,8 +27,11 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
if (newState.config.provider.type === 'testnet') { const { config } = newState
newState.config.provider.type = 'ropsten' if ( config && config.provider ) {
if (config.provider.type === 'testnet') {
newState.config.provider.type = 'ropsten'
}
} }
return newState return newState
} }

@ -28,11 +28,14 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
const transactions = newState.TransactionController.transactions const { TransactionController } = newState
newState.TransactionController.transactions = transactions.map((txMeta) => { if (TransactionController && TransactionController.transactions) {
if (!txMeta.err) return txMeta const transactions = TransactionController.transactions
else if (txMeta.err.message === 'Gave up submitting tx.') txMeta.status = 'failed' newState.TransactionController.transactions = transactions.map((txMeta) => {
return txMeta if (!txMeta.err) return txMeta
}) else if (txMeta.err.message === 'Gave up submitting tx.') txMeta.status = 'failed'
return txMeta
})
}
return newState return newState
} }

@ -28,14 +28,18 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
const transactions = newState.TransactionController.transactions const { TransactionController } = newState
newState.TransactionController.transactions = transactions.map((txMeta) => { if (TransactionController && TransactionController.transactions) {
if (!txMeta.err) return txMeta const transactions = newState.TransactionController.transactions
if (txMeta.err === 'transaction with the same hash was already imported.') {
txMeta.status = 'submitted' newState.TransactionController.transactions = transactions.map((txMeta) => {
delete txMeta.err if (!txMeta.err) return txMeta
} if (txMeta.err === 'transaction with the same hash was already imported.') {
return txMeta txMeta.status = 'submitted'
}) delete txMeta.err
}
return txMeta
})
}
return newState return newState
} }

@ -27,14 +27,17 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
const transactions = newState.TransactionController.transactions const { TransactionController } = newState
newState.TransactionController.transactions = transactions.map((txMeta) => { if (TransactionController && TransactionController.transactions) {
if (!txMeta.status === 'failed') return txMeta const transactions = newState.TransactionController.transactions
if (txMeta.retryCount > 0 && txMeta.retryCount < 2) { newState.TransactionController.transactions = transactions.map((txMeta) => {
txMeta.status = 'submitted' if (!txMeta.status === 'failed') return txMeta
delete txMeta.err if (txMeta.retryCount > 0 && txMeta.retryCount < 2) {
} txMeta.status = 'submitted'
return txMeta delete txMeta.err
}) }
return txMeta
})
}
return newState return newState
} }

@ -29,24 +29,27 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
const transactions = newState.TransactionController.transactions const { TransactionController } = newState
newState.TransactionController.transactions = transactions.map((txMeta) => { if (TransactionController && TransactionController.transactions) {
// no history: initialize const transactions = newState.TransactionController.transactions
if (!txMeta.history || txMeta.history.length === 0) { newState.TransactionController.transactions = transactions.map((txMeta) => {
const snapshot = txStateHistoryHelper.snapshotFromTxMeta(txMeta) // no history: initialize
txMeta.history = [snapshot] if (!txMeta.history || txMeta.history.length === 0) {
const snapshot = txStateHistoryHelper.snapshotFromTxMeta(txMeta)
txMeta.history = [snapshot]
return txMeta
}
// has history: migrate
const newHistory = (
txStateHistoryHelper.migrateFromSnapshotsToDiffs(txMeta.history)
// remove empty diffs
.filter((entry) => {
return !Array.isArray(entry) || entry.length > 0
})
)
txMeta.history = newHistory
return txMeta return txMeta
} })
// has history: migrate }
const newHistory = (
txStateHistoryHelper.migrateFromSnapshotsToDiffs(txMeta.history)
// remove empty diffs
.filter((entry) => {
return !Array.isArray(entry) || entry.length > 0
})
)
txMeta.history = newHistory
return txMeta
})
return newState return newState
} }

@ -29,32 +29,36 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
const transactions = newState.TransactionController.transactions const { TransactionController } = newState
if (TransactionController && TransactionController.transactions) {
newState.TransactionController.transactions = transactions.map((txMeta, _, txList) => { const transactions = newState.TransactionController.transactions
if (txMeta.status !== 'submitted') return txMeta
const confirmedTxs = txList.filter((tx) => tx.status === 'confirmed') newState.TransactionController.transactions = transactions.map((txMeta, _, txList) => {
.filter((tx) => tx.txParams.from === txMeta.txParams.from) if (txMeta.status !== 'submitted') return txMeta
.filter((tx) => tx.metamaskNetworkId.from === txMeta.metamaskNetworkId.from)
const highestConfirmedNonce = getHighestNonce(confirmedTxs)
const pendingTxs = txList.filter((tx) => tx.status === 'submitted') const confirmedTxs = txList.filter((tx) => tx.status === 'confirmed')
.filter((tx) => tx.txParams.from === txMeta.txParams.from) .filter((tx) => tx.txParams.from === txMeta.txParams.from)
.filter((tx) => tx.metamaskNetworkId.from === txMeta.metamaskNetworkId.from) .filter((tx) => tx.metamaskNetworkId.from === txMeta.metamaskNetworkId.from)
const highestContinuousNonce = getHighestContinuousFrom(pendingTxs, highestConfirmedNonce) const highestConfirmedNonce = getHighestNonce(confirmedTxs)
const maxNonce = Math.max(highestContinuousNonce, highestConfirmedNonce) const pendingTxs = txList.filter((tx) => tx.status === 'submitted')
.filter((tx) => tx.txParams.from === txMeta.txParams.from)
.filter((tx) => tx.metamaskNetworkId.from === txMeta.metamaskNetworkId.from)
const highestContinuousNonce = getHighestContinuousFrom(pendingTxs, highestConfirmedNonce)
if (parseInt(txMeta.txParams.nonce, 16) > maxNonce + 1) { const maxNonce = Math.max(highestContinuousNonce, highestConfirmedNonce)
txMeta.status = 'failed'
txMeta.err = { if (parseInt(txMeta.txParams.nonce, 16) > maxNonce + 1) {
message: 'nonce too high', txMeta.status = 'failed'
note: 'migration 019 custom error', txMeta.err = {
message: 'nonce too high',
note: 'migration 019 custom error',
}
} }
} return txMeta
return txMeta })
}) }
return newState return newState
} }

@ -28,12 +28,15 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
const transactions = newState.TransactionController.transactions const { TransactionController } = newState
if (TransactionController && TransactionController.transactions) {
newState.TransactionController.transactions = transactions.map((txMeta) => { const transactions = newState.TransactionController.transactions
if (txMeta.status !== 'submitted' || txMeta.submittedTime) return txMeta
txMeta.submittedTime = (new Date()).getTime() newState.TransactionController.transactions = transactions.map((txMeta) => {
return txMeta if (txMeta.status !== 'submitted' || txMeta.submittedTime) return txMeta
}) txMeta.submittedTime = (new Date()).getTime()
return txMeta
})
}
return newState return newState
} }

@ -28,23 +28,27 @@ module.exports = {
function transformState (state) { function transformState (state) {
const newState = state const newState = state
const transactions = newState.TransactionController.transactions
if (transactions.length <= 40) return newState
let reverseTxList = transactions.reverse()
let stripping = true
while (reverseTxList.length > 40 && stripping) {
let txIndex = reverseTxList.findIndex((txMeta) => {
return (txMeta.status === 'failed' ||
txMeta.status === 'rejected' ||
txMeta.status === 'confirmed' ||
txMeta.status === 'dropped')
})
if (txIndex < 0) stripping = false
else reverseTxList.splice(txIndex, 1)
}
newState.TransactionController.transactions = reverseTxList.reverse() const { TransactionController } = newState
if (TransactionController && TransactionController.transactions) {
const transactions = newState.TransactionController.transactions
if (transactions.length <= 40) return newState
let reverseTxList = transactions.reverse()
let stripping = true
while (reverseTxList.length > 40 && stripping) {
let txIndex = reverseTxList.findIndex((txMeta) => {
return (txMeta.status === 'failed' ||
txMeta.status === 'rejected' ||
txMeta.status === 'confirmed' ||
txMeta.status === 'dropped')
})
if (txIndex < 0) stripping = false
else reverseTxList.splice(txIndex, 1)
}
newState.TransactionController.transactions = reverseTxList.reverse()
}
return newState return newState
} }

@ -50,11 +50,19 @@ describe('Migrator', () => {
const migrator = new Migrator({ migrations: liveMigrations }) const migrator = new Migrator({ migrations: liveMigrations })
migrator.migrateData(firstTimeState) migrator.migrateData(firstTimeState)
.then((migratedData) => { .then((migratedData) => {
console.log(migratedData)
const last = liveMigrations.length - 1 const last = liveMigrations.length - 1
assert.equal(migratedData.meta.version, liveMigrations[last].version) assert.equal(migratedData.meta.version, liveMigrations[last].version)
done() done()
}).catch(done) }).catch(done)
}) })
it('should emit an error', function (done) {
this.timeout(15000)
const migrator = new Migrator({ migrations: [{ version: 1, migrate: async () => { throw new Error('test') } } ] })
migrator.on('error', () => done())
migrator.migrateData({ meta: {version: 0} })
.then((migratedData) => {
}).catch(done)
})
}) })

Loading…
Cancel
Save