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

feat: Enforce MarketParam.Pair uniqueness constraint #1193

Merged
merged 7 commits into from
Mar 19, 2024

Conversation

Eric-Warehime
Copy link
Contributor

Changelist

Enforces a uniqueness constraint on Create/Update operations for MarketParams.
This is required for Slinky since it uses Base/Quote as a compound primary key to reason about pairs being posted to chain as well as prices retrieved from the sidecar.

Test Plan

Unit tests included.

Mainnet data also shows no existing pairs with duplicates, so this should just prevent things in addition to avoiding this in governance votes in the interim.

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 Mar 18, 2024

Walkthrough

The recent updates bring enhanced validation mechanisms to prevent market pair duplication in market creation and parameter modification processes. A new function ensures specific market events are absent in Indexer blocks, while checks in creation and modification ensure market pair uniqueness. Test setups and error handling are refined to align with these changes, including a specific error for duplicate market parameter pairs.

Changes

Files Change Summary
.../keeper/prices.go, .../x/prices/keeper/market.go Added AssertNMarketEventsNotInIndexerBlock for verifying market events in Indexer blocks and validation in CreateMarket to prevent duplicate market pairs.
.../x/prices/keeper/market_param.go, .../x/prices/keeper/market_param_test.go Implemented validation in ModifyMarketParam for base/quote pair uniqueness and added a test for handling duplicate pairs.
.../x/prices/keeper/market_price_test.go, .../x/prices/keeper/market_test.go Updated test setups with explicit market pairs and refined error messages and logic related to market parameters and prices.
.../x/prices/types/errors.go Introduced ErrMarketParamPairAlreadyExists for duplicate market parameter pairs.

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.
    • 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 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 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: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b458799 and 9dc0311.
Files selected for processing (7)
  • protocol/testutil/keeper/prices.go (1 hunks)
  • protocol/x/prices/keeper/market.go (1 hunks)
  • protocol/x/prices/keeper/market_param.go (1 hunks)
  • protocol/x/prices/keeper/market_param_test.go (1 hunks)
  • protocol/x/prices/keeper/market_price_test.go (1 hunks)
  • protocol/x/prices/keeper/market_test.go (3 hunks)
  • protocol/x/prices/types/errors.go (1 hunks)
Additional comments: 7
protocol/x/prices/types/errors.go (1)
  • 27-27: The addition of ErrMarketParamPairAlreadyExists with code 205 is correctly implemented and consistent with the established pattern for market-related errors in this file.
protocol/x/prices/keeper/market_param.go (1)
  • 47-52: The added validation for base/quote uniqueness in ModifyMarketParam is correctly implemented and follows the error handling pattern of the codebase. However, consider more efficient data structures or indexing mechanisms for uniqueness checks if the number of market params is expected to grow significantly.
protocol/x/prices/keeper/market.go (1)
  • 39-47: The stateful validation added to CreateMarket for ensuring market pair uniqueness is correctly implemented. As with ModifyMarketParam, consider more efficient data structures or indexing mechanisms for uniqueness checks if the number of markets is expected to grow significantly.
protocol/x/prices/keeper/market_price_test.go (1)
  • 138-154: The addition of explicit pair definitions for market prices in the test setup is correctly implemented and necessary for testing the new market pair uniqueness validation logic.
protocol/x/prices/keeper/market_param_test.go (1)
  • 112-122: The new test case "Duplicate pair fails" in TestModifyMarketParam_Errors correctly tests the added validation logic for base/quote pair uniqueness and follows best practices for error handling.
protocol/x/prices/keeper/market_test.go (1)
  • 170-198: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [161-184]

The updates to error messages, adjustments to market param and price IDs, and the introduction of market creation logic with associated checks in TestCreateMarket_Errors are correctly implemented. These changes ensure that the new functionality is accurately tested and follow best practices for error handling.

protocol/testutil/keeper/prices.go (1)
  • 174-183: The addition of AssertNMarketEventsNotInIndexerBlock is a valuable tool for verifying the correct number of market events in the Indexer block message, especially in the context of testing the enforcement of market pair uniqueness constraints. It's recommended to add a brief comment explaining the function's purpose and usage to aid future maintainability.

ctx = ctx.WithTxBytes(constants.TestTxBytes)

mockTimeKeeper.On("Now").Return(constants.TimeT)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need a timekeeper now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other test cases never succesfully create a market param. They all fail before hitting this line

k.marketToCreatedAt[marketParam.Id] = k.timeProvider.Now()

The additional test case relies on a market param already existing so it has to be able to succeed once.

@@ -44,6 +44,13 @@ func (k Keeper) ModifyMarketParam(
errorsmod.Wrapf(types.ErrMarketExponentCannotBeUpdated, lib.UintToString(updatedMarketParam.Id))
}

// Validate base/quote uniqueness
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem necessary? Isn't there already a check that the updatedMarket must already exist in state + we've introduced a uniqueness constraint across all market-pairs in state?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this check is actually necessary, as it's possible for a MarketParam update to change the marketParam.Pair, and the updated Pair may conflict with one in state

Copy link
Contributor

@Christopher-Li Christopher-Li left a comment

Choose a reason for hiding this comment

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

great catch nikhil, i'm going to request changes for updates until we finalize

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 9dc0311 and 4d7e839.
Files selected for processing (4)
  • protocol/testing/e2e/gov/perpetuals_test.go (2 hunks)
  • protocol/testutil/keeper/perpetuals.go (1 hunks)
  • protocol/x/perpetuals/keeper/msg_server_update_perpetual_params_test.go (1 hunks)
  • protocol/x/subaccounts/keeper/subaccount_test.go (2 hunks)
Additional comments: 6
protocol/x/perpetuals/keeper/msg_server_update_perpetual_params_test.go (1)
  • 27-28: The introduction of specific pairs for test markets (testMarket1 and testMarket4) is a positive change. It ensures that the tests are more deterministic and closely aligned with the PR's objective to enforce uniqueness constraints on market pairs. This change enhances the robustness of the test suite by explicitly testing the behavior with distinct market pairs.
protocol/testutil/keeper/perpetuals.go (1)
  • 240-240: Moving the CreateNMarkets function call outside the loop is a significant performance optimization. Previously, if CreateNMarkets was called inside the loop, it would unnecessarily recreate markets for each iteration, leading to redundant operations and increased test execution time. This change ensures that markets are created once before the loop, which is more efficient and aligns with best practices for setting up test data.
protocol/testing/e2e/gov/perpetuals_test.go (2)
  • 4-4: The addition of the fmt import is necessary for the changes made in the test cases, specifically for generating market parameters with dynamic pairs. This is a good practice as it ensures that the tests cover scenarios with varying market pairs, aligning with the PR's objective to enforce uniqueness constraints.
  • 280-280: Modifying the generation of market parameters to include a pair based on the index (pricestest.WithPair(fmt.Sprintf("%d-%d", i, i))) is a thoughtful change. It ensures that each market parameter has a unique pair, which is crucial for testing the enforcement of uniqueness constraints on market pairs. This change enhances the test's ability to validate the new logic introduced in the PR effectively.
protocol/x/subaccounts/keeper/subaccount_test.go (2)
  • 2022-2026: The test case "2 updates, 1 update involves not-updatable perp" uses a MarketParamPrice with a PriceValue of 0, which might be intended to simulate a scenario where a perpetual cannot be updated due to its market parameters. However, it's crucial to ensure that the logic for determining whether a perpetual is updatable or not is correctly implemented and tested. This test case seems to focus on the behavior when an update involves a perpetual that cannot be updated, but it's important to verify that the system's behavior aligns with the expected business rules regarding perpetual updates.

Please verify that the logic for determining updatable perpetuals correctly handles cases where the market parameters might indicate a non-updatable state. Additionally, ensure that the test case accurately reflects the intended scenarios and that there are corresponding checks in the implementation to prevent updates to non-updatable perpetuals.

  • 3609-3613: The test case "2 updates, 1 update involves not-updatable perp" defines market parameter prices for perpetuals with IDs 100 and 101. It's important to ensure that these IDs correspond to perpetuals that are correctly set up in the test environment and that the test accurately simulates the scenario it's designed to test. Specifically, the PriceValue of 0 for the perpetual with ID 101 might be used to simulate a scenario where the perpetual cannot be updated due to its market parameters.

Please verify that the perpetual IDs used in the test case correspond to perpetuals that are correctly set up and that the test's setup accurately reflects the scenario it's intended to test. Additionally, ensure that the logic for handling updates to perpetuals correctly takes into account the market parameters and prevents updates to perpetuals that should not be updated based on their current state or market conditions.

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 4d7e839 and 0524fc8.
Files selected for processing (4)
  • protocol/x/prices/keeper/market_param.go (1 hunks)
  • protocol/x/prices/keeper/market_param_test.go (3 hunks)
  • protocol/x/prices/keeper/msg_server_update_market_param_test.go (1 hunks)
  • protocol/x/prices/types/errors.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • protocol/x/prices/keeper/market_param.go
  • protocol/x/prices/keeper/market_param_test.go
  • protocol/x/prices/types/errors.go
Additional comments: 1
protocol/x/prices/keeper/msg_server_update_market_param_test.go (1)
  • 33-33: The change from a hardcoded "PIKACHU-XXX" value to testMarketParam.Pair in the Pair field assignment enhances test flexibility and maintainability by using the setup provided by GenerateMarketParamPrice. This is a good practice for writing dynamic tests.

Ensure that all relevant test scenarios, especially edge cases, are still adequately covered after this change.

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 0524fc8 and 2082e88.
Files selected for processing (2)
  • protocol/testing/e2e/gov/prices_test.go (1 hunks)
  • protocol/testutil/keeper/perpetuals.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/testutil/keeper/perpetuals.go
Additional comments: 1
protocol/testing/e2e/gov/prices_test.go (1)
  • 31-31: The change to use GENESIS_MARKET_PARAM.Pair for MODIFIED_MARKET_PARAM.Pair is a good practice for ensuring consistency in test setups. This is particularly important for accurately testing the enforcement of uniqueness constraints on MarketParams during update operations. Well done on maintaining test accuracy and relevance to the PR objectives.

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 2082e88 and e138330.
Files selected for processing (3)
  • protocol/x/prices/keeper/market_param.go (1 hunks)
  • protocol/x/prices/keeper/market_param_test.go (3 hunks)
  • protocol/x/prices/types/errors.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • protocol/x/prices/keeper/market_param.go
  • protocol/x/prices/keeper/market_param_test.go
  • protocol/x/prices/types/errors.go

@Christopher-Li Christopher-Li merged commit 0e816e5 into dydxprotocol:main Mar 19, 2024
16 checks passed
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