diff options
Merge branch 'master' into open-popup
Diffstat (limited to 'app/scripts/controllers')
-rw-r--r-- | app/scripts/controllers/network.js | 7 | ||||
-rw-r--r-- | app/scripts/controllers/recent-blocks.js | 80 |
2 files changed, 78 insertions, 9 deletions
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 377ba6eca..a1db8946a 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -1,6 +1,7 @@ const assert = require('assert') const EventEmitter = require('events') const createMetamaskProvider = require('web3-provider-engine/zero.js') +const SubproviderFromProvider = require('web3-provider-engine/subproviders/web3.js') const createInfuraProvider = require('eth-json-rpc-infura/src/createProvider') const ObservableStore = require('obs-store') const ComposedStore = require('obs-store/lib/composed') @@ -133,15 +134,17 @@ module.exports = class NetworkController extends EventEmitter { _configureInfuraProvider (opts) { log.info('_configureInfuraProvider', opts) - const blockTrackerProvider = createInfuraProvider({ + const infuraProvider = createInfuraProvider({ network: opts.type, }) + const infuraSubprovider = new SubproviderFromProvider(infuraProvider) const providerParams = extend(this._baseProviderParams, { rpcUrl: opts.rpcUrl, engineParams: { pollingInterval: 8000, - blockTrackerProvider, + blockTrackerProvider: infuraProvider, }, + dataSubprovider: infuraSubprovider, }) const provider = createMetamaskProvider(providerParams) this._setProvider(provider) diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index 4a906261e..4ae3810eb 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -1,11 +1,14 @@ const ObservableStore = require('obs-store') const extend = require('xtend') +const BN = require('ethereumjs-util').BN +const EthQuery = require('eth-query') class RecentBlocksController { constructor (opts = {}) { - const { blockTracker } = opts + const { blockTracker, provider } = opts this.blockTracker = blockTracker + this.ethQuery = new EthQuery(provider) this.historyLength = opts.historyLength || 40 const initState = extend({ @@ -14,6 +17,7 @@ class RecentBlocksController { this.store = new ObservableStore(initState) this.blockTracker.on('block', this.processBlock.bind(this)) + this.backfill() } resetState () { @@ -23,12 +27,7 @@ class RecentBlocksController { } processBlock (newBlock) { - const block = extend(newBlock, { - gasPrices: newBlock.transactions.map((tx) => { - return tx.gasPrice - }), - }) - delete block.transactions + const block = this.mapTransactionsToPrices(newBlock) const state = this.store.getState() state.recentBlocks.push(block) @@ -39,6 +38,73 @@ class RecentBlocksController { this.store.updateState(state) } + + backfillBlock (newBlock) { + const block = this.mapTransactionsToPrices(newBlock) + + const state = this.store.getState() + + if (state.recentBlocks.length < this.historyLength) { + state.recentBlocks.unshift(block) + } + + this.store.updateState(state) + } + + mapTransactionsToPrices (newBlock) { + const block = extend(newBlock, { + gasPrices: newBlock.transactions.map((tx) => { + return tx.gasPrice + }), + }) + delete block.transactions + return block + } + + async backfill() { + this.blockTracker.once('block', async (block) => { + let blockNum = block.number + let recentBlocks + let state = this.store.getState() + recentBlocks = state.recentBlocks + + while (recentBlocks.length < this.historyLength) { + try { + let blockNumBn = new BN(blockNum.substr(2), 16) + const newNum = blockNumBn.subn(1).toString(10) + const newBlock = await this.getBlockByNumber(newNum) + + if (newBlock) { + this.backfillBlock(newBlock) + blockNum = newBlock.number + } + + state = this.store.getState() + recentBlocks = state.recentBlocks + } catch (e) { + log.error(e) + } + await this.wait() + } + }) + } + + async wait () { + return new Promise((resolve) => { + setTimeout(resolve, 100) + }) + } + + async getBlockByNumber (number) { + const bn = new BN(number) + return new Promise((resolve, reject) => { + this.ethQuery.getBlockByNumber('0x' + bn.toString(16), true, (err, block) => { + if (err) reject(err) + resolve(block) + }) + }) + } + } module.exports = RecentBlocksController |