From 31281cb085234a907f46a2aacbb94aed04f5534f Mon Sep 17 00:00:00 2001 From: Edward Hennis Date: Fri, 18 Oct 2019 18:38:27 -0400 Subject: [PATCH] [FOLD] Get rid of throws and unsafe function --- src/ripple/app/misc/FeeVoteImpl.cpp | 30 +++++++++-------------- src/ripple/basics/XRPAmount.h | 16 +++++++++++- src/ripple/protocol/SystemParameters.h | 9 ------- src/ripple/protocol/impl/STValidation.cpp | 10 ++------ 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/ripple/app/misc/FeeVoteImpl.cpp b/src/ripple/app/misc/FeeVoteImpl.cpp index c8723e21942..c43b840a2d3 100644 --- a/src/ripple/app/misc/FeeVoteImpl.cpp +++ b/src/ripple/app/misc/FeeVoteImpl.cpp @@ -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( + lastClosedLedger->fees().base); + auto const baseReserve = baseReserveVote.getVotes ().dropsAs( + lastClosedLedger->fees().accountReserve(0)); + auto const incReserve = incReserveVote.getVotes ().dropsAs( + lastClosedLedger->fees().increment); constexpr FeeUnit32 feeUnits = Setup::reference_fee_units; auto const seq = lastClosedLedger->info().seq + 1; @@ -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()) - obj[sfBaseFee] = *v; - else - Throw("XRPAmount conversion out of range"); - if(auto const v = baseReserve.dropsAs()) - obj[sfReserveBase] = *v; - else - Throw("XRPAmount conversion out of range"); - if(auto const v = incReserve.dropsAs()) - obj[sfReserveIncrement] = *v; - else - Throw("XRPAmount conversion out of range"); + obj[sfBaseFee] = baseFee; + obj[sfReserveBase] = baseReserve; + obj[sfReserveIncrement] = incReserve; obj[sfReferenceFeeUnits] = feeUnits.fee(); }); @@ -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::max()) setup.reference_fee = temp; } { diff --git a/src/ripple/basics/XRPAmount.h b/src/ripple/basics/XRPAmount.h index 488947c4da9..5063e6f6317 100644 --- a/src/ripple/basics/XRPAmount.h +++ b/src/ripple/basics/XRPAmount.h @@ -197,13 +197,27 @@ class XRPAmount if ((drops_ > std::numeric_limits::max()) || (!std::numeric_limits::is_signed && drops_ < 0) || (std::numeric_limits::is_signed && - drops_ < std::numeric_limits::lowest())) + drops_ < std::numeric_limits::lowest())) { return boost::none; } return static_cast(drops_); } + template + Dest + dropsAs(Dest defaultValue) const + { + return dropsAs().value_or(defaultValue); + } + + template + Dest + dropsAs(XRPAmount defaultValue) const + { + return dropsAs().value_or(defaultValue.drops()); + } + Json::Value jsonClipped() const { diff --git a/src/ripple/protocol/SystemParameters.h b/src/ripple/protocol/SystemParameters.h index 2bbd9ac74b8..ed07733063c 100644 --- a/src/ripple/protocol/SystemParameters.h +++ b/src/ripple/protocol/SystemParameters.h @@ -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 }; @@ -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(); - assert(initial); - return initial && amount <= *initial; -} - /* The currency code for the native currency. */ static inline std::string const& diff --git a/src/ripple/protocol/impl/STValidation.cpp b/src/ripple/protocol/impl/STValidation.cpp index 58d20ca7945..1fd269ea055 100644 --- a/src/ripple/protocol/impl/STValidation.cpp +++ b/src/ripple/protocol/impl/STValidation.cpp @@ -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()) setFieldU64(sfBaseFee, *v); - else - Throw("XRPAmount conversion out of range"); } if (fees.reserveBase) { if (auto const v = fees.reserveBase->dropsAs()) setFieldU32(sfReserveBase, *v); - else - Throw("XRPAmount conversion out of range"); } if (fees.reserveIncrement) { if (auto const v = fees.reserveIncrement->dropsAs()) setFieldU32(sfReserveIncrement, *v); - else - Throw("XRPAmount conversion out of range"); } if (!amendments.empty())