-
-
Notifications
You must be signed in to change notification settings - Fork 254
Fix: SelectedNetworkController should return globally selected networkClient from getProviderAndBlockTracker when domain not in state
#4063
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
Merged
+265
−91
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
042dd26
WIP
Gudahtt ec17d7f
register getSelectedNetworkClient action handler
adonesky1 8ef37c6
Add Node16 WeakRef
jiexi 556048e
Use WeakRef
jiexi e973b2a
Revert "Use WeakRef"
jiexi 9737690
Revert "Add Node16 WeakRef"
jiexi 3f35ca8
Fix setNetworkClientIdForDomain setup
jiexi 3ec3598
Update specs
jiexi 3424c04
add getSelectedNetworkClient spec
jiexi b0f1670
add jsdocs
adonesky1 71655dc
update test
adonesky1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import type { | |
| BlockTrackerProxy, | ||
| NetworkClientId, | ||
| NetworkControllerGetNetworkClientByIdAction, | ||
| NetworkControllerGetSelectedNetworkClientAction, | ||
| NetworkControllerGetStateAction, | ||
| NetworkControllerStateChangeEvent, | ||
| ProviderProxy, | ||
|
|
@@ -71,6 +72,7 @@ export type SelectedNetworkControllerActions = | |
|
|
||
| export type AllowedActions = | ||
| | NetworkControllerGetNetworkClientByIdAction | ||
| | NetworkControllerGetSelectedNetworkClientAction | ||
| | NetworkControllerGetStateAction | ||
| | PermissionControllerHasPermissions | ||
| | PermissionControllerGetSubjectsAction; | ||
|
|
@@ -171,9 +173,7 @@ export class SelectedNetworkController extends BaseController< | |
| op === 'remove' && | ||
| this.state.domains[domain] !== undefined | ||
| ) { | ||
| this.update(({ domains }) => { | ||
| delete domains[domain]; | ||
| }); | ||
| this.#unsetNetworkClientIdForDomain(domain); | ||
| } | ||
| } | ||
| }); | ||
|
|
@@ -222,24 +222,39 @@ export class SelectedNetworkController extends BaseController< | |
| 'NetworkController:getNetworkClientById', | ||
| networkClientId, | ||
| ); | ||
| const networkProxy = this.#domainProxyMap.get(domain); | ||
| if (networkProxy === undefined) { | ||
| this.#domainProxyMap.set(domain, { | ||
| provider: createEventEmitterProxy(networkClient.provider), | ||
| blockTracker: createEventEmitterProxy(networkClient.blockTracker, { | ||
| eventFilter: 'skipInternal', | ||
| }), | ||
| }); | ||
| } else { | ||
| networkProxy.provider.setTarget(networkClient.provider); | ||
| networkProxy.blockTracker.setTarget(networkClient.blockTracker); | ||
| } | ||
| const networkProxy = this.getProviderAndBlockTracker(domain); | ||
| networkProxy.provider.setTarget(networkClient.provider); | ||
| networkProxy.blockTracker.setTarget(networkClient.blockTracker); | ||
|
|
||
| this.update((state) => { | ||
| state.domains[domain] = networkClientId; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * This method is used when a domain is removed from the PermissionsController. | ||
| * It will remove re-point the network proxy to the globally selected network in the domainProxyMap or, if no globally selected network client is available, delete the proxy. | ||
| * | ||
| * @param domain - The domain for which to unset the network client ID. | ||
| */ | ||
| #unsetNetworkClientIdForDomain(domain: Domain) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Could use a JSDoc description here as well
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done here: b0f1670 |
||
| const globallySelectedNetworkClient = this.messagingSystem.call( | ||
| 'NetworkController:getSelectedNetworkClient', | ||
| ); | ||
| const networkProxy = this.#domainProxyMap.get(domain); | ||
| if (networkProxy && globallySelectedNetworkClient) { | ||
| networkProxy.provider.setTarget(globallySelectedNetworkClient.provider); | ||
| networkProxy.blockTracker.setTarget( | ||
| globallySelectedNetworkClient.blockTracker, | ||
| ); | ||
| } else if (networkProxy) { | ||
| this.#domainProxyMap.delete(domain); | ||
| } | ||
adonesky1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.update((state) => { | ||
| delete state.domains[domain]; | ||
| }); | ||
| } | ||
|
|
||
| #domainHasPermissions(domain: Domain): boolean { | ||
| return this.messagingSystem.call( | ||
| 'PermissionController:hasPermissions', | ||
|
|
@@ -282,23 +297,23 @@ export class SelectedNetworkController extends BaseController< | |
| * @returns The proxy and block tracker proxies. | ||
| */ | ||
| getProviderAndBlockTracker(domain: Domain): NetworkProxy { | ||
| if (!this.#getUseRequestQueue()) { | ||
| throw new Error( | ||
| 'Provider and BlockTracker should be fetched from NetworkController when useRequestQueue is false', | ||
| ); | ||
| } | ||
| const networkClientId = this.state.domains[domain]; | ||
| if (!networkClientId) { | ||
| throw new Error( | ||
| 'NetworkClientId has not been set for the requested domain', | ||
| ); | ||
| } | ||
| let networkProxy = this.#domainProxyMap.get(domain); | ||
| if (networkProxy === undefined) { | ||
| const networkClient = this.messagingSystem.call( | ||
| 'NetworkController:getNetworkClientById', | ||
| networkClientId, | ||
| ); | ||
| let networkClient; | ||
| if (networkClientId === undefined) { | ||
| networkClient = this.messagingSystem.call( | ||
| 'NetworkController:getSelectedNetworkClient', | ||
| ); | ||
| if (networkClient === undefined) { | ||
| throw new Error('Selected network not initialized'); | ||
| } | ||
| } else { | ||
| networkClient = this.messagingSystem.call( | ||
| 'NetworkController:getNetworkClientById', | ||
| networkClientId, | ||
| ); | ||
| } | ||
| networkProxy = { | ||
| provider: createEventEmitterProxy(networkClient.provider), | ||
| blockTracker: createEventEmitterProxy(networkClient.blockTracker, { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should have a JSDoc description.
Maybe also worth adding a
@deprecatedtag ongetProviderAndBlockTracker(it's equivalent to this, except more difficult to work with because of the return type)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done here: b0f1670