From 71bffd7e6d3404ed1186c7446d957fcbd9dfaf5b Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 30 Aug 2022 21:00:38 +0800 Subject: [PATCH 1/4] Add overflow check --- src/masternodes/loan.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/masternodes/loan.cpp b/src/masternodes/loan.cpp index 7d6b30e88b..a16249a589 100644 --- a/src/masternodes/loan.cpp +++ b/src/masternodes/loan.cpp @@ -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 be soon be saturated. (height=%d, amount=%s, interest=%s)", + 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; } From f141747f08d364d7e1d911fcc27411c34ba66812 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 30 Aug 2022 21:03:20 +0800 Subject: [PATCH 2/4] Update loan.cpp --- src/masternodes/loan.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/loan.cpp b/src/masternodes/loan.cpp index a16249a589..ec79ef97b7 100644 --- a/src/masternodes/loan.cpp +++ b/src/masternodes/loan.cpp @@ -266,7 +266,7 @@ CInterestAmount TotalInterestCalculation(const CInterestRateV3& rate, const uint if (heightDiff != 0 && interestAmount / heightDiff != totalInterest) { LogPrintf("WARNING: Overflow detected. This will be soon be saturated. (height=%d, amount=%s, interest=%s)", - heightDiff, GetInterestPerBlockHighPrecisionString(rate.interestPerBlock), + heightDiff, GetInterestPerBlockHighPrecisionString(rate.interestPerBlock)\n, GetInterestPerBlockHighPrecisionString({rate.interestPerBlock.negative, totalInterest})); } From 2716d192b8e905048053c9e7c9ca500d9fdb0d57 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 30 Aug 2022 21:03:41 +0800 Subject: [PATCH 3/4] Update loan.cpp --- src/masternodes/loan.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/masternodes/loan.cpp b/src/masternodes/loan.cpp index ec79ef97b7..f678a87fad 100644 --- a/src/masternodes/loan.cpp +++ b/src/masternodes/loan.cpp @@ -265,8 +265,8 @@ CInterestAmount TotalInterestCalculation(const CInterestRateV3& rate, const uint const auto totalInterest = heightDiff * interestAmount; if (heightDiff != 0 && interestAmount / heightDiff != totalInterest) { - LogPrintf("WARNING: Overflow detected. This will be soon be saturated. (height=%d, amount=%s, interest=%s)", - heightDiff, GetInterestPerBlockHighPrecisionString(rate.interestPerBlock)\n, + LogPrintf("WARNING: Overflow detected. This will be soon be saturated. (height=%d, amount=%s, interest=%s)\n", + heightDiff, GetInterestPerBlockHighPrecisionString(rate.interestPerBlock), GetInterestPerBlockHighPrecisionString({rate.interestPerBlock.negative, totalInterest})); } From e03f2ea70f852ce39cb0359440563707b987ca45 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 30 Aug 2022 21:06:32 +0800 Subject: [PATCH 4/4] Update loan.cpp --- src/masternodes/loan.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masternodes/loan.cpp b/src/masternodes/loan.cpp index f678a87fad..655c34346c 100644 --- a/src/masternodes/loan.cpp +++ b/src/masternodes/loan.cpp @@ -265,7 +265,7 @@ CInterestAmount TotalInterestCalculation(const CInterestRateV3& rate, const uint const auto totalInterest = heightDiff * interestAmount; if (heightDiff != 0 && interestAmount / heightDiff != totalInterest) { - LogPrintf("WARNING: Overflow detected. This will be soon be saturated. (height=%d, amount=%s, interest=%s)\n", + 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})); }