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

Add production field to data messages #135

Merged
merged 3 commits into from
Jan 18, 2025
Merged

Conversation

fracek
Copy link
Contributor

@fracek fracek commented Jan 18, 2025

No description provided.

Copy link

coderabbitai bot commented Jan 18, 2025

Warning

Rate limit exceeded

@fracek has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 6 minutes and 45 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 5797581 and 4f827ba.

📒 Files selected for processing (5)
  • change/@apibara-indexer-17e06f60-8a2d-4dd6-bc1f-a4b2fb4d3b3a.json (1 hunks)
  • change/@apibara-plugin-drizzle-f060747e-3aa6-4501-a250-dd8d12dbae1a.json (1 hunks)
  • change/@apibara-plugin-mongo-3fbd3cc4-77af-4525-9192-0cd635c71b48.json (1 hunks)
  • change/@apibara-plugin-sqlite-8891e8b0-1000-4500-8f2b-84a0c119f61e.json (1 hunks)
  • change/@apibara-protocol-70715784-8c5b-4a67-8852-e732f96e76d6.json (1 hunks)
📝 Walkthrough

Walkthrough

This pull request introduces a new DataProduction feature to the Apibara protocol, enhancing data streaming capabilities. The changes span multiple files across the protocol package, adding a new enum to categorize data production modes (unknown, backfill, live) and implementing corresponding serialization/deserialization logic. The modifications include updates to JSON definitions, protobuf definitions, TypeScript interfaces, and client-side streaming functionality, with a particular focus on adding a timeout mechanism for data retrieval.

Changes

File Change Summary
change/@apibara-protocol-70715784-8c5b-4a67-8852-e732f96e76d6.json Added new JSON object indicating prerelease version with fields: type, comment, packageName, email, dependentChangeType
packages/protocol/proto/stream.proto Added DataProduction enum with values: DATA_PRODUCTION_UNKNOWN, DATA_PRODUCTION_BACKFILL, DATA_PRODUCTION_LIVE and new field DataProduction production = 5;
packages/protocol/src/proto/stream.ts Implemented DataProduction enum and conversion functions for JSON handling
packages/protocol/src/stream.ts Added DataProduction constant and updated Data structure to include production field
packages/protocol/src/client.ts Introduced timeout mechanism for StreamDataIterable with a default timeout of 45 seconds
examples/starknet-client/src/main.ts Updated orderKey and added streaming timeout with a new options object
packages/protocol/src/client.ts, packages/protocol/src/proto/stream.ts, packages/protocol/src/stream.ts, packages/protocol/src/stream.test.ts, packages/protocol/src/testing/client.test.ts, packages/indexer/src/indexer.test.ts, packages/indexer/src/internal/testing.ts, packages/plugin-drizzle/tests/storage.test.ts, packages/plugin-mongo/tests/storage.test.ts, packages/plugin-sqlite/tests/kv.test.ts, packages/plugin-sqlite/tests/persistence.test.ts Added production field with value "backfill" to various mock data structures in test cases

Possibly related PRs

  • plugin-drizzle: add drizzle plugin and persistence #130: The changes in this PR involve the addition of a drizzle plugin and persistence capabilities, which may relate to the new JSON object added in the main PR that specifies a prerelease version and includes a "data production" field, enhancing the overall data handling and persistence mechanisms.
  • starknet: add event decoder module #133: This PR introduces an event decoder module, which could be relevant as it may interact with the data structures defined in the main PR, particularly if the "data production" field is used in event handling or decoding processes.

Poem

🐰 A rabbit's tale of data flow,
Streaming bits with timeout's glow,
Backfill, live, or yet unknown,
Our protocol has surely grown!
Hop along, dear data stream 🚀


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.

Copy link

@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 (2)
packages/protocol/src/client.ts (2)

26-26: Consider making the default timeout configurable.

The default timeout is hardcoded. Consider making it configurable through environment variables or configuration options.

-const DEFAULT_TIMEOUT_MS = 45_000;
+const DEFAULT_TIMEOUT_MS = Number(process.env.DNA_DEFAULT_TIMEOUT_MS ?? "45000");

150-163: Simplify cursor handling logic and improve error messages.

The cursor handling logic is complex and uses multiple assertions without clear error messages. Consider:

  1. Adding descriptive error messages to assertions
  2. Extracting the cursor comparison logic to a separate method
  3. Simplifying the nested conditions
+          function isCursorMatched(endCursor: Cursor, target: Cursor): boolean {
+            if (endCursor.orderKey !== target.orderKey) return false;
+            return !target.uniqueKey || target.uniqueKey === endCursor.uniqueKey;
+          }

-            assert(value.message.$case === "data");
-            assert(decodedMessage._tag === "data");
+            assert(value.message.$case === "data", "Expected data message");
+            assert(decodedMessage._tag === "data", "Expected data message tag");

-            const { orderKey, uniqueKey } = endingCursor;
             const endCursor = decodedMessage.data.endCursor;
+            assert(endCursor, "Expected end cursor in data message");

-            if (orderKey === endCursor?.orderKey) {
-              if (!uniqueKey || uniqueKey === endCursor.uniqueKey) {
-                shouldStop = true;
-                return { done: false, value: decodedMessage };
-              }
+            if (isCursorMatched(endCursor, endingCursor)) {
+              shouldStop = true;
+              return { done: false, value: decodedMessage };
             }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f1c0092 and 5997266.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (6)
  • change/@apibara-protocol-70715784-8c5b-4a67-8852-e732f96e76d6.json (1 hunks)
  • examples/starknet-client/src/main.ts (1 hunks)
  • packages/protocol/proto/stream.proto (2 hunks)
  • packages/protocol/src/client.ts (2 hunks)
  • packages/protocol/src/proto/stream.ts (8 hunks)
  • packages/protocol/src/stream.ts (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • change/@apibara-protocol-70715784-8c5b-4a67-8852-e732f96e76d6.json
🧰 Additional context used
🪛 GitHub Actions: Build
examples/starknet-client/src/main.ts

[error] 83-85: Code formatting issue: Long line needs to be split into multiple lines for better readability

🔇 Additional comments (11)
packages/protocol/src/stream.ts (3)

37-62: LGTM! Well-structured schema definition.

The implementation follows the established pattern, properly handles all enum values, and includes appropriate fallbacks.


64-64: LGTM! Consistent type export.

The type export correctly follows the established pattern.


144-144: LGTM! Consistent data structure modification.

The production field is correctly added to both the schema struct and type definition.

Also applies to: 169-169

packages/protocol/src/proto/stream.ts (4)

66-74: LGTM! Well-documented enum definition.

The enum values and documentation are clear and consistent with the protobuf definition.


76-92: LGTM! Robust JSON to enum conversion.

The function properly handles all input values with appropriate fallback.


94-106: LGTM! Complete enum to JSON conversion.

The function handles all enum values with appropriate fallback.


885-885: LGTM! Comprehensive Data interface modifications.

The production field is consistently implemented across all related functions with proper defaults and serialization logic.

Also applies to: 902-904, 943-949, 965-965, 983-985, 1002-1002

packages/protocol/proto/stream.proto (2)

109-110: LGTM! Well-defined message field.

The production field is properly documented and correctly defined with appropriate field number.


137-144: LGTM! Clear and well-documented enum.

The enum values are properly defined and documented with clear explanations.

examples/starknet-client/src/main.ts (1)

78-79: Verify the hardcoded orderKey value.

The orderKey has been updated from 800_000n to 1_078_335n. Consider making this value configurable through command-line arguments to improve flexibility.

-        orderKey: 1_078_335n,
+        orderKey: BigInt(args.startingBlock ?? "1078335"),
packages/protocol/src/client.ts (1)

171-173: LGTM! Good practice using finally block.

The timeout is properly cleared in the finally block, preventing potential memory leaks.

},
});

for await (const message of client.streamData(request)) {
for await (const message of client.streamData(request, { timeout: 40_000 })) {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Address the formatting issue and consider extracting the timeout value.

  1. Split the long line into multiple lines for better readability.
  2. Consider making the timeout configurable through command-line arguments.
-    for await (const message of client.streamData(request, { timeout: 40_000 })) {
+    const streamOptions = {
+      timeout: args.timeout ?? 40_000,
+    };
+    for await (const message of client.streamData(request, streamOptions)) {

Add this to the command args:

timeout: {
  type: "number",
  default: 40_000,
  description: "Timeout in milliseconds for data streaming",
},
🧰 Tools
🪛 GitHub Actions: Build

[error] 83-85: Code formatting issue: Long line needs to be split into multiple lines for better readability

Comment on lines 130 to 144
// biome-ignore lint/suspicious/noExplicitAny: any is ok
const t: Promise<{ done: boolean; value: any }> = new Promise(
(_, reject) => {
clock = setTimeout(() => {
reject(new Error(`No message received in ${timeout}ms`));
}, timeout);
},
);
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling for timeout.

  1. Create a specific error type for timeout errors.
  2. Include more context in the error message.
+class TimeoutError extends Error {
+  constructor(timeout: number) {
+    super(`No message received in ${timeout}ms`);
+    this.name = 'TimeoutError';
+  }
+}

-              reject(new Error(`No message received in ${timeout}ms`));
+              reject(new TimeoutError(timeout));

Committable suggestion skipped: line range outside the PR's diff.

@fracek fracek force-pushed the update-to-protocol branch 3 times, most recently from 8245237 to 1d0247c Compare January 18, 2025 15:12
@fracek fracek force-pushed the update-to-protocol branch from 1d0247c to 5797581 Compare January 18, 2025 15:42
Copy link

@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/protocol/src/stream.test.ts (1)

98-98: Consider adding test cases for other production values.

The test correctly verifies encoding/decoding of the "backfill" production value (encoded as 1). Consider adding test cases for other production values to ensure complete coverage of the enum.

Also applies to: 124-124

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3dc8390 and 5797581.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (14)
  • change/@apibara-protocol-70715784-8c5b-4a67-8852-e732f96e76d6.json (1 hunks)
  • examples/starknet-client/src/main.ts (1 hunks)
  • packages/indexer/src/indexer.test.ts (16 hunks)
  • packages/indexer/src/internal/testing.ts (1 hunks)
  • packages/plugin-drizzle/tests/storage.test.ts (28 hunks)
  • packages/plugin-mongo/tests/storage.test.ts (28 hunks)
  • packages/plugin-sqlite/tests/kv.test.ts (18 hunks)
  • packages/plugin-sqlite/tests/persistence.test.ts (28 hunks)
  • packages/protocol/proto/stream.proto (2 hunks)
  • packages/protocol/src/client.ts (2 hunks)
  • packages/protocol/src/proto/stream.ts (8 hunks)
  • packages/protocol/src/stream.test.ts (2 hunks)
  • packages/protocol/src/stream.ts (3 hunks)
  • packages/protocol/src/testing/client.test.ts (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • change/@apibara-protocol-70715784-8c5b-4a67-8852-e732f96e76d6.json
  • examples/starknet-client/src/main.ts
  • packages/protocol/proto/stream.proto
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (19)
packages/protocol/src/testing/client.test.ts (1)

12-16: LGTM! Consistent implementation of the production field.

The production field is correctly added to the mock data responses with a consistent value of "backfill".

Also applies to: 49-53

packages/indexer/src/internal/testing.ts (1)

59-59: LGTM! Production field added to mock message generator.

The production field is correctly added to the generateMockMessages function, maintaining consistency with other test files.

packages/indexer/src/indexer.test.ts (1)

134-134: LGTM! Comprehensive coverage of production field in test scenarios.

The production field is consistently added across all test scenarios, including error cases, maintaining the integrity of the tests while accommodating the new field.

Also applies to: 144-144, 154-154, 171-171, 181-181, 191-191, 201-201, 218-218, 228-228, 238-238, 331-331, 341-341, 351-351, 370-370, 380-380, 390-390

packages/protocol/src/stream.ts (3)

37-64: LGTM! Well-structured enum implementation.

The DataProduction implementation follows the established pattern, with proper handling of unknown/unrecognized values and clear documentation.


144-144: LGTM! Proper integration of the new field.

The production field is correctly added to the Data structure with the appropriate type.


169-169: LGTM! Type definition properly updated.

The StreamDataResponse type is correctly updated to include the new production field.

packages/protocol/src/client.ts (4)

26-33: LGTM! Well-structured error handling.

The timeout implementation follows best practices with:

  • A reasonable default timeout value
  • A dedicated error class with descriptive messages

41-44: LGTM! Clear interface documentation.

The timeout option is properly documented with its unit of measurement.


126-130: LGTM! Robust timeout handling.

The timeout implementation is well-structured with:

  • Proper default value handling
  • Clear error handling using the custom error class

Also applies to: 137-144


146-180: LGTM! Proper resource cleanup.

The implementation ensures proper cleanup of timeouts in both success and error cases, while maintaining existing functionality.

packages/protocol/src/proto/stream.ts (5)

66-106: LGTM! Well-documented enum definition.

The DataProduction enum is properly defined with:

  • Clear documentation for each value
  • Comprehensive JSON conversion functions

885-885: LGTM! Proper default value.

The production field is initialized to 0 (UNKNOWN), which is the appropriate default.


902-904: LGTM! Efficient encoding.

The production field is encoded efficiently, only when it differs from the default value.


943-949: LGTM! Proper decoding implementation.

The production field is correctly decoded with proper type casting.


965-965: LGTM! Complete JSON support.

The JSON conversion is properly implemented with:

  • Correct use of helper functions
  • Proper handling of undefined values

Also applies to: 983-985, 1002-1002

packages/plugin-drizzle/tests/storage.test.ts (1)

Line range hint 702-1162: LGTM! Consistent implementation of the production field.

The changes correctly add the production field with "backfill" value to all mock data responses, aligning with the PR objective. The test logic and assertions remain unchanged.

packages/plugin-sqlite/tests/kv.test.ts (1)

127-127: LGTM! The changes align with the PR objectives.

The addition of the production: "backfill" field to mock data responses is consistent across all test cases and doesn't affect the existing test logic or assertions.

Also applies to: 137-137, 147-147, 164-164, 174-174, 184-184, 194-194, 211-211, 221-221, 305-305, 315-315, 325-325, 342-342, 352-352, 362-362, 372-372, 389-389, 399-399, 479-479, 489-489, 499-499, 516-516, 526-526, 536-536, 546-546, 563-563, 573-573

packages/plugin-sqlite/tests/persistence.test.ts (1)

64-64: LGTM! The changes align with the PR objectives.

The addition of the production: "backfill" field to mock data responses is consistent across all test cases and doesn't affect the existing test logic or assertions.

Also applies to: 74-74, 84-84, 101-101, 111-111, 121-121, 131-131, 148-148, 158-158, 168-168, 303-303, 313-313, 323-323, 340-340, 350-350, 360-360, 370-370, 387-387, 397-397, 479-479, 489-489, 499-499, 516-516, 526-526, 536-536, 546-546, 563-563, 573-573

packages/plugin-mongo/tests/storage.test.ts (1)

456-456: LGTM! The changes align with the PR objectives.

The addition of the production: "backfill" field to mock data responses is consistent across all test cases and doesn't affect the existing test logic or assertions.

Also applies to: 466-466, 476-476, 493-493, 503-503, 513-513, 523-523, 540-540, 550-550, 560-560, 659-659, 669-669, 679-679, 696-696, 706-706, 716-716, 726-726, 743-743, 753-753, 852-852, 862-862, 872-872, 889-889, 899-899, 909-909, 919-919, 936-936, 946-946

@fracek fracek force-pushed the update-to-protocol branch from 5797581 to 4f827ba Compare January 18, 2025 15:48
@fracek fracek merged commit c806dcc into apibara:main Jan 18, 2025
2 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jan 20, 2025
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.

1 participant