Add limit on getLogs results size

This commit is contained in:
Prathamesh Musale 2024-09-16 18:44:21 +05:30
parent 2638756855
commit 349a913575
4 changed files with 20 additions and 3 deletions

View File

@ -28,6 +28,9 @@
# Enable ETH JSON RPC server at /rpc
enableEthRPCServer = true
// Max number of logs that can be returned in a single getLogs request (default: 10000)
ethGetLogsResultLimit = 10000
# Server GQL config
[server.gql]
path = "/graphql"

View File

@ -255,6 +255,9 @@ export interface ServerConfig {
// Enable ETH JSON RPC server at /rpc
enableEthRPCServer: boolean;
// Max number of logs that can be returned in a single getLogs request
ethGetLogsResultLimit?: number;
}
export interface FundingAmountsConfig {

View File

@ -30,3 +30,5 @@ export const DEFAULT_PREFETCH_BATCH_SIZE = 10;
export const DEFAULT_MAX_GQL_CACHE_SIZE = Math.pow(2, 20) * 8; // 8 MB
export const SUPPORTED_PAID_RPC_METHODS = ['eth_getBlockByHash', 'eth_getStorageAt', 'eth_getBlockByNumber'];
export const DEFAULT_ETH_GET_LOGS_RESULT_LIMIT = 10000;

View File

@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { utils } from 'ethers';
import { Between, Equal, FindConditions, In, LessThanOrEqual, MoreThanOrEqual } from 'typeorm';
import { JsonRpcProvider } from '@ethersproject/providers';
import { EventInterface, IndexerInterface } from './types';
import { DEFAULT_ETH_GET_LOGS_RESULT_LIMIT } from './constants';
const CODE_INVALID_PARAMS = -32602;
const CODE_INTERNAL_ERROR = -32603;
@ -20,6 +20,7 @@ const ERROR_INVALID_BLOCK_TAG = 'Invalid block tag';
const ERROR_INVALID_BLOCK_HASH = 'Invalid block hash';
const ERROR_BLOCK_NOT_FOUND = 'Block not found';
const ERROR_TOPICS_FILTER_NOT_SUPPORTED = 'Topics filter not supported';
const ERROR_LIMIT_EXCEEDED = 'Query results exceeds limit';
const DEFAULT_BLOCK_TAG = 'latest';
@ -121,7 +122,9 @@ export const createEthRPCHandlers = async (
// Address filter, address or a list of addresses
if (params.address) {
if (Array.isArray(params.address)) {
where.contract = In(params.address);
if (params.address.length > 0) {
where.contract = In(params.address);
}
} else {
where.contract = Equal(params.address);
}
@ -155,7 +158,13 @@ export const createEthRPCHandlers = async (
// Fetch events from the db
// Load block relation
const events = await indexer.getEvents({ where, relations: ['block'] });
const resultLimit = indexer.serverConfig.ethGetLogsResultLimit || DEFAULT_ETH_GET_LOGS_RESULT_LIMIT;
const events = await indexer.getEvents({ where, relations: ['block'], take: resultLimit + 1 });
// Limit number of results can be returned by a single query
if (events.length > resultLimit) {
throw new ErrorWithCode(CODE_SERVER_ERROR, `${ERROR_LIMIT_EXCEEDED}: ${resultLimit}`);
}
// Transform events into result logs
const result = await transformEventsToLogs(events);