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

fix: pagination returns less than the page size when decryption fails #1547

Merged
merged 22 commits into from
Jan 22, 2025

Conversation

rodrigopavezi
Copy link
Member

@rodrigopavezi rodrigopavezi commented Jan 17, 2025

Problem

First pagination implementation has performance issues and it will not work for request which cannot be decrypted. For instance when requesting 10 encrypted requests there might be requests that cannot be decrypted so it would return less than 10. That would cause confusion to the consumer.

Changes

Move the pagination logic to upper level so it will return the right number of valid requests.

Fixes #1382

Summary by CodeRabbit

Release Notes

  • Refactor

    • Removed pagination parameters from multiple data access methods across various components.
    • Simplified channel and transaction retrieval processes.
    • Updated method signatures to focus on core retrieval parameters.
  • Types

    • Modified type definitions to remove pagination-related properties.
    • Enhanced metadata structures to support optional pagination information.
  • Performance

    • Streamlined data retrieval methods.
    • Removed complex pagination logic from multiple services.
  • New Features

    • Introduced a new property for pagination information in the metadata of certain methods.
    • Enhanced handling of responses to ensure consistent data structures across methods.

- Simplified `getChannelsByTopic` and `getChannelsByMultipleTopics` methods by removing pagination parameters (page and pageSize) across various data access layers.
- Updated related methods and interfaces to reflect this change, ensuring consistency in data retrieval.
- Adjusted tests and API handlers to accommodate the new method signatures.

This refactor aims to streamline data access and improve code maintainability.
Copy link
Contributor

coderabbitai bot commented Jan 17, 2025

Walkthrough

This pull request introduces a significant simplification of pagination-related methods across multiple packages in the Request Network project. The changes primarily involve removing page and pageSize parameters from methods like getChannelsByTopic and getChannelsByMultipleTopics in various classes and interfaces. This modification affects data access, transaction management, and type definitions, streamlining the approach to retrieving channels and transactions by removing explicit pagination controls.

Changes

File Change Summary
packages/data-access/src/combined-data-access.ts Removed pagination parameters from getChannelsByTopic and getChannelsByMultipleTopics methods.
packages/data-access/src/data-read.ts Simplified methods by removing pagination logic and parameters.
packages/data-access/src/in-memory-indexer.ts Removed pagination parameters from getTransactionsByTopics method.
packages/request-client.js/src/api/request-network.ts Updated return types to support optional pagination metadata.
packages/request-client.js/src/http-data-access.ts Removed pagination parameters from channel retrieval methods.
packages/thegraph-data-access/src/queries.ts Modified GraphQL queries to remove pagination-related parameters.
packages/transaction-manager/src/transaction-manager.ts Restructured channel processing with new pagination handling methods.
packages/types/src/data-access-types.ts Removed pagination parameters from interfaces.
packages/types/src/storage-types.ts Updated IIndexer interface to remove pagination parameters.

Assessment against linked issues

Objective Addressed Explanation
Add pagination to the Request Node /getTransactionsByChannelId endpoint (#1382) The changes remove pagination parameters from various methods, which does not address the objective of adding pagination.

Possibly related PRs

  • feat: pagination #1490: This PR introduces pagination functionality to the getChannelsByTopic and getChannelsByMultipleTopics methods, which directly relates to the changes in the main PR that remove pagination parameters from these methods.
  • feat: allow no persist for Custom Data Access #1541: This PR enhances the DataAccess class to allow custom implementations to determine whether to persist data, which aligns with the main PR's focus on modifying the CombinedDataAccess class and its methods.

Suggested reviewers

  • kevindavee
  • alexandre-abrioux
  • aimensahnoun
  • sstefdev
  • yomarion
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@rodrigopavezi
Copy link
Member Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jan 17, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (9)
packages/transaction-manager/src/transaction-manager.ts (3)

230-326: Consider adding comprehensive unit tests for the new pagination logic.

The parseMultipleChannels method now incorporates complex pagination logic, including sorting and filtering channels. To ensure correctness and reliability, especially with various edge cases (e.g., empty channels, large datasets, undefined page or pageSize), add unit tests that cover these scenarios.


328-332: Enhance type safety by specifying concrete types instead of any.

The use of any[] in isValidChannel reduces type safety and may lead to runtime errors. Replace any[] with specific types to improve code reliability and maintainability.

Apply this diff to improve type annotations:

-private isValidChannel(cleaned: { transactions: any[]; ignoredTransactions: any[] }): boolean {
+private isValidChannel(cleaned: { transactions: TransactionTypes.ITransaction[]; ignoredTransactions: TransactionTypes.ITransaction[] }): boolean {

359-363: Avoid unnecessary type assertions to improve code clarity.

The double casting using as unknown as may hide potential type issues and makes the code less readable. Ensure that cleaned.transactions and cleaned.ignoredTransactions are correctly typed to eliminate the need for such assertions.

Apply this diff to remove unnecessary casts:

-result.transactions[channelId] =
-  cleaned.transactions as unknown as TransactionTypes.ITransaction[];
-result.ignoredTransactions[channelId] =
-  cleaned.ignoredTransactions as unknown as TransactionTypes.ITransaction[];
+result.transactions[channelId] = cleaned.transactions;
+result.ignoredTransactions[channelId] = cleaned.ignoredTransactions;

If cleaned does not have the correct types, consider updating its type definition accordingly.

packages/data-access/src/in-memory-indexer.ts (1)

Line range hint 58-71: Consider implementing batch processing for large datasets.

While using Set for deduplication is efficient, processing all transactions at once could consume significant memory for large datasets.

Consider implementing batch processing:

 async getTransactionsByTopics(topics: string[]): Promise<StorageTypes.IGetTransactionsResponse> {
   const channelIdsSet = new Set(topics.flatMap((topic) => this.#topicToChannelsIndex.get(topic)));
   const channelIds = Array.from(channelIdsSet);
 
+  // Process locations in batches
+  const BATCH_SIZE = 100;
   const locations = channelIds
     .map((channel) => this.#channelToLocationsIndex.get(channel))
     .flat();
 
-  const transactions = await this.parseDocuments(locations);
+  const transactions = [];
+  for (let i = 0; i < locations.length; i += BATCH_SIZE) {
+    const batch = locations.slice(i, i + BATCH_SIZE);
+    const batchTransactions = await this.parseDocuments(batch);
+    transactions.push(...batchTransactions);
+  }
 
   return {
     blockNumber: 0,
     transactions,
   };
 }
packages/data-access/src/data-read.ts (1)

73-88: Consider memory usage when handling large transaction sets.

The new implementation loads all transactions into memory before filtering. While this simplifies the logic, it could impact performance with large datasets.

Consider implementing streaming or chunked processing if memory usage becomes a concern with large transaction sets.

packages/request-client.js/src/api/request-network.ts (1)

398-407: Consider extracting common pagination logic.

The pagination handling logic is duplicated across multiple methods. Consider extracting it into a utility function.

private wrapWithPaginationMeta<T>(
  data: T[],
  meta: RequestLogicTypes.IReturnMeta,
  options?: { page?: number; pageSize?: number }
): T[] | { meta: RequestLogicTypes.IReturnGetRequestsByTopic['meta']; requests: T[] } {
  if (options?.page && options?.pageSize) {
    return { requests: data, meta };
  }
  return data;
}

Usage:

- if (options?.page && options?.pageSize) {
-   return {
-     requests,
-     meta: requestsAndMeta.meta,
-   };
- } else {
-   return requests;
- }
+ return this.wrapWithPaginationMeta(requests, requestsAndMeta.meta, options);

Also applies to: 473-481

packages/request-logic/src/request-logic.ts (2)

629-629: LGTM! Consider adding type safety for pagination.

The addition of pagination metadata from transactionManagerMeta aligns with moving pagination logic to a higher level.

Consider adding type safety by explicitly typing the pagination property:

-          pagination: transactionManagerMeta.pagination,
+          pagination: transactionManagerMeta.pagination as { hasMore: boolean; nextPage: number },

651-655: LGTM! Consider handling undefined pagination.

The spread operator usage correctly merges pagination metadata while preserving existing data.

Consider handling cases where pagination might be undefined:

-          finalResult.meta.pagination = {
-            ...finalResult.meta.pagination,
-            ...requestAndMeta.pagination,
-          };
+          if (requestAndMeta.pagination) {
+            finalResult.meta.pagination = {
+              ...finalResult.meta.pagination,
+              ...requestAndMeta.pagination,
+            };
+          }
packages/integration-test/test/node-client.test.ts (1)

276-276: LGTM! Consider using type inference.

The explicit type annotations improve type safety and clarity.

Consider leveraging TypeScript's type inference by using as const and letting the compiler infer the type:

-    let requests: Request[] = (await requestNetwork.fromTopic(topicsRequest1and2[0])) as Request[];
+    let requests = await requestNetwork.fromTopic(topicsRequest1and2[0]) as const;

Also applies to: 289-291, 344-346

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 59c889d and 06a0d9f.

📒 Files selected for processing (14)
  • packages/data-access/src/combined-data-access.ts (1 hunks)
  • packages/data-access/src/data-read.ts (2 hunks)
  • packages/data-access/src/in-memory-indexer.ts (1 hunks)
  • packages/integration-test/test/node-client.test.ts (3 hunks)
  • packages/request-client.js/src/api/request-network.ts (6 hunks)
  • packages/request-client.js/src/http-data-access.ts (2 hunks)
  • packages/request-logic/src/request-logic.ts (2 hunks)
  • packages/request-node/src/request/getChannelsByTopic.ts (1 hunks)
  • packages/thegraph-data-access/src/queries.ts (2 hunks)
  • packages/thegraph-data-access/src/subgraph-client.ts (1 hunks)
  • packages/transaction-manager/src/transaction-manager.ts (4 hunks)
  • packages/types/src/data-access-types.ts (0 hunks)
  • packages/types/src/request-logic-types.ts (1 hunks)
  • packages/types/src/storage-types.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/types/src/data-access-types.ts
🧰 Additional context used
📓 Learnings (1)
packages/data-access/src/data-read.ts (1)
Learnt from: MantisClone
PR: RequestNetwork/requestNetwork#1508
File: packages/data-access/src/data-read.ts:115-134
Timestamp: 2024-12-04T17:31:54.981Z
Learning: In `packages/data-access/src/data-read.ts`, within the `getChannelsByMultipleTopics` method, including transactions outside the `updatedBetween` range when retrieving all transactions for the identified channels is intentional and expected behavior, as specified in the code comments.
🔇 Additional comments (10)
packages/transaction-manager/src/transaction-manager.ts (2)

199-201: Pagination logic moved to parseMultipleChannels method.

The removal of page and pageSize from the dataAccess.getChannelsByTopic call and passing them to parseMultipleChannels aligns with the new pagination handling strategy. Ensure that this change does not affect the expected behavior and that parseMultipleChannels correctly handles pagination.


373-375: Verify the correctness of totalValidChannels calculation.

The formula used in calculateTotalValidChannels may lead to inaccurate totals if processedCount does not properly reflect the number of channels processed. Ensure that this calculation accurately represents the total number of valid channels, especially when paginating results.

Consider adding tests or using the following script to check the calculation logic:

✅ Verification successful

The totalValidChannels calculation is mathematically sound and efficient.

The implementation correctly handles both paginated and non-paginated scenarios:

  • Without pagination: Returns exact count of valid channels
  • With pagination: Uses statistical extrapolation based on the success rate of processed channels, which is an efficient approach for large datasets where processing all channels would be expensive
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify totalValidChannels calculation logic.

# Expected: The total number of valid channels should be consistent with the actual number of valid channels.

# Command to check the calculation (this is a placeholder and may require actual implementation context).
echo "Please review the calculation of totalValidChannels in calculateTotalValidChannels method."

Length of output: 190


Script:

#!/bin/bash
# Find the complete implementation of the method
echo "=== Method Implementation ==="
ast-grep --pattern 'calculateTotalValidChannels($_) {
  $$$
}'

echo -e "\n=== Test Files ==="
fd -e test.ts -e spec.ts -e test.js -e spec.js --exec grep -l "calculateTotalValidChannels"

echo -e "\n=== Method Usage ==="
rg "calculateTotalValidChannels" -A 2

Length of output: 916


Script:

#!/bin/bash
# Get the complete file content
echo "=== Complete File Content ==="
cat packages/transaction-manager/src/transaction-manager.ts

echo -e "\n=== Method Implementation with Context ==="
ast-grep --pattern 'private calculateTotalValidChannels(
  $_,
  $_,
  $_
) {
  $$$
}'

Length of output: 14220

packages/request-node/src/request/getChannelsByTopic.ts (1)

18-26: Removal of pagination parameters from request handling is appropriate.

The elimination of page and pageSize from query parameters simplifies the handler and aligns with the updated data access methods. Ensure that client applications are updated accordingly to reflect this change.

packages/data-access/src/combined-data-access.ts (1)

35-35: Updated method signatures reflect the new pagination approach.

The removal of page and pageSize from getChannelsByTopic and getChannelsByMultipleTopics aligns with the shift of pagination logic to higher levels of the application. This simplifies the data access layer and focuses on retrieving all relevant data.

Also applies to: 41-41

packages/thegraph-data-access/src/queries.ts (2)

110-119: LGTM! Efficient implementation of the count query.

The count query is well-implemented as it only retrieves the necessary id field, making it efficient for large datasets.


80-89: Consider adding a safety limit to prevent excessive data retrieval.

While removing pagination from the GraphQL query aligns with the PR objectives, fetching all transactions without limits could lead to performance issues if a channel contains many transactions.

Consider either:

  1. Adding a maximum limit in the GraphQL query
  2. Implementing cursor-based pagination instead of offset pagination
 query GetTransactionsByTopics($topics: [String!]!) {
   ${metaQueryBody}
   channels(
     where: { topics_contains: $topics }
   ){
     transactions(
       orderBy: blockTimestamp, 
-      orderDirection: asc
+      orderDirection: asc,
+      first: 1000  # Add a reasonable maximum limit
     ) {
       ...TransactionsBody
     }
   }
 }
packages/data-access/src/data-read.ts (2)

51-53: LGTM! Method signature simplified.

The removal of pagination parameters aligns with the PR objective to handle pagination at a higher level.


90-97: Verify transaction ordering within channels.

The grouping logic maintains the chronological order of transactions within each channel due to the prior sorting step. However, ensure this ordering is required by consumers of this API.

✅ Verification successful

Transaction ordering is correctly implemented and required.

The chronological ordering of transactions is explicitly maintained by sorting by blockTimestamp before grouping, and this order is required by the request logic for correct state processing.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for code that relies on transaction ordering within channels
rg -A 3 "transactionsByChannel|transactions: \{" --type ts

Length of output: 9623


Script:

#!/bin/bash
# Search for sorting operations in data-read.ts
rg -B 10 "transactionsByChannel" packages/data-access/src/data-read.ts

Length of output: 771


Script:

#!/bin/bash
# Search for allTransactions definition and sorting operations
rg -B 5 "allTransactions" packages/data-access/src/data-read.ts

Length of output: 658

packages/types/src/storage-types.ts (1)

63-63: LGTM! Interface simplified.

The removal of pagination parameters from getTransactionsByTopics is consistent with moving pagination handling to a higher level.

packages/request-client.js/src/api/request-network.ts (1)

297-299: LGTM! Return type enhancement supports pagination metadata.

The union type allows for backward compatibility while supporting pagination metadata when needed.

Also applies to: 322-324, 352-354, 426-428

@rodrigopavezi rodrigopavezi marked this pull request as ready for review January 21, 2025 01:38
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🔭 Outside diff range comments (2)
packages/request-client.js/test/api/request-network.test.ts (2)

Line range hint 230-235: Remove unused 'page' and 'pageSize' parameters from mock 'getChannelsByMultipleTopics'

The getChannelsByMultipleTopics method no longer accepts page and pageSize parameters. Update the mock implementation to reflect the updated method signature.

Apply this diff to update the method signature:

             async getChannelsByMultipleTopics(
               topics: [string],
               updatedBetween?: DataAccessTypes.ITimestampBoundaries,
-              page?: number,
-              pageSize?: number,
             ): Promise<any> {

Line range hint 301-306: Remove unused 'page' and 'pageSize' parameters from mock 'getChannelsByMultipleTopics'

Update the mock implementation of getChannelsByMultipleTopics to remove the page and pageSize parameters, reflecting the updated method signature.

Apply this diff to update the method signature:

             async getChannelsByMultipleTopics(): Promise<any> {
               return {
                 meta: {
                   [TestData.actionRequestId]: [],
                   [TestData.actionRequestIdSecondRequest]: [],
-                  transactionsStorageLocation: {},
                 },
🧹 Nitpick comments (12)
packages/transaction-manager/test/index.test.ts (8)

49-53: Specify a precise type for 'pagination' instead of setting it to undefined

Setting pagination to undefined without a clear type reduces clarity and type safety. Consider specifying the correct type for pagination and providing meaningful initial values.

Apply this diff to define the type and initialize pagination properly:

       meta: {
-        pagination: undefined,
+        pagination: {},
         storageMeta: {},
         transactionsStorageLocation: { [channelId]: ['fakeDataId1', 'fakeDataId2'] },
       },

1137-1143: Update assertions to include 'pagination' in metadata

Since pagination was added to the meta object, update the assertions in the test to include and verify the pagination property.

Apply this diff to update the assertions:

       expect(ret.meta).toEqual({
         dataAccessMeta: fakeMetaDataAccessGetChannelsReturn.meta,
         ignoredTransactions: {
           '01a98f126de3fab2b5130af5161998bf6e59b2c380deafeff938ff3f798281bf23': [null, null],
         },
+        pagination: undefined,
       });

1156-1157: Remove unnecessary 'pagination' property or provide a proper type

The pagination property is set to undefined in the test metadata. If pagination is not used in this context, consider removing it; otherwise, provide it with a proper value and type.

Apply this diff:

       meta: {
-        pagination: undefined,
+        // pagination: { page: 1, pageSize: 10 },
         storageMeta: {},
         transactionsStorageLocation: {
           [channelId]: ['fakeDataId1'],
         },

1201-1213: Update test expectations to include 'pagination' in metadata

Adjust the test assertions to include the pagination property in the meta object since it's now part of the returned data.

Apply this diff to the assertions:

       expect(ret.meta).toEqual({
         dataAccessMeta: {
+          pagination: undefined,
           storageMeta: {},
           transactionsStorageLocation: {
             [channelId]: ['fakeDataId1'],
           },
         },
         ignoredTransactions: {
           [channelId]: [null],
         },
       });

1353-1376: Include 'pagination' in test metadata and assertions

Update the test metadata and corresponding assertions to include the pagination property, ensuring consistency with the updated data structures.

Adjust the metadata as follows:

       expect(ret.meta).toEqual({
         dataAccessMeta: {
+          pagination: undefined,
           storageMeta: {},
           transactionsStorageLocation: {
             [channelId]: ['fakeDataId1', 'fakeDataId2'],
           },
         },
         ignoredTransactions: {
           [channelId]: [
             {
               reason: 'No decryption or cipher provider given',
               transaction: {
                 state: TransactionTypes.TransactionState.PENDING,
                 timestamp: 1,
                 transaction: encryptedTx,
               },
             },
             null,
           ],
         },
       });

1414-1434: Adjust test expectations to account for 'pagination' in metadata

Update the assertions in the test to include the pagination property within the dataAccessMeta object.

Apply this diff to the assertions:

       expect(ret.meta).toEqual({
         dataAccessMeta: {
+          pagination: undefined,
           storageMeta: {},
           transactionsStorageLocation: {
             [channelId]: ['fakeDataId1', 'fakeDataId1', 'fakeDataId2'],
           },
         },
         ignoredTransactions: {
           [channelId]: [
             {
               reason:
                 'as first transaction, the hash of the transaction do not match the channelId',
               transaction: txWrongHash,
             },
             null,
             null,
           ],
         },
       });

1500-1517: Include 'pagination' in test metadata and verify its correctness

Ensure that the pagination property is included in the meta object and that the test assertions verify its presence and correctness.

Update the test metadata and assertions:

       expect(ret.meta).toEqual({
         dataAccessMeta: {
+          pagination: undefined,
           storageMeta: {},
           transactionsStorageLocation: {
             [channelId]: ['fakeDataId1'],
             [channelId2]: ['fakeDataId2'],
           },
         },
         ignoredTransactions: {
           [channelId]: [null],
           [channelId2]: [null],
         },
       });

1530-1535: Update assertions to reflect added 'pagination' in metadata

Since pagination is now part of the meta object, update the test assertions to include and verify it.

Modify the assertions:

       expect(ret.meta).toEqual({
         dataAccessMeta: fakeMetaDataAccessGetChannelsReturn.meta,
         ignoredTransactions: {
           '01a98f126de3fab2b5130af5161998bf6e59b2c380deafeff938ff3f798281bf23': [null, null],
         },
+        pagination: undefined,
       });
packages/transaction-manager/src/transaction-manager.ts (1)

365-369: Improve channel validation logic.

The validation could be more robust by checking for additional conditions.

   private isValidChannel(cleaned: { transactions: any[]; ignoredTransactions: any[] }): boolean {
-    return (
-      cleaned.transactions && cleaned.transactions.length > 0 && cleaned.transactions[0] !== null
-    );
+    return Boolean(
+      cleaned.transactions?.length > 0 &&
+      cleaned.transactions[0] !== null &&
+      cleaned.transactions[0] !== undefined
+    );
   }
packages/request-client.js/test/index.test.ts (3)

861-864: LGTM! Consider adding error handling test cases.

The response handling for fromTopic is well-structured and handles both array and object responses consistently.

Consider adding test cases for error scenarios:

it('handles error responses from fromTopic', async () => {
  // Test case for network errors
  // Test case for invalid response format
  // Test case for empty response
});

902-909: LGTM! Consider adding pagination test cases.

The response handling for fromMultipleTopics follows the same consistent pattern and properly handles the response structure.

Consider adding test cases for pagination scenarios:

it('handles pagination in fromMultipleTopics response', async () => {
  // Test case for response with next page
  // Test case for response with previous page
  // Test case for response with both next and previous pages
});

935-938: LGTM! Consider adding validation test cases.

The response handling for fromIdentity maintains consistency with the other methods.

Consider adding test cases for identity validation:

it('validates identity in fromIdentity response', async () => {
  // Test case for invalid identity format
  // Test case for non-existent identity
  // Test case for unauthorized identity
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 06a0d9f and dfb22be.

📒 Files selected for processing (7)
  • packages/data-access/src/data-read.ts (1 hunks)
  • packages/request-client.js/src/http-data-read.ts (0 hunks)
  • packages/request-client.js/test/api/request-network.test.ts (8 hunks)
  • packages/request-client.js/test/index.test.ts (3 hunks)
  • packages/transaction-manager/src/transaction-manager.ts (4 hunks)
  • packages/transaction-manager/test/index.test.ts (13 hunks)
  • packages/types/src/data-access-types.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/request-client.js/src/http-data-read.ts
🧰 Additional context used
📓 Learnings (1)
packages/data-access/src/data-read.ts (1)
Learnt from: MantisClone
PR: RequestNetwork/requestNetwork#1508
File: packages/data-access/src/data-read.ts:115-134
Timestamp: 2024-12-04T17:31:54.981Z
Learning: In `packages/data-access/src/data-read.ts`, within the `getChannelsByMultipleTopics` method, including transactions outside the `updatedBetween` range when retrieving all transactions for the identified channels is intentional and expected behavior, as specified in the code comments.
🔇 Additional comments (9)
packages/request-client.js/test/api/request-network.test.ts (1)

175-178: ⚠️ Potential issue

Remove unused 'page' and 'pageSize' parameters from mock 'getChannelsByTopic'

Ensure the getChannelsByTopic mock implementation does not include obsolete parameters page and pageSize.

Apply this diff:

             async getChannelsByTopic(): Promise<any> {
               return {
                 meta: {
                   [TestData.actionRequestId]: [],
                   [TestData.actionRequestIdSecondRequest]: [],
-                  transactionsStorageLocation: {},
                 },

Likely invalid or redundant comment.

packages/transaction-manager/test/index.test.ts (3)

970-974: Ensure consistent handling of encrypted transactions in tests

The test case mixes encrypted and clear transactions, which might not accurately reflect expected behavior. Verify that the test correctly handles cases where clear transactions follow encrypted ones and adjust the expectations accordingly.

Review the test logic and ensure it aligns with the expected behavior when dealing with encrypted channels.


1261-1290: Clarify handling of ignored transactions in metadata

In the absence of a decryption provider, the transactions are ignored, and the reason is specified. Ensure that the ignoredTransactions and meta objects accurately reflect this and include pagination if applicable.

Review the test output for correctness and completeness, ensuring consistent inclusion of pagination and clear reasons for ignored transactions.


1546-1568: Ensure 'pagination' is correctly initialized in test mock data

In the fakeMetaDataAccessGetChannelsReturn object, the pagination property is provided with values. Ensure this is intentional and consistent throughout the tests.

Review the test data to confirm that pagination is correctly used and reflects realistic pagination metadata.

packages/data-access/src/data-read.ts (2)

53-53: LGTM! Simplified method implementation.

The implementation correctly delegates to getChannelsByMultipleTopics with a single topic, following the DRY principle.


60-60: LGTM! Simplified method signature.

The removal of pagination parameters aligns with the PR objectives to move pagination logic to a higher level.

packages/transaction-manager/src/transaction-manager.ts (3)

199-201: LGTM! Simplified method implementation.

The implementation correctly moves pagination handling to parseMultipleChannels.


217-219: LGTM! Simplified method implementation.

The implementation correctly moves pagination handling to parseMultipleChannels.


297-312: LGTM! Well-structured pagination implementation.

The pagination logic is clear and handles edge cases correctly.

@MantisClone MantisClone changed the title fix: pagination fix: pagination returns less than the page size when decryption fails Jan 21, 2025
@MantisClone
Copy link
Member

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Jan 21, 2025

✅ Actions performed

Full review triggered.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
packages/transaction-manager/src/transaction-manager.ts (2)

365-369: Ensure all transactions are valid when determining if a channel is valid

The isValidChannel method currently checks only the first transaction for null. It's safer to verify that all transactions are not null to accurately determine the validity of a channel.

Apply this diff to check all transactions:

-      return (
-        cleaned.transactions && cleaned.transactions.length > 0 && cleaned.transactions[0] !== null
-      );
+      return (
+        cleaned.transactions &&
+        cleaned.transactions.length > 0 &&
+        cleaned.transactions.every((tx) => tx !== null)
+      );

265-295: Optimize channel processing to avoid redundant decryption

Currently, decryptAndCleanChannel is called twice for the same channels: once to check validity and again during processing. This can be inefficient, especially with a large number of channels.

Consider storing the result of the initial decryption to reuse later, reducing unnecessary computations.

packages/thegraph-data-access/src/queries.ts (2)

80-80: Potential performance impact due to removal of pagination

By removing the $first and $skip pagination parameters, the GetTransactionsByTopics query might retrieve a large number of transactions in a single request, which could affect performance and exceed query limits.

Consider reintroducing pagination to manage the volume of data returned and to optimize query performance.


110-119: Optimize transaction count retrieval

The GetTransactionsByTopicsCount query fetches all transaction IDs to determine the count, which can be inefficient for topics with many transactions.

If supported, modify the query to directly return the count without fetching all transaction IDs. This can improve performance by reducing the amount of data transferred.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 59d5257 and 1ecf230.

📒 Files selected for processing (18)
  • packages/data-access/src/combined-data-access.ts (1 hunks)
  • packages/data-access/src/data-read.ts (1 hunks)
  • packages/data-access/src/in-memory-indexer.ts (1 hunks)
  • packages/integration-test/test/node-client.test.ts (3 hunks)
  • packages/request-client.js/src/api/request-network.ts (6 hunks)
  • packages/request-client.js/src/http-data-access.ts (2 hunks)
  • packages/request-client.js/src/http-data-read.ts (0 hunks)
  • packages/request-client.js/test/api/request-network.test.ts (8 hunks)
  • packages/request-client.js/test/index.test.ts (3 hunks)
  • packages/request-logic/src/request-logic.ts (2 hunks)
  • packages/request-node/src/request/getChannelsByTopic.ts (1 hunks)
  • packages/thegraph-data-access/src/queries.ts (2 hunks)
  • packages/thegraph-data-access/src/subgraph-client.ts (1 hunks)
  • packages/transaction-manager/src/transaction-manager.ts (4 hunks)
  • packages/transaction-manager/test/index.test.ts (13 hunks)
  • packages/types/src/data-access-types.ts (1 hunks)
  • packages/types/src/request-logic-types.ts (1 hunks)
  • packages/types/src/storage-types.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/request-client.js/src/http-data-read.ts
🧰 Additional context used
📓 Learnings (4)
packages/request-client.js/test/api/request-network.test.ts (1)
Learnt from: rodrigopavezi
PR: RequestNetwork/requestNetwork#1547
File: packages/request-client.js/test/api/request-network.test.ts:267-273
Timestamp: 2025-01-21T01:54:03.941Z
Learning: In the RequestNetwork class, pagination functionality is supported in methods like fromMultipleIdentities, fromTopic, etc., with options for page and pageSize. The pagination was moved to a higher level but not removed.
packages/data-access/src/data-read.ts (1)
Learnt from: MantisClone
PR: RequestNetwork/requestNetwork#1508
File: packages/data-access/src/data-read.ts:115-134
Timestamp: 2024-12-04T17:31:54.981Z
Learning: In `packages/data-access/src/data-read.ts`, within the `getChannelsByMultipleTopics` method, including transactions outside the `updatedBetween` range when retrieving all transactions for the identified channels is intentional and expected behavior, as specified in the code comments.
packages/types/src/data-access-types.ts (1)
Learnt from: rodrigopavezi
PR: RequestNetwork/requestNetwork#1547
File: packages/types/src/data-access-types.ts:95-95
Timestamp: 2025-01-21T01:49:12.846Z
Learning: In the RequestNetwork codebase, the `pagination` property in `IReturnGetChannelsByTopic` interface is intentionally typed as `any` to maintain flexibility. A more specific type can be considered in future improvements.
packages/request-client.js/src/api/request-network.ts (1)
Learnt from: rodrigopavezi
PR: RequestNetwork/requestNetwork#1547
File: packages/request-client.js/test/api/request-network.test.ts:147-150
Timestamp: 2025-01-21T01:54:29.040Z
Learning: In the RequestNetwork class, methods like `fromIdentity`, `fromTopic`, `fromMultipleIdentities`, and `fromMultipleTopics` support pagination options and can return either an array of Request objects or an object containing both requests and pagination metadata.
🔇 Additional comments (25)
packages/transaction-manager/src/transaction-manager.ts (1)

291-294: Consider improving error handling and logging

The error handling in the catch block could be more informative and utilize a proper logging mechanism instead of console.warn. This can help in better monitoring and debugging of issues in production environments.

packages/data-access/src/combined-data-access.ts (1)

35-35: Simplification of method interfaces looks good

The removal of pagination parameters from getChannelsByTopic and getChannelsByMultipleTopics simplifies the method signatures and aligns with the updated pagination handling.

Also applies to: 41-41

packages/data-access/src/in-memory-indexer.ts (1)

Line range hint 58-71: LGTM! Simplified transaction retrieval aligns with architectural changes.

The removal of pagination parameters and simplified implementation aligns with moving pagination logic to a higher level. This change helps ensure correct handling of decrypted requests in paginated results.

Let's verify that pagination is now handled at a higher level:

✅ Verification successful

Verified: Pagination is correctly implemented in TransactionManager

The removal of pagination from the in-memory-indexer is appropriate as pagination is properly handled in the TransactionManager's parseMultipleChannels method, which:

  • Processes and validates all transactions first
  • Applies pagination after decryption and validation
  • Handles both paginated and non-paginated cases efficiently
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that pagination is handled at the transaction manager level
# Expected: Find pagination logic in transaction manager
rg -A 5 "parseMultipleChannels.*page.*pageSize" packages/transaction-manager/src/

Length of output: 1043


Script:

#!/bin/bash
# Description: Examine the implementation of parseMultipleChannels
ast-grep --pattern 'parseMultipleChannels($$$) {
  $$$
}'

Length of output: 59


Script:

#!/bin/bash
# Description: Find parseMultipleChannels implementation using ripgrep
rg -A 15 "private parseMultipleChannels" packages/transaction-manager/src/

Length of output: 76


Script:

#!/bin/bash
# Description: Find parseMultipleChannels implementation with broader search
rg -A 20 "parseMultipleChannels" packages/transaction-manager/src/transaction-manager.ts

Length of output: 2055


Script:

#!/bin/bash
# Description: Get the remaining implementation of parseMultipleChannels
rg -B 5 -A 30 "filteredTransactions," packages/transaction-manager/src/transaction-manager.ts

Length of output: 2499

packages/types/src/data-access-types.ts (1)

95-95: Flexible pagination type maintained.

The any type for pagination is intentionally kept flexible, as confirmed by previous discussions.

packages/request-client.js/src/http-data-access.ts (1)

114-114: Pagination removal aligns with architectural changes.

Removing pagination parameters from HTTP data access is consistent with handling pagination at the transaction manager level.

Also applies to: 127-127

packages/data-access/src/data-read.ts (2)

53-53: LGTM! Clean and efficient implementation.

The simplified implementation correctly delegates to getChannelsByMultipleTopics, maintaining functionality while reducing code duplication.


Line range hint 60-102: LGTM! Robust transaction handling with proper filtering.

The implementation:

  1. Correctly retrieves and combines storage and pending transactions
  2. Properly filters channels based on updatedBetween boundaries
  3. Efficiently constructs the response structure
packages/types/src/storage-types.ts (1)

63-63: Breaking change: Method signature updated in IIndexer interface.

The removal of pagination parameters from getTransactionsByTopics is a breaking change. While this aligns with the PR objective to fix pagination issues, it requires updates in all implementations of the IIndexer interface.

Let's verify the impact:

packages/types/src/request-logic-types.ts (1)

101-101: LGTM: Addition of pagination metadata.

The addition of the pagination property to IReturnMeta aligns with the codebase's design choice of using any type for flexibility.

packages/request-client.js/src/api/request-network.ts (2)

297-299: LGTM: Updated return types to support pagination metadata.

The return type changes consistently handle both paginated and non-paginated responses across all methods.

Also applies to: 322-324, 352-354, 426-428


400-407: LGTM: Consistent pagination handling logic.

The conditional logic ensures that pagination metadata is only included when pagination parameters are provided, maintaining backward compatibility.

Also applies to: 474-481

packages/request-logic/src/request-logic.ts (1)

629-629: LGTM: Proper handling of pagination metadata.

The code correctly extracts and merges pagination metadata from multiple requests, ensuring that pagination information is preserved throughout the request processing chain.

Also applies to: 652-655

packages/request-client.js/test/api/request-network.test.ts (7)

145-149: LGTM! Pagination options are correctly maintained.

The test case correctly verifies that pagination options (page and pageSize) are still supported in the fromIdentity method, which aligns with the retrieved learning that pagination was moved to a higher level but not removed.


202-203: LGTM! Result handling is consistent with the new response structure.

The test case correctly handles the response from fromTopic by checking if the result is an array or an object containing requests, which aligns with the changes to support consistent pagination handling at a higher level.


265-273: LGTM! Pagination options are correctly maintained.

The test case correctly verifies that pagination options (page and pageSize) are still supported in the fromMultipleIdentities method, which aligns with the retrieved learning that pagination was moved to a higher level but not removed.


328-329: LGTM! Result handling is consistent with the new response structure.

The test case correctly handles the response from fromMultipleTopics by checking if the result is an array or an object containing requests, which aligns with the changes to support consistent pagination handling at a higher level.


Line range hint 861-864: LGTM! Result handling is consistent for encrypted requests.

The test case correctly handles the response from fromTopic for encrypted requests by checking if the result is an array or an object containing requests, which ensures consistent pagination handling even with encryption.


Line range hint 902-909: LGTM! Result handling is consistent for encrypted requests with multiple topics.

The test case correctly handles the response from fromMultipleTopics for encrypted requests by checking if the result is an array or an object containing requests, and verifies the data integrity of both requests.


Line range hint 935-938: LGTM! Result handling is consistent for encrypted requests with identity.

The test case correctly handles the response from fromIdentity for encrypted requests by checking if the result is an array or an object containing requests, which ensures consistent pagination handling even with encryption.

packages/request-client.js/test/index.test.ts (3)

861-864: LGTM! Integration test verifies result handling for encrypted requests.

The integration test correctly verifies that the response from fromTopic for encrypted requests is handled consistently, checking if the result is an array or an object containing requests, and validates the decrypted data.


902-909: LGTM! Integration test verifies result handling for encrypted requests with multiple topics.

The integration test correctly verifies that the response from fromMultipleTopics for encrypted requests is handled consistently, checking if the result is an array or an object containing requests, and validates the decrypted data for both requests.


935-938: LGTM! Integration test verifies result handling for encrypted requests with identity.

The integration test correctly verifies that the response from fromIdentity for encrypted requests is handled consistently, checking if the result is an array or an object containing requests, and validates the decrypted data.

packages/integration-test/test/node-client.test.ts (1)

276-277: LGTM! Type assertions improve type safety.

The added type assertions for the requests variable ensure proper typing of the return value from fromTopic calls, making the code more type-safe.

Also applies to: 289-291

packages/transaction-manager/test/index.test.ts (1)

49-53: LGTM! Test expectations updated to match new metadata structure.

The test cases have been properly updated to:

  1. Include pagination and storageMeta fields in metadata
  2. Verify correct handling of decryption failures
  3. Ensure proper error messages in ignored transactions

Also applies to: 1137-1143, 1156-1157, 1201-1213, 1226-1227, 1261-1290, 1293-1376, 1414-1434, 1447-1448, 1500-1517, 1530-1535, 1546-1546, 1552-1568, 1581-1585

packages/thegraph-data-access/src/subgraph-client.ts (1)

70-74: Verify error handling for GraphQL query failures.

Since this change is part of fixing decryption failures, ensure that GraphQL query failures are properly propagated to help diagnose issues.

#!/bin/bash
# Description: Check error handling patterns in GraphQL queries

# Test: Search for error handling patterns in GraphQL queries
echo "Checking error handling in GraphQL queries..."
rg -A 5 "graphql\.request" 

# Test: Search for error types and handling
echo "Checking error types..."
ast-grep --pattern 'catch (e) { $$$  }'

Copy link
Member

@MantisClone MantisClone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved 👍

@rodrigopavezi and I briefly discussed adding more tests. I leave it to @rodrigopavezi to determine how much additional testing to add, if any.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/transaction-manager/src/transaction-manager.ts (3)

235-296: Consider optimizing channel processing and error handling.

  1. The channel processing could be optimized by using array methods instead of multiple iterations
  2. Error handling uses console.warn which may not be appropriate for production

Consider these improvements:

- channelsWithTimestamps = Object.entries(resultGetTx.result.transactions).map(
-   ([channelId, transactions]) => {
-     const filteredTransactions = transactions.filter((tx) => {
-       const timestamp = tx.timestamp || 0;
-       if (updatedBetween?.from && timestamp < updatedBetween.from) return false;
-       if (updatedBetween?.to && timestamp > updatedBetween.to) return false;
-       return true;
-     });
-     return {
-       channelId,
-       latestTimestamp: Math.max(...filteredTransactions.map((tx) => tx.timestamp || 0), 0),
-       hasValidTransactions: filteredTransactions.length > 0,
-       filteredTransactions,
-     };
-   },
- );
+ channelsWithTimestamps = Object.entries(resultGetTx.result.transactions)
+   .map(([channelId, transactions]) => {
+     const filteredTransactions = transactions.filter(tx => {
+       const timestamp = tx.timestamp || 0;
+       return (!updatedBetween?.from || timestamp >= updatedBetween.from) &&
+              (!updatedBetween?.to || timestamp <= updatedBetween.to);
+     });
+     const latestTimestamp = filteredTransactions.reduce((max, tx) => 
+       Math.max(max, tx.timestamp || 0), 0);
+     return {
+       channelId,
+       latestTimestamp,
+       hasValidTransactions: filteredTransactions.length > 0,
+       filteredTransactions,
+     };
+   });

Also consider replacing console.warn with a proper logging system:

- console.warn(`Failed to decrypt channel ${channelId}:`, error);
+ logger.warn(`Failed to decrypt channel ${channelId}: ${error instanceof Error ? error.message : String(error)}`);

298-345: Consider adding constants for pagination logic.

The pagination implementation is solid, but readability could be improved.

Consider these improvements:

+ const PAGINATION_META_FIELDS = ['storageMeta', 'transactionsStorageLocation'] as const;
+ 
  if (page !== undefined && pageSize !== undefined) {
    const startIndex = (page - 1) * pageSize;
    const endIndex = Math.min(startIndex + pageSize, validChannels.length);
    channelsToProcess = validChannels.slice(startIndex, endIndex);

    paginationMeta = {
      total: validChannels.length,
      page,
      pageSize,
      hasMore: endIndex < validChannels.length,
    };
  }

  // Create the final meta object
  const successfulChannelIds = Object.keys(result.transactions);
  const paginatedMeta = {
    ...resultGetTx.meta,
-   storageMeta: Object.keys(resultGetTx.meta.storageMeta || {})
-     .filter((key) => successfulChannelIds.includes(key))
-     .reduce((acc, key) => {
-       acc[key] = resultGetTx.meta.storageMeta?.[key] || [];
-       return acc;
-     }, {} as Record<string, StorageTypes.IEntryMetadata[]>),
-   transactionsStorageLocation: Object.keys(resultGetTx.meta.transactionsStorageLocation)
-     .filter((key) => successfulChannelIds.includes(key))
-     .reduce((acc, key) => {
-       acc[key] = resultGetTx.meta.transactionsStorageLocation[key];
-       return acc;
-     }, {} as Record<string, string[]>),
+   ...PAGINATION_META_FIELDS.reduce((acc, field) => ({
+     ...acc,
+     [field]: Object.entries(resultGetTx.meta[field] || {})
+       .filter(([key]) => successfulChannelIds.includes(key))
+       .reduce((fieldAcc, [key, value]) => ({
+         ...fieldAcc,
+         [key]: value,
+       }), {}),
+   }), {}),
    pagination: paginationMeta,
  };

358-362: Add type safety to channel validation.

The current implementation could benefit from stronger typing.

Consider this improvement:

- private isValidChannel(cleaned: { transactions: any[]; ignoredTransactions: any[] }): boolean {
+ private isValidChannel(cleaned: { 
+   transactions: (TransactionTypes.ITimestampedTransaction | null)[]; 
+   ignoredTransactions: (TransactionTypes.IIgnoredTransaction | null)[];
+ }): boolean {
    return (
      cleaned.transactions && cleaned.transactions.length > 0 && cleaned.transactions[0] !== null
    );
  }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ecf230 and 6fbe1f5.

📒 Files selected for processing (6)
  • packages/integration-test/test/node-client.test.ts (3 hunks)
  • packages/request-client.js/src/api/request-network.ts (7 hunks)
  • packages/request-logic/src/request-logic.ts (2 hunks)
  • packages/thegraph-data-access/src/queries.ts (1 hunks)
  • packages/transaction-manager/src/transaction-manager.ts (4 hunks)
  • packages/transaction-manager/test/index.test.ts (13 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/request-logic/src/request-logic.ts
  • packages/thegraph-data-access/src/queries.ts
  • packages/integration-test/test/node-client.test.ts
  • packages/request-client.js/src/api/request-network.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-test
🔇 Additional comments (5)
packages/transaction-manager/src/transaction-manager.ts (4)

7-11: LGTM! Clean import additions for pagination support.

The new imports for StorageTypes and validatePaginationParams are appropriate for the pagination implementation.


200-202: LGTM! Proper validation and pagination handling.

The changes correctly:

  1. Validate pagination parameters before processing
  2. Move pagination handling to a higher level while keeping the data access layer focused on data retrieval

218-220: LGTM! Consistent implementation with getChannelsByTopic.

The changes maintain a consistent approach to pagination handling across both channel retrieval methods.


231-233: LGTM! Clear method signature update.

The added parameters properly support the pagination and timestamp filtering functionality.

packages/transaction-manager/test/index.test.ts (1)

1593-1854: LGTM! Comprehensive test coverage for pagination.

The test suite thoroughly covers:

  • Pagination functionality with various page sizes
  • Edge cases (empty results, boundary conditions)
  • Error handling for invalid parameters
  • Timestamp boundaries with pagination
  • Parameter validation

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6fbe1f5 and 2cfcea0.

📒 Files selected for processing (3)
  • packages/transaction-manager/src/transaction-manager.ts (4 hunks)
  • packages/transaction-manager/test/index.test.ts (13 hunks)
  • packages/utils/src/utils.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-test
🔇 Additional comments (10)
packages/utils/src/utils.ts (2)

172-183: LGTM! Robust validation for pagination parameters.

The validation logic correctly ensures that either both pagination parameters are provided or neither is provided, preventing partial pagination scenarios.


185-191: LGTM! Clear error messages for invalid pagination values.

The error messages clearly indicate the validation failures and include the actual values that caused the failure.

packages/transaction-manager/src/transaction-manager.ts (6)

200-202: LGTM! Proper validation and parameter passing.

The method correctly:

  1. Validates pagination parameters
  2. Passes timestamp boundaries to data access
  3. Forwards pagination handling to parseMultipleChannels

218-220: LGTM! Consistent pagination handling.

The method mirrors the pagination handling approach of getChannelsByTopic, maintaining consistency across the API.


235-249: LGTM! Robust timestamp filtering and channel processing.

The implementation correctly:

  1. Filters transactions based on timestamp boundaries
  2. Tracks the latest timestamp for each channel
  3. Maintains valid transaction state

253-257: LGTM! Proper channel ordering and filtering.

The implementation:

  1. Filters out channels without valid transactions
  2. Sorts channels by latest timestamp
  3. Maintains channel order for pagination

277-277: Consider using a proper logging system.

The console warning might not be appropriate for production code. Consider using a proper logging system.


357-361: LGTM! Clear validation of channel validity.

The isValidChannel method properly checks for:

  1. Existence of transactions array
  2. Non-empty transactions
  3. Valid first transaction
packages/transaction-manager/test/index.test.ts (2)

1591-1677: LGTM! Comprehensive pagination tests.

The tests thoroughly verify:

  1. Basic pagination with multiple pages
  2. Correct page size handling
  3. Proper metadata in response
  4. hasMore flag accuracy

1866-1937: LGTM! Thorough validation error testing.

The tests comprehensively cover all validation error cases:

  1. Invalid page numbers
  2. Invalid page sizes
  3. Partial pagination parameters
  4. Both single and multiple topic scenarios

packages/transaction-manager/test/index.test.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/transaction-manager/test/index.test.ts (1)

1741-1801: Consider extracting common test setup code.

The timestamp boundary test setup contains duplicated channel data structure that appears in multiple tests. Consider extracting this into a shared test fixture or helper function to improve maintainability.

Example refactor:

function createTestChannels() {
  return {
    [channelId]: [
      {
        state: TransactionTypes.TransactionState.PENDING,
        timestamp: 2000,
        transaction: { data },
      },
      {
        state: TransactionTypes.TransactionState.PENDING,
        timestamp: 2500,
        transaction: { data },
      },
    ],
    [channelId2]: [
      {
        state: TransactionTypes.TransactionState.PENDING,
        timestamp: 3000,
        transaction: { data: data2 },
      },
    ],
  };
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2cfcea0 and 8c56f99.

📒 Files selected for processing (1)
  • packages/transaction-manager/test/index.test.ts (13 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-test
🔇 Additional comments (3)
packages/transaction-manager/test/index.test.ts (3)

49-53: Well-structured metadata initialization!

The addition of pagination and storageMeta fields to the metadata structure aligns well with the pagination improvements mentioned in the PR objectives.


1137-1142: Consistent pagination metadata handling!

The test expectations have been properly updated to validate the new pagination metadata structure across different scenarios.

Also applies to: 1156-1157


1591-1874: Excellent pagination test coverage!

The new test suite thoroughly covers pagination functionality including:

  • Basic pagination with page and pageSize
  • Empty result handling
  • Parameter validation
  • Timestamp boundary handling
  • Error cases for invalid inputs

This comprehensive coverage will help ensure the reliability of the pagination implementation.

Copy link
Member

@aimensahnoun aimensahnoun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks are failing

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/transaction-manager/src/transaction-manager.ts (1)

246-250: Simplify timestamp filtering logic.

The nested ternary operations make the code harder to read. Consider extracting these checks into a separate function for better readability.

-          const hasTransactionsAfterFrom =
-            !updatedBetween?.from || timestamps.some((t) => t >= updatedBetween.from!);
-          const hasTransactionsBeforeTo =
-            !updatedBetween?.to || timestamps.some((t) => t <= updatedBetween.to!);
+          const hasTransactionsInRange = (timestamps: number[], bounds: TransactionTypes.ITimestampBoundaries) => {
+            const afterFrom = !bounds.from || timestamps.some(t => t >= bounds.from);
+            const beforeTo = !bounds.to || timestamps.some(t => t <= bounds.to);
+            return afterFrom && beforeTo;
+          };
+          hasValidTransactions = hasTransactionsInRange(timestamps, updatedBetween);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8c56f99 and 34132b7.

📒 Files selected for processing (1)
  • packages/transaction-manager/src/transaction-manager.ts (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build-and-test
🔇 Additional comments (3)
packages/transaction-manager/src/transaction-manager.ts (3)

200-202: LGTM! Consistent validation of pagination parameters.

The validation is correctly placed before data access operations and consistently implemented across both topic methods.

Also applies to: 218-220


327-336: LGTM! Well-structured pagination metadata.

The pagination metadata is well-structured and provides all necessary information for clients to handle paginated results effectively.


200-202: Verify impact on API consumers.

The changes to pagination handling could affect API consumers who rely on the previous behavior where invalid channels were included in non-paginated results.

Also applies to: 218-220

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/transaction-manager/src/transaction-manager.ts (1)

368-375: Enhance channel validation checks.

While the current validation is functional, consider adding more comprehensive checks:

   private isValidChannel(cleaned: {
     transactions: (TransactionTypes.ITimestampedTransaction | null)[];
     ignoredTransactions: (TransactionTypes.IIgnoredTransaction | null)[];
   }): boolean {
     return (
-      cleaned.transactions && cleaned.transactions.length > 0 && cleaned.transactions[0] !== null
+      cleaned &&
+      Array.isArray(cleaned.transactions) &&
+      cleaned.transactions.length > 0 &&
+      cleaned.transactions[0] !== null &&
+      Array.isArray(cleaned.ignoredTransactions)
     );
   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 34132b7 and e935f5e.

📒 Files selected for processing (1)
  • packages/transaction-manager/src/transaction-manager.ts (4 hunks)
🧰 Additional context used
📓 Learnings (1)
packages/transaction-manager/src/transaction-manager.ts (1)
Learnt from: rodrigopavezi
PR: RequestNetwork/requestNetwork#1547
File: packages/transaction-manager/src/transaction-manager.ts:301-302
Timestamp: 2025-01-22T10:28:23.757Z
Learning: In the RequestNetwork codebase, when filtering channels in pagination logic, validation (channel.isValid) is only applied when pagination is requested (page is defined). This allows seeing all channels (valid or invalid) when not using pagination, while ensuring only valid channels are included in paginated results.
🔇 Additional comments (4)
packages/transaction-manager/src/transaction-manager.ts (4)

7-7: LGTM! Import changes are well-organized.

The new imports for StorageTypes and validatePaginationParams are correctly placed and support the pagination-related changes.

Also applies to: 11-11


200-202: LGTM! Pagination validation is properly implemented.

The changes correctly:

  1. Validate pagination parameters before processing
  2. Move pagination handling to a higher level in parseMultipleChannels
  3. Simplify the dataAccess calls by removing pagination parameters

This aligns well with the PR objective of fixing pagination issues.

Also applies to: 218-220


288-288: Note about error logging.

The console.warn for decryption errors was previously discussed and intentionally kept as is.


235-364: LGTM! Robust pagination implementation.

The parseMultipleChannels implementation:

  1. Correctly handles failed decryption cases
  2. Maintains channel order by latest timestamp
  3. Provides comprehensive pagination metadata
  4. Properly filters channels based on timestamp boundaries

This effectively addresses the PR's objective of fixing pagination when decryption fails.

@rodrigopavezi rodrigopavezi merged commit 3dff5a7 into master Jan 22, 2025
10 checks passed
@rodrigopavezi rodrigopavezi deleted the fix/pagination branch January 22, 2025 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add pagination to the Request Node /getTransactionsByChannelId endpoint
4 participants