Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved subscribe and filter into network controller #16693

Merged
merged 6 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion app/scripts/controllers/network/network-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { strict as assert } from 'assert';
import EventEmitter from 'events';
import { ComposedStore, ObservableStore } from '@metamask/obs-store';
import { JsonRpcEngine } from 'json-rpc-engine';
import { providerFromEngine } from 'eth-json-rpc-middleware';
import {
providerFromEngine,
providerFromMiddleware,
} from 'eth-json-rpc-middleware';
import log from 'loglevel';
import {
createSwappableProxy,
createEventEmitterProxy,
} from 'swappable-obj-proxy';
import EthQuery from 'eth-query';
import createFilterMiddleware from 'eth-json-rpc-filters';
import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';
import {
INFURA_PROVIDER_TYPES,
BUILT_IN_NETWORKS,
Expand Down Expand Up @@ -447,13 +452,30 @@ export default class NetworkController extends EventEmitter {
}

_setNetworkClient({ networkMiddleware, blockTracker }) {
const networkProvider = providerFromMiddleware(networkMiddleware);
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
const filterMiddleware = createFilterMiddleware({
provider: networkProvider,
blockTracker,
});
const subscriptionManager = createSubscriptionManager({
provider: networkProvider,
blockTracker,
});
const metamaskMiddleware = createMetamaskMiddleware(
this._baseProviderParams,
);

const engine = new JsonRpcEngine();
subscriptionManager.events.on('notification', (message) =>
engine.emit('notification', message),
);
engine.push(filterMiddleware);
engine.push(subscriptionManager.middleware);
engine.push(metamaskMiddleware);
engine.push(networkMiddleware);

const provider = providerFromEngine(engine);

this._setProviderAndBlockTracker({ provider, blockTracker });
}

Expand Down
2 changes: 2 additions & 0 deletions app/scripts/controllers/permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,12 @@ export const unrestrictedMethods = Object.freeze([
'eth_signTypedData_v1',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
'eth_subscribe',
'eth_submitHashrate',
'eth_submitWork',
'eth_syncing',
'eth_uninstallFilter',
'eth_unsubscribe',
'metamask_getProviderState',
'metamask_watchAsset',
'net_listening',
Expand Down
25 changes: 9 additions & 16 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 @@ -3734,21 +3732,19 @@ export default class MetamaskController extends EventEmitter {
* @param {tabId} [options.tabId] - The tab ID of the sender - if the sender is within a tab
*/
setupProviderEngine({ origin, subjectType, sender, tabId }) {
const { provider } = this;

// 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,
// forward notifications from network provider
provider.on('data', (error, message) => {
if (error) {
// This should never happen, this error parameter is never set
throw error;
}
engine.emit('notification', message);
});
subscriptionManager.events.on('notification', (message) =>
engine.emit('notification', message),
);

if (isManifestV3) {
engine.push(createDupeReqFilterMiddleware());
Expand Down Expand Up @@ -3919,9 +3915,6 @@ export default class MetamaskController extends EventEmitter {
);
///: END:ONLY_INCLUDE_IN

Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
// filter and subscription polyfills
engine.push(filterMiddleware);
engine.push(subscriptionManager.middleware);
if (subjectType !== SUBJECT_TYPES.INTERNAL) {
// permissions
engine.push(
Expand Down