-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: Filter out duplicate JSON-RPC requests #24748
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
+424
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e34a722
fix: Filter out duplicate JSON-RPC requests
FrederikBolding 2c1fd63
Fix type issues
FrederikBolding 5eae169
Ignore type mismatch
FrederikBolding c4760a6
Properly clean up timers
FrederikBolding 31f5493
Update app/core/BackgroundBridge/createDupeReqFilterStream.ts
FrederikBolding 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
324 changes: 324 additions & 0 deletions
324
app/core/BackgroundBridge/createDupeReqFilterStream.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,324 @@ | ||
| import { Transform } from 'readable-stream'; | ||
|
|
||
| import type { JsonRpcNotification, JsonRpcRequest } from '@metamask/utils'; | ||
| import createDupeReqFilterStream, { | ||
| THREE_MINUTES, | ||
| } from './createDupeReqFilterStream'; | ||
|
|
||
| function createTestStream(output: JsonRpcRequest[] = [], S = Transform) { | ||
| const transformStream = createDupeReqFilterStream(); | ||
| const testOutStream = new S({ | ||
| transform: (chunk: JsonRpcRequest, _, cb) => { | ||
| output.push(chunk); | ||
| cb(); | ||
| }, | ||
| objectMode: true, | ||
| }); | ||
|
|
||
| transformStream.pipe(testOutStream); | ||
|
|
||
| return transformStream; | ||
| } | ||
|
|
||
| function runStreamTest( | ||
| requests: (JsonRpcRequest | JsonRpcNotification)[] = [], | ||
| advanceTimersTime = 10, | ||
| S = Transform, | ||
| ) { | ||
| return new Promise((resolve, reject) => { | ||
| const output: JsonRpcRequest[] = []; | ||
| const testStream = createTestStream(output, S); | ||
|
|
||
| testStream | ||
| .on('finish', () => resolve(output)) | ||
| .on('error', (err) => reject(err)); | ||
|
|
||
| requests.forEach((request) => testStream.write(request)); | ||
| testStream.end(); | ||
|
|
||
| jest.advanceTimersByTime(advanceTimersTime); | ||
| }); | ||
| } | ||
|
|
||
| describe('createDupeReqFilterStream', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers({ now: 10 }); | ||
| }); | ||
|
|
||
| it('lets through requests with ids being seen for the first time', async () => { | ||
| const requests = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| ].map((request) => ({ ...request, jsonrpc: '2.0' as const })); | ||
|
|
||
| const expectedOutput = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| ].map((output) => ({ ...output, jsonrpc: '2.0' })); | ||
|
|
||
| const output = await runStreamTest(requests); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| it('does not let through the request if the id has been seen before', async () => { | ||
| const requests = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, // duplicate | ||
| ].map((request) => ({ ...request, jsonrpc: '2.0' as const })); | ||
|
|
||
| const expectedOutput = [{ id: 1, method: 'foo' }].map((output) => ({ | ||
| ...output, | ||
| jsonrpc: '2.0', | ||
| })); | ||
|
|
||
| const output = await runStreamTest(requests); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| it("lets through requests if they don't have an id", async () => { | ||
| const requests = [{ method: 'notify1' }, { method: 'notify2' }].map( | ||
| (request) => ({ ...request, jsonrpc: '2.0' as const }), | ||
| ); | ||
|
|
||
| const expectedOutput = [{ method: 'notify1' }, { method: 'notify2' }].map( | ||
| (output) => ({ ...output, jsonrpc: '2.0' }), | ||
| ); | ||
|
|
||
| const output = await runStreamTest(requests); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| it('handles a mix of request types', async () => { | ||
| const requests = [ | ||
| { id: 1, method: 'foo' }, | ||
| { method: 'notify1' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { method: 'notify2' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 3, method: 'baz' }, | ||
| ].map((request) => ({ ...request, jsonrpc: '2.0' as const })); | ||
|
|
||
| const expectedOutput = [ | ||
| { id: 1, method: 'foo' }, | ||
| { method: 'notify1' }, | ||
| { id: 2, method: 'bar' }, | ||
| { method: 'notify2' }, | ||
| { id: 3, method: 'baz' }, | ||
| ].map((output) => ({ ...output, jsonrpc: '2.0' })); | ||
|
|
||
| const output = await runStreamTest(requests); | ||
| expect(output).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| it('expires single id after three minutes', () => { | ||
| const output: JsonRpcRequest[] = []; | ||
| const testStream = createTestStream(output); | ||
|
|
||
| const requests1 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputBeforeExpiryTime = [{ id: 1, method: 'foo' }]; | ||
|
|
||
| requests1.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputBeforeExpiryTime); | ||
|
|
||
| const requests2 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputAfterExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES); | ||
|
|
||
| requests2.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputAfterExpiryTime); | ||
| }); | ||
|
|
||
| it('does not expire single id after less than three', () => { | ||
| const output: JsonRpcRequest[] = []; | ||
| const testStream = createTestStream(output); | ||
|
|
||
| const requests1 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputBeforeTimeElapses = [{ id: 1, method: 'foo' }]; | ||
|
|
||
| requests1.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputBeforeTimeElapses); | ||
|
|
||
| const requests2 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputAfterTimeElapses = expectedOutputBeforeTimeElapses; | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES - 1); | ||
|
|
||
| requests2.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputAfterTimeElapses); | ||
| }); | ||
|
|
||
| it('expires multiple ids after three minutes', () => { | ||
| const output: JsonRpcRequest[] = []; | ||
| const testStream = createTestStream(output); | ||
|
|
||
| const requests1 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 3, method: 'baz' }, | ||
| { id: 3, method: 'baz' }, | ||
| ]; | ||
| const expectedOutputBeforeExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 3, method: 'baz' }, | ||
| ]; | ||
|
|
||
| requests1.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputBeforeExpiryTime); | ||
|
|
||
| const requests2 = [ | ||
| { id: 3, method: 'baz' }, | ||
| { id: 3, method: 'baz' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputAfterExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 3, method: 'baz' }, | ||
| { id: 3, method: 'baz' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES); | ||
|
|
||
| requests2.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputAfterExpiryTime); | ||
| }); | ||
|
|
||
| it('expires single id in three minute intervals', () => { | ||
| const output: JsonRpcRequest[] = []; | ||
| const testStream = createTestStream(output); | ||
|
|
||
| const requests1 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputBeforeExpiryTime = [{ id: 1, method: 'foo' }]; | ||
|
|
||
| requests1.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputBeforeExpiryTime); | ||
|
|
||
| const requests2 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputAfterFirstExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES); | ||
|
|
||
| requests2.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputAfterFirstExpiryTime); | ||
|
|
||
| const requests3 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
| const expectedOutputAfterSecondExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 1, method: 'foo' }, | ||
| ]; | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES); | ||
|
|
||
| requests3.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputAfterSecondExpiryTime); | ||
| }); | ||
|
|
||
| it('expires somes ids at intervals while not expiring others', () => { | ||
| const output: JsonRpcRequest[] = []; | ||
| const testStream = createTestStream(output); | ||
|
|
||
| const requests1 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| ]; | ||
| const expectedOutputBeforeExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| ]; | ||
|
|
||
| requests1.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputBeforeExpiryTime); | ||
|
|
||
| const requests2 = [{ id: 3, method: 'baz' }]; | ||
| const expectedOutputAfterFirstExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 3, method: 'baz' }, | ||
| ]; | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES - 1); | ||
|
|
||
| requests2.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputAfterFirstExpiryTime); | ||
|
|
||
| const requests3 = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 3, method: 'baz' }, | ||
| { id: 4, method: 'buzz' }, | ||
| ]; | ||
| const expectedOutputAfterSecondExpiryTime = [ | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 3, method: 'baz' }, | ||
| { id: 1, method: 'foo' }, | ||
| { id: 2, method: 'bar' }, | ||
| { id: 4, method: 'buzz' }, | ||
| ]; | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES - 1); | ||
|
|
||
| requests3.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputAfterSecondExpiryTime); | ||
| }); | ||
|
|
||
| it('handles running expiry job without seeing any ids', () => { | ||
| const output: JsonRpcRequest[] = []; | ||
| const testStream = createTestStream(output); | ||
|
|
||
| const requests1 = [{ id: 1, method: 'foo' }]; | ||
| const expectedOutputBeforeExpiryTime = [{ id: 1, method: 'foo' }]; | ||
|
|
||
| requests1.forEach((request) => testStream.write(request)); | ||
| expect(output).toEqual(expectedOutputBeforeExpiryTime); | ||
|
|
||
| jest.advanceTimersByTime(THREE_MINUTES + 1); | ||
|
|
||
| expect(output).toEqual(expectedOutputBeforeExpiryTime); | ||
| }); | ||
| }); | ||
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.