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

@@ -42,7 +42,7 @@ private:
}
public:
void
testStepLimit(std::initializer_list<uint256> fs)
{
@@ -76,7 +76,7 @@ public:
balance("bob", USD(0)), owners("bob", 1),
balance("dan", USD(1)), owners("dan", 2)));
}
void
testCrossingLimit(std::initializer_list<uint256> fs)
{
@@ -160,6 +160,7 @@ public:
testAll({});
testAll({featureFlow});
testAll({featureFlow, fix1373});
testAll({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -114,6 +114,8 @@ public:
test_convert_all_of_an_asset({});
test_convert_all_of_an_asset({featureFlow});
test_convert_all_of_an_asset({featureFlow, fix1373});
test_convert_all_of_an_asset(
{featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -147,6 +147,7 @@ public:
testXRPDiscrepancy ({});
testXRPDiscrepancy ({featureFlow});
testXRPDiscrepancy ({featureFlow, fix1373});
testXRPDiscrepancy ({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -467,8 +467,8 @@ struct Flow_test : public beast::unit_test::suite
paths.push_back (p2);
}
return flow (sb, deliver, alice, carol, paths, false, false, true,
boost::none, smax, flowJournal);
return flow (sb, deliver, alice, carol, paths, false, false,
true, false, boost::none, smax, flowJournal);
}();
BEAST_EXPECT(flowResult.removableOffers.size () == 1);
@@ -1199,6 +1199,44 @@ struct Flow_test : public beast::unit_test::suite
env.close();
}
void
testSelfPayLowQualityOffer (std::initializer_list<uint256> fs)
{
// The new payment code used to assert if an offer was made for more
// XRP than the offering account held. This unit test reproduces
// that failing case.
testcase ("Self crossing low quality offer");
using namespace jtx;
Env env(*this, features (fs));
auto const ann = Account("ann");
auto const gw = Account("gateway");
auto const CTB = gw["CTB"];
auto const fee = env.current ()->fees ().base;
env.fund (reserve(env, 2) + drops (9999640) + (fee), ann);
env.fund (reserve(env, 2) + (fee*4), gw);
env.close();
env (rate(gw, 1.002));
env (trust(ann, CTB(10)));
env.close();
env (pay(gw, ann, CTB(2.856)));
env.close();
env (offer(ann, drops(365611702030), CTB(5.713)));
env.close();
// This payment caused the assert.
env (pay(ann, ann, CTB(0.687)),
sendmax (drops(20000000000)), txflags (tfPartialPayment));
}
void run() override
{
testLimitQuality();
@@ -1225,10 +1263,12 @@ struct Flow_test : public beast::unit_test::suite
testUnfundedOffer(true, {fs...});
testUnfundedOffer(false, {fs...});
testReexecuteDirectStep({fix1368, fs...});
testSelfPayLowQualityOffer({fs...});
};
testWithFeats();
testWithFeats(featureFlow);
testWithFeats(featureFlow, fix1373);
testWithFeats(featureFlow, fix1373, featureFlowCross);
}
};

View File

@@ -533,6 +533,7 @@ public:
testAll({});
testAll({featureFlow});
testAll({featureFlow, fix1373});
testAll({featureFlow, fix1373, featureFlowCross});
}
};

File diff suppressed because it is too large Load Diff

View File

@@ -638,14 +638,14 @@ struct PayStrand_test : public beast::unit_test::suite
// Test every combination of element type pairs on a path
void
testAllPairs()
testAllPairs(std::initializer_list<uint256> fs)
{
testcase("All pairs");
using namespace jtx;
using RippleCalc = ::ripple::path::RippleCalc;
ExistingElementPool eep;
Env env(*this, features(fix1373));
Env env(*this, features(fs));
auto const closeTime = fix1298Time() +
100 * env.closed()->info().closeTimeResolution;
@@ -892,9 +892,11 @@ struct PayStrand_test : public beast::unit_test::suite
alice,
bob,
deliver,
boost::none,
sendMaxIssue,
path,
true,
false,
env.app().logs().journal("Flow"));
BEAST_EXPECT(r.first == expTer);
if (sizeof...(expSteps))
@@ -917,9 +919,11 @@ struct PayStrand_test : public beast::unit_test::suite
alice,
alice,
/*deliver*/ xrpIssue(),
/*limitQuality*/ boost::none,
/*sendMaxIssue*/ EUR.issue(),
path,
true,
false,
env.app().logs().journal("Flow"));
BEAST_EXPECT(r.first == tesSUCCESS);
}
@@ -930,9 +934,11 @@ struct PayStrand_test : public beast::unit_test::suite
alice,
alice,
/*deliver*/ xrpIssue(),
/*limitQuality*/ boost::none,
/*sendMaxIssue*/ xrpIssue(),
path,
true,
false,
env.app().logs().journal("Flow"));
BEAST_EXPECT(r.first == tesSUCCESS);
}
@@ -1043,9 +1049,11 @@ struct PayStrand_test : public beast::unit_test::suite
alice,
xrpAccount(),
XRP,
boost::none,
USD.issue(),
STPath(),
true,
false,
flowJournal);
BEAST_EXPECT(r.first == temBAD_PATH);
}
@@ -1057,8 +1065,10 @@ struct PayStrand_test : public beast::unit_test::suite
alice,
XRP,
boost::none,
boost::none,
STPath(),
true,
false,
flowJournal);
BEAST_EXPECT(r.first == temBAD_PATH);
}
@@ -1070,8 +1080,10 @@ struct PayStrand_test : public beast::unit_test::suite
bob,
USD,
boost::none,
boost::none,
STPath(),
true,
false,
flowJournal);
BEAST_EXPECT(r.first == terNO_ACCOUNT);
}
@@ -1204,8 +1216,10 @@ struct PayStrand_test : public beast::unit_test::suite
gw,
USD,
boost::none,
boost::none,
STPath(),
true,
false,
env.app().logs().journal("Flow"));
BEAST_EXPECT(r.first == tesSUCCESS);
BEAST_EXPECT(equal(r.second, D{alice, gw, usdC}));
@@ -1242,9 +1256,11 @@ struct PayStrand_test : public beast::unit_test::suite
alice,
bob,
XRP,
boost::none,
USD.issue(),
path,
false,
false,
env.app().logs().journal("Flow"));
BEAST_EXPECT(r.first == tesSUCCESS);
BEAST_EXPECT(equal(r.second, D{alice, gw, usdC}, B{USD.issue(), xrpIssue()}, XRPS{bob}));
@@ -1402,14 +1418,18 @@ struct PayStrand_test : public beast::unit_test::suite
void
run() override
{
testAllPairs();
testAllPairs({featureFlow, fix1373});
testAllPairs({featureFlow, fix1373, featureFlowCross});
testToStrand({featureFlow});
testToStrand({featureFlow, fix1373});
testToStrand({featureFlow, fix1373, featureFlowCross});
testRIPD1373({});
testRIPD1373({featureFlow, fix1373});
testRIPD1373({featureFlow, fix1373, featureFlowCross});
testLoop({});
testLoop({featureFlow});
testLoop({featureFlow, fix1373});
testLoop({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -78,6 +78,7 @@ struct SetAuth_test : public beast::unit_test::suite
testAuth({});
testAuth({featureFlow});
testAuth({featureFlow, fix1373});
testAuth({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -509,6 +509,7 @@ public:
testWithFeatures({});
testWithFeatures({featureFlow});
testWithFeatures({featureFlow, fix1373});
testWithFeatures({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -83,6 +83,14 @@ features (std::initializer_list<uint256> keys)
return keys;
}
/** Activate features in the Env ctor */
inline
auto
features (std::vector<uint256> keys)
{
return keys;
}
//------------------------------------------------------------------------------
/** A transaction testing environment. */
@@ -143,6 +151,14 @@ private:
app().config().features.insert(key);
}
void
construct_arg (
std::vector<uint256> const& list)
{
for(auto const& key : list)
app().config().features.insert(key);
}
public:
Env() = delete;
Env (Env const&) = delete;

View File

@@ -27,16 +27,28 @@ namespace jtx {
Json::Value
offer (Account const& account,
STAmount const& in, STAmount const& out)
STAmount const& in, STAmount const& out, std::uint32_t flags)
{
Json::Value jv;
jv[jss::Account] = account.human();
jv[jss::TakerPays] = in.getJson(0);
jv[jss::TakerGets] = out.getJson(0);
if (flags)
jv[jss::Flags] = flags;
jv[jss::TransactionType] = "OfferCreate";
return jv;
}
Json::Value
offer_cancel (Account const& account, std::uint32_t offerSeq)
{
Json::Value jv;
jv[jss::Account] = account.human();
jv[jss::OfferSequence] = offerSeq;
jv[jss::TransactionType] = "OfferCancel";
return jv;
}
} // jtx
} // test
} // ripple

View File

@@ -31,7 +31,11 @@ namespace jtx {
/** Create an offer. */
Json::Value
offer (Account const& account,
STAmount const& in, STAmount const& out);
STAmount const& in, STAmount const& out, std::uint32_t flags = 0);
/** Cancel an offer. */
Json::Value
offer_cancel (Account const& account, std::uint32_t offerSeq);
} // jtx
} // test

View File

@@ -42,7 +42,7 @@ public:
operator()(Env&, JTx& jtx) const;
};
/** Sets the QualityIn on a trust JTx as a percentage. */
/** Sets the QualityIn on a trust JTx. */
class qualityInPercent
{
private:

View File

@@ -96,6 +96,7 @@ struct BookDirs_test : public beast::unit_test::suite
{
test_bookdir({});
test_bookdir({featureFlow, fix1373});
test_bookdir({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -0,0 +1,104 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2016 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/ledger/CashDiff.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/beast/unit_test.h>
namespace ripple {
namespace test {
class CashDiff_test : public beast::unit_test::suite
{
// Exercise diffIsDust (STAmount, STAmount)
void
testDust ()
{
testcase ("diffIsDust (STAmount, STAmount)");
Issue const usd (Currency (0x5553440000000000), AccountID (0x4985601));
Issue const usf (Currency (0x5553460000000000), AccountID (0x4985601));
// Positive and negative are never dust.
expect (!diffIsDust (STAmount{usd, 1}, STAmount{usd, -1}));
// Different issues are never dust.
expect (!diffIsDust (STAmount{usd, 1}, STAmount{usf, 1}));
// Native and non-native are never dust.
expect (!diffIsDust (STAmount{usd, 1}, STAmount{1}));
// Equal values are always dust.
expect (diffIsDust (STAmount{0}, STAmount{0}));
{
// Test IOU.
std::uint64_t oldProbe = 0;
std::uint64_t newProbe = 10;
std::uint8_t e10 = 1;
do
{
STAmount large (usd, newProbe + 1);
STAmount small (usd, newProbe);
expect (diffIsDust (large, small, e10));
expect (diffIsDust (large, small, e10+1) == (e10 > 13));
oldProbe = newProbe;
newProbe = oldProbe * 10;
e10 += 1;
} while (newProbe > oldProbe);
}
{
// Test XRP.
// A delta of 2 or less is always dust.
expect (diffIsDust (STAmount{2}, STAmount{0}));
std::uint64_t oldProbe = 0;
std::uint64_t newProbe = 10;
std::uint8_t e10 = 0;
do
{
// Differences of 2 of fewer drops are always treated as dust,
// so use a delta of 3.
STAmount large (newProbe + 3);
STAmount small (newProbe);
expect (diffIsDust (large, small, e10));
expect (diffIsDust (large, small, e10+1) == (e10 >= 20));
oldProbe = newProbe;
newProbe = oldProbe * 10;
e10 += 1;
} while (newProbe > oldProbe);
}
}
public:
void run ()
{
testDust();
}
};
BEAST_DEFINE_TESTSUITE (CashDiff, ledger, ripple);
} // test
} // ripple

View File

@@ -331,7 +331,40 @@ class PaymentSandbox_test : public beast::unit_test::suite
accountSend (sb, alice, xrpAccount (), XRP(100), dj);
BEAST_EXPECT(accountFundsXRP (sb, alice) == beast::zero);
}
}
void testBalanceHook(std::initializer_list<uint256> fs)
{
// Make sure the Issue::Account returned by PAymentSandbox::balanceHook
// is correct.
testcase ("balanceHook");
using namespace jtx;
Env env (*this, features(fs));
Account const gw ("gw");
auto const USD = gw["USD"];
Account const alice ("alice");
auto const closeTime = fix1274Time () +
100 * env.closed ()->info ().closeTimeResolution;
env.close (closeTime);
ApplyViewImpl av (&*env.current (), tapNONE);
PaymentSandbox sb (&av);
// The currency we pass for the last argument mimics the currency that
// is typically passed to creditHook, since it comes from a trust line.
Issue tlIssue = noIssue();
tlIssue.currency = USD.issue().currency;
sb.creditHook (gw.id(), alice.id(), {USD, 400}, {tlIssue, 600});
sb.creditHook (gw.id(), alice.id(), {USD, 100}, {tlIssue, 600});
// Expect that the STAmount issuer returned by balanceHook() is correct.
STAmount const balance =
sb.balanceHook (gw.id(), alice.id(), {USD, 600});
BEAST_EXPECT (balance.getIssuer() == USD.issue().account);
}
public:
@@ -342,9 +375,11 @@ public:
testSubtractCredits(fs);
testTinyBalance(fs);
testReserve(fs);
testBalanceHook(fs);
};
testAll({});
testAll({featureFlow, fix1373});
testAll({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -280,6 +280,20 @@ public:
BEAST_EXPECT(q12 < q13);
BEAST_EXPECT(q31 < q21);
BEAST_EXPECT(q21 < q11);
BEAST_EXPECT(q11 >= q11);
BEAST_EXPECT(q12 >= q11);
BEAST_EXPECT(q13 >= q12);
BEAST_EXPECT(q21 >= q31);
BEAST_EXPECT(q11 >= q21);
BEAST_EXPECT(q12 > q11);
BEAST_EXPECT(q13 > q12);
BEAST_EXPECT(q21 > q31);
BEAST_EXPECT(q11 > q21);
BEAST_EXPECT(q11 <= q11);
BEAST_EXPECT(q11 <= q12);
BEAST_EXPECT(q12 <= q13);
BEAST_EXPECT(q31 <= q21);
BEAST_EXPECT(q21 <= q11);
BEAST_EXPECT(q31 != q21);
}

View File

@@ -155,6 +155,7 @@ public:
{
testGWB({});
testGWB({featureFlow, fix1373});
testGWB({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -220,6 +220,7 @@ public:
withFeatsTests({});
withFeatsTests({featureFlow});
withFeatsTests({featureFlow, fix1373});
withFeatsTests({featureFlow, fix1373, featureFlowCross});
}
};

View File

@@ -19,6 +19,7 @@
//==============================================================================
#include <test/ledger/BookDirs_test.cpp>
#include <test/ledger/CashDiff_test.cpp>
#include <test/ledger/Directory_test.cpp>
#include <test/ledger/Invariants_test.cpp>
#include <test/ledger/PaymentSandbox_test.cpp>