From 63ae7fb589b43d2af87f31d51f5b5f96669dabe0 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 8 Jan 2018 13:19:18 -0800 Subject: bugfix - transactions controller - breakout logic and fix order of operations bug --- app/scripts/controllers/transactions.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'app/scripts/controllers') diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js index 469deb670..ded9739a8 100644 --- a/app/scripts/controllers/transactions.js +++ b/app/scripts/controllers/transactions.js @@ -180,8 +180,10 @@ module.exports = class TransactionController extends EventEmitter { // ensure value txMeta.gasPriceSpecified = Boolean(txParams.gasPrice) txMeta.nonceSpecified = Boolean(txParams.nonce) - const gasPrice = txParams.gasPrice || this.getGasPrice ? this.getGasPrice() - : await this.query.gasPrice() + let gasPrice = txParams.gasPrice + if (!gasPrice) { + gasPrice = this.getGasPrice ? this.getGasPrice() : await this.query.gasPrice() + } txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16)) txParams.value = txParams.value || '0x0' // set gasLimit -- cgit v1.2.3 From ab7701efb35f39e73fcccc2110a754384e23d695 Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 8 Jan 2018 14:57:57 -0800 Subject: provider - infura - use infura REST api --- app/scripts/controllers/network.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'app/scripts/controllers') 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) -- cgit v1.2.3 From 4a9dad7c40b97f1e625931d6b57fc9d7fdc5080d Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Thu, 11 Jan 2018 15:00:48 -0800 Subject: Improve gas price estimation by backfilling recent-blocks When first initializing, recent-block controller now back-fills up to its desired history length. This makes estimated gas prices reflect a longer recent history, even when first switching to a new network. Fixes #2925 --- app/scripts/controllers/recent-blocks.js | 79 +++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 7 deletions(-) (limited to 'app/scripts/controllers') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index 4a906261e..c65c2b1c4 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -1,11 +1,13 @@ const ObservableStore = require('obs-store') const extend = require('xtend') +const BN = require('ethereumjs-util').BN class RecentBlocksController { constructor (opts = {}) { - const { blockTracker } = opts + const { blockTracker, ethQuery } = opts this.blockTracker = blockTracker + this.ethQuery = ethQuery this.historyLength = opts.historyLength || 40 const initState = extend({ @@ -14,6 +16,7 @@ class RecentBlocksController { this.store = new ObservableStore(initState) this.blockTracker.on('block', this.processBlock.bind(this)) + this.backfill() } resetState () { @@ -23,12 +26,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 +37,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 -- cgit v1.2.3 From 7cb66ce4cba446f8149c4a8872dbdfbc53f72f7c Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 12 Jan 2018 10:25:36 -0800 Subject: Prefer passing a provider over an ethQuery instance --- app/scripts/controllers/recent-blocks.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app/scripts/controllers') diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index c65c2b1c4..4ae3810eb 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -1,13 +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, ethQuery } = opts + const { blockTracker, provider } = opts this.blockTracker = blockTracker - this.ethQuery = ethQuery + this.ethQuery = new EthQuery(provider) this.historyLength = opts.historyLength || 40 const initState = extend({ -- cgit v1.2.3