Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recent-blocks - dont listen for block when on infura providers #6124

Merged
merged 2 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/scripts/controllers/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 28 additions & 3 deletions app/scripts/controllers/recent-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand All @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down