Skip to content
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

feat: enhanced eth_getLogs with timestamp range validation and new error handling #3431

Merged
Prev Previous commit
Next Next commit
chore: added comments to clarify the logic
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
quiet-node committed Jan 27, 2025
commit cf8685c38181d0b38039753c5532a85457966038
31 changes: 31 additions & 0 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
@@ -2767,6 +2767,37 @@ export class EthImpl implements Eth {
return await this.getAcccountNonceFromContractResult(address, blockNum, requestDetails);
}

/**
* Retrieves logs based on the provided parameters.
*
* The function handles log retrieval as follows:
*
* - Using `blockHash`:
* - If `blockHash` is provided, logs are retrieved based on the timestamp of the block associated with the `blockHash`.
*
* - Without `blockHash`:
*
* - If only `fromBlock` is provided:
* - Logs are retrieved from `fromBlock` to the latest block.
* - If `fromBlock` does not exist, an empty array is returned.
*
* - If only `toBlock` is provided:
* - A predefined error `MISSING_FROM_BLOCK_PARAM` is thrown because `fromBlock` is required.
*
* - If both `fromBlock` and `toBlock` are provided:
* - Logs are retrieved from `fromBlock` to `toBlock`.
* - If `toBlock` does not exist, an empty array is returned.
* - If the timestamp range between `fromBlock` and `toBlock` exceeds 7 days, a predefined error `TIMESTAMP_RANGE_TOO_LARGE` is thrown.
*
* @param {string | null} blockHash - The block hash to prioritize log retrieval.
* @param {string | 'latest'} fromBlock - The starting block for log retrieval.
* @param {string | 'latest'} toBlock - The ending block for log retrieval.
* @param {string | string[] | null} address - The address(es) to filter logs by.
* @param {any[] | null} topics - The topics to filter logs by.
* @param {RequestDetails} requestDetails - The details of the request.
* @returns {Promise<Log[]>} - A promise that resolves to an array of logs or an empty array if no logs are found.
* @throws {Error} Throws specific errors like `MISSING_FROM_BLOCK_PARAM` or `TIMESTAMP_RANGE_TOO_LARGE` when applicable.
*/
async getLogs(
blockHash: string | null,
fromBlock: string | 'latest',
Original file line number Diff line number Diff line change
@@ -140,6 +140,13 @@ export class CommonService implements ICommonService {
} else {
fromBlockNum = parseInt(fromBlockResponse.number);
const toBlockResponse = await this.getHistoricalBlockResponse(requestDetails, toBlock, true);

/**
* If `toBlock` is not provided, the `lte` field cannot be set,
* resulting in a request to the Mirror Node that includes only the `gte` parameter.
* Such requests will be rejected, hence causing the whole request to fail.
* Return false to handle this gracefully and return an empty response to end client.
*/
if (!toBlockResponse) {
return false;
}