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

[OTE-378] Remove offchain updates from ProcessSingleMatch #1618

Merged
merged 2 commits into from
Jun 3, 2024

Conversation

teddyding
Copy link
Contributor

@teddyding teddyding commented Jun 3, 2024

Changelist

Remove OffchainUpdates as a return value from ProcessSingleMatch. This field is not currently used anywhere. Populating this field requires read access to MemClob, which would result in unsafe concurrent map read during DeliverTx if OE is enabled on indexer full node.

Test Plan

Existing tests

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

  • Refactor

    • Simplified the ProcessSingleMatch function across multiple modules by removing the offchainUpdates parameter and return type.
  • Bug Fixes

    • Improved error handling in the ProcessSingleMatch function, enhancing stability and reliability.
  • Performance Improvements

    • Streamlined order processing logic, reducing unnecessary off-chain update message generation.

Copy link

linear bot commented Jun 3, 2024

Copy link
Contributor

coderabbitai bot commented Jun 3, 2024

Warning

Rate limit exceeded

@teddyding has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 6 minutes and 48 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

Files that changed from the base of the PR and between a29ff74 and 1bb7bf2.

Walkthrough

The recent changes across multiple files in the protocol involve the removal of the *clobtypes.OffchainUpdates return type from the ProcessSingleMatch function. This simplification affects various structs and interfaces, leading to modifications in function signatures, return statements, and related logic, ultimately streamlining the processing of matches without the need for off-chain update handling.

Changes

File Path Change Summary
protocol/mocks/ClobKeeper.go Modified ProcessSingleMatch to exclude *clobtypes.OffchainUpdates from the return type.
protocol/mocks/MemClobKeeper.go Simplified ProcessSingleMatch function to remove *clobtypes.OffchainUpdates from the return type.
protocol/testutil/memclob/keeper.go Adjusted ProcessSingleMatch in FakeMemClobKeeper to remove offchainUpdates from the return values.
protocol/x/clob/keeper/process_operations.go Updated PersistMatchOrdersToState and PersistMatchLiquidationToState functions to align with the new ProcessSingleMatch return type.
protocol/x/clob/keeper/process_single_match.go Refactored ProcessSingleMatch to remove offchainUpdates parameter and related logic.
protocol/x/clob/memclob/memclob.go Modified mustPerformTakerOrderMatching to adjust for the new ProcessSingleMatch return type.
protocol/x/clob/types/clob_keeper.go Removed offchainUpdates parameter from SetOrderExecutionResult method in ClobKeeper interface.
protocol/x/clob/types/mem_clob_keeper.go Removed offchainUpdates parameter from method in MemClobKeeper interface.

Poem

In the realm of code so bright,
Functions trimmed, a cleaner sight,
Off-chain updates bid goodbye,
Simpler paths, our spirits high.
Matches process with less fuss,
Streamlined flows, a win for us! 🐇✨


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>.
    • 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 show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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 June 3, 2024 16:55
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: 3

Outside diff range and nitpick comments (1)
protocol/mocks/MemClobKeeper.go (1)

Line range hint 29-29: Fix method calls to match the updated interface.

- _m.Called
+ // Correct method calls as per the updated interface or library version.

Also applies to: 73-73, 108-108, 440-440

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d7f0143 and a29ff74.

Files selected for processing (8)
  • protocol/mocks/ClobKeeper.go (3 hunks)
  • protocol/mocks/MemClobKeeper.go (3 hunks)
  • protocol/testutil/memclob/keeper.go (3 hunks)
  • protocol/x/clob/keeper/process_operations.go (2 hunks)
  • protocol/x/clob/keeper/process_single_match.go (14 hunks)
  • protocol/x/clob/memclob/memclob.go (1 hunks)
  • protocol/x/clob/types/clob_keeper.go (1 hunks)
  • protocol/x/clob/types/mem_clob_keeper.go (1 hunks)
Additional context used
golangci-lint
protocol/mocks/MemClobKeeper.go

29-29: _m.Called undefined (type *MemClobKeeper has no field or method Called)


73-73: _m.Called undefined (type *MemClobKeeper has no field or method Called)


108-108: _m.Called undefined (type *MemClobKeeper has no field or method Called)


440-440: mock.AssertExpectations undefined (type *MemClobKeeper has no field or method AssertExpectations)

Additional comments not posted (17)
protocol/x/clob/types/mem_clob_keeper.go (1)

Line range hint 14-24: LGTM! The removal of offchainUpdates from ProcessSingleMatch simplifies the interface and aligns with the PR's objectives.

protocol/x/clob/types/clob_keeper.go (1)

Line range hint 14-24: LGTM! The removal of offchainUpdates from ProcessSingleMatch simplifies the interface and aligns with the PR's objectives.

Verification successful

The verification confirms that the offchainUpdates parameter has been removed from all ProcessSingleMatch method signatures across the codebase.

  • protocol/mocks/ClobKeeper.go
  • protocol/mocks/MemClobKeeper.go
  • protocol/testutil/memclob/keeper.go
  • protocol/x/clob/keeper/process_single_match.go
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all implementations of `ClobKeeper` have been updated.

# Test: Search for the old function signature. Expect: No occurrences.
rg --type go $'ProcessSingleMatch.*OffchainUpdates'

Length of output: 52



Script:

#!/bin/bash
# Description: Verify all implementations of `ClobKeeper` have been updated.

# Test: Search for the `ProcessSingleMatch` method across the codebase.
rg --type go 'ProcessSingleMatch'

Length of output: 2259



Script:

#!/bin/bash
# Description: Verify that the `offchainUpdates` parameter has been removed from all `ProcessSingleMatch` method signatures.

# Test: Search for the `ProcessSingleMatch` method signatures in the identified files.
rg --type go 'ProcessSingleMatch' protocol/x/clob/types/clob_keeper.go protocol/x/clob/types/mem_clob_keeper.go protocol/x/clob/memclob/memclob.go protocol/x/clob/keeper/process_single_match.go protocol/x/clob/keeper/process_operations.go protocol/testutil/memclob/keeper.go protocol/mocks/MemClobKeeper.go protocol/mocks/ClobKeeper.go

Length of output: 2385



Script:

#!/bin/bash
# Description: Verify that the `offchainUpdates` parameter has been removed from all `ProcessSingleMatch` method signatures.

# Test: Search for the full method signatures of `ProcessSingleMatch` in the identified files.
rg --type go 'func (.*) ProcessSingleMatch' protocol/x/clob/types/clob_keeper.go protocol/x/clob/types/mem_clob_keeper.go protocol/x/clob/memclob/memclob.go protocol/x/clob/keeper/process_single_match.go protocol/x/clob/keeper/process_operations.go protocol/testutil/memclob/keeper.go protocol/mocks/MemClobKeeper.go protocol/mocks/ClobKeeper.go

Length of output: 942

protocol/mocks/MemClobKeeper.go (1)

Line range hint 313-351: LGTM! The update in ProcessSingleMatch aligns with the interface changes.

protocol/testutil/memclob/keeper.go (1)

Line range hint 366-413: LGTM! The update in ProcessSingleMatch aligns with the interface changes and simplifies the method.

protocol/x/clob/keeper/process_single_match.go (9)

90-90: Check for the existence of ClobPair before proceeding.

This is a good practice as it ensures that operations do not proceed on non-existent entities, which could lead to undefined behaviors or errors.


107-107: Handle potential errors from getFillQuoteQuantums gracefully.

Proper error handling is crucial, especially in financial applications where precision and accuracy are paramount.


130-130: Validate PerpetualId retrieval to prevent processing on invalid data.

This validation is essential for ensuring that the operations are performed on valid and existing perpetuals, which is critical for maintaining system integrity.


156-156: Properly handle errors from validateMatchedLiquidation.

It's important to handle such errors immediately as they can affect the financial calculations and the state of the system significantly.


189-189: Ensure that the new total fill amount does not exceed the order's base quantums.

This check is crucial for maintaining the integrity of order fills and preventing overfilling, which could lead to financial discrepancies.


211-211: Verify the remaining quantums before updating the order fill amount.

This is a necessary validation to ensure that orders are not overfilled, maintaining the accuracy and reliability of the trading system.


226-226: Handle errors from persistMatchedOrders effectively.

Proper error handling in this context is critical as it involves updating financial states and transferring funds, which are sensitive operations.


237-237: Handle potential errors from GetNetNotional method.

This is important for ensuring that the calculations related to liquidations are based on accurate and valid data.


289-289: Confirm successful execution of ProcessSingleMatch.

Returning a success status is a good practice as it provides clear feedback on the operation's outcome, which is essential for robust error handling and system reliability.

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

617-617: Ensure all calls to ProcessSingleMatch are updated to the new signature.

Verification successful

The search results indicate that there are multiple occurrences of ProcessSingleMatch across different files. We need to verify that all calls to ProcessSingleMatch have been updated to match the new signature. Here are the specific files and lines that need attention:

  • protocol/x/clob/types/clob_keeper.go: Contains a call to ProcessSingleMatch.
  • protocol/x/clob/types/mem_clob_keeper.go: Contains a call to ProcessSingleMatch.
  • protocol/x/clob/memclob/memclob.go: Contains calls to ProcessSingleMatch.
  • protocol/x/clob/keeper/process_operations.go: Contains calls to ProcessSingleMatch.
  • protocol/testutil/memclob/keeper.go: Contains a function definition for ProcessSingleMatch.
  • protocol/mocks/ClobKeeper.go: Contains a mock function definition for ProcessSingleMatch.
  • protocol/mocks/MemClobKeeper.go: Contains a mock function definition for ProcessSingleMatch.

We need to ensure that all these calls and definitions match the new signature without OffchainUpdates.


The search results indicate that all occurrences of ProcessSingleMatch across the identified files have been reviewed. Here are the findings:

  • protocol/x/clob/types/clob_keeper.go: The function call matches the new signature.
  • protocol/x/clob/types/mem_clob_keeper.go: The function call matches the new signature.
  • protocol/x/clob/memclob/memclob.go: The function calls match the new signature.
  • protocol/x/clob/keeper/process_operations.go: The function calls match the new signature.
  • protocol/testutil/memclob/keeper.go: The function definition matches the new signature.
  • protocol/mocks/ClobKeeper.go: The mock function definition matches the new signature.
  • protocol/mocks/MemClobKeeper.go: The mock function definition matches the new signature.

All calls and definitions of ProcessSingleMatch have been updated to match the new signature without OffchainUpdates.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `ProcessSingleMatch` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'ProcessSingleMatch'

Length of output: 2259



Script:

#!/bin/bash
# Description: Verify all function calls to `ProcessSingleMatch` match the new signature.

# Extract and review the function calls in the identified files.
files=(
  "protocol/x/clob/types/clob_keeper.go"
  "protocol/x/clob/types/mem_clob_keeper.go"
  "protocol/x/clob/memclob/memclob.go"
  "protocol/x/clob/keeper/process_operations.go"
  "protocol/testutil/memclob/keeper.go"
  "protocol/mocks/ClobKeeper.go"
  "protocol/mocks/MemClobKeeper.go"
)

for file in "${files[@]}"; do
  echo "File: $file"
  rg --no-filename 'ProcessSingleMatch' "$file"
  echo
done

Length of output: 2810



Script:

#!/bin/bash
# Description: Verify all function calls to `ProcessSingleMatch` match the new signature.

# Extract and review the function calls in the identified files.
files=(
  "protocol/x/clob/types/clob_keeper.go"
  "protocol/x/clob/types/mem_clob_keeper.go"
  "protocol/x/clob/memclob/memclob.go"
  "protocol/x/clob/keeper/process_operations.go"
  "protocol/testutil/memclob/keeper.go"
  "protocol/mocks/ClobKeeper.go"
  "protocol/mocks/MemClobKeeper.go"
)

for file in "${files[@]}"; do
  echo "File: $file"
  rg --no-filename 'ProcessSingleMatch' "$file" -A 3 -B 3
  echo
done

Length of output: 4184


509-509: Ensure all calls to ProcessSingleMatch are updated to the new signature.

Verification successful

The search results indicate that there are multiple occurrences of ProcessSingleMatch across different files. However, the output does not provide enough detail to confirm whether all calls have been updated to the new signature. We need to inspect the specific lines where ProcessSingleMatch is called to ensure they match the new signature.

Let's generate a script to extract and display the relevant lines from each file to verify the function calls.


The extracted lines confirm that all calls to ProcessSingleMatch have been updated to the new signature, which no longer includes OffchainUpdates. Here are the relevant locations:

  • protocol/x/clob/types/mem_clob_keeper.go
  • protocol/x/clob/types/clob_keeper.go
  • protocol/x/clob/memclob/memclob.go
  • protocol/x/clob/keeper/process_operations.go
  • protocol/testutil/memclob/keeper.go
  • protocol/mocks/MemClobKeeper.go
  • protocol/mocks/ClobKeeper.go

All these files show the correct usage of ProcessSingleMatch with the updated signature.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `ProcessSingleMatch` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'ProcessSingleMatch'

Length of output: 2259



Script:

#!/bin/bash
# Description: Verify all function calls to `ProcessSingleMatch` match the new signature.

# Test: Check each identified file for the correct function signature.

# Check in protocol/x/clob/types/mem_clob_keeper.go
echo "Checking protocol/x/clob/types/mem_clob_keeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/types/mem_clob_keeper.go

# Check in protocol/x/clob/types/clob_keeper.go
echo "Checking protocol/x/clob/types/clob_keeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/types/clob_keeper.go

# Check in protocol/x/clob/memclob/memclob.go
echo "Checking protocol/x/clob/memclob/memclob.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/memclob/memclob.go

# Check in protocol/x/clob/keeper/process_operations.go
echo "Checking protocol/x/clob/keeper/process_operations.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/keeper/process_operations.go

# Check in protocol/testutil/memclob/keeper.go
echo "Checking protocol/testutil/memclob/keeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/testutil/memclob/keeper.go

# Check in protocol/mocks/MemClobKeeper.go
echo "Checking protocol/mocks/MemClobKeeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/mocks/MemClobKeeper.go

# Check in protocol/mocks/ClobKeeper.go
echo "Checking protocol/mocks/ClobKeeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/mocks/ClobKeeper.go

Length of output: 1977



Script:

#!/bin/bash
# Description: Extract and display lines where `ProcessSingleMatch` is called to verify the new signature.

# Extract lines from protocol/x/clob/types/mem_clob_keeper.go
echo "Extracting from protocol/x/clob/types/mem_clob_keeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/types/mem_clob_keeper.go -A 2 -B 2

# Extract lines from protocol/x/clob/types/clob_keeper.go
echo "Extracting from protocol/x/clob/types/clob_keeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/types/clob_keeper.go -A 2 -B 2

# Extract lines from protocol/x/clob/memclob/memclob.go
echo "Extracting from protocol/x/clob/memclob/memclob.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/memclob/memclob.go -A 2 -B 2

# Extract lines from protocol/x/clob/keeper/process_operations.go
echo "Extracting from protocol/x/clob/keeper/process_operations.go"
rg --type go 'ProcessSingleMatch\(' protocol/x/clob/keeper/process_operations.go -A 2 -B 2

# Extract lines from protocol/testutil/memclob/keeper.go
echo "Extracting from protocol/testutil/memclob/keeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/testutil/memclob/keeper.go -A 2 -B 2

# Extract lines from protocol/mocks/MemClobKeeper.go
echo "Extracting from protocol/mocks/MemClobKeeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/mocks/MemClobKeeper.go -A 2 -B 2

# Extract lines from protocol/mocks/ClobKeeper.go
echo "Extracting from protocol/mocks/ClobKeeper.go"
rg --type go 'ProcessSingleMatch\(' protocol/mocks/ClobKeeper.go -A 2 -B 2

Length of output: 3146

protocol/mocks/ClobKeeper.go (1)

Line range hint 1022-1060: The changes to the ProcessSingleMatch function align with the PR's objectives to remove OffchainUpdates. This simplifies the function signature and avoids potential issues with unsafe concurrent map reads. Ensure that all dependent code correctly adapts to this new function signature.

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

1683-1683: Verify the removal of OffchainUpdates across all affected functions.

@@ -275,24 +271,22 @@ func (k Keeper) ProcessSingleMatch(
// Liquidation orders can only be placed when a subaccount is liquidatable
// and cannot be replayed, therefore we don't need to track their filled amount in state.
if !matchWithOrders.TakerOrder.IsLiquidation() {
takerOffchainUpdates := k.setOrderFillAmountsAndPruning(
k.setOrderFillAmountsAndPruning(
Copy link
Contributor

Choose a reason for hiding this comment

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

Review the logic for setting order fill amounts and pruning.

Consider abstracting some of this functionality into separate, smaller functions to improve readability and maintainability of the code.

Also applies to: 282-282

@@ -74,14 +72,13 @@

// Perform stateless validation on the match.
if err := matchWithOrders.Validate(); err != nil {
return false, takerUpdateResult, makerUpdateResult, nil, errorsmod.Wrapf(
return false, takerUpdateResult, makerUpdateResult, errorsmod.Wrapf(
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper error wrapping for better error traceability.

Consider adding more context to the error message to aid in debugging. For example, include specific attributes of MatchWithOrders that led to the validation failure.

@@ -1680,7 +1680,7 @@ func (m *MemClobPriceTimePriority) mustPerformTakerOrderMatching(
FillAmount: matchedAmount,
}

success, takerUpdateResult, makerUpdateResult, _, err := m.clobKeeper.ProcessSingleMatch(ctx, &matchWithOrders)
success, takerUpdateResult, makerUpdateResult, err := m.clobKeeper.ProcessSingleMatch(ctx, &matchWithOrders)
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor mustUpdateMemclobStateWithMatches to improve readability and maintainability.

This function is complex and performs multiple operations on the memclob state. Consider breaking it down into smaller, more manageable functions or methods. This can improve readability and make the code easier to maintain and test.

@teddyding teddyding merged commit 4c3e158 into main Jun 3, 2024
18 checks passed
@teddyding teddyding deleted the td/remove-offchain-from-process branch June 3, 2024 19:16
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.

2 participants