Skip to content

Commit

Permalink
chore: added comments to clarify the logic
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
  • Loading branch information
quiet-node committed Jan 27, 2025
1 parent e0cd6be commit cf8685c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/relay/src/lib/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit cf8685c

Please sign in to comment.