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

[CT-1050] DeliverTx state change reset for subaccount updates #2063

Merged
merged 51 commits into from
Aug 12, 2024
Merged

Conversation

dydxwill
Copy link
Contributor

@dydxwill dydxwill commented Aug 8, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Introduced a new streaming mechanism for managing subaccount updates.
    • Added support for real-time updates specific to subaccounts, enhancing responsiveness and data accuracy.
    • New metrics for tracking gRPC operations related to subaccount snapshots and updates.
  • Bug Fixes

    • Improved handling of subscription management for subaccounts, ensuring timely updates.
  • Documentation

    • Updated documentation to reflect changes in subaccount management and streaming functionalities.
  • Chores

    • Streamlined CI/CD workflows by reducing redundant job definitions and enhancing maintainability.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9ca9cd7 and e6666f0.

Files selected for processing (4)
  • protocol/streaming/full_node_streaming_manager.go (15 hunks)
  • protocol/streaming/noop_streaming_manager.go (4 hunks)
  • protocol/streaming/types/interface.go (4 hunks)
  • protocol/x/clob/keeper/keeper.go (2 hunks)
Files skipped from review as they are similar to previous changes (3)
  • protocol/streaming/noop_streaming_manager.go
  • protocol/streaming/types/interface.go
  • protocol/x/clob/keeper/keeper.go
Additional comments not posted (9)
protocol/streaming/full_node_streaming_manager.go (9)

42-43: Addition of subaccountIdToSubscriptionIdMapping looks good.

This new mapping enhances the ability to manage subaccount subscriptions effectively.


59-60: Modification of OrderbookSubscription struct looks good.

The addition of subaccountIds aligns with the need to manage subaccount subscriptions.


81-86: Initialization logic for new fields looks good.

The initialization of new fields for managing subaccount subscriptions is consistent with the rest of the setup.


Line range hint 135-153: Changes to Subscribe method look good.

The method now correctly handles subaccount IDs, converting pointers to values for storage.


256-269: Changes to removeSubscription method look good.

The method now correctly iterates over subaccountIdToSubscriptionIdMapping to remove subscription IDs.


348-405: New method SendSubaccountSnapshot looks good.

The method correctly handles sending subaccount snapshots and manages errors appropriately.


498-526: New method SendSubaccountUpdates looks good.

The method correctly groups subaccount updates and caches them appropriately.


589-620: New method AddSubaccountUpdatesToCache looks good.

The caching logic is consistent with existing patterns and handles overflow correctly.


168-178: Verify subaccount ID handling in Subscribe method.

Ensure that all subaccount IDs are correctly managed and that the mapping is updated consistently.

Verification successful

Subaccount ID Handling Verified in Subscribe Method

The handling of subaccount IDs in the Subscribe method is consistent and correctly updates the mapping. The implementation ensures that new subscription IDs are appended and that cleanup occurs when IDs are removed.

  • The map is initialized and updated correctly.
  • Consistent checks and updates are performed for subaccount IDs.
  • Proper cleanup of empty slices is implemented.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all subaccount ID handling in the `Subscribe` method.

# Test: Search for subaccount ID handling. Expect: Consistent management and mapping updates.
rg --type go --context 5 'subaccountIdToSubscriptionIdMapping'

Length of output: 6295


Script:

#!/bin/bash
# Search for all occurrences of subaccountIdToSubscriptionIdMapping in the codebase
rg 'subaccountIdToSubscriptionIdMapping' --type go --context 5

Length of output: 6295

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, codebase verification and nitpick comments (2)
protocol/streaming/full_node_streaming_manager.go (2)

42-43: Add a comment for the new mapping.

The subaccountIdToSubscriptionIdMapping is a significant addition. Consider adding a comment to explain its purpose and usage.

+  // map from subaccount id to subscription ids.
  subaccountIdToSubscriptionIdMapping map[satypes.SubaccountId][]uint32

59-60: Document the subaccountIds field.

The subaccountIds field in OrderbookSubscription is a new addition. Adding a comment would improve code readability and maintainability.

+  // Subaccount ids to subscribe to.
  subaccountIds []satypes.SubaccountId
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e6666f0 and 7da102e.

Files selected for processing (1)
  • protocol/streaming/full_node_streaming_manager.go (15 hunks)
Additional comments not posted (7)
protocol/streaming/full_node_streaming_manager.go (7)

81-86: Ensure proper initialization of new fields.

The initialization of subaccountIdToSubscriptionIdMapping looks correct. Ensure that all new fields are properly initialized and used throughout the codebase.


256-269: Ensure consistency in subscription removal.

The logic for removing subscriptions from subaccountIdToSubscriptionIdMapping mirrors that for clobPairIdToSubscriptionIdMapping. Ensure consistency and correctness in both implementations.


498-526: Review the SendSubaccountUpdates method.

The SendSubaccountUpdates method is a new addition. Ensure that it correctly groups and sends subaccount updates.


588-619: Review the AddSubaccountUpdatesToCache method.

The AddSubaccountUpdatesToCache method is a new addition. Ensure that it correctly adds subaccount updates to the cache and handles buffer overflows appropriately.


Line range hint 683-714:
Verify snapshot initialization for subaccounts.

The InitializeNewStreams method now includes subaccount snapshot initialization. Ensure that this logic is correctly integrated and does not introduce race conditions.

Verification successful

Verify snapshot initialization for subaccounts in InitializeNewStreams.

The InitializeNewStreams method does indeed include logic for initializing subaccount snapshots. The method uses a sync.Once instance (subscription.initialize.Do) to ensure that snapshots are sent only once per subscription. This approach should prevent race conditions by ensuring that the initialization logic runs only once per subscription.

  • Location:
    • protocol/streaming/full_node_streaming_manager.go, lines 681-718

The integration appears correct, and the use of sync.Once helps mitigate race conditions. However, manual verification or additional testing may be necessary to ensure that this logic functions correctly in all scenarios.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the initialization of subaccount snapshots in InitializeNewStreams.

# Test: Check for correct initialization and integration. Expect: Proper handling of subaccount snapshots.
rg --type go -A 5 $'func (sm *FullNodeStreamingManagerImpl) InitializeNewStreams'

Length of output: 82


Script:

#!/bin/bash
# Description: Search for any function definitions in FullNodeStreamingManagerImpl that might relate to stream initialization.

# Test: Look for functions in FullNodeStreamingManagerImpl that might handle stream initialization.
ast-grep --lang go --pattern $'func (sm *FullNodeStreamingManagerImpl) $_($$$) {
  $$$
}'

Length of output: 39856


348-405: Review the SendSubaccountSnapshot method.

The SendSubaccountSnapshot method is a new addition. Ensure that it correctly handles subaccount snapshots and integrates well with the existing streaming logic.

Verification successful

The SendSubaccountSnapshot method is correctly implemented and integrated.

The method is defined and used within the protocol/streaming/full_node_streaming_manager.go file, indicating proper integration with the existing streaming logic. Additionally, its performance is monitored through a metric key, ensuring its operation is tracked. No issues were found with its implementation.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation and usage of the SendSubaccountSnapshot method.

# Test: Check for correct implementation and integration. Expect: Proper handling of subaccount snapshots.
rg --type go -A 5 $'func (sm *FullNodeStreamingManagerImpl) SendSubaccountSnapshot'

Length of output: 84


Script:

#!/bin/bash
# Description: Search for the SendSubaccountSnapshot method across the codebase
# and check for any references to understand its integration.

# Search for the definition of the SendSubaccountSnapshot method.
ast-grep --lang go --pattern 'func (sm *FullNodeStreamingManagerImpl) SendSubaccountSnapshot($_, $_, $_, $_) { $$$ }'

# Search for references or calls to the SendSubaccountSnapshot method to check its usage.
rg --type go 'SendSubaccountSnapshot'

Length of output: 4931


Line range hint 135-185:
Verify subaccount ID handling in Subscribe method.

The Subscribe method now includes handling for subaccount IDs. Ensure that this logic correctly integrates with existing subscription processes and that all edge cases are considered.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7da102e and 4ba411b.

Files selected for processing (2)
  • protocol/streaming/noop_streaming_manager.go (1 hunks)
  • protocol/streaming/types/interface.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/streaming/types/interface.go
Additional comments not posted (3)
protocol/streaming/noop_streaming_manager.go (3)

47-52: Confirm the purpose of the SendSubaccountUpdates method.

The SendSubaccountUpdates method is currently empty. As this class is a no-operation implementation, confirm if leaving it unimplemented is intentional.


Line range hint 13-18:
The Subscribe method changes are appropriate.

The update to handle multiple subaccounts aligns with the intended functionality. Returning types.ErrNotImplemented is suitable for a no-operation implementation.


Line range hint 38-45:
The InitializeNewStreams method changes are appropriate.

The addition of the parameter for subaccount snapshots is consistent with the PR's objectives and appears to be correctly integrated.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4ba411b and d63bec6.

Files selected for processing (1)
  • protocol/x/subaccounts/keeper/subaccount.go (1 hunks)
Additional comments not posted (2)
protocol/x/subaccounts/keeper/subaccount.go (2)

135-161: LGTM! The GetStreamSubaccountUpdate function is well-implemented.

The method effectively retrieves subaccount data and formats it for streaming. The use of BigInt().Uint64() for quantums conversion is appropriate. The logic is clear and aligns with the intended functionality.


Line range hint 303-310:
LGTM! The SendSubaccountUpdates function is effectively implemented.

The function appropriately checks for the presence of updates before sending them to the gRPC streaming manager. This ensures efficient use of resources and aligns with best practices.

@@ -75,6 +91,21 @@ func (k Keeper) ProcessProposerOperations(
allUpdates.Append(orderbookUpdate)
}
k.SendOrderbookUpdates(ctx, allUpdates)

// send subaccount snapshots
subaccountIdsFromProposed := fetchSubaccountIdsInvolvedInOpQueue(
Copy link
Contributor

Choose a reason for hiding this comment

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

super meganit, up to you: combine the two loops where we iterate through the opqueue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

only sending local for now

SubaccountId: &id,
UpdatedAssetPositions: assetPositions,
UpdatedPerpetualPositions: perpetualPositions,
Snapshot: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a snapshot

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why isn't it? We are getting the subaccount from state which should have all asset/perp positions?

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d63bec6 and 518220f.

Files selected for processing (1)
  • protocol/x/clob/keeper/process_operations.go (3 hunks)
Additional comments not posted (3)
protocol/x/clob/keeper/process_operations.go (3)

41-54: LGTM: Efficient retrieval of subaccount IDs.

The function efficiently collects subaccount IDs involved in matches using a map to avoid duplicates. The use of lib.MergeMaps is appropriate here.


76-79: Clarify subaccount snapshot criteria.

The comment provides a clear definition of impacted subaccounts. Ensure this aligns with the actual implementation logic.


98-111: Consider combining loops for efficiency.

The loops for processing order IDs and subaccount IDs could potentially be combined to improve efficiency, as suggested in previous reviews.

@dydxwill dydxwill merged commit 4bc25b8 into main Aug 12, 2024
13 checks passed
@dydxwill dydxwill deleted the wl/sa3 branch August 12, 2024 21:04
jonfung-dydx added a commit that referenced this pull request Aug 15, 2024
jonfung-dydx added a commit that referenced this pull request Sep 17, 2024
jonfung-dydx added a commit that referenced this pull request Sep 26, 2024
jonfung-dydx added a commit that referenced this pull request Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants