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-1190] Emit FinalizeBlock updates in single batch. #2260

Merged
merged 3 commits into from
Sep 16, 2024
Merged

Conversation

teddyding
Copy link
Contributor

@teddyding teddyding commented Sep 16, 2024

Changelist

Stream FinalizeBlock updates in a batch. Add to cache and flush before and after.

Test Plan

TODO: test in validation mode

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

Summary by CodeRabbit

  • New Features

    • Introduced StagedFinalizeBlockEvent for managing events during block finalization.
    • Added methods for staging and streaming updates related to order fills and subaccount updates.
    • Implemented a new constant for tracking streaming updates after block finalization.
  • Bug Fixes

    • Streamlined handling of order book fills by directly staging updates.
  • Refactor

    • Restructured the dydxprotocol namespace for improved clarity and maintainability.
    • Enhanced the FullNodeStreamingManager to better manage event staging and streaming.
  • Documentation

    • Updated comments and structure for clarity on new functionalities and methods.

Copy link

linear bot commented Sep 16, 2024

@teddyding teddyding changed the title [CT-1190)] Emit FinalizeBlock updates in single batch. [CT-1190] Emit FinalizeBlock updates in single batch. Sep 16, 2024
Copy link
Contributor

coderabbitai bot commented Sep 16, 2024

Walkthrough

The pull request introduces significant restructuring within the dydxprotocol namespace, including the addition of new modules and the reorganization of existing imports and exports. Key changes include the introduction of the StagedFinalizeBlockEvent interface and related functionalities for managing block finalization events. Additionally, new constants and methods for handling streaming updates and subaccount modifications are implemented across various files, enhancing the protocol's overall structure and functionality.

Changes

Files Change Summary
indexer/packages/v4-protos/src/codegen/dydxprotocol/bundle.ts, clob/streaming.ts Restructured dydxprotocol namespace; added multiple exports including ClientFactory, clob, and StagedFinalizeBlockEvent interface with encoding/decoding methods.
indexer/packages/v4-protos/src/codegen/gogoproto/bundle.ts, google/bundle.ts Minor modifications in import statements; no changes to exported entities.
proto/dydxprotocol/clob/streaming.proto Introduced StagedFinalizeBlockEvent message with oneof construct for event handling during block finalization.
protocol/app/app.go Added a transient store key for the streaming manager; modified function parameters to include this key.
protocol/lib/metrics/constants.go, metric_keys.go Added new constants for streaming updates and gRPC metrics tracking.
protocol/streaming/constants.go Defined constants for managing streaming events in the FullNodeStreamingManager.
protocol/streaming/full_node_streaming_manager.go, noop_streaming_manager.go Enhanced FullNodeStreamingManagerImpl with new methods for staging and retrieving finalize block events; added placeholder methods in NoopGrpcStreamingManager.
protocol/streaming/types/interface.go Added methods to the FullNodeStreamingManager interface for staging and streaming updates.
protocol/x/clob/abci.go, keeper/grpc_stream_finalize_block.go, process_operations.go Implemented new functions for handling updates related to order fills and simplified existing methods for managing order book updates.
protocol/x/subaccounts/keeper/subaccount.go Modified method for sending finalized subaccount updates to streamline processing.

Possibly related PRs

Suggested labels

indexer, proto

🐰 In the meadow, changes bloom,
New structures rise, dispel the gloom.
Events now staged, updates in line,
A dancing protocol, oh so fine!
With every hop, we celebrate,
A brighter future, we create! 🌼


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 generate interesting stats about this repository and render them as a table.
    -- @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.

@teddyding teddyding marked this pull request as ready for review September 16, 2024 16:22
@teddyding teddyding requested a review from a team as a code owner September 16, 2024 16:22
@teddyding teddyding changed the base branch from main to td/fns-oe-refactored September 16, 2024 16:23
@@ -490,19 +490,11 @@ func (sm *FullNodeStreamingManagerImpl) TracksSubaccountId(subaccountId satypes.
return exists
}

// SendOrderbookUpdates groups updates by their clob pair ids and
// sends messages to the subscribers.
func (sm *FullNodeStreamingManagerImpl) SendOrderbookUpdates(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the 3 functions SendOrderbookUpdates, SendOrderbookFillUpdates, SendFinalizedSubaccountUpdates, we are just taking out a few lines into a helper function. No logic change

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.

Caution

Inline review comments failed to post

Actionable comments posted: 1

Outside diff range and nitpick comments (2)
protocol/x/clob/keeper/process_operations.go (2)

545-547: LGTM! The change simplifies the code and improves efficiency.

Replacing the previous method of sending order book fill updates with a direct call to StageFinalizeBlockFill streamlines the process and reduces complexity. This is a positive change that improves code readability and maintainability.

To ensure the robustness of this change, consider adding unit tests that verify:

  1. The behavior of StageFinalizeBlockFill in staging the order book fill for streaming.
  2. The end-to-end flow of streaming order book fills to confirm that it still works as expected after this modification.

652-654: LGTM! The change simplifies the code and improves efficiency, similar to PersistMatchOrdersToState.

Replacing the previous logic of sending order book fill updates with a direct call to StageFinalizeBlockFill streamlines the process and reduces complexity for liquidation fills as well. This is a positive change that improves code readability and maintainability.

To ensure the robustness of this change, consider adding unit tests that verify:

  1. The behavior of StageFinalizeBlockFill in staging the liquidation fill for streaming.
  2. The end-to-end flow of streaming liquidation fills to confirm that it still works as expected after this modification.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 03454e4 and e996b1a.

Files ignored due to path filters (1)
  • protocol/x/clob/types/streaming.pb.go is excluded by !**/*.pb.go
Files selected for processing (16)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/bundle.ts (2 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/streaming.ts (1 hunks)
  • indexer/packages/v4-protos/src/codegen/gogoproto/bundle.ts (1 hunks)
  • indexer/packages/v4-protos/src/codegen/google/bundle.ts (1 hunks)
  • proto/dydxprotocol/clob/streaming.proto (1 hunks)
  • protocol/app/app.go (4 hunks)
  • protocol/lib/metrics/constants.go (1 hunks)
  • protocol/lib/metrics/metric_keys.go (1 hunks)
  • protocol/streaming/constants.go (1 hunks)
  • protocol/streaming/full_node_streaming_manager.go (12 hunks)
  • protocol/streaming/noop_streaming_manager.go (1 hunks)
  • protocol/streaming/types/interface.go (1 hunks)
  • protocol/x/clob/abci.go (1 hunks)
  • protocol/x/clob/keeper/grpc_stream_finalize_block.go (1 hunks)
  • protocol/x/clob/keeper/process_operations.go (3 hunks)
  • protocol/x/subaccounts/keeper/subaccount.go (1 hunks)
Files skipped from review due to trivial changes (2)
  • indexer/packages/v4-protos/src/codegen/gogoproto/bundle.ts
  • indexer/packages/v4-protos/src/codegen/google/bundle.ts
Additional context used
buf
proto/dydxprotocol/clob/streaming.proto

4-4: import "dydxprotocol/subaccounts/streaming.proto": file does not exist

(COMPILE)

Additional comments not posted (34)
protocol/streaming/constants.go (1)

1-13: LGTM!

The constants are well-defined and their purposes are clearly documented. The naming conventions are consistent and the constants serve a clear purpose in the FullNodeStreamingManager.

proto/dydxprotocol/clob/streaming.proto (3)

1-2: LGTM!

The syntax and package declarations are correct.


7-7: LGTM!

The Go package option is correct.


9-16: LGTM!

The StagedFinalizeBlockEvent message type is well-defined and follows the protocol buffer language syntax. The oneof field allows for flexibility in the types of events that can be processed during the FinalizeBlock process.

protocol/x/clob/keeper/grpc_stream_finalize_block.go (2)

15-29: LGTM!

The function getUpdatesToSyncLocalOpsQueue is well-structured and effectively retrieves the necessary order book updates to synchronize the local operations queue with the latest on-chain state after block finalization. It plays a crucial role in ensuring that the order fill amounts are accurately reflected in the local state.

The logic is clear and follows these steps:

  1. Retrieve the local validator's operations queue.
  2. Fetch the order IDs involved in the operations queue.
  3. For each order ID, retrieve the order book updates.
  4. Append all the updates to a single OffchainUpdates object.

The function effectively reverts the optimistic fill amounts that were adjusted during the transaction state transitions, ensuring consistency between the local and on-chain state.


34-50: LGTM!

The function StreamBatchUpdatesAfterFinalizeBlock is well-structured and effectively streams updates after the consensus has finalized a block. It plays a crucial role in ensuring that the local operations queue is synchronized with the latest on-chain state.

The logic is clear and follows these steps:

  1. Measure the time taken for streaming updates using telemetry.MeasureSince.
  2. Call getUpdatesToSyncLocalOpsQueue to retrieve the necessary order book updates.
  3. Pass the retrieved updates and the PerpetualIdToClobPairId mapping to the StreamBatchUpdatesAfterFinalizeBlock method of the full node's streaming manager.

The use of telemetry helps in monitoring the performance of this operation, which is valuable for identifying potential bottlenecks or issues.

Overall, the function is well-designed and contributes to maintaining consistency between the local and on-chain state after block finalization.

protocol/streaming/types/interface.go (4)

53-56: LGTM!

The StageFinalizeBlockFill method is a valuable addition to the FullNodeStreamingManager interface. It allows for the staging of fill events during the block finalization process, which is crucial for managing the flow of events in a streaming architecture.

The method signature and parameters are well-defined and appropriate for the intended functionality.


57-60: LGTM!

The StageFinalizeBlockSubaccountUpdate method is a valuable addition to the FullNodeStreamingManager interface. It allows for the staging of subaccount update events during the block finalization process, which is crucial for handling changes to subaccount states.

The method signature and parameters are well-defined and appropriate for the intended functionality.


61-63: LGTM!

The GetStagedFinalizeBlockEvents method is a valuable addition to the FullNodeStreamingManager interface. It complements the staging methods by providing a way to retrieve the staged events for block finalization.

The method signature and parameters are well-defined and appropriate for the intended functionality.


65-69: LGTM!

The StreamBatchUpdatesAfterFinalizeBlock method is a valuable addition to the FullNodeStreamingManager interface. It enhances the synchronization of updates related to order books and their associated operations after block finalization.

The method signature and parameters are well-defined and appropriate for the intended functionality. The orderBookUpdatesToSyncLocalOpsQueue parameter represents the off-chain updates that need to be synchronized, while the perpetualIdToClobPairId parameter provides the necessary mapping for associating updates with specific order books.

This method is crucial for maintaining consistency between the off-chain updates and the local operations queue in a streaming architecture.

indexer/packages/v4-protos/src/codegen/dydxprotocol/clob/streaming.ts (4)

1-4: LGTM!

The imports are correctly defined and follow the appropriate naming conventions.


7-16: LGTM!

The StagedFinalizeBlockEvent interface and its SDK type are correctly defined with the appropriate properties and types.


18-23: LGTM!

The createBaseStagedFinalizeBlockEvent function correctly returns a StagedFinalizeBlockEvent object with undefined properties, which serves as a base for creating new instances of the event.


25-71: LGTM!

The StagedFinalizeBlockEvent object is correctly defined with the following methods:

  • encode: Serializes the event data into a binary format using Protocol Buffers.
  • decode: Reconstructs the event from a binary input by iterating over the tags and decoding the corresponding properties.
  • fromPartial: Creates a StagedFinalizeBlockEvent instance from a partial object by calling the fromPartial methods of the respective properties.

The implementation follows the expected patterns for encoding, decoding, and creating instances from partial data.

protocol/streaming/noop_streaming_manager.go (4)

82-86: Skipped reviewing the empty method.

The StageFinalizeBlockFill method is part of the NoopGrpcStreamingManager struct, which is a no-op implementation of the streaming manager. The method body is empty, indicating that the functionality is not yet implemented.


88-92: Skipped reviewing the no-op method.

The GetStagedFinalizeBlockEvents method is part of the NoopGrpcStreamingManager struct, which is a no-op implementation of the streaming manager. The method always returns nil, indicating that no events are staged.


94-98: Skipped reviewing the empty method.

The StageFinalizeBlockSubaccountUpdate method is part of the NoopGrpcStreamingManager struct, which is a no-op implementation of the streaming manager. The method body is empty, indicating that the functionality is not yet implemented.


100-105: Skipped reviewing the empty method.

The StreamBatchUpdatesAfterFinalizeBlock method is part of the NoopGrpcStreamingManager struct, which is a no-op implementation of the streaming manager. The method body is empty, indicating that the functionality is not yet implemented.

protocol/lib/metrics/metric_keys.go (1)

86-88: LGTM!

The addition of new gRPC metrics to track the counts of staged updates for finalized blocks aligns with the PR objective and enhances the monitoring capabilities. The metric names follow the existing naming conventions and provide more granular insights into the performance and behavior of block finalization processes.

protocol/x/clob/abci.go (1)

49-58: LGTM!

The Precommit function is a well-structured addition that enhances the clob module's functionality by conditionally managing streaming updates based on the state of the streaming manager. The logic is clear, and the function follows best practices by avoiding unnecessary processing when the streaming manager is disabled.

The function is unlikely to introduce any breaking changes or compatibility issues, as it is a new addition and does not modify existing behavior.

indexer/packages/v4-protos/src/codegen/dydxprotocol/bundle.ts (2)

39-194: Significant restructuring of imports.

The changes to the imports reflect a substantial reorganization of the codebase, with the introduction of several new modules across various domains such as clob, daemons, delaymsg, epochs, govplus, indexer, listing, perpetuals, prices, ratelimit, revshare, rewards, sending, stats, subaccounts, vault, and vest. Additionally, the order of some existing imports has been modified.

These changes suggest an effort to introduce new functionalities and potentially optimize the structure of the code. The broad scope of the modifications indicates a significant impact on the overall protocol.


248-405: Comprehensive expansion of the dydxprotocol namespace.

The dydxprotocol namespace has undergone a significant expansion with the addition of several new exported modules such as daemons, delaymsg, epochs, govplus, indexer, listing, perpetuals, prices, ratelimit, revshare, rewards, sending, stats, subaccounts, vault, and vest. These new modules cover a wide range of aspects within the protocol, indicating a comprehensive upgrade of the system's capabilities.

Furthermore, the existing exported modules within the dydxprotocol namespace have been updated to include additional components from the newly imported modules, enhancing their functionality.

The introduction of the ClientFactory export suggests potential changes in the management of client interactions within the protocol.

Overall, the modifications to the exports align with the changes observed in the imports, reflecting a coordinated effort to restructure and enhance the codebase.

protocol/lib/metrics/constants.go (2)

205-205: LGTM!

The new constant StreamBatchUpdatesAfterFinalizeBlock is declared correctly and follows the naming convention used in the file. The constant's name suggests that it is related to a new feature or functionality for streaming batch updates after a block has been finalized.


205-205: Verify the usage of the new constant in the codebase.

The new constant StreamBatchUpdatesAfterFinalizeBlock is public (exported), which indicates that it is intended to be used by other packages. Please ensure that the constant is being used appropriately in the codebase.

Run the following script to verify the usage of the new constant:

Verification successful

Constant usage verified and found to be appropriate

The new constant StreamBatchUpdatesAfterFinalizeBlock is being used correctly and consistently throughout the codebase. It's implemented in streaming managers, used in the CLOB module, defined in the streaming interface, and even used for performance metrics. This indicates that it's a core part of the streaming functionality in the protocol and is being utilized as intended.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new constant `StreamBatchUpdatesAfterFinalizeBlock` in the codebase.

# Test: Search for the constant usage. Expect: At least one occurrence of the constant being used.
rg --type go -C 5 $'StreamBatchUpdatesAfterFinalizeBlock'

Length of output: 6104

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

448-450: Simplify sending finalized subaccount updates. LGTM!

The change simplifies the code by directly passing the subaccountUpdate object to the StageFinalizeBlockSubaccountUpdate method, instead of wrapping it in a slice. This eliminates the overhead of creating a slice for a single item.

The core functionality remains unaltered and the conditional checks are preserved. Overall, this is a clean refactor that potentially improves efficiency without introducing issues.

protocol/x/clob/keeper/process_operations.go (1)

Line range hint 533-544: Verify the impact of removing the gRPC streaming logic.

The code block that handled sending absolute fill amounts to a gRPC stream when gRPC streaming was enabled has been removed. This includes the logic to merge order IDs from both local and proposed operation queues and send order book updates based on these merged IDs.

Please ensure that:

  1. The removal of this code block doesn't break any existing functionality.
  2. The order book updates are still being handled correctly after this change.

To verify the impact, run the following script:

If Test 1 returns any results, it indicates that there are still references to the removed code block which need to be cleaned up.

If Test 2 doesn't return any results, it suggests that the order book updates might not be sent anymore after this change. In that case, further investigation would be required to ensure the updates are being handled correctly elsewhere.

Verification successful

Verification successful: gRPC streaming removal and order book updates

The removal of the gRPC streaming logic appears to have been implemented correctly:

  • No references to the removed code block were found, indicating a clean removal.
  • Order book updates are still being sent using k.SendOrderbookFillUpdates in the process_operations.go file.

These findings suggest that the changes have been made without breaking existing functionality, and order book updates are still being handled appropriately.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash

# Description: 
# - Verify that the gRPC streaming functionality is handled correctly elsewhere.
# - Check if there are any references to the removed code block.

# Test 1: Search for references to the removed code block
echo "Searching for references to the removed gRPC streaming code block..."
rg --type go -A 5 $'mergedOrderIds := lib.MergeMaps\(localOrderIds, proposedOrderIds\)'

# Test 2: Verify that the order book updates are still being sent
echo "Verifying if order book updates are still being sent..."
rg --type go -A 5 $'k\.SendOrderbookFillUpdates'

Length of output: 787

protocol/streaming/full_node_streaming_manager.go (7)

58-60: LGTM!

The streamingManagerTransientStoreKey field is a valid addition to the FullNodeStreamingManagerImpl struct. It is properly initialized in the constructor and will be useful for managing staged events in the full node streaming process.

Also applies to: 97-97, 115-116


381-387: LGTM!

The getStagedEventsCount function is a well-structured helper that encapsulates the logic of retrieving the staged events count from the transient store. It handles the case when the count bytes are not found and follows good practices.


389-423: LGTM!

The StageFinalizeBlockSubaccountUpdate and StageFinalizeBlockFill methods provide a clean way to stage specific events during the FinalizeBlock process. By storing the events in the transient store, they ensure correct processing even with multiple FinalizeBlock calls. The methods are well-structured and utilize the stageFinalizeBlockEvent helper effectively.


425-445: LGTM!

The getStagedFinalizeBlockEvents function and GetStagedFinalizeBlockEvents method provide an efficient and clean way to retrieve all staged FinalizeBlock events from the transient store. The function encapsulates the retrieval logic well, first getting the count and then iterating over the stored event bytes. The method exposes this functionality using a gas-free context. Both are well-structured and follow good practices.


447-461: LGTM!

The stageFinalizeBlockEvent function encapsulates the common logic of storing a staged FinalizeBlock event in the transient store. It efficiently manages the event count and stores the event bytes using a gas-free context. The function is well-structured and follows good practices.


Line range hint 493-530: LGTM!

The refactored getStreamUpdatesFromOffchainUpdates function and the new getStreamUpdatesForOrderbookFills and getStreamUpdatesForSubaccountUpdates functions improve the modularity and readability of the code.

  • The refactored getStreamUpdatesFromOffchainUpdates returns both stream updates and clob pair IDs, making it easier to associate updates with their identifiers.
  • The getStreamUpdatesForOrderbookFills function encapsulates the logic of getting stream updates for orderbook fills, handling deleveraging fills as well.
  • Similarly, the getStreamUpdatesForSubaccountUpdates function encapsulates the logic for subaccount updates.

These changes enhance the structure and organization of the code.

Also applies to: 551-586, 640-663


843-924: LGTM!

The StreamBatchUpdatesAfterFinalizeBlock method and its related functions significantly improve the streaming process after a block is finalized.

  • The method consolidates the streaming of updates, ensuring that all relevant updates, including staged events and orderbook updates, are sent in a batch. This enhances the efficiency of the update handling process.
  • The addBatchUpdatesToCache function encapsulates the logic of adding batch updates to the cache, improving modularity.
  • The getStagedEventsFromFinalizeBlock function provides a clear separation of retrieving staged events after consensus, enhancing readability.

These changes streamline the post-finalization streaming process and improve the overall structure of the code.

Also applies to: 926-961

protocol/app/app.go (1)

Line range hint 2064-2078: Streaming manager now utilizes a transient store.

The changes look good. The streaming manager's constructor now accepts a streamingManagerTransientStoreKey argument, allowing it to interact with a dedicated transient store. This enhances the streaming manager's capabilities.

Verify that all calls to getFullNodeStreamingManagerFromOptions have been updated to pass the new streamingManagerTransientStoreKey argument. Run this script to check:

Verification successful

Function call to getFullNodeStreamingManagerFromOptions correctly updated

The verification process confirms that the call to getFullNodeStreamingManagerFromOptions has been properly updated to include the new streamingManagerTransientStoreKey argument. The function is called with tkeys[streaming.StreamingManagerTransientStoreKey] as the last argument, which correctly corresponds to the new parameter in the function signature.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all calls to `getFullNodeStreamingManagerFromOptions` pass the new argument.

# Test: Search for the function calls. Expect: All calls passing the new argument.
rg --type go $'getFullNodeStreamingManagerFromOptions' -A 5

Length of output: 976

Comments failed to post (1)
proto/dydxprotocol/clob/streaming.proto (1)

4-4: Fix the missing import file.

The static analysis tool reports that the file dydxprotocol/subaccounts/streaming.proto does not exist. Please ensure that the file exists and the import path is correct.

Do you want me to help you find the correct import path or open a GitHub issue to track this task?

Tools
buf

4-4: import "dydxprotocol/subaccounts/streaming.proto": file does not exist

(COMPILE)

Comment on lines 905 to 906
sm.Lock()
defer sm.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

for my understanding - why do we need to lock before getStreamUpdatesForSubaccountUpdates?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't; just wanted to lock before the 1st flush. I re-ordered the code blocks a bit to optimize

Base automatically changed from td/fns-oe-refactored to main September 16, 2024 16:41
@@ -796,6 +840,42 @@ func (sm *FullNodeStreamingManagerImpl) GetSubaccountSnapshotsForInitStreams(
return ret
}

func (sm *FullNodeStreamingManagerImpl) addBatchUpdatesToCache(
Copy link
Contributor

Choose a reason for hiding this comment

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

Docstring comment for when this is used / what exactly the batching does?

Copy link
Contributor

@jonfung-dydx jonfung-dydx Sep 16, 2024

Choose a reason for hiding this comment

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

might be good to add a comments saying this should never be called without a lock since we're modifying streamUpdateCache

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)
protocol/streaming/full_node_streaming_manager.go (1)

843-882: Looks good, but add a comment about the lock requirement.

The function correctly adds the batched updates to the cache and maintains the subscription mappings. The logic is clear and well-structured.

Add a comment at the beginning of the function to make it clear that the caller must acquire the lock before calling this function:

// addBatchUpdatesToCacheWithLock adds batched updates to the cache.
// Note: The caller must acquire the lock before calling this function.
func (sm *FullNodeStreamingManagerImpl) addBatchUpdatesToCacheWithLock(...) {
  ...
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e996b1a and ac7224a.

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

Line range hint 493-530: LGTM!

The function correctly groups offchain updates by clob pair id and converts them to stream updates. The logic is clear and the implementation looks good.


533-549: Looks good!

The refactoring to use getStreamUpdatesFromOffchainUpdates simplifies the function and improves code reuse. The logic flow is straightforward.


Line range hint 551-586: Nicely done!

The function correctly handles both regular and deleveraging fills, and groups them by clob pair id to create stream updates. The logic is well-structured and easy to follow.


590-607: Looks good to me!

The refactoring to use getStreamUpdatesForOrderbookFills simplifies the function and improves code modularity. The logic flow is clear and concise.


640-663: Looks good!

The function correctly groups subaccount updates and converts them to stream updates. The implementation is straightforward and easy to understand.


Line range hint 883-930: Great work!

The refactoring to use the new helper functions improves code modularity and readability. The logic flow is clear and well-structured, with the lock being acquired and released appropriately.

Batching the updates and flushing them before and after adding the staged updates ensures that the onchain updates are emitted in a single batch, which is great for performance and consistency.

@teddyding teddyding merged commit 9fe7566 into main Sep 16, 2024
21 checks passed
@teddyding teddyding deleted the td/batch-update branch September 16, 2024 22:17
jonfung-dydx added a commit that referenced this pull request Sep 17, 2024
@coderabbitai coderabbitai bot mentioned this pull request Sep 18, 2024
6 tasks
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.

3 participants