mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 01:47:00 +01:00
Moved subscribe and filter into network controller (#16693)
Our middleware for handling subscription and filter-related methods (`eth-json-rpc-filters`) currently lives in our RPC pipeline ahead of the network stack. This commit moves this middleware to the network client middleware instead. There are two reasons for this change. First, this middleware wraps RPC methods that are supported by the network. Second, it is necessary for this middleware to live with the network client so that it will aid us in unifying the NetworkController in this repo and the NetworkController in the `controllers` repo. Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
This commit is contained in:
parent
362497ed64
commit
6f6984fa58
@ -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,
|
||||
@ -447,13 +452,30 @@ export default class NetworkController extends EventEmitter {
|
||||
}
|
||||
|
||||
_setNetworkClient({ networkMiddleware, blockTracker }) {
|
||||
const networkProvider = providerFromMiddleware(networkMiddleware);
|
||||
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 });
|
||||
}
|
||||
|
||||
|
@ -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',
|
||||
|
@ -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 {
|
||||
@ -3729,21 +3727,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());
|
||||
@ -3914,9 +3910,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(
|
||||
|
Loading…
Reference in New Issue
Block a user