mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
migrations - back fixes
This commit is contained in:
parent
7d243aacf9
commit
d4e30040a2
@ -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) {
|
||||||
|
const transactions = newState.TransactionController.transactions
|
||||||
|
|
||||||
newState.TransactionController.transactions = transactions.map((txMeta) => {
|
newState.TransactionController.transactions = transactions.map((txMeta) => {
|
||||||
if (txMeta.status !== 'submitted' || txMeta.submittedTime) return txMeta
|
if (txMeta.status !== 'submitted' || txMeta.submittedTime) return txMeta
|
||||||
txMeta.submittedTime = (new Date()).getTime()
|
txMeta.submittedTime = (new Date()).getTime()
|
||||||
return txMeta
|
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
|
const { TransactionController } = newState
|
||||||
|
if (TransactionController && TransactionController.transactions) {
|
||||||
|
const transactions = newState.TransactionController.transactions
|
||||||
|
|
||||||
let reverseTxList = transactions.reverse()
|
if (transactions.length <= 40) return newState
|
||||||
let stripping = true
|
|
||||||
while (reverseTxList.length > 40 && stripping) {
|
let reverseTxList = transactions.reverse()
|
||||||
let txIndex = reverseTxList.findIndex((txMeta) => {
|
let stripping = true
|
||||||
return (txMeta.status === 'failed' ||
|
while (reverseTxList.length > 40 && stripping) {
|
||||||
txMeta.status === 'rejected' ||
|
let txIndex = reverseTxList.findIndex((txMeta) => {
|
||||||
txMeta.status === 'confirmed' ||
|
return (txMeta.status === 'failed' ||
|
||||||
txMeta.status === 'dropped')
|
txMeta.status === 'rejected' ||
|
||||||
})
|
txMeta.status === 'confirmed' ||
|
||||||
if (txIndex < 0) stripping = false
|
txMeta.status === 'dropped')
|
||||||
else reverseTxList.splice(txIndex, 1)
|
})
|
||||||
|
if (txIndex < 0) stripping = false
|
||||||
|
else reverseTxList.splice(txIndex, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
newState.TransactionController.transactions = reverseTxList.reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
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…
x
Reference in New Issue
Block a user