Merge pull request #1914 from MetaMask/history-diff
Move Tx State History to diff-based formatfeature/default_network_editable
commit
e0c35179c2
@ -0,0 +1,37 @@ |
|||||||
|
const jsonDiffer = require('fast-json-patch') |
||||||
|
const clone = require('clone') |
||||||
|
|
||||||
|
module.exports = { |
||||||
|
generateHistoryEntry, |
||||||
|
replayHistory, |
||||||
|
snapshotFromTxMeta, |
||||||
|
migrateFromSnapshotsToDiffs, |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
function migrateFromSnapshotsToDiffs(longHistory) { |
||||||
|
return ( |
||||||
|
longHistory |
||||||
|
// convert non-initial history entries into diffs
|
||||||
|
.map((entry, index) => { |
||||||
|
if (index === 0) return entry |
||||||
|
return generateHistoryEntry(longHistory[index - 1], entry) |
||||||
|
}) |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
function generateHistoryEntry(previousState, newState) { |
||||||
|
return jsonDiffer.compare(previousState, newState) |
||||||
|
} |
||||||
|
|
||||||
|
function replayHistory(shortHistory) { |
||||||
|
return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument) |
||||||
|
} |
||||||
|
|
||||||
|
function snapshotFromTxMeta(txMeta) { |
||||||
|
// create txMeta snapshot for history
|
||||||
|
const snapshot = clone(txMeta) |
||||||
|
// dont include previous history in this snapshot
|
||||||
|
delete snapshot.history |
||||||
|
return snapshot |
||||||
|
} |
@ -0,0 +1,52 @@ |
|||||||
|
const version = 18 |
||||||
|
|
||||||
|
/* |
||||||
|
|
||||||
|
This migration updates "transaction state history" to diffs style |
||||||
|
|
||||||
|
*/ |
||||||
|
|
||||||
|
const clone = require('clone') |
||||||
|
const txStateHistoryHelper = require('../lib/tx-state-history-helper') |
||||||
|
|
||||||
|
|
||||||
|
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) => { |
||||||
|
// no history: initialize
|
||||||
|
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 newState |
||||||
|
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,23 @@ |
|||||||
|
const assert = require('assert') |
||||||
|
const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper') |
||||||
|
const testVault = require('../data/v17-long-history.json') |
||||||
|
|
||||||
|
|
||||||
|
describe('tx-state-history-helper', function () { |
||||||
|
it('migrates history to diffs and can recover original values', function () { |
||||||
|
testVault.data.TransactionController.transactions.forEach((tx, index) => { |
||||||
|
const newHistory = txStateHistoryHelper.migrateFromSnapshotsToDiffs(tx.history) |
||||||
|
newHistory.forEach((newEntry, index) => { |
||||||
|
if (index === 0) { |
||||||
|
assert.equal(Array.isArray(newEntry), false, 'initial history item IS NOT a json patch obj') |
||||||
|
} else { |
||||||
|
assert.equal(Array.isArray(newEntry), true, 'non-initial history entry IS a json patch obj') |
||||||
|
} |
||||||
|
const oldEntry = tx.history[index] |
||||||
|
const historySubset = newHistory.slice(0, index + 1) |
||||||
|
const reconstructedValue = txStateHistoryHelper.replayHistory(historySubset) |
||||||
|
assert.deepEqual(oldEntry, reconstructedValue, 'was able to reconstruct old entry from diffs') |
||||||
|
}) |
||||||
|
}) |
||||||
|
}) |
||||||
|
}) |
Loading…
Reference in new issue