Skip to content

Commit

Permalink
Merge pull request #2269 from jonjove/cap-0024
Browse files Browse the repository at this point in the history
CAP-0024

Reviewed-by: MonsieurNicolas
  • Loading branch information
latobarita committed Sep 18, 2019
2 parents 7973e88 + c7283b9 commit 7a84569
Show file tree
Hide file tree
Showing 29 changed files with 4,200 additions and 710 deletions.
6 changes: 4 additions & 2 deletions src/invariant/LiabilitiesMatchOffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ getOfferBuyingLiabilities(LedgerEntry const& le)
{
auto const& oe = le.data.offer();
auto res = exchangeV10WithoutPriceErrorThresholds(
oe.price, oe.amount, INT64_MAX, INT64_MAX, INT64_MAX, false);
oe.price, oe.amount, INT64_MAX, INT64_MAX, INT64_MAX,
RoundingType::NORMAL);
return res.numSheepSend;
}

Expand All @@ -38,7 +39,8 @@ getOfferSellingLiabilities(LedgerEntry const& le)
{
auto const& oe = le.data.offer();
auto res = exchangeV10WithoutPriceErrorThresholds(
oe.price, oe.amount, INT64_MAX, INT64_MAX, INT64_MAX, false);
oe.price, oe.amount, INT64_MAX, INT64_MAX, INT64_MAX,
RoundingType::NORMAL);
return res.numWheatReceived;
}

Expand Down
38 changes: 34 additions & 4 deletions src/test/TestAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ TestAccount::pay(PublicKey const& destination, Asset const& asset,
applyTx(tx({payment(destination, asset, amount)}), mApp);
}

PathPaymentResult
PathPaymentStrictReceiveResult
TestAccount::pay(PublicKey const& destination, Asset const& sendCur,
int64_t sendMax, Asset const& destCur, int64_t destAmount,
std::vector<Asset> const& path, Asset* noIssuer)
Expand All @@ -309,19 +309,49 @@ TestAccount::pay(PublicKey const& destination, Asset const& sendCur,
{
applyTx(transaction, mApp);
}
catch (ex_PATH_PAYMENT_NO_ISSUER&)
catch (ex_PATH_PAYMENT_STRICT_RECEIVE_NO_ISSUER&)
{
REQUIRE(noIssuer);
REQUIRE(*noIssuer == transaction->getResult()
.result.results()[0]
.tr()
.pathPaymentResult()
.pathPaymentStrictReceiveResult()
.noIssuer());
throw;
}

REQUIRE(!noIssuer);

return getFirstResult(*transaction).tr().pathPaymentResult();
return getFirstResult(*transaction).tr().pathPaymentStrictReceiveResult();
}

PathPaymentStrictSendResult
TestAccount::pathPaymentStrictSend(PublicKey const& destination,
Asset const& sendCur, int64_t sendAmount,
Asset const& destCur, int64_t destMin,
std::vector<Asset> const& path,
Asset* noIssuer)
{
auto transaction = tx({txtest::pathPaymentStrictSend(
destination, sendCur, sendAmount, destCur, destMin, path)});

try
{
applyTx(transaction, mApp);
}
catch (ex_PATH_PAYMENT_STRICT_SEND_NO_ISSUER&)
{
REQUIRE(noIssuer);
REQUIRE(*noIssuer == transaction->getResult()
.result.results()[0]
.tr()
.pathPaymentStrictSendResult()
.noIssuer());
throw;
}

REQUIRE(!noIssuer);

return getFirstResult(*transaction).tr().pathPaymentStrictSendResult();
}
};
13 changes: 10 additions & 3 deletions src/test/TestAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ class TestAccount

void pay(PublicKey const& destination, int64_t amount);
void pay(PublicKey const& destination, Asset const& asset, int64_t amount);
PathPaymentResult pay(PublicKey const& destination, Asset const& sendCur,
int64_t sendMax, Asset const& destCur,
int64_t destAmount, std::vector<Asset> const& path,
PathPaymentStrictReceiveResult pay(PublicKey const& destination,
Asset const& sendCur, int64_t sendMax,
Asset const& destCur, int64_t destAmount,
std::vector<Asset> const& path,
Asset* noIssuer = nullptr);

PathPaymentStrictSendResult
pathPaymentStrictSend(PublicKey const& destination, Asset const& sendCur,
int64_t sendAmount, Asset const& destCur,
int64_t destMin, std::vector<Asset> const& path,
Asset* noIssuer = nullptr);

operator SecretKey() const
Expand Down
95 changes: 67 additions & 28 deletions src/test/TestExceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,71 @@ throwIf(PaymentResult const& result)
}

void
throwIf(PathPaymentResult const& result)
throwIf(PathPaymentStrictReceiveResult const& result)
{
switch (result.code())
{
case PATH_PAYMENT_MALFORMED:
throw ex_PATH_PAYMENT_MALFORMED{};
case PATH_PAYMENT_UNDERFUNDED:
throw ex_PATH_PAYMENT_UNDERFUNDED{};
case PATH_PAYMENT_SRC_NO_TRUST:
throw ex_PATH_PAYMENT_SRC_NO_TRUST{};
case PATH_PAYMENT_SRC_NOT_AUTHORIZED:
throw ex_PATH_PAYMENT_SRC_NOT_AUTHORIZED{};
case PATH_PAYMENT_NO_DESTINATION:
throw ex_PATH_PAYMENT_NO_DESTINATION{};
case PATH_PAYMENT_NO_TRUST:
throw ex_PATH_PAYMENT_NO_TRUST{};
case PATH_PAYMENT_NOT_AUTHORIZED:
throw ex_PATH_PAYMENT_NOT_AUTHORIZED{};
case PATH_PAYMENT_LINE_FULL:
throw ex_PATH_PAYMENT_LINE_FULL{};
case PATH_PAYMENT_NO_ISSUER:
throw ex_PATH_PAYMENT_NO_ISSUER{};
case PATH_PAYMENT_TOO_FEW_OFFERS:
throw ex_PATH_PAYMENT_TOO_FEW_OFFERS{};
case PATH_PAYMENT_OFFER_CROSS_SELF:
throw ex_PATH_PAYMENT_OFFER_CROSS_SELF{};
case PATH_PAYMENT_OVER_SENDMAX:
throw ex_PATH_PAYMENT_OVER_SENDMAX{};
case PATH_PAYMENT_SUCCESS:
case PATH_PAYMENT_STRICT_RECEIVE_MALFORMED:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_MALFORMED{};
case PATH_PAYMENT_STRICT_RECEIVE_UNDERFUNDED:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_UNDERFUNDED{};
case PATH_PAYMENT_STRICT_RECEIVE_SRC_NO_TRUST:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_SRC_NO_TRUST{};
case PATH_PAYMENT_STRICT_RECEIVE_SRC_NOT_AUTHORIZED:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_SRC_NOT_AUTHORIZED{};
case PATH_PAYMENT_STRICT_RECEIVE_NO_DESTINATION:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_NO_DESTINATION{};
case PATH_PAYMENT_STRICT_RECEIVE_NO_TRUST:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_NO_TRUST{};
case PATH_PAYMENT_STRICT_RECEIVE_NOT_AUTHORIZED:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_NOT_AUTHORIZED{};
case PATH_PAYMENT_STRICT_RECEIVE_LINE_FULL:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_LINE_FULL{};
case PATH_PAYMENT_STRICT_RECEIVE_NO_ISSUER:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_NO_ISSUER{};
case PATH_PAYMENT_STRICT_RECEIVE_TOO_FEW_OFFERS:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_TOO_FEW_OFFERS{};
case PATH_PAYMENT_STRICT_RECEIVE_OFFER_CROSS_SELF:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_OFFER_CROSS_SELF{};
case PATH_PAYMENT_STRICT_RECEIVE_OVER_SENDMAX:
throw ex_PATH_PAYMENT_STRICT_RECEIVE_OVER_SENDMAX{};
case PATH_PAYMENT_STRICT_RECEIVE_SUCCESS:
break;
default:
throw ex_UNKNOWN{};
}
}

void
throwIf(PathPaymentStrictSendResult const& result)
{
switch (result.code())
{
case PATH_PAYMENT_STRICT_SEND_MALFORMED:
throw ex_PATH_PAYMENT_STRICT_SEND_MALFORMED{};
case PATH_PAYMENT_STRICT_SEND_UNDERFUNDED:
throw ex_PATH_PAYMENT_STRICT_SEND_UNDERFUNDED{};
case PATH_PAYMENT_STRICT_SEND_SRC_NO_TRUST:
throw ex_PATH_PAYMENT_STRICT_SEND_SRC_NO_TRUST{};
case PATH_PAYMENT_STRICT_SEND_SRC_NOT_AUTHORIZED:
throw ex_PATH_PAYMENT_STRICT_SEND_SRC_NOT_AUTHORIZED{};
case PATH_PAYMENT_STRICT_SEND_NO_DESTINATION:
throw ex_PATH_PAYMENT_STRICT_SEND_NO_DESTINATION{};
case PATH_PAYMENT_STRICT_SEND_NO_TRUST:
throw ex_PATH_PAYMENT_STRICT_SEND_NO_TRUST{};
case PATH_PAYMENT_STRICT_SEND_NOT_AUTHORIZED:
throw ex_PATH_PAYMENT_STRICT_SEND_NOT_AUTHORIZED{};
case PATH_PAYMENT_STRICT_SEND_LINE_FULL:
throw ex_PATH_PAYMENT_STRICT_SEND_LINE_FULL{};
case PATH_PAYMENT_STRICT_SEND_NO_ISSUER:
throw ex_PATH_PAYMENT_STRICT_SEND_NO_ISSUER{};
case PATH_PAYMENT_STRICT_SEND_TOO_FEW_OFFERS:
throw ex_PATH_PAYMENT_STRICT_SEND_TOO_FEW_OFFERS{};
case PATH_PAYMENT_STRICT_SEND_OFFER_CROSS_SELF:
throw ex_PATH_PAYMENT_STRICT_SEND_OFFER_CROSS_SELF{};
case PATH_PAYMENT_STRICT_SEND_UNDER_DESTMIN:
throw ex_PATH_PAYMENT_STRICT_SEND_UNDER_DESTMIN{};
case PATH_PAYMENT_STRICT_SEND_SUCCESS:
break;
default:
throw ex_UNKNOWN{};
Expand Down Expand Up @@ -362,8 +398,8 @@ throwIf(TransactionResult const& result)
case PAYMENT:
throwIf(opResult.tr().paymentResult());
break;
case PATH_PAYMENT:
throwIf(opResult.tr().pathPaymentResult());
case PATH_PAYMENT_STRICT_RECEIVE:
throwIf(opResult.tr().pathPaymentStrictReceiveResult());
break;
case MANAGE_SELL_OFFER:
throwIf(opResult.tr().manageSellOfferResult());
Expand Down Expand Up @@ -395,6 +431,9 @@ throwIf(TransactionResult const& result)
case MANAGE_BUY_OFFER:
throwIf(opResult.tr().manageBuyOfferResult());
break;
case PATH_PAYMENT_STRICT_SEND:
throwIf(opResult.tr().pathPaymentStrictSendResult());
break;
}
}
}
Expand Down
40 changes: 27 additions & 13 deletions src/test/TestExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,33 @@ TEST_EXCEPTION(ex_MANAGE_BUY_OFFER_BUY_NO_ISSUER)
TEST_EXCEPTION(ex_MANAGE_BUY_OFFER_NOT_FOUND)
TEST_EXCEPTION(ex_MANAGE_BUY_OFFER_LOW_RESERVE)

TEST_EXCEPTION(ex_PATH_PAYMENT_SUCCESS)
TEST_EXCEPTION(ex_PATH_PAYMENT_MALFORMED)
TEST_EXCEPTION(ex_PATH_PAYMENT_UNDERFUNDED)
TEST_EXCEPTION(ex_PATH_PAYMENT_SRC_NO_TRUST)
TEST_EXCEPTION(ex_PATH_PAYMENT_SRC_NOT_AUTHORIZED)
TEST_EXCEPTION(ex_PATH_PAYMENT_NO_DESTINATION)
TEST_EXCEPTION(ex_PATH_PAYMENT_NO_TRUST)
TEST_EXCEPTION(ex_PATH_PAYMENT_NOT_AUTHORIZED)
TEST_EXCEPTION(ex_PATH_PAYMENT_LINE_FULL)
TEST_EXCEPTION(ex_PATH_PAYMENT_NO_ISSUER)
TEST_EXCEPTION(ex_PATH_PAYMENT_TOO_FEW_OFFERS)
TEST_EXCEPTION(ex_PATH_PAYMENT_OFFER_CROSS_SELF)
TEST_EXCEPTION(ex_PATH_PAYMENT_OVER_SENDMAX)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_SUCCESS)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_MALFORMED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_UNDERFUNDED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_SRC_NO_TRUST)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_SRC_NOT_AUTHORIZED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_NO_DESTINATION)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_NO_TRUST)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_NOT_AUTHORIZED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_LINE_FULL)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_NO_ISSUER)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_TOO_FEW_OFFERS)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_OFFER_CROSS_SELF)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_RECEIVE_OVER_SENDMAX)

TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_SUCCESS)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_MALFORMED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_UNDERFUNDED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_SRC_NO_TRUST)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_SRC_NOT_AUTHORIZED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_NO_DESTINATION)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_NO_TRUST)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_NOT_AUTHORIZED)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_LINE_FULL)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_NO_ISSUER)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_TOO_FEW_OFFERS)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_OFFER_CROSS_SELF)
TEST_EXCEPTION(ex_PATH_PAYMENT_STRICT_SEND_UNDER_DESTMIN)

TEST_EXCEPTION(ex_PAYMENT_MALFORMED)
TEST_EXCEPTION(ex_PAYMENT_UNDERFUNDED)
Expand Down
34 changes: 21 additions & 13 deletions src/test/TxTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@
#include "test/TestExceptions.h"
#include "test/TestUtils.h"
#include "test/test.h"
#include "transactions/AllowTrustOpFrame.h"
#include "transactions/BumpSequenceOpFrame.h"
#include "transactions/ChangeTrustOpFrame.h"
#include "transactions/CreateAccountOpFrame.h"
#include "transactions/InflationOpFrame.h"
#include "transactions/ManageDataOpFrame.h"
#include "transactions/ManageSellOfferOpFrame.h"
#include "transactions/MergeOpFrame.h"
#include "transactions/PathPaymentOpFrame.h"
#include "transactions/PaymentOpFrame.h"
#include "transactions/SetOptionsOpFrame.h"
#include "transactions/OperationFrame.h"
#include "transactions/TransactionFrame.h"
#include "transactions/TransactionUtils.h"
#include "util/Logging.h"
Expand Down Expand Up @@ -534,8 +524,8 @@ pathPayment(PublicKey const& to, Asset const& sendCur, int64_t sendMax,
std::vector<Asset> const& path)
{
Operation op;
op.body.type(PATH_PAYMENT);
PathPaymentOp& ppop = op.body.pathPaymentOp();
op.body.type(PATH_PAYMENT_STRICT_RECEIVE);
PathPaymentStrictReceiveOp& ppop = op.body.pathPaymentStrictReceiveOp();
ppop.sendAsset = sendCur;
ppop.sendMax = sendMax;
ppop.destAsset = destCur;
Expand All @@ -546,6 +536,24 @@ pathPayment(PublicKey const& to, Asset const& sendCur, int64_t sendMax,
return op;
}

Operation
pathPaymentStrictSend(PublicKey const& to, Asset const& sendCur,
int64_t sendAmount, Asset const& destCur, int64_t destMin,
std::vector<Asset> const& path)
{
Operation op;
op.body.type(PATH_PAYMENT_STRICT_SEND);
PathPaymentStrictSendOp& ppop = op.body.pathPaymentStrictSendOp();
ppop.sendAsset = sendCur;
ppop.sendAmount = sendAmount;
ppop.destAsset = destCur;
ppop.destMin = destMin;
ppop.destination = to;
std::copy(std::begin(path), std::end(path), std::back_inserter(ppop.path));

return op;
}

Operation
createPassiveOffer(Asset const& selling, Asset const& buying,
Price const& price, int64_t amount)
Expand Down
5 changes: 5 additions & 0 deletions src/test/TxTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ Operation pathPayment(PublicKey const& to, Asset const& sendCur,
int64_t sendMax, Asset const& destCur, int64_t destAmount,
std::vector<Asset> const& path);

Operation pathPaymentStrictSend(PublicKey const& to, Asset const& sendCur,
int64_t sendAmount, Asset const& destCur,
int64_t destMin,
std::vector<Asset> const& path);

Operation manageOffer(int64 offerId, Asset const& selling, Asset const& buying,
Price const& price, int64_t amount);
Operation manageBuyOffer(int64 offerId, Asset const& selling,
Expand Down
4 changes: 2 additions & 2 deletions src/transactions/ManageBuyOfferOpFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ManageBuyOfferOpFrame::getOfferBuyingLiabilities()
{
auto res = exchangeV10WithoutPriceErrorThresholds(
getInversePrice(mManageBuyOffer.price), INT64_MAX, INT64_MAX, INT64_MAX,
mManageBuyOffer.buyAmount, false);
mManageBuyOffer.buyAmount, RoundingType::NORMAL);
return res.numSheepSend;
}

Expand All @@ -62,7 +62,7 @@ ManageBuyOfferOpFrame::getOfferSellingLiabilities()
{
auto res = exchangeV10WithoutPriceErrorThresholds(
getInversePrice(mManageBuyOffer.price), INT64_MAX, INT64_MAX, INT64_MAX,
mManageBuyOffer.buyAmount, false);
mManageBuyOffer.buyAmount, RoundingType::NORMAL);
return res.numWheatReceived;
}

Expand Down
2 changes: 1 addition & 1 deletion src/transactions/ManageOfferOpFrameBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ ManageOfferOpFrameBase::doApply(AbstractLedgerTxn& ltxOuter)
Price maxWheatPrice(mPrice.d, mPrice.n);
ConvertResult r = convertWithOffers(
ltx, mSheep, maxSheepSend, sheepSent, mWheat, maxWheatReceive,
wheatReceived, false,
wheatReceived, RoundingType::NORMAL,
[this, passive, &maxWheatPrice](LedgerTxnEntry const& entry) {
auto const& o = entry.current().data.offer();
assert(o.offerID != mOfferID);
Expand Down
4 changes: 2 additions & 2 deletions src/transactions/ManageSellOfferOpFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ManageSellOfferOpFrame::getOfferBuyingLiabilities()
{
auto res = exchangeV10WithoutPriceErrorThresholds(
mManageSellOffer.price, mManageSellOffer.amount, INT64_MAX, INT64_MAX,
INT64_MAX, false);
INT64_MAX, RoundingType::NORMAL);
return res.numSheepSend;
}

Expand All @@ -71,7 +71,7 @@ ManageSellOfferOpFrame::getOfferSellingLiabilities()
{
auto res = exchangeV10WithoutPriceErrorThresholds(
mManageSellOffer.price, mManageSellOffer.amount, INT64_MAX, INT64_MAX,
INT64_MAX, false);
INT64_MAX, RoundingType::NORMAL);
return res.numWheatReceived;
}

Expand Down
Loading

0 comments on commit 7a84569

Please sign in to comment.