Skip to content

Commit

Permalink
Merge pull request #481 from lidofinance/develop
Browse files Browse the repository at this point in the history
Merge develop to main
  • Loading branch information
jake4take authored Sep 20, 2024
2 parents 66c44aa + 725be9a commit 4a8275e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pages/api/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const rpc = rpcFactory({
allowedCallAddresses,
allowedLogsAddresses,
maxBatchCount: config.PROVIDER_MAX_BATCH,
disallowEmptyAddressGetLogs: false,
disallowEmptyAddressGetLogs: true,
});

export default wrapNextRequest([
Expand Down
4 changes: 1 addition & 3 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ app.prepare().then(() => {
})
.listen(port, () => {
console.debug(`> Ready on http://${hostname}:${port}`);
})
// hanging socket timeout
.setTimeout(10_000);
});
// prevents malicious client from slowly sending headers and rest of request
server.headersTimeout = 10_000;
server.requestTimeout = 30_000;
Expand Down
2 changes: 1 addition & 1 deletion shared/hooks/use-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const useTokenTransferSubscription = () => {
[queryClient, subscriptions],
);

const shouldWatch = address && tokens.length > 0;
const shouldWatch = !!(address && tokens.length > 0);

useWatchContractEvent({
abi: Erc20EventsAbi,
Expand Down
7 changes: 6 additions & 1 deletion utils/use-web3-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ const runtimeMutableTransport = (
if (
requestParams.method === 'eth_getLogs' &&
Array.isArray(requestParams?.params) &&
requestParams.params[0]?.address?.length < 0
// works for empty array, empty string and all falsish values
!requestParams.params[0]?.address?.length
) {
console.warn(
'[runtimeMutableTransport] Invalid empty getLogs',
requestParams,
);
const error = new InvalidParamsRpcError(
new Error(`Empty address for eth_getLogs is not supported`),
);
Expand Down
46 changes: 2 additions & 44 deletions utilsApi/rpcFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Readable, Transform } from 'node:stream';
import { Readable } from 'node:stream';
import { ReadableStream } from 'node:stream/web';
import type { NextApiRequest, NextApiResponse } from 'next';
import { Counter, Registry } from 'prom-client';
Expand Down Expand Up @@ -38,28 +38,6 @@ export class SizeTooLargeError extends ClientError {
}
}

const createSizeLogger = (MAX_SIZE: number) => {
let bytesWritten = 0;
const logSizeStream = new Transform({
transform(chunk, _encoding, callback) {
bytesWritten += chunk.length;
if (bytesWritten > MAX_SIZE) {
// Emit an error if size exceeds MAX_SIZE
return callback(
new SizeTooLargeError(
`Stream size exceeds the maximum limit of ${MAX_SIZE} bytes`,
),
);
}
return callback(null, chunk); // Pass the chunk through
},
flush(callback) {
callback();
},
});
return logSizeStream;
};

export type RPCFactoryParams = {
metrics: {
prefix: string;
Expand Down Expand Up @@ -89,7 +67,6 @@ export const rpcFactory = ({
allowedCallAddresses = {},
allowedLogsAddresses = {},
maxBatchCount,
maxResponseSize = 1_000_000, // ~1MB,
disallowEmptyAddressGetLogs = false,
}: RPCFactoryParams) => {
const rpcRequestBlocked = new Counter({
Expand Down Expand Up @@ -214,26 +191,7 @@ export const rpcFactory = ({
requested.headers.get('Content-Type') ?? 'application/json',
);
if (requested.body) {
const sizeLimit = createSizeLogger(maxResponseSize);
const readableStream = Readable.fromWeb(
requested.body as ReadableStream,
);
readableStream
.pipe(sizeLimit)
.on('error', (error) => {
if (error instanceof SizeTooLargeError) {
console.warn(
`[rpcFactory] RPC response too large: ${JSON.stringify(requests)}`,
);
// Payload Too Large
res.status(413).end();
} else {
res.statusCode = 500;
res.end(DEFAULT_API_ERROR_MESSAGE);
}
readableStream.destroy();
})
.pipe(res);
Readable.fromWeb(requested.body as ReadableStream).pipe(res);
} else {
res
.status(requested.status)
Expand Down

0 comments on commit 4a8275e

Please sign in to comment.