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 surplus invariant fixed #786

Merged
merged 3 commits into from
Mar 6, 2024

Conversation

dapp-whisperer
Copy link
Contributor

@dapp-whisperer dapp-whisperer commented Mar 4, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced surplus check logic in contract operations to improve reliability and security.
    • Introduced a new test for liquidation scenarios, enhancing test coverage and stability.
  • Bug Fixes

    • Improved assignment logic for transaction initiators in tests, ensuring more accurate simulation of user interactions.
  • Refactor

    • Updated test setup and logging for better clarity and debugging capabilities in testing environments.

Copy link
Contributor

coderabbitai bot commented Mar 4, 2024

Walkthrough

The recent updates involve enhancing the logic for surplus checks in a smart contract and improving a test suite for better coverage and debugging capabilities. Specifically, the surplus check logic now considers an additional condition, and the testing framework sees refinements in variable assignments and the introduction of new logging and testing functionalities focused on liquidation scenarios.

Changes

Files Summary
.../TestContracts/invariants/TargetFunctions.sol Updated surplus check logic to include a condition based on _icrToLiq compared to cdpManager.MCR().
.../foundry_test/EchidnaToFoundry.t.sol - Added conditional sender assignment based on msg.sender modulo 3.
- Changed actor assignment in setUp.
- Added logging for price variables in _logStakes.
- Introduced test_debugTheLiquidation.

🐇💻✨

In the land of code, where smart contracts thrive,

A rabbit hopped in, making updates come alive.

With a flick and a hop, surplus checks refined,

Tests enhanced, with liquidation in mind.

Through the fields of logic, it dances with grace,

Celebrating changes, in the digital space. 🌟🥕

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.

@getrecon-bot
Copy link

Job ID c4441f51-1a8f-4ade-89ad-e623a99cd4f3
Command yarn && git submodule init && git submodule update && solc-select use 0.8.17 && cd packages/contracts/ && yarn echidna --test-mode assertion --test-limit 300000
Instance ID i-017a9636ddeba5622
Instance Type c5.2xlarge
Status Started
Elapsed

@getrecon-bot
Copy link

Job ID c4441f51-1a8f-4ade-89ad-e623a99cd4f3
Command yarn && git submodule init && git submodule update && solc-select use 0.8.17 && cd packages/contracts/ && yarn echidna --test-mode assertion --test-limit 300000
Instance ID i-017a9636ddeba5622
Instance Type c5.2xlarge
Status Running
Elapsed 47 seconds

1 similar comment
@getrecon-bot
Copy link

Job ID c4441f51-1a8f-4ade-89ad-e623a99cd4f3
Command yarn && git submodule init && git submodule update && solc-select use 0.8.17 && cd packages/contracts/ && yarn echidna --test-mode assertion --test-limit 300000
Instance ID i-017a9636ddeba5622
Instance Type c5.2xlarge
Status Running
Elapsed 47 seconds

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c49e5f1 and 12a473e.
Files selected for processing (2)
  • packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol (1 hunks)
  • packages/contracts/foundry_test/EchidnaToFoundry.t.sol (4 hunks)
Additional comments: 5
packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol (1)
  • 240-243: The conditional surplus checks after liquidation are based on _icrToLiq <= cdpManager.MCR(). This condition ensures that surplus checks are only performed when the ICR (Initial Collateral Ratio) to liquidate is less than or equal to the MCR (Minimum Collateral Ratio). It's crucial to verify that this logic aligns with the intended behavior for handling surplus in liquidation scenarios. If the condition is too restrictive or not aligned with the protocol's risk parameters, it may need adjustment.
packages/contracts/foundry_test/EchidnaToFoundry.t.sol (4)
  • 28-30: The conditional assignment for sender based on msg.sender modulo 3 is a clever way to simulate different users in tests. However, ensure that this randomness aligns with the intended test scenarios and doesn't introduce unintended flakiness.
  • 37-37: Changing the assignment of actor in the setUp function to use address(USER1) directly simplifies the setup process. This change assumes USER1 is a predefined constant representing a user address, which should be validated for correctness in the context of the test environment.
  • 229-232: Adding logging statements for price-related variables in the _logStakes function enhances the visibility of test execution, which is beneficial for debugging. Ensure that these logs provide sufficient context and are used consistently across all relevant test functions.
  • 1678-1690: The addition of a new test function test_debugTheLiquidation for liquidation scenarios is a valuable enhancement to the testing framework. It's important to ensure that this test covers a wide range of liquidation conditions and accurately reflects the contract's behavior in these scenarios. Consider adding more detailed comments or documentation within the test function to explain the specific scenarios being tested and the expected outcomes.

Comment on lines +240 to +243
if (_icrToLiq <= cdpManager.MCR()) {
gte(vars.collSurplusPoolBefore + 4, vars.collSurplusPoolAfter, "SURPLUS-CHECK-1");
gte(vars.userSurplusBefore + 4, vars.userSurplusAfter, "SURPLUS-CHECK-2");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic for surplus checks after liquidation (SURPLUS-CHECK-1 and SURPLUS-CHECK-2) uses a fixed value of 4 to compare the before and after states of vars.collSurplusPool and vars.userSurplus. This approach assumes a maximum surplus cap of 4 wei, which might not be flexible or accurate for all scenarios. Consider if this fixed value aligns with the intended logic and whether it should be configurable or calculated based on dynamic parameters to ensure it accurately reflects the surplus cap under different conditions.


The use of gte function calls within the surplus check logic (SURPLUS-CHECK-1 and SURPLUS-CHECK-2) implies a comparison that expects the after state to be greater than or equal to the before state plus a fixed value. This logic should be carefully reviewed to ensure it accurately represents the expected state changes following a liquidation event. If the logic does not correctly account for all possible outcomes or if the fixed value does not accurately represent the surplus cap, it may lead to incorrect assessments of the protocol's financial health post-liquidation.

@getrecon-bot
Copy link

Job ID c4441f51-1a8f-4ade-89ad-e623a99cd4f3
Command yarn && git submodule init && git submodule update && solc-select use 0.8.17 && cd packages/contracts/ && yarn echidna --test-mode assertion --test-limit 300000
Instance ID i-017a9636ddeba5622
Instance Type c5.2xlarge
Status Error
Elapsed 1 day 5 hours 25 minutes 55 seconds

@dapp-whisperer dapp-whisperer merged commit b322029 into feat-surplus-invariant Mar 6, 2024
4 of 5 checks passed
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 12a473e and 6990b14.
Files selected for processing (2)
  • packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol (2 hunks)
  • packages/contracts/foundry_test/EchidnaToFoundry.t.sol (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/contracts/contracts/TestContracts/invariants/TargetFunctions.sol
Additional comments: 4
packages/contracts/foundry_test/EchidnaToFoundry.t.sol (4)
  • 28-31: The conditional assignment for sender based on msg.sender modulo 3 is a clever way to diversify the sender address for testing purposes. However, ensure that this logic aligns with the intended test scenarios and that it doesn't introduce any unintended side effects in the test outcomes.
  • 37-37: Changing the assignment of actor in the setUp function to use address(USER1) directly is a straightforward modification. This change simplifies the code by removing an unnecessary indirection through the actors array. Just ensure that USER1 is properly defined and accessible within this context.
  • 224-240: > 📝 NOTE

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

Adding logging statements for price-related variables in the _logStakes function enhances the visibility of test execution, which is beneficial for debugging and understanding the test flow. It's good practice to include such logging in test environments to aid in diagnosing issues. Ensure that these logs are only enabled during testing to avoid any performance impact in production code.

  • 1678-1693: The addition of a new test function test_debugTheLiquidation for liquidation scenarios is a valuable enhancement to the testing framework. It appears to simulate various operations leading up to and including liquidation, which is crucial for ensuring the robustness of the contract under test. Make sure that the test covers a wide range of scenarios and correctly asserts the expected outcomes to fully validate the liquidation logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants