Skip to content

refactor: Simplify RPC handling in SnapController #3397

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
merged 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 94.42,
"functions": 98.4,
"lines": 98.69,
"statements": 98.52
"branches": 94.97,
"functions": 98.38,
"lines": 98.76,
"statements": 98.59
}
56 changes: 0 additions & 56 deletions packages/snaps-controllers/src/snaps/RequestQueue.test.ts

This file was deleted.

51 changes: 0 additions & 51 deletions packages/snaps-controllers/src/snaps/RequestQueue.ts

This file was deleted.

79 changes: 0 additions & 79 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4880,85 +4880,6 @@ describe('SnapController', () => {
snapController.destroy();
});

it('handlers will throw if there are too many pending requests before a snap has started', async () => {
const rootMessenger = getControllerMessenger();
const messenger = getSnapControllerMessenger(rootMessenger);
const fakeSnap = getPersistedSnapObject({ status: SnapStatus.Stopped });
const snapId = fakeSnap.id;
const snapController = getSnapController(
getSnapControllerOptions({
messenger,
state: {
snaps: {
[snapId]: fakeSnap,
},
},
}),
);

let resolveExecutePromise: any;
const deferredExecutePromise = new Promise((resolve) => {
resolveExecutePromise = resolve;
});

rootMessenger.registerActionHandler(
'ExecutionService:executeSnap',
async () => deferredExecutePromise,
);

// Fill up the request queue
const finishPromise = Promise.all(
new Array(100).fill(1).map(async (_, id) =>
snapController.handleRequest({
snapId,
origin: MOCK_ORIGIN,
handler: HandlerType.OnRpcRequest,
request: {
jsonrpc: '2.0',
method: 'bar',
params: {},
id,
},
}),
),
);

await expect(
snapController.handleRequest({
snapId,
origin: MOCK_ORIGIN,
handler: HandlerType.OnRpcRequest,
request: {
jsonrpc: '2.0',
method: 'bar',
params: {},
id: 100,
},
}),
).rejects.toThrow(
'Exceeds maximum number of requests waiting to be resolved, please try again.',
);

// Before processing the pending requests,
// we need an rpc message handler function to be returned
jest
.spyOn(messenger, 'call')
.mockImplementation(async (method, ..._args: unknown[]) => {
if (method === 'ExecutionService:executeSnap') {
return deferredExecutePromise;
} else if (method === 'ExecutionService:handleRpcRequest') {
return Promise.resolve(undefined);
}
return true;
});

// Resolve the promise that the pending requests are waiting for and wait for them to finish
resolveExecutePromise();
await finishPromise;

snapController.destroy();
});

it('crashes the Snap on unhandled errors', async () => {
const { manifest, sourceCode, svgIcon } =
await getMockSnapFilesWithUpdatedChecksum({
Expand Down
Loading
Loading