Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(torii): subscribe to token updates (metadata etc.) #2990

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Feb 4, 2025

Summary by CodeRabbit

  • New Features
    • Tokens now include a unique identifier for enhanced tracking.
    • Clients can subscribe to token updates, receiving real‑time streaming notifications for token-related changes.

Copy link

coderabbitai bot commented Feb 4, 2025

Walkthrough

Ohayo, sensei! This PR extends token-related functionality in the gRPC service. It adds a new token_id field to the Token message and introduces a new RPC method SubscribeTokens (with its response message) in the protocol definitions. Additionally, the server code has been refactored with reorganized imports and a new type alias for the token subscription stream. A new subscription module with token management structures and methods has also been introduced to handle token update notifications.

Changes

File(s) Change Summary
crates/torii/grpc/proto/types.proto
crates/torii/grpc/proto/world.proto
Added token_id field to the Token message; introduced new RPC method SubscribeTokens and message SubscribeTokensResponse for token update subscriptions.
crates/torii/grpc/src/server/mod.rs Reorganized import statements; added a new type alias SubscribeTokensResponseStream and updated the type signature for the token subscription stream.
crates/torii/grpc/src/server/subscriptions/mod.rs
crates/torii/grpc/src/server/subscriptions/token.rs
Added a new token module; introduced TokenSubscriber, TokenManager, and Service with methods to add, update, remove subscribers, and process token update events.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant WorldService
    participant Service
    participant TokenManager
    Client->>WorldService: SubscribeTokens(RetrieveTokensRequest)
    WorldService->>Service: Create Subscription
    Service->>TokenManager: add_subscriber(contract_addresses)
    TokenManager-->>Service: Confirm Registration
    loop Token Update
        TokenManager-->>Service: Notify Token Update
        Service->>Client: Stream SubscribeTokensResponse
    end
Loading

Possibly related PRs

Suggested reviewers

  • glihm

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.

🪧 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.

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: 0

🧹 Nitpick comments (4)
crates/torii/grpc/src/server/subscriptions/token.rs (4)

34-59: Ohayo sensei! Consider using a globally unique subscription ID generator.

Storing subscription IDs generated with a random u64 may risk collisions over a large number of subscriptions. Using a time-based or atomic counter-based ID might reduce the already small probability of collisions.


60-60: Ohayo sensei! Fix Rust formatting to pass CI.

The CI pipeline indicates code formatting issues on lines 60, 98, and 135. Please run Rust’s built-in formatter (cargo fmt) to comply with the project formatting guidelines.

Also applies to: 98-98, 135-135

🧰 Tools
🪛 GitHub Actions: ci

[warning] 60-60: Code formatting issues detected. Please adhere to the Rust formatting guidelines.


111-120: Ohayo sensei! Consider graceful shutdown for publish_updates.

When shutting down, the infinite loop in publish_updates may continue receiving from the channel until it empties or indefinitely if no complete signal is sent. You could add a cancellation mechanism or a select statement that also listens for a shutdown request to ensure a clean exit.


122-162: Ohayo sensei! Confirm the performance of process_balance_update under high load.

Looping over all subscribers in process_balance_update may become costly if there are many subscribers. Consider using a more scalable data structure or partitioning subscriptions by contract address to avoid iterating the entire map.

🧰 Tools
🪛 GitHub Actions: ci

[warning] 135-135: Code formatting issues detected. Please adhere to the Rust formatting guidelines.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bf0c5ee and e984885.

📒 Files selected for processing (5)
  • crates/torii/grpc/proto/types.proto (1 hunks)
  • crates/torii/grpc/proto/world.proto (2 hunks)
  • crates/torii/grpc/src/server/mod.rs (3 hunks)
  • crates/torii/grpc/src/server/subscriptions/mod.rs (1 hunks)
  • crates/torii/grpc/src/server/subscriptions/token.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • crates/torii/grpc/src/server/subscriptions/mod.rs
🧰 Additional context used
🪛 GitHub Actions: ci
crates/torii/grpc/src/server/subscriptions/token.rs

[warning] 60-60: Code formatting issues detected. Please adhere to the Rust formatting guidelines.


[warning] 98-98: Code formatting issues detected. Please adhere to the Rust formatting guidelines.


[warning] 135-135: Code formatting issues detected. Please adhere to the Rust formatting guidelines.

crates/torii/grpc/src/server/mod.rs

[warning] 56-56: Code formatting issues detected. Please adhere to the Rust formatting guidelines.


[warning] 1256-1256: Code formatting issues detected. Please adhere to the Rust formatting guidelines.

🔇 Additional comments (6)
crates/torii/grpc/src/server/subscriptions/token.rs (1)

91-110: Ohayo sensei! Verify concurrency in the Service::new method.

Spawning a new task to run publish_updates might lead to potential race conditions if other tasks modify shared states unexpectedly. Ensure that the references to subs_manager remain valid for the duration of the spawned task, and confirm that the manager’s concurrency design is robust (e.g. no risk of partial updates or deadlocks).

🧰 Tools
🪛 GitHub Actions: ci

[warning] 98-98: Code formatting issues detected. Please adhere to the Rust formatting guidelines.

crates/torii/grpc/src/server/mod.rs (2)

59-59: Ohayo sensei! Great job consolidating these imports.

All relevant proto::world entities are neatly grouped. This improves readability and maintainability.


1246-1247: Ohayo sensei! Confirm consistent naming for token subscription streams.

The introduction of SubscribeTokensResponseStream and aliasing it with SubscribeTokensStream is appropriate. Just ensure a consistent naming convention throughout the codebase so it’s easy to locate references.

Also applies to: 1258-1259

crates/torii/grpc/proto/types.proto (1)

165-165: Ohayo sensei! Validate the usage of token_id in upstream logic.

Adding this field is helpful to distinguish tokens, but ensure that instrumentation code and any references to Token in logic or tests properly handle token_id so we don’t accidentally break backward compatibility.

crates/torii/grpc/proto/world.proto (2)

43-45: Ohayo sensei: New RPC method for token updates!

The addition of the SubscribeTokens RPC method correctly follows the structure of existing subscription RPCs. This new method enhances the service's token handling capability by providing a stream of token updates. Please ensure that client documentation and integration tests are updated accordingly to include this new branch of functionality.


91-95: Ohayo sensei: Clean and clear SubscribeTokensResponse message!

The new SubscribeTokensResponse message neatly encapsulates a types.Token, aligning well with the new RPC. Do verify that the Token message in the imported types.proto includes the token_id field as per the overarching PR objectives, to guarantee that token updates carry the correct metadata.

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