diff options
author | Thomas Huang <tmashuang@users.noreply.github.com> | 2017-12-21 02:07:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-21 02:07:34 +0800 |
commit | d3f5ad874e4bf90cda4e440f9b0635c6ac416382 (patch) | |
tree | e9375bffd3fc868af4dce48b49732254bb2b59a0 | |
parent | 80b8098a27ed37e8644ef325d73e173cef033b96 (diff) | |
parent | 30b45c8a385e9ae4c6b78d0a7ec6929f7bad4d9a (diff) | |
download | tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar.gz tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar.bz2 tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar.lz tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar.xz tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.tar.zst tangerine-wallet-browser-d3f5ad874e4bf90cda4e440f9b0635c6ac416382.zip |
Merge pull request #2763 from MetaMask/AddRecentBlocksController
Add Recent Blocks controller
-rw-r--r-- | app/scripts/controllers/recent-blocks.js | 44 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 39 |
2 files changed, 69 insertions, 14 deletions
diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js new file mode 100644 index 000000000..4a906261e --- /dev/null +++ b/app/scripts/controllers/recent-blocks.js @@ -0,0 +1,44 @@ +const ObservableStore = require('obs-store') +const extend = require('xtend') + +class RecentBlocksController { + + constructor (opts = {}) { + const { blockTracker } = opts + this.blockTracker = blockTracker + this.historyLength = opts.historyLength || 40 + + const initState = extend({ + recentBlocks: [], + }, opts.initState) + this.store = new ObservableStore(initState) + + this.blockTracker.on('block', this.processBlock.bind(this)) + } + + resetState () { + this.store.updateState({ + recentBlocks: [], + }) + } + + processBlock (newBlock) { + const block = extend(newBlock, { + gasPrices: newBlock.transactions.map((tx) => { + return tx.gasPrice + }), + }) + delete block.transactions + + const state = this.store.getState() + state.recentBlocks.push(block) + + while (state.recentBlocks.length > this.historyLength) { + state.recentBlocks.shift() + } + + this.store.updateState(state) + } +} + +module.exports = RecentBlocksController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 9d126b416..23f2a1598 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -23,6 +23,7 @@ const ShapeShiftController = require('./controllers/shapeshift') const AddressBookController = require('./controllers/address-book') const InfuraController = require('./controllers/infura') const BlacklistController = require('./controllers/blacklist') +const RecentBlocksController = require('./controllers/recent-blocks') const MessageManager = require('./lib/message-manager') const PersonalMessageManager = require('./lib/personal-message-manager') const TypedMessageManager = require('./lib/typed-message-manager') @@ -91,6 +92,10 @@ module.exports = class MetamaskController extends EventEmitter { this.provider = this.initializeProvider() this.blockTracker = this.provider._blockTracker + this.recentBlocksController = new RecentBlocksController({ + blockTracker: this.blockTracker, + }) + // eth data query tools this.ethQuery = new EthQuery(this.provider) // account tracker watches balances, nonces, and any code at their address. @@ -196,25 +201,30 @@ module.exports = class MetamaskController extends EventEmitter { this.blacklistController.store.subscribe((state) => { this.store.updateState({ BlacklistController: state }) }) + this.recentBlocksController.store.subscribe((state) => { + this.store.updateState({ RecentBlocks: state }) + }) this.infuraController.store.subscribe((state) => { this.store.updateState({ InfuraController: state }) }) // manual mem state subscriptions - this.networkController.store.subscribe(this.sendUpdate.bind(this)) - this.accountTracker.store.subscribe(this.sendUpdate.bind(this)) - this.txController.memStore.subscribe(this.sendUpdate.bind(this)) - this.balancesController.store.subscribe(this.sendUpdate.bind(this)) - this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) - this.personalMessageManager.memStore.subscribe(this.sendUpdate.bind(this)) - this.typedMessageManager.memStore.subscribe(this.sendUpdate.bind(this)) - this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) - this.preferencesController.store.subscribe(this.sendUpdate.bind(this)) - this.addressBookController.store.subscribe(this.sendUpdate.bind(this)) - this.currencyController.store.subscribe(this.sendUpdate.bind(this)) - this.noticeController.memStore.subscribe(this.sendUpdate.bind(this)) - this.shapeshiftController.store.subscribe(this.sendUpdate.bind(this)) - this.infuraController.store.subscribe(this.sendUpdate.bind(this)) + const sendUpdate = this.sendUpdate.bind(this) + this.networkController.store.subscribe(sendUpdate) + this.accountTracker.store.subscribe(sendUpdate) + this.txController.memStore.subscribe(sendUpdate) + this.balancesController.store.subscribe(sendUpdate) + this.messageManager.memStore.subscribe(sendUpdate) + this.personalMessageManager.memStore.subscribe(sendUpdate) + this.typedMessageManager.memStore.subscribe(sendUpdate) + this.keyringController.memStore.subscribe(sendUpdate) + this.preferencesController.store.subscribe(sendUpdate) + this.recentBlocksController.store.subscribe(sendUpdate) + this.addressBookController.store.subscribe(sendUpdate) + this.currencyController.store.subscribe(sendUpdate) + this.noticeController.memStore.subscribe(sendUpdate) + this.shapeshiftController.store.subscribe(sendUpdate) + this.infuraController.store.subscribe(sendUpdate) } // @@ -298,6 +308,7 @@ module.exports = class MetamaskController extends EventEmitter { this.currencyController.store.getState(), this.noticeController.memStore.getState(), this.infuraController.store.getState(), + this.recentBlocksController.store.getState(), // config manager this.configManager.getConfig(), this.shapeshiftController.store.getState(), |