From 0a5ae395099f9cadef5e446a0d591d00be4685e5 Mon Sep 17 00:00:00 2001 From: kumavis Date: Thu, 21 Sep 2017 17:37:30 -0700 Subject: [PATCH 1/9] bug - fix event emitter mem leak warning --- app/scripts/contentscript.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 90a0f1f22..b4708189e 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -42,16 +42,21 @@ function setupStreams () { name: 'contentscript', target: 'inpage', }) - pageStream.on('error', console.error) const pluginPort = extension.runtime.connect({ name: 'contentscript' }) const pluginStream = new PortStream(pluginPort) - pluginStream.on('error', console.error) // forward communication plugin->inpage - pageStream.pipe(pluginStream).pipe(pageStream) + pump( + pageStream, + pluginStream, + pageStream, + (err) => logStreamDisconnectWarning('MetaMask Contentscript Forwarding', err) + ) // setup local multistream channels const mux = new ObjectMultiplex() + mux.setMaxListeners(25) + pump( mux, pageStream, From 2ca2df183265edb048ee1f0e6bd2437cab7c4efe Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 22 Sep 2017 13:58:46 -0700 Subject: [PATCH 2/9] deps - bump eth-block-tracker --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 0d02f1df5..a9268060d 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "end-of-stream": "^1.1.0", "ensnare": "^1.0.0", "eth-bin-to-ops": "^1.0.1", + "eth-block-tracker": "^2.2.0", "eth-contract-metadata": "^1.1.4", "eth-hd-keyring": "^1.1.1", "eth-json-rpc-filters": "^1.1.0", From 15195bca75f84859b4d4efce9b8155504750c99d Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 22 Sep 2017 14:20:44 -0700 Subject: [PATCH 3/9] deps - bump provider engine for block tracker --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a9268060d..38ed28eef 100644 --- a/package.json +++ b/package.json @@ -139,7 +139,7 @@ "valid-url": "^1.0.9", "vreme": "^3.0.2", "web3": "^0.20.1", - "web3-provider-engine": "^13.2.9", + "web3-provider-engine": "^13.2.10", "web3-stream-provider": "^3.0.1", "xtend": "^4.0.1" }, From dd45592641500504d0d804316e6625e5b89718f9 Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 22 Sep 2017 14:22:07 -0700 Subject: [PATCH 4/9] metamask - use provider-engines block tracker --- app/scripts/metamask-controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fef16c3a9..53fb27476 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -81,7 +81,7 @@ module.exports = class MetamaskController extends EventEmitter { // rpc provider this.provider = this.initializeProvider() - this.blockTracker = this.provider + this.blockTracker = this.provider._blockTracker // eth data query tools this.ethQuery = new EthQuery(this.provider) From 8ad74cf93a8c0170f260507694f6a05eafa56c4f Mon Sep 17 00:00:00 2001 From: kumavis Date: Fri, 22 Sep 2017 15:16:42 -0700 Subject: [PATCH 5/9] deps - bump filter deps and add random missing deps --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 38ed28eef..018c10483 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "eth-block-tracker": "^2.2.0", "eth-contract-metadata": "^1.1.4", "eth-hd-keyring": "^1.1.1", - "eth-json-rpc-filters": "^1.1.0", + "eth-json-rpc-filters": "^1.2.1", "eth-phishing-detect": "^1.1.4", "eth-query": "^2.1.2", "eth-sig-util": "^1.2.2", @@ -81,6 +81,7 @@ "ethereumjs-tx": "^1.3.0", "ethereumjs-util": "github:ethereumjs/ethereumjs-util#ac5d0908536b447083ea422b435da27f26615de9", "ethereumjs-wallet": "^0.6.0", + "ethjs-contract": "^0.1.9", "ethjs-ens": "^2.0.0", "ethjs-query": "^0.2.9", "express": "^4.14.0", @@ -91,6 +92,7 @@ "gulp": "github:gulpjs/gulp#4.0", "gulp-eslint": "^4.0.0", "hat": "0.0.3", + "human-standard-token-abi": "^1.0.2", "idb-global": "^2.1.0", "identicon.js": "^2.3.1", "iframe": "^1.0.0", @@ -139,7 +141,7 @@ "valid-url": "^1.0.9", "vreme": "^3.0.2", "web3": "^0.20.1", - "web3-provider-engine": "^13.2.10", + "web3-provider-engine": "^13.2.12", "web3-stream-provider": "^3.0.1", "xtend": "^4.0.1" }, From b654eb9b1f37cd3757e4614bb048884ab89d2986 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 26 Sep 2017 11:52:57 -0700 Subject: [PATCH 6/9] wrap block tracker in events proxy --- app/scripts/controllers/network.js | 22 ++++++++------------ app/scripts/lib/events-proxy.js | 31 +++++++++++++++++++++++++++++ test/unit/network-contoller-test.js | 1 + 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 app/scripts/lib/events-proxy.js diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 0a3e5e26b..6daedbb67 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -4,6 +4,7 @@ const ObservableStore = require('obs-store') const ComposedStore = require('obs-store/lib/composed') const extend = require('xtend') const EthQuery = require('eth-query') +const createEventEmitterProxy = require('../lib/events-proxy.js') const RPC_ADDRESS_LIST = require('../config.js').network const DEFAULT_RPC = RPC_ADDRESS_LIST['rinkeby'] @@ -31,16 +32,8 @@ module.exports = class NetworkController extends EventEmitter { initializeProvider (opts, providerContructor = MetaMaskProvider) { this.providerInit = opts this._provider = providerContructor(opts) - this._proxy = new Proxy(this._provider, { - get: (obj, name) => { - if (name === 'on') return this._on.bind(this) - return this._provider[name] - }, - set: (obj, name, value) => { - this._provider[name] = value - return value - }, - }) + this._proxy = createEventEmitterProxy(this._provider) + this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker) this.provider.on('block', this._logBlock.bind(this)) this.provider.on('error', this.verifyNetwork.bind(this)) this.ethQuery = new EthQuery(this.provider) @@ -55,11 +48,12 @@ module.exports = class NetworkController extends EventEmitter { this._provider.removeAllListeners() this._provider.stop() - this.provider = MetaMaskProvider(newInit) + this._provider = MetaMaskProvider(newInit) // apply the listners created by other controllers - Object.keys(this._providerListeners).forEach((key) => { - this._providerListeners[key].forEach((handler) => this._provider.addListener(key, handler)) - }) + const blockTrackerHandlers = this.provider._blockTracker.proxyEventHandlers + this.provider.setTarget(this._provider) + + this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker, blockTrackerHandlers) this.emit('networkDidChange') } diff --git a/app/scripts/lib/events-proxy.js b/app/scripts/lib/events-proxy.js new file mode 100644 index 000000000..d1199a278 --- /dev/null +++ b/app/scripts/lib/events-proxy.js @@ -0,0 +1,31 @@ +module.exports = function createEventEmitterProxy(eventEmitter, listeners) { + let target = eventEmitter + const eventHandlers = listeners || {} + const proxy = new Proxy({}, { + get: (obj, name) => { + // intercept listeners + if (name === 'on') return addListener + if (name === 'setTarget') return setTarget + if (name === 'proxyEventHandlers') return eventHandlers + return target[name] + }, + set: (obj, name, value) => { + target[name] = value + return true + }, + }) + function setTarget (eventEmitter) { + target = eventEmitter + // migrate listeners + Object.keys(eventHandlers).forEach((name) => { + eventHandlers[name].forEach((handler) => target.on(name, handler)) + }) + } + function addListener (name, handler) { + if (!eventHandlers[name]) eventHandlers[name] = [] + eventHandlers[name].push(handler) + target.on(name, handler) + } + if (listeners) proxy.setTarget(eventEmitter) + return proxy +} \ No newline at end of file diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js index 87c2ee7a3..853e4e457 100644 --- a/test/unit/network-contoller-test.js +++ b/test/unit/network-contoller-test.js @@ -71,6 +71,7 @@ function dummyProviderConstructor() { // provider sendAsync: noop, // block tracker + _blockTracker: {}, start: noop, stop: noop, on: noop, From 2ed8d579daa4d33c38891ac77d1415fcd237a187 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 26 Sep 2017 13:37:13 -0700 Subject: [PATCH 7/9] listen for the blocke event on the block tracker instead of rawBlock on the provider --- app/scripts/controllers/network.js | 1 - app/scripts/controllers/transactions.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 6daedbb67..00bdca2c3 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -52,7 +52,6 @@ module.exports = class NetworkController extends EventEmitter { // apply the listners created by other controllers const blockTrackerHandlers = this.provider._blockTracker.proxyEventHandlers this.provider.setTarget(this._provider) - this.provider._blockTracker = createEventEmitterProxy(this._provider._blockTracker, blockTrackerHandlers) this.emit('networkDidChange') } diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js index fb3be6073..4e52a3c14 100644 --- a/app/scripts/controllers/transactions.js +++ b/app/scripts/controllers/transactions.js @@ -70,7 +70,7 @@ module.exports = class TransactionController extends EventEmitter { this.pendingTxTracker.on('txFailed', this.setTxStatusFailed.bind(this)) this.pendingTxTracker.on('txConfirmed', this.setTxStatusConfirmed.bind(this)) - this.blockTracker.on('rawBlock', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker)) + this.blockTracker.on('block', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker)) // this is a little messy but until ethstore has been either // removed or redone this is to guard against the race condition // where ethStore hasent been populated by the results yet From 4f887c6a62ebcecfb578d55fffd3c9f7a1bd088a Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 26 Sep 2017 13:44:45 -0700 Subject: [PATCH 8/9] fix test --- test/unit/network-contoller-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js index 853e4e457..c1fdaf032 100644 --- a/test/unit/network-contoller-test.js +++ b/test/unit/network-contoller-test.js @@ -21,7 +21,7 @@ describe('# Network Controller', function () { it('provider should be updatable without reassignment', function () { networkController.initializeProvider(networkControllerProviderInit, dummyProviderConstructor) const provider = networkController.provider - networkController._provider = {test: true} + networkController.provider.setTarget({test: true, on: () => {}}) assert.ok(provider.test) }) }) From 9d1cb0f76dce203299200940f21e868f6a5efef3 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 26 Sep 2017 13:56:09 -0700 Subject: [PATCH 9/9] network contoller - clean up unused code --- app/scripts/controllers/network.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/scripts/controllers/network.js b/app/scripts/controllers/network.js index 00bdca2c3..dc9978043 100644 --- a/app/scripts/controllers/network.js +++ b/app/scripts/controllers/network.js @@ -114,10 +114,4 @@ module.exports = class NetworkController extends EventEmitter { log.info(`BLOCK CHANGED: #${block.number.toString('hex')} 0x${block.hash.toString('hex')}`) this.verifyNetwork() } - - _on (event, handler) { - if (!this._providerListeners[event]) this._providerListeners[event] = [] - this._providerListeners[event].push(handler) - this._provider.on(event, handler) - } }