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

Add overflow check on interest rates #1432

Merged
merged 5 commits into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/masternodes/loan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,24 @@ const auto InterestPerBlock = [](const CInterestRateV3& rate, const uint32_t hei

CInterestAmount TotalInterestCalculation(const CInterestRateV3& rate, const uint32_t height)
{
const auto totalInterest = (height - rate.height) * rate.interestPerBlock.amount;
const auto heightDiff = (height - rate.height);
const auto interestAmount = rate.interestPerBlock.amount;
const auto totalInterest = heightDiff * interestAmount;

if (heightDiff != 0 && interestAmount / heightDiff != totalInterest) {
LogPrintf("WARNING: Overflow detected. This will soon be saturated. (height=%d, amount=%s, interest=%s)\n",
heightDiff, GetInterestPerBlockHighPrecisionString(rate.interestPerBlock),
GetInterestPerBlockHighPrecisionString({rate.interestPerBlock.negative, totalInterest}));
}

auto interest = InterestAddition(rate.interestToHeight, {rate.interestPerBlock.negative, totalInterest});

LogPrint(BCLog::LOAN, "%s(): CInterestRate{.height=%d, .perBlock=%d, .toHeight=%d}, height %d - totalInterest %d\n",
__func__,
rate.height, rate.interestPerBlock.negative ? -InterestPerBlock(rate, height) : InterestPerBlock(rate, height),
rate.interestToHeight.negative ? -CeilInterest(rate.interestToHeight.amount, height) : CeilInterest(rate.interestToHeight.amount, height),
rate.interestToHeight.negative ? -FloorInterest(rate.interestToHeight.amount) : CeilInterest(rate.interestToHeight.amount, height),
height,
interest.negative ? -CeilInterest(interest.amount, height) : CeilInterest(interest.amount, height));
interest.negative ? -FloorInterest(interest.amount) : CeilInterest(interest.amount, height));

return interest;
}
Expand Down