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

[IND-450]: Prevent orders transitioning from canceled to best effort canceled #753

Merged
merged 4 commits into from
Nov 6, 2023

Conversation

Christopher-Li
Copy link
Contributor

Changelist

  • Convert CanceledOrdersCache to have a cache for BEST_EFFORT_CANCELED and CANCELED
  • Update vulcan remove-order-handler.ts to update CanceledOrdersCache with status
  • Update ender's abstract-order-fill-handler.ts to update short term GTT and short term post-only orders to the correct status
  • Fix bug in ender's abstract-order-fill-handler.ts to never set stateful orders to BEST_EFFORT_CANCELED because stateful orders are never added to the CanceledOrdersCache in vulcan, and because we never send BEST_EFFORT_CANCELED for stateful orders

Test Plan

Tested in unit tests, verified previous behavior in dev

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

@Christopher-Li Christopher-Li added the bug Something isn't working label Nov 3, 2023
Copy link

linear bot commented Nov 3, 2023

IND-450 BUG: Orders can transition from CANCELED -> BEST_EFFORT_CANCELED

This can happen in this case:
1. When a short-term order expires, it transitions to the CANCELED state.
2. We add that short-term order to a cache of canceled orders here in vulcan.
3. If the order is partially filled, when we ingest a fill from on-chain events, and the order is short-term + in the canceled cache, it transitions to the BEST_EFFORT_CANCELED state here.

More context on specific orders in testnet-4 where this happened: https://dydx-team.slack.com/archives/C05S4CTSQ74/p1697638332790579

Copy link
Contributor

coderabbitai bot commented Nov 3, 2023

Walkthrough

The changes primarily revolve around the handling of canceled orders in the codebase. A new enum CanceledOrderStatus has been introduced to better represent the cancellation status of an order. The code has been refactored to use this new enum instead of boolean flags. The cache handling for canceled orders has been updated to accommodate different types of cancellations. Test cases have been updated and new ones have been added to reflect these changes.

Changes

File(s) Change Summary
indexer/packages/redis/.../canceled-orders-cache.test.ts
indexer/packages/redis/src/caches/canceled-orders-cache.ts
indexer/packages/redis/src/types.ts
Introduced a new enum CanceledOrderStatus and updated the cache handling functions to use this enum. Renamed removeOrderFromCache to removeOrderFromCaches and added getOrderCanceledStatus and addBestEffortCanceledOrderId functions.
indexer/services/ender/.../order-handler.test.ts
indexer/services/ender/src/handlers/order-fills/abstract-order-fill-handler.ts
indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts
indexer/services/ender/src/handlers/order-fills/order-handler.ts
Updated the OrderHandler and related classes to use the new CanceledOrderStatus enum. Updated the upsertOrderFromEvent and getOrderStatus methods to use the new enum. Added new test cases for different cancellation statuses.
indexer/services/ender/src/scripts/dydx_get_order_status.sql
indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql
indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql
Updated SQL scripts to use the new CanceledOrderStatus enum. Replaced the is_cancelled parameter with order_canceled_status in the dydx_get_order_status and dydx_order_fill_handler_per_order functions.
indexer/services/vulcan/.../order-place-handler.test.ts
indexer/services/vulcan/src/handlers/order-place-handler.ts
Updated the OrderPlaceHandler class and related test cases to use the new CanceledOrderStatus enum and the renamed removeOrderFromCaches function.
indexer/services/vulcan/.../order-remove-handler.test.ts
indexer/services/vulcan/src/handlers/order-remove-handler.ts
Updated the OrderRemoveHandler class and related test cases to use the new CanceledOrderStatus enum. Updated the addOrderToCanceledOrdersCache method to handle different removal statuses.
indexer/services/vulcan/__tests__/helpers/helpers.ts Introduced a new function expectCanceledOrderStatus to assert the cancellation status of an order in test cases.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4bd3c34 and 10a2875.
Files selected for processing (14)
  • indexer/packages/redis/tests/caches/canceled-orders-cache.test.ts (3 hunks)
  • indexer/packages/redis/src/caches/canceled-orders-cache.ts (4 hunks)
  • indexer/packages/redis/src/types.ts (1 hunks)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (4 hunks)
  • indexer/services/ender/src/handlers/order-fills/abstract-order-fill-handler.ts (5 hunks)
  • indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts (2 hunks)
  • indexer/services/ender/src/handlers/order-fills/order-handler.ts (4 hunks)
  • indexer/services/ender/src/scripts/dydx_get_order_status.sql (1 hunks)
  • indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql (4 hunks)
  • indexer/services/vulcan/tests/handlers/order-place-handler.test.ts (4 hunks)
  • indexer/services/vulcan/tests/handlers/order-remove-handler.test.ts (17 hunks)
  • indexer/services/vulcan/tests/helpers/helpers.ts (2 hunks)
  • indexer/services/vulcan/src/handlers/order-place-handler.ts (1 hunks)
  • indexer/services/vulcan/src/handlers/order-remove-handler.ts (2 hunks)
Files skipped from review due to trivial changes (4)
  • indexer/packages/redis/src/types.ts
  • indexer/services/ender/src/scripts/dydx_get_order_status.sql
  • indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql
  • indexer/services/vulcan/tests/handlers/order-place-handler.test.ts
Additional comments: 31
indexer/services/ender/src/handlers/order-fills/liquidation-handler.ts (2)
  • 16-19: The import statement for CanceledOrderStatus is added correctly.

  • 210-214: The upsertOrderFromEvent method is updated to use the CanceledOrderStatus.NOT_CANCELED enum value instead of false. This change is consistent with the PR description and should work as expected, assuming that the upsertOrderFromEvent method has been updated to handle the new enum type.

indexer/packages/redis/__tests__/caches/canceled-orders-cache.test.ts (3)
  • 4-10: The import statements have been updated to reflect the changes in the canceled-orders-cache module. The function removeOrderFromCache has been renamed to removeOrderFromCaches, and the new functions getOrderCanceledStatus and addBestEffortCanceledOrderId have been added. The import statement for CanceledOrderStatus has also been added.

  • 43-52: The test case 'successfully removes canceled order' has been updated to use the new function removeOrderFromCaches. Ensure that the function behaves as expected and the test case passes.

  • 67-83: New test cases have been added to test the getOrderCanceledStatus function. These test cases check if the function correctly returns CANCELED, BEST_EFFORT_CANCELED, and NOT_CANCELED statuses. Ensure that these test cases cover all possible scenarios and pass.

indexer/services/ender/src/handlers/order-fills/abstract-order-fill-handler.ts (4)
  • 27-30: The new import statement for CanceledOrderStatus from @dydxprotocol-indexer/redis is introduced. Ensure that the CanceledOrderStatus enum is correctly defined in the @dydxprotocol-indexer/redis module.

  • 293-304: The upsertOrderFromEvent method's signature is modified to replace the isCanceled parameter with canceledOrderStatus of type CanceledOrderStatus. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 310-316: The getOrderStatus method's signature is modified to replace the isCanceled parameter with canceledOrderStatus of type CanceledOrderStatus. The logic within the getOrderStatus method is also updated to use the canceledOrderStatus parameter instead of the isCanceled parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 367-383: The logic within the getOrderStatus method is updated to handle the new CanceledOrderStatus enum. Ensure that the logic correctly handles all possible values of CanceledOrderStatus.

indexer/services/vulcan/__tests__/helpers/helpers.ts (2)
  • 5-11: The new import CanceledOrderStatus is used in the new function expectCanceledOrderStatus. Ensure that this enum is correctly defined and exported in the @dydxprotocol-indexer/redis module.

  • 84-91: The new function expectCanceledOrderStatus retrieves the canceled order status for a given order ID from the CanceledOrdersCache and checks if it matches the expected status. This function will be useful in unit tests to verify the correct status of canceled orders.

+ export async function expectCanceledOrderStatus(
+   orderId: string,
+   canceledOrderStatus: CanceledOrderStatus,
+ ) {
+   expect(await CanceledOrdersCache.getOrderCanceledStatus(orderId, redisClient)).toEqual(
+     canceledOrderStatus,
+   );
+ }
indexer/services/vulcan/src/handlers/order-place-handler.ts (1)
  • 364-370: The method removeOrderFromCanceledOrdersCache has been renamed to removeOrderFromCaches. Ensure that all calls to this method throughout the codebase have been updated to match the new name.
-  protected async removeOrderFromCanceledOrdersCache(
+  protected async removeOrderFromCaches(
indexer/packages/redis/src/caches/canceled-orders-cache.ts (7)
  • 1-11: The introduction of a new cache key BEST_EFFORT_CANCELED_ORDERS_CACHE_KEY and the new enum CanceledOrderStatus is a good approach to handle different cancellation statuses. This change is consistent with the PR summary.

  • 26-37: The isOrderCanceled function now checks both CANCELED_ORDERS_CACHE_KEY and BEST_EFFORT_CANCELED_ORDERS_CACHE_KEY to determine if an order is canceled. This change is consistent with the PR summary.

  • 39-60: The new function getOrderCanceledStatus returns the cancellation status of an order based on the two cache keys. This function is a good addition as it provides more detailed information about the cancellation status of an order.

  • 62-70: The function removeOrderFromCaches now removes the order from both cache keys. This change is consistent with the PR summary.

  • 79-85: The new function addBestEffortCanceledOrderId adds the order ID to the BEST_EFFORT_CANCELED_ORDERS_CACHE_KEY. This function is a good addition as it provides a way to add an order to the best effort canceled orders cache.

  • 95-100: The function addCanceledOrderId now uses the new function addOrderIdtoCache to add the order ID to the CANCELED_ORDERS_CACHE_KEY. This change is consistent with the PR summary.

  • 95-116: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [109-139]

The new function addOrderIdtoCache adds the order ID to the specified cache key. This function is a good addition as it provides a way to add an order to a specified cache key.

indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (2)
  • 1475-1513: The test cases are well-structured and cover a variety of scenarios. This is a good practice as it ensures that the code is robust and can handle different situations.

  • 1563-1567: The code correctly checks the isBestEffortCanceled flag and calls the appropriate function based on its value. This is a good practice as it ensures that the correct function is called based on the order status.

indexer/services/vulcan/__tests__/handlers/order-remove-handler.test.ts (3)
  • 42-42: The import of CanceledOrderStatus is added to reflect the changes in the codebase where the isOrderCanceled variable and function are replaced with canceledOrderStatus and getOrderCanceledStatus respectively.

  • 62-62: The function expectCanceledOrdersCacheFound is replaced with expectCanceledOrderStatus to align with the changes in the codebase.

  • 1875-1875: The expected order status in these test cases is changed to reflect the new CanceledOrderStatus enum values. This is a necessary change to ensure the tests are aligned with the updated codebase.

indexer/services/vulcan/src/handlers/order-remove-handler.ts (2)
  • 300-306: The addOrderToCanceledOrdersCache method now accepts an orderRemove parameter of type OrderRemoveV1 instead of orderId as a string. It checks the removalStatus of the orderRemove object and calls different methods from the CanceledOrdersCache class based on the status. This change allows for handling different removal statuses of orders in the cache.

  • 338-363: The addOrderToCanceledOrdersCache method now checks the removalStatus of the orderRemove object and calls different methods from the CanceledOrdersCache class based on the status. This change allows for handling different removal statuses of orders in the cache.

-  protected async addOrderToCanceledOrdersCache(
-    orderId: string,
-    blockHeight: number,
-  ): Promise<void> {
-    await runFuncWithTimingStat(
-      CanceledOrdersCache.addCanceledOrderId(orderId, blockHeight, redisClient),
-      this.generateTimingStatsOptions('add_order_to_canceled_order_cache'),
-    );
-  }
+  protected async addOrderToCanceledOrdersCache(
+    orderRemove: OrderRemoveV1,
+    timestampMs: number,
+  ): Promise<void> {
+    const orderId: string = OrderTable.orderIdToUuid(orderRemove.removedOrderId!);
+
+    if (
+      orderRemove.removalStatus ===
+      OrderRemoveV1_OrderRemovalStatus.ORDER_REMOVAL_STATUS_BEST_EFFORT_CANCELED
+    ) {
+      await runFuncWithTimingStat(
+        CanceledOrdersCache.addBestEffortCanceledOrderId(orderId, timestampMs, redisClient),
+        this.generateTimingStatsOptions('add_order_to_canceled_order_cache'),
+      );
+    } else if (
+      orderRemove.removalStatus ===
+      OrderRemoveV1_OrderRemovalStatus.ORDER_REMOVAL_STATUS_CANCELED
+    ) {
+      await runFuncWithTimingStat(
+        CanceledOrdersCache.addCanceledOrderId(orderId, timestampMs, redisClient),
+        this.generateTimingStatsOptions('add_order_to_canceled_order_cache'),
+      );
+    }
+  }
indexer/services/ender/src/handlers/order-fills/order-handler.ts (5)
  • 16-22: The import statement has been updated to include CanceledOrderStatus from the @dydxprotocol-indexer/redis module. Ensure that the module is correctly installed and imported.

  • 75-82: The getOrderCanceledStatus function is being called with orderUuid and redisClient as arguments. Ensure that orderUuid and redisClient are correctly initialized and have the expected values.

  • 93-97: The dydx_order_fill_handler_per_order function is being called with canceledOrderStatus as an argument. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 181-188: The getOrderCanceledStatus function is being called with orderUuid and redisClient as arguments. Ensure that orderUuid and redisClient are correctly initialized and have the expected values.

  • 192-197: The upsertOrderFromEvent function is being called with canceledOrderStatus as an argument. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 10a2875 and 9d6159e.
Files selected for processing (1)
  • indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql (2 hunks)
Files skipped from review due to trivial changes (1)
  • indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9d6159e and 0e7ad9a.
Files selected for processing (2)
  • indexer/packages/redis/src/caches/canceled-orders-cache.ts (4 hunks)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (4 hunks)
Additional comments: 11
indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (4)
  • 710-714: The ternary operator is used to set the status of the order. Ensure that the isOrderCanceled variable is correctly set before this point.

  • 1483-1507: The test cases are well-structured and cover different scenarios. However, ensure that the expected outcomes are correctly defined for each case.

  • 1557-1561: The code correctly checks the status of the order and calls the appropriate function from the CanceledOrdersCache class. Ensure that these functions are implemented correctly and handle all edge cases.

  • 1610-1613: The test assertions are correct. Ensure that the makerOrder and takerOrder objects are correctly set before this point.

indexer/packages/redis/src/caches/canceled-orders-cache.ts (7)
  • 1-11: The introduction of a new cache key BEST_EFFORT_CANCELED_ORDERS_CACHE_KEY and the enum CanceledOrderStatus is a good approach to handle different cancellation statuses. This will improve the readability and maintainability of the code.

  • 26-37: The function isOrderCanceled now checks both the CANCELED_ORDERS_CACHE_KEY and the BEST_EFFORT_CANCELED_ORDERS_CACHE_KEY for the order's cancellation status. This is a good approach to handle different cancellation statuses.

  • 39-60: The function getOrderCanceledStatus is a good addition. It returns the cancellation status of an order based on the two cache keys. This will improve the readability and maintainability of the code.

  • 62-70: The function removeOrderFromCaches now removes the order from both cache keys. This is a good approach to handle different cancellation statuses.

  • 79-85: The function addBestEffortCanceledOrderId is a good addition. It specifically adds the order to the BEST_EFFORT_CANCELED_ORDERS_CACHE_KEY. This will improve the readability and maintainability of the code.

  • 95-100: The function addCanceledOrderId now calls addOrderIdtoCache with the CANCELED_ORDERS_CACHE_KEY. This is a good approach to handle different cancellation statuses.

  • 95-116: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [109-139]

The function addOrderIdtoCache now accepts a cacheKey parameter to specify the cache key to add the order to. This is a good approach to handle different cancellation statuses.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0e7ad9a and fe4dd7c.
Files selected for processing (1)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (4 hunks)
Additional comments: 4
indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (4)
  • 711-711: The change from OrderStatus.BEST_EFFORT_CANCELED to OrderStatus.CANCELED is noted. Ensure that this change is reflected everywhere in the codebase where OrderStatus.BEST_EFFORT_CANCELED was previously used.

  • 1483-1514: The addition of new test cases for "post-only canceled" orders is a good practice. It helps to ensure that the system behaves as expected under different conditions.

  • 1557-1561: The conditional handling of OrderStatus.BEST_EFFORT_CANCELED and OrderStatus.CANCELED is clear and straightforward. However, ensure that the addBestEffortCanceledOrderId and addCanceledOrderId methods handle any potential errors and exceptions.

  • 1610-1610: The test case checks if the status of the maker order is equal to the expected status. This is a good practice as it ensures that the status of the order is updated correctly.

@Christopher-Li Christopher-Li merged commit 32f713f into main Nov 6, 2023
11 checks passed
@Christopher-Li Christopher-Li deleted the cl_canceled_to_bec branch November 6, 2023 18:22
vincentwschau pushed a commit that referenced this pull request Nov 9, 2023
…canceled (#753)

* [IND-450]: Prevent orders transitioning from canceled to best effort canceled

* fix liquidations

* nits

* lint
vincentwschau pushed a commit that referenced this pull request Nov 9, 2023
…canceled (#753)

* [IND-450]: Prevent orders transitioning from canceled to best effort canceled

* fix liquidations

* nits

* lint
vincentwschau added a commit that referenced this pull request Nov 10, 2023
…canceled (#753)

* [IND-450]: Prevent orders transitioning from canceled to best effort canceled

* fix liquidations

* nits

* lint
vincentwschau added a commit that referenced this pull request Nov 10, 2023
…canceled (#753)

* [IND-450]: Prevent orders transitioning from canceled to best effort canceled

* fix liquidations

* nits

* lint
vincentwschau added a commit that referenced this pull request Nov 10, 2023
…canceled (#753)

* [IND-450]: Prevent orders transitioning from canceled to best effort canceled

* fix liquidations

* nits

* lint
vincentwschau pushed a commit that referenced this pull request Nov 10, 2023
…canceled (#753)

* [IND-450]: Prevent orders transitioning from canceled to best effort canceled

* fix liquidations

* nits

* lint
vincentwschau pushed a commit that referenced this pull request Nov 10, 2023
…canceled (#753)

* [IND-450]: Prevent orders transitioning from canceled to best effort canceled

* fix liquidations

* nits

* lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working indexer
Development

Successfully merging this pull request may close these issues.

3 participants