diff options
author | Frankie <frankie.diamond@gmail.com> | 2018-01-16 06:47:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-16 06:47:48 +0800 |
commit | 9d7c635b0aa768219aaabe6f5bb839ac6d243969 (patch) | |
tree | a0f098b7354e464326c55b0c226158a1b3fefe06 | |
parent | 5fb2cfed6a4c91c0f6dadb0022987d7b56f0bf91 (diff) | |
parent | 582634d9fa6a31b695ded5125b26c8a4f06ed21f (diff) | |
download | tangerine-wallet-browser-9d7c635b0aa768219aaabe6f5bb839ac6d243969.tar tangerine-wallet-browser-9d7c635b0aa768219aaabe6f5bb839ac6d243969.tar.gz tangerine-wallet-browser-9d7c635b0aa768219aaabe6f5bb839ac6d243969.tar.bz2 tangerine-wallet-browser-9d7c635b0aa768219aaabe6f5bb839ac6d243969.tar.lz tangerine-wallet-browser-9d7c635b0aa768219aaabe6f5bb839ac6d243969.tar.xz tangerine-wallet-browser-9d7c635b0aa768219aaabe6f5bb839ac6d243969.tar.zst tangerine-wallet-browser-9d7c635b0aa768219aaabe6f5bb839ac6d243969.zip |
Merge branch 'master' into i2826-SignBytes
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | app/scripts/controllers/recent-blocks.js | 80 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 5 |
3 files changed, 75 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e90134613..ef9edf42a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Current Master - Fix bug that prevented eth_signTypedData from signing bytes. +- Further improve gas price estimation. ## 3.13.4 2018-1-9 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 diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index f62b5e5cd..000e17b9e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -5,7 +5,6 @@ const Dnode = require('dnode') const ObservableStore = require('obs-store') const asStream = require('obs-store/lib/asStream') const AccountTracker = require('./lib/account-tracker') -const EthQuery = require('eth-query') const RpcEngine = require('json-rpc-engine') const debounce = require('debounce') const createEngineStream = require('json-rpc-middleware-stream/engineStream') @@ -96,10 +95,9 @@ module.exports = class MetamaskController extends EventEmitter { this.recentBlocksController = new RecentBlocksController({ blockTracker: this.blockTracker, + provider: this.provider, }) - // eth data query tools - this.ethQuery = new EthQuery(this.provider) // account tracker watches balances, nonces, and any code at their address. this.accountTracker = new AccountTracker({ provider: this.provider, @@ -140,7 +138,6 @@ module.exports = class MetamaskController extends EventEmitter { signTransaction: this.keyringController.signTransaction.bind(this.keyringController), provider: this.provider, blockTracker: this.blockTracker, - ethQuery: this.ethQuery, getGasPrice: this.getGasPrice.bind(this), }) this.txController.on('newUnapprovedTx', opts.showUnapprovedTx.bind(opts)) |