diff --git a/src/history/HistoryTests.cpp b/src/history/HistoryTests.cpp index 8817f0b515..6c1bb999d4 100644 --- a/src/history/HistoryTests.cpp +++ b/src/history/HistoryTests.cpp @@ -1,6 +1,7 @@ // Copyright 2014 Stellar Development Foundation and contributors. Licensed // under the Apache License, Version 2.0. See the COPYING file at the root // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 + #include "util/asio.h" #include "bucket/BucketList.h" #include "bucket/BucketManager.h" @@ -16,6 +17,7 @@ #include "main/ExternalQueue.h" #include "main/PersistentState.h" #include "process/ProcessManager.h" +#include "test/TestAccount.h" #include "test/TxTests.h" #include "test/test.h" #include "util/Fs.h" @@ -25,6 +27,7 @@ #include "util/TmpDir.h" #include "work/WorkManager.h" #include "work/WorkParent.h" + #include #include #include @@ -34,6 +37,7 @@ using namespace stellar; namespace stellar { +using namespace txtest; using xdr::operator==; }; @@ -94,11 +98,6 @@ class HistoryTests Application::pointer appPtr; Application& app; - SecretKey mRoot; - SecretKey mAlice; - SecretKey mBob; - SecretKey mCarol; - std::default_random_engine mGenerator; std::bernoulli_distribution mFlip{0.5}; @@ -110,15 +109,15 @@ class HistoryTests std::vector mBucket0Hashes; std::vector mBucket1Hashes; - std::vector mRootBalances; - std::vector mAliceBalances; - std::vector mBobBalances; - std::vector mCarolBalances; + std::vector rootBalances; + std::vector aliceBalances; + std::vector bobBalances; + std::vector carolBalances; - std::vector mRootSeqs; - std::vector mAliceSeqs; - std::vector mBobSeqs; - std::vector mCarolSeqs; + std::vector rootSeqs; + std::vector aliceSeqs; + std::vector bobSeqs; + std::vector carolSeqs; public: HistoryTests(std::shared_ptr cg = @@ -128,10 +127,6 @@ class HistoryTests , appPtr( Application::create(clock, mConfigurator->configure(cfg, true))) , app(*appPtr) - , mRoot(txtest::getRoot(app.getNetworkID())) - , mAlice(txtest::getAccount("alice")) - , mBob(txtest::getAccount("bob")) - , mCarol(txtest::getAccount("carol")) { CHECK(HistoryManager::initializeHistoryArchive(app, "test")); } @@ -267,48 +262,36 @@ HistoryTests::generateRandomLedger() uint64_t small = 100 + ledgerSeq; uint64_t closeTime = 60 * 5 * ledgerSeq; - SequenceNumber rseq = txtest::getAccountSeqNum(mRoot, app) + 1; - - Hash const& networkID = app.getNetworkID(); + auto root = TestAccount{app, getRoot(app.getNetworkID())}; + auto alice = TestAccount{app, getAccount("alice")}; + auto bob = TestAccount{app, getAccount("bob")}; + auto carol = TestAccount{app, getAccount("carol")}; // Root sends to alice every tx, bob every other tx, carol every 4rd tx. - txSet->add( - txtest::createCreateAccountTx(networkID, mRoot, mAlice, rseq++, big)); - txSet->add( - txtest::createCreateAccountTx(networkID, mRoot, mBob, rseq++, big)); - txSet->add( - txtest::createCreateAccountTx(networkID, mRoot, mCarol, rseq++, big)); - txSet->add(txtest::createPaymentTx(networkID, mRoot, mAlice, rseq++, big)); - txSet->add(txtest::createPaymentTx(networkID, mRoot, mBob, rseq++, big)); - txSet->add(txtest::createPaymentTx(networkID, mRoot, mCarol, rseq++, big)); + txSet->add(root.tx({createCreateAccountOp(nullptr, alice, big)})); + txSet->add(root.tx({createCreateAccountOp(nullptr, bob, big)})); + txSet->add(root.tx({createCreateAccountOp(nullptr, carol, big)})); + txSet->add(root.tx({createPaymentOp(nullptr, alice, big)})); + txSet->add(root.tx({createPaymentOp(nullptr, bob, big)})); + txSet->add(root.tx({createPaymentOp(nullptr, carol, big)})); // They all randomly send a little to one another every ledger after #4 if (ledgerSeq > 4) { - SequenceNumber aseq = txtest::getAccountSeqNum(mAlice, app) + 1; - SequenceNumber bseq = txtest::getAccountSeqNum(mBob, app) + 1; - SequenceNumber cseq = txtest::getAccountSeqNum(mCarol, app) + 1; - if (flip()) - txSet->add(txtest::createPaymentTx(networkID, mAlice, mBob, aseq++, - small)); + txSet->add(alice.tx({createPaymentOp(nullptr, bob, small)})); if (flip()) - txSet->add(txtest::createPaymentTx(networkID, mAlice, mCarol, - aseq++, small)); + txSet->add(alice.tx({createPaymentOp(nullptr, carol, small)})); if (flip()) - txSet->add(txtest::createPaymentTx(networkID, mBob, mAlice, bseq++, - small)); + txSet->add(bob.tx({createPaymentOp(nullptr, alice, small)})); if (flip()) - txSet->add(txtest::createPaymentTx(networkID, mBob, mCarol, bseq++, - small)); + txSet->add(bob.tx({createPaymentOp(nullptr, carol, small)})); if (flip()) - txSet->add(txtest::createPaymentTx(networkID, mCarol, mAlice, - cseq++, small)); + txSet->add(carol.tx({createPaymentOp(nullptr, alice, small)})); if (flip()) - txSet->add(txtest::createPaymentTx(networkID, mCarol, mBob, cseq++, - small)); + txSet->add(carol.tx({createPaymentOp(nullptr, bob, small)})); } // Provoke sortForHash and hash-caching: @@ -337,15 +320,15 @@ HistoryTests::generateRandomLedger() .getCurr() ->getHash()); - mRootBalances.push_back(txtest::getAccountBalance(mRoot, app)); - mAliceBalances.push_back(txtest::getAccountBalance(mAlice, app)); - mBobBalances.push_back(txtest::getAccountBalance(mBob, app)); - mCarolBalances.push_back(txtest::getAccountBalance(mCarol, app)); + rootBalances.push_back(root.getBalance()); + aliceBalances.push_back(alice.getBalance()); + bobBalances.push_back(bob.getBalance()); + carolBalances.push_back(carol.getBalance()); - mRootSeqs.push_back(txtest::getAccountSeqNum(mRoot, app)); - mAliceSeqs.push_back(txtest::getAccountSeqNum(mAlice, app)); - mBobSeqs.push_back(txtest::getAccountSeqNum(mBob, app)); - mCarolSeqs.push_back(txtest::getAccountSeqNum(mCarol, app)); + rootSeqs.push_back(root.loadSequenceNumber()); + aliceSeqs.push_back(alice.loadSequenceNumber()); + bobSeqs.push_back(bob.loadSequenceNumber()); + carolSeqs.push_back(carol.loadSequenceNumber()); } void @@ -414,6 +397,10 @@ HistoryTests::catchupApplication(uint32_t initLedger, Application::pointer app2, bool doStart, uint32_t gap) { + auto root = TestAccount{*app2, getRoot(app.getNetworkID())}; + auto alice = TestAccount{*app2, getAccount("alice")}; + auto bob = TestAccount{*app2, getAccount("bob")}; + auto carol = TestAccount{*app2, getAccount("carol")}; auto& lm = app2->getLedgerManager(); if (doStart) @@ -569,25 +556,25 @@ HistoryTests::catchupApplication(uint32_t initLedger, CHECK(wantBucket0Hash == haveBucket0Hash); CHECK(wantBucket1Hash == haveBucket1Hash); - auto haveRootBalance = mRootBalances.at(i); - auto haveAliceBalance = mAliceBalances.at(i); - auto haveBobBalance = mBobBalances.at(i); - auto haveCarolBalance = mCarolBalances.at(i); - - auto haveRootSeq = mRootSeqs.at(i); - auto haveAliceSeq = mAliceSeqs.at(i); - auto haveBobSeq = mBobSeqs.at(i); - auto haveCarolSeq = mCarolSeqs.at(i); - - auto wantRootBalance = txtest::getAccountBalance(mRoot, *app2); - auto wantAliceBalance = txtest::getAccountBalance(mAlice, *app2); - auto wantBobBalance = txtest::getAccountBalance(mBob, *app2); - auto wantCarolBalance = txtest::getAccountBalance(mCarol, *app2); - - auto wantRootSeq = txtest::getAccountSeqNum(mRoot, *app2); - auto wantAliceSeq = txtest::getAccountSeqNum(mAlice, *app2); - auto wantBobSeq = txtest::getAccountSeqNum(mBob, *app2); - auto wantCarolSeq = txtest::getAccountSeqNum(mCarol, *app2); + auto haveRootBalance = rootBalances.at(i); + auto haveAliceBalance = aliceBalances.at(i); + auto haveBobBalance = bobBalances.at(i); + auto haveCarolBalance = carolBalances.at(i); + + auto haveRootSeq = rootSeqs.at(i); + auto haveAliceSeq = aliceSeqs.at(i); + auto haveBobSeq = bobSeqs.at(i); + auto haveCarolSeq = carolSeqs.at(i); + + auto wantRootBalance = root.getBalance(); + auto wantAliceBalance = alice.getBalance(); + auto wantBobBalance = bob.getBalance(); + auto wantCarolBalance = carol.getBalance(); + + auto wantRootSeq = root.loadSequenceNumber(); + auto wantAliceSeq = alice.loadSequenceNumber(); + auto wantBobSeq = bob.loadSequenceNumber(); + auto wantCarolSeq = carol.loadSequenceNumber(); CHECK(haveRootBalance == wantRootBalance); CHECK(haveAliceBalance == wantAliceBalance); diff --git a/src/test/TestAccount.cpp b/src/test/TestAccount.cpp index 1c578e6406..b787aba0b4 100644 --- a/src/test/TestAccount.cpp +++ b/src/test/TestAccount.cpp @@ -12,15 +12,27 @@ namespace stellar using namespace txtest; +SequenceNumber +TestAccount::loadSequenceNumber() const +{ + return loadAccount(getSecretKey(), mApp)->getSeqNum(); +} + void TestAccount::updateSequenceNumber() { if (mSn == 0) { - mSn = getAccountSeqNum(getSecretKey(), mApp); + mSn = loadSequenceNumber(); } } +int64_t +TestAccount::getBalance() const +{ + return loadAccount(getSecretKey(), mApp)->getBalance(); +} + TransactionFramePtr TestAccount::tx(std::vector const& ops) { @@ -32,8 +44,7 @@ TestAccount TestAccount::createRoot(Application& app) { auto secretKey = getRoot(app.getNetworkID()); - auto sequenceNumber = getAccountSeqNum(secretKey, app); - return TestAccount{app, secretKey, sequenceNumber}; + return TestAccount{app, secretKey}; } TestAccount @@ -41,8 +52,7 @@ TestAccount::create(SecretKey const& secretKey, uint64_t initialBalance) { applyCreateAccountTx(mApp, getSecretKey(), secretKey, nextSequenceNumber(), initialBalance); - auto sequenceNumber = getAccountSeqNum(secretKey, mApp); - return TestAccount{mApp, secretKey, sequenceNumber}; + return TestAccount{mApp, secretKey}; } TestAccount diff --git a/src/test/TestAccount.h b/src/test/TestAccount.h index e3b06291f8..ea159d4add 100644 --- a/src/test/TestAccount.h +++ b/src/test/TestAccount.h @@ -95,6 +95,9 @@ class TestAccount updateSequenceNumber(); return ++mSn; } + SequenceNumber loadSequenceNumber() const; + + int64_t getBalance() const; private: Application& mApp; diff --git a/src/test/TestExceptions.cpp b/src/test/TestExceptions.cpp new file mode 100644 index 0000000000..2f26239bbf --- /dev/null +++ b/src/test/TestExceptions.cpp @@ -0,0 +1,317 @@ +// Copyright 2016 Stellar Development Foundation and contributors. Licensed +// under the Apache License, Version 2.0. See the COPYING file at the root +// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 + +#include "test/TestExceptions.h" + +namespace stellar +{ + +namespace txtest +{ + +void +throwIf(CreateAccountResult const& result) +{ + switch (result.code()) + { + case CREATE_ACCOUNT_MALFORMED: + throw ex_CREATE_ACCOUNT_MALFORMED{}; + case CREATE_ACCOUNT_UNDERFUNDED: + throw ex_CREATE_ACCOUNT_UNDERFUNDED{}; + case CREATE_ACCOUNT_LOW_RESERVE: + throw ex_CREATE_ACCOUNT_LOW_RESERVE{}; + case CREATE_ACCOUNT_ALREADY_EXIST: + throw ex_CREATE_ACCOUNT_ALREADY_EXIST{}; + case CREATE_ACCOUNT_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(PaymentResult const& result) +{ + switch (result.code()) + { + case PAYMENT_MALFORMED: + throw ex_PAYMENT_MALFORMED{}; + case PAYMENT_UNDERFUNDED: + throw ex_PAYMENT_UNDERFUNDED{}; + case PAYMENT_SRC_NO_TRUST: + throw ex_PAYMENT_SRC_NO_TRUST{}; + case PAYMENT_SRC_NOT_AUTHORIZED: + throw ex_PAYMENT_SRC_NOT_AUTHORIZED{}; + case PAYMENT_NO_DESTINATION: + throw ex_PAYMENT_NO_DESTINATION{}; + case PAYMENT_NO_TRUST: + throw ex_PAYMENT_NO_TRUST{}; + case PAYMENT_NOT_AUTHORIZED: + throw ex_PAYMENT_NOT_AUTHORIZED{}; + case PAYMENT_LINE_FULL: + throw ex_PAYMENT_LINE_FULL{}; + case PAYMENT_NO_ISSUER: + throw ex_PAYMENT_NO_ISSUER{}; + case PAYMENT_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(PathPaymentResult 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: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(ManageOfferResult const& result) +{ + switch (result.code()) + { + case MANAGE_OFFER_MALFORMED: + throw ex_MANAGE_OFFER_MALFORMED{}; + case MANAGE_OFFER_SELL_NO_TRUST: + throw ex_MANAGE_OFFER_SELL_NO_TRUST{}; + case MANAGE_OFFER_BUY_NO_TRUST: + throw ex_MANAGE_OFFER_BUY_NO_TRUST{}; + case MANAGE_OFFER_SELL_NOT_AUTHORIZED: + throw ex_MANAGE_OFFER_SELL_NOT_AUTHORIZED{}; + case MANAGE_OFFER_BUY_NOT_AUTHORIZED: + throw ex_MANAGE_OFFER_BUY_NOT_AUTHORIZED{}; + case MANAGE_OFFER_LINE_FULL: + throw ex_MANAGE_OFFER_LINE_FULL{}; + case MANAGE_OFFER_UNDERFUNDED: + throw ex_MANAGE_OFFER_UNDERFUNDED{}; + case MANAGE_OFFER_CROSS_SELF: + throw ex_MANAGE_OFFER_CROSS_SELF{}; + case MANAGE_OFFER_SELL_NO_ISSUER: + throw ex_MANAGE_OFFER_SELL_NO_ISSUER{}; + case MANAGE_OFFER_BUY_NO_ISSUER: + throw ex_MANAGE_OFFER_BUY_NO_ISSUER{}; + case MANAGE_OFFER_NOT_FOUND: + throw ex_MANAGE_OFFER_NOT_FOUND{}; + case MANAGE_OFFER_LOW_RESERVE: + throw ex_MANAGE_OFFER_LOW_RESERVE{}; + case MANAGE_OFFER_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(SetOptionsResult const& result) +{ + switch (result.code()) + { + case SET_OPTIONS_LOW_RESERVE: + throw ex_SET_OPTIONS_LOW_RESERVE{}; + case SET_OPTIONS_TOO_MANY_SIGNERS: + throw ex_SET_OPTIONS_TOO_MANY_SIGNERS{}; + case SET_OPTIONS_BAD_FLAGS: + throw ex_SET_OPTIONS_BAD_FLAGS{}; + case SET_OPTIONS_INVALID_INFLATION: + throw ex_SET_OPTIONS_INVALID_INFLATION{}; + case SET_OPTIONS_CANT_CHANGE: + throw ex_SET_OPTIONS_CANT_CHANGE{}; + case SET_OPTIONS_UNKNOWN_FLAG: + throw ex_SET_OPTIONS_UNKNOWN_FLAG{}; + case SET_OPTIONS_THRESHOLD_OUT_OF_RANGE: + throw ex_SET_OPTIONS_THRESHOLD_OUT_OF_RANGE{}; + case SET_OPTIONS_BAD_SIGNER: + throw ex_SET_OPTIONS_BAD_SIGNER{}; + case SET_OPTIONS_INVALID_HOME_DOMAIN: + throw ex_SET_OPTIONS_INVALID_HOME_DOMAIN{}; + case SET_OPTIONS_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(ChangeTrustResult const& result) +{ + switch (result.code()) + { + case CHANGE_TRUST_MALFORMED: + throw ex_CHANGE_TRUST_MALFORMED{}; + case CHANGE_TRUST_NO_ISSUER: + throw ex_CHANGE_TRUST_NO_ISSUER{}; + case CHANGE_TRUST_INVALID_LIMIT: + throw ex_CHANGE_TRUST_INVALID_LIMIT{}; + case CHANGE_TRUST_LOW_RESERVE: + throw ex_CHANGE_TRUST_LOW_RESERVE{}; + case CHANGE_TRUST_SELF_NOT_ALLOWED: + throw ex_CHANGE_TRUST_SELF_NOT_ALLOWED{}; + case CHANGE_TRUST_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(AllowTrustResult const& result) +{ + switch (result.code()) + { + case ALLOW_TRUST_MALFORMED: + throw ex_ALLOW_TRUST_MALFORMED{}; + case ALLOW_TRUST_NO_TRUST_LINE: + throw ex_ALLOW_TRUST_NO_TRUST_LINE{}; + case ALLOW_TRUST_TRUST_NOT_REQUIRED: + throw ex_ALLOW_TRUST_TRUST_NOT_REQUIRED{}; + case ALLOW_TRUST_CANT_REVOKE: + throw ex_ALLOW_TRUST_CANT_REVOKE{}; + case ALLOW_TRUST_SELF_NOT_ALLOWED: + throw ex_ALLOW_TRUST_SELF_NOT_ALLOWED{}; + case ALLOW_TRUST_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(AccountMergeResult const& result) +{ + switch (result.code()) + { + case ACCOUNT_MERGE_MALFORMED: + throw ex_ACCOUNT_MERGE_MALFORMED{}; + case ACCOUNT_MERGE_NO_ACCOUNT: + throw ex_ACCOUNT_MERGE_NO_ACCOUNT{}; + case ACCOUNT_MERGE_IMMUTABLE_SET: + throw ex_ACCOUNT_MERGE_IMMUTABLE_SET{}; + case ACCOUNT_MERGE_HAS_SUB_ENTRIES: + throw ex_ACCOUNT_MERGE_HAS_SUB_ENTRIES{}; + case ACCOUNT_MERGE_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(InflationResult const& result) +{ + switch (result.code()) + { + case INFLATION_NOT_TIME: + throw ex_INFLATION_NOT_TIME{}; + case INFLATION_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(ManageDataResult const& result) +{ + switch (result.code()) + { + case MANAGE_DATA_NOT_SUPPORTED_YET: + throw ex_MANAGE_DATA_NOT_SUPPORTED_YET{}; + case MANAGE_DATA_NAME_NOT_FOUND: + throw ex_MANAGE_DATA_NAME_NOT_FOUND{}; + case MANAGE_DATA_LOW_RESERVE: + throw ex_MANAGE_DATA_LOW_RESERVE{}; + case MANAGE_DATA_INVALID_NAME: + throw ex_MANAGE_DATA_INVALID_NAME{}; + case MANAGE_DATA_SUCCESS: + break; + default: + throw ex_UNKNOWN{}; + } +} + +void +throwIf(TransactionResult const& result) +{ + switch (result.result.code()) + { + case txNO_ACCOUNT: + throw ex_txNO_ACCOUNT{}; + case txINTERNAL_ERROR: + throw ex_txINTERNAL_ERROR{}; + default: + // ignore rest for now + break; + } + + auto opResult = result.result.results()[0]; + switch (opResult.tr().type()) + { + case CREATE_ACCOUNT: + throwIf(opResult.tr().createAccountResult()); + break; + case PAYMENT: + throwIf(opResult.tr().paymentResult()); + break; + case PATH_PAYMENT: + throwIf(opResult.tr().pathPaymentResult()); + break; + case MANAGE_OFFER: + throwIf(opResult.tr().manageOfferResult()); + break; + case CREATE_PASSIVE_OFFER: + throwIf(opResult.tr().createPassiveOfferResult()); + break; + case SET_OPTIONS: + throwIf(opResult.tr().setOptionsResult()); + break; + case CHANGE_TRUST: + throwIf(opResult.tr().changeTrustResult()); + break; + case ALLOW_TRUST: + throwIf(opResult.tr().allowTrustResult()); + break; + case ACCOUNT_MERGE: + throwIf(opResult.tr().accountMergeResult()); + break; + case INFLATION: + throwIf(opResult.tr().inflationResult()); + break; + case MANAGE_DATA: + throwIf(opResult.tr().manageDataResult()); + break; + } +} + +} +} diff --git a/src/test/TestExceptions.h b/src/test/TestExceptions.h index 2f4933375e..407a8aa5f8 100644 --- a/src/test/TestExceptions.h +++ b/src/test/TestExceptions.h @@ -4,12 +4,16 @@ // under the Apache License, Version 2.0. See the COPYING file at the root // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 +#include "xdr/Stellar-transaction.h" + namespace stellar { namespace txtest { +void throwIf(TransactionResult const& result); + class ex_txNO_ACCOUNT { }; @@ -18,6 +22,10 @@ class ex_txINTERNAL_ERROR { }; +class ex_UNKNOWN +{ +}; + class ex_ACCOUNT_MERGE_MALFORMED { }; @@ -221,5 +229,9 @@ class ex_SET_OPTIONS_BAD_SIGNER class ex_SET_OPTIONS_INVALID_HOME_DOMAIN { }; + +class ex_INFLATION_NOT_TIME +{ +}; } } diff --git a/src/test/TxTests.cpp b/src/test/TxTests.cpp index e0173ce7a8..3bec8c893f 100644 --- a/src/test/TxTests.cpp +++ b/src/test/TxTests.cpp @@ -99,23 +99,6 @@ applyCheck(TransactionFramePtr tx, LedgerDelta& delta, Application& app) return res; } -bool -throwingApplyCheck(TransactionFramePtr tx, LedgerDelta& delta, Application& app) -{ - auto r = applyCheck(tx, delta, app); - switch (tx->getResultCode()) - { - case txNO_ACCOUNT: - throw ex_txNO_ACCOUNT{}; - case txINTERNAL_ERROR: - throw ex_txINTERNAL_ERROR{}; - default: - // ignore rest for now - break; - } - return r; -} - TxSetResultMeta closeLedgerOn(Application& app, uint32 ledgerSeq, int day, int month, int year, std::vector const& txs) @@ -214,22 +197,6 @@ loadTrustLine(SecretKey const& k, Asset const& asset, Application& app, return res; } -SequenceNumber -getAccountSeqNum(SecretKey const& k, Application& app) -{ - AccountFrame::pointer account; - account = loadAccount(k, app); - return account->getSeqNum(); -} - -int64_t -getAccountBalance(SecretKey const& k, Application& app) -{ - AccountFrame::pointer account; - account = loadAccount(k, app); - return account->getBalance(); -} - xdr::xvector getAccountSigners(SecretKey const& k, Application& app) { @@ -329,31 +296,9 @@ applyAllowTrust(Application& app, SecretKey const& from, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - + applyCheck(txFrame, delta, app); + throwIf(txFrame->getResult()); checkTransaction(*txFrame); - - auto result = AllowTrustOpFrame::getInnerCode( - txFrame->getResult().result.results()[0]); - switch (result) - { - case ALLOW_TRUST_MALFORMED: - throw ex_ALLOW_TRUST_MALFORMED{}; - case ALLOW_TRUST_NO_TRUST_LINE: - throw ex_ALLOW_TRUST_NO_TRUST_LINE{}; - case ALLOW_TRUST_TRUST_NOT_REQUIRED: - throw ex_ALLOW_TRUST_TRUST_NOT_REQUIRED{}; - case ALLOW_TRUST_CANT_REVOKE: - throw ex_ALLOW_TRUST_CANT_REVOKE{}; - case ALLOW_TRUST_SELF_NOT_ALLOWED: - throw ex_ALLOW_TRUST_SELF_NOT_ALLOWED{}; - default: - break; - } - - REQUIRE(AllowTrustOpFrame::getInnerCode( - txFrame->getResult().result.results()[0]) == - ALLOW_TRUST_SUCCESS); } Operation @@ -393,18 +338,9 @@ applyCreateAccountTx(Application& app, SecretKey const& from, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - - checkTransaction(*txFrame); - auto txResult = txFrame->getResult(); - auto result = - CreateAccountOpFrame::getInnerCode(txResult.result.results()[0]); - - AccountFrame::pointer toAccountAfter; - toAccountAfter = loadAccount(to, app, false); - - if (result != CREATE_ACCOUNT_SUCCESS) + if (!applyCheck(txFrame, delta, app)) { + auto toAccountAfter = loadAccount(to, app, false); // check that the target account didn't change REQUIRE(!!toAccount == !!toAccountAfter); if (toAccount && toAccountAfter) @@ -412,27 +348,11 @@ applyCreateAccountTx(Application& app, SecretKey const& from, REQUIRE(toAccount->getAccount() == toAccountAfter->getAccount()); } } - else - { - REQUIRE(toAccountAfter); - } - REQUIRE(txResult.feeCharged == app.getLedgerManager().getTxFee()); - - switch (result) - { - case CREATE_ACCOUNT_MALFORMED: - throw ex_CREATE_ACCOUNT_MALFORMED{}; - case CREATE_ACCOUNT_UNDERFUNDED: - throw ex_CREATE_ACCOUNT_UNDERFUNDED{}; - case CREATE_ACCOUNT_LOW_RESERVE: - throw ex_CREATE_ACCOUNT_LOW_RESERVE{}; - case CREATE_ACCOUNT_ALREADY_EXIST: - throw ex_CREATE_ACCOUNT_ALREADY_EXIST{}; - default: - break; - } + throwIf(txFrame->getResult()); + checkTransaction(*txFrame); - REQUIRE(result == CREATE_ACCOUNT_SUCCESS); + auto toAccountAfter = loadAccount(to, app, false); + REQUIRE(toAccountAfter); } Operation @@ -473,19 +393,9 @@ applyPaymentTx(Application& app, SecretKey const& from, SecretKey const& to, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - - checkTransaction(*txFrame); - auto txResult = txFrame->getResult(); - auto result = PaymentOpFrame::getInnerCode(txResult.result.results()[0]); - - REQUIRE(txResult.feeCharged == app.getLedgerManager().getTxFee()); - - AccountFrame::pointer toAccountAfter; - toAccountAfter = loadAccount(to, app, false); - - if (result != PAYMENT_SUCCESS) + if (!applyCheck(txFrame, delta, app)) { + auto toAccountAfter = loadAccount(to, app, false); // check that the target account didn't change REQUIRE(!!toAccount == !!toAccountAfter); if (toAccount && toAccountAfter) @@ -493,37 +403,12 @@ applyPaymentTx(Application& app, SecretKey const& from, SecretKey const& to, REQUIRE(toAccount->getAccount() == toAccountAfter->getAccount()); } } - else - { - REQUIRE(toAccount); - REQUIRE(toAccountAfter); - } - - switch (result) - { - case PAYMENT_MALFORMED: - throw ex_PAYMENT_MALFORMED{}; - case PAYMENT_UNDERFUNDED: - throw ex_PAYMENT_UNDERFUNDED{}; - case PAYMENT_SRC_NO_TRUST: - throw ex_PAYMENT_SRC_NO_TRUST{}; - case PAYMENT_SRC_NOT_AUTHORIZED: - throw ex_PAYMENT_SRC_NOT_AUTHORIZED{}; - case PAYMENT_NO_DESTINATION: - throw ex_PAYMENT_NO_DESTINATION{}; - case PAYMENT_NO_TRUST: - throw ex_PAYMENT_NO_TRUST{}; - case PAYMENT_NOT_AUTHORIZED: - throw ex_PAYMENT_NOT_AUTHORIZED{}; - case PAYMENT_LINE_FULL: - throw ex_PAYMENT_LINE_FULL{}; - case PAYMENT_NO_ISSUER: - throw ex_PAYMENT_NO_ISSUER{}; - default: - break; - } + throwIf(txFrame->getResult()); + checkTransaction(*txFrame); - REQUIRE(result == PAYMENT_SUCCESS); + auto toAccountAfter = loadAccount(to, app, false); + REQUIRE(toAccount); + REQUIRE(toAccountAfter); } void @@ -538,29 +423,9 @@ applyChangeTrust(Application& app, SecretKey const& from, PublicKey const& to, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - + applyCheck(txFrame, delta, app); + throwIf(txFrame->getResult()); checkTransaction(*txFrame); - - auto result = ChangeTrustOpFrame::getInnerCode( - txFrame->getResult().result.results()[0]); - switch (result) - { - case CHANGE_TRUST_MALFORMED: - throw ex_CHANGE_TRUST_MALFORMED{}; - case CHANGE_TRUST_NO_ISSUER: - throw ex_CHANGE_TRUST_NO_ISSUER{}; - case CHANGE_TRUST_INVALID_LIMIT: - throw ex_CHANGE_TRUST_INVALID_LIMIT{}; - case CHANGE_TRUST_LOW_RESERVE: - throw ex_CHANGE_TRUST_LOW_RESERVE{}; - case CHANGE_TRUST_SELF_NOT_ALLOWED: - throw ex_CHANGE_TRUST_SELF_NOT_ALLOWED{}; - default: - break; - } - - REQUIRE(result == CHANGE_TRUST_SUCCESS); } TransactionFramePtr @@ -599,40 +464,9 @@ applyCreditPaymentTx(Application& app, SecretKey const& from, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - + applyCheck(txFrame, delta, app); + throwIf(txFrame->getResult()); checkTransaction(*txFrame); - - auto& firstResult = getFirstResult(*txFrame); - - auto res = firstResult.tr().paymentResult(); - auto result = res.code(); - - switch (result) - { - case PAYMENT_MALFORMED: - throw ex_PAYMENT_MALFORMED{}; - case PAYMENT_UNDERFUNDED: - throw ex_PAYMENT_UNDERFUNDED{}; - case PAYMENT_SRC_NO_TRUST: - throw ex_PAYMENT_SRC_NO_TRUST{}; - case PAYMENT_SRC_NOT_AUTHORIZED: - throw ex_PAYMENT_SRC_NOT_AUTHORIZED{}; - case PAYMENT_NO_DESTINATION: - throw ex_PAYMENT_NO_DESTINATION{}; - case PAYMENT_NO_TRUST: - throw ex_PAYMENT_NO_TRUST{}; - case PAYMENT_NOT_AUTHORIZED: - throw ex_PAYMENT_NOT_AUTHORIZED{}; - case PAYMENT_LINE_FULL: - throw ex_PAYMENT_LINE_FULL{}; - case PAYMENT_NO_ISSUER: - throw ex_PAYMENT_NO_ISSUER{}; - default: - break; - } - - REQUIRE(result == PAYMENT_SUCCESS); } TransactionFramePtr @@ -667,8 +501,17 @@ applyPathPaymentTx(Application& app, SecretKey const& from, PublicKey const& to, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - + applyCheck(txFrame, delta, app); + try + { + throwIf(txFrame->getResult()); + } + catch (ex_PATH_PAYMENT_NO_ISSUER &) + { + REQUIRE(noIssuer); + REQUIRE(*noIssuer == txFrame->getResult().result.results()[0].tr().pathPaymentResult().noIssuer()); + throw; + } checkTransaction(*txFrame); auto& firstResult = getFirstResult(*txFrame); @@ -681,39 +524,6 @@ applyPathPaymentTx(Application& app, SecretKey const& from, PublicKey const& to, REQUIRE(!noIssuer); } - switch (result) - { - 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: - REQUIRE(noIssuer); - REQUIRE(*noIssuer == res.noIssuer()); - 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{}; - default: - break; - } - - REQUIRE(result == PATH_PAYMENT_SUCCESS); return res; } @@ -768,16 +578,12 @@ applyCreateOfferHelper(Application& app, uint64 offerId, txFrame = manageOfferOp(app.getNetworkID(), offerId, source, selling, buying, price, amount, seq); - try - { - throwingApplyCheck(txFrame, delta, app); - } - catch (...) + if (!applyCheck(txFrame, delta, app)) { REQUIRE(delta.getHeaderFrame().getLastGeneratedID() == lastGeneratedID); - throw; } + throwIf(txFrame->getResult()); checkTransaction(*txFrame); delta.commit(); @@ -787,37 +593,30 @@ applyCreateOfferHelper(Application& app, uint64 offerId, auto& manageOfferResult = results[0].tr().manageOfferResult(); - if (manageOfferResult.code() == MANAGE_OFFER_SUCCESS) - { - OfferFrame::pointer offer; + OfferFrame::pointer offer; - auto& offerResult = manageOfferResult.success().offer; + auto& offerResult = manageOfferResult.success().offer; - switch (offerResult.effect()) - { - case MANAGE_OFFER_CREATED: - case MANAGE_OFFER_UPDATED: - { - offer = - loadOffer(source.getPublicKey(), expectedOfferID, app, true); - auto& offerEntry = offer->getOffer(); - REQUIRE(offerEntry == offerResult.offer()); - REQUIRE(offerEntry.price == price); - REQUIRE(offerEntry.selling == selling); - REQUIRE(offerEntry.buying == buying); - } - break; - case MANAGE_OFFER_DELETED: - REQUIRE( - !loadOffer(source.getPublicKey(), expectedOfferID, app, false)); - break; - default: - abort(); - } - } - else + switch (offerResult.effect()) { - REQUIRE(delta.getHeaderFrame().getLastGeneratedID() == lastGeneratedID); + case MANAGE_OFFER_CREATED: + case MANAGE_OFFER_UPDATED: + { + offer = + loadOffer(source.getPublicKey(), expectedOfferID, app, true); + auto& offerEntry = offer->getOffer(); + REQUIRE(offerEntry == offerResult.offer()); + REQUIRE(offerEntry.price == price); + REQUIRE(offerEntry.selling == selling); + REQUIRE(offerEntry.buying == buying); + } + break; + case MANAGE_OFFER_DELETED: + REQUIRE( + !loadOffer(source.getPublicKey(), expectedOfferID, app, false)); + break; + default: + abort(); } return manageOfferResult; @@ -832,42 +631,8 @@ applyManageOffer(Application& app, uint64 offerId, SecretKey const& source, ManageOfferResult const& createOfferRes = applyCreateOfferHelper( app, offerId, source, selling, buying, price, amount, seq); - switch (createOfferRes.code()) - { - case MANAGE_OFFER_MALFORMED: - throw ex_MANAGE_OFFER_MALFORMED{}; - case MANAGE_OFFER_SELL_NO_TRUST: - throw ex_MANAGE_OFFER_SELL_NO_TRUST{}; - case MANAGE_OFFER_BUY_NO_TRUST: - throw ex_MANAGE_OFFER_BUY_NO_TRUST{}; - case MANAGE_OFFER_SELL_NOT_AUTHORIZED: - throw ex_MANAGE_OFFER_SELL_NOT_AUTHORIZED{}; - case MANAGE_OFFER_BUY_NOT_AUTHORIZED: - throw ex_MANAGE_OFFER_BUY_NOT_AUTHORIZED{}; - case MANAGE_OFFER_LINE_FULL: - throw ex_MANAGE_OFFER_LINE_FULL{}; - case MANAGE_OFFER_UNDERFUNDED: - throw ex_MANAGE_OFFER_UNDERFUNDED{}; - case MANAGE_OFFER_CROSS_SELF: - throw ex_MANAGE_OFFER_CROSS_SELF{}; - case MANAGE_OFFER_SELL_NO_ISSUER: - throw ex_MANAGE_OFFER_SELL_NO_ISSUER{}; - case MANAGE_OFFER_BUY_NO_ISSUER: - throw ex_MANAGE_OFFER_BUY_NO_ISSUER{}; - case MANAGE_OFFER_NOT_FOUND: - throw ex_MANAGE_OFFER_NOT_FOUND{}; - case MANAGE_OFFER_LOW_RESERVE: - throw ex_MANAGE_OFFER_LOW_RESERVE{}; - default: - break; - } - - REQUIRE(createOfferRes.code() == MANAGE_OFFER_SUCCESS); - auto& success = createOfferRes.success().offer; - REQUIRE(success.effect() == expectedEffect); - return success.effect() == MANAGE_OFFER_CREATED ? success.offer().offerID : 0; } @@ -887,16 +652,11 @@ applyCreatePassiveOffer(Application& app, SecretKey const& source, txFrame = createPassiveOfferOp(app.getNetworkID(), source, selling, buying, price, amount, seq); - try - { - throwingApplyCheck(txFrame, delta, app); - } - catch (...) + if (!applyCheck(txFrame, delta, app)) { REQUIRE(delta.getHeaderFrame().getLastGeneratedID() == lastGeneratedID); - throw; } - + throwIf(txFrame->getResult()); checkTransaction(*txFrame); delta.commit(); @@ -936,43 +696,6 @@ applyCreatePassiveOffer(Application& app, SecretKey const& source, } } - if (createPassiveOfferResult.code() != MANAGE_OFFER_SUCCESS) - { - REQUIRE(delta.getHeaderFrame().getLastGeneratedID() == lastGeneratedID); - } - - switch (createPassiveOfferResult.code()) - { - case MANAGE_OFFER_MALFORMED: - throw ex_MANAGE_OFFER_MALFORMED{}; - case MANAGE_OFFER_SELL_NO_TRUST: - throw ex_MANAGE_OFFER_SELL_NO_TRUST{}; - case MANAGE_OFFER_BUY_NO_TRUST: - throw ex_MANAGE_OFFER_BUY_NO_TRUST{}; - case MANAGE_OFFER_SELL_NOT_AUTHORIZED: - throw ex_MANAGE_OFFER_SELL_NOT_AUTHORIZED{}; - case MANAGE_OFFER_BUY_NOT_AUTHORIZED: - throw ex_MANAGE_OFFER_BUY_NOT_AUTHORIZED{}; - case MANAGE_OFFER_LINE_FULL: - throw ex_MANAGE_OFFER_LINE_FULL{}; - case MANAGE_OFFER_UNDERFUNDED: - throw ex_MANAGE_OFFER_UNDERFUNDED{}; - case MANAGE_OFFER_CROSS_SELF: - throw ex_MANAGE_OFFER_CROSS_SELF{}; - case MANAGE_OFFER_SELL_NO_ISSUER: - throw ex_MANAGE_OFFER_SELL_NO_ISSUER{}; - case MANAGE_OFFER_BUY_NO_ISSUER: - throw ex_MANAGE_OFFER_BUY_NO_ISSUER{}; - case MANAGE_OFFER_NOT_FOUND: - throw ex_MANAGE_OFFER_NOT_FOUND{}; - case MANAGE_OFFER_LOW_RESERVE: - throw ex_MANAGE_OFFER_LOW_RESERVE{}; - default: - break; - } - - REQUIRE(createPassiveOfferResult.code() == MANAGE_OFFER_SUCCESS); - auto& success = createPassiveOfferResult.success().offer; REQUIRE(success.effect() == expectedEffect); @@ -1064,37 +787,9 @@ applySetOptions(Application& app, SecretKey const& source, SequenceNumber seq, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - + applyCheck(txFrame, delta, app); + throwIf(txFrame->getResult()); checkTransaction(*txFrame); - auto result = SetOptionsOpFrame::getInnerCode( - txFrame->getResult().result.results()[0]); - - switch (result) - { - case SET_OPTIONS_LOW_RESERVE: - throw ex_SET_OPTIONS_LOW_RESERVE{}; - case SET_OPTIONS_TOO_MANY_SIGNERS: - throw ex_SET_OPTIONS_TOO_MANY_SIGNERS{}; - case SET_OPTIONS_BAD_FLAGS: - throw ex_SET_OPTIONS_BAD_FLAGS{}; - case SET_OPTIONS_INVALID_INFLATION: - throw ex_SET_OPTIONS_INVALID_INFLATION{}; - case SET_OPTIONS_CANT_CHANGE: - throw ex_SET_OPTIONS_CANT_CHANGE{}; - case SET_OPTIONS_UNKNOWN_FLAG: - throw ex_SET_OPTIONS_UNKNOWN_FLAG{}; - case SET_OPTIONS_THRESHOLD_OUT_OF_RANGE: - throw ex_SET_OPTIONS_THRESHOLD_OUT_OF_RANGE{}; - case SET_OPTIONS_BAD_SIGNER: - throw ex_SET_OPTIONS_BAD_SIGNER{}; - case SET_OPTIONS_INVALID_HOME_DOMAIN: - throw ex_SET_OPTIONS_INVALID_HOME_DOMAIN{}; - default: - break; - } - - REQUIRE(SET_OPTIONS_SUCCESS == result); } Operation @@ -1114,8 +809,7 @@ createInflation(Hash const& networkID, SecretKey const& from, } OperationResult -applyInflation(Application& app, SecretKey const& from, SequenceNumber seq, - InflationResultCode result) +applyInflation(Application& app, SecretKey const& from, SequenceNumber seq) { TransactionFramePtr txFrame = createInflation(app.getNetworkID(), from, seq); @@ -1123,10 +817,8 @@ applyInflation(Application& app, SecretKey const& from, SequenceNumber seq, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); bool res = applyCheck(txFrame, delta, app); - + throwIf(txFrame->getResult()); checkTransaction(*txFrame); - REQUIRE(InflationOpFrame::getInnerCode( - txFrame->getResult().result.results()[0]) == result); if (res) { delta.commit(); @@ -1164,25 +856,9 @@ applyAccountMerge(Application& app, SecretKey const& source, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - - auto result = - MergeOpFrame::getInnerCode(txFrame->getResult().result.results()[0]); - switch (result) - { - case ACCOUNT_MERGE_MALFORMED: - throw ex_ACCOUNT_MERGE_MALFORMED{}; - case ACCOUNT_MERGE_NO_ACCOUNT: - throw ex_ACCOUNT_MERGE_NO_ACCOUNT{}; - case ACCOUNT_MERGE_IMMUTABLE_SET: - throw ex_ACCOUNT_MERGE_IMMUTABLE_SET{}; - case ACCOUNT_MERGE_HAS_SUB_ENTRIES: - throw ex_ACCOUNT_MERGE_HAS_SUB_ENTRIES{}; - default: - break; - } - - REQUIRE(result == ACCOUNT_MERGE_SUCCESS); + applyCheck(txFrame, delta, app); + throwIf(txFrame->getResult()); + checkTransaction(*txFrame); } TransactionFramePtr @@ -1207,23 +883,9 @@ applyManageData(Application& app, SecretKey const& source, LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader(), app.getDatabase()); - throwingApplyCheck(txFrame, delta, app); - - auto result = ManageDataOpFrame::getInnerCode( - txFrame->getResult().result.results()[0]); - switch (result) - { - case MANAGE_DATA_NOT_SUPPORTED_YET: - throw ex_MANAGE_DATA_NOT_SUPPORTED_YET{}; - case MANAGE_DATA_NAME_NOT_FOUND: - throw ex_MANAGE_DATA_NAME_NOT_FOUND{}; - case MANAGE_DATA_LOW_RESERVE: - throw ex_MANAGE_DATA_LOW_RESERVE{}; - case MANAGE_DATA_INVALID_NAME: - throw ex_MANAGE_DATA_INVALID_NAME{}; - default: - break; - } + applyCheck(txFrame, delta, app); + throwIf(txFrame->getResult()); + checkTransaction(*txFrame); auto dataFrame = DataFrame::loadData(source.getPublicKey(), name, app.getDatabase()); @@ -1256,19 +918,6 @@ getFirstResultCode(TransactionFrame const& tx) return getFirstOperationFrame(tx).getResultCode(); } -Operation& -getFirstOperation(TransactionFrame& tx) -{ - return tx.getEnvelope().tx.operations[0]; -} - -void -reSignTransaction(TransactionFrame& tx, SecretKey const& source) -{ - tx.getEnvelope().signatures.clear(); - tx.addSignature(source); -} - void checkAmounts(int64_t a, int64_t b, int64_t maxd) { diff --git a/src/test/TxTests.h b/src/test/TxTests.h index 8bfeeccc69..af0f6a57a0 100644 --- a/src/test/TxTests.h +++ b/src/test/TxTests.h @@ -33,7 +33,6 @@ struct ThresholdSetter optional highThreshold; }; -bool throwingApplyCheck(TransactionFramePtr tx, LedgerDelta& delta, Application& app); bool applyCheck(TransactionFramePtr tx, LedgerDelta& delta, Application& app); TxSetResultMeta closeLedgerOn(Application& app, uint32 ledgerSeq, int day, @@ -57,10 +56,6 @@ OfferFrame::pointer loadOffer(PublicKey const& k, uint64 offerID, TrustFrame::pointer loadTrustLine(SecretKey const& k, Asset const& asset, Application& app, bool mustExist = true); -SequenceNumber getAccountSeqNum(SecretKey const& k, Application& app); - -int64_t getAccountBalance(SecretKey const& k, Application& app); - xdr::xvector getAccountSigners(SecretKey const& k, Application& app); @@ -176,8 +171,7 @@ Operation createInflationOp(); TransactionFramePtr createInflation(Hash const& networkID, SecretKey const& from, SequenceNumber seq); OperationResult -applyInflation(Application& app, SecretKey const& from, SequenceNumber seq, - InflationResultCode targetResult = INFLATION_SUCCESS); +applyInflation(Application& app, SecretKey const& from, SequenceNumber seq); Operation createMergeOp(SecretKey const* from, PublicKey const& dest); @@ -207,11 +201,6 @@ OperationFrame const& getFirstOperationFrame(TransactionFrame const& tx); OperationResult const& getFirstResult(TransactionFrame const& tx); OperationResultCode getFirstResultCode(TransactionFrame const& tx); -// modifying the type of the operation will lead to undefined behavior -Operation& getFirstOperation(TransactionFrame& tx); - -void reSignTransaction(TransactionFrame& tx, SecretKey const& source); - // checks that b-maxd <= a <= b // bias towards seller means // * amount left in an offer should be higher than the exact calculation diff --git a/src/transactions/InflationTests.cpp b/src/transactions/InflationTests.cpp index a84487954c..dd453a7616 100644 --- a/src/transactions/InflationTests.cpp +++ b/src/transactions/InflationTests.cpp @@ -9,6 +9,7 @@ #include "main/Application.h" #include "main/Config.h" #include "test/TestAccount.h" +#include "test/TestExceptions.h" #include "test/TestUtils.h" #include "test/TxTests.h" #include "test/test.h" @@ -295,8 +296,8 @@ TEST_CASE("inflation", "[tx][inflation]") { for_all_versions(app, [&]{ closeLedgerOn(app, 2, 30, 6, 2014); - applyInflation(app, root, root.nextSequenceNumber(), - INFLATION_NOT_TIME); + REQUIRE_THROWS_AS(applyInflation(app, root, root.nextSequenceNumber()), + ex_INFLATION_NOT_TIME); REQUIRE(app.getLedgerManager().getCurrentLedgerHeader().inflationSeq == 0); @@ -310,30 +311,30 @@ TEST_CASE("inflation", "[tx][inflation]") REQUIRE(app.getLedgerManager().getCurrentLedgerHeader().inflationSeq == 1); - applyInflation(app, root, root.nextSequenceNumber(), - INFLATION_NOT_TIME); + REQUIRE_THROWS_AS(applyInflation(app, root, root.nextSequenceNumber()), + ex_INFLATION_NOT_TIME); REQUIRE(app.getLedgerManager().getCurrentLedgerHeader().inflationSeq == 1); closeLedgerOn(app, 5, 8, 7, 2014); - applyInflation(app, root, root.nextSequenceNumber(), INFLATION_SUCCESS); + applyInflation(app, root, root.nextSequenceNumber()); REQUIRE(app.getLedgerManager().getCurrentLedgerHeader().inflationSeq == 2); closeLedgerOn(app, 6, 14, 7, 2014); - applyInflation(app, root, root.nextSequenceNumber(), - INFLATION_NOT_TIME); + REQUIRE_THROWS_AS(applyInflation(app, root, root.nextSequenceNumber()), + ex_INFLATION_NOT_TIME); REQUIRE(app.getLedgerManager().getCurrentLedgerHeader().inflationSeq == 2); closeLedgerOn(app, 7, 15, 7, 2014); - applyInflation(app, root, root.nextSequenceNumber(), INFLATION_SUCCESS); + applyInflation(app, root, root.nextSequenceNumber()); REQUIRE(app.getLedgerManager().getCurrentLedgerHeader().inflationSeq == 3); closeLedgerOn(app, 8, 21, 7, 2014); - applyInflation(app, root, root.nextSequenceNumber(), - INFLATION_NOT_TIME); + REQUIRE_THROWS_AS(applyInflation(app, root, root.nextSequenceNumber()), + ex_INFLATION_NOT_TIME); REQUIRE(app.getLedgerManager().getCurrentLedgerHeader().inflationSeq == 3); }); @@ -351,7 +352,7 @@ TEST_CASE("inflation", "[tx][inflation]") auto target2 = TestAccount{app, getAccount("target2"), 0}; auto minBalance = app.getLedgerManager().getMinBalance(0); - auto rootBalance = getAccountBalance(root, app); + auto rootBalance = root.getBalance(); auto voter1tx = root.tx({createCreateAccountOp(nullptr, voter1, rootBalance / 6)}); voter1tx->getEnvelope().tx.fee = 999999999; @@ -376,11 +377,11 @@ TEST_CASE("inflation", "[tx][inflation]") REQUIRE(clh.feePool == 1000000499); REQUIRE(clh.totalCoins == 1000000000000000000); - auto beforeInflationRoot = getAccountBalance(root, app); - auto beforeInflationVoter1 = getAccountBalance(voter1, app); - auto beforeInflationVoter2 = getAccountBalance(voter2, app); - auto beforeInflationTarget1 = getAccountBalance(target1, app); - auto beforeInflationTarget2 = getAccountBalance(target2, app); + auto beforeInflationRoot = root.getBalance(); + auto beforeInflationVoter1 = voter1.getBalance(); + auto beforeInflationVoter2 = voter2.getBalance(); + auto beforeInflationTarget1 = target1.getBalance(); + auto beforeInflationTarget2 = target2.getBalance(); REQUIRE( beforeInflationRoot + @@ -399,11 +400,11 @@ TEST_CASE("inflation", "[tx][inflation]") REQUIRE(clh.feePool == 95361000000301); REQUIRE(clh.totalCoins == 1000095361000000298); - auto afterInflationRoot = getAccountBalance(root, app); - auto afterInflationVoter1 = getAccountBalance(voter1, app); - auto afterInflationVoter2 = getAccountBalance(voter2, app); - auto afterInflationTarget1 = getAccountBalance(target1, app); - auto afterInflationTarget2 = getAccountBalance(target2, app); + auto afterInflationRoot = root.getBalance(); + auto afterInflationVoter1 = voter1.getBalance(); + auto afterInflationVoter2 = voter2.getBalance(); + auto afterInflationTarget1 = target1.getBalance(); + auto afterInflationTarget2 = target2.getBalance(); auto inflationError = 95359999999702; REQUIRE(beforeInflationRoot == afterInflationRoot + 100); @@ -428,11 +429,11 @@ TEST_CASE("inflation", "[tx][inflation]") REQUIRE(clh.feePool == 95361000000301); REQUIRE(clh.totalCoins == 1000190721000000000); - auto afterInflationRoot = getAccountBalance(root, app); - auto afterInflationVoter1 = getAccountBalance(voter1, app); - auto afterInflationVoter2 = getAccountBalance(voter2, app); - auto afterInflationTarget1 = getAccountBalance(target1, app); - auto afterInflationTarget2 = getAccountBalance(target2, app); + auto afterInflationRoot = root.getBalance(); + auto afterInflationVoter1 = voter1.getBalance(); + auto afterInflationVoter2 = voter2.getBalance(); + auto afterInflationTarget1 = target1.getBalance(); + auto afterInflationTarget2 = target2.getBalance(); REQUIRE(beforeInflationRoot == afterInflationRoot + 100); REQUIRE(beforeInflationVoter1 == afterInflationVoter1); diff --git a/src/transactions/MergeTests.cpp b/src/transactions/MergeTests.cpp index 4b3f0aa750..27612d299c 100644 --- a/src/transactions/MergeTests.cpp +++ b/src/transactions/MergeTests.cpp @@ -76,8 +76,8 @@ TEST_CASE("merge", "[tx][merge]") SECTION("with create") { - int64 a1Balance = getAccountBalance(a1, app); - int64 b1Balance = getAccountBalance(b1, app); + int64 a1Balance = a1.getBalance(); + int64 b1Balance = b1.getBalance(); auto createBalance = app.getLedgerManager().getMinBalance(1); auto txFrame = a1.tx({createMergeOp(&a1.getSecretKey(), b1), @@ -93,7 +93,7 @@ TEST_CASE("merge", "[tx][merge]") txFrame->getResult().result.results()[2]); REQUIRE(result == ACCOUNT_MERGE_SUCCESS); - REQUIRE(getAccountBalance(b1, app) == + REQUIRE(b1.getBalance() == 2 * a1Balance + b1Balance - createBalance - 2 * txFrame->getFee()); }); @@ -105,21 +105,21 @@ TEST_CASE("merge", "[tx][merge]") txFrame->getResult().result.results()[2]); REQUIRE(result == ACCOUNT_MERGE_SUCCESS); - REQUIRE(getAccountBalance(b1, app) == + REQUIRE(b1.getBalance() == a1Balance + b1Balance - txFrame->getFee()); }); } SECTION("merge account twice") { - int64 a1Balance = getAccountBalance(a1, app); - int64 b1Balance = getAccountBalance(b1, app); + int64 a1Balance = a1.getBalance(); + int64 b1Balance = b1.getBalance(); auto txFrame = a1.tx({createMergeOp(nullptr, b1), createMergeOp(nullptr, b1)}); for_versions_to(4, app, [&]{ - applyCheck(txFrame, delta, app); + REQUIRE(applyCheck(txFrame, delta, app)); auto result = MergeOpFrame::getInnerCode( txFrame->getResult().result.results()[1]); @@ -127,24 +127,25 @@ TEST_CASE("merge", "[tx][merge]") auto a1BalanceAfterFee = a1Balance - txFrame->getFee(); REQUIRE(result == ACCOUNT_MERGE_SUCCESS); REQUIRE(b1Balance + a1BalanceAfterFee + a1BalanceAfterFee == - getAccountBalance(b1, app)); + b1.getBalance()); REQUIRE(!loadAccount(a1, app, false)); }); for_versions(5, 7, app, [&]{ - applyCheck(txFrame, delta, app); + REQUIRE(!applyCheck(txFrame, delta, app)); auto result = MergeOpFrame::getInnerCode( txFrame->getResult().result.results()[1]); REQUIRE(result == ACCOUNT_MERGE_NO_ACCOUNT); - REQUIRE(b1Balance == getAccountBalance(b1, app)); + REQUIRE(b1Balance == b1.getBalance()); REQUIRE((a1Balance - txFrame->getFee()) == - getAccountBalance(a1, app)); + a1.getBalance()); }); for_versions_from(8, app, [&]{ - throwingApplyCheck(txFrame, delta, app); + REQUIRE(!applyCheck(txFrame, delta, app)); + REQUIRE(txFrame->getResult().result.results()[1].code() == opNO_ACCOUNT); }); } @@ -207,7 +208,7 @@ TEST_CASE("merge", "[tx][merge]") std::string t1("test"); - applyManageData(app, a1, t1, &value, a1.nextSequenceNumber()); + a1.manageData(t1, &value); REQUIRE_THROWS_AS(a1.merge(b1), ex_ACCOUNT_MERGE_HAS_SUB_ENTRIES); }); } @@ -230,8 +231,8 @@ TEST_CASE("merge", "[tx][merge]") a1.nextSequenceNumber()); auto tx2 = createPaymentTx(app.getNetworkID(), a1, root, a1.nextSequenceNumber(), 100); - int64 a1Balance = getAccountBalance(a1, app); - int64 b1Balance = getAccountBalance(b1, app); + int64 a1Balance = a1.getBalance(); + int64 b1Balance = b1.getBalance(); auto r = closeLedgerOn(app, 2, 1, 1, 2015, {tx1, tx2}); checkTx(0, r, txSUCCESS); checkTx(1, r, txNO_ACCOUNT); @@ -241,7 +242,7 @@ TEST_CASE("merge", "[tx][merge]") int64 expectedB1Balance = a1Balance + b1Balance - 2 * app.getLedgerManager().getTxFee(); - REQUIRE(expectedB1Balance == getAccountBalance(b1, app)); + REQUIRE(expectedB1Balance == b1.getBalance()); }); } } diff --git a/src/transactions/PaymentTests.cpp b/src/transactions/PaymentTests.cpp index d9335c01ba..564eb37ab3 100644 --- a/src/transactions/PaymentTests.cpp +++ b/src/transactions/PaymentTests.cpp @@ -188,8 +188,8 @@ TEST_CASE("payment", "[tx][payment]") auto amount = app.getLedgerManager().getMinBalance(0) + payment; auto b1 = root.create("B", amount); - int64 a1Balance = getAccountBalance(a1, app); - int64 b1Balance = getAccountBalance(b1, app); + int64 a1Balance = a1.getBalance(); + int64 b1Balance = b1.getBalance(); auto txFrame = a1.tx( {createPaymentOp(nullptr, b1, 200), createMergeOp(nullptr, b1)}); @@ -201,7 +201,7 @@ TEST_CASE("payment", "[tx][payment]") REQUIRE(!loadAccount(a1, app, false)); REQUIRE((a1Balance + b1Balance - txFrame->getFee()) == - getAccountBalance(b1, app)); + b1.getBalance()); }); } @@ -211,8 +211,8 @@ TEST_CASE("payment", "[tx][payment]") auto amount = app.getLedgerManager().getMinBalance(0) + payment; auto b1 = root.create("B", amount); - int64 a1Balance = getAccountBalance(a1, app); - int64 b1Balance = getAccountBalance(b1, app); + int64 a1Balance = a1.getBalance(); + int64 b1Balance = b1.getBalance(); auto txFrame = a1.tx({createPaymentOp(nullptr, b1, 200), createMergeOp(&b1.getSecretKey(), a1)}); @@ -225,7 +225,7 @@ TEST_CASE("payment", "[tx][payment]") REQUIRE(!loadAccount(b1, app, false)); REQUIRE((a1Balance + b1Balance - txFrame->getFee()) == - getAccountBalance(a1, app)); + a1.getBalance()); }); } @@ -233,8 +233,8 @@ TEST_CASE("payment", "[tx][payment]") { auto b1 = root.create("B", app.getLedgerManager().getMinBalance(0)); - int64 a1Balance = getAccountBalance(a1, app); - int64 b1Balance = getAccountBalance(b1, app); + int64 a1Balance = a1.getBalance(); + int64 b1Balance = b1.getBalance(); auto txFrame = a1.tx( {createMergeOp(nullptr, b1), createPaymentOp(nullptr, b1, 200)}); @@ -244,8 +244,8 @@ TEST_CASE("payment", "[tx][payment]") REQUIRE(txFrame->getResultCode() == txINTERNAL_ERROR); - REQUIRE(b1Balance == getAccountBalance(b1, app)); - REQUIRE((a1Balance - txFrame->getFee()) == getAccountBalance(a1, app)); + REQUIRE(b1Balance == b1.getBalance()); + REQUIRE((a1Balance - txFrame->getFee()) == a1.getBalance()); }); for_versions_from(8, app, [&]{ @@ -253,8 +253,8 @@ TEST_CASE("payment", "[tx][payment]") REQUIRE(txFrame->getResultCode() == txFAILED); - REQUIRE(b1Balance == getAccountBalance(b1, app)); - REQUIRE((a1Balance - txFrame->getFee()) == getAccountBalance(a1, app)); + REQUIRE(b1Balance == b1.getBalance()); + REQUIRE((a1Balance - txFrame->getFee()) == a1.getBalance()); }); } @@ -332,15 +332,15 @@ TEST_CASE("payment", "[tx][payment]") auto tx2 = createPaymentTx(app.getNetworkID(), b1, root, b1.nextSequenceNumber(), 6); - int64 rootBalance = getAccountBalance(root, app); + int64 rootBalance = root.getBalance(); auto r = closeLedgerOn(app, 2, 1, 1, 2015, {tx1, tx2}); checkTx(0, r, txSUCCESS); checkTx(1, r, txINSUFFICIENT_BALANCE); int64 expectedrootBalance = rootBalance + paymentAmount; int64 expectedb1Balance = app.getLedgerManager().getMinBalance(0) + 5; - REQUIRE(expectedb1Balance == getAccountBalance(b1, app)); - REQUIRE(expectedrootBalance == getAccountBalance(root, app)); + REQUIRE(expectedb1Balance == b1.getBalance()); + REQUIRE(expectedrootBalance == root.getBalance()); }); } @@ -350,9 +350,9 @@ TEST_CASE("payment", "[tx][payment]") auto sourceAccount = root.create("source", amount); auto secondSourceAccount = root.create("secondSource", amount); auto payAndMergeDestination = root.create("payAndMerge", amount); - auto balanceBefore = getAccountBalance(sourceAccount, app) - + getAccountBalance(secondSourceAccount, app) - + getAccountBalance(payAndMergeDestination, app); + auto balanceBefore = sourceAccount.getBalance() + + secondSourceAccount.getBalance() + + payAndMergeDestination.getBalance(); auto tx = sourceAccount.tx({ createPaymentOp(nullptr, payAndMergeDestination, 500000000), @@ -363,18 +363,18 @@ TEST_CASE("payment", "[tx][payment]") tx->addSignature(secondSourceAccount); for_versions_to(7, app, [&]{ - throwingApplyCheck(tx, delta, app); - auto balanceAfter = getAccountBalance(sourceAccount, app) - + getAccountBalance(secondSourceAccount, app) - + getAccountBalance(payAndMergeDestination, app); + REQUIRE(applyCheck(tx, delta, app)); + auto balanceAfter = sourceAccount.getBalance() + + secondSourceAccount.getBalance() + + payAndMergeDestination.getBalance(); REQUIRE(balanceBefore + amount - 1000000800 == balanceAfter); }); for_versions_from(8, app, [&]{ - throwingApplyCheck(tx, delta, app); - auto balanceAfter = getAccountBalance(sourceAccount, app) - + getAccountBalance(secondSourceAccount, app) - + getAccountBalance(payAndMergeDestination, app); + REQUIRE(applyCheck(tx, delta, app)); + auto balanceAfter = sourceAccount.getBalance() + + secondSourceAccount.getBalance() + + payAndMergeDestination.getBalance(); REQUIRE(balanceBefore - 400 == balanceAfter); }); } @@ -500,17 +500,17 @@ TEST_CASE("payment", "[tx][payment]") SECTION("path payment to self XLM") { - auto a1Balance = getAccountBalance(a1, app); + auto a1Balance = a1.getBalance(); auto amount = a1Balance / 10; for_versions_to(7, app, [&]{ a1.pay(a1, xlmCur, amount, xlmCur, amount, {}); - REQUIRE(getAccountBalance(a1, app) == a1Balance - amount - txfee); + REQUIRE(a1.getBalance() == a1Balance - amount - txfee); }); for_versions_from(8, app, [&]{ a1.pay(a1, xlmCur, amount, xlmCur, amount, {}); - REQUIRE(getAccountBalance(a1, app) == a1Balance - txfee); + REQUIRE(a1.getBalance() == a1Balance - txfee); }); } @@ -1158,12 +1158,12 @@ TEST_CASE("payment", "[tx][payment]") return account; }; - auto validateAccountAsset = [&](const SecretKey& account, + auto validateAccountAsset = [&](const TestAccount& account, int assetIndex, int difference, int feeCount) { if (assets[assetIndex].type() == ASSET_TYPE_NATIVE) { - REQUIRE(getAccountBalance(account, app) == + REQUIRE(account.getBalance() == initialBalance + difference - feeCount * txFee); } else @@ -1173,7 +1173,7 @@ TEST_CASE("payment", "[tx][payment]") initialBalance + difference); } }; - auto validateAccountAssets = [&](const SecretKey& account, + auto validateAccountAssets = [&](const TestAccount& account, int assetIndex, int difference, int feeCount) { for (size_t i = 0; i < pathSize; i++)