1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00

controllers - account-tracker - refactor + update for eth-block-tracker@4

This commit is contained in:
kumavis 2018-05-23 16:22:40 -07:00
parent 53b946362a
commit eb2423799d

View File

@ -7,14 +7,13 @@
* on each new block. * on each new block.
*/ */
const async = require('async')
const EthQuery = require('eth-query') const EthQuery = require('eth-query')
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const EventEmitter = require('events').EventEmitter const log = require('loglevel')
function noop () {} const pify = require('pify')
class AccountTracker extends EventEmitter { class AccountTracker {
/** /**
* This module is responsible for tracking any number of accounts and caching their current balances & transaction * This module is responsible for tracking any number of accounts and caching their current balances & transaction
@ -35,8 +34,6 @@ class AccountTracker extends EventEmitter {
* *
*/ */
constructor (opts = {}) { constructor (opts = {}) {
super()
const initState = { const initState = {
accounts: {}, accounts: {},
currentBlockGasLimit: '', currentBlockGasLimit: '',
@ -44,12 +41,12 @@ class AccountTracker extends EventEmitter {
this.store = new ObservableStore(initState) this.store = new ObservableStore(initState)
this._provider = opts.provider this._provider = opts.provider
this._query = new EthQuery(this._provider) this._query = pify(new EthQuery(this._provider))
this._blockTracker = opts.blockTracker this._blockTracker = opts.blockTracker
// subscribe to latest block // subscribe to latest block
this._blockTracker.on('block', this._updateForBlock.bind(this)) this._blockTracker.on('latest', this._updateForBlock.bind(this))
// blockTracker.currentBlock may be null // blockTracker.currentBlock may be null
this._currentBlockNumber = this._blockTracker.currentBlock this._currentBlockNumber = this._blockTracker.getCurrentBlock()
} }
/** /**
@ -67,49 +64,57 @@ class AccountTracker extends EventEmitter {
const accounts = this.store.getState().accounts const accounts = this.store.getState().accounts
const locals = Object.keys(accounts) const locals = Object.keys(accounts)
const toAdd = [] const accountsToAdd = []
addresses.forEach((upstream) => { addresses.forEach((upstream) => {
if (!locals.includes(upstream)) { if (!locals.includes(upstream)) {
toAdd.push(upstream) accountsToAdd.push(upstream)
} }
}) })
const toRemove = [] const accountsToRemove = []
locals.forEach((local) => { locals.forEach((local) => {
if (!addresses.includes(local)) { if (!addresses.includes(local)) {
toRemove.push(local) accountsToRemove.push(local)
} }
}) })
toAdd.forEach(upstream => this.addAccount(upstream)) this.addAccounts(accountsToAdd)
toRemove.forEach(local => this.removeAccount(local)) this.removeAccounts(accountsToRemove)
this._updateAccounts()
} }
/** /**
* Adds a new address to this AccountTracker's accounts object, which points to an empty object. This object will be * Adds new addresses to track the balances of
* given a balance as long this._currentBlockNumber is defined. * given a balance as long this._currentBlockNumber is defined.
* *
* @param {string} address A hex address of a new account to store in this AccountTracker's accounts object * @param {array} addresses An array of hex addresses of new accounts to track
* *
*/ */
addAccount (address) { addAccounts (addresses) {
const accounts = this.store.getState().accounts const accounts = this.store.getState().accounts
// add initial state for addresses
addresses.forEach(address => {
accounts[address] = {} accounts[address] = {}
})
// save accounts state
this.store.updateState({ accounts }) this.store.updateState({ accounts })
// fetch balances for the accounts if there is block number ready
if (!this._currentBlockNumber) return if (!this._currentBlockNumber) return
this._updateAccount(address) addresses.forEach(address => this._updateAccount(address))
} }
/** /**
* Removes an account from this AccountTracker's accounts object * Removes accounts from being tracked
* *
* @param {string} address A hex address of a the account to remove * @param {array} an array of hex addresses to stop tracking
* *
*/ */
removeAccount (address) { removeAccounts (addresses) {
const accounts = this.store.getState().accounts const accounts = this.store.getState().accounts
// remove each state object
addresses.forEach(address => {
delete accounts[address] delete accounts[address]
})
// save accounts state
this.store.updateState({ accounts }) this.store.updateState({ accounts })
} }
@ -118,72 +123,56 @@ class AccountTracker extends EventEmitter {
* via EthQuery * via EthQuery
* *
* @private * @private
* @param {object} block Data about the block that contains the data to update to. * @param {number} blockNumber the block number to update to.
* @fires 'block' The updated state, if all account updates are successful * @fires 'block' The updated state, if all account updates are successful
* *
*/ */
_updateForBlock (block) { async _updateForBlock (blockNumber) {
this._currentBlockNumber = block.number this._currentBlockNumber = blockNumber
const currentBlockGasLimit = block.gasLimit
// this shouldn't be here...
const currentBlock = await this._query.getBlockByNumber(blockNumber, false)
const currentBlockGasLimit = currentBlock.gasLimit
this.store.updateState({ currentBlockGasLimit }) this.store.updateState({ currentBlockGasLimit })
async.parallel([ try {
this._updateAccounts.bind(this), await this._updateAccounts()
], (err) => { } catch (err) {
if (err) return console.error(err) log.error(err)
this.emit('block', this.store.getState()) }
})
} }
/** /**
* Calls this._updateAccount for each account in this.store * Calls this._updateAccount for each account in this.store
* *
* @param {Function} cb A callback to pass to this._updateAccount, called after each account is successfully updated * @returns {Promise} after all account balances updated
* *
*/ */
_updateAccounts (cb = noop) { async _updateAccounts () {
const accounts = this.store.getState().accounts const accounts = this.store.getState().accounts
const addresses = Object.keys(accounts) const addresses = Object.keys(accounts)
async.each(addresses, this._updateAccount.bind(this), cb) await Promise.all(addresses.map(this._updateAccount.bind(this)))
} }
/** /**
* Updates the current balance of an account. Gets an updated balance via this._getAccount. * Updates the current balance of an account.
* *
* @private * @private
* @param {string} address A hex address of a the account to be updated * @param {string} address A hex address of a the account to be updated
* @param {Function} cb A callback to call once the account at address is successfully update * @returns {Promise} after the account balance is updated
* *
*/ */
_updateAccount (address, cb = noop) { async _updateAccount (address) {
this._getAccount(address, (err, result) => { // query balance
if (err) return cb(err) const balance = await this._query.getBalance(address)
result.address = address const result = { address, balance }
const accounts = this.store.getState().accounts // update accounts state
const { accounts } = this.store.getState()
// only populate if the entry is still present // only populate if the entry is still present
if (accounts[address]) { if (!accounts[address]) return
accounts[address] = result accounts[address] = result
this.store.updateState({ accounts }) this.store.updateState({ accounts })
} }
cb(null, result)
})
}
/**
* Gets the current balance of an account via EthQuery.
*
* @private
* @param {string} address A hex address of a the account to query
* @param {Function} cb A callback to call once the account at address is successfully update
*
*/
_getAccount (address, cb = noop) {
const query = this._query
async.parallel({
balance: query.getBalance.bind(query, address),
}, cb)
}
} }