From 73edfc9f31b1cbd44ae8b5372e7bef5d1d5959ad Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 22:05:06 -0800 Subject: eth-store - convert to obs-store subclass --- app/scripts/lib/eth-store.js | 214 ++++++++++++++++++++----------------------- 1 file changed, 98 insertions(+), 116 deletions(-) (limited to 'app/scripts/lib/eth-store.js') diff --git a/app/scripts/lib/eth-store.js b/app/scripts/lib/eth-store.js index 7e2caf884..64a3c6b59 100644 --- a/app/scripts/lib/eth-store.js +++ b/app/scripts/lib/eth-store.js @@ -7,140 +7,122 @@ * on each new block. */ -const EventEmitter = require('events').EventEmitter -const inherits = require('util').inherits const async = require('async') -const clone = require('clone') const EthQuery = require('eth-query') +const ObservableStore = require('obs-store') +function noop() {} -module.exports = EthereumStore +class EthereumStore extends ObservableStore { -inherits(EthereumStore, EventEmitter) -function EthereumStore(engine) { - const self = this - EventEmitter.call(self) - self._currentState = { - accounts: {}, - transactions: {}, + constructor (opts = {}) { + super({ + accounts: {}, + 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 -// -EthereumStore.prototype.getState = function () { - const self = this - return clone(self._currentState) -} - -EthereumStore.prototype.addAccount = function (address) { - const self = this - self._currentState.accounts[address] = {} - self._didUpdate() - if (!self.currentBlockNumber) return - self._updateAccount(address, () => { - self._didUpdate() - }) -} + // + // public + // -EthereumStore.prototype.removeAccount = function (address) { - const self = this - delete self._currentState.accounts[address] - self._didUpdate() -} + addAccount (address) { + const accounts = this.getState().accounts + accounts[address] = {} + this.updateState({ accounts }) + if (!this._currentBlockNumber) return + this._updateAccount(address) + } -EthereumStore.prototype.addTransaction = function (txHash) { - const self = this - self._currentState.transactions[txHash] = {} - self._didUpdate() - if (!self.currentBlockNumber) return - self._updateTransaction(self.currentBlockNumber, txHash, noop) -} + removeAccount (address) { + const accounts = this.getState().accounts + delete accounts[address] + this.updateState({ accounts }) + } -EthereumStore.prototype.removeTransaction = function (address) { - const self = this - delete self._currentState.transactions[address] - self._didUpdate() -} + addTransaction (txHash) { + const transactions = this.getState().transactions + transactions[txHash] = {} + this.updateState({ transactions }) + if (!this._currentBlockNumber) return + this._updateTransaction(this._currentBlockNumber, txHash, noop) + } + removeTransaction (txHash) { + const transactions = this.getState().transactions + delete transactions[txHash] + this.updateState({ transactions }) + } -// -// private -// -EthereumStore.prototype._didUpdate = function () { - const self = this - var state = self.getState() - self.emit('update', state) -} + // + // private + // + + _updateForBlock (block) { + const blockNumber = '0x' + block.number.toString('hex') + this._currentBlockNumber = blockNumber + async.parallel([ + this._updateAccounts.bind(this), + this._updateTransactions.bind(this, blockNumber), + ], (err) => { + if (err) return console.error(err) + this.emit('block', this.getState()) + }) + } -EthereumStore.prototype._updateForBlock = function (block) { - const self = this - var blockNumber = '0x' + block.number.toString('hex') - self.currentBlockNumber = blockNumber - async.parallel([ - self._updateAccounts.bind(self), - self._updateTransactions.bind(self, blockNumber), - ], function (err) { - if (err) return console.error(err) - self.emit('block', self.getState()) - self._didUpdate() - }) -} + _updateAccounts (cb) { + const accounts = this.getState().accounts + const addresses = Object.keys(accounts) + async.each(addresses, this._updateAccount.bind(this), cb) + } -EthereumStore.prototype._updateAccounts = function (cb) { - var accountsState = this._currentState.accounts - var addresses = Object.keys(accountsState) - async.each(addresses, this._updateAccount.bind(this), cb) -} + _updateAccount (address, cb) { + const accounts = this.getState().accounts + this._getAccount(address, (err, result) => { + if (err) return cb(err) + result.address = address + // only populate if the entry is still present + if (accounts[address]) { + accounts[address] = result + } + cb(null, result) + }) + } -EthereumStore.prototype._updateAccount = function (address, cb) { - var accountsState = this._currentState.accounts - this.getAccount(address, function (err, result) { - if (err) return cb(err) - result.address = address - // only populate if the entry is still present - if (accountsState[address]) { - accountsState[address] = result - } - cb(null, result) - }) -} + _updateTransactions (block, cb) { + const transactions = this.getState().transactions + const txHashes = Object.keys(transactions) + async.each(txHashes, this._updateTransaction.bind(this, block), cb) + } -EthereumStore.prototype.getAccount = function (address, cb) { - const query = this._query - async.parallel({ - balance: query.getBalance.bind(query, address), - nonce: query.getTransactionCount.bind(query, address), - code: query.getCode.bind(query, address), - }, 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) + }) + } -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) -} + _getAccount (address, cb) { + const query = this._query + async.parallel({ + balance: query.getBalance.bind(query, address), + nonce: query.getTransactionCount.bind(query, address), + code: query.getCode.bind(query, address), + }, 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 \ No newline at end of file -- cgit v1.2.3 From b233e7e37c56852080bdc7b1843eb68ba96b5382 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 2 Feb 2017 22:32:00 -0800 Subject: eth-store - cbs default to noop --- app/scripts/lib/eth-store.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'app/scripts/lib/eth-store.js') diff --git a/app/scripts/lib/eth-store.js b/app/scripts/lib/eth-store.js index 64a3c6b59..96b4a60f2 100644 --- a/app/scripts/lib/eth-store.js +++ b/app/scripts/lib/eth-store.js @@ -76,13 +76,13 @@ class EthereumStore extends ObservableStore { }) } - _updateAccounts (cb) { + _updateAccounts (cb = noop) { const accounts = this.getState().accounts const addresses = Object.keys(accounts) async.each(addresses, this._updateAccount.bind(this), cb) } - _updateAccount (address, cb) { + _updateAccount (address, cb = noop) { const accounts = this.getState().accounts this._getAccount(address, (err, result) => { if (err) return cb(err) @@ -95,13 +95,13 @@ class EthereumStore extends ObservableStore { }) } - _updateTransactions (block, cb) { + _updateTransactions (block, cb = noop) { const transactions = this.getState().transactions const txHashes = Object.keys(transactions) async.each(txHashes, this._updateTransaction.bind(this, block), cb) } - _updateTransaction (block, txHash, cb) { + _updateTransaction (block, txHash, cb = noop) { // would use the block here to determine how many confirmations the tx has const transactions = this.getState().transactions this._query.getTransaction(txHash, (err, result) => { @@ -114,7 +114,7 @@ class EthereumStore extends ObservableStore { }) } - _getAccount (address, cb) { + _getAccount (address, cb = noop) { const query = this._query async.parallel({ balance: query.getBalance.bind(query, address), -- cgit v1.2.3 From 99d6a329a2e8358b85ff8a82c7f17e2ebf71a399 Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 3 Feb 2017 12:35:01 -0800 Subject: eths-store - eagerly set current block --- app/scripts/lib/eth-store.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'app/scripts/lib/eth-store.js') diff --git a/app/scripts/lib/eth-store.js b/app/scripts/lib/eth-store.js index 96b4a60f2..773c81d1b 100644 --- a/app/scripts/lib/eth-store.js +++ b/app/scripts/lib/eth-store.js @@ -25,6 +25,8 @@ class EthereumStore extends ObservableStore { this._blockTracker = opts.blockTracker // subscribe to latest block this._blockTracker.on('block', this._updateForBlock.bind(this)) + // blockTracker.currentBlock may be null + this._currentBlockNumber = this._blockTracker.currentBlock } // -- cgit v1.2.3 From bc4efa18072997fea7f20051171f11a36cf006c8 Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 3 Feb 2017 16:07:58 -0800 Subject: eth-store - update store state after manipulating --- app/scripts/lib/eth-store.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'app/scripts/lib/eth-store.js') diff --git a/app/scripts/lib/eth-store.js b/app/scripts/lib/eth-store.js index 773c81d1b..8812a507b 100644 --- a/app/scripts/lib/eth-store.js +++ b/app/scripts/lib/eth-store.js @@ -92,6 +92,7 @@ class EthereumStore extends ObservableStore { // only populate if the entry is still present if (accounts[address]) { accounts[address] = result + this.updateState({ accounts }) } cb(null, result) }) @@ -111,6 +112,7 @@ class EthereumStore extends ObservableStore { // only populate if the entry is still present if (transactions[txHash]) { transactions[txHash] = result + this.updateState({ transactions }) } cb(null, result) }) -- cgit v1.2.3