1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

eth-store - convert to obs-store subclass

This commit is contained in:
kumavis 2017-02-02 22:05:06 -08:00
parent f08f40aee2
commit 73edfc9f31
3 changed files with 124 additions and 139 deletions

View File

@ -7,140 +7,122 @@
* on each new block. * on each new block.
*/ */
const EventEmitter = require('events').EventEmitter
const inherits = require('util').inherits
const async = require('async') const async = require('async')
const clone = require('clone')
const EthQuery = require('eth-query') const EthQuery = require('eth-query')
const ObservableStore = require('obs-store')
module.exports = EthereumStore function noop() {}
inherits(EthereumStore, EventEmitter) class EthereumStore extends ObservableStore {
function EthereumStore(engine) {
const self = this constructor (opts = {}) {
EventEmitter.call(self) super({
self._currentState = {
accounts: {}, accounts: {},
transactions: {}, transactions: {},
})
this._provider = opts.provider
this._query = new EthQuery(this._provider)
this._blockTracker = opts.blockTracker
// subscribe to latest block
this._blockTracker.on('block', this._updateForBlock.bind(this))
} }
self._query = new EthQuery(engine)
engine.on('block', self._updateForBlock.bind(self)) //
} // public
//
// addAccount (address) {
// public const accounts = this.getState().accounts
// accounts[address] = {}
this.updateState({ accounts })
if (!this._currentBlockNumber) return
this._updateAccount(address)
}
EthereumStore.prototype.getState = function () { removeAccount (address) {
const self = this const accounts = this.getState().accounts
return clone(self._currentState) delete accounts[address]
} this.updateState({ accounts })
}
EthereumStore.prototype.addAccount = function (address) { addTransaction (txHash) {
const self = this const transactions = this.getState().transactions
self._currentState.accounts[address] = {} transactions[txHash] = {}
self._didUpdate() this.updateState({ transactions })
if (!self.currentBlockNumber) return if (!this._currentBlockNumber) return
self._updateAccount(address, () => { this._updateTransaction(this._currentBlockNumber, txHash, noop)
self._didUpdate() }
})
}
EthereumStore.prototype.removeAccount = function (address) { removeTransaction (txHash) {
const self = this const transactions = this.getState().transactions
delete self._currentState.accounts[address] delete transactions[txHash]
self._didUpdate() this.updateState({ transactions })
} }
EthereumStore.prototype.addTransaction = function (txHash) {
const self = this
self._currentState.transactions[txHash] = {}
self._didUpdate()
if (!self.currentBlockNumber) return
self._updateTransaction(self.currentBlockNumber, txHash, noop)
}
EthereumStore.prototype.removeTransaction = function (address) {
const self = this
delete self._currentState.transactions[address]
self._didUpdate()
}
// //
// private // private
// //
EthereumStore.prototype._didUpdate = function () { _updateForBlock (block) {
const self = this const blockNumber = '0x' + block.number.toString('hex')
var state = self.getState() this._currentBlockNumber = blockNumber
self.emit('update', state)
}
EthereumStore.prototype._updateForBlock = function (block) {
const self = this
var blockNumber = '0x' + block.number.toString('hex')
self.currentBlockNumber = blockNumber
async.parallel([ async.parallel([
self._updateAccounts.bind(self), this._updateAccounts.bind(this),
self._updateTransactions.bind(self, blockNumber), this._updateTransactions.bind(this, blockNumber),
], function (err) { ], (err) => {
if (err) return console.error(err) if (err) return console.error(err)
self.emit('block', self.getState()) this.emit('block', this.getState())
self._didUpdate()
}) })
} }
EthereumStore.prototype._updateAccounts = function (cb) { _updateAccounts (cb) {
var accountsState = this._currentState.accounts const accounts = this.getState().accounts
var addresses = Object.keys(accountsState) const addresses = Object.keys(accounts)
async.each(addresses, this._updateAccount.bind(this), cb) async.each(addresses, this._updateAccount.bind(this), cb)
} }
EthereumStore.prototype._updateAccount = function (address, cb) { _updateAccount (address, cb) {
var accountsState = this._currentState.accounts const accounts = this.getState().accounts
this.getAccount(address, function (err, result) { this._getAccount(address, (err, result) => {
if (err) return cb(err) if (err) return cb(err)
result.address = address result.address = address
// only populate if the entry is still present // only populate if the entry is still present
if (accountsState[address]) { if (accounts[address]) {
accountsState[address] = result accounts[address] = result
} }
cb(null, result) cb(null, result)
}) })
} }
EthereumStore.prototype.getAccount = function (address, cb) { _updateTransactions (block, cb) {
const transactions = this.getState().transactions
const txHashes = Object.keys(transactions)
async.each(txHashes, this._updateTransaction.bind(this, block), cb)
}
_updateTransaction (block, txHash, cb) {
// would use the block here to determine how many confirmations the tx has
const transactions = this.getState().transactions
this._query.getTransaction(txHash, (err, result) => {
if (err) return cb(err)
// only populate if the entry is still present
if (transactions[txHash]) {
transactions[txHash] = result
}
cb(null, result)
})
}
_getAccount (address, cb) {
const query = this._query const query = this._query
async.parallel({ async.parallel({
balance: query.getBalance.bind(query, address), balance: query.getBalance.bind(query, address),
nonce: query.getTransactionCount.bind(query, address), nonce: query.getTransactionCount.bind(query, address),
code: query.getCode.bind(query, address), code: query.getCode.bind(query, address),
}, cb) }, cb)
}
EthereumStore.prototype._updateTransactions = function (block, cb) {
const self = this
var transactionsState = self._currentState.transactions
var txHashes = Object.keys(transactionsState)
async.each(txHashes, self._updateTransaction.bind(self, block), cb)
}
EthereumStore.prototype._updateTransaction = function (block, txHash, cb) {
const self = this
// would use the block here to determine how many confirmations the tx has
var transactionsState = self._currentState.transactions
self._query.getTransaction(txHash, function (err, result) {
if (err) return cb(err)
// only populate if the entry is still present
if (transactionsState[txHash]) {
transactionsState[txHash] = result
self._didUpdate()
} }
cb(null, result)
})
} }
function noop() {} module.exports = EthereumStore

View File

@ -55,7 +55,10 @@ module.exports = class MetamaskController extends EventEmitter {
// eth data query tools // eth data query tools
this.ethQuery = new EthQuery(this.provider) this.ethQuery = new EthQuery(this.provider)
this.ethStore = new EthStore(this.provider) this.ethStore = new EthStore({
provider: this.provider,
blockTracker: this.provider,
})
// key mgmt // key mgmt
this.keyringController = new KeyringController({ this.keyringController = new KeyringController({
@ -113,7 +116,7 @@ module.exports = class MetamaskController extends EventEmitter {
}) })
// manual mem state subscriptions // manual mem state subscriptions
this.ethStore.on('update', this.sendUpdate.bind(this)) this.ethStore.subscribe(this.sendUpdate.bind(this))
this.networkStore.subscribe(this.sendUpdate.bind(this)) this.networkStore.subscribe(this.sendUpdate.bind(this))
this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this))
this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) this.txManager.memStore.subscribe(this.sendUpdate.bind(this))

View File

@ -11,8 +11,8 @@
"disc": "gulp disc --debug", "disc": "gulp disc --debug",
"dist": "gulp dist --disableLiveReload", "dist": "gulp dist --disableLiveReload",
"test": "npm run lint && npm run fastTest && npm run ci", "test": "npm run lint && npm run fastTest && npm run ci",
"fastTest": "METAMASK_ENV=test mocha --require test/helper.js --compilers js:babel-register --recursive \"test/unit/**/*.js\"", "fastTest": "METAMASK_ENV=test mocha --require test/helper.js --recursive \"test/unit/**/*.js\"",
"watch": "mocha watch --compilers js:babel-register --recursive \"test/unit/**/*.js\"", "watch": "mocha watch --recursive \"test/unit/**/*.js\"",
"genStates": "node development/genStates.js", "genStates": "node development/genStates.js",
"ui": "npm run genStates && beefy ui-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./", "ui": "npm run genStates && beefy ui-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./",
"mock": "beefy mock-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./", "mock": "beefy mock-dev.js:bundle.js --live --open --index=./development/index.html --cwd ./",
@ -71,7 +71,7 @@
"mississippi": "^1.2.0", "mississippi": "^1.2.0",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"multiplex": "^6.7.0", "multiplex": "^6.7.0",
"obs-store": "^2.3.0", "obs-store": "^2.3.1",
"once": "^1.3.3", "once": "^1.3.3",
"ping-pong-stream": "^1.0.0", "ping-pong-stream": "^1.0.0",
"pojo-migrator": "^2.1.0", "pojo-migrator": "^2.1.0",