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

Ad/fix/update get provider and block tracker for snaps #4259

Merged
merged 6 commits into from
May 6, 2024

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented May 6, 2024

Explanation

Modifies SelectedNetworkController's, getProviderAndBlockTracker method to return the NetworkController's globally selected network client proxy if the domain arg is either metamask or a snap (identified by if prefixed by npm: or local:)

References

see this thread for further information

Changelog

@metamask/selected-network-controller

  • CHANGE: Modifies SelectedNetworkController's, getProviderAndBlockTracker method to return the NetworkController's globally selected network client proxy if the domain arg is either metamask or a snap (identified as starting with npm: or local:)

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

…proxied NetworkController selected network client"
…ly selected proxy if requesting domain is "metamask"
@adonesky1 adonesky1 requested a review from a team as a code owner May 6, 2024 17:18
@adonesky1 adonesky1 requested a review from Gudahtt May 6, 2024 17:18
Gudahtt
Gudahtt previously approved these changes May 6, 2024
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

const snapDomain = 'npm:@metamask/bip32-example-snap';
const result = controller.getProviderAndBlockTracker(snapDomain);
expect(domainProxyMap.get(snapDomain)).toBeUndefined();
expect(messenger.call).toHaveBeenCalledWith(
Copy link
Member

@Gudahtt Gudahtt May 6, 2024

Choose a reason for hiding this comment

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

Nit: Hmm, this feels like testing internal implementation details, rather than testing the behavior we want. In theory we might refactor this to use a new equivalent action, or we might end up calling this in other code paths, or we might rename this action, etc. which could break the test despite the behaviour the test is meant to test being unchanged.

To make this test less fragile, we could try switching the globally selected network and test that the provider we got has the correct chain before and after the switch. Maybe that would work, that's just the first suggestion that comes to mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I totally agree that this is not a great way to test it. Testing it the right way involves pulling in the actual NetworkController which I was hoping to do at a later time 😅 . I put this TODO in there about this same point a few weeks ago:

// TODO - improve these tests by using a full NetworkController and doing more robust behavioral testing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added the TODO in another spot here: 0fd8b54

Copy link
Member

@Gudahtt Gudahtt May 6, 2024

Choose a reason for hiding this comment

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

Hmm, we've been trying to move away from using actual full controllers as unit test fixtures as that tends to be fragile as well for different reasons. e.g. any breaking change to controller construction, initialization, etc. breaks unrelated tests, and sometimes they have internals that aren't well suited to unit tests that are difficult to mock out (e.g. network requests).

Leaving this improvement as a TODO sounds good to me though!

Gudahtt
Gudahtt previously approved these changes May 6, 2024
…roller.test.ts

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
Gudahtt
Gudahtt previously approved these changes May 6, 2024
@adonesky1 adonesky1 merged commit c88f286 into main May 6, 2024
139 checks passed
@adonesky1 adonesky1 deleted the ad/fix/update-getProviderAndBlockTracker-for-snaps branch May 6, 2024 17:54
@adonesky1 adonesky1 mentioned this pull request May 6, 2024
adonesky1 added a commit that referenced this pull request May 6, 2024
# @metamask/selected-network-controller

## [13.0.0]

### Changed

- `getProviderAndBlockTracker` now returns the `NetworkController`'s
globally selected network client proxy if the `domain` arg is either
`metamask` or a snap (identified as starting with `npm:` or `local:`)
([#4259](#4259))
- **BREAKING:** Now when `setNetworkClientIdForDomain` is called with a
snap's domain (identified as starting with `npm:` or `local:`), the
`domain` will not be added to state and no proxy will be created for
this domain in the `domainProxyMap`
([#4258](#4258))
- In order to remove snaps that made it into `domains` state prior to
this change, consumers will need to run a migration.

---------

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
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.

None yet

2 participants