From 21dc17ea68bb42e2eac6718843e6a05d1bd5cbf9 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 14 Sep 2026 23:20:38 -0700 Subject: [PATCH 1/5] Select loadgen source accounts in constant time Port the focused account-pool change from 75f3fbc05e onto batched-getdata. Use uniform indexed selection and swap-removal instead of walking the available-account hash set for every transaction. Preserve release only after application, including Soroban setup transitions. Cover repeated account recycling and consecutive runs using real ledger application, with more submissions than available accounts and no rejected transactions. --- src/simulation/LoadGenerator.cpp | 19 +++++++++-------- src/simulation/LoadGenerator.h | 5 ++++- src/simulation/test/LoadGeneratorTests.cpp | 24 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index b9b1191f3..ece7879fc 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -248,7 +248,7 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, { if (it->second <= lcl) { - mAccountsAvailable.insert(it->first); + mAccountsAvailable.push_back(it->first); it = mAccountsExternalized.erase(it); } else @@ -414,12 +414,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 +430,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,10 +1027,9 @@ 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); return sourceAccountId; @@ -1428,11 +1428,12 @@ 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(); for (auto const& kv : mAccountsExternalized) { - mAccountsAvailable.insert(kv.first); + mAccountsAvailable.push_back(kv.first); } mAccountsExternalized.clear(); diff --git a/src/simulation/LoadGenerator.h b/src/simulation/LoadGenerator.h index f6051e635..0b865e737 100644 --- a/src/simulation/LoadGenerator.h +++ b/src/simulation/LoadGenerator.h @@ -281,7 +281,10 @@ class LoadGenerator // 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..cf562c0f8 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -328,6 +328,30 @@ 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 applied accounts + // to the pool. Real application catches premature reuse through bad + // sequence numbers; a second run also exercises resetting the pool. + uint32_t const txsPerRun = 3 * nAccounts; + auto& completed = + app.getMetrics().NewMeter({"loadgen", "run", "complete"}, "run"); + auto& rejected = + app.getMetrics().NewMeter({"loadgen", "txn", "rejected"}, "txn"); + for (uint32_t run = 0; run < 2; ++run) + { + auto before = getSuccessfulTxCount(); + auto runsBefore = completed.count(); + loadGen.generateLoad(GeneratedLoadConfig::txLoad( + LoadGenMode::PAY, nAccounts, txsPerRun, + /* txRate */ 100, /* offset */ nAccounts)); + simulation->crankUntil( + [&]() { return completed.count() == runsBefore + 1; }, + 120 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(getSuccessfulTxCount() - before == txsPerRun); + REQUIRE(rejected.count() == 0); + } + } SECTION("invalid loadgen parameters") { uint32 numAccounts = 100; From c02730f5af1cdd2fb43b6adc980f5de8702ccc3a Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:42:39 -0800 Subject: [PATCH 2/5] Match externalized loadgen accounts in time proportional to the tx set cleanupAccounts runs on the main thread in valueExternalized for every ledger, on every node, whether or not that node generates load. It hashed and allocated a set entry for the source of every transaction in the externalized set (about 6,000 to 7,500 per ledger in perfnet's 3,000 TPS runs), then walked every in-use account with a std::map lookup. When ledgers fall behind the offered load, the in-use backlog grows by tens of accounts per ledger per generator, so the walk grew with it on the path between externalize and the next ledger. Keep in-use accounts indexed by public key instead. Accounts selected since the previous call are indexed at the start of the next one (their keys are known once TxGenerator has generated a transaction for them), and each externalized transaction's source is looked up in that index. The cost is now one allocation-free lookup per transaction in the set plus one index insert per newly selected account, independent of the backlog, with an early return when no account is waiting. Which accounts are released, and when, is unchanged. Add a test for perfnet's block_time_check load: synthetic SAC payments in overlay-only mode with more transactions than source accounts, which only completes if accounts return to the pool after their ledgers close. --- src/simulation/LoadGenerator.cpp | 58 ++++++++----- src/simulation/LoadGenerator.h | 8 ++ src/simulation/test/LoadGeneratorTests.cpp | 95 ++++++++++++++++++++++ 3 files changed, 142 insertions(+), 19 deletions(-) diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index ece7879fc..0972fa520 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -236,7 +236,6 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, PerPhaseTransactionList const& perPhaseTxs) { 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 @@ -257,31 +256,47 @@ 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. + for (auto const& txs : perPhaseTxs) + { + for (auto const& tx : txs) { - ++it; + auto it = mAccountsInUseByKey.find(tx->getSourceID()); + if (it == mAccountsInUseByKey.end()) + { + continue; + } + mAccountsExternalized.emplace(it->second, ledgerSeq); + mAccountsInUse.erase(it->second); + mAccountsInUseByKey.erase(it); } } } @@ -293,6 +308,8 @@ LoadGenerator::reset() { mTxGenerator.reset(); mAccountsInUse.clear(); + mAccountsInUseByKey.clear(); + mAccountsInUseToIndex.clear(); mAccountsExternalized.clear(); mAccountsAvailable.clear(); mNoAccountsAvailableSinceLedger.reset(); @@ -1031,6 +1048,7 @@ LoadGenerator::getNextAvailableAccount(uint32_t ledgerNum) mAccountsAvailable[sourceAccountIdx] = mAccountsAvailable.back(); mAccountsAvailable.pop_back(); releaseAssert(mAccountsInUse.insert(sourceAccountId).second); + mAccountsInUseToIndex.push_back(sourceAccountId); return sourceAccountId; } @@ -1431,6 +1449,8 @@ LoadGenerator::waitTillComplete(GeneratedLoadConfig cfg) mAccountsAvailable.insert(mAccountsAvailable.end(), mAccountsInUse.begin(), mAccountsInUse.end()); mAccountsInUse.clear(); + mAccountsInUseByKey.clear(); + mAccountsInUseToIndex.clear(); for (auto const& kv : mAccountsExternalized) { mAccountsAvailable.push_back(kv.first); diff --git a/src/simulation/LoadGenerator.h b/src/simulation/LoadGenerator.h index 0b865e737..0664e40ae 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 @@ -278,6 +279,13 @@ 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; diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index cf562c0f8..a5fd9c622 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -251,6 +251,101 @@ 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(); + + 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); +} + TEST_CASE("generate load with unique accounts", "[loadgen]") { Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); From 33012c95e60043f3e14c5b682a91c327a7607528 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:44:18 -0800 Subject: [PATCH 3/5] Stop re-registering loadgen metrics and copying envelopes per transaction LoadGenerator::execute built a fresh TxMetrics for every submitted transaction, which resolved all ten loadgen meters through MetricsRegistry::NewMeter: a MetricName allocation, a mutex, and two searches of the registry's std::map each. It also copied the whole envelope into a new StellarMessage (twice for classic payments) only to measure its size. Use the TxMetrics member resolved once at construction, which refers to the same registry meters, and compute the message size as the 4-byte message type discriminant plus the envelope's XDR size. The recorded values are unchanged. Extend the overlay-only synthetic SAC test to check the per-transaction meters, including that loadgen.txn.bytes still equals the wire size of each submitted StellarMessage. --- src/simulation/LoadGenerator.cpp | 22 +++++++++------ src/simulation/test/LoadGeneratorTests.cpp | 32 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index 0972fa520..6091df1b6 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -1551,7 +1551,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) @@ -1559,8 +1567,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(); @@ -1582,8 +1589,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(); @@ -1607,16 +1613,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/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index a5fd9c622..2d530fc1d 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -327,6 +327,33 @@ TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", 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); @@ -344,6 +371,11 @@ TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", 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("generate load with unique accounts", "[loadgen]") From 1184e544477c5e4b7199643f6511c10f81f055b1 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:56:53 -0800 Subject: [PATCH 4/5] Make the loadgen account recycling test check what it claims, faster The section asserted that no transaction was rejected, but on this branch HerderImpl::recvTransaction hands every transaction to the overlay and always returns TX_STATUS_PENDING, so loadgen.txn.rejected cannot move. The checks that matter are that every transaction applies successfully and that the run completes, which requires each account's cached sequence number to match the ledger. Say so, drop the rejected assertion, and stop cranking as soon as a run fails instead of waiting for the timeout. Submit 2x the accounts at 200 tx/s instead of 3x at 100 tx/s. That still forces every account through the pool more than once and stays within the unique-account check (3 x 200 txs per one-second ledger = 600 <= 1,000 accounts), and each run takes about 10 s of submission instead of 30 s. --- src/simulation/test/LoadGeneratorTests.cpp | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index 2d530fc1d..52de6a0bd 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -457,26 +457,32 @@ TEST_CASE("generate load with unique accounts", "[loadgen]") } SECTION("recycle available accounts across ledgers and runs") { - // More submissions than accounts requires returning applied accounts - // to the pool. Real application catches premature reuse through bad - // sequence numbers; a second run also exercises resetting the pool. - uint32_t const txsPerRun = 3 * nAccounts; + // 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& rejected = - app.getMetrics().NewMeter({"loadgen", "txn", "rejected"}, "txn"); + 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 */ 100, /* offset */ nAccounts)); + /* txRate */ 200, /* offset */ nAccounts)); simulation->crankUntil( - [&]() { return completed.count() == runsBefore + 1; }, + [&]() { + return completed.count() == runsBefore + 1 || + failed.count() != failedBefore; + }, 120 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(failed.count() == failedBefore); REQUIRE(getSuccessfulTxCount() - before == txsPerRun); - REQUIRE(rejected.count() == 0); } } SECTION("invalid loadgen parameters") From 0fd7d0edf1ba9f896745f6df5d03b45f9647576e Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:03:55 -0800 Subject: [PATCH 5/5] Hash externalized transactions without rebuilding their frames HerderImpl::processExternalized needs two things from every transaction in the externalized set on the main thread: its full hash, so the Rust overlay can drop it from the mempool, and (in BUILD_TESTS) its source account, so loadgen can release it. It got both by calling createTransactionFrames, which copies every envelope into a new TransactionFrame (with an OperationFrame per operation) only to hash it and read its source, then frees them all. That is thousands of frames per ledger at perfnet's load. Add TxSetXDRFrame::forEachTransactionEnvelope, which visits the set's envelopes in createTransactionFrames' order, and getTransactionFullHashes, which hashes each envelope's XDR in place: the same SHA-256 that TransactionFrame::getFullHash and FeeBumpTransactionFrame::getFullHash compute. LoadGenerator::cleanupAccounts takes the tx set and reads each source from the envelope with getEnvelopeSourceID, which mirrors getSourceID: the unmuxed source, or a fee bump's inner source. The overlay receives the same hashes in the same order, and loadgen releases the same accounts. Add a test that builds legacy and generalized sets (sequential and parallel phases; v0, v1, muxed and fee-bump envelopes) from XDR and checks the visited envelopes, full hashes and sources against the frames createTransactionFrames builds from the same set. --- src/herder/HerderImpl.cpp | 16 +-- src/herder/TxSetFrame.cpp | 62 ++++++++++++ src/herder/TxSetFrame.h | 10 ++ src/simulation/LoadGenerator.cpp | 46 ++++++--- src/simulation/LoadGenerator.h | 8 +- src/simulation/test/LoadGeneratorTests.cpp | 111 +++++++++++++++++++++ 6 files changed, 226 insertions(+), 27 deletions(-) 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 6091df1b6..c763d764f 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -231,9 +231,31 @@ 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; @@ -285,20 +307,16 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, // 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. - for (auto const& txs : perPhaseTxs) - { - for (auto const& tx : txs) + txSet.forEachTransactionEnvelope([&](TransactionEnvelope const& env) { + auto it = mAccountsInUseByKey.find(getEnvelopeSourceID(env)); + if (it == mAccountsInUseByKey.end()) { - auto it = mAccountsInUseByKey.find(tx->getSourceID()); - if (it == mAccountsInUseByKey.end()) - { - continue; - } - mAccountsExternalized.emplace(it->second, ledgerSeq); - mAccountsInUse.erase(it->second); - mAccountsInUseByKey.erase(it); + return; } - } + mAccountsExternalized.emplace(it->second, ledgerSeq); + mAccountsInUse.erase(it->second); + mAccountsInUseByKey.erase(it); + }); } // Reset everything except Soroban persistent state diff --git a/src/simulation/LoadGenerator.h b/src/simulation/LoadGenerator.h index 0664e40ae..97ca72ecd 100644 --- a/src/simulation/LoadGenerator.h +++ b/src/simulation/LoadGenerator.h @@ -227,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 diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index 52de6a0bd..7e482fb35 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -378,6 +378,117 @@ TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", 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);