Skip to content

Commit

Permalink
Moved subscribe and filter into network controller
Browse files Browse the repository at this point in the history
fixes #16573
  • Loading branch information
BelfordZ committed Nov 25, 2022
1 parent f44af06 commit 6ad94ca
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 58 deletions.
15 changes: 14 additions & 1 deletion app/scripts/controllers/network/createInfuraClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {

import { createInfuraMiddleware } from '@metamask/eth-json-rpc-infura';
import { PollingBlockTracker } from 'eth-block-tracker';
import createFilterMiddleware from 'eth-json-rpc-filters';
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';

import { BUILT_IN_NETWORKS } from '../../../../shared/constants/network';

Expand All @@ -23,6 +25,15 @@ export default function createInfuraClient({ network, projectId }) {
const infuraProvider = providerFromMiddleware(infuraMiddleware);
const blockTracker = new PollingBlockTracker({ provider: infuraProvider });

const filterMiddleware = createFilterMiddleware({
infuraProvider,
blockTracker,
});
const subscriptionManager = createSubscriptionManager({
infuraProvider,
blockTracker,
});

const networkMiddleware = mergeMiddleware([
createNetworkAndChainIdMiddleware({ network }),
createBlockCacheMiddleware({ blockTracker }),
Expand All @@ -31,8 +42,10 @@ export default function createInfuraClient({ network, projectId }) {
createRetryOnEmptyMiddleware({ blockTracker, provider: infuraProvider }),
createBlockTrackerInspectorMiddleware({ blockTracker }),
infuraMiddleware,
filterMiddleware,
subscriptionManager.middleware,
]);
return { networkMiddleware, blockTracker };
return { networkMiddleware, blockTracker, subscriptionManager };
}

function createNetworkAndChainIdMiddleware({ network }) {
Expand Down
17 changes: 16 additions & 1 deletion app/scripts/controllers/network/createJsonRpcClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
providerFromMiddleware,
} from 'eth-json-rpc-middleware';
import { PollingBlockTracker } from 'eth-block-tracker';
import createFilterMiddleware from 'eth-json-rpc-filters';
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';
import { SECOND } from '../../../../shared/constants/time';

const inTest = process.env.IN_TEST;
Expand All @@ -19,11 +21,22 @@ const getTestMiddlewares = () => {
export default function createJsonRpcClient({ rpcUrl, chainId }) {
const fetchMiddleware = createFetchMiddleware({ rpcUrl });
const blockProvider = providerFromMiddleware(fetchMiddleware);

// create subscription polyfill middleware
const blockTracker = new PollingBlockTracker({
...blockTrackerOpts,
provider: blockProvider,
});

const filterMiddleware = createFilterMiddleware({
blockProvider,
blockTracker,
});
const subscriptionManager = createSubscriptionManager({
blockProvider,
blockTracker,
});

const networkMiddleware = mergeMiddleware([
...getTestMiddlewares(),
createChainIdMiddleware(chainId),
Expand All @@ -32,9 +45,11 @@ export default function createJsonRpcClient({ rpcUrl, chainId }) {
createInflightCacheMiddleware(),
createBlockTrackerInspectorMiddleware({ blockTracker }),
fetchMiddleware,
filterMiddleware,
subscriptionManager.middleware,
]);

return { networkMiddleware, blockTracker };
return { networkMiddleware, blockTracker, subscriptionManager };
}

function createChainIdMiddleware(chainId) {
Expand Down
7 changes: 6 additions & 1 deletion app/scripts/controllers/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,16 @@ export default class NetworkController extends EventEmitter {
this._setNetworkClient(networkClient);
}

_setNetworkClient({ networkMiddleware, blockTracker }) {
_setNetworkClient({ networkMiddleware, blockTracker, subscriptionManager }) {
const metamaskMiddleware = createMetamaskMiddleware(
this._baseProviderParams,
);
const engine = new JsonRpcEngine();

subscriptionManager.events.on('notification', (message) =>
engine.emit('notification', message),
);

engine.push(metamaskMiddleware);
engine.push(networkMiddleware);
const provider = providerFromEngine(engine);
Expand Down
18 changes: 1 addition & 17 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { storeAsStream } from '@metamask/obs-store/dist/asStream';
import { JsonRpcEngine } from 'json-rpc-engine';
import { debounce } from 'lodash';
import createEngineStream from 'json-rpc-middleware-stream/engineStream';
import createFilterMiddleware from 'eth-json-rpc-filters';
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';
import { providerAsMiddleware } from 'eth-json-rpc-middleware';
import KeyringController from 'eth-keyring-controller';
import {
Expand Down Expand Up @@ -3710,19 +3708,7 @@ export default class MetamaskController extends EventEmitter {
setupProviderEngine({ origin, subjectType, sender, tabId }) {
// setup json rpc engine stack
const engine = new JsonRpcEngine();
const { blockTracker, provider } = this;

// create filter polyfill middleware
const filterMiddleware = createFilterMiddleware({ provider, blockTracker });

// create subscription polyfill middleware
const subscriptionManager = createSubscriptionManager({
provider,
blockTracker,
});
subscriptionManager.events.on('notification', (message) =>
engine.emit('notification', message),
);
const { provider } = this;

// append origin to each request
engine.push(createOriginMiddleware({ origin }));
Expand Down Expand Up @@ -3890,8 +3876,6 @@ export default class MetamaskController extends EventEmitter {
///: END:ONLY_INCLUDE_IN

// filter and subscription polyfills
engine.push(filterMiddleware);
engine.push(subscriptionManager.middleware);
if (subjectType !== SUBJECT_TYPES.INTERNAL) {
// permissions
engine.push(
Expand Down
Loading

0 comments on commit 6ad94ca

Please sign in to comment.