1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Merge pull request #2237 from MetaMask/network-controller-contd

Networking refactor
This commit is contained in:
Frankie 2017-09-29 16:50:31 -07:00 committed by GitHub
commit f57fd717fe
5 changed files with 56 additions and 62 deletions

View File

@ -2,11 +2,13 @@ const MAINET_RPC_URL = 'https://mainnet.infura.io/metamask'
const ROPSTEN_RPC_URL = 'https://ropsten.infura.io/metamask'
const KOVAN_RPC_URL = 'https://kovan.infura.io/metamask'
const RINKEBY_RPC_URL = 'https://rinkeby.infura.io/metamask'
const LOCALHOST_RPC_URL = 'http://localhost:8545'
global.METAMASK_DEBUG = 'GULP_METAMASK_DEBUG'
module.exports = {
network: {
localhost: LOCALHOST_RPC_URL,
mainnet: MAINET_RPC_URL,
ropsten: ROPSTEN_RPC_URL,
kovan: KOVAN_RPC_URL,

View File

@ -1,5 +1,6 @@
const assert = require('assert')
const EventEmitter = require('events')
const MetaMaskProvider = require('web3-provider-engine/zero.js')
const createMetamaskProvider = require('web3-provider-engine/zero.js')
const ObservableStore = require('obs-store')
const ComposedStore = require('obs-store/lib/composed')
const extend = require('xtend')
@ -9,6 +10,7 @@ const RPC_ADDRESS_LIST = require('../config.js').network
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
module.exports = class NetworkController extends EventEmitter {
constructor (config) {
super()
config.provider.rpcTarget = this.getRpcAddressForType(config.provider.type, config.provider)
@ -18,13 +20,13 @@ module.exports = class NetworkController extends EventEmitter {
this._proxy = createEventEmitterProxy()
this.on('networkDidChange', this.lookupNetwork)
this.providerStore.subscribe((state) => this.switchNetwork({ rpcUrl: state.rpcTarget }))
this.providerStore.subscribe((state) => this._switchNetwork({ rpcUrl: state.rpcTarget }))
}
initializeProvider (opts, providerContructor = MetaMaskProvider) {
this._baseProviderParams = opts
const provider = providerContructor(opts)
this._setProvider(provider)
initializeProvider (_providerParams) {
this._baseProviderParams = _providerParams
const rpcUrl = this.getCurrentRpcAddress()
this._configureStandardProvider({ rpcUrl })
this._proxy.on('block', this._logBlock.bind(this))
this._proxy.on('error', this.verifyNetwork.bind(this))
this.ethQuery = new EthQuery(this._proxy)
@ -32,17 +34,8 @@ module.exports = class NetworkController extends EventEmitter {
return this._proxy
}
switchNetwork (opts) {
this.setNetworkState('loading')
const providerParams = extend(this._baseProviderParams, opts)
this._baseProviderParams = providerParams
const provider = MetaMaskProvider(providerParams)
this._setProvider(provider)
this.emit('networkDidChange')
}
verifyNetwork () {
// Check network when restoring connectivity:
// Check network when restoring connectivity:
if (this.isNetworkLoading()) this.lookupNetwork()
}
@ -79,10 +72,13 @@ module.exports = class NetworkController extends EventEmitter {
return this.getRpcAddressForType(provider.type)
}
setProviderType (type) {
async setProviderType (type) {
assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`)
// skip if type already matches
if (type === this.getProviderConfig().type) return
const rpcTarget = this.getRpcAddressForType(type)
this.providerStore.updateState({type, rpcTarget})
assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`)
this.providerStore.updateState({ type, rpcTarget })
}
getProviderConfig () {
@ -94,6 +90,22 @@ module.exports = class NetworkController extends EventEmitter {
return provider && provider.rpcTarget ? provider.rpcTarget : DEFAULT_RPC
}
//
// Private
//
_switchNetwork (providerParams) {
this.setNetworkState('loading')
this._configureStandardProvider(providerParams)
this.emit('networkDidChange')
}
_configureStandardProvider(_providerParams) {
const providerParams = extend(this._baseProviderParams, _providerParams)
const provider = createMetamaskProvider(providerParams)
this._setProvider(provider)
}
_setProvider (provider) {
// collect old block tracker events
const oldProvider = this._provider

View File

@ -217,13 +217,11 @@ module.exports = class MetamaskController extends EventEmitter {
//
initializeProvider () {
return this.networkController.initializeProvider({
const providerOpts = {
static: {
eth_syncing: false,
web3_clientVersion: `MetaMask/v${version}`,
},
// rpc data source
rpcUrl: this.networkController.getCurrentRpcAddress(),
originHttpHeaderKey: 'X-Metamask-Origin',
// account mgmt
getAccounts: (cb) => {
@ -243,7 +241,9 @@ module.exports = class MetamaskController extends EventEmitter {
processMessage: this.newUnsignedMessage.bind(this),
// personal_sign msg signing
processPersonalMessage: this.newUnsignedPersonalMessage.bind(this),
})
}
const providerProxy = this.networkController.initializeProvider(providerOpts)
return providerProxy
}
initPublicConfigStore () {
@ -312,13 +312,14 @@ module.exports = class MetamaskController extends EventEmitter {
const txController = this.txController
const noticeController = this.noticeController
const addressBookController = this.addressBookController
const networkController = this.networkController
return {
// etc
getState: (cb) => cb(null, this.getState()),
setProviderType: this.networkController.setProviderType.bind(this.networkController),
setCurrentCurrency: this.setCurrentCurrency.bind(this),
markAccountsFound: this.markAccountsFound.bind(this),
// coinbase
buyEth: this.buyEth.bind(this),
// shapeshift
@ -333,12 +334,14 @@ module.exports = class MetamaskController extends EventEmitter {
// vault management
submitPassword: this.submitPassword.bind(this),
// network management
setProviderType: nodeify(networkController.setProviderType, networkController),
setCustomRpc: nodeify(this.setCustomRpc, this),
// PreferencesController
setSelectedAddress: nodeify(preferencesController.setSelectedAddress, preferencesController),
addToken: nodeify(preferencesController.addToken, preferencesController),
setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController),
setDefaultRpc: nodeify(this.setDefaultRpc, this),
setCustomRpc: nodeify(this.setCustomRpc, this),
// AddressController
setAddressBook: nodeify(addressBookController.setAddressBook, addressBookController),
@ -689,19 +692,13 @@ module.exports = class MetamaskController extends EventEmitter {
createShapeShiftTx (depositAddress, depositType) {
this.shapeshiftController.createShapeShiftTx(depositAddress, depositType)
}
// network
setDefaultRpc () {
this.networkController.setRpcTarget('http://localhost:8545')
return Promise.resolve('http://localhost:8545')
}
// network
setCustomRpc (rpcTarget, rpcList) {
async setCustomRpc (rpcTarget, rpcList) {
this.networkController.setRpcTarget(rpcTarget)
return this.preferencesController.updateFrequentRpcList(rpcTarget)
.then(() => {
return Promise.resolve(rpcTarget)
})
await this.preferencesController.updateFrequentRpcList(rpcTarget)
return rpcTarget
}
}

View File

@ -119,14 +119,11 @@ var actions = {
SET_RPC_TARGET: 'SET_RPC_TARGET',
SET_DEFAULT_RPC_TARGET: 'SET_DEFAULT_RPC_TARGET',
SET_PROVIDER_TYPE: 'SET_PROVIDER_TYPE',
USE_ETHERSCAN_PROVIDER: 'USE_ETHERSCAN_PROVIDER',
useEtherscanProvider: useEtherscanProvider,
showConfigPage,
SHOW_ADD_TOKEN_PAGE: 'SHOW_ADD_TOKEN_PAGE',
showAddTokenPage,
addToken,
setRpcTarget: setRpcTarget,
setDefaultRpcTarget: setDefaultRpcTarget,
setProviderType: setProviderType,
// loading overlay
SHOW_LOADING: 'SHOW_LOADING_INDICATION',
@ -706,16 +703,19 @@ function markAccountsFound () {
// config
//
// default rpc target refers to localhost:8545 in this instance.
function setDefaultRpcTarget () {
log.debug(`background.setDefaultRpcTarget`)
function setProviderType (type) {
return (dispatch) => {
background.setDefaultRpc((err, result) => {
log.debug(`background.setProviderType`)
background.setProviderType(type, (err, result) => {
if (err) {
log.error(err)
return dispatch(self.displayWarning('Had a problem changing networks.'))
return dispatch(self.displayWarning('Had a problem changing networks!'))
}
})
return {
type: actions.SET_PROVIDER_TYPE,
value: type,
}
}
}
@ -744,23 +744,6 @@ function addToAddressBook (recipient, nickname) {
}
}
function setProviderType (type) {
log.debug(`background.setProviderType`)
background.setProviderType(type)
return {
type: actions.SET_PROVIDER_TYPE,
value: type,
}
}
function useEtherscanProvider () {
log.debug(`background.useEtherscanProvider`)
background.useEtherscanProvider()
return {
type: actions.USE_ETHERSCAN_PROVIDER,
}
}
function showLoadingIndication (message) {
return {
type: actions.SHOW_LOADING,

View File

@ -319,7 +319,7 @@ App.prototype.renderNetworkDropdown = function () {
{
key: 'default',
closeMenu: () => this.setState({ isNetworkMenuOpen: !isOpen }),
onClick: () => props.dispatch(actions.setDefaultRpcTarget()),
onClick: () => props.dispatch(actions.setProviderType('localhost')),
style: {
fontSize: '18px',
},