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

Restrict order replacement to only changing size, price, GTT/GTB #1623

Merged
merged 1 commit into from
Jun 4, 2024

Conversation

roy-dydx
Copy link
Contributor

@roy-dydx roy-dydx commented Jun 4, 2024

Changelist

Order replacement cannot change things like side, time in force, etc. anymore.

Test Plan

Modified 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

  • Bug Fixes

    • Improved handling of reduce-only orders and short-term order replacements to ensure correct failure scenarios.
    • Updated error messages for order replacement failures to be more concise.
  • Tests

    • Enhanced test cases for reduce-only and short-term orders to reflect updated failure conditions.
    • Added new test cases and updated existing ones to cover various order replacement scenarios and validate error handling.

Copy link
Contributor

coderabbitai bot commented Jun 4, 2024

Walkthrough

The recent changes mainly focus on updating test cases and validation logic for order replacements in the protocol/x/clob module. New test scenarios are added for reduce-only and short-term orders, while existing tests are adjusted to reflect changes in expected outcomes. A new validation function ensures order replacements meet specific criteria, and error messages are updated for clarity.

Changes

Files & Paths Change Summary
protocol/x/clob/e2e/reduce_only_orders_test.go Added new test cases for reduce-only order failures and removed outdated ones.
protocol/x/clob/e2e/short_term_orders_test.go Modified test case descriptions and expected outcomes for short-term order replacements.
protocol/x/clob/memclob/memclob.go Introduced validateReplacement function and updated validateNewOrder logic for order replacements.
protocol/x/clob/memclob/memclob_place_order_test.go Adjusted test descriptions and expected outcomes for placing and replacing orders.
protocol/x/clob/types/errors.go Simplified the error message for ErrInvalidReplacement.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant TestFramework as Test Framework
    participant ClobModule as CLOB Module
    participant Validator as Validator

    User->>TestFramework: Run reduce-only order tests
    TestFramework->>ClobModule: Place reduce-only order
    ClobModule->>Validator: Validate order
    Validator-->>ClobModule: Return validation result
    ClobModule-->>TestFramework: Return test result
    TestFramework-->>User: Display test outcome

    User->>TestFramework: Run short-term order tests
    TestFramework->>ClobModule: Place short-term order
    ClobModule->>Validator: Validate order
    Validator-->>ClobModule: Return validation result
    ClobModule-->>TestFramework: Return test result
    TestFramework-->>User: Display test outcome
Loading

Poem

In the land of code where orders play,
A rabbit hopped and had its say.
"Replace with care," it did declare,
"Validate with utmost flair."
Tests now hum a tune so bright,
Errors clear as day and night. 🐇✨


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.

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 (3)
protocol/x/clob/memclob/memclob.go (3)

Line range hint 404-404: ### Missing Method Implementations
Several methods such as GenerateStreamOrderbookFill, GetOrderbookUpdatesForOrderUpdate, GetOrderbookUpdatesForOrderPlacement, and GetOrderbookUpdatesForOrderRemoval are called but not defined in the MemClobPriceTimePriority struct. This will cause runtime errors if not addressed.

+ // TODO: Implement the missing methods or ensure they are correctly linked if defined elsewhere.

Also applies to: 873-873, 877-877, 1512-1512, 1952-1952


Line range hint 934-934: ### Unused Variables
The variables placedPreexistingStatefulOrderIds and placedOrderRemovalOrderIds are declared but not used within the ReplayOperations method. This could lead to inefficiencies and should be addressed either by utilizing these variables or removing them if they are unnecessary.

- placedPreexistingStatefulOrderIds := make(map[types.OrderId]struct{})
- placedOrderRemovalOrderIds := make(map[types.OrderId]struct{})

Also applies to: 935-935


Line range hint 36-36: ### Undefined Type: Orderbook
The type Orderbook is referenced but not defined within the file. This will cause compilation errors. Ensure that the Orderbook type is correctly defined and imported if it's located in another package.

+ type Orderbook struct {
+     // Define the structure fields here.
+ }

Also applies to: 70-70, 2174-2174

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 86eb47d and c2fe018.

Files selected for processing (5)
  • protocol/x/clob/e2e/reduce_only_orders_test.go (2 hunks)
  • protocol/x/clob/e2e/short_term_orders_test.go (5 hunks)
  • protocol/x/clob/memclob/memclob.go (2 hunks)
  • protocol/x/clob/memclob/memclob_place_order_test.go (6 hunks)
  • protocol/x/clob/types/errors.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • protocol/x/clob/types/errors.go
Additional context used
Learnings (1)
protocol/x/clob/memclob/memclob_place_order_test.go (1)
User: jonfung-dydx
PR: dydxprotocol/v4-chain#1009
File: protocol/x/clob/memclob/memclob_place_order_test.go:4108-4108
Timestamp: 2024-02-05T20:01:55.204Z
Learning: The `assertPlaceOrderOffchainMessages` function in `memclob_test_util.go` indeed requires `ctx` as a parameter for its operations, contrary to the initial comment made.
golangci-lint
protocol/x/clob/memclob/memclob.go

36-36: undefined: Orderbook


70-70: undefined: Orderbook


2174-2174: undefined: Orderbook


404-404: m.GenerateStreamOrderbookFill undefined (type *MemClobPriceTimePriority has no field or method GenerateStreamOrderbookFill)


873-873: m.GetOrderbookUpdatesForOrderUpdate undefined (type *MemClobPriceTimePriority has no field or method GetOrderbookUpdatesForOrderUpdate)


877-877: m.GetOrderbookUpdatesForOrderUpdate undefined (type *MemClobPriceTimePriority has no field or method GetOrderbookUpdatesForOrderUpdate)


934-934: placedPreexistingStatefulOrderIds declared and not used


935-935: placedOrderRemovalOrderIds declared and not used


1512-1512: m.GetOrderbookUpdatesForOrderPlacement undefined (type *MemClobPriceTimePriority has no field or method GetOrderbookUpdatesForOrderPlacement)


1952-1952: m.GetOrderbookUpdatesForOrderRemoval undefined (type *MemClobPriceTimePriority has no field or method GetOrderbookUpdatesForOrderRemoval)

Additional comments not posted (8)
protocol/x/clob/e2e/reduce_only_orders_test.go (1)

677-690: New test function TestReduceOnlyOrderFailure correctly handles scenarios where reduce-only orders are disabled.

protocol/x/clob/e2e/short_term_orders_test.go (2)

Line range hint 975-1001: Ensure the test cases for FOK and IOC order types are correctly failing as expected.

The modifications to the test descriptions and expected outcomes correctly reflect the new behavior for FOK and IOC orders, which should now fail under the new restrictions. This is consistent with the PR's objectives and the changes made in the codebase.


699-709: Update test description to reflect the new expected behavior.

- "Success: Replace in same block on opposite side"
+ "Fail: Replace in same block on opposite side"

This change aligns with the PR's objective to restrict order replacements, ensuring that replacements that change the order side are correctly handled as failures.

Likely invalid or redundant comment.

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

1421-1429: ### Use of validateReplacement in validateNewOrder
The use of validateReplacement within validateNewOrder for both resting and matched orders is a good practice as it ensures that replacements adhere to the defined constraints. This is crucial for maintaining the integrity of order operations within the system.


1337-1366: ### Ensure Consistency in Order Replacement Validation
The validateReplacement function correctly enforces that only size, price, and GTB/GTT can change during an order replacement. However, it's crucial to ensure that this function is consistently used wherever order replacements are validated to maintain the integrity of the order replacement logic.

Verification successful

"""

"""


"""

"""


"""

"""


The search results indicate that there are multiple functions related to order processing and replacement across various files. However, the key functions that directly handle order validation and replacement logic are primarily located in protocol/x/clob/memclob/memclob.go.

The validateReplacement function is used in two places within memclob.go, which suggests that it is being utilized for validating order replacements in the relevant contexts. Additionally, the search did not reveal any other functions that perform order replacement validation without using validateReplacement.

Based on the gathered information, it appears that the validateReplacement function is consistently used for order replacement validation in the relevant parts of the codebase.

  • protocol/x/clob/memclob/memclob.go: validateReplacement(existingRestingOrder, order)
  • protocol/x/clob/memclob/memclob.go: validateReplacement(existingMatchedOrder, order)

These usages indicate that the function is applied where necessary to ensure the integrity of the order replacement logic.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that `validateReplacement` is used consistently across all relevant parts of the codebase.

# Test: Search for usage of `validateReplacement`. Expect: Usage in all relevant parts.
rg --type go $'validateReplacement'

Length of output: 452



Script:

#!/bin/bash
# Description: Search for other potential order replacement validation points by looking for keywords related to order replacement.

# Test: Search for keywords related to order replacement. Expect: Identify all relevant parts.
rg --type go 'order.*replace|replace.*order|validate.*order'

Length of output: 12329



Script:

#!/bin/bash
# Description: Search for function definitions related to order validation and check if they call `validateReplacement`.

# Test: Search for function definitions related to order validation.
rg --type go 'func.*validate.*order' --context 10

Length of output: 51



Script:

#!/bin/bash
# Description: Search for functions related to order processing and replacement, and check if they call `validateReplacement`.

# Test: Search for functions related to order processing and replacement.
rg --type go 'func.*order' --context 10

Length of output: 190290

protocol/x/clob/memclob/memclob_place_order_test.go (3)

207-215: The test case "Replacing an order fails if GoodTilBlock is greater than existing order and changes sides" is well-structured and covers the scenario comprehensively.


2190-2204: The test case "IOC Taker cannot replace unfilled non IOC order" correctly handles the scenario where an Immediate-Or-Cancel (IOC) order attempts to replace a non-IOC order, which should fail as expected.


2206-2220: The test case "FOK Taker cannot replace unfilled non IOC order" is correctly implemented to ensure that a Fill-Or-Kill (FOK) order cannot replace a non-IOC order. This is an important check to maintain the integrity of order types.

@roy-dydx roy-dydx merged commit ed69514 into main Jun 4, 2024
17 of 18 checks passed
@roy-dydx roy-dydx deleted the roy/replace branch June 4, 2024 16:47
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