Skip to content

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Aug 26, 2025

Explanation

lookupNetwork operates on an arbitrary network or, if given nothing, the globally selected network. The behavior between these two cases is largely the same, except that in the "selected network" case, it publishes one of two events, NetworkController:infuraIsBlocked or NetworkController:infuraIsUnblocked. This happens after making a request to the network, and while the request is running, it is possible for the network to change. This creates a potential problem because if that happens, the events that we want to fire would no longer represent the network they were intended to represent.

lookupNetwork accounts for this by subscribing to NetworkController:networkDidChange before starting the request; if it's fired, then it exits early (with the presumption that the network switch called its own version of lookupNetwork and will publish NetworkController:infuraIsBlocked or NetworkController:infuraIsUnblocked at that point in time).

However, this approach to detect a network switch is complicated and unnecessary. We can simply capture selectedNetworkClientId before we start the request, ask what it is afterward, and compare the two to know whether the network switched. This means that we can bring the implementation of the two branches within lookupNetwork closer together (and we can eliminate some tests).

References

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 communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

mcmire added 8 commits August 13, 2025 15:46
…ookupNetwork

A while back, `lookupNetworkByClientId` was added a while back,
presumably as a multichain-compatible replacement of `lookupNetwork`.
However, `lookupNetwork` already takes an (optional) network client ID;
in fact, it calls `lookupNetworkByClientId` internally. This duplication
is confusing, and it would make for a simpler API and make
NetworkController more maintainable if we removed
`lookupNetworkByClientId` entirely.

This commit does that and refactors `lookupNetwork` to be more
understandable. It also ensures that `lookupNetwork` is well-tested both
when given a network client ID and not given one.
…pNetwork

`lookupNetwork` operates on an arbitrary network or, if given nothing,
the globally selected network. The behavior between these two cases is
largely the same, except that in the "selected network" case, it
publishes one of two events, `NetworkController:infuraIsBlocked` or
`NetworkController:infuraIsUnblocked`. This happens after making a
request to the network, and while the request is running, it is possible
for the network to change. This creates a potential problem because if
that happens, the events that we want to fire would no longer represent
the network they were intended to represent.

`lookupNetwork` accounts for this by subscribing to
`NetworkController:networkDidChange` before starting the request; if
it's fired, then it exits early (with the presumption that the network
switch called its own version of `lookupNetwork` and will publish
`NetworkController:infuraIsBlocked` or
`NetworkController:infuraIsUnblocked` at that point in time).

However, this approach to detect a network switch is complicated and
unnecessary. We can simply capture `selectedNetworkClientId` before we
start the request, ask what it is afterward, and compare the two to know
whether the network switched. This means that we can bring the
implementation of the two branches within `lookupNetwork` closer
together (and we can eliminate some tests).
@mcmire mcmire marked this pull request as ready for review August 26, 2025 22:12
@mcmire mcmire requested review from a team as code owners August 26, 2025 22:12
@mcmire
Copy link
Contributor Author

mcmire commented Aug 26, 2025

Adding no-changelog as there should be no functional changes.

Base automatically changed from remove-lookup-network-by-network-client-id to main August 27, 2025 19:08
cursor[bot]

This comment was marked as outdated.

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

Successfully merging this pull request may close these issues.

1 participant