Skip to content

Commit

Permalink
Add limit on getLogs results size
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Sep 16, 2024
1 parent 2638756 commit 349a913
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/codegen/src/templates/config-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions packages/util/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions packages/util/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 12 additions & 3 deletions packages/util/src/eth-rpc-handlers.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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';

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 349a913

Please sign in to comment.