Skip to content

Commit

Permalink
[FOLD] Get rid of throws and unsafe function
Browse files Browse the repository at this point in the history
  • Loading branch information
ximinez committed Oct 21, 2019
1 parent 1142d89 commit 31281cb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 37 deletions.
30 changes: 11 additions & 19 deletions src/ripple/app/misc/FeeVoteImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ FeeVoteImpl::doVoting(
}

// choose our positions
auto const baseFee = baseFeeVote.getVotes ();
auto const baseReserve = baseReserveVote.getVotes ();
auto const incReserve = incReserveVote.getVotes ();
// If any of the values are invalid, send the current values.
auto const baseFee = baseFeeVote.getVotes ().dropsAs<std::uint64_t>(
lastClosedLedger->fees().base);
auto const baseReserve = baseReserveVote.getVotes ().dropsAs<std::uint32_t>(
lastClosedLedger->fees().accountReserve(0));
auto const incReserve = incReserveVote.getVotes ().dropsAs<std::uint32_t>(
lastClosedLedger->fees().increment);
constexpr FeeUnit32 feeUnits = Setup::reference_fee_units;
auto const seq = lastClosedLedger->info().seq + 1;

Expand All @@ -229,21 +233,9 @@ FeeVoteImpl::doVoting(
{
obj[sfAccount] = AccountID();
obj[sfLedgerSequence] = seq;
// These throws are likely to terminate the app if any fire.
// They should not be possible to hit, though, because the voting
// process (FeeVoteImpl) ignores any out of range values.
if(auto const v = baseFee.dropsAs<std::uint64_t>())
obj[sfBaseFee] = *v;
else
Throw<std::runtime_error>("XRPAmount conversion out of range");
if(auto const v = baseReserve.dropsAs<std::uint32_t>())
obj[sfReserveBase] = *v;
else
Throw<std::runtime_error>("XRPAmount conversion out of range");
if(auto const v = incReserve.dropsAs<std::uint32_t>())
obj[sfReserveIncrement] = *v;
else
Throw<std::runtime_error>("XRPAmount conversion out of range");
obj[sfBaseFee] = baseFee;
obj[sfReserveBase] = baseReserve;
obj[sfReserveIncrement] = incReserve;
obj[sfReferenceFeeUnits] = feeUnits.fee();
});

Expand Down Expand Up @@ -274,7 +266,7 @@ setup_FeeVote (Section const& section)
{
std::uint64_t temp;
if (set(temp, "reference_fee", section) &&
isLegalAmount(temp))
temp <= std::numeric_limits<XRPAmount::value_type>::max())
setup.reference_fee = temp;
}
{
Expand Down
16 changes: 15 additions & 1 deletion src/ripple/basics/XRPAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,27 @@ class XRPAmount
if ((drops_ > std::numeric_limits<Dest>::max()) ||
(!std::numeric_limits<Dest>::is_signed && drops_ < 0) ||
(std::numeric_limits<Dest>::is_signed &&
drops_ < std::numeric_limits<Dest>::lowest()))
drops_ < std::numeric_limits<Dest>::lowest()))
{
return boost::none;
}
return static_cast<Dest>(drops_);
}

template <class Dest>
Dest
dropsAs(Dest defaultValue) const
{
return dropsAs<Dest>().value_or(defaultValue);
}

template <class Dest>
Dest
dropsAs(XRPAmount defaultValue) const
{
return dropsAs<Dest>().value_or(defaultValue.drops());
}

Json::Value
jsonClipped() const
{
Expand Down
9 changes: 0 additions & 9 deletions src/ripple/protocol/SystemParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ systemName ()
/** Configure the native currency. */

/** Number of drops in the genesis account. */
static
constexpr
XRPAmount
INITIAL_XRP{ 100'000'000'000 * DROPS_PER_XRP };
Expand All @@ -52,14 +51,6 @@ bool isLegalAmount (XRPAmount const& amount)
return amount <= INITIAL_XRP;
}

inline
bool isLegalAmount (std::uint64_t amount)
{
auto const initial = INITIAL_XRP.dropsAs<std::uint64_t>();
assert(initial);
return initial && amount <= *initial;
}

/* The currency code for the native currency. */
static inline
std::string const&
Expand Down
10 changes: 2 additions & 8 deletions src/ripple/protocol/impl/STValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,25 @@ STValidation::STValidation(
if (fees.loadFee)
setFieldU32(sfLoadFee, *fees.loadFee);

// These throws are likely to terminate the app if any fire.
// They should not be possible to hit, though, because the voting
// IF any of the values are out of the valid range, don't send a value.
// They should not be an issue, though, because the voting
// process (FeeVoteImpl) ignores any out of range values.
if (fees.baseFee)
{
if (auto const v = fees.baseFee->dropsAs<std::uint64_t>())
setFieldU64(sfBaseFee, *v);
else
Throw<std::runtime_error>("XRPAmount conversion out of range");
}

if (fees.reserveBase)
{
if (auto const v = fees.reserveBase->dropsAs<std::uint32_t>())
setFieldU32(sfReserveBase, *v);
else
Throw<std::runtime_error>("XRPAmount conversion out of range");
}

if (fees.reserveIncrement)
{
if (auto const v = fees.reserveIncrement->dropsAs<std::uint32_t>())
setFieldU32(sfReserveIncrement, *v);
else
Throw<std::runtime_error>("XRPAmount conversion out of range");
}

if (!amendments.empty())
Expand Down

0 comments on commit 31281cb

Please sign in to comment.