diff options
author | Thomas Huang <tmashuang@users.noreply.github.com> | 2018-04-06 09:53:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-06 09:53:34 +0800 |
commit | 77486a23654a7709091f99bc7ef76d894a46113a (patch) | |
tree | 0dd0b39f45d67b88b9eda83337fdf5c2a6f45864 | |
parent | b8efe120a9ff21aa4b9762dbd72de9a65a0d768d (diff) | |
parent | 7d243aacf9db9dc8e3e2e3acfc54298ffc06fe12 (diff) | |
download | tangerine-wallet-browser-77486a23654a7709091f99bc7ef76d894a46113a.tar tangerine-wallet-browser-77486a23654a7709091f99bc7ef76d894a46113a.tar.gz tangerine-wallet-browser-77486a23654a7709091f99bc7ef76d894a46113a.tar.bz2 tangerine-wallet-browser-77486a23654a7709091f99bc7ef76d894a46113a.tar.lz tangerine-wallet-browser-77486a23654a7709091f99bc7ef76d894a46113a.tar.xz tangerine-wallet-browser-77486a23654a7709091f99bc7ef76d894a46113a.tar.zst tangerine-wallet-browser-77486a23654a7709091f99bc7ef76d894a46113a.zip |
Merge pull request #3906 from MetaMask/migration-25
Migration 25
-rw-r--r-- | app/scripts/migrations/025.js | 61 | ||||
-rw-r--r-- | app/scripts/migrations/index.js | 1 | ||||
-rw-r--r-- | test/unit/migrations/025-test.js | 49 | ||||
-rw-r--r-- | test/unit/migrator-test.js | 25 |
4 files changed, 133 insertions, 3 deletions
diff --git a/app/scripts/migrations/025.js b/app/scripts/migrations/025.js new file mode 100644 index 000000000..fc3b20a44 --- /dev/null +++ b/app/scripts/migrations/025.js @@ -0,0 +1,61 @@ +// next version number +const version = 25 + +/* + +normalizes txParams on unconfirmed txs + +*/ +const ethUtil = require('ethereumjs-util') +const clone = require('clone') + +module.exports = { + version, + + migrate: async function (originalVersionedData) { + const versionedData = clone(originalVersionedData) + versionedData.meta.version = version + const state = versionedData.data + const newState = transformState(state) + versionedData.data = newState + return versionedData + }, +} + +function transformState (state) { + const newState = state + + if (newState.TransactionController) { + if (newState.TransactionController.transactions) { + const transactions = newState.TransactionController.transactions + newState.TransactionController.transactions = transactions.map((txMeta) => { + if (txMeta.status !== 'unapproved') return txMeta + txMeta.txParams = normalizeTxParams(txMeta.txParams) + return txMeta + }) + } + } + + return newState +} + +function normalizeTxParams (txParams) { + // functions that handle normalizing of that key in txParams + const whiteList = { + from: from => ethUtil.addHexPrefix(from).toLowerCase(), + to: to => ethUtil.addHexPrefix(txParams.to).toLowerCase(), + nonce: nonce => ethUtil.addHexPrefix(nonce), + value: value => ethUtil.addHexPrefix(value), + data: data => ethUtil.addHexPrefix(data), + gas: gas => ethUtil.addHexPrefix(gas), + gasPrice: gasPrice => ethUtil.addHexPrefix(gasPrice), + } + + // apply only keys in the whiteList + const normalizedTxParams = {} + Object.keys(whiteList).forEach((key) => { + if (txParams[key]) normalizedTxParams[key] = whiteList[key](txParams[key]) + }) + + return normalizedTxParams +} diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index 7e4542740..6c4a51b32 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -35,4 +35,5 @@ module.exports = [ require('./022'), require('./023'), require('./024'), + require('./025'), ] diff --git a/test/unit/migrations/025-test.js b/test/unit/migrations/025-test.js new file mode 100644 index 000000000..76c25dbb6 --- /dev/null +++ b/test/unit/migrations/025-test.js @@ -0,0 +1,49 @@ +const assert = require('assert') +const migration25 = require('../../../app/scripts/migrations/025') +const firstTimeState = { + meta: {}, + data: require('../../../app/scripts/first-time-state'), +} + +const storage = { + "meta": {}, + "data": { + "TransactionController": { + "transactions": [ + ] + }, + }, +} + +const transactions = [] + + +while (transactions.length <= 10) { + transactions.push({ txParams: { from: '0x8aCce2391c0d510a6c5E5d8f819a678f79b7e675', random: 'stuff', chainId: 2 }, status: 'unapproved' }) + transactions.push({ txParams: { from: '0x8aCce2391c0d510a6c5E5d8f819a678f79b7e675' }, status: 'confirmed' }) +} + + +storage.data.TransactionController.transactions = transactions + +describe('storage is migrated successfully and the txParams.from are lowercase', () => { + it('should lowercase the from for unapproved txs', (done) => { + migration25.migrate(storage) + .then((migratedData) => { + const migratedTransactions = migratedData.data.TransactionController.transactions + migratedTransactions.forEach((tx) => { + if (tx.status === 'unapproved') assert(!tx.txParams.random) + if (tx.status === 'unapproved') assert(!tx.txParams.chainId) + }) + done() + }).catch(done) + }) + + it('should migrate first time state', (done) => { + migration25.migrate(firstTimeState) + .then((migratedData) => { + assert.equal(migratedData.meta.version, 25) + done() + }).catch(done) + }) +}) diff --git a/test/unit/migrator-test.js b/test/unit/migrator-test.js index 16066fefe..2bad7da51 100644 --- a/test/unit/migrator-test.js +++ b/test/unit/migrator-test.js @@ -1,7 +1,8 @@ const assert = require('assert') const clone = require('clone') const Migrator = require('../../app/scripts/lib/migrator/') -const migrations = [ +const liveMigrations = require('../../app/scripts/migrations/') +const stubMigrations = [ { version: 1, migrate: (data) => { @@ -29,13 +30,31 @@ const migrations = [ }, ] const versionedData = {meta: {version: 0}, data: {hello: 'world'}} + +const firstTimeState = { + meta: { version: 0 }, + data: require('../../app/scripts/first-time-state'), +} + describe('Migrator', () => { - const migrator = new Migrator({ migrations }) + const migrator = new Migrator({ migrations: stubMigrations }) it('migratedData version should be version 3', (done) => { migrator.migrateData(versionedData) .then((migratedData) => { - assert.equal(migratedData.meta.version, migrations[2].version) + assert.equal(migratedData.meta.version, stubMigrations[2].version) done() }).catch(done) }) + + it('should match the last version in live migrations', (done) => { + const migrator = new Migrator({ migrations: liveMigrations }) + migrator.migrateData(firstTimeState) + .then((migratedData) => { + console.log(migratedData) + const last = liveMigrations.length - 1 + assert.equal(migratedData.meta.version, liveMigrations[last].version) + done() + }).catch(done) + }) + }) |