1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/test/e2e/tests/eth-subscribe.spec.js
Mark Stacey bd23a49013
Revert "Moved subscribe and filter into network controller (#16693)" (#18129)
* Revert "Moved subscribe and filter into network controller (#16693)"

This reverts commit 6f6984fa58. That
commit was an RPC middleware refactor intended to move the subscribe
and filter middleware into the network controller, to simplify the
process of sharing this middleware between clients.

This refactor resulted in `eth_subscribe` notifications being sent on
the wrong connections, causing the UI to break in some cases (the UI
`provider` connection does not support notifications). This happened
because the `setupProviderEngine` function runs per-connection,
whereas the engine setup inside the network controller is global. The
global network client cannot support notifications because it has no
way to route them; they'll need to stay in the per-connection provider
engine.

Closes #17467

* Add e2e test

An e2e test has been added that confirms subscriptions are only
broadcast to the site that registered them. This test fails on
`develop`.
2023-03-15 12:16:31 -02:30

79 lines
2.9 KiB
JavaScript

const { convertToHexValue, withFixtures } = require('../helpers');
const FixtureBuilder = require('../fixture-builder');
describe('eth_subscribe', function () {
const ganacheOptions = {
accounts: [
{
secretKey:
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC',
balance: convertToHexValue(25000000000000000000),
},
],
};
it('only broadcasts subscription notifications on the page that registered the subscription', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
dappOptions: { numberOfDapps: 2 },
title: this.test.title,
},
async ({ driver }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
await driver.openNewPage('http://127.0.0.1:8080/');
const setupSubscriptionListener = `
const responseContainer = document.createElement('div');
responseContainer.setAttribute('id', 'eth-subscribe-responses');
const body = window.document.getElementsByTagName('body')[0];
body.appendChild(responseContainer);
window.ethereum.addListener('message', (message) => {
if (message.type === 'eth_subscription') {
const response = document.createElement('p');
response.setAttribute('data-testid', 'eth-subscribe-response');
response.append(JSON.stringify(message.data.result));
const responses = window.document.getElementById('eth-subscribe-responses');
responses.appendChild(response)
}
});
`;
await driver.executeScript(setupSubscriptionListener);
// A `newHeads` subscription will emit a notification for each new block
// See here for more information: https://docs.infura.io/infura/networks/ethereum/json-rpc-methods/subscription-methods/eth_subscribe
await driver.executeScript(`
window.ethereum.request({
method: 'eth_subscribe',
params: ['newHeads']
});
`);
// Verify that the new block is seen on the first dapp
await driver.findElement('[data-testid="eth-subscribe-response"]');
// Switch to the second dapp
await driver.openNewPage('http://127.0.0.1:8081/');
// Setup the same subscription listener as on the first dapp, but without registering a new subscription
await driver.executeScript(setupSubscriptionListener);
// Verify that the new block is not seen on the second dapp
await driver.assertElementNotPresent(
'[data-testid="eth-subscribe-response"]',
);
},
);
});
});