-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
WalkthroughThe Changes
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
Files selected for processing (1)
- src/TermDiscountRateAdapter.sol (2 hunks)
Additional comments not posted (1)
src/TermDiscountRateAdapter.sol (1)
16-16
: MappingrateInvalid
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.
src/TermDiscountRateAdapter.sol
Outdated
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; |
There was a problem hiding this comment.
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 fromlen - 1
but is not initialized before the loop starts. - The loop condition checks
rateInvalid
but does not correctly decrementi
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.
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; |
There was a problem hiding this 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
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 ingetDiscountRate
.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");
There was a problem hiding this 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
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
There was a problem hiding this 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
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
andAccessControlUpgradeable
. TheADMIN_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 theADMIN_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 therateInvalid
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 ofonlyRole(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.
Summary by CodeRabbit
New Features
Improvements