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: reject market-updates that modify enabled field in ante-handler #1962

Merged
merged 10 commits into from
Jul 25, 2024

Conversation

nivasan1
Copy link
Contributor

@nivasan1 nivasan1 commented Jul 24, 2024

In This PR

  • I modify the ValidateMarketUpdateDecorator's AnteHandler to reject MarketMap market updates that mutate markets as follows
  1. For markets that do not exist in the market-map / have yet to be added to the PricesKeeper
    • These markets should always be Disabled, any update that results in a market-map violating this will be rejected.
  2. For markets that have already been added to the PricesKeeper
    • The enabled field of the market in the market-map should never be changed by incoming market-updates

Summary by CodeRabbit

  • New Features

    • Introduced a MarketMapKeeper to enhance market data management in the application.
    • Improved validation of market updates, ensuring proper handling of enabled and disabled market states.
    • Added new error constants for better feedback on market update scenarios.
  • Bug Fixes

    • Refined logic for market updates, preventing issues with enabling previously disabled markets.
  • Tests

    • Expanded test cases to cover various market states and ensure robust validation of market updates, including conditions for handling enabled and disabled markets.

Copy link
Contributor

coderabbitai bot commented Jul 24, 2024

Walkthrough

This update significantly enhances market management in the application by introducing a MarketMapKeeper to the HandlerOptions struct. This addition improves the validation and handling of market updates, leading to better state management and streamlined processes. Overall, the changes boost both functionality and robustness during transaction processing.

Changes

Files Change Summary
protocol/app/ante.go, protocol/app/ante/market_update.go, protocol/app/ante/market_update_test.go Added MarketMapKeeper to HandlerOptions, updated decorators and methods for market update validation, and enhanced tests for managing market states.
protocol/app/app.go Updated buildAnteHandler to include MarketMapKeeper, integrating market-related operations into the ante handler's context.
protocol/x/prices/types/errors.go Introduced new error constants related to market updates, enhancing error handling and providing clearer feedback for market operations.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant AnteHandler
    participant MarketMapKeeper

    User->>App: Initiate transaction
    App->>AnteHandler: Build AnteHandler with MarketMapKeeper
    AnteHandler->>MarketMapKeeper: Validate market updates
    MarketMapKeeper-->>AnteHandler: Return market data
    AnteHandler->>App: Process transaction with validation
    App-->>User: Transaction result
Loading

Poem

🐇 In fields of code where markets play,
New maps emerge to light the way.
With updates swift and logic bright,
We’ll hop through changes, pure delight!
A keeper of data, wise and true,
Enhancing trades for me and you! 🌼


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 testing code 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 testing code 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 testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0076245 and 33ad36a.

Files selected for processing (4)
  • protocol/app/ante.go (2 hunks)
  • protocol/app/ante/market_update.go (4 hunks)
  • protocol/app/ante/market_update_test.go (8 hunks)
  • protocol/app/app.go (1 hunks)
Additional comments not posted (13)
protocol/app/ante/market_update.go (5)

22-24: Interface definition looks good.

The MarketMapKeeper interface is well-defined with a single method GetAllMarkets.


43-49: Constructor changes look good.

The NewValidateMarketUpdateDecorator constructor correctly initializes the MarketMapKeeper parameter.


75-97: Integration of validation logic looks good.

The AnteHandle method correctly integrates the call to doMarketsUpdateEnabledValues for validating market updates.


131-174: Validation logic and error handling look good.

The doMarketsUpdateEnabledValues method correctly validates market updates and handles errors appropriately.


176-196: Helper function logic and error handling look good.

The marketParamsSliceToMap function correctly converts a slice of market parameters into a map and handles errors appropriately.

protocol/app/ante/market_update_test.go (5)

Line range hint 4-26: Import changes look good.

The new imports for slinkylib and other dependencies are necessary and correctly added.


Line range hint 28-181: Test cases look good.

The TestIsMarketUpdateTx function includes comprehensive test cases that cover all necessary scenarios.


182-195: Test variables look good.

The new test variables testMarketWithEnabled and testMarketParams are correctly defined and used.


226-230: Struct changes look good.

The args struct is correctly updated with new fields marketMapMarkets and marketParams.


Line range hint 387-510: Test cases look good.

The TestValidateMarketUpdateDecorator_AnteHandle function includes comprehensive test cases that cover all necessary scenarios.

protocol/app/ante.go (2)

35-35: Struct changes look good.

The HandlerOptions struct is correctly updated with a new field MarketMapKeeper.


122-124: Function changes look good.

The NewAnteHandler function correctly integrates the MarketMapKeeper parameter.

protocol/app/app.go (1)

1911-1911: Verify the integration of MarketMapKeeper.

Ensure that the MarketMapKeeper is correctly integrated and utilized within the AnteHandler to enforce the new validation rules for market updates.

Verification successful

Verified the integration of MarketMapKeeper.

The MarketMapKeeper is correctly integrated and utilized within the AnteHandler logic to enforce validation rules for market updates.

  • protocol/app/ante.go: MarketMapKeeper is part of the AnteHandler options.
  • protocol/app/ante/market_update.go: MarketMapKeeper is used in ValidateMarketUpdateDecorator to validate market updates.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `MarketMapKeeper` in the `AnteHandler`.

# Test: Search for the `MarketMapKeeper` usage in the `AnteHandler`. Expect: Proper utilization in validation logic.
rg --type go -A 10 'MarketMapKeeper' | grep -B 10 -A 10 'AnteHandler'

Length of output: 2691


Script:

#!/bin/bash
# Description: Verify the actual usage of `MarketMapKeeper` within the `AnteHandler` logic.

# Search for the `AnteHandler` implementation and look for `MarketMapKeeper` usage within it.
rg --type go -A 20 'AnteHandler' | grep -A 20 'MarketMapKeeper'

Length of output: 3115


Script:

#!/bin/bash
# Description: Verify the actual usage of `MarketMapKeeper` within the `AnteHandler` implementation.

# Search for the `MarketMapKeeper` usage in the context of `AnteHandler` implementation.
rg --type go 'MarketMapKeeper' -A 20

Length of output: 49910

}

// setup initial market-map markets
for _, market := range tt.args.marketMapMarkets {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move this to before creating the market in x/prices? I'm adding a dependency when creating the market in x/prices that the market needs to exist in market map

if !exists {
// if market does not exist in x/prices, it should be disabled
if market.Ticker.Enabled {
return errors.New("newly added market should be disabled")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we add errors registered to the x/prices module for these?

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.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
protocol/app/ante/market_update_test.go (1)

387-387: Add test description for clarity.

Consider adding a comment to describe the purpose of this test case.

// Test case for accepting a single message with no cross markets (only isolated).
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 33ad36a and 36fcae4.

Files selected for processing (2)
  • protocol/app/ante/market_update.go (4 hunks)
  • protocol/app/ante/market_update_test.go (8 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/app/ante/market_update.go
Additional comments not posted (10)
protocol/app/ante/market_update_test.go (10)

226-230: Ensure new fields in args struct are utilized properly.

The args struct now includes marketMapMarkets and marketParams. Ensure these fields are correctly utilized in the test cases.


446-461: New test case: Reject a single message with a new market that's enabled.

This test case ensures that a new market that is enabled is rejected if it already exists in the market map as disabled.


463-477: New test case: Reject a single update-markets message with a new market that's enabled.

This test case ensures that an update-markets message with a new market that's enabled is rejected if it already exists in the market map as disabled.


479-493: New test case: Reject a single message with a new market that's enabled - simulate.

This test case ensures that a new market that is enabled is rejected in simulation mode if it already exists in the market map as disabled.


495-508: New test case: Reject a single message with a new market that's enabled, but doesn't exist in x/marketmap.

This test case ensures that a new market that is enabled is rejected if it doesn't exist in the market map.


510-523: New test case: Reject a single update-markets message with a new market that's enabled, but doesn't exist in x/marketmap.

This test case ensures that an update-markets message with a new market that's enabled is rejected if it doesn't exist in the market map.


525-539: New test case: Reject a single message with a new market that's enabled, but doesn't exist in x/marketmap - simulate.

This test case ensures that a new market that is enabled is rejected in simulation mode if it doesn't exist in the market map.


541-556: New test case: Reject single message disabling a market.

This test case ensures that a message disabling a market is rejected if the market is enabled in the market map.


558-573: New test case: Reject single update markets message disabling a market.

This test case ensures that an update markets message disabling a market is rejected if the market is enabled in the market map.


575-590: New test case: Reject single message disabling a market - simulate.

This test case ensures that a message disabling a market is rejected in simulation mode if the market is enabled in the market map.

@nivasan1 nivasan1 force-pushed the nv/ante-handler-upsert-rejections branch from 36fcae4 to 9ec5a76 Compare July 24, 2024 21:39
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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 36fcae4 and 9ec5a76.

Files selected for processing (4)
  • protocol/app/ante.go (2 hunks)
  • protocol/app/ante/market_update.go (4 hunks)
  • protocol/app/ante/market_update_test.go (8 hunks)
  • protocol/app/app.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • protocol/app/ante.go
  • protocol/app/app.go
Additional comments not posted (13)
protocol/app/ante/market_update.go (7)

22-24: Interface MarketMapKeeper looks good.

The MarketMapKeeper interface is well-defined and aligns with the PR objectives.


43-49: Constructor update looks good.

The NewValidateMarketUpdateDecorator constructor update aligns with the introduction of the MarketMapKeeper interface.


75-97: Enhanced validation logic in AnteHandle looks good.

The additional validation logic ensures that market updates adhere to specific rules, enhancing the robustness of market state management.


131-174: New method doMarketsUpdateEnabledValues looks good.

The doMarketsUpdateEnabledValues method encapsulates the validation logic, making the code more modular and maintainable.


176-196: New function marketParamsSliceToMap looks good.

The marketParamsSliceToMap function includes validation to prevent duplicate entries, ensuring the integrity of market data.


Line range hint 4-4:
Test cases in TestIsMarketUpdateTx look good.

The test cases cover various scenarios, ensuring comprehensive validation of market update transactions.


182-196: Test cases in TestValidateMarketUpdateDecorator_AnteHandle look good.

The new test cases ensure that the enhanced validation logic in the AnteHandle method is thoroughly tested.

protocol/app/ante/market_update_test.go (6)

195-195: New test variable testMarketWithEnabled looks good.

The new test variable is essential for testing various scenarios involving market updates.


226-230: Updated args struct looks good.

The updated struct enhances the test's ability to simulate different market states and validate the logic of the ValidateMarketUpdateDecorator.


387-387: New test cases in TestValidateMarketUpdateDecorator_AnteHandle look good.

The new test cases ensure comprehensive coverage of various states and transitions of market updates.


446-461: New test case for rejecting enabled market updates looks good.

The test case ensures that the validation logic correctly rejects unauthorized market updates.


463-477: New test case for rejecting update-markets messages with enabled market looks good.

The test case ensures that the validation logic correctly rejects unauthorized market updates.


479-539: New test case for rejecting enabled market updates in simulation looks good.

The test case ensures that the validation logic correctly rejects unauthorized market updates in simulation scenarios.

@nivasan1 nivasan1 requested a review from chenyaoy July 25, 2024 16:59
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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9ec5a76 and a1407e6.

Files selected for processing (5)
  • protocol/app/ante.go (2 hunks)
  • protocol/app/ante/market_update.go (4 hunks)
  • protocol/app/ante/market_update_test.go (8 hunks)
  • protocol/app/app.go (1 hunks)
  • protocol/x/prices/types/errors.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • protocol/app/ante.go
  • protocol/app/app.go
Additional comments not posted (11)
protocol/x/prices/types/errors.go (4)

51-55: LGTM!

The error constant ErrUnsafeMarketUpdate is clear and follows the existing pattern.


57-61: LGTM!

The error constant ErrMarketUpdateChangesMarketMapEnabledValue is clear and follows the existing pattern.


63-67: LGTM!

The error constant ErrMarketDoesNotExistInMarketMap is clear and follows the existing pattern.


69-73: LGTM!

The error constant ErrAdditionOfEnabledMarket is clear and follows the existing pattern.

protocol/app/ante/market_update.go (5)

22-24: LGTM!

The interface MarketMapKeeper is clear and follows the existing pattern.


43-49: LGTM!

The constructor NewValidateMarketUpdateDecorator is clear and follows the existing pattern.


75-97: LGTM!

The method AnteHandle is clear and follows the existing pattern.


131-173: LGTM!

The method doMarketsUpdateEnabledValues is clear and follows the existing pattern.


175-195: LGTM!

The method marketParamsSliceToMap is clear and follows the existing pattern.

protocol/app/ante/market_update_test.go (2)

Line range hint 28-178: LGTM!

The test case TestIsMarketUpdateTx is clear and follows the existing pattern.


Line range hint 233-680: LGTM!

The test case TestValidateMarketUpdateDecorator_AnteHandle is clear and follows the existing pattern.

@nivasan1 nivasan1 force-pushed the nv/ante-handler-upsert-rejections branch from da537c5 to e2e7f5c Compare July 25, 2024 20:33
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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a1407e6 and e2e7f5c.

Files selected for processing (5)
  • protocol/app/ante.go (2 hunks)
  • protocol/app/ante/market_update.go (4 hunks)
  • protocol/app/ante/market_update_test.go (8 hunks)
  • protocol/app/app.go (1 hunks)
  • protocol/x/prices/types/errors.go (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • protocol/app/ante.go
  • protocol/app/ante/market_update.go
  • protocol/app/app.go
  • protocol/x/prices/types/errors.go
Additional comments not posted (4)
protocol/app/ante/market_update_test.go (4)

4-5: LGTM! New imports are necessary for the added functionality.

The new imports are appropriate for the added functionality.


182-193: LGTM! New test cases cover various scenarios.

The new test cases for TestIsMarketUpdateTx adequately cover different scenarios for identifying market update transactions.


264-268: LGTM! Modifications to the args struct are necessary for the new test cases.

The modifications to the args struct are appropriate for the new test cases.


Line range hint 425-429:
LGTM! New test cases cover various scenarios.

The new test cases for TestValidateMarketUpdateDecorator_AnteHandle adequately cover different scenarios for validating market update transactions.

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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e2e7f5c and 58d3cc7.

Files ignored due to path filters (1)
  • protocol/go.sum is excluded by !**/*.sum
Files selected for processing (2)
  • protocol/app/ante/market_update_test.go (9 hunks)
  • protocol/go.mod (1 hunks)
Additional comments not posted (18)
protocol/app/ante/market_update_test.go (17)

264-268: Good addition of new fields in the args struct.

The new fields marketMapMarkets and marketParams enhance the test's ability to simulate different market states.


425-425: Ensure comprehensive test coverage.

The test case "accept a single message with no cross markets (only isolated)" is well-defined. Ensure that all edge cases are covered.


485-499: Good test case for rejecting enabled markets.

The test case "reject a single message with a new market that's enabled" is crucial for validating the rejection logic.


501-515: Good test case for rejecting update-markets with enabled markets.

The test case "reject a single update-markets message with a new market that's enabled" ensures that the update logic is correctly validated.


517-531: Good test case for rejecting enabled markets in simulation mode.

The test case "reject a single message with a new market that's enabled - simulate" covers the simulation scenario effectively.


533-546: Good test case for rejecting enabled markets not in market map.

The test case "reject a single message with a new market that's enabled, but doesn't exist in x/marketmap" ensures that only existing markets can be enabled.


548-561: Good test case for rejecting update-markets with enabled markets not in market map.

The test case "reject a single update-markets message with a new market that's enabled, but doesn't exist in x/marketmap" validates the update logic for non-existent markets.


563-577: Good test case for rejecting enabled markets not in market map in simulation mode.

The test case "reject a single message with a new market that's enabled, but doesn't exist in x/marketmap - simulate" covers the simulation scenario for non-existent markets.


579-594: Good test case for rejecting disabling markets.

The test case "reject single message disabling a market" ensures that enabled markets cannot be disabled through updates.


596-611: Good test case for rejecting update-markets disabling markets.

The test case "reject single update markets message disabling a market" validates the update logic for disabling markets.


613-628: Good test case for rejecting disabling markets in simulation mode.

The test case "reject single message disabling a market - simulate" covers the simulation scenario for disabling markets.


630-645: Good test case for adding providers to enabled markets.

The test case "adding an additional provider, while market is enabled" ensures that providers can be added to enabled markets.


647-662: Good test case for adding providers to enabled markets in simulation mode.

The test case "adding an additional provider, market is enabled - simulate" covers the simulation scenario for adding providers to enabled markets.


664-679: Good test case for adding providers via update-markets to enabled markets.

The test case "adding an additional provider via update-markets, while market is enabled" validates the update logic for adding providers to enabled markets.


681-695: Good test case for adding providers to disabled markets.

The test case "adding an additional provider, while market is disabled" ensures that providers can be added to disabled markets.


697-711: Good test case for adding providers to disabled markets in simulation mode.

The test case "adding an additional provider, while market is disabled - simulate" covers the simulation scenario for adding providers to disabled markets.


713-727: Good test case for adding providers via update-markets to disabled markets.

The test case "adding an additional provider via update-markets, while market is disabled" validates the update logic for adding providers to disabled markets.

protocol/go.mod (1)

70-70: Dependency Update: Verify compatibility with the new version of slinky.

The slinky dependency has been updated to a newer version. Ensure that the new version is compatible and does not introduce any breaking changes.

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