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

Cherry-pick fix for indexer v3.0.0 #943

Merged
merged 1 commit into from
Jan 9, 2024
Merged

Conversation

dydxwill
Copy link
Contributor

@dydxwill dydxwill commented Jan 9, 2024

Changelist

cherry-picked fixes:
#940

Test Plan

tested in staging

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

Important

Auto Review Skipped

Auto reviews are disabled on base/target branches other than the default branch. Please add the base/target branch pattern to the list of additional branches to be reviewed in the settings.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Walkthrough

The overall changes include an emergency patch to the time in force (TIF) handling logic, where unexpected TIF values now default to 'GTT' instead of raising an error. A new test case was added for this scenario, and an existing test was commented out. The perpetual markets table in the database was updated to include a 'FINAL_SETTLEMENT' status. Additionally, there were updates to handling trading rewards in the block processor and related tests, including a new subtype and associated validation logic.

Changes

Files Change Summary
.../protocol-translations.test.ts
.../protocol-translations.ts
.../dydx_from_protocol_time_in_force.sql
Modified TIF handling to return 'GTT' for unexpected values as an emergency patch.
.../perpetual-market-table.test.ts
.../20240108175501_add_perpetual_markets_status_final_settlement.ts
Added tests and database migration for 'FINAL_SETTLEMENT' status in perpetual markets.
.../order-handler.test.ts Added tests for invalid TimeInForce handling and updated existing tests.
.../trading-rewards-handler.test.ts
.../block-processor.ts
.../block-helpers.ts
.../types.ts
Changed trading rewards handling logic, including test adjustments and event subtype addition.

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.

@dydxwill dydxwill changed the base branch from main to release/indexer/v3.x January 9, 2024 18:26
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: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7a027e4 and 3b70c41.
Files selected for processing (10)
  • indexer/packages/postgres/tests/lib/protocol-translations.test.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/perpetual-market-table.test.ts (2 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240108175501_add_perpetual_markets_status_final_settlement.ts (1 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/tests/handlers/trading-rewards-handler.test.ts (1 hunks)
  • indexer/services/ender/src/lib/block-processor.ts (1 hunks)
  • indexer/services/ender/src/scripts/helpers/dydx_from_protocol_time_in_force.sql (1 hunks)
  • indexer/services/scripts/src/helpers/block-helpers.ts (2 hunks)
  • indexer/services/scripts/src/helpers/types.ts (1 hunks)
Additional comments: 15
indexer/packages/postgres/src/db/migrations/migration_files/20240108175501_add_perpetual_markets_status_final_settlement.ts (2)
  • 3-9: The migration script correctly adds the 'FINAL_SETTLEMENT' status to the 'perpetual_markets' table. Ensure that all dependent services are aware of this new status and handle it appropriately.

  • 12-18: The rollback script correctly reverts the changes made by the up script. This is important for maintaining database integrity in case the migration needs to be undone.

indexer/services/scripts/src/helpers/types.ts (1)
  • 23-23: The addition of the 'TRADING_REWARD' enum value to 'DydxIndexerSubtypes' is correct and follows the established pattern of the enum.
indexer/services/ender/src/scripts/helpers/dydx_from_protocol_time_in_force.sql (1)
  • 17-19: The emergency patch to the 'dydx_from_protocol_time_in_force' function now returns 'GTT' for any unrecognized 'tif' values instead of raising an exception. This change should be communicated to all stakeholders to ensure that the new default behavior is understood and handled correctly in all consuming services.
indexer/services/scripts/src/helpers/block-helpers.ts (1)
  • 102-107: The new case for 'TRADING_REWARD' in the 'annotateIndexerTendermintEvent' function is correctly implemented, including the decoding of 'TradingRewardsEventV1'.
indexer/packages/postgres/__tests__/stores/perpetual-market-table.test.ts (2)
  • 1-7: The modifications to the imports are correct, adding necessary imports and removing unused ones.

  • 154-167: The new test case for winding down a perpetual market is well-structured and correctly asserts the expected behavior after updating the market status to 'FINAL_SETTLEMENT'.

indexer/services/ender/__tests__/handlers/trading-rewards-handler.test.ts (1)
  • 39-39: The change of 'defaultTransactionIndex' to '-2' is noted. Ensure that this value aligns with the intended test scenarios and that it does not inadvertently affect other tests that rely on this constant.
indexer/packages/postgres/__tests__/lib/protocol-translations.test.ts (1)
  • 171-172: The new test case for the emergency patch in 'protocolOrderTIFToTIF' correctly tests the new behavior of returning 'GTT' for an invalid TimeInForce value.
indexer/services/ender/src/lib/block-processor.ts (1)
  • 54-54: The addition of TradingRewardsValidator to TXN_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING and its removal from BLOCK_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING should be verified to ensure that it aligns with the intended event processing logic. Confirm that TradingRewardsValidator is now correctly associated with transaction events rather than block events.
Verification successful

The shell script output confirms that the TradingRewardsValidator class is defined in trading-rewards-validator.ts and is referenced in block-processor.ts. However, the output does not explicitly confirm the removal of TradingRewardsValidator from BLOCK_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING or its exclusive association with transaction events.

To verify the review comment, we need to check the usage of TradingRewardsValidator within block-processor.ts to ensure it's only associated with transaction events and not block events. We should also check if there are any other references to TradingRewardsValidator in the context of block events.


The output from the rg command shows that TradingRewardsValidator is imported in block-processor.ts and is used in the TXN_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING mapping. The specific line of code provided confirms that TradingRewardsValidator is associated with a serialized subtype and version, presumably for transaction events.

The ast-grep command did not return any matches, which suggests that TradingRewardsValidator is not present in the BLOCK_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING. This supports the review comment that TradingRewardsValidator has been removed from block event processing and is now correctly associated with transaction events.

Based on the provided script outputs, the review comment is consistent with the changes in the codebase. The TradingRewardsValidator is indeed used for transaction events and not for block events in block-processor.ts.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that TradingRewardsValidator is only used in transaction events and not block events.
ast-grep --lang javascript --pattern $'class TradingRewardsValidator {
  $$$
}'
# Ensure no references to TradingRewardsValidator in block event processing.
rg "TradingRewardsValidator" --files-with-matches

Length of output: 247



Script:

#!/bin/bash
# Check the usage of TradingRewardsValidator in block-processor.ts to confirm it's associated with transaction events.
rg "TradingRewardsValidator" indexer/services/ender/src/lib/block-processor.ts

# Search for any mapping of TradingRewardsValidator within BLOCK_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING.
ast-grep --lang javascript --pattern $'const BLOCK_EVENT_SUBTYPE_VERSION_TO_VALIDATOR_MAPPING = {
  $$$
  $_: TradingRewardsValidator,
  $$$
}'

Length of output: 413

indexer/packages/postgres/src/lib/protocol-translations.ts (1)
  • 278-280: The change to the protocolOrderTIFToTIF function to return TimeInForce.GTT for unexpected protocolOrderTIF values instead of throwing an error is a significant behavior change. Ensure that this emergency patch is well-documented and that all stakeholders are aware of the new default behavior, as it could have implications on how orders are processed.
indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (4)
  • 1566-1566: The comment on line 1566 indicates that an invalid TimeInForce in the order is treated as GTT. This is a significant behavior change and should be clearly documented in the function that processes the orders, not just in the test. Ensure that the actual code reflects this behavior and that it is intentional and documented.

  • 1670-1670: The test case on line 1670 is creating a partial IndexerOrder with an undefined orderId. This is a good test for validation logic, but ensure that the system is expected to handle such cases gracefully and that appropriate error handling is in place in the production code.

  • 1602-1602: The comment on line 1602 about the fixed-point notation for quoteAmount is helpful for understanding the expected format. It's good practice to include such comments when dealing with financial calculations to avoid confusion about precision and representation.

  • 1640-1662: The expectDefaultOrderAndFillSubaccountKafkaMessages function is being called with a set of parameters to validate Kafka messages. It's important to ensure that the Kafka message expectations are correctly aligned with the test cases and that the Kafka producer mock is being verified for the correct calls with the expected message content.

@@ -1451,12 +1451,223 @@ describe('OrderHandler', () => {
]);
});

// Testing emergency patch
it('handles invalid TimeInForce (%s)', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The test description for handling invalid TimeInForce is missing a parameter in the it function call. It should include a parameter that describes the test case, which is typically passed to it.each for parameterized tests.

- it('handles invalid TimeInForce (%s)', async () => {
+ it('handles invalid TimeInForce', async () => {

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
it('handles invalid TimeInForce (%s)', async () => {
it('handles invalid TimeInForce', async () => {

clobPairId: testConstants.defaultPerpetualMarket3.clobPairId,
orderFlags: ORDER_FLAG_SHORT_TERM.toString(),
// Invalid TIF
timeInForce: (4 as IndexerOrder_TimeInForce),
Copy link
Contributor

Choose a reason for hiding this comment

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

The casting of an invalid TimeInForce value directly in the test case is a good way to simulate the behavior when an invalid value is encountered. However, it would be beneficial to add a comment explaining the choice of the specific invalid values (4 and 17) for clarity and maintainability.

+ // Using out-of-range values to simulate invalid TimeInForce
  timeInForce: (4 as IndexerOrder_TimeInForce),

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
timeInForce: (4 as IndexerOrder_TimeInForce),
// Using out-of-range values to simulate invalid TimeInForce
timeInForce: (4 as IndexerOrder_TimeInForce),

Comment on lines 1663 to 1664
});

Copy link
Contributor

Choose a reason for hiding this comment

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

The test case starting on line 1663 is missing a description for the it.each function call. This should be added for clarity and to ensure the test runner can properly display which test case is running.

- it.each([
+ it.each([
+   // Descriptions of each test case for better readability and reporting
    [
      undefined, // no maker order
    ],

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
});
// Descriptions of each test case for better readability and reporting
it.each([
[
undefined, // no maker order
],
});

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.

The commented-out test case for an unrecognized TimeInForce value is part of the emergency patch. Ensure that this test case is revisited once the emergency patch is no longer needed, as it is important for catching unexpected values.

@dydxwill dydxwill changed the title Cherry-pick fixes for indexer v1.0.5 Cherry-pick fixes for indexer v3.0.0 Jan 9, 2024
@dydxwill dydxwill changed the title Cherry-pick fixes for indexer v3.0.0 Cherry-pick fix for indexer v3.0.0 Jan 9, 2024
@dydxwill dydxwill merged commit aece446 into release/indexer/v3.x Jan 9, 2024
11 checks passed
@dydxwill dydxwill deleted the wl/rel1 branch January 9, 2024 18:59
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