diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 9d25ad692..9b3dfe58e 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -349,22 +349,16 @@ HerderImpl::processExternalized(uint64 slotIndex, StellarValue const& value, releaseAssert(externalizedSet != nullptr); // Notify overlay to clear TXs from mempool (for RustOverlayManager) - // Extract TX hashes from the externalized set so Rust can remove them + // Extract TX hashes from the externalized set so Rust can remove them. + // Hash the envelopes in place: building a frame per transaction would + // copy every envelope on the main thread only to hash it. std::vector txHashes; if (externalizedSet) { - auto txFramesList = - externalizedSet->createTransactionFrames(mApp.getNetworkID()); - for (auto const& txPhase : txFramesList) - { - for (auto const& txFrame : txPhase) - { - txHashes.push_back(txFrame->getFullHash()); - } - } + txHashes = externalizedSet->getTransactionFullHashes(); #ifdef BUILD_TESTS mApp.getLoadGenerator().cleanupAccounts( - static_cast(slotIndex), txFramesList); + static_cast(slotIndex), *externalizedSet); #endif } mApp.getOverlayManager().notifyTxSetExternalized(value.txSetHash, txHashes); diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index 03b391422..c8b514eb9 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -1367,6 +1367,68 @@ TxSetXDRFrame::createTransactionFrames(Hash const& networkID) const return phaseTxs; } +void +TxSetXDRFrame::forEachTransactionEnvelope( + std::function const& f) const +{ + if (isGeneralizedTxSet()) + { + auto const& txSet = + std::get(mXDRTxSet).v1TxSet(); + for (auto const& phase : txSet.phases) + { + switch (phase.v()) + { + case 0: + for (auto const& component : phase.v0Components()) + { + for (auto const& tx : component.txsMaybeDiscountedFee().txs) + { + f(tx); + } + } + break; + case 1: + for (auto const& stage : + phase.parallelTxsComponent().executionStages) + { + for (auto const& cluster : stage) + { + for (auto const& tx : cluster) + { + f(tx); + } + } + } + break; + default: + break; + } + } + } + else + { + for (auto const& tx : std::get(mXDRTxSet).txs) + { + f(tx); + } + } +} + +std::vector +TxSetXDRFrame::getTransactionFullHashes() const +{ + ZoneScoped; + std::vector hashes; + hashes.reserve(sizeTxTotal()); + // Same digest as TransactionFrame::getFullHash and + // FeeBumpTransactionFrame::getFullHash: SHA-256 of the envelope's XDR. + forEachTransactionEnvelope([&hashes](TransactionEnvelope const& tx) { + hashes.emplace_back(xdrSha256(tx)); + }); + return hashes; +} + size_t TxSetXDRFrame::encodedSize() const { diff --git a/src/herder/TxSetFrame.h b/src/herder/TxSetFrame.h index fbf5e182c..66f926f70 100644 --- a/src/herder/TxSetFrame.h +++ b/src/herder/TxSetFrame.h @@ -261,6 +261,16 @@ class TxSetXDRFrame : public NonMovableOrCopyable PerPhaseTransactionList createTransactionFrames(Hash const& networkID) const; + // Calls `f` with the envelope of every transaction in the set, in the + // order createTransactionFrames returns their frames, without building + // any frames (which copies every envelope). + void forEachTransactionEnvelope( + std::function const& f) const; + + // Full hashes (`TransactionFrameBase::getFullHash`) of every transaction + // in the set, in the same order, hashed directly from the XDR. + std::vector getTransactionFullHashes() const; + #ifdef BUILD_TESTS mutable ApplicableTxSetFrameConstPtr mApplicableTxSetOverride; diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index b9b1191f3..c763d764f 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -231,12 +231,33 @@ LoadGenerator::getTxPerStep(uint32_t txRate, std::chrono::seconds spikeInterval, return txs - static_cast(submittedSoFar); } +AccountID +LoadGenerator::getEnvelopeSourceID(TransactionEnvelope const& env) +{ + // Mirrors TransactionFrame::getSourceID and + // FeeBumpTransactionFrame::getSourceID (the inner transaction's source). + switch (env.type()) + { + case ENVELOPE_TYPE_TX_V0: + { + AccountID res; + res.ed25519() = env.v0().tx.sourceAccountEd25519; + return res; + } + case ENVELOPE_TYPE_TX: + return toAccountID(env.v1().tx.sourceAccount); + case ENVELOPE_TYPE_TX_FEE_BUMP: + return toAccountID(env.feeBump().tx.innerTx.v1().tx.sourceAccount); + default: + // TransactionFrameBase::makeTransactionFromWire aborts here too. + abort(); + } +} + void -LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, - PerPhaseTransactionList const& perPhaseTxs) +LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, TxSetXDRFrame const& txSet) { ZoneScoped; - auto const& accounts = mTxGenerator.getAccounts(); // An account stays in use until the transaction it submitted has been // applied, so that the next transaction generated for it sees the updated @@ -248,7 +269,7 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, { if (it->second <= lcl) { - mAccountsAvailable.insert(it->first); + mAccountsAvailable.push_back(it->first); it = mAccountsExternalized.erase(it); } else @@ -257,33 +278,45 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, } } - // Accounts whose transaction is in the set externalized for `ledgerSeq` - // are released once that ledger is applied. Accounts whose transaction is - // not in it are still pending in the mempool and remain in use. - UnorderedSet externalized; - for (auto const& txs : perPhaseTxs) + // Index the accounts selected since the last call. A transaction has been + // generated for each of them, so TxGenerator has cached the account. + if (!mAccountsInUseToIndex.empty()) { - for (auto const& tx : txs) + auto const& accounts = mTxGenerator.getAccounts(); + for (auto id : mAccountsInUseToIndex) { - externalized.insert(tx->getSourceID()); + releaseAssert(mAccountsInUse.find(id) != mAccountsInUse.end()); + auto accIt = accounts.find(id); + releaseAssert(accIt != accounts.end()); + releaseAssert( + mAccountsInUseByKey.emplace(accIt->second->getPublicKey(), id) + .second); } + mAccountsInUseToIndex.clear(); } - for (auto it = mAccountsInUse.begin(); it != mAccountsInUse.end();) + // No account is waiting for its transaction, e.g. loadgen is idle. + if (mAccountsInUseByKey.empty()) { - auto accIt = accounts.find(*it); - releaseAssert(accIt != accounts.end()); - if (externalized.find(accIt->second->getPublicKey()) != - externalized.end()) - { - mAccountsExternalized.emplace(*it, ledgerSeq); - it = mAccountsInUse.erase(it); - } - else + return; + } + + // Accounts whose transaction is in the set externalized for `ledgerSeq` + // are released once that ledger is applied. Accounts whose transaction is + // not in it are still pending in the mempool and remain in use. The set + // holds every node's transactions; looking each source up in the in-use + // index keeps this proportional to the set, however large the mempool + // backlog grows, and the lookups allocate nothing. + txSet.forEachTransactionEnvelope([&](TransactionEnvelope const& env) { + auto it = mAccountsInUseByKey.find(getEnvelopeSourceID(env)); + if (it == mAccountsInUseByKey.end()) { - ++it; + return; } - } + mAccountsExternalized.emplace(it->second, ledgerSeq); + mAccountsInUse.erase(it->second); + mAccountsInUseByKey.erase(it); + }); } // Reset everything except Soroban persistent state @@ -293,6 +326,8 @@ LoadGenerator::reset() { mTxGenerator.reset(); mAccountsInUse.clear(); + mAccountsInUseByKey.clear(); + mAccountsInUseToIndex.clear(); mAccountsExternalized.clear(); mAccountsAvailable.clear(); mNoAccountsAvailableSinceLedger.reset(); @@ -414,12 +449,13 @@ LoadGenerator::start(GeneratedLoadConfig& cfg) if (cfg.mode != LoadGenMode::PAY_PREGENERATED) { + mAccountsAvailable.reserve(cfg.nAccounts); // For upgrade modes, use root account (represented by special ID) uint32_t accounts = cfg.nAccounts; if (cfg.mode == LoadGenMode::SOROBAN_UPGRADE_SETUP || cfg.mode == LoadGenMode::SOROBAN_CREATE_UPGRADE) { - mAccountsAvailable.insert(TxGenerator::ROOT_ACCOUNT_ID); + mAccountsAvailable.push_back(TxGenerator::ROOT_ACCOUNT_ID); if (accounts) { accounts--; @@ -429,7 +465,7 @@ LoadGenerator::start(GeneratedLoadConfig& cfg) // Mark all accounts "available" as source accounts for (auto i = 0u; i < accounts; i++) { - mAccountsAvailable.insert(i + cfg.offset); + mAccountsAvailable.push_back(i + cfg.offset); } if (cfg.modeInvokes()) @@ -1026,11 +1062,11 @@ LoadGenerator::getNextAvailableAccount(uint32_t ledgerNum) auto sourceAccountIdx = rand_uniform(0, mAccountsAvailable.size() - 1); - auto it = mAccountsAvailable.begin(); - std::advance(it, sourceAccountIdx); - sourceAccountId = *it; - mAccountsAvailable.erase(it); + sourceAccountId = mAccountsAvailable[sourceAccountIdx]; + mAccountsAvailable[sourceAccountIdx] = mAccountsAvailable.back(); + mAccountsAvailable.pop_back(); releaseAssert(mAccountsInUse.insert(sourceAccountId).second); + mAccountsInUseToIndex.push_back(sourceAccountId); return sourceAccountId; } @@ -1428,11 +1464,14 @@ LoadGenerator::waitTillComplete(GeneratedLoadConfig cfg) // The first setup phase has fully applied, so its source accounts are // safe to reuse for the instance-creation phase. - mAccountsAvailable.insert(mAccountsInUse.begin(), mAccountsInUse.end()); + mAccountsAvailable.insert(mAccountsAvailable.end(), + mAccountsInUse.begin(), mAccountsInUse.end()); mAccountsInUse.clear(); + mAccountsInUseByKey.clear(); + mAccountsInUseToIndex.clear(); for (auto const& kv : mAccountsExternalized) { - mAccountsAvailable.insert(kv.first); + mAccountsAvailable.push_back(kv.first); } mAccountsExternalized.clear(); @@ -1530,7 +1569,15 @@ TxSubmitStatus LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, TransactionResultCode& code) { - TxMetrics txm(mApp.getMetrics()); + // The meters live in the registry for the application's lifetime, so use + // the references resolved at construction instead of looking all ten up + // again (a locked map search each) for every transaction. + auto& txm = mTxMetrics; + // Wire size of the TRANSACTION StellarMessage that floods `txf`: the + // 4-byte message type discriminant followed by the envelope, sized without + // copying the envelope into a new message. + uint64_t const msgBytes = + sizeof(uint32_t) + xdr::xdr_size(txf->getEnvelope()); // Record tx metrics. switch (mode) @@ -1538,8 +1585,7 @@ LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, case LoadGenMode::PAY: case LoadGenMode::PAY_PREGENERATED: txm.mNativePayment.Mark(txf->getNumOperations()); - txm.mNativePaymentBytes.Mark( - xdr::xdr_argpack_size(*txf->toStellarMessage())); + txm.mNativePaymentBytes.Mark(msgBytes); break; case LoadGenMode::SOROBAN_UPLOAD: txm.mSorobanUploadTxs.Mark(); @@ -1561,8 +1607,7 @@ LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, { case LoadGenMode::PAY: txm.mNativePayment.Mark(txf->getNumOperations()); - txm.mNativePaymentBytes.Mark( - xdr::xdr_argpack_size(*txf->toStellarMessage())); + txm.mNativePaymentBytes.Mark(msgBytes); break; case LoadGenMode::SOROBAN_UPLOAD: txm.mSorobanUploadTxs.Mark(); @@ -1586,16 +1631,14 @@ LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, else { txm.mNativePayment.Mark(txf->getNumOperations()); - txm.mNativePaymentBytes.Mark( - xdr::xdr_argpack_size(*txf->toStellarMessage())); + txm.mNativePaymentBytes.Mark(msgBytes); } break; } txm.mTxnAttempted.Mark(); - auto msg = txf->toStellarMessage(); - txm.mTxnBytes.Mark(xdr::xdr_argpack_size(*msg)); + txm.mTxnBytes.Mark(msgBytes); // Skip certain checks for pregenerated classic transactions. The synthetic // Soroban half of MIXED_PREGEN_* is generated locally, but must still go diff --git a/src/simulation/LoadGenerator.h b/src/simulation/LoadGenerator.h index f6051e635..97ca72ecd 100644 --- a/src/simulation/LoadGenerator.h +++ b/src/simulation/LoadGenerator.h @@ -11,6 +11,7 @@ #include "test/TestAccount.h" #include "test/TxTests.h" #include "util/NonCopyable.h" +#include "util/UnorderedMap.h" #include "xdr/Stellar-types.h" #include #include @@ -226,8 +227,12 @@ class LoadGenerator void stop(); - void cleanupAccounts(uint32_t ledgerSeq, - PerPhaseTransactionList const& perPhaseTxs); + void cleanupAccounts(uint32_t ledgerSeq, TxSetXDRFrame const& txSet); + + // The source account of the transaction in `env`, as + // TransactionFrameBase::getSourceID reports it for a frame built from + // `env`: the unmuxed source, or the inner transaction's for a fee bump. + static AccountID getEnvelopeSourceID(TransactionEnvelope const& env); private: struct TxMetrics @@ -278,10 +283,20 @@ class LoadGenerator // Track account IDs that are currently being referenced by the transaction // queue (to avoid source account collisions during tx submission) std::unordered_set mAccountsInUse; + // In-use accounts keyed by public key, so that cleanupAccounts matches an + // externalized transaction set in time proportional to the set rather + // than to the number of accounts waiting. Accounts selected since the last + // cleanupAccounts call wait in mAccountsInUseToIndex: their keys are only + // known once TxGenerator has generated a transaction for them. + UnorderedMap mAccountsInUseByKey; + std::vector mAccountsInUseToIndex; // Accounts whose transaction externalized in the given ledger; released // once that ledger is applied. std::unordered_map mAccountsExternalized; - std::unordered_set mAccountsAvailable; + // Accounts enter this pool only when initialized or released from one of + // the disjoint in-use/externalized collections. A dense vector allows + // uniform random selection and removal without walking the account pool. + std::vector mAccountsAvailable; // First ledger at which load generation stalled because every account had // a pending transaction. std::optional mNoAccountsAvailableSinceLedger; diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index 7ba135721..7e482fb35 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -251,6 +251,244 @@ TEST_CASE("mixed pregen and synthetic soroban in overlay-only mode", 100 * simulation->getExpectedLedgerCloseTime(), false); } +TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", + "[loadgen]") +{ + // perfnet's block_time_check load: MIXED_PREGEN_SAC_PAYMENT with no + // classic stream, in overlay-only mode, with more transactions than + // source accounts. The soroban stream fails the run as soon as its pool + // is empty, so the run completes only if accounts return to the pool once + // their transactions' ledgers close. The pool covers twenty seconds of + // load, far more than the few one-second ledgers a transaction is in + // flight, so a stalled real-time simulation does not drain it. + uint32_t const nAccounts = 300; + uint32_t const sorobanTxRate = 15; + uint32_t const nTxs = nAccounts + 100; + + Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); + Simulation::pointer simulation = Topologies::pair(networkID, [&](int i) { + auto cfg = getTestConfig(i); + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; + cfg.ARTIFICIALLY_GENERATE_LOAD_FOR_TESTING = true; + cfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = + Config::CURRENT_LEDGER_PROTOCOL_VERSION; + cfg.GENESIS_TEST_ACCOUNT_COUNT = nAccounts; + return cfg; + }); + + simulation->startAllNodes(); + simulation->crankUntil( + [&]() { return simulation->haveAllExternalized(3, 1); }, + 10 * simulation->getExpectedLedgerCloseTime(), false); + + auto nodes = simulation->getNodes(); + auto& app = *nodes[0]; + + // Max out Soroban network limits so synthetic footprints pass checkValid + // and every submitted transaction fits in the next ledger. + upgradeSorobanNetworkConfig( + [&](SorobanNetworkConfig& cfg) { + auto mx = std::numeric_limits::max(); + cfg.mLedgerMaxTxCount = mx; + cfg.mLedgerMaxInstructions = mx; + cfg.mLedgerMaxTransactionsSizeBytes = mx; + cfg.mLedgerMaxDiskReadEntries = mx; + cfg.mLedgerMaxDiskReadBytes = mx; + cfg.mLedgerMaxWriteLedgerEntries = mx; + cfg.mLedgerMaxWriteBytes = mx; + cfg.mTxMaxInstructions = mx; + cfg.mTxMaxDiskReadEntries = mx; + cfg.mTxMaxDiskReadBytes = mx; + cfg.mTxMaxWriteLedgerEntries = mx; + cfg.mTxMaxWriteBytes = mx; + cfg.mTxMaxFootprintEntries = mx; + cfg.mTxMaxSizeBytes = mx; + cfg.mTxMaxContractEventsSizeBytes = mx; + }, + simulation); + + // MIXED_PREGEN_* modes open a pregenerated classic file even when the + // classic stream is off; this run never reads from it. + std::string fileName = + app.getConfig().LOADGEN_PREGENERATED_TRANSACTIONS_FILE; + auto cleanup = gsl::finally([&]() { std::remove(fileName.c_str()); }); + generateTransactions(app, fileName, /* numTransactions */ 1, nAccounts, + /* offset */ 0); + + for (auto& node : nodes) + { + node->setRunInOverlayOnlyMode(true); + } + + auto& completed = + app.getMetrics().NewMeter({"loadgen", "run", "complete"}, "run"); + auto& failed = + app.getMetrics().NewMeter({"loadgen", "run", "failed"}, "run"); + auto const completedBefore = completed.count(); + auto const failedBefore = failed.count(); + + // Per-transaction submission metrics. + auto& attempted = + app.getMetrics().NewMeter({"loadgen", "txn", "attempted"}, "txn"); + auto& invokes = + app.getMetrics().NewMeter({"loadgen", "soroban", "invoke"}, "txn"); + auto& payments = + app.getMetrics().NewMeter({"loadgen", "payment", "submitted"}, "op"); + auto& bytes = app.getMetrics().NewMeter({"loadgen", "txn", "bytes"}, "txn"); + auto const attemptedBefore = attempted.count(); + auto const invokesBefore = invokes.count(); + auto const paymentsBefore = payments.count(); + auto const bytesBefore = bytes.count(); + + // Every synthetic SAC payment has the same XDR shape, so one reference + // transaction gives the wire size of each message loadgen submits. + TxGenerator refGen(app); + SCAddress refDest(SC_ADDRESS_TYPE_CONTRACT); + refDest.contractId() = sha256("reference destination"); + auto refTx = refGen + .invokeSACPayment( + app.getLedgerManager().getLastClosedLedgerNum() + 1, + /* fromAccountId */ 0, refDest, + makeSyntheticContractInstance("loadgen_sac"), + /* amount */ 100, std::nullopt) + .second; + auto const refMsgBytes = xdr::xdr_argpack_size(*refTx->toStellarMessage()); + + GeneratedLoadConfig cfg = GeneratedLoadConfig::txLoad( + LoadGenMode::MIXED_PREGEN_SAC_PAYMENT, nAccounts, nTxs, sorobanTxRate, + /* offset */ 0); + cfg.preloadedTransactionsFile = fileName; + auto& mix = cfg.getMutMixPregenSorobanConfig(); + mix.classicTxRate = 0; + mix.sorobanTxRate = sorobanTxRate; + app.getLoadGenerator().generateLoad(cfg); + + simulation->crankUntil( + [&]() { + return completed.count() == completedBefore + 1 || + failed.count() != failedBefore; + }, + 60 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(failed.count() == failedBefore); + REQUIRE(completed.count() == completedBefore + 1); + + REQUIRE(attempted.count() - attemptedBefore == nTxs); + REQUIRE(invokes.count() - invokesBefore == nTxs); + REQUIRE(payments.count() == paymentsBefore); + REQUIRE(bytes.count() - bytesBefore == nTxs * refMsgBytes); +} + +TEST_CASE("externalized tx set hashes and sources match transaction frames", + "[loadgen][herder][txset]") +{ + // Herder hands the overlay getTransactionFullHashes() and loadgen the + // envelopes from forEachTransactionEnvelope() instead of building a frame + // per transaction. Pin both to what frames built from the same set give: + // same order, same full hashes, same source accounts. + Hash const networkID = sha256("externalized tx set test network"); + auto key = [](std::string const& name) { + return txtest::getAccount(name).getPublicKey(); + }; + uint32_t nextSeq = 1; + auto v1Tx = [&](PublicKey const& source, + std::optional muxId = std::nullopt) { + TransactionEnvelope env(ENVELOPE_TYPE_TX); + env.v1().tx.sourceAccount = toMuxedAccount(source, muxId); + env.v1().tx.fee = 100; + env.v1().tx.seqNum = nextSeq++; + env.v1().tx.operations.emplace_back(txtest::payment(key("dest"), 1)); + return env; + }; + auto v0Tx = [&](PublicKey const& source) { + TransactionEnvelope env(ENVELOPE_TYPE_TX_V0); + env.v0().tx.sourceAccountEd25519 = source.ed25519(); + env.v0().tx.fee = 100; + env.v0().tx.seqNum = nextSeq++; + env.v0().tx.operations.emplace_back(txtest::payment(key("dest"), 1)); + return env; + }; + auto feeBumpTx = [&](PublicKey const& feeSource, PublicKey const& source) { + TransactionEnvelope env(ENVELOPE_TYPE_TX_FEE_BUMP); + env.feeBump().tx.feeSource = toMuxedAccount(feeSource, 7); + env.feeBump().tx.fee = 400; + env.feeBump().tx.innerTx.type(ENVELOPE_TYPE_TX); + env.feeBump().tx.innerTx.v1() = v1Tx(source).v1(); + return env; + }; + + auto checkMatchesFrames = [&](TxSetXDRFrameConstPtr const& txSet, + size_t expectedTxs) { + std::vector frames; + for (auto const& phase : txSet->createTransactionFrames(networkID)) + { + frames.insert(frames.end(), phase.begin(), phase.end()); + } + std::vector envelopes; + txSet->forEachTransactionEnvelope( + [&](TransactionEnvelope const& env) { envelopes.push_back(env); }); + auto hashes = txSet->getTransactionFullHashes(); + + REQUIRE(frames.size() == expectedTxs); + REQUIRE(envelopes.size() == expectedTxs); + REQUIRE(hashes.size() == expectedTxs); + for (size_t i = 0; i < expectedTxs; ++i) + { + REQUIRE(envelopes[i] == frames[i]->getEnvelope()); + REQUIRE(hashes[i] == frames[i]->getFullHash()); + REQUIRE(LoadGenerator::getEnvelopeSourceID(envelopes[i]) == + frames[i]->getSourceID()); + } + }; + + SECTION("source account of each envelope type") + { + REQUIRE(LoadGenerator::getEnvelopeSourceID(v0Tx(key("a"))) == key("a")); + REQUIRE(LoadGenerator::getEnvelopeSourceID(v1Tx(key("b"))) == key("b")); + // Muxed sources resolve to the underlying account. + REQUIRE(LoadGenerator::getEnvelopeSourceID(v1Tx(key("c"), 42)) == + key("c")); + // A fee bump's source is its inner transaction's, not the fee source. + REQUIRE(LoadGenerator::getEnvelopeSourceID( + feeBumpTx(key("fee"), key("inner"))) == key("inner")); + } + SECTION("legacy tx set") + { + TransactionSet xdrSet; + xdrSet.previousLedgerHash = sha256("previous ledger"); + xdrSet.txs = {v0Tx(key("a")), v1Tx(key("b")), v1Tx(key("c"), 42), + feeBumpTx(key("fee"), key("inner"))}; + checkMatchesFrames(TxSetXDRFrame::makeFromWire(xdrSet), 4); + } + SECTION("generalized tx set with sequential and parallel phases") + { + GeneralizedTransactionSet xdrSet(1); + auto& v1Set = xdrSet.v1TxSet(); + v1Set.previousLedgerHash = sha256("previous ledger"); + + auto& classic = v1Set.phases.emplace_back(0); + for (auto const& txs : + {std::vector{v1Tx(key("a")), v0Tx(key("b"))}, + std::vector{ + v1Tx(key("c"), 42), feeBumpTx(key("fee"), key("d"))}}) + { + auto& component = classic.v0Components().emplace_back( + TXSET_COMP_TXS_MAYBE_DISCOUNTED_FEE); + component.txsMaybeDiscountedFee().txs.assign(txs.begin(), + txs.end()); + } + + auto& parallel = v1Set.phases.emplace_back(1); + auto& stages = parallel.parallelTxsComponent().executionStages; + auto& stage0 = stages.emplace_back(); + stage0.push_back({v1Tx(key("e")), v1Tx(key("f"))}); + stage0.push_back({feeBumpTx(key("fee"), key("g"))}); + auto& stage1 = stages.emplace_back(); + stage1.push_back({v1Tx(key("h"), 9)}); + + checkMatchesFrames(TxSetXDRFrame::makeFromWire(xdrSet), 8); + } +} + TEST_CASE("generate load with unique accounts", "[loadgen]") { Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); @@ -328,6 +566,36 @@ TEST_CASE("generate load with unique accounts", "[loadgen]") 120 * simulation->getExpectedLedgerCloseTime(), false); REQUIRE(getSuccessfulTxCount() == nTxs); } + SECTION("recycle available accounts across ledgers and runs") + { + // More submissions than accounts requires returning accounts to the + // pool once their transactions have applied. Every transaction must + // apply successfully, and a run completes only once each account's + // cached sequence number matches the ledger. A second run exercises + // resetting the pool. + uint32_t const txsPerRun = 2 * nAccounts; + auto& completed = + app.getMetrics().NewMeter({"loadgen", "run", "complete"}, "run"); + auto& failed = + app.getMetrics().NewMeter({"loadgen", "run", "failed"}, "run"); + for (uint32_t run = 0; run < 2; ++run) + { + auto before = getSuccessfulTxCount(); + auto runsBefore = completed.count(); + auto failedBefore = failed.count(); + loadGen.generateLoad(GeneratedLoadConfig::txLoad( + LoadGenMode::PAY, nAccounts, txsPerRun, + /* txRate */ 200, /* offset */ nAccounts)); + simulation->crankUntil( + [&]() { + return completed.count() == runsBefore + 1 || + failed.count() != failedBefore; + }, + 120 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(failed.count() == failedBefore); + REQUIRE(getSuccessfulTxCount() - before == txsPerRun); + } + } SECTION("invalid loadgen parameters") { uint32 numAccounts = 100;