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

Full node streaming -- perp position to signed int #2544

Merged
merged 2 commits into from
Oct 28, 2024

Conversation

jonfung-dydx
Copy link
Contributor

@jonfung-dydx jonfung-dydx commented Oct 28, 2024

previously was uint32. change to int64

Summary by CodeRabbit

  • New Features

    • Updated handling of the quantums field in subaccount positions to allow for negative values, indicating short positions.
    • Enhanced documentation for clarity on the quantums field's interpretation.
  • Bug Fixes

    • Adjusted data type of quantums from uint64 to int64 across relevant interfaces and methods to reflect the ability to represent negative values.

@jonfung-dydx jonfung-dydx requested a review from a team as a code owner October 28, 2024 16:14
@jonfung-dydx jonfung-dydx requested a review from dydxwill October 28, 2024 16:14
Copy link
Contributor

coderabbitai bot commented Oct 28, 2024

Walkthrough

The pull request introduces changes to the SubaccountPerpetualPosition and SubaccountPerpetualPositionSDKType interfaces by updating the quantums field's documentation and changing its default value from Long.UZERO to Long.ZERO. The type for quantums is modified from uint64 to int64 in both encoding and decoding methods. Similar updates are made in related methods and structures to ensure consistent handling of negative values indicating short positions. The SubaccountAssetPosition interfaces remain unchanged structurally but retain previous definitions for quantums.

Changes

File Path Change Summary
indexer/packages/v4-protos/src/codegen/dydxprotocol/subaccounts/streaming.ts Updated documentation for quantums in SubaccountPerpetualPosition interfaces; changed type from uint64 to int64 in encoding/decoding methods; default value for quantums in createBaseSubaccountPerpetualPosition updated to Long.ZERO.
proto/dydxprotocol/subaccounts/streaming.proto Changed quantums field type from uint64 to int64 in SubaccountPerpetualPosition.
protocol/x/subaccounts/keeper/subaccount.go Modified Quantums field type from Uint64() to Int64() in GetStreamSubaccountUpdate and GenerateStreamSubaccountUpdate methods.

Possibly related PRs

Suggested labels

protocol, indexer, proto

Suggested reviewers

  • dydxwill

🐇 In the meadow, changes bloom bright,
Quantums now dance in the soft moonlight.
Short positions clear, with a hop and a cheer,
Our code's now refined, let’s spread the good cheer!
With each little tweak, our paths intertwine,
A world of subaccounts, all perfectly aligned! 🌼


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

🧹 Outside diff range and nitpick comments (1)
proto/dydxprotocol/subaccounts/streaming.proto (1)

31-32: Consider version strategy for breaking change.

Changing the field type from uint64 to int64 is a breaking change that affects wire format compatibility. Consider:

  1. Implementing a migration strategy
  2. Updating API version
  3. Coordinating with client implementations
  4. Adding validation for negative values in relevant handlers
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 61c2d54 and 1023a11.

⛔ Files ignored due to path filters (1)
  • protocol/x/subaccounts/types/streaming.pb.go is excluded by !**/*.pb.go
📒 Files selected for processing (3)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/subaccounts/streaming.ts (6 hunks)
  • proto/dydxprotocol/subaccounts/streaming.proto (1 hunks)
  • protocol/x/subaccounts/keeper/subaccount.go (2 hunks)
🔇 Additional comments (8)
proto/dydxprotocol/subaccounts/streaming.proto (1)

31-32: LGTM! Verify client compatibility.

The change from uint64 to int64 for quantums is well-documented and appropriate for representing both long and short positions.

Let's verify other proto definitions for consistency:

✅ Verification successful

Verified: Position representation change is consistent

The change from uint64 to int64 for quantums in streaming.proto aligns with the codebase's design. The main perpetual position message in perpetual_position.proto already uses a custom serializable integer type that can handle signed values, and other quantum-related fields across the codebase are appropriately typed for their specific use cases (e.g. order quantums remain unsigned as they represent absolute sizes).

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other quantum definitions in proto files to ensure consistent typing
rg -t proto "quantums.*=.*;" --no-heading

Length of output: 73


Script:

#!/bin/bash
# Search for files with .proto extension first
fd -e proto

# Then search for quantum definitions in those files
rg "quantums.*=.*;" $(fd -e proto)

Length of output: 12108


Script:

#!/bin/bash
# Let's check the perpetual position related files specifically
cat proto/dydxprotocol/subaccounts/perpetual_position.proto
cat proto/dydxprotocol/indexer/protocol/v1/perpetual.proto

Length of output: 1697

indexer/packages/v4-protos/src/codegen/dydxprotocol/subaccounts/streaming.ts (5)

62-62: LGTM! Clear documentation update.

The updated documentation clearly indicates that negative values represent short positions, which aligns with the PR objective of supporting signed integers for perpetual positions.

Also applies to: 74-74


181-181: LGTM! Appropriate default value change.

The change from Long.UZERO to Long.ZERO is correct for supporting signed integers, allowing for both positive and negative position values.


192-192: LGTM! Correct encoding type update.

The encoding method now uses int64 instead of uint64, which is necessary for properly serializing negative position values.


212-212: LGTM! Correct decoding type update.

The decoding method now uses int64 instead of uint64, ensuring proper deserialization of negative position values.


227-227: Verify handling of negative values in API consumers.

The change from Long.UZERO to Long.ZERO is correct, but we should ensure all consumers of this API are prepared to handle negative position values.

Let's verify the API consumers:

✅ Verification successful

Based on the search results, I can generate the final response as I have sufficient information to verify the API consumers. The search results show:

  1. The SubaccountPerpetualPosition type is used in streaming updates and its documentation explicitly states "The size of the position in base quantums. Negative means short."
  2. The consumers of this type primarily handle it for streaming updates and position tracking.
  3. The code shows no assumptions about non-negative values in the quantums field.
  4. The change from UZERO to ZERO aligns with the documented behavior of supporting negative values for short positions.

Change from Long.UZERO to Long.ZERO is safe and correctly implemented.

The codebase already expects and handles negative quantum values for short positions. The documentation and usage patterns confirm this is the intended behavior.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for direct usage of SubaccountPerpetualPosition to verify consumers
# are prepared for negative quantums

# Look for comparisons or operations with quantums that might assume non-negative values
rg -A 3 "quantums\s*(===|!==|<=|>=|>|<|[+\-*/])" --type ts

# Look for direct usage of the type
rg -A 3 "SubaccountPerpetualPosition" --type ts

Length of output: 23392

protocol/x/subaccounts/keeper/subaccount.go (2)

301-301: LGTM! Consistent with the previous change.

The change maintains consistency with the earlier modification while preserving Uint64() for asset positions which don't need to represent short positions.


153-153: LGTM! Verify integration with streaming consumers.

The change from Uint64() to Int64() correctly enables representation of short positions in perpetual markets.

Let's verify the streaming consumers are ready for signed integers:

✅ Verification successful

The search results show that the streaming consumers are ready for signed integers. Specifically:

  1. The protocol defines SubaccountPerpetualPosition with signed integer quantums in proto/dydxprotocol/subaccounts/streaming.proto:
// The size of the position in base quantums. Negative means short.
int64 quantums = 2;
  1. The indexer code handles signed quantums through the Long type in indexer/packages/v4-protos/src/codegen/dydxprotocol/subaccounts/streaming.ts:
/** The size of the position in base quantums. Negative means short. */
quantums: Long;
  1. The streaming messages are properly generated and handled in both the protocol and indexer codebases.

LGTM! The streaming consumers are ready for signed integers.

The change from Uint64() to Int64() is safe as the streaming consumers already expect and handle signed integers for perpetual position quantums.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for streaming message handling code to verify int64 compatibility
rg -A 5 "SubaccountPerpetualPosition.*quantums.*" 

Length of output: 51


Script:

#!/bin/bash
# Let's try a broader search for perpetual position quantums in proto definitions and handlers
rg -l "PerpetualPosition" | xargs rg -A 5 "quantums"

# Also search for streaming update handlers
rg -l "StreamSubaccountUpdate" | xargs rg -A 5 "quantums"

Length of output: 393653

@jonfung-dydx jonfung-dydx merged commit 0e185fd into main Oct 28, 2024
38 of 39 checks passed
@jonfung-dydx jonfung-dydx deleted the jonfung/signedFNSSubaccount branch October 28, 2024 19:14
@jonfung-dydx
Copy link
Contributor Author

@Mergifyio backport release/protocol/v6.x

Copy link
Contributor

mergify bot commented Oct 28, 2024

backport release/protocol/v6.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Oct 28, 2024
jonfung-dydx added a commit that referenced this pull request Oct 28, 2024
…2546)

Co-authored-by: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
@teddyding
Copy link
Contributor

https://github.com/Mergifyio backport release/protocol/v7.x

Copy link
Contributor

mergify bot commented Nov 12, 2024

backport release/protocol/v7.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Nov 12, 2024
teddyding pushed a commit that referenced this pull request Nov 12, 2024
…2562)

Co-authored-by: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants