diff options
author | Thomas Huang <tmashuang@users.noreply.github.com> | 2019-02-13 03:42:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-13 03:42:29 +0800 |
commit | c54656efb7b6b09e6e35739c620db91e6bdb0ea7 (patch) | |
tree | b124f9ffef3883deb52c450a9a16e6506c77f0b6 /app | |
parent | ff292dcb7083053297ec99b445d6f65eb921109f (diff) | |
parent | c10fb3ce3fc7c4849c15bd5448afcd67d87f0edb (diff) | |
download | tangerine-wallet-browser-c54656efb7b6b09e6e35739c620db91e6bdb0ea7.tar tangerine-wallet-browser-c54656efb7b6b09e6e35739c620db91e6bdb0ea7.tar.gz tangerine-wallet-browser-c54656efb7b6b09e6e35739c620db91e6bdb0ea7.tar.bz2 tangerine-wallet-browser-c54656efb7b6b09e6e35739c620db91e6bdb0ea7.tar.lz tangerine-wallet-browser-c54656efb7b6b09e6e35739c620db91e6bdb0ea7.tar.xz tangerine-wallet-browser-c54656efb7b6b09e6e35739c620db91e6bdb0ea7.tar.zst tangerine-wallet-browser-c54656efb7b6b09e6e35739c620db91e6bdb0ea7.zip |
Merge pull request #6124 from MetaMask/dissable-recent-block-controller
recent-blocks - dont listen for block when on infura providers
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/controllers/network/network.js | 2 | ||||
-rw-r--r-- | app/scripts/controllers/recent-blocks.js | 31 | ||||
-rw-r--r-- | 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 1a251bce1..2958ba3b0 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 d270f6f44..982ad2aa4 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 isListening = false + const { type } = networkController.getProviderConfig() + if (!INFURA_PROVIDER_TYPES.includes(type) && type !== 'loading') { + this.blockTracker.on('latest', blockListner) + isListening = true + } + networkController.on('networkDidChange', (newType) => { + if (INFURA_PROVIDER_TYPES.includes(newType) && isListening) { + this.blockTracker.removeListener('latest', blockListner) + } else if ( + !INFURA_PROVIDER_TYPES.includes(type) && + type !== 'loading' && + !isListening + ) { + this.blockTracker.on('latest', blockListner) + + } }) this.backfill() } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 16ab34c7d..622eb78af 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. |