From 929bd56228b701f8cd63a34b0a75c3194af7388f Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Wed, 28 Nov 2018 11:29:11 -0330 Subject: [PATCH] Cache most recent account balances in the account-tracker --- app/scripts/lib/account-tracker.js | 20 +++++++++++--- app/scripts/migrations/030.js | 42 ++++++++++++++++++++++++++++++ app/scripts/migrations/index.js | 1 + 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 app/scripts/migrations/030.js diff --git a/app/scripts/lib/account-tracker.js b/app/scripts/lib/account-tracker.js index 2e9340018fbe..e071f9f59a8e 100644 --- a/app/scripts/lib/account-tracker.js +++ b/app/scripts/lib/account-tracker.js @@ -36,6 +36,7 @@ class AccountTracker { constructor (opts = {}) { const initState = { accounts: {}, + cachedAccountBalances: {}, currentBlockGasLimit: '', } this.store = new ObservableStore(initState) @@ -184,12 +185,25 @@ class AccountTracker { // query balance const balance = await this._query.getBalance(address) const result = { address, balance } - // update accounts state - const { accounts } = this.store.getState() + // update accounts and cachedAccountBalances state + const { accounts, cachedAccountBalances } = this.store.getState() // only populate if the entry is still present if (!accounts[address]) return accounts[address] = result - this.store.updateState({ accounts }) + this.store.updateState({ + accounts, + cachedAccountBalances: this._generateAccountBalancesToCache(result, cachedAccountBalances), + }) + } + + _generateAccountBalancesToCache ({ address, balance }, cachedAccountBalances) { + const accountBalancesToCache = { ...cachedAccountBalances } + + if (balance !== null && balance !== undefined) { + accountBalancesToCache[address] = balance + } + + return accountBalancesToCache } } diff --git a/app/scripts/migrations/030.js b/app/scripts/migrations/030.js new file mode 100644 index 000000000000..cf0defe85fe9 --- /dev/null +++ b/app/scripts/migrations/030.js @@ -0,0 +1,42 @@ +// next version number +const version = 30 + +/* + +Adds cachedAccountBalances to the account tracker. + +*/ + +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) { + if (!state.AccountTracker) { + return state + } + + const cachedAccountBalances = {} + const accounts = state.AccountTracker.accounts + + let balance + for (const accountId in state.AccountTracker.accounts) { + balance = accounts[accountId].balance + if (balance !== null && balance !== undefined) { + cachedAccountBalances[accountId] = balance + } + } + state.AccountTracker.cachedAccountBalances = cachedAccountBalances + return state +} diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index 9344b77ed3b0..99cca94b8ab0 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -40,4 +40,5 @@ module.exports = [ require('./027'), require('./028'), require('./029'), + require('./030'), ]