From fdffb6fedc6a29a2f952f7db669bdc2907afb854 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 14 Aug 2017 18:46:04 -0700 Subject: introduce tx-state-history-helper and diff-based history --- test/unit/tx-controller-test.js | 13 ++++++++----- test/unit/tx-state-history-helper.js | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 test/unit/tx-state-history-helper.js (limited to 'test/unit') diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 57d7deccd..c84a374ae 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -47,7 +47,7 @@ describe('Transaction Controller', function () { metamaskNetworkId: currentNetworkId, txParams, } - txController._saveTxList([txMeta]) + txController.addTx(txMeta) stub = sinon.stub(txController, 'addUnapprovedTransaction').returns(Promise.resolve(txMeta)) }) @@ -279,9 +279,12 @@ describe('Transaction Controller', function () { it('replaces the tx with the same id', function () { txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) txController.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop) - txController.updateTx({ id: '1', status: 'blah', hash: 'foo', metamaskNetworkId: currentNetworkId, txParams: {} }) - var result = txController.getTx('1') - assert.equal(result.hash, 'foo') + const tx1 = txController.getTx('1') + tx1.status = 'blah' + tx1.hash = 'foo' + txController.updateTx(tx1) + const savedResult = txController.getTx('1') + assert.equal(savedResult.hash, 'foo') }) it('updates gas price', function () { @@ -297,9 +300,9 @@ describe('Transaction Controller', function () { }, } - const updatedMeta = clone(txMeta) txController.addTx(txMeta) + const updatedMeta = txController.getTx('1') updatedMeta.txParams.gasPrice = desiredGasPrice txController.updateTx(updatedMeta) var result = txController.getTx('1') diff --git a/test/unit/tx-state-history-helper.js b/test/unit/tx-state-history-helper.js new file mode 100644 index 000000000..2ac91c39f --- /dev/null +++ b/test/unit/tx-state-history-helper.js @@ -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('history-differ', 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') + }) + }) + }) +}) -- cgit From fec0f2ca5e4c2aabe657dfe0317365b06e7f665f Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 14 Aug 2017 19:15:09 -0700 Subject: tx controller - test - test tx state history in updateTx --- test/unit/tx-controller-test.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'test/unit') diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index c84a374ae..38305f658 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -6,12 +6,15 @@ const clone = require('clone') const sinon = require('sinon') const TransactionController = require('../../app/scripts/controllers/transactions') const TxProvideUtils = require('../../app/scripts/lib/tx-utils') +const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper') + const noop = () => true const currentNetworkId = 42 const otherNetworkId = 36 const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex') const { createStubedProvider } = require('../stub/provider') + describe('Transaction Controller', function () { let txController, engine, provider, providerResultStub @@ -287,7 +290,7 @@ describe('Transaction Controller', function () { assert.equal(savedResult.hash, 'foo') }) - it('updates gas price', function () { + it('updates gas price and adds history items', function () { const originalGasPrice = '0x01' const desiredGasPrice = '0x02' @@ -300,13 +303,22 @@ describe('Transaction Controller', function () { }, } - txController.addTx(txMeta) - const updatedMeta = txController.getTx('1') - updatedMeta.txParams.gasPrice = desiredGasPrice - txController.updateTx(updatedMeta) - var result = txController.getTx('1') + const updatedTx = txController.getTx('1') + // verify tx was initialized correctly + assert.equal(result.history.length, 1, 'one history item (initial)') + assert.equal(Array.isArray(result.history[0]), false, 'first history item is initial state') + assert.deepEqual(result.history[0], txStateHistoryHelper.snapshotFromTxMeta(updatedTx), 'first history item is initial state') + // modify value and updateTx + updatedTx.txParams.gasPrice = desiredGasPrice + txController.updateTx(updatedTx) + // check updated value + const result = txController.getTx('1') assert.equal(result.txParams.gasPrice, desiredGasPrice, 'gas price updated') + // validate history was updated + assert.equal(result.history.length, 2, 'two history items (initial + diff)') + const expectedEntry = { op: 'replace', path: '/txParams/gasPrice', value: desiredGasPrice } + assert.deepEqual(result.history[1], [expectedEntry], 'two history items (initial + diff)') }) }) -- cgit From 1af797b1b3a37d8fe00afbd0d84cb3349b8fd471 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 14 Aug 2017 19:15:36 -0700 Subject: tx controller - tx state history various small fixes --- test/unit/tx-state-history-helper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit') diff --git a/test/unit/tx-state-history-helper.js b/test/unit/tx-state-history-helper.js index 2ac91c39f..5bb6c9bee 100644 --- a/test/unit/tx-state-history-helper.js +++ b/test/unit/tx-state-history-helper.js @@ -3,7 +3,7 @@ const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-hel const testVault = require('../data/v17-long-history.json') -describe('history-differ', function () { +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) -- cgit From a9e5564e8d8485741fd09efd994493fb1d8af614 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 14 Aug 2017 19:34:22 -0700 Subject: tx controller - test - fix typo from moved test --- test/unit/tx-controller-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/unit') diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 38305f658..7bb193242 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -306,9 +306,9 @@ describe('Transaction Controller', function () { txController.addTx(txMeta) const updatedTx = txController.getTx('1') // verify tx was initialized correctly - assert.equal(result.history.length, 1, 'one history item (initial)') - assert.equal(Array.isArray(result.history[0]), false, 'first history item is initial state') - assert.deepEqual(result.history[0], txStateHistoryHelper.snapshotFromTxMeta(updatedTx), 'first history item is initial state') + 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') // modify value and updateTx updatedTx.txParams.gasPrice = desiredGasPrice txController.updateTx(updatedTx) -- cgit