mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Merge pull request #2936 from MetaMask/i2925-BetterGasEstimation
Improve gas price estimation by backfilling recent-blocks
This commit is contained in:
commit
582634d9fa
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
## Current Master
|
## Current Master
|
||||||
|
|
||||||
|
- Further improve gas price estimation.
|
||||||
|
|
||||||
## 3.13.4 2018-1-9
|
## 3.13.4 2018-1-9
|
||||||
|
|
||||||
- Remove recipient field if application initializes a tx with an empty string, or 0x, and tx data. Throw an error with the same condition, but without tx data.
|
- Remove recipient field if application initializes a tx with an empty string, or 0x, and tx data. Throw an error with the same condition, but without tx data.
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
const extend = require('xtend')
|
const extend = require('xtend')
|
||||||
|
const BN = require('ethereumjs-util').BN
|
||||||
|
const EthQuery = require('eth-query')
|
||||||
|
|
||||||
class RecentBlocksController {
|
class RecentBlocksController {
|
||||||
|
|
||||||
constructor (opts = {}) {
|
constructor (opts = {}) {
|
||||||
const { blockTracker } = opts
|
const { blockTracker, provider } = opts
|
||||||
this.blockTracker = blockTracker
|
this.blockTracker = blockTracker
|
||||||
|
this.ethQuery = new EthQuery(provider)
|
||||||
this.historyLength = opts.historyLength || 40
|
this.historyLength = opts.historyLength || 40
|
||||||
|
|
||||||
const initState = extend({
|
const initState = extend({
|
||||||
@ -14,6 +17,7 @@ class RecentBlocksController {
|
|||||||
this.store = new ObservableStore(initState)
|
this.store = new ObservableStore(initState)
|
||||||
|
|
||||||
this.blockTracker.on('block', this.processBlock.bind(this))
|
this.blockTracker.on('block', this.processBlock.bind(this))
|
||||||
|
this.backfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
resetState () {
|
resetState () {
|
||||||
@ -23,12 +27,7 @@ class RecentBlocksController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
processBlock (newBlock) {
|
processBlock (newBlock) {
|
||||||
const block = extend(newBlock, {
|
const block = this.mapTransactionsToPrices(newBlock)
|
||||||
gasPrices: newBlock.transactions.map((tx) => {
|
|
||||||
return tx.gasPrice
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
delete block.transactions
|
|
||||||
|
|
||||||
const state = this.store.getState()
|
const state = this.store.getState()
|
||||||
state.recentBlocks.push(block)
|
state.recentBlocks.push(block)
|
||||||
@ -39,6 +38,73 @@ class RecentBlocksController {
|
|||||||
|
|
||||||
this.store.updateState(state)
|
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
|
module.exports = RecentBlocksController
|
||||||
|
@ -5,7 +5,6 @@ const Dnode = require('dnode')
|
|||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
const asStream = require('obs-store/lib/asStream')
|
const asStream = require('obs-store/lib/asStream')
|
||||||
const AccountTracker = require('./lib/account-tracker')
|
const AccountTracker = require('./lib/account-tracker')
|
||||||
const EthQuery = require('eth-query')
|
|
||||||
const RpcEngine = require('json-rpc-engine')
|
const RpcEngine = require('json-rpc-engine')
|
||||||
const debounce = require('debounce')
|
const debounce = require('debounce')
|
||||||
const createEngineStream = require('json-rpc-middleware-stream/engineStream')
|
const createEngineStream = require('json-rpc-middleware-stream/engineStream')
|
||||||
@ -96,10 +95,9 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
this.recentBlocksController = new RecentBlocksController({
|
this.recentBlocksController = new RecentBlocksController({
|
||||||
blockTracker: this.blockTracker,
|
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.
|
// account tracker watches balances, nonces, and any code at their address.
|
||||||
this.accountTracker = new AccountTracker({
|
this.accountTracker = new AccountTracker({
|
||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
@ -140,7 +138,6 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
signTransaction: this.keyringController.signTransaction.bind(this.keyringController),
|
signTransaction: this.keyringController.signTransaction.bind(this.keyringController),
|
||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
blockTracker: this.blockTracker,
|
blockTracker: this.blockTracker,
|
||||||
ethQuery: this.ethQuery,
|
|
||||||
getGasPrice: this.getGasPrice.bind(this),
|
getGasPrice: this.getGasPrice.bind(this),
|
||||||
})
|
})
|
||||||
this.txController.on('newUnapprovedTx', opts.showUnapprovedTx.bind(opts))
|
this.txController.on('newUnapprovedTx', opts.showUnapprovedTx.bind(opts))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user