Skip to content

Commit

Permalink
fix: onRetryFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptodev-2s committed Jul 22, 2024
1 parent 87543b0 commit 18eb12d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/block-ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ describe('createBlockRefMiddleware', () => {
id: 1,
jsonrpc: '2.0',
result: 'something',
error: undefined,
});
},
);
Expand Down Expand Up @@ -209,7 +208,6 @@ describe('createBlockRefMiddleware', () => {
id: 1,
jsonrpc: '2.0',
result: 'something',
error: undefined,
});
},
);
Expand Down
7 changes: 3 additions & 4 deletions src/block-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PollingBlockTracker } from '@metamask/eth-block-tracker';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
import { serializeError } from '@metamask/rpc-errors';
import type { Json, JsonRpcParams } from '@metamask/utils';
import { klona } from 'klona/full';

Expand Down Expand Up @@ -69,10 +70,8 @@ export function createBlockRefMiddleware({
childRequest,
);
res.result = childResult;
res.error = undefined;
} catch (error: any) {
res.result = undefined;
res.error = error;
} catch (error) {
res.error = serializeError(error);
}

return undefined;
Expand Down
1 change: 0 additions & 1 deletion src/retryOnEmpty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ describe('createRetryOnEmptyMiddleware', () => {
id: 1,
jsonrpc: '2.0',
result: 'something',
error: undefined,
});
},
);
Expand Down
49 changes: 22 additions & 27 deletions src/retryOnEmpty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,34 @@ export function createRetryOnEmptyMiddleware({
// create child request with specific block-ref
const childRequest = klona(req);
// attempt child request until non-empty response is received
try {
const childResult = await retry(10, async () => {
log('Performing request %o', childRequest);
const result = await provider.request<JsonRpcParams, Block>(
childRequest,
const childResult = await retry(10, async () => {
log('Performing request %o', childRequest);
const attemptResult = await provider.request<JsonRpcParams, Block>(
childRequest,
);
log('Result is %o', attemptResult);
// verify result
const allEmptyValues: unknown[] = emptyValues;
if (allEmptyValues.includes(attemptResult)) {
throw new Error(
`RetryOnEmptyMiddleware - empty result "${JSON.stringify(
attemptResult,
)}" for request "${JSON.stringify(childRequest)}"`,
);
log('Result is %o', result);
// verify result
if (emptyValues.includes(result as any)) {
throw new Error(
`RetryOnEmptyMiddleware - empty result "${JSON.stringify(
result,
)}" for request "${JSON.stringify(childRequest)}"`,
);
}
return result;
});
log('Copying result %o', childResult);
res.result = childResult;
res.error = undefined;
} catch (error: any) {
log('Copying error %o', error);
res.result = undefined;
res.error = error;
}
}
return attemptResult;
});
log('Copying result %o', childResult);
// copy child result onto original response
res.result = childResult;
return undefined;
});
}

async function retry(
async function retry<Response>(
maxRetries: number,
asyncFn: () => Promise<Block>,
): Promise<Block> {
asyncFn: () => Promise<Response>,
): Promise<Response> {
for (let index = 0; index < maxRetries; index++) {
try {
return await asyncFn();
Expand Down

0 comments on commit 18eb12d

Please sign in to comment.