Reduce STAmount public interface (RIPD-867):

* Implement subtraction as addition to the additive inverse
* Do not allow comparison with, addition to or subtraction from integers
* Remove unused functions
* Convert member functions to free functions
* Isolate unit-test specific code into the unit test
This commit is contained in:
Nik Bougalis
2015-05-17 10:29:47 -07:00
parent 67b18e4bea
commit 6f5d8bba2d
19 changed files with 351 additions and 541 deletions

View File

@@ -430,7 +430,7 @@ private:
{
std::string txt = amount.getText ();
txt += "/";
txt += amount.getHumanCurrency ();
txt += to_string (amount.issue().currency);
return txt;
}
@@ -451,11 +451,11 @@ public:
}
/** Returns the reserve the account would have if an offer was added. */
std::uint32_t
STAmount
getAccountReserve (SLE::pointer account)
{
return mEngine->getLedger ()->getReserve (
account->getFieldU32 (sfOwnerCount) + 1);
return STAmount (mEngine->getLedger ()->getReserve (
account->getFieldU32 (sfOwnerCount) + 1));
}
TER
@@ -504,7 +504,7 @@ public:
if (!isLegalNet (saTakerPays) || !isLegalNet (saTakerGets))
return temBAD_AMOUNT;
if (saTakerPays.isNative () && saTakerGets.isNative ())
if (saTakerPays.native () && saTakerGets.native ())
{
if (m_journal.debug) m_journal.warning <<
"Malformed offer: XRP for XRP";
@@ -537,8 +537,8 @@ public:
return temBAD_CURRENCY;
}
if (saTakerPays.isNative () != !uPaysIssuerID ||
saTakerGets.isNative () != !uGetsIssuerID)
if (saTakerPays.native () != !uPaysIssuerID ||
saTakerGets.native () != !uGetsIssuerID)
{
if (m_journal.warning) m_journal.warning <<
"Malformed offer: bad issuer";
@@ -660,7 +660,7 @@ public:
}
// Make sure that we are authorized to hold what the taker will pay us.
if (result == tesSUCCESS && !saTakerPays.isNative ())
if (result == tesSUCCESS && !saTakerPays.native ())
result = checkAcceptAsset (Issue (uPaysCurrency, uPaysIssuerID));
bool const bOpenLedger (mParams & tapOPEN_LEDGER);
@@ -858,8 +858,8 @@ transact_CreateOffer (
{
CrossType cross_type = CrossType::IouToIou;
bool const pays_xrp = txn.getFieldAmount (sfTakerPays).isNative ();
bool const gets_xrp = txn.getFieldAmount (sfTakerGets).isNative ();
bool const pays_xrp = txn.getFieldAmount (sfTakerPays).native ();
bool const gets_xrp = txn.getFieldAmount (sfTakerGets).native ();
if (pays_xrp && !gets_xrp)
cross_type = CrossType::IouToXrp;

View File

@@ -57,17 +57,22 @@ public:
return Transactor::preCheck ();
}
/** Returns the reserve the account would have if an offer was added. */
STAmount
getAccountReserve (SLE::pointer account)
{
return STAmount (mEngine->getLedger ()->getReserve (
account->getFieldU32 (sfOwnerCount) + 1));
}
TER doApply () override
{
assert (mTxnAccount);
// A ticket counts against the reserve of the issuing account, but we check
// the starting balance because we want to allow dipping into the reserve to
// pay fees.
auto const accountReserve (mEngine->getLedger ()->getReserve (
mTxnAccount->getFieldU32 (sfOwnerCount) + 1));
if (mPriorBalance < accountReserve)
// A ticket counts against the reserve of the issuing account, but we
// check the starting balance because we want to allow dipping into the
// reserve to pay fees.
if (mPriorBalance < getAccountReserve (mTxnAccount))
return tecINSUFFICIENT_RESERVE;
std::uint32_t expiration (0);

View File

@@ -73,7 +73,7 @@ public:
if (bMax)
maxSourceAmount = mTxn.getFieldAmount (sfSendMax);
else if (saDstAmount.isNative ())
else if (saDstAmount.native ())
maxSourceAmount = saDstAmount;
else
maxSourceAmount = STAmount (
@@ -178,7 +178,7 @@ public:
STAmount maxSourceAmount;
if (bMax)
maxSourceAmount = mTxn.getFieldAmount (sfSendMax);
else if (saDstAmount.isNative ())
else if (saDstAmount.native ())
maxSourceAmount = saDstAmount;
else
maxSourceAmount = STAmount (
@@ -197,7 +197,7 @@ public:
if (!sleDst)
{
// Destination account does not exist.
if (!saDstAmount.isNative ())
if (!saDstAmount.native ())
{
m_journal.trace <<
"Delay transaction: Destination account does not exist.";
@@ -218,7 +218,7 @@ public:
// transaction would succeed.
return telNO_DST_PARTIAL;
}
else if (saDstAmount < mEngine->getLedger ()->getReserve (0))
else if (saDstAmount < STAmount (mEngine->getLedger ()->getReserve (0)))
{
// getReserve() is the minimum amount that an account can have.
// Reserve is not scaled by load.
@@ -260,7 +260,7 @@ public:
TER terResult;
bool const bRipple = bPaths || bMax || !saDstAmount.isNative ();
bool const bRipple = bPaths || bMax || !saDstAmount.native ();
// XXX Should bMax be sufficient to imply ripple?
if (bRipple)
@@ -331,37 +331,39 @@ public:
{
// Direct XRP payment.
// uOwnerCount is the number of entries in this legder for this account
// that require a reserve.
std::uint32_t const uOwnerCount (mTxnAccount->getFieldU32 (sfOwnerCount));
// uOwnerCount is the number of entries in this legder for this
// account that require a reserve.
auto const uOwnerCount = mTxnAccount->getFieldU32 (sfOwnerCount);
// This is the total reserve in drops.
// TODO(tom): there should be a class for this.
std::uint64_t const uReserve (mEngine->getLedger ()->getReserve (uOwnerCount));
std::uint64_t const uReserve =
mEngine->getLedger ()->getReserve (uOwnerCount);
// mPriorBalance is the balance on the sending account BEFORE the
// fees were charged. We want to make sure we have enough reserve
// to send. Allow final spend to use reserve for fee.
auto const mmm = std::max(mTxn.getTransactionFee (),
STAmount (uReserve));
// mPriorBalance is the balance on the sending account BEFORE the fees were charged.
//
// Make sure have enough reserve to send. Allow final spend to use
// reserve for fee.
auto const mmm = std::max(uReserve, getNValue (mTxn.getTransactionFee ()));
if (mPriorBalance < saDstAmount + mmm)
{
// Vote no.
// However, transaction might succeed, if applied in a different order.
// Vote no. However the transaction might succeed, if applied in
// a different order.
m_journal.trace << "Delay transaction: Insufficient funds: " <<
" " << mPriorBalance.getText () <<
" / " << (saDstAmount + uReserve).getText () <<
" / " << (saDstAmount + mmm).getText () <<
" (" << uReserve << ")";
terResult = tecUNFUNDED_PAYMENT;
}
else
{
// The source account does have enough money, so do the arithmetic
// for the transfer and make the ledger change.
mTxnAccount->setFieldAmount (sfBalance, mSourceBalance - saDstAmount);
sleDst->setFieldAmount (sfBalance, sleDst->getFieldAmount (sfBalance) + saDstAmount);
// The source account does have enough money, so do the
// arithmetic for the transfer and make the ledger change.
mTxnAccount->setFieldAmount (sfBalance,
mSourceBalance - saDstAmount);
sleDst->setFieldAmount (sfBalance,
sleDst->getFieldAmount (sfBalance) + saDstAmount);
// Re-arm the password change fee if we can and need to.
if ((sleDst->getFlags () & lsfPasswordSpent))

View File

@@ -58,7 +58,7 @@ public:
if (!isLegalNet (saLimitAmount))
return temBAD_AMOUNT;
if (saLimitAmount.isNative ())
if (saLimitAmount.native ())
{
if (m_journal.trace) m_journal.trace <<
"Malformed transaction: specifies native limit " <<
@@ -121,9 +121,9 @@ public:
// to fund accounts in a way where there's no incentive to trick them
// into creating an account you have no intention of using.
std::uint64_t const uReserveCreate = (uOwnerCount < 2)
STAmount const reserveCreate ((uOwnerCount < 2)
? 0
: mEngine->getLedger ()->getReserve (uOwnerCount + 1);
: mEngine->getLedger ()->getReserve (uOwnerCount + 1));
std::uint32_t uQualityIn (bQualityIn ? mTxn.getFieldU32 (sfQualityIn) : 0);
std::uint32_t uQualityOut (bQualityOut ? mTxn.getFieldU32 (sfQualityOut) : 0);
@@ -380,7 +380,7 @@ public:
terResult = mEngine->view ().trustDelete (sleRippleState, uLowAccountID, uHighAccountID);
}
// Reserve is not scaled by load.
else if (bReserveIncrease && mPriorBalance < uReserveCreate)
else if (bReserveIncrease && mPriorBalance < reserveCreate)
{
m_journal.trace <<
"Delay transaction: Insufficent reserve to add trust line.";
@@ -405,7 +405,7 @@ public:
"Redundant: Setting non-existent ripple line to defaults.";
return tecNO_LINE_REDUNDANT;
}
else if (mPriorBalance < uReserveCreate) // Reserve is not scaled by load.
else if (mPriorBalance < reserveCreate) // Reserve is not scaled by load.
{
m_journal.trace <<
"Delay transaction: Line does not exist. Insufficent reserve to create line.";

View File

@@ -360,10 +360,10 @@ BasicTaker::do_cross (
{
assert (!done ());
assert (!offer1.in.isNative ());
assert (offer1.out.isNative ());
assert (offer2.in.isNative ());
assert (!offer2.out.isNative ());
assert (!offer1.in.native ());
assert (offer1.out.native ());
assert (offer2.in.native ());
assert (!offer2.out.native ());
// If the taker owns the first leg of the offer, then the taker's available
// funds aren't the limiting factor for the input - the offer itself is.

View File

@@ -126,7 +126,7 @@ TER Transactor::payFee ()
return telINSUF_FEE_P;
}
if (saPaid < zero || !saPaid.isNative ())
if (saPaid < zero || !saPaid.native ())
return temBAD_FEE;
if (!saPaid)
@@ -307,13 +307,11 @@ TER Transactor::checkSign ()
{
#if RIPPLE_ENABLE_MULTI_SIGN
// If the mSigningPubKey is empty, then we must be multi-signing.
TER const signingTER = mSigningPubKey.getAccountPublic ().empty () ?
checkMultiSign () : checkSingleSign ();
#else
TER const signingTER = checkSingleSign ();
#endif // RIPPLE_ENABLE_MULTI_SIGN
if (mSigningPubKey.getAccountPublic ().empty ())
return checkMultiSign ();
#endif
return signingTER;
return checkSingleSign ();
}
TER Transactor::checkSingleSign ()

View File

@@ -128,9 +128,7 @@ private:
STAmount parse_amount (std::string const& amount, Issue const& issue)
{
STAmount result (issue);
expect (result.setValue (amount), amount);
return result;
return amountFromString (issue, amount);
}
Amounts parse_amounts (
@@ -162,7 +160,7 @@ private:
{
std::string txt = amount.getText ();
txt += "/";
txt += amount.getHumanCurrency ();
txt += to_string (amount.issue().currency);
return txt;
}
@@ -212,7 +210,7 @@ private:
Amounts const expected (parse_amounts (
flow.in, issue_in,
flow.out, issue_out));
expect (expected == result, name + (sell ? " (s)" : " (b)"));
if (expected != result)
@@ -233,7 +231,7 @@ private:
public:
// Notation for clamp scenario descriptions:
//
//
// IN:OUT (with the last in the list being limiting factor)
// N = Nothing
// T = Taker Offer Balance

View File

@@ -362,7 +362,7 @@ void payInDrops (TestLedger& ledger,
std::uint64_t getNativeBalance(TestLedger& ledger, UserAccount& acct)
{
return getNValue(ledger.getAccountState(acct)->getBalance());
return ledger.getAccountState(acct)->getBalance().mantissa ();
}
std::uint32_t getOwnerCount(TestLedger& ledger, UserAccount& acct)