void applyAllowTrust(Application& app, SecretKey& from, SecretKey& trustor, SequenceNumber seq, std::string const& currencyCode, bool authorize, AllowTrustResultCode result) { TransactionFramePtr txFrame; txFrame = createAllowTrust(from, trustor, seq, currencyCode, authorize); LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader()); txFrame->apply(delta, app); checkTransaction(*txFrame); REQUIRE(AllowTrustOpFrame::getInnerCode( txFrame->getResult().result.results()[0]) == result); }
void applyChangeTrust(Application& app, SecretKey& from, SecretKey& to, SequenceNumber seq, std::string const& currencyCode, int64_t limit, ChangeTrustResultCode result) { TransactionFramePtr txFrame; txFrame = createChangeTrust(from, to, seq, currencyCode, limit); LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader()); txFrame->apply(delta, app); checkTransaction(*txFrame); REQUIRE(ChangeTrustOpFrame::getInnerCode( txFrame->getResult().result.results()[0]) == result); }
void applySetOptions(Application& app, SecretKey& source, AccountID* inflationDest, uint32_t* setFlags, uint32_t* clearFlags, Thresholds* thrs, Signer* signer, SequenceNumber seq, SetOptionsResultCode result) { TransactionFramePtr txFrame; txFrame = createSetOptions(source, inflationDest, setFlags, clearFlags, thrs, signer, seq); LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader()); txFrame->apply(delta, app); checkTransaction(*txFrame); REQUIRE(SetOptionsOpFrame::getInnerCode( txFrame->getResult().result.results()[0]) == result); }
void applyPaymentTx(Application& app, SecretKey& from, SecretKey& to, SequenceNumber seq, int64_t amount, PaymentResultCode result) { TransactionFramePtr txFrame; AccountFrame fromAccount; AccountFrame toAccount; bool beforeToExists = AccountFrame::loadAccount( to.getPublicKey(), toAccount, app.getDatabase()); REQUIRE(AccountFrame::loadAccount(from.getPublicKey(), fromAccount, app.getDatabase())); txFrame = createPaymentTx(from, to, seq, amount); LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader()); txFrame->apply(delta, app); checkTransaction(*txFrame); auto txResult = txFrame->getResult(); auto innerCode = PaymentOpFrame::getInnerCode(txResult.result.results()[0]); REQUIRE(innerCode == result); REQUIRE(txResult.feeCharged == app.getLedgerManager().getTxFee()); AccountFrame toAccountAfter; bool afterToExists = AccountFrame::loadAccount( to.getPublicKey(), toAccountAfter, app.getDatabase()); if (!(innerCode == PAYMENT_SUCCESS || innerCode == PAYMENT_SUCCESS_MULTI)) { // check that the target account didn't change REQUIRE(beforeToExists == afterToExists); if (beforeToExists && afterToExists) { REQUIRE(memcmp(&toAccount.getAccount(), &toAccountAfter.getAccount(), sizeof(AccountEntry)) == 0); } } else { REQUIRE(afterToExists); } }
OperationResult applyInflation(Application& app, SecretKey& from, SequenceNumber seq, InflationResultCode result) { TransactionFramePtr txFrame = createInflation(from, seq); LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader()); bool res = txFrame->apply(delta, app); checkTransaction(*txFrame); REQUIRE(InflationOpFrame::getInnerCode( txFrame->getResult().result.results()[0]) == result); if (res) { delta.commit(); } return getFirstResult(*txFrame); }
PaymentResult applyCreditPaymentTx(Application& app, SecretKey& from, SecretKey& to, Currency& ci, SequenceNumber seq, int64_t amount, PaymentResultCode result, std::vector<Currency>* path) { TransactionFramePtr txFrame; txFrame = createCreditPaymentTx(from, to, ci, seq, amount, path); LedgerDelta delta(app.getLedgerManager().getCurrentLedgerHeader()); txFrame->apply(delta, app); checkTransaction(*txFrame); auto& firstResult = getFirstResult(*txFrame); PaymentResult res = firstResult.tr().paymentResult(); auto resCode = res.code(); REQUIRE(resCode == result); return res; }
static void doInflation(Application& app, int nbAccounts, std::function<int64(int)> getBalance, std::function<int(int)> getVote, int expectedWinnerCount) { // simulate the expected inflation based off the current ledger state std::map<int, int64> balances; // load account balances for (int i = 0; i < nbAccounts; i++) { AccountFrame act; if (getBalance(i) < 0) { balances[i] = -1; REQUIRE(!AccountFrame::loadAccount(getTestAccount(i).getPublicKey(), act, app.getDatabase())); } else { REQUIRE(AccountFrame::loadAccount(getTestAccount(i).getPublicKey(), act, app.getDatabase())); balances[i] = act.getBalance(); // double check that inflationDest is setup properly if (act.getAccount().inflationDest) { REQUIRE(getTestAccount(getVote(i)).getPublicKey() == *act.getAccount().inflationDest); } else { REQUIRE(getVote(i) < 0); } } } LedgerManager& lm = app.getLedgerManager(); LedgerHeader& cur = lm.getCurrentLedgerHeader(); cur.feePool = 10000; int64 expectedTotcoins = cur.totalCoins; int64 expectedFees = cur.feePool; std::vector<int64> expectedBalances; auto root = getRoot(); TransactionFramePtr txFrame = createInflation(root, getAccountSeqNum(root, app) + 1); expectedFees += txFrame->getFee(app); expectedBalances = simulateInflation(nbAccounts, expectedTotcoins, expectedFees, [&](int i) { return balances[i]; }, getVote); // perform actual inflation { LedgerDelta delta(lm.getCurrentLedgerHeader()); REQUIRE(txFrame->apply(delta, app)); delta.commit(); } // verify ledger state LedgerHeader& cur2 = lm.getCurrentLedgerHeader(); REQUIRE(cur2.totalCoins == expectedTotcoins); REQUIRE(cur2.feePool == expectedFees); // verify balances InflationResult const& infResult = getFirstResult(*txFrame).tr().inflationResult(); auto const& payouts = infResult.payouts(); int actualChanges = 0; for (int i = 0; i < nbAccounts; i++) { AccountFrame act; auto const& pk = getTestAccount(i).getPublicKey(); if (expectedBalances[i] < 0) { REQUIRE(!AccountFrame::loadAccount(pk, act, app.getDatabase())); REQUIRE(balances[i] < 0); // account didn't get deleted } else { REQUIRE(AccountFrame::loadAccount(pk, act, app.getDatabase())); REQUIRE(expectedBalances[i] == act.getBalance()); if (expectedBalances[i] != balances[i]) { REQUIRE(balances[i] >= 0); actualChanges++; bool found = false; for (auto const& p : payouts) { if (p.destination == pk) { int64 computedFromResult = balances[i] + p.amount; REQUIRE(computedFromResult == expectedBalances[i]); found = true; break; } } REQUIRE(found); } } } REQUIRE(actualChanges == expectedWinnerCount); REQUIRE(expectedWinnerCount == payouts.size()); }
static CreateOfferResult applyCreateOfferHelper(Application& app, LedgerDelta& delta, uint64 offerId, SecretKey& source, Currency& takerGets, Currency& takerPays, Price const& price, int64_t amount, SequenceNumber seq) { uint64_t expectedOfferID = delta.getHeaderFrame().getLastGeneratedID() + 1; if (offerId != 0) { expectedOfferID = offerId; } TransactionFramePtr txFrame; txFrame = createOfferOp(offerId, source, takerGets, takerPays, price, amount, seq); txFrame->apply(delta, app); checkTransaction(*txFrame); auto& results = txFrame->getResult().result.results(); REQUIRE(results.size() == 1); auto& createOfferResult = results[0].tr().createOfferResult(); if (createOfferResult.code() == CREATE_OFFER_SUCCESS) { OfferFrame offer; auto& offerResult = createOfferResult.success().offer; auto& offerEntry = offer.getOffer(); switch (offerResult.effect()) { case CREATE_OFFER_CREATED: case CREATE_OFFER_UPDATED: REQUIRE(OfferFrame::loadOffer(source.getPublicKey(), expectedOfferID, offer, app.getDatabase())); REQUIRE(memcmp(&offerEntry, &offerResult.offer(), sizeof(OfferEntry)) == 0); REQUIRE(offerEntry.price == price); REQUIRE(memcmp(&offerEntry.takerGets, &takerGets, sizeof(Currency)) == 0); REQUIRE(memcmp(&offerEntry.takerPays, &takerPays, sizeof(Currency)) == 0); break; case CREATE_OFFER_DELETED: REQUIRE(!OfferFrame::loadOffer(source.getPublicKey(), expectedOfferID, offer, app.getDatabase())); break; default: abort(); } } return createOfferResult; }