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: Throw when encountering unexpected RPC method hooks #24357

Merged
merged 18 commits into from May 10, 2024

Conversation

rekmarks
Copy link
Member

@rekmarks rekmarks commented May 2, 2024

Description

Removes RPC handler hooks that had become unused. In order to prevent this from occurring again, also adds a check to fail immediately on dapp connections if extraneous hooks are provided. Finally, adds tests for createMethodMiddleware.js.

Note for reviewers

  • In an indication that this is useful, you'll note that the set of hooks removed in this PR is different from that the original PR (Remove unused RPC handler hook #20580).
  • In the commit history, you'll see an added and reverted TypeScript conversion of the method middleware and its tests. The RPC method handler types are a mess and we should probably wait to convert until we've resolved some upstream issues. Either way, the conversion would be too distracting for this PR.

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@rekmarks rekmarks requested a review from a team as a code owner May 2, 2024 21:21
@metamaskbot metamaskbot added the INVALID-PR-TEMPLATE PR's body doesn't match template label May 2, 2024
@rekmarks rekmarks changed the title Remove unused rpc handler hook refactor: Remove unused rpc handler hook May 2, 2024
@rekmarks rekmarks mentioned this pull request May 2, 2024
3 tasks
@rekmarks rekmarks marked this pull request as draft May 2, 2024 21:27
@rekmarks rekmarks force-pushed the remove-unused-rpc-handler-hook branch from 168fef4 to c1b9c73 Compare May 2, 2024 23:26
@rekmarks rekmarks changed the title refactor: Remove unused rpc handler hook refactor: Remove unused rpc handler hooks May 2, 2024
@rekmarks rekmarks force-pushed the remove-unused-rpc-handler-hook branch from c1b9c73 to d84cfdb Compare May 2, 2024 23:27
@rekmarks rekmarks changed the title refactor: Remove unused rpc handler hooks feat: Throw if encountering unexpected RPC handler hooks May 2, 2024
@rekmarks rekmarks changed the title feat: Throw if encountering unexpected RPC handler hooks feat: Throw when encountering unexpected RPC handler hooks May 2, 2024
@rekmarks rekmarks force-pushed the remove-unused-rpc-handler-hook branch from d84cfdb to 6ff3d6f Compare May 3, 2024 05:03
Fixes unit test failures by mocking the method middleware in
metamask-controller.test.js.
Our RPC method middleware / handler types are a hot mess. This makes
them slightly less odious to work with in the extension, although they
will still have to be fixed upstream.
Reverts the TypeScript conversion of method middleware files to make
review easier.
@rekmarks rekmarks force-pushed the remove-unused-rpc-handler-hook branch from 6ff3d6f to 350a322 Compare May 5, 2024 00:58
@rekmarks rekmarks requested a review from legobeat May 5, 2024 01:04
@rekmarks rekmarks marked this pull request as ready for review May 5, 2024 01:23
@rekmarks rekmarks changed the title feat: Throw when encountering unexpected RPC handler hooks feat: Throw when encountering unexpected RPC method hooks May 5, 2024
Copy link

codecov bot commented May 5, 2024

Codecov Report

Attention: Patch coverage is 78.57143% with 6 lines in your changes are missing coverage. Please review.

Project coverage is 67.46%. Comparing base (d27a233) to head (38cf3c7).
Report is 50 commits behind head on develop.

❗ Current head 38cf3c7 differs from pull request most recent head 2c1d1a3. Consider uploading reports for the commit 2c1d1a3 to get more accurate results

Files Patch % Lines
...ib/rpc-method-middleware/createMethodMiddleware.js 77.78% 6 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #24357      +/-   ##
===========================================
+ Coverage    67.37%   67.46%   +0.09%     
===========================================
  Files         1278     1286       +8     
  Lines        49881    50111     +230     
  Branches     12944    13006      +62     
===========================================
+ Hits         33605    33805     +200     
- Misses       16276    16306      +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@metamaskbot
Copy link
Collaborator

Builds ready [350a322]
Page Load Metrics (659 ± 503 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint5512473147
domContentLoaded7161021
load4426916591048503
domInteractive7161021
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -260 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Copy link
Contributor

github-actions bot commented May 5, 2024

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Collaborator

Builds ready [3a118aa]
Page Load Metrics (297 ± 345 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint5410970126
domContentLoaded86112115
load432475297719345
domInteractive86012115
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -260 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Comment on lines -20 to -28
type MessageType = (typeof MESSAGE_TYPE)[keyof typeof MESSAGE_TYPE];

type HandlerInterface = {
methodNames: MessageType[];
implementation: unknown;
hookNames?: { [key: string]: boolean };
};

const handlers: HandlerInterface[] = [
Copy link
Member Author

Choose a reason for hiding this comment

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

handlers is self-declaring.

Comment on lines +87 to +94

const rpcMethodMiddlewareMock = {
createMethodMiddleware: () => (_req, _res, next, _end) => {
next();
},
};
jest.mock('./lib/rpc-method-middleware', () => rpcMethodMiddlewareMock);

Copy link
Member Author

Choose a reason for hiding this comment

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

These tests now blow up without this mock.

@@ -10,7 +10,6 @@ module.exports = {
'<rootDir>/app/scripts/controllers/preferences.js',
'<rootDir>/app/scripts/flask/**/*.js',
'<rootDir>/app/scripts/lib/**/*.(js|ts)',
'<rootDir>/app/scripts/lib/createRPCMethodTrackingMiddleware.js',
Copy link
Member Author

Choose a reason for hiding this comment

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

Obviously extraneous due to the line above it.

@metamaskbot
Copy link
Collaborator

Builds ready [de5d01f]
Page Load Metrics (634 ± 553 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint58143832110
domContentLoaded8261242
load4735186341152553
domInteractive8261242
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -260 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@metamaskbot
Copy link
Collaborator

Builds ready [62bbc83]
Page Load Metrics (984 ± 668 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint751431022211
domContentLoaded106318126
load6137259841392668
domInteractive106318126
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -260 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@legobeat legobeat requested a review from a team May 10, 2024 02:03
@legobeat
Copy link
Contributor

It does seem like this could be handled more consistently at the type level by TypeScript, but I'm assuming the issue is that there is still a lot of unmigrated js code that will benefit from this in the meantime?

@rekmarks
Copy link
Member Author

@legobeat there's definitely unmigrated JS code, but also the types for the method middleware and its handler + hooks pattern are currently broken and implemented in 3 different repositories.

🙃We're all trying to find the guy who did this...

I may try to wrangle the types once this is in.

@metamaskbot
Copy link
Collaborator

Builds ready [38cf3c7]
Page Load Metrics (736 ± 563 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint68142882211
domContentLoaded9391594
load5530677361172563
domInteractive9391594
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -260 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

setNetworkClientIdForDomain:
this.selectedNetworkController.setNetworkClientIdForDomain.bind(
this.selectedNetworkController,
),

getUseRequestQueue: this.preferencesController.getUseRequestQueue.bind(
Copy link
Contributor

Choose a reason for hiding this comment

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

whoops 😅

Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

@rekmarks rekmarks merged commit 98d5ff2 into develop May 10, 2024
71 of 72 checks passed
@rekmarks rekmarks deleted the remove-unused-rpc-handler-hook branch May 10, 2024 17:58
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2024
@metamaskbot metamaskbot added the release-11.18.0 Issue or pull request that will be included in release 11.18.0 label May 10, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [5edfc0d]
Page Load Metrics (745 ± 567 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint5914384209
domContentLoaded9281363
load4728937451181567
domInteractive9281363
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -260 Bytes (-0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@metamaskbot metamaskbot added release-11.17.0 Issue or pull request that will be included in release 11.17.0 and removed release-11.18.0 Issue or pull request that will be included in release 11.18.0 labels May 23, 2024
@metamaskbot
Copy link
Collaborator

Missing release label release-11.17.0 on PR. Adding release label release-11.17.0 on PR and removing other release labels(release-11.18.0), as PR was added to branch 11.17.0 when release was cut.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
INVALID-PR-TEMPLATE PR's body doesn't match template release-11.17.0 Issue or pull request that will be included in release 11.17.0 team-extension-platform
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants