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-485]: Query for PerpetualPositions by openEventId #777

Merged
merged 2 commits into from
Nov 9, 2023

Conversation

Christopher-Li
Copy link
Contributor

Changelist

Change existing queries to perpetual_positions to order by openEventId rather than createdAtHeight and change the corresponding SQL statement in Ender to also use opentEventId

Test Plan

Unit 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 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 9, 2023
Copy link

linear bot commented Nov 9, 2023

IND-485 Inconsistent perpetual position querying

It seems like two perpetual positions of the same height can be created. We should use openEventId instead of createdAtHeightto identify a perpetualPosition.

context: https://dydx-team.slack.com/archives/C05S4CTSQ74/p1699470337533439?thread_ts=1698760513.413069&cid=C05S4CTSQ74

Copy link
Contributor

coderabbitai bot commented Nov 9, 2023

# Walkthrough
The changes primarily revolve around the introduction of a new constant `defaultTendermintEvent4` and modifications to the sorting order in various functions and SQL queries. The sorting order has been changed from `createdAtHeight` to `openEventId` in the `findAll` function and in several SQL scripts. Additionally, new test cases have been added to verify the correct sorting of `PerpetualPositions` based on `openEventId`.

# Changes

| File Path | Change Summary |
| --- | --- |
| `.../postgres/__tests__/helpers/constants.ts`<br>`.../postgres/__tests__/helpers/mock-generators.ts`<br>`.../postgres/__tests__/stores/perpetual-position-table.test.ts` | Addition of a new constant `defaultTendermintEvent4` of type `TendermintEventCreateObject` and `defaultTendermintEventId4` of type `Buffer`. Corresponding creation and usage in test data and functions. |
| `.../postgres/src/stores/perpetual-position-table.ts`<br>`.../ender/src/scripts/dydx_order_fill_handler_per_order.sql`<br>`.../ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql` | Modification of sorting order from `createdAtHeight` to `openEventId` in the `findAll` function and SQL queries. |
| `.../ender/__tests__/handlers/order-fills/liquidation-handler.test.ts`<br>`.../ender/__tests__/handlers/order-fills/order-handler.test.ts` | Changes to event IDs and test constants. |

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 238427b and 910858c.
Files selected for processing (7)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/helpers/mock-generators.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/perpetual-position-table.test.ts (3 hunks)
  • indexer/packages/postgres/src/stores/perpetual-position-table.ts (1 hunks)
  • indexer/services/ender/tests/handlers/order-fills/liquidation-handler.test.ts (3 hunks)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (4 hunks)
  • indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql (1 hunks)
Files skipped from review due to trivial changes (2)
  • indexer/packages/postgres/tests/helpers/constants.ts
  • indexer/packages/postgres/tests/helpers/mock-generators.ts
Additional comments: 11
indexer/packages/postgres/src/stores/perpetual-position-table.ts (1)
  • 124-129: The ordering of the results from the findAll function has been changed from createdAtHeight to openEventId. Ensure that this change does not affect any dependent functions or modules that rely on the previous ordering.
indexer/services/ender/src/scripts/dydx_order_fill_handler_per_order.sql (1)
  • 167-173: The change in the ORDER BY clause from "createdAtHeight" to "openEventId" may affect the order of the records retrieved from the database. Ensure that this change does not introduce any unintended side effects in the application logic that relies on the order of these records.
- ORDER BY "createdAtHeight" DESC;
+ ORDER BY "openEventId" DESC;
indexer/services/ender/__tests__/handlers/order-fills/liquidation-handler.test.ts (3)
  • 144-145: Ensure that the openEventId and lastEventId values are correctly set and that they are consistent with the new ordering logic.

  • 237-237: The message 'creates fills and orders (with %s), sends vulcan message for maker order update and updates perpetualPosition' is clear and descriptive. Ensure that it accurately reflects the test case.

  • 300-300: Ensure that the openEventId value is correctly set and that it is consistent with the new ordering logic.

indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (4)
  • 146-147: Ensure that the openEventId and lastEventId are correctly updated in all relevant places in the codebase.

  • 314-318: Ensure that the openEventId is correctly updated in all relevant places in the codebase.

  • 917-920: Ensure that the openEventId is correctly updated in all relevant places in the codebase.

  • 1129-1140: Ensure that the openEventId is correctly updated in all relevant places in the codebase.

indexer/packages/postgres/__tests__/stores/perpetual-position-table.test.ts (2)
  • 15-18: The new import TendermintEventCreateObject is used in the new test case. Ensure that this import is used correctly and that the type is defined as expected.

  • 191-224: This new test case checks if the PerpetualPositions are sorted by openEventId. It creates two positions with different openEventIds and checks if the returned positions are sorted correctly. The test case seems to be correct, but ensure that the PerpetualPositionTable.findAll function is implemented to sort by openEventId.

+  it('Successfully finds PerpetualPositions sorted by openEventId', async () => {
+    const earlierPosition: PerpetualPositionCreateObject = {
+      ...defaultPerpetualPosition,
+      openEventId: defaultTendermintEventId3,
+      lastEventId: defaultTendermintEventId3,
+    };
+    const nextTendermintEvent: TendermintEventCreateObject = {
+      blockHeight: defaultTendermintEvent3.blockHeight,
+      transactionIndex: defaultTendermintEvent3.transactionIndex,
+      eventIndex: defaultTendermintEvent3.eventIndex + 1,
+    };
+    const nextTendermintEventId: Buffer = TendermintEventTable.createEventId(
+      nextTendermintEvent.blockHeight,
+      nextTendermintEvent.transactionIndex,
+      nextTendermintEvent.eventIndex,
+    );
+    const laterPosition: PerpetualPositionCreateObject = {
+      ...defaultPerpetualPosition,
+      openEventId: nextTendermintEventId,
+      lastEventId: nextTendermintEventId,
+    };
+    await TendermintEventTable.create(nextTendermintEvent);
+    await Promise.all([
+      await PerpetualPositionTable.create(earlierPosition),
+      await PerpetualPositionTable.create(laterPosition),
+    ]);
+
+    const perpetualPositions: PerpetualPositionFromDatabase[] = await
+    PerpetualPositionTable.findAll({}, [], { readReplica: true });
+
+    expect(perpetualPositions.length).toEqual(2);
+    expect(perpetualPositions[0]).toEqual(expect.objectContaining(laterPosition));
+    expect(perpetualPositions[1]).toEqual(expect.objectContaining(earlierPosition));
+  });

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 9ed26bd and a564b30.
Files selected for processing (7)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/helpers/mock-generators.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/perpetual-position-table.test.ts (3 hunks)
  • indexer/packages/postgres/src/stores/perpetual-position-table.ts (1 hunks)
  • indexer/services/ender/tests/handlers/order-fills/liquidation-handler.test.ts (3 hunks)
  • indexer/services/ender/tests/handlers/order-fills/order-handler.test.ts (4 hunks)
  • indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql (1 hunks)
Files skipped from review due to trivial changes (2)
  • indexer/packages/postgres/tests/helpers/mock-generators.ts
  • indexer/services/ender/tests/handlers/order-fills/liquidation-handler.test.ts
Additional comments: 9
indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql (1)
  • 22-30: The change in ordering from "createdAtHeight" to "openEventId" could potentially alter the result of the query. Ensure that this change is intended and that it does not break any dependent functionality. Also, verify that "openEventId" is always unique and non-null to prevent unexpected results.
indexer/packages/postgres/__tests__/stores/perpetual-position-table.test.ts (2)
  • 15-18: The new import TendermintEventCreateObject is used in the new test case for sorting PerpetualPositions by openEventId. This is in line with the changes made in the PR.

  • 191-224: This new test case checks if the PerpetualPositions are sorted by openEventId. It creates two PerpetualPositions with different openEventIds and checks if the returned positions are sorted correctly. This is a good addition to the test suite as it verifies the new functionality introduced in this PR.

indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts (3)
  • 146-147: Ensure that the new openEventId and lastEventId values are correct and that they are being used correctly in the tests.

  • 317-318: Ensure that the openEventId value is correct and that it is being used correctly in the tests.

  • 1140-1140: Ensure that the openEventId value is correct and that it is being used correctly in the tests.

indexer/packages/postgres/__tests__/helpers/constants.ts (2)
  • 287-294: The addition of defaultTendermintEvent4 is consistent with the existing pattern of defining test constants. Ensure that the values assigned to blockHeight, transactionIndex, and eventIndex are appropriate for the tests where this constant will be used.

  • 310-314: The creation of defaultTendermintEventId4 is consistent with the existing pattern of defining test constants. Ensure that the values passed to createEventId are appropriate for the tests where this constant will be used.

indexer/packages/postgres/src/stores/perpetual-position-table.ts (1)
  • 124-130: The ordering of the query results has been changed from createdAtHeight to openEventId. Ensure that this change does not affect any dependent logic that relies on the previous ordering.
- baseQuery = baseQuery.orderBy(
-   PerpetualPositionColumns.createdAtHeight,
-   Ordering.ASC,
- );
+ baseQuery = baseQuery.orderBy(
+   PerpetualPositionColumns.subaccountId,
+   Ordering.ASC,
+ ).orderBy(
+   PerpetualPositionColumns.openEventId,
+   Ordering.DESC,
+ );

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 a564b30 and 8612b0d.
Files selected for processing (1)
  • indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql (1 hunks)
Files skipped from review due to trivial changes (1)
  • indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql

@Christopher-Li Christopher-Li merged commit 7db0ebf into main Nov 9, 2023
11 checks passed
@Christopher-Li Christopher-Li deleted the cl_openEventId branch November 9, 2023 21:31
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