From dccc28391e58f82125dcd274478d7a40a2ac17ba Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Tue, 30 Aug 2022 12:06:34 +0100 Subject: [PATCH 1/3] 0 interest is not negative --- src/masternodes/loan.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/masternodes/loan.h b/src/masternodes/loan.h index 5e79795d00..ff48ebea9a 100644 --- a/src/masternodes/loan.h +++ b/src/masternodes/loan.h @@ -336,7 +336,10 @@ inline auto InterestAddition = [](const CInterestAmount &a, const CInterestAmoun interest.amount = a.amount + b.amount; interest.negative = b.negative; } else { - if (a.amount > b.amount) { + if (a.amount == b.amount) { + interest.amount = 0; + interest.negative = false; + } else if (a.amount > b.amount) { interest.amount = a.amount - b.amount; interest.negative = a.negative; } else { From 23f2ea427afd08468dcb53076343d2de3e04f753 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Tue, 30 Aug 2022 13:56:34 +0100 Subject: [PATCH 2/3] Make negative 0 change easier to read --- src/masternodes/loan.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/masternodes/loan.h b/src/masternodes/loan.h index ff48ebea9a..b538621517 100644 --- a/src/masternodes/loan.h +++ b/src/masternodes/loan.h @@ -336,10 +336,7 @@ inline auto InterestAddition = [](const CInterestAmount &a, const CInterestAmoun interest.amount = a.amount + b.amount; interest.negative = b.negative; } else { - if (a.amount == b.amount) { - interest.amount = 0; - interest.negative = false; - } else if (a.amount > b.amount) { + if (a.amount > b.amount) { interest.amount = a.amount - b.amount; interest.negative = a.negative; } else { @@ -347,6 +344,9 @@ inline auto InterestAddition = [](const CInterestAmount &a, const CInterestAmoun interest.negative = !a.negative; } } + if (interest.negative && interest.amount == 0) { + interest.negative = false; + } return interest; }; From 367920621e02a80c2854a228b1b15f8f6b53628c Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Tue, 30 Aug 2022 14:20:25 +0100 Subject: [PATCH 3/3] Test for non-negative zero --- src/test/loan_tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/loan_tests.cpp b/src/test/loan_tests.cpp index a459788eef..3190a3f7dc 100644 --- a/src/test/loan_tests.cpp +++ b/src/test/loan_tests.cpp @@ -366,9 +366,9 @@ BOOST_AUTO_TEST_CASE(loan_total_interest_calculation) rate = mnview.GetInterestRate(vault_id, token_id, 11); totalInterest = TotalInterestCalculation(*rate, 11); BOOST_CHECK_EQUAL(rate->interestPerBlock.negative, true); - BOOST_CHECK_EQUAL(rate->interestToHeight.negative, true); + BOOST_CHECK_EQUAL(rate->interestToHeight.negative, false); BOOST_CHECK_EQUAL(rate->interestToHeight.amount.GetLow64(), 0); - BOOST_CHECK_EQUAL(totalInterest.negative, true); + BOOST_CHECK_EQUAL(totalInterest.negative, false); BOOST_CHECK_EQUAL(totalInterest.amount.GetLow64(), 0); BOOST_REQUIRE(mnview.IncreaseInterest(15, vault_id, scheme_id, token_id, tokenInterest, 0));