-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add performance metrics for signature requests (#26967)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> - Adds `Signature` traces - Wraps `newUnsignedTypedMessage` in a utility function, adds end of `Middleware` trace and `Signature` trace after hash is returned from background. Note that this PR still using a preview will be updated once core is released. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26967?quickstart=1) ## **Related issues** Fixes: MetaMask/MetaMask-planning#2902 ## **Manual testing steps** No QA needed. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> An example Sentry trace for `Signature` https://metamask.sentry.io/performance/trace/53446691f4d44d40a5f9de82f1cebd05/?fov=0%2C4381.00004196167&node=txn-d4053774d6c2495b8564fdce1fac8bec&pageEnd&pageStart&project=273496&source=traces&statsPeriod=15m×tamp=1725629491.052 ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [x] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [x] 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. --------- Co-authored-by: Vinicius Stevam <45455812+vinistevam@users.noreply.github.com>
- Loading branch information
1 parent
77176e0
commit f36a831
Showing
11 changed files
with
209 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { SignatureController } from '@metamask/signature-controller'; | ||
import type { | ||
OriginalRequest, | ||
TypedMessageParams, | ||
} from '@metamask/message-manager'; | ||
import { endTrace, TraceName } from '../../../../shared/lib/trace'; | ||
import { MESSAGE_TYPE } from '../../../../shared/constants/app'; | ||
|
||
export type SignatureParams = [TypedMessageParams, OriginalRequest]; | ||
|
||
export type MessageType = keyof typeof MESSAGE_TYPE; | ||
|
||
export type AddSignatureMessageRequest = { | ||
signatureParams: SignatureParams; | ||
signatureController: SignatureController; | ||
}; | ||
|
||
async function handleSignature( | ||
signatureParams: SignatureParams, | ||
signatureController: SignatureController, | ||
functionName: keyof SignatureController, | ||
) { | ||
const [, signatureRequest] = signatureParams; | ||
const { id } = signatureRequest; | ||
const actionId = id?.toString(); | ||
|
||
endTrace({ name: TraceName.Middleware, id: actionId }); | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore: Expected 4-5 arguments, but got 2. | ||
const hash = await signatureController[functionName](...signatureParams); | ||
|
||
endTrace({ name: TraceName.Signature, id: actionId }); | ||
|
||
return hash; | ||
} | ||
|
||
export async function addTypedMessage({ | ||
signatureParams, | ||
signatureController, | ||
}: { | ||
signatureParams: SignatureParams; | ||
signatureController: SignatureController; | ||
}) { | ||
return handleSignature( | ||
signatureParams, | ||
signatureController, | ||
'newUnsignedTypedMessage', | ||
); | ||
} | ||
|
||
export async function addPersonalMessage({ | ||
signatureParams, | ||
signatureController, | ||
}: { | ||
signatureParams: SignatureParams; | ||
signatureController: SignatureController; | ||
}) { | ||
return handleSignature( | ||
signatureParams, | ||
signatureController, | ||
'newUnsignedPersonalMessage', | ||
); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { SignatureController } from '@metamask/signature-controller'; | ||
import type { | ||
OriginalRequest, | ||
TypedMessageParams, | ||
} from '@metamask/message-manager'; | ||
import { endTrace, TraceName } from '../../../../shared/lib/trace'; | ||
import { addTypedMessage } from './util'; | ||
import type { AddSignatureMessageRequest, SignatureParams } from './util'; | ||
|
||
jest.mock('../../../../shared/lib/trace', () => ({ | ||
...jest.requireActual('../../../../shared/lib/trace'), | ||
endTrace: jest.fn(), | ||
})); | ||
|
||
describe('addSignatureMessage', () => { | ||
const idMock = 1234; | ||
const hashMock = 'hash-mock'; | ||
const messageParamsMock = { | ||
from: '0x12345', | ||
} as TypedMessageParams; | ||
|
||
const originalRequestMock = { | ||
id: idMock, | ||
} as OriginalRequest; | ||
|
||
const signatureParamsMock: SignatureParams = [ | ||
messageParamsMock, | ||
originalRequestMock, | ||
]; | ||
const signatureControllerMock: SignatureController = { | ||
newUnsignedTypedMessage: jest.fn(() => hashMock), | ||
newUnsignedPersonalMessage: jest.fn(() => hashMock), | ||
} as unknown as SignatureController; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return a hash when called with valid parameters', async () => { | ||
const request: AddSignatureMessageRequest = { | ||
signatureParams: signatureParamsMock, | ||
signatureController: signatureControllerMock, | ||
}; | ||
|
||
const result = await addTypedMessage(request); | ||
expect(result).toBe(hashMock); | ||
}); | ||
|
||
it('should call endTrace with correct parameters', async () => { | ||
const request: AddSignatureMessageRequest = { | ||
signatureParams: signatureParamsMock, | ||
signatureController: signatureControllerMock, | ||
}; | ||
|
||
await addTypedMessage(request); | ||
|
||
expect(endTrace).toHaveBeenCalledTimes(2); | ||
expect(endTrace).toHaveBeenCalledWith({ | ||
name: TraceName.Middleware, | ||
id: idMock.toString(), | ||
}); | ||
expect(endTrace).toHaveBeenCalledWith({ | ||
name: TraceName.Signature, | ||
id: idMock.toString(), | ||
}); | ||
}); | ||
}); |
This file contains 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 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 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 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 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 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