Use payment flow code for offer crossing (RIPD-1094):

Replace Taker.cpp with calls to the payment flow() code.

This change required a number of tweaks in the payment flow code.
These tweaks are conditionalized on whether or not offer crossing
is taking place.  The flag is explicitly passed as a parameter to
the flow code.

For testing, a class was added that identifies differences in the
contents of two PaymentSandboxes.  That code may be reusable in
the future.

None of the Taker offer crossing code is removed.  Both versions
of the code are co-resident to support an amendment cut-over.

The code that identifies differences between Taker and Flow offer
crossing is enabled by a feature.  That makes it easy to enable
or disable difference logging by changing the config file.  This
approach models what was done with the payment flow code.  The
differencing code should never be enabled on a production server.

Extensive offer crossing unit tests are added to examine and
verify the behavior of corner cases.  The tests are currently
configured to run against both Taker and Flow offer crossing.
This gives us confidence that most cases run identically and
some of the (few) differences in behavior are documented.
This commit is contained in:
Scott Schurr
2016-03-15 17:17:09 -07:00
parent 2cd55ebf98
commit 369909df84
50 changed files with 5367 additions and 711 deletions

View File

@@ -176,59 +176,7 @@ accountHolds (ReadView const& view,
STAmount amount;
if (isXRP(currency))
{
// XRP: return balance minus reserve
if (fix1141 (view.info ().parentCloseTime))
{
auto const sle = view.read(
keylet::account(account));
auto const ownerCount =
view.ownerCountHook (account, sle->getFieldU32 (sfOwnerCount));
auto const reserve =
view.fees().accountReserve(ownerCount);
auto const fullBalance =
sle->getFieldAmount(sfBalance);
auto const balance = view.balanceHook(
account, issuer, fullBalance).xrp();
if (balance < reserve)
amount.clear ();
else
amount = balance - reserve;
JLOG (j.trace()) << "accountHolds:" <<
" account=" << to_string (account) <<
" amount=" << amount.getFullText () <<
" fullBalance=" << to_string (fullBalance.xrp()) <<
" balance=" << to_string (balance) <<
" reserve=" << to_string (reserve);
return amount;
}
else
{
// pre-switchover
// XRP: return balance minus reserve
auto const sle = view.read(
keylet::account(account));
auto const reserve =
view.fees().accountReserve(
sle->getFieldU32(sfOwnerCount));
auto const balance =
sle->getFieldAmount(sfBalance).xrp ();
if (balance < reserve)
amount.clear ();
else
amount = balance - reserve;
JLOG (j.trace()) << "accountHolds:" <<
" account=" << to_string (account) <<
" amount=" << amount.getFullText () <<
" balance=" << to_string (balance) <<
" reserve=" << to_string (reserve);
return view.balanceHook(
account, issuer, amount);
}
return {xrpLiquid (view, account, 0, j)};
}
// IOU: Return balance on trust line modulo freeze
@@ -290,6 +238,115 @@ accountFunds (ReadView const& view, AccountID const& id,
return saFunds;
}
// Prevent ownerCount from wrapping under error conditions.
//
// adjustment allows the ownerCount to be adjusted up or down in multiple steps.
// If id != boost.none, then do error reporting.
//
// Returns adjusted owner count.
static
std::uint32_t
confineOwnerCount (std::uint32_t current, std::int32_t adjustment,
boost::optional<AccountID> const& id = boost::none,
beast::Journal j = beast::Journal{})
{
std::uint32_t adjusted {current + adjustment};
if (adjustment > 0)
{
// Overflow is well defined on unsigned
if (adjusted < current)
{
if (id)
{
JLOG (j.fatal()) <<
"Account " << *id <<
" owner count exceeds max!";
}
adjusted = std::numeric_limits<std::uint32_t>::max ();
}
}
else
{
// Underflow is well defined on unsigned
if (adjusted > current)
{
if (id)
{
JLOG (j.fatal()) <<
"Account " << *id <<
" owner count set below 0!";
}
adjusted = 0;
assert(!id);
}
}
return adjusted;
}
XRPAmount
xrpLiquid (ReadView const& view, AccountID const& id,
std::int32_t ownerCountAdj, beast::Journal j)
{
auto const sle = view.read(keylet::account(id));
if (sle == nullptr)
return zero;
// Return balance minus reserve
if (fix1141 (view.info ().parentCloseTime))
{
std::uint32_t const ownerCount = confineOwnerCount (
view.ownerCountHook (id, sle->getFieldU32 (sfOwnerCount)),
ownerCountAdj);
auto const reserve =
view.fees().accountReserve(ownerCount);
auto const fullBalance =
sle->getFieldAmount(sfBalance);
auto const balance = view.balanceHook(id, xrpAccount(), fullBalance);
STAmount amount = balance - reserve;
if (balance < reserve)
amount.clear ();
JLOG (j.trace()) << "accountHolds:" <<
" account=" << to_string (id) <<
" amount=" << amount.getFullText() <<
" fullBalance=" << fullBalance.getFullText() <<
" balance=" << balance.getFullText() <<
" reserve=" << to_string (reserve) <<
" ownerCount=" << to_string (ownerCount) <<
" ownerCountAdj=" << to_string (ownerCountAdj);
return amount.xrp();
}
else
{
// pre-switchover
// XRP: return balance minus reserve
std::uint32_t const ownerCount =
confineOwnerCount (sle->getFieldU32 (sfOwnerCount), ownerCountAdj);
auto const reserve =
view.fees().accountReserve(sle->getFieldU32(sfOwnerCount));
auto const balance = sle->getFieldAmount(sfBalance);
STAmount amount = balance - reserve;
if (balance < reserve)
amount.clear ();
JLOG (j.trace()) << "accountHolds:" <<
" account=" << to_string (id) <<
" amount=" << amount.getFullText() <<
" balance=" << balance.getFullText() <<
" reserve=" << to_string (reserve) <<
" ownerCount=" << to_string (ownerCount) <<
" ownerCountAdj=" << to_string (ownerCountAdj);
return view.balanceHook(id, xrpAccount(), amount).xrp();
}
}
void
forEachItem (ReadView const& view, AccountID const& id,
std::function<void(std::shared_ptr<SLE const> const&)> f)
@@ -678,37 +735,12 @@ hashOfSeq (ReadView const& ledger, LedgerIndex seq,
void
adjustOwnerCount (ApplyView& view,
std::shared_ptr<SLE> const& sle,
int amount, beast::Journal j)
std::int32_t amount, beast::Journal j)
{
assert(amount != 0);
auto const current =
sle->getFieldU32 (sfOwnerCount);
auto adjusted = current + amount;
std::uint32_t const current {sle->getFieldU32 (sfOwnerCount)};
AccountID const id = (*sle)[sfAccount];
if (amount > 0)
{
// Overflow is well defined on unsigned
if (adjusted < current)
{
JLOG (j.fatal()) <<
"Account " << id <<
" owner count exceeds max!";
adjusted =
std::numeric_limits<std::uint32_t>::max ();
}
}
else
{
// Underflow is well defined on unsigned
if (adjusted > current)
{
JLOG (j.fatal()) <<
"Account " << id <<
" owner count set below 0!";
adjusted = 0;
assert(false);
}
}
std::uint32_t const adjusted = confineOwnerCount (current, amount, id, j);
view.adjustOwnerCountHook (id, current, adjusted);
sle->setFieldU32 (sfOwnerCount, adjusted);
view.update(sle);
@@ -1257,7 +1289,7 @@ offerDelete (ApplyView& view,
// Direct send w/o fees:
// - Redeeming IOUs and/or sending sender's own IOUs.
// - Create trust line of needed.
// - Create trust line if needed.
// --> bCheckIssuer : normally require issuer to be involved.
TER
rippleCredit (ApplyView& view,