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

feat: Add handling to QueuedRequestController for requests that can switch the network without prompting user approval #4846

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Oct 24, 2024

Explanation

The release of Chain Permissions in Extension have changed the behavior of wallet_switchEthereumChain and wallet_addEthereumChain which breaks previous assumptions that the QueuedRequestController operated from. Specifically, it was previously assumed that the above methods would ALWAYS generate an approval that the user must accept or reject. This is important because the side effect of this behavior was that it was not possible for those two methods to immediately switch the chain if there happened to be existing pending approvals. The result of this new behavior was that pending approvals were being immediately cleared if one of the method calls above were to a chain that was already permitted which is the case where a network switch happens immediately without user interaction.

This PR fixes this new case by:

  • explicitly waiting for any batch of processing requests to finish before proceeding with processing a request that has the potential to switch the globally selected network
  • processing a request that has the potential to switch the globally selected network by itself (rather than releasing it within a batch of several requests for the same origin)
  • flushing the request queue for the origin IF a request that has the potential to switch the globally selected network actually does change the globally selected network

References

Related: MetaMask/metamask-extension#28090

Changelog

@metamask/queued-request-controller

  • BREAKING: QueuedRequestController now requires the canRequestSwitchNetworkWithoutApproval callback in its constructor params.
  • CHANGED: QueuedRequestController now ensures that any queued requests for a origin are failed if a request that can switch the globally selected network without approval actually does change the globally selected network for that origin.
  • CHANGED: QueuedRequestController now ensures that a request that can switch the globally selected network without approval is queued behind any existing pending requests.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@jiexi jiexi requested a review from a team as a code owner October 24, 2024 16:29
@jiexi
Copy link
Contributor Author

jiexi commented Oct 24, 2024

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.2-preview-5b0b6ae7",
  "@metamask-previews/address-book-controller": "6.0.1-preview-5b0b6ae7",
  "@metamask-previews/announcement-controller": "7.0.1-preview-5b0b6ae7",
  "@metamask-previews/approval-controller": "7.1.0-preview-5b0b6ae7",
  "@metamask-previews/assets-controllers": "39.0.0-preview-5b0b6ae7",
  "@metamask-previews/base-controller": "7.0.1-preview-5b0b6ae7",
  "@metamask-previews/build-utils": "3.0.1-preview-5b0b6ae7",
  "@metamask-previews/chain-controller": "0.1.3-preview-5b0b6ae7",
  "@metamask-previews/composable-controller": "9.0.1-preview-5b0b6ae7",
  "@metamask-previews/controller-utils": "11.4.0-preview-5b0b6ae7",
  "@metamask-previews/ens-controller": "14.0.1-preview-5b0b6ae7",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-5b0b6ae7",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-5b0b6ae7",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-5b0b6ae7",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-5b0b6ae7",
  "@metamask-previews/keyring-controller": "17.3.0-preview-5b0b6ae7",
  "@metamask-previews/logging-controller": "6.0.1-preview-5b0b6ae7",
  "@metamask-previews/message-manager": "11.0.0-preview-5b0b6ae7",
  "@metamask-previews/multichain": "0.0.0-preview-5b0b6ae7",
  "@metamask-previews/name-controller": "8.0.1-preview-5b0b6ae7",
  "@metamask-previews/network-controller": "22.0.0-preview-5b0b6ae7",
  "@metamask-previews/notification-controller": "7.0.0-preview-5b0b6ae7",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-5b0b6ae7",
  "@metamask-previews/permission-controller": "11.0.2-preview-5b0b6ae7",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-5b0b6ae7",
  "@metamask-previews/phishing-controller": "12.0.3-preview-5b0b6ae7",
  "@metamask-previews/polling-controller": "11.0.0-preview-5b0b6ae7",
  "@metamask-previews/preferences-controller": "13.1.0-preview-5b0b6ae7",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-5b0b6ae7",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-5b0b6ae7",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-5b0b6ae7",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-5b0b6ae7",
  "@metamask-previews/signature-controller": "20.1.0-preview-5b0b6ae7",
  "@metamask-previews/transaction-controller": "37.3.0-preview-5b0b6ae7",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-5b0b6ae7"
}

@jiexi jiexi force-pushed the jl/fix-queued-request-controller-switch-chain branch from 9aeae07 to a227546 Compare October 24, 2024 20:17
@jiexi
Copy link
Contributor Author

jiexi commented Oct 24, 2024

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.2-preview-e9ee650e",
  "@metamask-previews/address-book-controller": "6.0.1-preview-e9ee650e",
  "@metamask-previews/announcement-controller": "7.0.1-preview-e9ee650e",
  "@metamask-previews/approval-controller": "7.1.0-preview-e9ee650e",
  "@metamask-previews/assets-controllers": "39.0.0-preview-e9ee650e",
  "@metamask-previews/base-controller": "7.0.1-preview-e9ee650e",
  "@metamask-previews/build-utils": "3.0.1-preview-e9ee650e",
  "@metamask-previews/chain-controller": "0.1.3-preview-e9ee650e",
  "@metamask-previews/composable-controller": "9.0.1-preview-e9ee650e",
  "@metamask-previews/controller-utils": "11.4.0-preview-e9ee650e",
  "@metamask-previews/ens-controller": "14.0.1-preview-e9ee650e",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-e9ee650e",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-e9ee650e",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-e9ee650e",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-e9ee650e",
  "@metamask-previews/keyring-controller": "17.3.0-preview-e9ee650e",
  "@metamask-previews/logging-controller": "6.0.1-preview-e9ee650e",
  "@metamask-previews/message-manager": "11.0.0-preview-e9ee650e",
  "@metamask-previews/multichain": "0.0.0-preview-e9ee650e",
  "@metamask-previews/name-controller": "8.0.1-preview-e9ee650e",
  "@metamask-previews/network-controller": "22.0.0-preview-e9ee650e",
  "@metamask-previews/notification-controller": "7.0.0-preview-e9ee650e",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-e9ee650e",
  "@metamask-previews/permission-controller": "11.0.2-preview-e9ee650e",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-e9ee650e",
  "@metamask-previews/phishing-controller": "12.1.0-preview-e9ee650e",
  "@metamask-previews/polling-controller": "11.0.0-preview-e9ee650e",
  "@metamask-previews/preferences-controller": "13.1.0-preview-e9ee650e",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-e9ee650e",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-e9ee650e",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-e9ee650e",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-e9ee650e",
  "@metamask-previews/signature-controller": "20.1.0-preview-e9ee650e",
  "@metamask-previews/transaction-controller": "37.3.0-preview-e9ee650e",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-e9ee650e"
}

@jiexi
Copy link
Contributor Author

jiexi commented Oct 24, 2024

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.2-preview-b4526f73",
  "@metamask-previews/address-book-controller": "6.0.1-preview-b4526f73",
  "@metamask-previews/announcement-controller": "7.0.1-preview-b4526f73",
  "@metamask-previews/approval-controller": "7.1.0-preview-b4526f73",
  "@metamask-previews/assets-controllers": "39.0.0-preview-b4526f73",
  "@metamask-previews/base-controller": "7.0.1-preview-b4526f73",
  "@metamask-previews/build-utils": "3.0.1-preview-b4526f73",
  "@metamask-previews/chain-controller": "0.1.3-preview-b4526f73",
  "@metamask-previews/composable-controller": "9.0.1-preview-b4526f73",
  "@metamask-previews/controller-utils": "11.4.0-preview-b4526f73",
  "@metamask-previews/ens-controller": "14.0.1-preview-b4526f73",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-b4526f73",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-b4526f73",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-b4526f73",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-b4526f73",
  "@metamask-previews/keyring-controller": "17.3.0-preview-b4526f73",
  "@metamask-previews/logging-controller": "6.0.1-preview-b4526f73",
  "@metamask-previews/message-manager": "11.0.0-preview-b4526f73",
  "@metamask-previews/multichain": "0.0.0-preview-b4526f73",
  "@metamask-previews/name-controller": "8.0.1-preview-b4526f73",
  "@metamask-previews/network-controller": "22.0.0-preview-b4526f73",
  "@metamask-previews/notification-controller": "7.0.0-preview-b4526f73",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-b4526f73",
  "@metamask-previews/permission-controller": "11.0.2-preview-b4526f73",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-b4526f73",
  "@metamask-previews/phishing-controller": "12.1.0-preview-b4526f73",
  "@metamask-previews/polling-controller": "11.0.0-preview-b4526f73",
  "@metamask-previews/preferences-controller": "13.1.0-preview-b4526f73",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-b4526f73",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-b4526f73",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-b4526f73",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-b4526f73",
  "@metamask-previews/signature-controller": "20.1.0-preview-b4526f73",
  "@metamask-previews/transaction-controller": "37.3.0-preview-b4526f73",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-b4526f73"
}

@jiexi
Copy link
Contributor Author

jiexi commented Oct 24, 2024

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.2-preview-49199726",
  "@metamask-previews/address-book-controller": "6.0.1-preview-49199726",
  "@metamask-previews/announcement-controller": "7.0.1-preview-49199726",
  "@metamask-previews/approval-controller": "7.1.0-preview-49199726",
  "@metamask-previews/assets-controllers": "39.0.0-preview-49199726",
  "@metamask-previews/base-controller": "7.0.1-preview-49199726",
  "@metamask-previews/build-utils": "3.0.1-preview-49199726",
  "@metamask-previews/chain-controller": "0.1.3-preview-49199726",
  "@metamask-previews/composable-controller": "9.0.1-preview-49199726",
  "@metamask-previews/controller-utils": "11.4.0-preview-49199726",
  "@metamask-previews/ens-controller": "14.0.1-preview-49199726",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-49199726",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-49199726",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-49199726",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-49199726",
  "@metamask-previews/keyring-controller": "17.3.0-preview-49199726",
  "@metamask-previews/logging-controller": "6.0.1-preview-49199726",
  "@metamask-previews/message-manager": "11.0.0-preview-49199726",
  "@metamask-previews/multichain": "0.0.0-preview-49199726",
  "@metamask-previews/name-controller": "8.0.1-preview-49199726",
  "@metamask-previews/network-controller": "22.0.0-preview-49199726",
  "@metamask-previews/notification-controller": "7.0.0-preview-49199726",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-49199726",
  "@metamask-previews/permission-controller": "11.0.2-preview-49199726",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-49199726",
  "@metamask-previews/phishing-controller": "12.1.0-preview-49199726",
  "@metamask-previews/polling-controller": "11.0.0-preview-49199726",
  "@metamask-previews/preferences-controller": "13.1.0-preview-49199726",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-49199726",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-49199726",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-49199726",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-49199726",
  "@metamask-previews/signature-controller": "20.1.0-preview-49199726",
  "@metamask-previews/transaction-controller": "37.3.0-preview-49199726",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-49199726"
}

@jiexi jiexi marked this pull request as draft October 24, 2024 21:40
@jiexi
Copy link
Contributor Author

jiexi commented Oct 25, 2024

@metamaskbot publish-preview

@jiexi jiexi changed the title fix: Fix queuing being broken in QueuedRequestController due to wallet_switchEthereumChain and wallet_addEthereumChain now potentially changing the selected network without needing to prompt the user for approval feat: Add handling to QueuedRequestController for requests that can switch the network without prompting user approval Oct 25, 2024
Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.2-preview-f9d64175",
  "@metamask-previews/address-book-controller": "6.0.1-preview-f9d64175",
  "@metamask-previews/announcement-controller": "7.0.1-preview-f9d64175",
  "@metamask-previews/approval-controller": "7.1.0-preview-f9d64175",
  "@metamask-previews/assets-controllers": "39.0.0-preview-f9d64175",
  "@metamask-previews/base-controller": "7.0.1-preview-f9d64175",
  "@metamask-previews/build-utils": "3.0.1-preview-f9d64175",
  "@metamask-previews/chain-controller": "0.1.3-preview-f9d64175",
  "@metamask-previews/composable-controller": "9.0.1-preview-f9d64175",
  "@metamask-previews/controller-utils": "11.4.0-preview-f9d64175",
  "@metamask-previews/ens-controller": "14.0.1-preview-f9d64175",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-f9d64175",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-f9d64175",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-f9d64175",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-f9d64175",
  "@metamask-previews/keyring-controller": "17.3.0-preview-f9d64175",
  "@metamask-previews/logging-controller": "6.0.1-preview-f9d64175",
  "@metamask-previews/message-manager": "11.0.0-preview-f9d64175",
  "@metamask-previews/multichain": "0.0.0-preview-f9d64175",
  "@metamask-previews/name-controller": "8.0.1-preview-f9d64175",
  "@metamask-previews/network-controller": "22.0.0-preview-f9d64175",
  "@metamask-previews/notification-controller": "7.0.0-preview-f9d64175",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-f9d64175",
  "@metamask-previews/permission-controller": "11.0.2-preview-f9d64175",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-f9d64175",
  "@metamask-previews/phishing-controller": "12.2.0-preview-f9d64175",
  "@metamask-previews/polling-controller": "11.0.0-preview-f9d64175",
  "@metamask-previews/preferences-controller": "13.1.0-preview-f9d64175",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-f9d64175",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-f9d64175",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-f9d64175",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-f9d64175",
  "@metamask-previews/signature-controller": "20.1.0-preview-f9d64175",
  "@metamask-previews/transaction-controller": "37.3.0-preview-f9d64175",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-f9d64175"
}

@jiexi
Copy link
Contributor Author

jiexi commented Oct 28, 2024

@metamaskbot publish-preview

@jiexi jiexi requested review from mcmire and Gudahtt October 28, 2024 19:00
Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.2-preview-f504f91d",
  "@metamask-previews/address-book-controller": "6.0.1-preview-f504f91d",
  "@metamask-previews/announcement-controller": "7.0.1-preview-f504f91d",
  "@metamask-previews/approval-controller": "7.1.0-preview-f504f91d",
  "@metamask-previews/assets-controllers": "39.0.0-preview-f504f91d",
  "@metamask-previews/base-controller": "7.0.1-preview-f504f91d",
  "@metamask-previews/build-utils": "3.0.1-preview-f504f91d",
  "@metamask-previews/chain-controller": "0.1.3-preview-f504f91d",
  "@metamask-previews/composable-controller": "9.0.1-preview-f504f91d",
  "@metamask-previews/controller-utils": "11.4.0-preview-f504f91d",
  "@metamask-previews/ens-controller": "14.0.1-preview-f504f91d",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-f504f91d",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-f504f91d",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-f504f91d",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-f504f91d",
  "@metamask-previews/keyring-controller": "17.3.0-preview-f504f91d",
  "@metamask-previews/logging-controller": "6.0.1-preview-f504f91d",
  "@metamask-previews/message-manager": "11.0.0-preview-f504f91d",
  "@metamask-previews/multichain": "0.0.0-preview-f504f91d",
  "@metamask-previews/name-controller": "8.0.1-preview-f504f91d",
  "@metamask-previews/network-controller": "22.0.0-preview-f504f91d",
  "@metamask-previews/notification-controller": "7.0.0-preview-f504f91d",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-f504f91d",
  "@metamask-previews/permission-controller": "11.0.2-preview-f504f91d",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-f504f91d",
  "@metamask-previews/phishing-controller": "12.3.0-preview-f504f91d",
  "@metamask-previews/polling-controller": "11.0.0-preview-f504f91d",
  "@metamask-previews/preferences-controller": "13.1.0-preview-f504f91d",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-f504f91d",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-f504f91d",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-f504f91d",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-f504f91d",
  "@metamask-previews/signature-controller": "20.1.0-preview-f504f91d",
  "@metamask-previews/transaction-controller": "37.3.0-preview-f504f91d",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-f504f91d"
}

@jiexi jiexi marked this pull request as ready for review October 28, 2024 21:18
this.#processingRequestCount += 1;
firstRequest.processRequest(networkSwitchError);
this.#updateQueuedRequestCount();
await firstRequest.processedPromise;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe this would be easier to read if this property was called something like requestHasBeenProcessed

@@ -408,20 +475,12 @@ export class QueuedRequestController extends BaseController<
try {
await requestNext();
} finally {
endRequest?.();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Upon first reading this, I thought this was actively ending the request. Really we're communicating that the request has ended here, rather than ending it (it's already over at this point).

So maybe requestHasEnded would be a better name for this function.

}): Promise<void> {
const { promise, reject, resolve } = createDeferredPromise({
#waitForDequeue(request: QueuedRequestMiddlewareJsonRpcRequest) {
const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It's somewhat explained by the variable names, but a short comment about the purpose of each of these two promises might be helpful here. Deferred Promises are confusing.

Alternatively, maybe we could add a TSDoc comment to this method, and explain this there instead

return { dequeuedPromise, endRequest };
}

#processNextBatchIfReady() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: A TSDoc comment would be appreciated here

// Queue request for later processing
// Network switch is handled when this batch is processed
if (
this.state.queuedRequestCount > 0 ||
this.#originOfCurrentBatch !== request.origin ||
this.#networkClientIdOfCurrentBatch !== request.networkClientId
this.#networkClientIdOfCurrentBatch !== request.networkClientId ||
(this.#processingRequestCount > 0 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe worth an additional comment here to explain why these are a special case

firstRequest.processRequest(networkSwitchError);
this.#updateQueuedRequestCount();
await firstRequest.processedPromise;
this.#processingRequestCount -= 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe worth adding a try.. finally here to ensure the request count isn't permanently wrong when a request fails.

I know it's OK now because it can't fail (it can hang, that's it, reject is never called though). But it would be one less footgun to leave lying around.

if (this.#networkClientIdOfCurrentBatch !== selectedNetworkClientId) {
this.#flushQueueForOrigin(this.#originOfCurrentBatch);
}
this.#processNextBatchIfReady();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this call needed? The next batch should still get triggered the same way as before, when the end of enqueueRequest is hit for the switch/add Ethereum chain method call. It seems like it's getting triggered twice here now.

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a while since I've looked at this but I can mostly follow the changes here. The only thing that jumps out at me is the number of tests being added vs. the number of lines being added/rearranged here. I feel like there could be cases that haven't been tested yet, although there are a good number of tests so I'm not 100% sure. I feel like I might need to take another look tomorrow but in the meantime I just had some clarifying questions.

@@ -222,6 +223,39 @@ describe('QueuedRequestController', () => {
expect(mockShowApprovalRequest).toHaveBeenCalledTimes(1);
});

it('queues request if a requests are already being processed on the same origin, but canRequestSwitchNetworkWithoutApproval returns true', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, is there a test that's the opposite of this test anywhere?

});
// Trigger first request
const firstRequest = controller.enqueueRequest(
buildRequest(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I imagine the other tests are written this way, however, it seems that it matters that the origin for these requests are the same. That isn't completely obvious just by looking at this test, but should we make it explicit?

@@ -1063,6 +1097,146 @@ describe('QueuedRequestController', () => {
});
});

describe('when a the first request in a batch can switch the network', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question as above, are there tests which follow the same steps as these tests but check for different results? Or is the "opposite" behavior somehow covered by the existing tests already?

try {
// If globally selected network is different from origin selected network,
// switch network before processing batch
await this.#switchNetworkIfNecessary(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does moving this call effectively do and do we have a test for this change in particular?

// the queue from continuing by artificially increasing the processing
// request count
this.#processingRequestCount += 1;
firstRequest.processRequest(networkSwitchError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the effect of passing networkSwitchError to processRequest here and do we have a test for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants