mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 01:39:44 +01:00
migration for removing unnecessary transactions from state
This commit is contained in:
parent
43dde3cbde
commit
f0f45e6fe1
50
app/scripts/migrations/023.js
Normal file
50
app/scripts/migrations/023.js
Normal file
@ -0,0 +1,50 @@
|
||||
|
||||
const version = 23
|
||||
|
||||
/*
|
||||
|
||||
This migration removes transactions that are no longer usefull down to 40 total
|
||||
|
||||
*/
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
}
|
@ -32,4 +32,6 @@ module.exports = [
|
||||
require('./019'),
|
||||
require('./020'),
|
||||
require('./021'),
|
||||
require('./022'),
|
||||
require('./023'),
|
||||
]
|
||||
|
99
test/unit/migrations/023-test.js
Normal file
99
test/unit/migrations/023-test.js
Normal file
@ -0,0 +1,99 @@
|
||||
const assert = require('assert')
|
||||
const migration23 = require('../../../app/scripts/migrations/023')
|
||||
const properTime = (new Date()).getTime()
|
||||
const storage = {
|
||||
"meta": {},
|
||||
"data": {
|
||||
"TransactionController": {
|
||||
"transactions": [
|
||||
]
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const transactions = []
|
||||
const transactions40 = []
|
||||
const transactions20 = []
|
||||
|
||||
const txStates = [
|
||||
'unapproved',
|
||||
'approved',
|
||||
'signed',
|
||||
'submitted',
|
||||
'confirmed',
|
||||
'rejected',
|
||||
'failed',
|
||||
'dropped',
|
||||
]
|
||||
|
||||
const deletableTxStates = [
|
||||
'confirmed',
|
||||
'rejected',
|
||||
'failed',
|
||||
'dropped',
|
||||
]
|
||||
|
||||
let nonDeletableCount = 0
|
||||
|
||||
let status
|
||||
while (transactions.length <= 100) {
|
||||
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
|
||||
if (!deletableTxStates.find((s) => s === status)) nonDeletableCount++
|
||||
transactions.push({status})
|
||||
}
|
||||
|
||||
while (transactions40.length < 40) {
|
||||
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
|
||||
transactions40.push({status})
|
||||
}
|
||||
|
||||
while (transactions20.length < 20) {
|
||||
status = txStates[Math.floor(Math.random() * Math.floor(txStates.length - 1))]
|
||||
transactions20.push({status})
|
||||
}
|
||||
|
||||
|
||||
|
||||
storage.data.TransactionController.transactions = transactions
|
||||
|
||||
describe('storage is migrated successfully and the proper transactions are remove from state', () => {
|
||||
it('should remove transactions that are unneeded', (done) => {
|
||||
migration23.migrate(storage)
|
||||
.then((migratedData) => {
|
||||
let leftoverNonDeletableTxCount = 0
|
||||
const migratedTransactions = migratedData.data.TransactionController.transactions
|
||||
migratedTransactions.forEach((tx) => {
|
||||
if (!deletableTxStates.find((s) => s === tx.status)) {
|
||||
leftoverNonDeletableTxCount++
|
||||
}
|
||||
})
|
||||
assert.equal(leftoverNonDeletableTxCount, nonDeletableCount, 'migration shouldnt delete transactions we want to keep')
|
||||
assert((migratedTransactions.length >= 40), `should be equal or greater to 40 if they are non deletable states got ${migratedTransactions.length} transactions`)
|
||||
done()
|
||||
}).catch(done)
|
||||
})
|
||||
|
||||
it('should not remove any transactions because 40 is the expectable limit', (done) => {
|
||||
storage.meta.version = 22
|
||||
storage.data.TransactionController.transactions = transactions40
|
||||
migration23.migrate(storage)
|
||||
.then((migratedData) => {
|
||||
const migratedTransactions = migratedData.data.TransactionController.transactions
|
||||
|
||||
assert.equal(migratedTransactions.length, 40, 'migration shouldnt delete when at limit')
|
||||
done()
|
||||
}).catch(done)
|
||||
})
|
||||
|
||||
it('should not remove any transactions because 20 txs is under the expectable limit', (done) => {
|
||||
storage.meta.version = 22
|
||||
storage.data.TransactionController.transactions = transactions20
|
||||
migration23.migrate(storage)
|
||||
.then((migratedData) => {
|
||||
const migratedTransactions = migratedData.data.TransactionController.transactions
|
||||
assert.equal(migratedTransactions.length, 20, 'migration shouldnt delete when under limit')
|
||||
done()
|
||||
}).catch(done)
|
||||
})
|
||||
|
||||
})
|
@ -240,12 +240,12 @@ describe('TransactionStateManager', function () {
|
||||
})
|
||||
|
||||
describe('#wipeTransactions', function () {
|
||||
|
||||
|
||||
const specificAddress = '0xaa'
|
||||
const otherAddress = '0xbb'
|
||||
|
||||
it('should remove only the transactions from a specific address', function () {
|
||||
|
||||
|
||||
const txMetas = [
|
||||
{ id: 0, status: 'unapproved', txParams: { from: specificAddress, to: otherAddress }, metamaskNetworkId: currentNetworkId },
|
||||
{ id: 1, status: 'confirmed', txParams: { from: otherAddress, to: specificAddress }, metamaskNetworkId: currentNetworkId },
|
||||
@ -268,7 +268,7 @@ describe('TransactionStateManager', function () {
|
||||
{ id: 1, status: 'confirmed', txParams: { from: specificAddress, to: otherAddress }, metamaskNetworkId: otherNetworkId },
|
||||
{ id: 2, status: 'confirmed', txParams: { from: specificAddress, to: otherAddress }, metamaskNetworkId: otherNetworkId },
|
||||
]
|
||||
|
||||
|
||||
txMetas.forEach((txMeta) => txStateManager.addTx(txMeta, noop))
|
||||
|
||||
txStateManager.wipeTransactions(specificAddress)
|
||||
|
Loading…
Reference in New Issue
Block a user