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

prevent discount rate manipulation #16

Closed
wants to merge 4 commits into from
Closed

Conversation

aazhou1
Copy link

@aazhou1 aazhou1 commented Sep 5, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new mapping to track the validity of auction rates, enhancing the management of auction data.
  • Improvements

    • Enhanced validation process for retrieving auction clearing rates, ensuring only valid rates are returned.
    • Improved error messages for better clarity and user feedback when conditions are not met.

@aazhou1 aazhou1 self-assigned this Sep 5, 2024
Copy link

coderabbitai bot commented Sep 5, 2024

Walkthrough

The TermDiscountRateAdapter contract has been modified to enhance the handling and validation of auction rates. A new public mapping, rateInvalid, tracks the validity of auction rates for specific addresses and auction IDs. The getAuctionClearingRate function now incorporates improved checks for auction validity, ensuring that only valid rates are returned. Additionally, error messages in require statements have been clarified for better feedback.

Changes

File Change Summary
src/TermDiscountRateAdapter.sol Introduced mapping(address => mapping (bytes32 => bool)) public rateInvalid to track auction rate validity. Updated getAuctionClearingRate to include detailed validation checks for auction rates and improved error messages in require statements. Updated constructor to accept an additional parameter adminWallet_.

Poem

In fields of code, I hop with glee,
Valid rates now dance, oh what a spree!
With checks so bright, they shine like the sun,
Auctions are fair, let the fun be begun!
So here’s to the change, a joyful delight,
A rabbit's cheer for rates that are right! 🐇✨


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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

@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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2272739 and a3ee52b.

Files selected for processing (1)
  • src/TermDiscountRateAdapter.sol (2 hunks)
Additional comments not posted (1)
src/TermDiscountRateAdapter.sol (1)

16-16: Mapping rateInvalid is correctly declared.

The mapping is declared as public, which is appropriate for its intended use in tracking the validity of auction rates. This aligns with the PR objectives to prevent discount rate manipulation.

Comment on lines 37 to 52
require(len > 0, "No auctions found");

return auctionMetadata[len - 1].auctionClearingRate;
if (len > 1) {
if ((block.timestamp - auctionMetadata[len - 1].auctionClearingBlockTimestamp) < 30 minutes) {
uint256 i = 0;
while (!rateInvalid[repoToken][auctionMetadata[len - 1].termAuctionId]) {
i--;
require(i >= 0, "No valid auction rate found");
}
return auctionMetadata[i].auctionClearingRate;
}
}

require(!rateInvalid[repoToken][auctionMetadata[0].termAuctionId], "Most recent auction rate is invalid");

return auctionMetadata[0].auctionClearingRate;
Copy link

Choose a reason for hiding this comment

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

Review and fix the loop logic in getDiscountRate.

The loop intended to find a valid auction rate has several issues:

  • The index i is decremented from len - 1 but is not initialized before the loop starts.
  • The loop condition checks rateInvalid but does not correctly decrement i within the bounds of the array, which can lead to an out-of-bounds error.

Consider the following refactor to address these issues:

-                uint256 i = 0;
-                while (!rateInvalid[repoToken][auctionMetadata[len - 1].termAuctionId]) {
-                    i--;
-                    require(i >= 0, "No valid auction rate found");
-                }
+                int256 i = int256(len - 1);
+                while (i >= 0 && !rateInvalid[repoToken][auctionMetadata[uint256(i)].termAuctionId]) {
+                    i--;
+                }
+                require(i >= 0, "No valid auction rate found");
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
require(len > 0, "No auctions found");
return auctionMetadata[len - 1].auctionClearingRate;
if (len > 1) {
if ((block.timestamp - auctionMetadata[len - 1].auctionClearingBlockTimestamp) < 30 minutes) {
uint256 i = 0;
while (!rateInvalid[repoToken][auctionMetadata[len - 1].termAuctionId]) {
i--;
require(i >= 0, "No valid auction rate found");
}
return auctionMetadata[i].auctionClearingRate;
}
}
require(!rateInvalid[repoToken][auctionMetadata[0].termAuctionId], "Most recent auction rate is invalid");
return auctionMetadata[0].auctionClearingRate;
require(len > 0, "No auctions found");
if (len > 1) {
if ((block.timestamp - auctionMetadata[len - 1].auctionClearingBlockTimestamp) < 30 minutes) {
int256 i = int256(len - 1);
while (i >= 0 && !rateInvalid[repoToken][auctionMetadata[uint256(i)].termAuctionId]) {
i--;
}
require(i >= 0, "No valid auction rate found");
return auctionMetadata[uint256(i)].auctionClearingRate;
}
}
require(!rateInvalid[repoToken][auctionMetadata[0].termAuctionId], "Most recent auction rate is invalid");
return auctionMetadata[0].auctionClearingRate;

Copy link

@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 a3ee52b and a87931f.

Files selected for processing (1)
  • src/TermDiscountRateAdapter.sol (2 hunks)
Additional comments not posted (5)
src/TermDiscountRateAdapter.sol (5)

7-7: Approved import addition.

The addition of AccessControlUpgradeable is appropriate for role management in this context.


14-16: Well-structured contract setup.

The contract uses inheritance effectively and defines necessary roles and immutabilities clearly.

Also applies to: 19-19


25-27: Constructor enhancement approved.

The addition of devopsWallet_ for role assignment during construction enhances security and initialization clarity.


59-60: Function to mark rates as invalid is well-implemented.

Secured by the DEVOPS_ROLE, this function effectively allows marking auction rates as invalid, aligning with the PR's objectives.


41-57: Review and verify loop logic in getDiscountRate.

The loop logic intended to find a valid auction rate needs verification against previous comments. Consider the following refactor to address potential issues:

-                uint256 i = 0;
-                while (!rateInvalid[repoToken][auctionMetadata[len - 1].termAuctionId]) {
-                    i--;
-                    require(i >= 0, "No valid auction rate found");
-                }
+                int256 i = int256(len - 1);
+                while (i >= 0 && !rateInvalid[repoToken][auctionMetadata[uint256(i)].termAuctionId]) {
+                    i--;
+                }
+                require(i >= 0, "No valid auction rate found");

Copy link

@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 a87931f and 4d0cee9.

Files selected for processing (1)
  • src/TermDiscountRateAdapter.sol (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/TermDiscountRateAdapter.sol

Copy link

@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 4d0cee9 and 57fa41b.

Files selected for processing (1)
  • src/TermDiscountRateAdapter.sol (2 hunks)
Additional comments not posted (5)
src/TermDiscountRateAdapter.sol (5)

7-7: Approved: Import statement for AccessControlUpgradeable.

The import of AccessControlUpgradeable from OpenZeppelin is appropriate for managing role-based access control, which is crucial for functions that modify sensitive data.


14-16: Approved: Contract and role initialization.

The contract inherits from ITermDiscountRateAdapter and AccessControlUpgradeable. The ADMIN_ROLE is correctly defined using a keccak256 hash, which is a standard practice for defining unique role identifiers in Solidity.

Also applies to: 19-19


25-27: Approved: Constructor modification.

The constructor now accepts an adminWallet_ parameter and grants the ADMIN_ROLE to it. This change enhances security by allowing role management through constructor parameters, aligning with best practices for contract initialization in upgradeable contracts.


41-57: Review: Enhanced auction rate validation logic.

The function getDiscountRate has been modified to include more robust checks for auction validity using the rateInvalid mapping. This is a critical enhancement for preventing rate manipulation. The logic correctly handles scenarios where no valid rates are found within the last 30 minutes and ensures that the most recent auction rate is not invalid. These changes are aligned with the PR's objectives to enhance the integrity of discount rate calculations.


66-86: Review: Invalidate auction result function.

The function invalidateAuctionResult is well-implemented with checks for the existence of the auction and whether the rate has already been invalidated. The use of onlyRole(ADMIN_ROLE) ensures that only authorized users can invalidate rates, which is crucial for maintaining control over sensitive operations. The function's logic is clear and aligns with the PR's objectives to prevent manipulation.

@aazhou1 aazhou1 closed this Sep 13, 2024
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.

3 participants