Skip to content

Commit

Permalink
Delete recent blocks controller (#8575)
Browse files Browse the repository at this point in the history
* delete recent blocks controller

* delete percentile from direct dependencies
  • Loading branch information
rekmarks authored May 12, 2020
1 parent 0aa41e3 commit 0470386
Show file tree
Hide file tree
Showing 18 changed files with 11 additions and 393 deletions.
1 change: 0 additions & 1 deletion app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ initialize().catch(log.error)
* @property {number} conversionRate - A number representing the current exchange rate from the user's preferred currency to Ether.
* @property {number} conversionDate - A unix epoch date (ms) for the time the current conversion rate was last retrieved.
* @property {Object} infuraNetworkStatus - An object of infura network status checks.
* @property {Block[]} recentBlocks - An array of recent blocks, used to calculate an effective but cheap gas price.
* @property {boolean} forgottenPassword - Returns true if the user has initiated the password recovery screen, is recovering from seed phrase.
*/

Expand Down
175 changes: 0 additions & 175 deletions app/scripts/controllers/recent-blocks.js

This file was deleted.

17 changes: 6 additions & 11 deletions app/scripts/controllers/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const SIMPLE_GAS_COST = '0x5208' // Hex for 21000, cost of a simple send.
@param {Object} opts.provider - A network provider.
@param {Function} opts.signTransaction - function the signs an ethereumjs-tx
@param {Object} opts.getPermittedAccounts - get accounts that an origin has permissions for
@param {Function} [opts.getGasPrice] - optional gas price calculator
@param {Function} opts.signTransaction - ethTx signer that returns a rawTx
@param {number} [opts.txHistoryLimit] - number *optional* for limiting how many transactions are in state
@param {Object} opts.preferencesStore
Expand All @@ -77,7 +76,6 @@ export default class TransactionController extends EventEmitter {
this.getPermittedAccounts = opts.getPermittedAccounts
this.blockTracker = opts.blockTracker
this.signEthTx = opts.signTransaction
this.getGasPrice = opts.getGasPrice
this.inProcessOfSigning = new Set()

this.memStore = new ObservableStore({})
Expand Down Expand Up @@ -291,9 +289,7 @@ export default class TransactionController extends EventEmitter {
if (txMeta.txParams.gasPrice) {
return
}
const gasPrice = this.getGasPrice
? this.getGasPrice()
: await this.query.gasPrice()
const gasPrice = await this.query.gasPrice()

return ethUtil.addHexPrefix(gasPrice.toString(16))
}
Expand Down Expand Up @@ -347,13 +343,12 @@ export default class TransactionController extends EventEmitter {
const originalTxMeta = this.txStateManager.getTx(originalTxId)
const { txParams } = originalTxMeta
const lastGasPrice = gasPrice || originalTxMeta.txParams.gasPrice
const suggestedGasPriceBN = new ethUtil.BN(ethUtil.stripHexPrefix(this.getGasPrice()), 16)
const lastGasPriceBN = new ethUtil.BN(ethUtil.stripHexPrefix(lastGasPrice), 16)
// essentially lastGasPrice * 1.1 but
// dont trust decimals so a round about way of doing that
const lastGasPriceBNBumped = lastGasPriceBN.mul(new ethUtil.BN(110, 10)).div(new ethUtil.BN(100, 10))
// transactions that are being retried require a >=%10 bump or the clients will throw an error
txParams.gasPrice = suggestedGasPriceBN.gt(lastGasPriceBNBumped) ? `0x${suggestedGasPriceBN.toString(16)}` : `0x${lastGasPriceBNBumped.toString(16)}`
// essentially lastGasPrice * 1.1
const lastGasPriceBNBumped = lastGasPriceBN
.mul(new ethUtil.BN(110, 10))
.div(new ethUtil.BN(100, 10))
txParams.gasPrice = `0x${lastGasPriceBNBumped.toString(16)}`

const txMeta = this.txStateManager.generateTxMeta({
txParams: originalTxMeta.txParams,
Expand Down
48 changes: 1 addition & 47 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import CachedBalancesController from './controllers/cached-balances'
import AlertController from './controllers/alert'
import OnboardingController from './controllers/onboarding'
import ThreeBoxController from './controllers/threebox'
import RecentBlocksController from './controllers/recent-blocks'
import IncomingTransactionsController from './controllers/incoming-transactions'
import MessageManager from './lib/message-manager'
import DecryptMessageManager from './lib/decrypt-message-manager'
Expand All @@ -53,10 +52,8 @@ import getBuyEthUrl from './lib/buy-eth-url'
import selectChainId from './lib/select-chain-id'
import { Mutex } from 'await-semaphore'
import { version } from '../manifest/_base.json'
import ethUtil, { BN } from 'ethereumjs-util'
import ethUtil from 'ethereumjs-util'

const GWEI_BN = new BN('1000000000')
import percentile from 'percentile'
import seedPhraseVerifier from './lib/seed-phrase-verifier'
import log from 'loglevel'
import TrezorKeyring from 'eth-trezor-keyring'
Expand Down Expand Up @@ -150,12 +147,6 @@ export default class MetamaskController extends EventEmitter {
preferences: this.preferencesController.store,
})

this.recentBlocksController = new RecentBlocksController({
blockTracker: this.blockTracker,
provider: this.provider,
networkController: this.networkController,
})

this.ensController = new EnsController({
provider: this.provider,
networkStore: this.networkController.networkStore,
Expand Down Expand Up @@ -258,7 +249,6 @@ export default class MetamaskController extends EventEmitter {
signTransaction: this.keyringController.signTransaction.bind(this.keyringController),
provider: this.provider,
blockTracker: this.blockTracker,
getGasPrice: this.getGasPrice.bind(this),
})
this.txController.on('newUnapprovedTx', () => opts.showUnapprovedTx())

Expand Down Expand Up @@ -334,7 +324,6 @@ export default class MetamaskController extends EventEmitter {
TypesMessageManager: this.typedMessageManager.memStore,
KeyringController: this.keyringController.memStore,
PreferencesController: this.preferencesController.store,
RecentBlocksController: this.recentBlocksController.store,
AddressBookController: this.addressBookController,
CurrencyController: this.currencyRateController,
InfuraController: this.infuraController.store,
Expand Down Expand Up @@ -469,7 +458,6 @@ export default class MetamaskController extends EventEmitter {
setCurrentLocale: this.setCurrentLocale.bind(this),
markPasswordForgotten: this.markPasswordForgotten.bind(this),
unMarkPasswordForgotten: this.unMarkPasswordForgotten.bind(this),
getGasPrice: (cb) => cb(null, this.getGasPrice()),
buyEth: this.buyEth.bind(this),

// primary HD keyring management
Expand Down Expand Up @@ -1804,40 +1792,6 @@ export default class MetamaskController extends EventEmitter {
// MISCELLANEOUS
//=============================================================================

/**
* A method for estimating a good gas price at recent prices.
* Returns the lowest price that would have been included in
* 50% of recent blocks.
*
* @returns {string} - A hex representation of the suggested wei gas price.
*/
getGasPrice () {
const { recentBlocksController } = this
const { recentBlocks } = recentBlocksController.store.getState()

// Return 1 gwei if no blocks have been observed:
if (recentBlocks.length === 0) {
return '0x' + GWEI_BN.toString(16)
}

const lowestPrices = recentBlocks.map((block) => {
if (!block.gasPrices || block.gasPrices.length < 1) {
return GWEI_BN
}
return block.gasPrices
.map((hexPrefix) => hexPrefix.substr(2))
.map((hex) => new BN(hex, 16))
.sort((a, b) => {
return a.gt(b) ? 1 : -1
})[0]
})
.map((number) => number.div(GWEI_BN).toNumber())

const percentileNum = percentile(65, lowestPrices)
const percentileNumBn = new BN(percentileNum)
return '0x' + percentileNumBn.mul(GWEI_BN).toString(16)
}

/**
* Returns the nonce that will be associated with a transaction once approved
* @param {string} address - The hex string address for the transaction
Expand Down
1 change: 0 additions & 1 deletion app/scripts/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ async function start () {
: {}
// remove unnecessary data
delete state.localeMessages
delete state.metamask.recentBlocks
// return state to be added to request
return state
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
"nonce-tracker": "^1.0.0",
"obj-multiplex": "^1.0.0",
"obs-store": "^4.0.3",
"percentile": "^1.2.0",
"pify": "^5.0.0",
"post-message-stream": "^3.0.0",
"promise-to-callback": "^1.0.0",
Expand Down
26 changes: 0 additions & 26 deletions test/unit/app/controllers/metamask-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,32 +176,6 @@ describe('MetaMaskController', function () {
})
})

describe('#getGasPrice', function () {

it('gives the 50th percentile lowest accepted gas price from recentBlocksController', async function () {
const realRecentBlocksController = metamaskController.recentBlocksController
metamaskController.recentBlocksController = {
store: {
getState: () => {
return {
recentBlocks: [
{ gasPrices: [ '0x3b9aca00', '0x174876e800'] },
{ gasPrices: [ '0x3b9aca00', '0x174876e800'] },
{ gasPrices: [ '0x174876e800', '0x174876e800' ] },
{ gasPrices: [ '0x174876e800', '0x174876e800' ] },
],
}
},
},
}

const gasPrice = metamaskController.getGasPrice()
assert.equal(gasPrice, '0x174876e800', 'accurately estimates 65th percentile accepted gas price')

metamaskController.recentBlocksController = realRecentBlocksController
})
})

describe('#createNewVaultAndKeychain', function () {
it('can only create new vault on keyringController once', async function () {
const selectStub = sandbox.stub(metamaskController, 'selectFirstIdentity')
Expand Down
Loading

0 comments on commit 0470386

Please sign in to comment.