Skip to content

Commit

Permalink
Fix interest per block float calculation
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Jan 31, 2022
1 parent 40b8d7c commit ed5daaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/masternodes/loan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,6 @@ inline base_uint<128> InterestPerBlockCalculationV2(CAmount amount, CAmount toke
return arith_uint256(amount) * netInterest * COIN / blocksPerYear;
}

static base_uint<128> InterestPerBlockCalculation(CAmount amount, CAmount tokenInterest, CAmount schemeInterest, uint32_t height)
{
if (int(height) >= Params().GetConsensus().FortCanningHillHeight)
return InterestPerBlockCalculationV2(amount, tokenInterest, schemeInterest);

if (int(height) >= Params().GetConsensus().FortCanningMuseumHeight)
return std::ceil(InterestPerBlockCalculationV1<float>(amount, tokenInterest, schemeInterest));

return InterestPerBlockCalculationV1<CAmount>(amount, tokenInterest, schemeInterest);
}

CAmount CeilInterest(const base_uint<128>& value, uint32_t height)
{
if (int(height) >= Params().GetConsensus().FortCanningHillHeight) {
Expand Down Expand Up @@ -303,9 +292,15 @@ Res CLoanView::StoreInterest(uint32_t height, const CVaultId& vaultId, const std
if (int(height) >= Params().GetConsensus().FortCanningHillHeight) {
CBalances amounts;
ReadBy<LoanTokenAmount>(vaultId, amounts);
rate.interestPerBlock = InterestPerBlockCalculation(amounts.balances[id], token->interest, scheme->rate, height);
rate.interestPerBlock = InterestPerBlockCalculationV2(amounts.balances[id], token->interest, scheme->rate);

} else if (int(height) >= Params().GetConsensus().FortCanningMuseumHeight) {
CAmount interestPerBlock = rate.interestPerBlock.GetLow64();
interestPerBlock += std::ceil(InterestPerBlockCalculationV1<float>(loanIncreased, token->interest, scheme->rate));
rate.interestPerBlock = interestPerBlock;

} else
rate.interestPerBlock += InterestPerBlockCalculation(loanIncreased, token->interest, scheme->rate, height);
rate.interestPerBlock += InterestPerBlockCalculationV1<CAmount>(loanIncreased, token->interest, scheme->rate);

rate.height = height;

Expand Down Expand Up @@ -345,9 +340,15 @@ Res CLoanView::EraseInterest(uint32_t height, const CVaultId& vaultId, const std
if (int(height) >= Params().GetConsensus().FortCanningHillHeight) {
CBalances amounts;
ReadBy<LoanTokenAmount>(vaultId, amounts);
rate.interestPerBlock = InterestPerBlockCalculation(amounts.balances[id], token->interest, scheme->rate, height);
rate.interestPerBlock = InterestPerBlockCalculationV2(amounts.balances[id], token->interest, scheme->rate);

} else if (int(height) >= Params().GetConsensus().FortCanningMuseumHeight) {
CAmount interestPerBlock = rate.interestPerBlock.GetLow64();
CAmount newInterestPerBlock = std::ceil(InterestPerBlockCalculationV1<float>(loanDecreased, token->interest, scheme->rate));
rate.interestPerBlock = std::max(CAmount{0}, interestPerBlock - newInterestPerBlock);

} else {
auto interestPerBlock = InterestPerBlockCalculation(loanDecreased, token->interest, scheme->rate, height);
auto interestPerBlock = InterestPerBlockCalculationV1<CAmount>(loanDecreased, token->interest, scheme->rate);
rate.interestPerBlock = rate.interestPerBlock < interestPerBlock ? 0
: rate.interestPerBlock - interestPerBlock;
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/amount_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,11 @@ BOOST_AUTO_TEST_CASE(CTokenAmount_Sub_Negative_Result_Test)
BOOST_CHECK_EQUAL(amount.Sub(val).msg, "amount 0.00000010 is less than 0.00000011");
}

BOOST_AUTO_TEST_CASE(CAmount_Float_Test)
{
CAmount amount1 = 16765189, amount2 = 237824;
// https://en.wikipedia.org/wiki/Floating-point_arithmetic
BOOST_CHECK_EQUAL(CAmount(amount1 + float(amount2)), amount1 + amount2 - 1);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit ed5daaa

Please sign in to comment.