From 7e64a578a49edcbf4162bc61b7814885286a35e0 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 8 Feb 2019 11:24:55 -1000 Subject: [PATCH 1/2] recent-blocks - dont listen for block when on infura providers --- app/scripts/controllers/network/network.js | 2 +- app/scripts/controllers/recent-blocks.js | 31 +++++++++++++++++++--- app/scripts/metamask-controller.js | 1 + 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js index 1a251bce1931..2958ba3b0cb1 100644 --- a/app/scripts/controllers/network/network.js +++ b/app/scripts/controllers/network/network.js @@ -158,7 +158,7 @@ module.exports = class NetworkController extends EventEmitter { _switchNetwork (opts) { this.setNetworkState('loading') this._configureProvider(opts) - this.emit('networkDidChange') + this.emit('networkDidChange', opts.type) } _configureProvider (opts) { diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index d270f6f4432d..b88aaeb0c82c 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -3,6 +3,14 @@ const extend = require('xtend') const EthQuery = require('eth-query') const log = require('loglevel') const pify = require('pify') +const { + ROPSTEN, + RINKEBY, + KOVAN, + MAINNET, +} = require('./network/enums') +const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET] + class RecentBlocksController { @@ -24,7 +32,7 @@ class RecentBlocksController { * */ constructor (opts = {}) { - const { blockTracker, provider } = opts + const { blockTracker, provider, networkController } = opts this.blockTracker = blockTracker this.ethQuery = new EthQuery(provider) this.historyLength = opts.historyLength || 40 @@ -33,13 +41,30 @@ class RecentBlocksController { recentBlocks: [], }, opts.initState) this.store = new ObservableStore(initState) - - this.blockTracker.on('latest', async (newBlockNumberHex) => { + const blockListner = async (newBlockNumberHex) => { try { await this.processBlock(newBlockNumberHex) } catch (err) { log.error(err) } + } + let isListeng = false + const { type } = networkController.getProviderConfig() + if (!INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading') { + this.blockTracker.on('latest', blockListner) + isListeng = true + } + networkController.on('networkDidChange', (newType) => { + if (INFURA_PROVIDER_TYPES.includes(newType) && isListeng) { + this.blockTracker.removeListener('latest', blockListner) + } else if ( + !INFURA_PROVIDER_TYPES.includes(type) && + type !== 'loading' && + !isListeng + ) { + this.blockTracker.on('latest', blockListner) + + } }) this.backfill() } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 16ab34c7dfba..622eb78afe8a 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -126,6 +126,7 @@ module.exports = class MetamaskController extends EventEmitter { this.recentBlocksController = new RecentBlocksController({ blockTracker: this.blockTracker, provider: this.provider, + networkController: this.networkController, }) // account tracker watches balances, nonces, and any code at their address. From c10fb3ce3fc7c4849c15bd5448afcd67d87f0edb Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 12 Feb 2019 09:05:06 -1000 Subject: [PATCH 2/2] fix typo --- app/scripts/controllers/recent-blocks.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index b88aaeb0c82c..982ad2aa4dac 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -48,19 +48,19 @@ class RecentBlocksController { log.error(err) } } - let isListeng = false + let isListening = false const { type } = networkController.getProviderConfig() if (!INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading') { this.blockTracker.on('latest', blockListner) - isListeng = true + isListening = true } networkController.on('networkDidChange', (newType) => { - if (INFURA_PROVIDER_TYPES.includes(newType) && isListeng) { + if (INFURA_PROVIDER_TYPES.includes(newType) && isListening) { this.blockTracker.removeListener('latest', blockListner) } else if ( !INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading' && - !isListeng + !isListening ) { this.blockTracker.on('latest', blockListner)