1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Export individual fns from tx-state-history-helpers (#8370)

This commit is contained in:
Whymarrh Whitby 2020-04-20 13:00:51 -02:30 committed by GitHub
parent e05db747f5
commit 164923acd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 35 deletions

View File

@ -1,20 +1,12 @@
import jsonDiffer from 'fast-json-patch'
import { cloneDeep } from 'lodash'
/** @module*/
export default {
generateHistoryEntry,
replayHistory,
snapshotFromTxMeta,
migrateFromSnapshotsToDiffs,
}
/**
converts non-initial history entries into diffs
@param {array} longHistory
@returns {array}
*/
function migrateFromSnapshotsToDiffs (longHistory) {
export function migrateFromSnapshotsToDiffs (longHistory) {
return (
longHistory
// convert non-initial history entries into diffs
@ -39,7 +31,7 @@ function migrateFromSnapshotsToDiffs (longHistory) {
@param {string} [note] - a optional note for the state change
@returns {array}
*/
function generateHistoryEntry (previousState, newState, note) {
export function generateHistoryEntry (previousState, newState, note) {
const entry = jsonDiffer.compare(previousState, newState)
// Add a note to the first op, since it breaks if we append it to the entry
if (entry[0]) {
@ -56,7 +48,7 @@ function generateHistoryEntry (previousState, newState, note) {
Recovers previous txMeta state obj
@returns {Object}
*/
function replayHistory (_shortHistory) {
export function replayHistory (_shortHistory) {
const shortHistory = cloneDeep(_shortHistory)
return shortHistory.reduce((val, entry) => jsonDiffer.applyPatch(val, entry).newDocument)
}
@ -66,7 +58,7 @@ function replayHistory (_shortHistory) {
* @param {Object} txMeta - the tx metadata object
* @returns {Object} a deep clone without history
*/
function snapshotFromTxMeta (txMeta) {
export function snapshotFromTxMeta (txMeta) {
const shallow = { ...txMeta }
delete shallow.history
return cloneDeep(shallow)

View File

@ -1,7 +1,7 @@
import EventEmitter from 'safe-event-emitter'
import ObservableStore from 'obs-store'
import log from 'loglevel'
import txStateHistoryHelper from './lib/tx-state-history-helper'
import { generateHistoryEntry, replayHistory, snapshotFromTxMeta } from './lib/tx-state-history-helpers'
import createId from '../../lib/random-id'
import { getFinalStates, normalizeTxParams } from './lib/util'
/**
@ -146,7 +146,7 @@ class TransactionStateManager extends EventEmitter {
// initialize history
txMeta.history = []
// capture initial snapshot of txMeta for history
const snapshot = txStateHistoryHelper.snapshotFromTxMeta(txMeta)
const snapshot = snapshotFromTxMeta(txMeta)
txMeta.history.push(snapshot)
const transactions = this.getFullTxList()
@ -197,11 +197,11 @@ class TransactionStateManager extends EventEmitter {
}
// create txMeta snapshot for history
const currentState = txStateHistoryHelper.snapshotFromTxMeta(txMeta)
const currentState = snapshotFromTxMeta(txMeta)
// recover previous tx state obj
const previousState = txStateHistoryHelper.replayHistory(txMeta.history)
const previousState = replayHistory(txMeta.history)
// generate history entry and add to history
const entry = txStateHistoryHelper.generateHistoryEntry(previousState, currentState, note)
const entry = generateHistoryEntry(previousState, currentState, note)
txMeta.history.push(entry)
// commit txMeta to state

View File

@ -7,8 +7,10 @@ This migration updates "transaction state history" to diffs style
*/
import { cloneDeep } from 'lodash'
import txStateHistoryHelper from '../controllers/transactions/lib/tx-state-history-helper'
import {
snapshotFromTxMeta,
migrateFromSnapshotsToDiffs,
} from '../controllers/transactions/lib/tx-state-history-helpers'
export default {
@ -36,13 +38,13 @@ function transformState (state) {
newState.TransactionController.transactions = transactions.map((txMeta) => {
// no history: initialize
if (!txMeta.history || txMeta.history.length === 0) {
const snapshot = txStateHistoryHelper.snapshotFromTxMeta(txMeta)
const snapshot = snapshotFromTxMeta(txMeta)
txMeta.history = [snapshot]
return txMeta
}
// has history: migrate
const newHistory = (
txStateHistoryHelper.migrateFromSnapshotsToDiffs(txMeta.history)
migrateFromSnapshotsToDiffs(txMeta.history)
// remove empty diffs
.filter((entry) => {
return !Array.isArray(entry) || entry.length > 0

View File

@ -1,8 +1,6 @@
import txStateHistoryHelper from '../../app/scripts/controllers/transactions/lib/tx-state-history-helper'
import { snapshotFromTxMeta } from '../../app/scripts/controllers/transactions/lib/tx-state-history-helpers'
export default createTxMeta
function createTxMeta (partialMeta) {
export default function createTxMeta (partialMeta) {
const txMeta = Object.assign({
status: 'unapproved',
txParams: {},
@ -10,7 +8,7 @@ function createTxMeta (partialMeta) {
// initialize history
txMeta.history = []
// capture initial snapshot of txMeta for history
const snapshot = txStateHistoryHelper.snapshotFromTxMeta(txMeta)
const snapshot = snapshotFromTxMeta(txMeta)
txMeta.history.push(snapshot)
return txMeta
}

View File

@ -1,5 +1,10 @@
import { strict as assert } from 'assert'
import txStateHistoryHelper from '../../../../../app/scripts/controllers/transactions/lib/tx-state-history-helper'
import {
snapshotFromTxMeta,
migrateFromSnapshotsToDiffs,
replayHistory,
generateHistoryEntry,
} from '../../../../../app/scripts/controllers/transactions/lib/tx-state-history-helpers'
import testVault from '../../../../data/v17-long-history.json'
describe('Transaction state history helper', function () {
@ -12,7 +17,7 @@ describe('Transaction state history helper', function () {
},
},
}
const output = txStateHistoryHelper.snapshotFromTxMeta(input)
const output = snapshotFromTxMeta(input)
assert.ok('foo' in output, 'has a foo key')
assert.ok('bar' in output.foo, 'has a bar key')
assert.ok('bam' in output.foo.bar, 'has a bar key')
@ -21,7 +26,7 @@ describe('Transaction state history helper', function () {
it('should remove the history key', function () {
const input = { foo: 'bar', history: 'remembered' }
const output = txStateHistoryHelper.snapshotFromTxMeta(input)
const output = snapshotFromTxMeta(input)
assert.equal(typeof output.history, 'undefined', 'should remove history')
})
})
@ -29,7 +34,7 @@ describe('Transaction state history helper', function () {
describe('#migrateFromSnapshotsToDiffs', function () {
it('migrates history to diffs and can recover original values', function () {
testVault.data.TransactionController.transactions.forEach((tx) => {
const newHistory = txStateHistoryHelper.migrateFromSnapshotsToDiffs(tx.history)
const newHistory = 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')
@ -38,7 +43,7 @@ describe('Transaction state history helper', function () {
}
const oldEntry = tx.history[index]
const historySubset = newHistory.slice(0, index + 1)
const reconstructedValue = txStateHistoryHelper.replayHistory(historySubset)
const reconstructedValue = replayHistory(historySubset)
assert.deepEqual(oldEntry, reconstructedValue, 'was able to reconstruct old entry from diffs')
})
})
@ -61,7 +66,7 @@ describe('Transaction state history helper', function () {
const history = [initialState, diff1, diff2]
const beforeStateSnapshot = JSON.stringify(initialState)
const latestState = txStateHistoryHelper.replayHistory(history)
const latestState = replayHistory(history)
const afterStateSnapshot = JSON.stringify(initialState)
assert.notEqual(initialState, latestState, 'initial state is not the same obj as the latest state')
assert.equal(beforeStateSnapshot, afterStateSnapshot, 'initial state is not modified during run')
@ -92,7 +97,7 @@ describe('Transaction state history helper', function () {
}
const before = new Date().getTime()
const result = txStateHistoryHelper.generateHistoryEntry(prevState, nextState, note)
const result = generateHistoryEntry(prevState, nextState, note)
const after = new Date().getTime()
assert.ok(Array.isArray(result))
assert.equal(result.length, 3)

View File

@ -1,6 +1,6 @@
import { strict as assert } from 'assert'
import TxStateManager from '../../../../../app/scripts/controllers/transactions/tx-state-manager'
import txStateHistoryHelper from '../../../../../app/scripts/controllers/transactions/lib/tx-state-history-helper'
import { snapshotFromTxMeta } from '../../../../../app/scripts/controllers/transactions/lib/tx-state-history-helpers'
const noop = () => true
@ -229,7 +229,7 @@ describe('TransactionStateManager', function () {
// verify tx was initialized correctly
assert.equal(updatedTx.history.length, 1, 'one history item (initial)')
assert.equal(Array.isArray(updatedTx.history[0]), false, 'first history item is initial state')
assert.deepEqual(updatedTx.history[0], txStateHistoryHelper.snapshotFromTxMeta(updatedTx), 'first history item is initial state')
assert.deepEqual(updatedTx.history[0], snapshotFromTxMeta(updatedTx), 'first history item is initial state')
// modify value and updateTx
updatedTx.txParams.gasPrice = desiredGasPrice
const before = new Date().getTime()