generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 5
feat(ocap-kernel): add resource limits for remote communications #714
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
Open
sirtimid
wants to merge
19
commits into
main
Choose a base branch
from
sirtimid/remote-comms-resource-limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
fc631d8
feat(ocap-kernel): add resource limits for remote communications
sirtimid ba2ab03
fix bugs
sirtimid 9f83890
merge
sirtimid b5d7b3c
close channel to release network resources
sirtimid 89c09f6
small refactor
sirtimid 5e4e59e
update last timestamp on inbound message receipt
sirtimid a44644a
fix yet another bug
sirtimid 2d8437e
Check connection limit for inbound connections
sirtimid ca35a69
remove redundant check
sirtimid 799b1d3
thresholds
sirtimid 8323837
close channel after dials
sirtimid 4b98dfe
fix bug
sirtimid 79bae98
fix more bugs oof
sirtimid 972080b
cleanup
sirtimid 33e187d
moooore cleanup
sirtimid a85f406
fixing moooore bugs
sirtimid 4092b73
fixing all bugs
sirtimid 2190328
fixing race condition
sirtimid 71fe98b
fix Messages stuck in queue when write fails on replaced channel
sirtimid 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
233 changes: 233 additions & 0 deletions
233
packages/kernel-errors/src/errors/ResourceLimitError.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
|
|
||
| import { ResourceLimitError } from './ResourceLimitError.ts'; | ||
| import { ErrorCode, ErrorSentinel } from '../constants.ts'; | ||
| import { unmarshalErrorOptions } from '../marshal/unmarshalError.ts'; | ||
| import type { MarshaledOcapError } from '../types.ts'; | ||
|
|
||
| describe('ResourceLimitError', () => { | ||
| it('creates a ResourceLimitError with the correct properties', () => { | ||
| const error = new ResourceLimitError('Connection limit exceeded'); | ||
| expect(error).toBeInstanceOf(ResourceLimitError); | ||
| expect(error.code).toBe(ErrorCode.ResourceLimitError); | ||
| expect(error.message).toBe('Connection limit exceeded'); | ||
| expect(error.data).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('creates a ResourceLimitError with connection limit data', () => { | ||
| const error = new ResourceLimitError('Connection limit exceeded', { | ||
| data: { | ||
| limitType: 'connection', | ||
| current: 100, | ||
| limit: 100, | ||
| }, | ||
| }); | ||
| expect(error).toBeInstanceOf(ResourceLimitError); | ||
| expect(error.code).toBe(ErrorCode.ResourceLimitError); | ||
| expect(error.message).toBe('Connection limit exceeded'); | ||
| expect(error.data).toStrictEqual({ | ||
| limitType: 'connection', | ||
| current: 100, | ||
| limit: 100, | ||
| }); | ||
| }); | ||
|
|
||
| it('creates a ResourceLimitError with message size limit data', () => { | ||
| const error = new ResourceLimitError('Message size limit exceeded', { | ||
| data: { | ||
| limitType: 'messageSize', | ||
| current: 1048577, | ||
| limit: 1048576, | ||
| }, | ||
| }); | ||
| expect(error).toBeInstanceOf(ResourceLimitError); | ||
| expect(error.code).toBe(ErrorCode.ResourceLimitError); | ||
| expect(error.message).toBe('Message size limit exceeded'); | ||
| expect(error.data).toStrictEqual({ | ||
| limitType: 'messageSize', | ||
| current: 1048577, | ||
| limit: 1048576, | ||
| }); | ||
| }); | ||
|
|
||
| it('creates a ResourceLimitError with partial data', () => { | ||
| const error = new ResourceLimitError('Resource limit exceeded', { | ||
| data: { | ||
| limitType: 'connection', | ||
| }, | ||
| }); | ||
| expect(error).toBeInstanceOf(ResourceLimitError); | ||
| expect(error.data).toStrictEqual({ | ||
| limitType: 'connection', | ||
| }); | ||
| }); | ||
|
|
||
| it('creates a ResourceLimitError with a cause', () => { | ||
| const cause = new Error('Original error'); | ||
| const error = new ResourceLimitError('Resource limit exceeded', { cause }); | ||
| expect(error).toBeInstanceOf(ResourceLimitError); | ||
| expect(error.code).toBe(ErrorCode.ResourceLimitError); | ||
| expect(error.cause).toBe(cause); | ||
| }); | ||
|
|
||
| it('creates a ResourceLimitError with a custom stack', () => { | ||
| const customStack = 'custom stack trace'; | ||
| const error = new ResourceLimitError('Resource limit exceeded', { | ||
| stack: customStack, | ||
| }); | ||
| expect(error).toBeInstanceOf(ResourceLimitError); | ||
| expect(error.stack).toBe(customStack); | ||
| }); | ||
|
|
||
| it('unmarshals a valid marshaled ResourceLimitError with connection limit data', () => { | ||
| const marshaledError: MarshaledOcapError = { | ||
| [ErrorSentinel]: true, | ||
| message: 'Connection limit exceeded', | ||
| code: ErrorCode.ResourceLimitError, | ||
| data: { | ||
| limitType: 'connection', | ||
| current: 100, | ||
| limit: 100, | ||
| }, | ||
| stack: 'stack trace', | ||
| }; | ||
|
|
||
| const unmarshaledError = ResourceLimitError.unmarshal( | ||
| marshaledError, | ||
| unmarshalErrorOptions, | ||
| ); | ||
| expect(unmarshaledError).toBeInstanceOf(ResourceLimitError); | ||
| expect(unmarshaledError.code).toBe(ErrorCode.ResourceLimitError); | ||
| expect(unmarshaledError.message).toBe('Connection limit exceeded'); | ||
| expect(unmarshaledError.stack).toBe('stack trace'); | ||
| expect(unmarshaledError.data).toStrictEqual({ | ||
| limitType: 'connection', | ||
| current: 100, | ||
| limit: 100, | ||
| }); | ||
| }); | ||
|
|
||
| it('unmarshals a valid marshaled ResourceLimitError with message size limit data', () => { | ||
| const marshaledError: MarshaledOcapError = { | ||
| [ErrorSentinel]: true, | ||
| message: 'Message size limit exceeded', | ||
| code: ErrorCode.ResourceLimitError, | ||
| data: { | ||
| limitType: 'messageSize', | ||
| current: 1048577, | ||
| limit: 1048576, | ||
| }, | ||
| stack: 'stack trace', | ||
| }; | ||
|
|
||
| const unmarshaledError = ResourceLimitError.unmarshal( | ||
| marshaledError, | ||
| unmarshalErrorOptions, | ||
| ); | ||
| expect(unmarshaledError).toBeInstanceOf(ResourceLimitError); | ||
| expect(unmarshaledError.code).toBe(ErrorCode.ResourceLimitError); | ||
| expect(unmarshaledError.message).toBe('Message size limit exceeded'); | ||
| expect(unmarshaledError.data).toStrictEqual({ | ||
| limitType: 'messageSize', | ||
| current: 1048577, | ||
| limit: 1048576, | ||
| }); | ||
| }); | ||
|
|
||
| it('unmarshals a valid marshaled ResourceLimitError without data', () => { | ||
| const marshaledError = { | ||
| [ErrorSentinel]: true, | ||
| message: 'Resource limit exceeded', | ||
| code: ErrorCode.ResourceLimitError, | ||
| stack: 'stack trace', | ||
| } as unknown as MarshaledOcapError; | ||
|
|
||
| const unmarshaledError = ResourceLimitError.unmarshal( | ||
| marshaledError, | ||
| unmarshalErrorOptions, | ||
| ); | ||
| expect(unmarshaledError).toBeInstanceOf(ResourceLimitError); | ||
| expect(unmarshaledError.code).toBe(ErrorCode.ResourceLimitError); | ||
| expect(unmarshaledError.message).toBe('Resource limit exceeded'); | ||
| expect(unmarshaledError.data).toBeUndefined(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { | ||
| name: 'invalid limitType value', | ||
| marshaledError: { | ||
| [ErrorSentinel]: true, | ||
| message: 'Resource limit exceeded', | ||
| code: ErrorCode.ResourceLimitError, | ||
| data: { | ||
| limitType: 'invalid', | ||
| current: 100, | ||
| limit: 100, | ||
| }, | ||
| stack: 'stack trace', | ||
| } as unknown as MarshaledOcapError, | ||
| expectedError: | ||
| 'At path: data.limitType -- Expected the value to satisfy a union of `literal | literal`, but received: "invalid"', | ||
| }, | ||
| { | ||
| name: 'invalid current type', | ||
| marshaledError: { | ||
| [ErrorSentinel]: true, | ||
| message: 'Resource limit exceeded', | ||
| code: ErrorCode.ResourceLimitError, | ||
| data: { | ||
| limitType: 'connection', | ||
| current: 'not a number', | ||
| limit: 100, | ||
| }, | ||
| stack: 'stack trace', | ||
| } as unknown as MarshaledOcapError, | ||
| expectedError: | ||
| 'At path: data.current -- Expected a number, but received: "not a number"', | ||
| }, | ||
| { | ||
| name: 'invalid limit type', | ||
| marshaledError: { | ||
| [ErrorSentinel]: true, | ||
| message: 'Resource limit exceeded', | ||
| code: ErrorCode.ResourceLimitError, | ||
| data: { | ||
| limitType: 'connection', | ||
| current: 100, | ||
| limit: 'not a number', | ||
| }, | ||
| stack: 'stack trace', | ||
| } as unknown as MarshaledOcapError, | ||
| expectedError: | ||
| 'At path: data.limit -- Expected a number, but received: "not a number"', | ||
| }, | ||
| { | ||
| name: 'wrong error code', | ||
| marshaledError: { | ||
| [ErrorSentinel]: true, | ||
| message: 'Resource limit exceeded', | ||
| code: 'WRONG_ERROR_CODE' as ErrorCode, | ||
| stack: 'stack trace', | ||
| } as unknown as MarshaledOcapError, | ||
| expectedError: | ||
| 'At path: code -- Expected the literal `"RESOURCE_LIMIT_ERROR"`, but received: "WRONG_ERROR_CODE"', | ||
| }, | ||
| { | ||
| name: 'missing required fields', | ||
| marshaledError: { | ||
| [ErrorSentinel]: true, | ||
| message: 'Resource limit exceeded', | ||
| // Missing code field | ||
| } as unknown as MarshaledOcapError, | ||
| expectedError: | ||
| 'At path: code -- Expected the literal `"RESOURCE_LIMIT_ERROR"`, but received: undefined', | ||
| }, | ||
| ])( | ||
| 'throws an error when unmarshaling with $name', | ||
| ({ marshaledError, expectedError }) => { | ||
| expect(() => | ||
| ResourceLimitError.unmarshal(marshaledError, unmarshalErrorOptions), | ||
| ).toThrow(expectedError); | ||
| }, | ||
| ); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { | ||
| assert, | ||
| literal, | ||
| number, | ||
| object, | ||
| optional, | ||
| union, | ||
| } from '@metamask/superstruct'; | ||
|
|
||
| import { BaseError } from '../BaseError.ts'; | ||
| import { marshaledErrorSchema, ErrorCode } from '../constants.ts'; | ||
| import type { ErrorOptionsWithStack, MarshaledOcapError } from '../types.ts'; | ||
|
|
||
| export class ResourceLimitError extends BaseError { | ||
| constructor( | ||
| message: string, | ||
| options?: ErrorOptionsWithStack & { | ||
| data?: { | ||
| limitType?: 'connection' | 'messageSize'; | ||
| current?: number; | ||
| limit?: number; | ||
| }; | ||
| }, | ||
| ) { | ||
| super(ErrorCode.ResourceLimitError, message, { | ||
| ...options, | ||
| }); | ||
| harden(this); | ||
| } | ||
|
|
||
| /** | ||
| * A superstruct struct for validating marshaled {@link ResourceLimitError} instances. | ||
| */ | ||
| public static struct = object({ | ||
| ...marshaledErrorSchema, | ||
| code: literal(ErrorCode.ResourceLimitError), | ||
| data: optional( | ||
| object({ | ||
| limitType: optional( | ||
| union([literal('connection'), literal('messageSize')]), | ||
| ), | ||
| current: optional(number()), | ||
| limit: optional(number()), | ||
| }), | ||
| ), | ||
| }); | ||
|
|
||
| /** | ||
| * Unmarshals a {@link MarshaledError} into a {@link ResourceLimitError}. | ||
| * | ||
| * @param marshaledError - The marshaled error to unmarshal. | ||
| * @param unmarshalErrorOptions - The function to unmarshal the error options. | ||
| * @returns The unmarshaled error. | ||
| */ | ||
| public static unmarshal( | ||
| marshaledError: MarshaledOcapError, | ||
| unmarshalErrorOptions: ( | ||
| marshaledError: MarshaledOcapError, | ||
| ) => ErrorOptionsWithStack, | ||
| ): ResourceLimitError { | ||
| assert(marshaledError, this.struct); | ||
| const options = unmarshalErrorOptions(marshaledError); | ||
| const data = marshaledError.data as | ||
| | { | ||
| limitType?: 'connection' | 'messageSize'; | ||
| current?: number; | ||
| limit?: number; | ||
| } | ||
| | undefined; | ||
| return new ResourceLimitError(marshaledError.message, { | ||
| ...options, | ||
| ...(data !== undefined && { data }), | ||
| }); | ||
| } | ||
| } | ||
| harden(ResourceLimitError); | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.