diff --git a/package-lock.json b/package-lock.json index 7d20eb5..bd18fca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "eth-json-rpc-filters", - "version": "4.1.0", + "version": "4.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7dc058c..71062ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eth-json-rpc-filters", - "version": "4.1.0", + "version": "4.1.1", "description": "[json-rpc-engine](https://github.com/kumavis/json-rpc-engine) middleware implementing ethereum filter methods. Backed by an [eth-block-tracker](https://github.com/MetaMask/eth-block-tracker) and web3 provider interface (`web3.currentProvider`).", "main": "index.js", "scripts": { diff --git a/subscriptionManager.js b/subscriptionManager.js index ea014eb..76f9915 100644 --- a/subscriptionManager.js +++ b/subscriptionManager.js @@ -13,15 +13,24 @@ function createSubscriptionMiddleware({ blockTracker, provider }) { const subscriptions = {} const filterManager = createFilterMiddleware({ blockTracker, provider }) + // internal flag + let isDestroyed = false + // create subscriptionManager api object const events = new SafeEventEmitter() const middleware = createScaffoldMiddleware({ eth_subscribe: createAsyncMiddleware(subscribe), eth_unsubscribe: createAsyncMiddleware(unsubscribe), }) + middleware.destroy = destroy return { events, middleware } async function subscribe(req, res) { + + if (isDestroyed) throw new Error( + 'SubscriptionManager - attempting to use after destroying' + ) + const subscriptionType = req.params[0] // subId is 16 byte hex string const subId = unsafeRandomBytes(16) @@ -81,6 +90,11 @@ function createSubscriptionMiddleware({ blockTracker, provider }) { } async function unsubscribe(req, res) { + + if (isDestroyed) throw new Error( + 'SubscriptionManager - attempting to use after destroying' + ) + const id = req.params[0] const subscription = subscriptions[id] // if missing, return "false" to indicate it was not removed @@ -105,6 +119,14 @@ function createSubscriptionMiddleware({ blockTracker, provider }) { }) } + function destroy () { + events.removeAllListeners() + for (const id in subscriptions) { + subscriptions[id].destroy() + delete subscriptions[id] + } + isDestroyed = true + } } function normalizeBlock(block) { diff --git a/test/ganache.js b/test/ganache.js index b9b2c15..166c933 100644 --- a/test/ganache.js +++ b/test/ganache.js @@ -1,13 +1,7 @@ const test = require('tape') -const clone = require('deep-clone') -const JsonRpcEngine = require('json-rpc-engine') -const asMiddleware = require('json-rpc-engine/src/asMiddleware') -const createScaffoldMiddleware = require('eth-json-rpc-middleware/scaffold') -const providerFromEngine = require('eth-json-rpc-middleware/providerFromEngine') const ethUtil = require('ethereumjs-util') const { createTestSetup, - createPayload, asyncTest, deployLogEchoContract, } = require('./util') diff --git a/tx-filter.js b/tx-filter.js index 2f3545e..63ef0de 100644 --- a/tx-filter.js +++ b/tx-filter.js @@ -5,13 +5,13 @@ const { incrementHexInt } = require('./hexUtils') class TxFilter extends BaseFilter { - constructor ({ provider, params }) { + constructor ({ provider }) { super() this.type = 'tx' this.provider = provider } - async update ({ oldBlock, newBlock }) { + async update ({ oldBlock }) { const toBlock = oldBlock const fromBlock = incrementHexInt(oldBlock) const blocks = await getBlocksForRange({ provider: this.provider, fromBlock, toBlock })