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

Emergency patch to handle invalid TimeInForce. #942

Merged
merged 3 commits into from
Jan 9, 2024

Conversation

dydxwill
Copy link
Contributor

@dydxwill dydxwill commented Jan 9, 2024

Changelist

#907

Test Plan

Tested on internal mainnet indexer.

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.

Copy link
Contributor

coderabbitai bot commented Jan 9, 2024

Warning

Rate Limit Exceeded

@dydxwill has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 21 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 1c8d509 and 196317a.

Walkthrough

An emergency patch has been applied across several components of a system handling order processing to address an issue with unexpected TimeInForce (TIF) values. Instead of throwing errors, the system now defaults to using 'Good-Til-Cancelled' (GTT) when encountering unrecognized TIFs. This change affects test cases, the protocol translations logic, and a SQL script, suggesting a coordinated update to ensure the system remains functional while dealing with unanticipated TIF inputs.

Changes

File Path Change Summary
indexer/packages/postgres/__tests__/lib/protocol-translations.test.ts
indexer/packages/postgres/src/lib/protocol-translations.ts
Added a test case for an emergency patch and modified the protocolOrderTIFToTIF function to return TimeInForce.GTT instead of throwing an error when encountering an unexpected protocolOrderTIF. The error-throwing code has been commented out with a note indicating it was removed for an emergency patch.
indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts Added a new test case for handling invalid TimeInForce in the OrderHandler module.
indexer/services/ender/src/scripts/helpers/dydx_from_protocol_time_in_force.sql Modified the logic for handling time in force (TIF) values in the SQL script. The case for handling unexpected TIF values has been changed to return 'GTT' instead of raising an exception. Additionally, the original exception-raising code has been commented out with a note indicating it was removed for an emergency patch.

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-tests for this file.
  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c829c1d and 1e3e6f9.
Files selected for processing (4)
  • indexer/packages/postgres/tests/lib/protocol-translations.test.ts (2 hunks)
  • indexer/packages/postgres/src/lib/protocol-translations.ts (1 hunks)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (1 hunks)
  • indexer/services/ender/src/scripts/helpers/dydx_from_protocol_time_in_force.sql (1 hunks)
Additional comments: 3
indexer/services/ender/src/scripts/helpers/dydx_from_protocol_time_in_force.sql (1)
  • 17-19: The change to default to 'GTT' for unrecognized TimeInForce values could have implications for system behavior. Ensure that downstream systems are capable of handling these default values without issues. It's also important to consider logging such occurrences for monitoring any unexpected values that might indicate other issues in the system.
Verification successful

Based on the search results, the 'GTT' value is consistently handled within the dydx_from_protocol_time_in_force.sql script for various cases, including the default case for unrecognized TimeInForce values. This suggests that the system is prepared to handle 'GTT' as a default value, and the change made in the PR is consistent with the script's logic.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the default 'GTT' value for unrecognized TIFs is handled correctly downstream.
# This script checks for any handling of 'GTT' values in the codebase.
rg --type sql "GTT" -- "indexer/services/ender/src/scripts/"

Length of output: 553

indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (2)
  • 1454-1676: The test case added for handling invalid TimeInForce values seems to correctly simulate the scenario where the OrderHandler receives orders with invalid TimeInForce values. The test asserts that the system treats these invalid values as TimeInForce.GTT (Good-Til-Time), which aligns with the emergency patch described in the PR objectives. This is a critical update to ensure the system's robustness in the face of unexpected TimeInForce values.

However, it's important to ensure that this behavior is consistent across all parts of the system that deal with TimeInForce. If there are other modules or services that also process TimeInForce values, they should be updated and tested accordingly to handle invalid values in the same manner.

  • 1451-1686: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [1-1453]

The rest of the test file contains various test cases for the OrderHandler module. These tests cover a range of scenarios, including creating fills and orders, updating existing orders, and handling different types of orders such as limit, post-only, and immediate-or-cancel (IOC).

It's important to ensure that these tests are comprehensive and cover all the edge cases. Additionally, the tests should be clear and maintainable, with proper descriptions and assertions that accurately reflect the expected behavior of the system.

One potential area of improvement is to ensure that the test descriptions are clear and descriptive. For example, the test case descriptions should clearly state the expected outcome of the test, such as what the status of an order should be after processing, especially in cases where orders are partially filled or canceled.

Comment on lines +171 to +172
// Test case for emergency patch
['INVALID', (100 as IndexerOrder_TimeInForce), TimeInForce.GTT],
Copy link
Contributor

Choose a reason for hiding this comment

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

The addition of the test case for the emergency patch is good practice to ensure the new behavior is as expected. However, it's important to ensure that the commented-out test case does not leave a gap in the test coverage. If the behavior change is permanent, consider removing the commented-out test case to avoid confusion.

Comment on lines +181 to +188
// Commented out for emergency patch
/* it('throws error if unrecognized protocolTIF given', () => {
expect(
() => {
protocolOrderTIFToTIF(100 as IndexerOrder_TimeInForce);
},
).toThrow(new Error('Unexpected TimeInForce from protocol: 100'));
});
});*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Commenting out the test case that expects an exception for an unrecognized protocolTIF is appropriate given the emergency patch. However, ensure that this is a temporary measure and that a plan is in place to either reinstate or permanently remove this test case based on the long-term decision regarding the handling of unrecognized protocolTIF values.

Comment on lines +278 to +280
return TimeInForce.GTT;
// Removed for emergency patch
// throw new Error(`Unexpected TimeInForce from protocol: ${protocolOrderTIF}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on the context provided from the order-translations.ts file, the error logging present does not pertain to the handling of 'GTT' values. There is no explicit logging for cases where an unrecognized TimeInForce defaults to 'GTT'. This suggests that while the system may handle 'GTT' as a default value, there may not be monitoring in place specifically for these fallback cases. It would be advisable to consider adding such logging to aid in monitoring and debugging.

Analysis chain

The modification to return TimeInForce.GTT for unrecognized protocolOrderTIF values, instead of throwing an error, aligns with the changes in the SQL script. Ensure that this change is reflected in all parts of the system that rely on this function and that proper logging is in place for such cases.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the default 'GTT' value for unrecognized TIFs is handled correctly in all parts of the system.
# This script checks for any handling of 'GTT' values in the codebase.
rg --type ts "GTT" -- "indexer/packages/postgres/src/lib/"

Length of output: 900



Script:

#!/bin/bash
# Check for logging related to the default 'GTT' value being used in the system.
rg --type ts "logger.*GTT" -- "indexer/packages/postgres/src/lib/"

Length of output: 66



Script:

#!/bin/bash
# Check for general logging mechanisms that might capture the use of the default 'GTT' value.
rg --type ts "logger" -- "indexer/packages/postgres/src/lib/"

Length of output: 393



Script:

#!/bin/bash
# Examine the context of the logger calls in order-translations.ts to determine if they are related to 'GTT' handling.
rg --type ts -C 5 "logger.error" -- "indexer/packages/postgres/src/lib/order-translations.ts"

Length of output: 1197

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 1e3e6f9 and 1c8d509.
Files selected for processing (1)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (1 hunks)
Additional comments: 5
indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (5)
  • 1454-1675: The test case added for handling invalid TimeInForce values is critical and should ensure the system's stability. It's good to see parameterized tests being used to cover different scenarios. However, there are a few points to consider:
  1. Ensure that the test case covers all possible invalid TimeInForce values that the system might encounter.
  2. Verify that the test case includes assertions for all relevant aspects of the system's state after processing the orders.
  3. Check if there is any cleanup required after the test runs to prevent state leakage into other tests.
  4. Confirm that the logging and error handling in the test case are appropriate and provide enough information for debugging if the test fails.
  • 1485-1485: Casting a number to an enum type (IndexerOrder_TimeInForce) is a clever way to simulate invalid TimeInForce values. However, ensure that the range of invalid values tested reflects possible real-world scenarios that the system might encounter.
// Invalid TIF
timeInForce: (4 as IndexerOrder_TimeInForce),
  • 1577-1578: The test asserts that an invalid TimeInForce in the order is treated as TimeInForce.GTT. This behavior should be explicitly documented in the system's handling of orders to avoid confusion and ensure that it aligns with the business logic.
// Invalid TIF in order is treated as GTT
timeInForce: TimeInForce.GTT,
  • 1676-1676: It's important to include negative test cases, such as when fillOrderEvent fails validation. This ensures that the system behaves correctly in the face of invalid input. The use of expect functions to assert the absence of certain states, like candles, is a good practice.

  • 1562-1565: Ensure that the fixed-point notation used for prices in the test cases matches the precision expected by the system. Using fixed-point notation helps avoid floating-point precision issues, which is crucial in financial systems.

const makerPrice: string = '0.00000000000001';
const takerPrice: string = '0.0000000000000015';

@dydxwill dydxwill changed the title Cherry-pick https://github.com/dydxprotocol/v4-chain/pull/907 into main Emergency patch to handle invalid TimeInForce. Jan 9, 2024
@dydxwill dydxwill merged commit 7a027e4 into main Jan 9, 2024
11 checks passed
@dydxwill dydxwill deleted the wl/merge_emergency_change branch January 9, 2024 18:23
dydxwill added a commit that referenced this pull request Jan 9, 2024
* Emergency patch to handle invalid TimeInForce. (#907)

---------

Co-authored-by: vincentwschau <[email protected]>
@dydxwill dydxwill mentioned this pull request Jan 9, 2024
6 tasks
dydxwill added a commit that referenced this pull request Jan 9, 2024
dydxwill added a commit that referenced this pull request Jan 9, 2024
dydxwill added a commit that referenced this pull request Jan 9, 2024
* Emergency patch to handle invalid TimeInForce. (#907)

---------

Co-authored-by: vincentwschau <[email protected]>
dydxwill added a commit that referenced this pull request Jan 9, 2024
* Emergency patch to handle invalid TimeInForce. (#907)

---------

Co-authored-by: vincentwschau <[email protected]>
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