Skip to content

Commit

Permalink
Added bounding for fee percent,
Browse files Browse the repository at this point in the history
Solved range issues.
  • Loading branch information
Sebastian Rusu committed Apr 6, 2020
1 parent 438ad41 commit 2b6d66d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/stake/stakepoolfee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@
#include "validation.h"

#include <cmath>
#include <algorithm>

bool IsValidPoolFeePercent(double feePercent)
{
double normalized = floor(feePercent * 100.0);
double multiplied = feePercent * 100.0;

if (multiplied > 10000.0)
return false;
if (multiplied < 1.0)
return false;

double normalized = floor(multiplied);

return normalized >= 1.0 && normalized <= 10000.0;
}

Expand All @@ -26,6 +35,7 @@ CAmount StakePoolTicketFee(CAmount stakeDiff, CAmount relayFee, int64_t height,
// percentage, thus making the entirety
// be a multiplication by 10000.
uint64_t poolfeePercentInt = static_cast<uint64_t>(floor(poolFeePercent * 100.0));
poolfeePercentInt = std::min(std::max(poolfeePercentInt, 1ULL), 10000ULL);

// Calculate voter subsidy
CAmount subsidy = GetVoterSubsidy(static_cast<int>(height), Params().GetConsensus());
Expand Down
4 changes: 2 additions & 2 deletions src/stake/staketx.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ struct TicketContribData {
if (voteFees == MaxFees)
return MAX_MONEY;

return 1 << voteFees;
return std::min(1LL << voteFees, MAX_MONEY);
}

CAmount revokeFeeLimits()
Expand All @@ -132,7 +132,7 @@ struct TicketContribData {
if (revokeFees == MaxFees)
return MAX_MONEY;

return 1 << revokeFees;
return std::min(1LL << revokeFees, MAX_MONEY);
}

// convert an absolute amount to a log2 representation.
Expand Down
15 changes: 7 additions & 8 deletions src/test/ticket_purchase_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ BOOST_AUTO_TEST_CASE(stake_pool_fee_percent_test)
{99.99, true},
{100.0, true},
{0.0, false},
{0.01 - std::numeric_limits<double>().epsilon(), false},
{100.0 + std::numeric_limits<double>().epsilon(), false},
{100.000001, false},
{1000.0, false}
};
Expand Down Expand Up @@ -119,6 +117,7 @@ BOOST_AUTO_TEST_CASE(stake_pool_ticket_fee_test)

// test with good data

// TODO: Update these if still failing after finalizing all the implementations in the voter subsidy
std::vector<TestData> goodTests {
{10 * COIN, static_cast<CAmount>(0.01 * COIN), 25000, 1.00, static_cast<CAmount>(0.01500463 * COIN)},
{20 * COIN, static_cast<CAmount>(0.01 * COIN), 25000, 1.00, static_cast<CAmount>(0.01621221 * COIN)},
Expand Down Expand Up @@ -187,22 +186,22 @@ BOOST_AUTO_TEST_CASE(ticket_vote_or_revoke_fee_limits)
if (test.voteFees <= TicketContribData::MaxFees) {
BOOST_CHECK_EQUAL(tcd.voteFeeLimits(), test.expectedVoteFeeLimits);
BOOST_CHECK_NO_THROW(tcd.voteFeeLimits());

if (test.expectedVoteFeeLimits < MAX_MONEY)
BOOST_CHECK_EQUAL(TicketContribData::feeFromAmount(test.expectedVoteFeeLimits), test.voteFees);
} else {
BOOST_CHECK_THROW(tcd.voteFeeLimits(), std::range_error);
}

if (test.revokeFees <= TicketContribData::MaxFees) {
BOOST_CHECK_EQUAL(tcd.revokeFeeLimits(), test.expectedRevokeFeeLimits);
BOOST_CHECK_NO_THROW(tcd.revokeFeeLimits());

if (test.expectedRevokeFeeLimits < MAX_MONEY)
BOOST_CHECK_EQUAL(TicketContribData::feeFromAmount(test.expectedRevokeFeeLimits), test.revokeFees);
} else {
BOOST_CHECK_THROW(tcd.revokeFeeLimits(), std::range_error);
}

if (test.expectedVoteFeeLimits < MAX_MONEY)
BOOST_CHECK_EQUAL(TicketContribData::feeFromAmount(test.expectedVoteFeeLimits), test.voteFees);

if (test.expectedRevokeFeeLimits < MAX_MONEY)
BOOST_CHECK_EQUAL(TicketContribData::feeFromAmount(test.expectedRevokeFeeLimits), test.revokeFees);
}

BOOST_CHECK_EQUAL(TicketContribData::feeFromAmount(0), 0);
Expand Down

0 comments on commit 2b6d66d

Please sign in to comment.