Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/herder/HerderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash> 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<uint32_t>(slotIndex), txFramesList);
static_cast<uint32_t>(slotIndex), *externalizedSet);
#endif
}
mApp.getOverlayManager().notifyTxSetExternalized(value.txSetHash, txHashes);
Expand Down
62 changes: 62 additions & 0 deletions src/herder/TxSetFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,68 @@ TxSetXDRFrame::createTransactionFrames(Hash const& networkID) const
return phaseTxs;
}

void
TxSetXDRFrame::forEachTransactionEnvelope(
std::function<void(TransactionEnvelope const&)> const& f) const
{
if (isGeneralizedTxSet())
{
auto const& txSet =
std::get<GeneralizedTransactionSet>(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<TransactionSet>(mXDRTxSet).txs)
{
f(tx);
}
}
}

std::vector<Hash>
TxSetXDRFrame::getTransactionFullHashes() const
{
ZoneScoped;
std::vector<Hash> 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
{
Expand Down
10 changes: 10 additions & 0 deletions src/herder/TxSetFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(TransactionEnvelope const&)> const& f) const;

// Full hashes (`TransactionFrameBase::getFullHash`) of every transaction
// in the set, in the same order, hashed directly from the XDR.
std::vector<Hash> getTransactionFullHashes() const;

#ifdef BUILD_TESTS
mutable ApplicableTxSetFrameConstPtr mApplicableTxSetOverride;

Expand Down
123 changes: 83 additions & 40 deletions src/simulation/LoadGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,33 @@ LoadGenerator::getTxPerStep(uint32_t txRate, std::chrono::seconds spikeInterval,
return txs - static_cast<int64_t>(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
Expand All @@ -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
Expand All @@ -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<AccountID> 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
Expand All @@ -293,6 +326,8 @@ LoadGenerator::reset()
{
mTxGenerator.reset();
mAccountsInUse.clear();
mAccountsInUseByKey.clear();
mAccountsInUseToIndex.clear();
mAccountsExternalized.clear();
mAccountsAvailable.clear();
mNoAccountsAvailableSinceLedger.reset();
Expand Down Expand Up @@ -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--;
Expand All @@ -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())
Expand Down Expand Up @@ -1026,11 +1062,11 @@ LoadGenerator::getNextAvailableAccount(uint32_t ledgerNum)

auto sourceAccountIdx =
rand_uniform<uint64_t>(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;
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -1530,16 +1569,23 @@ 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)
{
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();
Expand All @@ -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();
Expand All @@ -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
Expand Down
21 changes: 18 additions & 3 deletions src/simulation/LoadGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <optional>
#include <unordered_map>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<uint64_t> 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<AccountID, uint64_t> mAccountsInUseByKey;
std::vector<uint64_t> mAccountsInUseToIndex;
// Accounts whose transaction externalized in the given ledger; released
// once that ledger is applied.
std::unordered_map<uint64_t, uint32_t> mAccountsExternalized;
std::unordered_set<uint64_t> 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<uint64_t> mAccountsAvailable;
// First ledger at which load generation stalled because every account had
// a pending transaction.
std::optional<uint32_t> mNoAccountsAvailableSinceLedger;
Expand Down
Loading
Loading