aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-10-11 05:13:12 +0800
committerkumavis <aaron@kumavis.me>2017-10-11 05:13:12 +0800
commitefa92a7fc5925533f72e876c9bf84df0a6258d4a (patch)
tree7b3653c635c36826d2b2d3180050090d0930c41b
parentff4e9a0d1122db83221bc956f11c9520bf0e008c (diff)
downloadtangerine-wallet-browser-efa92a7fc5925533f72e876c9bf84df0a6258d4a.tar
tangerine-wallet-browser-efa92a7fc5925533f72e876c9bf84df0a6258d4a.tar.gz
tangerine-wallet-browser-efa92a7fc5925533f72e876c9bf84df0a6258d4a.tar.bz2
tangerine-wallet-browser-efa92a7fc5925533f72e876c9bf84df0a6258d4a.tar.lz
tangerine-wallet-browser-efa92a7fc5925533f72e876c9bf84df0a6258d4a.tar.xz
tangerine-wallet-browser-efa92a7fc5925533f72e876c9bf84df0a6258d4a.tar.zst
tangerine-wallet-browser-efa92a7fc5925533f72e876c9bf84df0a6258d4a.zip
network controller - refactor to use eth-rpc-client
-rw-r--r--app/scripts/controllers/network.js56
-rw-r--r--app/scripts/metamask-controller.js7
-rw-r--r--package.json2
3 files changed, 36 insertions, 29 deletions
diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js
index 0f9db4d53..412967dbe 100644
--- a/app/scripts/controllers/network.js
+++ b/app/scripts/controllers/network.js
@@ -5,6 +5,7 @@ const ObservableStore = require('obs-store')
const ComposedStore = require('obs-store/lib/composed')
const extend = require('xtend')
const EthQuery = require('eth-query')
+const createEthRpcClient = require('eth-rpc-client')
const createEventEmitterProxy = require('../lib/events-proxy.js')
const RPC_ADDRESS_LIST = require('../config.js').network
const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby']
@@ -17,7 +18,8 @@ module.exports = class NetworkController extends EventEmitter {
this.networkStore = new ObservableStore('loading')
this.providerStore = new ObservableStore(config.provider)
this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore })
- this._proxy = createEventEmitterProxy()
+ this.providerProxy = createEventEmitterProxy()
+ this.blockTrackerProxy = createEventEmitterProxy()
this.on('networkDidChange', this.lookupNetwork)
}
@@ -25,12 +27,11 @@ module.exports = class NetworkController extends EventEmitter {
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)
+ this._configureStandardClient({ rpcUrl })
+ this.providerProxy.on('block', this._logBlock.bind(this))
+ this.providerProxy.on('error', this.verifyNetwork.bind(this))
+ this.ethQuery = new EthQuery(this.providerProxy)
this.lookupNetwork()
- return this._proxy
}
verifyNetwork () {
@@ -76,8 +77,10 @@ module.exports = class NetworkController extends EventEmitter {
assert(type !== 'rpc', `NetworkController.setProviderType - cannot connect by type "rpc"`)
// skip if type already matches
if (type === this.getProviderConfig().type) return
+ // lookup rpcTarget for type
const rpcTarget = this.getRpcAddressForType(type)
assert(rpcTarget, `NetworkController - unknown rpc address for type "${type}"`)
+ // update connection
this.providerStore.updateState({ type, rpcTarget })
this._switchNetwork({ rpcUrl: rpcTarget })
}
@@ -97,32 +100,33 @@ module.exports = class NetworkController extends EventEmitter {
_switchNetwork (providerParams) {
this.setNetworkState('loading')
- this._configureStandardProvider(providerParams)
+ this._configureStandardClient(providerParams)
this.emit('networkDidChange')
}
- _configureStandardProvider(_providerParams) {
+ _configureStandardClient(_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
- let blockTrackerHandlers
- if (oldProvider) {
- // capture old block handlers
- blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers
- // tear down
- oldProvider.removeAllListeners()
- oldProvider.stop()
+ const client = createEthRpcClient(providerParams)
+ this._setClient(client)
+ }
+
+ _createMetamaskProvider(providerParams) {
+ const { provider, blockTracker } = createEthRpcClient(providerParams)
+ }
+
+ _setClient (newClient) {
+ // teardown old client
+ const oldClient = this._currentClient
+ if (oldClient) {
+ oldClient.blockTracker.stop()
+ // asyncEventEmitter lacks a "removeAllListeners" method
+ // oldClient.blockTracker.removeAllListeners
+ oldClient.blockTracker._events = {}
}
- // override block tracler
- provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers)
// set as new provider
- this._provider = provider
- this._proxy.setTarget(provider)
+ this._currentClient = newClient
+ this.providerProxy.setTarget(newClient.provider)
+ this.blockTrackerProxy.setTarget(newClient.blockTracker)
}
_logBlock (block) {
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 1292d2a1e..67bbdc15a 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -82,8 +82,8 @@ module.exports = class MetamaskController extends EventEmitter {
this.blacklistController.scheduleUpdates()
// rpc provider and block tracker
- this.provider = this.networkController.initializeProvider({
- static: {
+ this.networkController.initializeProvider({
+ scaffold: {
eth_syncing: false,
web3_clientVersion: `MetaMask/v${version}`,
},
@@ -97,7 +97,8 @@ module.exports = class MetamaskController extends EventEmitter {
processPersonalMessage: this.newUnsignedPersonalMessage.bind(this),
processTypedMessage: this.newUnsignedTypedMessage.bind(this),
})
- this.blockTracker = this.provider._blockTracker
+ this.provider = this.networkController.providerProxy
+ this.blockTracker = this.networkController.blockTrackerProxy
// eth data query tools
this.ethQuery = new EthQuery(this.provider)
diff --git a/package.json b/package.json
index 225742487..3a6be4c61 100644
--- a/package.json
+++ b/package.json
@@ -71,9 +71,11 @@
"eth-contract-metadata": "^1.1.4",
"eth-hd-keyring": "^1.2.1",
"eth-json-rpc-filters": "^1.2.2",
+ "eth-json-rpc-middleware": "^1.4.1",
"eth-keyring-controller": "^2.1.0",
"eth-phishing-detect": "^1.1.4",
"eth-query": "^2.1.2",
+ "eth-rpc-client": "^1.0.0",
"eth-sig-util": "^1.4.0",
"eth-simple-keyring": "^1.1.1",
"eth-token-tracker": "^1.1.4",