diff --git a/README.md b/README.md index 7973a937c..dbe94bd43 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# stellar-core: leader schedule with terms, leader tx-set push, apply-time tx-set validation, transactions sent to the leaders only (experimental branch) +# stellar-core: leader schedule with terms, leader tx-set push, apply-time tx-set validation, transactions sent to the leaders only, tx sets selected during the previous ballot (experimental branch) **This branch is an experiment. It is not production code and must not run on a real network.** The leader schedule and the tx-set push have no feature flag and no protocol gate, and the branch @@ -16,13 +16,20 @@ instances**: at 48 validators (16 organizations) and 8,000 TPS, and at the goal ms (limit 960), a ledger every 0.90 s, 15.7 MB/s per node; with leader terms and pre-validation alone it is at 1,032 ms, 1.02 s per ledger and 43 MB/s. +**The sixth experiment (tx sets selected during the previous ballot) did not help at the goal +fixture.** Measured once, on 2026-11-12, at 90 validators and 10,000 TPS on m6id.8xlarge (the +CPU of c6id.8xlarge; no c6id capacity that evening): the first window is at 1,068 ms, against +980 ms for the branch without it on c6id.8xlarge. The set was ready in time for 17% of the +ledgers, and using it left the ledgers unevenly filled. It was not measured at 48 validators. +Details: [What the sixth experiment did at the goal fixture](#what-the-sixth-experiment-did-at-the-goal-fixture). + The earlier benchmark (48 validators, 5,000 TPS, 1,000 ms, 4-core instances) is kept below. ## What this branch is It is the overlay-v2 stack up to [#5459](https://github.com/stellar/stellar-core/pull/5459) (Rust overlay over QUIC, millisecond close times, compressed tx-set responses, leader-only tx-set -construction) plus five experiments: +construction) plus six experiments: 1. **No nomination** ([#5495](https://github.com/stellar/stellar-core/pull/5495)). The SCP nomination phase is deleted and replaced by a leader schedule that every node computes locally. @@ -45,7 +52,7 @@ construction) plus five experiments: leaves in one flight instead of being paced over a round trip. - Details and limits: [docs/rust-overlay/txset-fetching.md](docs/rust-overlay/txset-fetching.md). 3. **Vote without validating the tx set; validate and execute at apply** - ([#5498](https://github.com/stellar/stellar-core/pull/5498), the latest experiment). Gated on + ([#5498](https://github.com/stellar/stellar-core/pull/5498)). Gated on protocol 30; protocol 29 and older behave as before. - **Vote.** A node votes for the leader's value as soon as it holds the tx set, without running the set's validity check. Still checked before the vote: the value itself (signature, close @@ -80,8 +87,8 @@ construction) plus five experiments: - **One build.** The first-round leader builds once, a lead time before its trigger, pushes the set as soon as it exists and proposes it unchanged at the trigger. 5. **Transactions go to the upcoming leaders only** - ([#5501](https://github.com/stellar/stellar-core/pull/5501), the latest experiment). No - protocol gate and no version negotiation. + ([#5501](https://github.com/stellar/stellar-core/pull/5501)). No protocol gate and no + version negotiation. - **Targets.** A node that accepts a transaction no longer advertises it to every peer. Its overlay sends the body straight to the leaders of the node's current round and of the next one for the next ledger and, one ledger before a new leader term starts, to that term's @@ -97,11 +104,37 @@ construction) plus five experiments: connection key, signed by the node key. - **Cost.** Followers no longer hold the transactions before the leader's set arrives, so they verify signatures when they check the set at apply instead of in the background. - -Unchanged by all five: the ballot protocol's accept, confirm and commit rules. For a valid tx +6. **The leader selects its next set during the previous ballot and executes its own set + before the trigger** ([#5503](https://github.com/stellar/stellar-core/pull/5503), the latest + experiment). No protocol gate; nothing on the wire changes. + - **Why.** A set names the hash of the ledger it goes on top of, so its build could not + start before that ledger had closed. At 90 validators the build (0.24 s) was the first + 0.29 s of every ledger. + - **Selection.** The node that expects to lead the next ledger takes the pre-validation of + experiment 4 one step further: when its candidates are validated, a background thread + trims and surge-prices them against the state its speculative execution left, and writes + the set without a parent. + - **Completion.** When the ledger has closed with the value that was executed, the leader + only names it as the parent, hashes the set and pushes it. It does so only if a build + would have to start at once (the trigger is due, or less than a build's lead time away) + and a build of that size would take time; otherwise it builds as before, from a fresher + mempool. The selection is dropped if the ledger closed with another value, if the limits + changed, or if a time-bounded transaction is not valid for the close time the set gets. + - **Early execution.** The leader hands its prepared set to its apply thread as soon as the + set is ready, instead of at the trigger. If that execution finds the set invalid before + the proposal, the set is not proposed. + - **Checks.** The builder still validates the structure of what it selected before the set + leaves it. Re-reading the written XDR and validating the result now happens on a + background thread, after the push. + - **Cost.** A set selected early holds what the mempool held when the previous ledger was + executed: transactions that arrive during the rest of that ledger's ballot wait for the + next set. + +Unchanged by all six: the ballot protocol's accept, confirm and commit rules. For a valid tx set, ledger state and meta are also unchanged; experiment 3 changes when a set is validated and executed, and what an invalid set does to the ledger; experiment 4 changes who leads; experiment -5 changes which transactions reach a leader, and which submissions a node refuses. +5 changes which transactions reach a leader, and which submissions a node refuses; experiment 6 +changes when a leader chooses its transactions and when it starts executing its own set. ## Performance results @@ -146,28 +179,31 @@ per build, all on 2026-11-12. "Base" is this branch up to experiment 3. ### 800 ms target at the goal: 90 validators, 10,000 TPS -30 organizations, 22-23 validators per region, the same instances and load shape, 10,080 TPS -offered. Both runs missed their first window and the load was stopped after it. +30 organizations, 22-23 validators per region, the same load shape, 10,080 TPS offered. All +three runs missed their first window and the load was stopped after it. The first two ran on +c6id.8xlarge; the third, eleven hours later, on m6id.8xlarge (the same CPU model and core +count, 128 GiB instead of 64, other availability zones), because c6id.8xlarge could not be +had: it differs from the second in the build **and** in the instance type. -| Window 1 | Leader terms and pre-validation (#5502, `8aa7f6bf0`) | + transactions to leaders only (#5501, `6ab837c27`) | -|---|---:|---:| -| p75, average over 90 nodes (limit 960 ms) | 1,032 ms | **980 ms** | -| p99, average over 90 nodes (limit 1,600 ms) | 1,887 ms (over) | **1,441 ms** (inside) | -| Time per ledger | 1.02 s | **0.90 s** (median 0.88 s) | -| The first minute of the load | 802 ms for 30 s, then a backlog | 805 ms over the whole minute | -| Tx sets at the 10,000 cap | 93% | 39% | -| Leader builds finished before the trigger | 0 of 40 sampled | 40 of 55 sampled | -| Leader build; candidates in the mempool | 0.33 s; 20,200 | 0.24 s; 8,350 (median) | -| Trigger to first ballot | 0.58 s | **0.39 s** | -| First ballot to externalize | 0.32 s | 0.32 s | -| Externalize to ledger closed | 0.07 s | 0.07 s | -| **Trigger to ledger closed** | 0.97 s | **0.77 s** | -| Tx set at a follower after the proposal | 0.23 s | 0.17 s | -| Tx-set validation at apply | (not listed) | 0.14 s | -| **Overlay traffic per node, each way** | 43 MB/s | **15.7 MB/s** | -| Leader and ballot timeouts, empty tx sets | 0 | 0 | -| Submitted against included at the end | 49,855 short (backlog) | 81 more (the backlog drained) | -| Run verdict | FAIL (block time: p75 and p99) | FAIL (block time: p75) | +| Window 1 | Leader terms and pre-validation (#5502, `8aa7f6bf0`) | + transactions to leaders only (#5501, `6ab837c27`) | + sets selected during the previous ballot (experiment 6, `18a3a0238`), **on m6id.8xlarge** | +|---|---:|---:|---:| +| p75, average over 90 nodes (limit 960 ms) | 1,032 ms | **980 ms** | 1,068 ms | +| p99, average over 90 nodes (limit 1,600 ms) | 1,887 ms (over) | **1,441 ms** (inside) | 1,664 ms (over) | +| Time per ledger | 1.02 s | **0.90 s** (median 0.88 s) | 1.03 s (median 1.03 s) | +| The first minute of the load | 802 ms for 30 s, then a backlog | 805 ms over the whole minute | 834 ms (median 806 ms); 23 of 72 sets at the cap | +| Tx sets at the 10,000 cap | 93% | 39% | 91% | +| Leader builds finished before the trigger | 0 of 40 sampled | 40 of 55 sampled | 10 of 20 sampled sets ready before the trigger (6 of them selected early) | +| Leader build; candidates in the mempool | 0.33 s; 20,200 | 0.24 s; 8,350 (median) | 0.30 s; 20,200 (median) | +| Trigger to first ballot | 0.58 s | **0.39 s** | 0.56 s | +| First ballot to externalize | 0.32 s | 0.32 s | 0.33 s | +| Externalize to ledger closed | 0.07 s | 0.07 s | 0.07 s | +| **Trigger to ledger closed** | 0.97 s | **0.77 s** | 0.95 s | +| Tx set at a follower after the proposal | 0.23 s | 0.17 s | 0.23 s | +| Tx-set validation at apply | (not listed) | 0.14 s | 0.16 s | +| **Overlay traffic per node, each way** | 43 MB/s | **15.7 MB/s** | 14.2 MB/s | +| Leader and ballot timeouts, empty tx sets | 0 | 0 | 0 | +| Submitted against included at the end | 49,855 short (backlog) | 81 more (the backlog drained) | not compared (load stopped in a backlog) | +| Run verdict | FAIL (block time: p75 and p99) | FAIL (block time: p75) | FAIL (block time: p75 and p99) | - **Neither build holds 800 ms at 90 validators and 10,000 TPS.** With transactions sent to the leaders only the miss is 20 ms on the window's p75 instead of 72 ms, p99 stays inside its @@ -180,6 +216,41 @@ offered. Both runs missed their first window and the load was stopped after it. - There is no run of the base (up to experiment 3) at this fixture. In the #5502 column, the set's arrival, the build and the traffic are from that PR's first run at this fixture (`deec2d87c`, 1,046 ms); its rerun with `8aa7f6bf0` reported the same stage table. +- **Experiment 6 made this fixture worse in its one run**: 1,068 ms against 980 ms, with the + network in a backlog within the first minute. The next section says why. There is no run of + experiments 1 to 5 on m6id.8xlarge to separate the instance type from the change. + +### What the sixth experiment did at the goal fixture + +One run (`18a3a0238`, 90 x m6id.8xlarge, 2026-11-12), read from the logs of twelve nodes and +the network's metrics. Trigger to first ballot and the other stage rows of the table above are +means over the window, in which the network was already in a backlog. + +| | | +|---|---| +| Ledgers for which the leader used a set selected early | 75 of about 433 (17%); 269 times the selection was not finished when the previous ledger closed; none on a term's first ledger | +| Time the background job needs after the leader's execution | 0.40 s: validation 0.18 s (0.05 s before, when it decoded less), selection 0.22 s | +| Completing an early set at the close | 0.024 s, against a build of 0.30 s | +| Leader's cycle (previous close to close), early set / built set | 0.74 s (6 sampled ledgers) / 1.01 s (14) | +| Transactions in the first early set after a built one | 4,620, 4,875, 4,787, 5,022 on the sampled leaders (a ledger has to carry about 8,060) | +| Sets found invalid at apply; failed self-checks; dropped executions | 0; 0 of 432; 0 of 39,006 | + +1. **The selection was rarely ready.** It can only start when the leader has executed the + previous ledger, and it took 0.4 s, about what is left of that ledger's ballot. The estimate + it was built on was 0.15 to 0.3 s. +2. **Where it was ready, it did what it was built for**: the set was pushed when the previous + ledger closed, and those ledgers took 0.74 s instead of 1.01 s. +3. **But a set selected early is short when the ledger before it was built at its close.** It + holds what reached the mempool in the 0.3 to 0.5 s between that build's pull and its own. + What it leaves behind fills the next built set to the 10,000 cap. A capped set takes longer + to push, validate and agree on, so its ledger is late and the next trigger is already due. + In the first minute of the load 23 of 72 sets were at the cap and 12 held fewer than 6,000 + transactions; in the run without experiment 6 that minute had 2 capped sets and ran at + 805 ms. +4. **Moving the builder's checks gained little**: the structural check that stays in front of + the push takes 0.037 s at these sizes; the two that moved to the background took 0.05 s. +5. Not built: using an early set only when it is full (which is the backlog, where the build + is on the critical path), and a job that fits into the ballot. ### Where a ledger's time goes at 90 validators and 10,000 TPS @@ -550,6 +621,28 @@ Defaults that matter: validators as generators, `--scp-timeout-ms 2000`, ## Known limitations of the prototype +Tx sets selected during the previous ballot, and the leader's early execution: +- A set selected early holds what the mempool held when the previous ledger was executed. + Transactions that arrive during the rest of that ledger's ballot wait for the next set. + Submit-to-apply latency was not measured. +- The set still names its parent's header hash, which exists only once the parent is committed + (it contains the bucket-list hash), so the set cannot be pushed before the previous ledger + closes. Computing that hash earlier means running the commit's bucket-list work on copies + before externalize; not attempted. +- The first ledger of a leader term gains little: its leader executed the previous ledger as a + follower, shortly before consensus, and its selection is rarely ready in time. +- The selection runs on a background thread next to the validation tasks. With real transaction + execution it would compete with the apply thread for cores; apply is a sleep in every run + here. +- Time-bounded transactions of a set selected early are validated again on the main thread at + the close when the close time's second moved. The load in these runs has no time bounds. +- Re-reading the written set is no longer done before the push and the proposal. A fault that + only that check would catch closes a ledger without transactions and costs the leader its + term, where the builder used to stop. The structure of the selection and the shape of the + XDR are still checked first. +- The tie-break among candidates with equal fee rates in a set selected early is seeded from a + node secret; no test observes it. + Transactions to the leaders only: - No protocol gate and no version negotiation: a node without the change ignores the identity stream, and nodes with the change flood toward it. @@ -617,6 +710,10 @@ Leader schedule: Unchanged from stellar-core: see [INSTALL.md](./INSTALL.md), [CONTRIBUTING.md](./CONTRIBUTING.md) and [`./docs`](./docs). +Test status with experiment 6 (the commit before this README change): the full test suite +passes (657 test cases), also with `--all-versions`; the new tests also pass under +AddressSanitizer. + Test status with experiments 4 and 5 (`6ab837c27`, #5501 on top of #5502): the full test suite passes, also with `--all-versions`, including the Rust overlay's tests. diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 859b1d13f..e2e03d17b 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -205,14 +205,25 @@ selectPrewarmCandidates(std::vector&& envelopes, // ledger being closed (`closingTxs`, still in the mempool) and those whose // validity depends on the close time of the ledger they go into (their // result could not be reused). -std::vector +// With `forSelection` (the job goes on to select a set from what it pulled, +// for a close time it assumes), the time-dependent transactions of the +// windows are candidates too, and every pulled transaction that could go +// into the next ledger is decoded and returned in `mPulled`, in mempool +// order. +struct PrevalidationCandidates +{ + std::vector mToValidate; + std::vector mPulled; +}; + +PrevalidationCandidates selectPrevalidationCandidates(std::vector&& envelopes, std::vector const& windows, bool supportsSoroban, Hash const& networkID, UnorderedSet const& closingTxs, - std::atomic const& stop) + std::atomic const& stop, bool forSelection) { - std::vector selected; + PrevalidationCandidates res; std::vector taken(windows.size(), 0); for (auto const& env : envelopes) { @@ -221,9 +232,14 @@ selectPrevalidationCandidates(std::vector&& envelopes, break; } size_t const phase = isSorobanEnvelope(env) ? 1 : 0; - if (phase >= windows.size() || taken[phase] >= windows[phase] || - (phase == 1 && !supportsSoroban) || - txValidityDependsOnCloseTime(env)) + if (phase >= windows.size() || (phase == 1 && !supportsSoroban)) + { + continue; + } + bool const timeDependent = txValidityDependsOnCloseTime(env); + bool const validate = + taken[phase] < windows[phase] && (forSelection || !timeDependent); + if (!validate && !forSelection) { continue; } @@ -232,10 +248,94 @@ selectPrevalidationCandidates(std::vector&& envelopes, { continue; } - ++taken[phase]; - selected.push_back(std::move(tx)); + if (validate) + { + // The window counts the candidates whose results a later build + // can share, as it does without a selection. + if (!timeDependent) + { + ++taken[phase]; + } + res.mToValidate.push_back(tx); + } + if (forSelection) + { + res.mPulled.push_back(std::move(tx)); + } + } + return res; +} + +// The mempool is fee-ordered and sequence-number-oblivious, so it can hand +// out several transactions from one source account (e.g. a chained pair). A +// tx set may only contain one tx per source account, so keep the lowest +// sequence number per account and let the others wait for a later ledger. +void +keepOnePerSourceAccount(TxFrameList& txs) +{ + std::unordered_map firstBySource; + TxFrameList kept; + for (auto const& tx : txs) + { + auto [it, inserted] = + firstBySource.emplace(tx->getSourceID(), kept.size()); + if (inserted) + { + kept.push_back(tx); + } + else if (tx->getSeqNum() < kept[it->second]->getSeqNum()) + { + kept[it->second] = tx; + } + } + txs = std::move(kept); +} + +// One transaction per source account holds across the phases of a set, not +// only within each: of an account's classic and Soroban candidates, the one +// with the lower sequence number stays (the classic one if they are equal). +void +keepOnePerSourceAccount(TxFrameList& classicTxs, TxFrameList& sorobanTxs) +{ + keepOnePerSourceAccount(classicTxs); + keepOnePerSourceAccount(sorobanTxs); + if (classicTxs.empty() || sorobanTxs.empty()) + { + return; + } + std::unordered_map classicSeq; + for (auto const& tx : classicTxs) + { + classicSeq.emplace(tx->getSourceID(), tx->getSeqNum()); + } + std::unordered_set dropFromClassic; + TxFrameList keptSoroban; + for (auto const& tx : sorobanTxs) + { + auto it = classicSeq.find(tx->getSourceID()); + if (it == classicSeq.end()) + { + keptSoroban.push_back(tx); + } + else if (tx->getSeqNum() < it->second) + { + dropFromClassic.insert(it->first); + keptSoroban.push_back(tx); + } + } + sorobanTxs = std::move(keptSoroban); + if (!dropFromClassic.empty()) + { + TxFrameList keptClassic; + for (auto const& tx : classicTxs) + { + if (dropFromClassic.count(tx->getSourceID()) == 0) + { + keptClassic.push_back(tx); + } + } + classicTxs = std::move(keptClassic); } - return selected; } // The number of transactions in each phase of a generalized tx set. @@ -464,8 +564,7 @@ HerderImpl::computeTxFloodTargets() // Every validator of the schedule, in an order that does not change // from slot to slot (the overlay is only told when the list // changes). - std::vector everyone = - mHerderSCPDriver.getLeaderSchedule(slot); + std::vector everyone = mHerderSCPDriver.getLeaderSchedule(slot); if (everyone.empty()) { return std::nullopt; @@ -509,9 +608,8 @@ HerderImpl::computeTxFloodTargets() { auto const next = computeLeaderSchedule( lcl.hash, mHerderSCPDriver.getLeaderSchedule(slot)); - if (!next.empty() && - std::find(targets.begin(), targets.end(), next.front()) == - targets.end()) + if (!next.empty() && std::find(targets.begin(), targets.end(), + next.front()) == targets.end()) { targets.emplace_back(next.front()); } @@ -738,11 +836,11 @@ HerderImpl::checkTxAdmission(TransactionFrameBasePtr const& tx, validationLedgerSeq = lcl.header.ledgerSeq + 1; } auto diagnostics = DiagnosticEventManager::createDisabled(); - auto result = tx->checkValid( - mApp.getAppConnector(), *ledgerView, 0, 0, - getUpperBoundCloseTimeOffset(mApp, - getApplyTime(lcl.header.scpValue)), - diagnostics, validationLedgerSeq); + auto result = + tx->checkValid(mApp.getAppConnector(), *ledgerView, 0, 0, + getUpperBoundCloseTimeOffset( + mApp, getApplyTime(lcl.header.scpValue)), + diagnostics, validationLedgerSeq); if (!result->isSuccess()) { CLOG_DEBUG(Herder, "Rejecting transaction {} from {}: {}", @@ -1306,34 +1404,54 @@ HerderImpl::maybeStartSpeculativeApply(SCPEnvelope const& envelope) return; } + startSpeculativeApply(st.slotIndex, *value, nullptr); +} + +void +HerderImpl::startSpeculativeApply(uint64_t slotIndex, Value const& value, + TxSetXDRFrameConstPtr txSet) +{ + auto const& cfg = mApp.getConfig(); + if (!cfg.EXPERIMENTAL_SPECULATIVE_LEDGER_APPLY || + !cfg.backgroundLedgerApply()) + { + return; + } // Only the ledger right after the LCL can be executed, and only while // nothing else is being applied: the ledger apply thread executes it on // top of the state the LCL left. auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); - if (st.slotIndex != lcl.header.ledgerSeq + 1 || + if (slotIndex != lcl.header.ledgerSeq + 1 || !protocolValidatesTxSetsAtApply(lcl.header.ledgerVersion) || !isTracking() || !mLedgerManager.isSynced() || mLedgerManager.isApplying()) { return; } - if (mSpeculativeApplyValue && - mSpeculativeApplyValue->first == st.slotIndex && - mSpeculativeApplyValue->second == *value) + if (mSpeculativeApplyValue && mSpeculativeApplyValue->first == slotIndex && + mSpeculativeApplyValue->second == value) { // already handed over return; } StellarValue sv; - if (!toStellarValue(*value, sv) || isEmptyTxSetStellarValue(sv)) + if (!toStellarValue(value, sv) || isEmptyTxSetStellarValue(sv)) { // An empty-tx-set value takes no time to apply. return; } - auto txSetResult = mPendingEnvelopes.getTxSet(sv.txSetHash); - auto const* txSet = std::get_if(&txSetResult); - if (!txSet || !*txSet || (*txSet)->previousLedgerHash() != lcl.hash) + if (!txSet) + { + auto txSetResult = mPendingEnvelopes.getTxSet(sv.txSetHash); + if (auto const* received = + std::get_if(&txSetResult)) + { + txSet = *received; + } + } + if (!txSet || txSet->getContentsHash() != sv.txSetHash || + txSet->previousLedgerHash() != lcl.hash) { return; } @@ -1341,11 +1459,11 @@ HerderImpl::maybeStartSpeculativeApply(SCPEnvelope const& envelope) // Executing the ledger starts with validating its transaction set, which // verifies the signatures the prewarm has not reached itself, on all // cores; a prewarm that is still running would only compete with it. - if (mSignaturePrewarm && mSignaturePrewarm->ledgerSeq <= st.slotIndex) + if (mSignaturePrewarm && mSignaturePrewarm->ledgerSeq <= slotIndex) { stopSignaturePrewarm(); } - mSpeculativeApplyValue = std::make_pair(st.slotIndex, *value); + mSpeculativeApplyValue = std::make_pair(slotIndex, value); // A pre-validation that is still running was for a build on top of the // LCL that this node has not needed (it votes for another leader's // value): keep what it found, and leave the cores to the execution. @@ -1354,17 +1472,39 @@ HerderImpl::maybeStartSpeculativeApply(SCPEnvelope const& envelope) // leaves, to validate its candidates on while this ledger is in its // ballot. A ledger with upgrades is left out: they are applied when the // ledger commits, after that state. - LedgerManager::SpeculativeApplyExecuted onExecuted; - if (sv.upgrades.empty() && getSCP().isValidator() && + bool const prevalidate = + sv.upgrades.empty() && getSCP().isValidator() && !buildsLegacyTxSet(lcl.header.ledgerVersion) && - mayLeadAfter(st.slotIndex, sv, /*appliedTransactions=*/true)) + mayLeadAfter(slotIndex, sv, /*appliedTransactions=*/true); + // Executing a ledger starts with the full validation of its set. For a + // set this node built, that is the verdict every other node will reach + // when it applies the ledger: if it comes before the proposal (the + // leader executes a prepared set ahead of the trigger), an invalid set + // is not proposed. + bool const ownTxSet = mLastBuiltTxSet && + mLastBuiltTxSet->first == sv.txSetHash && + mLastBuiltTxSet->second == getConsensusTime(sv); + LedgerManager::SpeculativeApplyExecuted onExecuted; + if (prevalidate || ownTxSet) { - onExecuted = [this](std::shared_ptr state) { - startProposalPrevalidation(std::move(state)); + onExecuted = [this, prevalidate, ownTxSet, + ledgerSeq = static_cast(slotIndex), + txSetHash = sv.txSetHash]( + std::shared_ptr state) { + if (ownTxSet && state && state->mTxSetSkipped) + { + txSetSelfCheckFailed(txSetHash, ledgerSeq, + "this node's own execution of the " + "ledger found the set invalid"); + } + if (prevalidate) + { + startProposalPrevalidation(std::move(state)); + } }; } mLedgerManager.startSpeculativeApply( - LedgerCloseData(static_cast(st.slotIndex), *txSet, sv), + LedgerCloseData(static_cast(slotIndex), txSet, sv), std::move(onExecuted)); } @@ -1442,6 +1582,53 @@ HerderImpl::startProposalPrevalidation( job->value = state->mValue; job->start = std::chrono::steady_clock::now(); job->pendingTasks = tasks; + { + // The node first in line for the next ledger selects its set from + // what this job validates (see ProposalPrevalidation). The set is + // validated for the close time the next trigger is expected to + // have: the target after this ledger's close time, or now if that + // has passed. + auto const schedule = mHerderSCPDriver.getExpectedLeaderSchedule( + static_cast(state->mLedgerSeq) + 1, state->mValue, + state->mAppliedTxs > 0); + job->selectEarly = !schedule.empty() && + schedule[0] == getSCP().getLocalNodeID() && + !buildsLegacyTxSet(state->mHeader->ledgerVersion); + auto const version = state->mHeader->ledgerVersion; + auto const executedCloseTime = getConsensusTime(state->mValue); + auto const expectedTrigger = std::max( + executedCloseTime.toSystemTime() + + mLedgerManager.getExpectedLedgerCloseTime(), + mApp.getClock().system_now()); + auto const assumedCloseTime = + std::max(ConsensusTime::fromSystemTime(expectedTrigger, version), + executedCloseTime.next(version)); + job->assumedCloseTimeOffset = + (assumedCloseTime.toApplyTime() - getApplyTime(state->mValue)) + .seconds(); + job->validTxTargets = windows; + if (job->selectEarly) + { + // What orders candidates with equal fee rates must not be + // predictable by who submits them. The global random engine is + // not used: whether a job runs depends on timing, and a draw + // here would shift every later draw of this node. A hash keyed + // with a secret of this node is as unpredictable to others. + if (!mTieBreakSecret) + { + mTieBreakSecret = + sha256(mApp.getConfig().NODE_SEED.getStrKeySeed().value + + " tx set tie-break"); + } + SHA256 hasher; + hasher.add(*mTieBreakSecret); + hasher.add( + xdr::xdr_to_opaque(state->mLedgerSeq, state->mValue.txSetHash)); + auto const digest = hasher.finish(); + static_assert(sizeof(size_t) <= sizeof(digest)); + std::memcpy(&job->selectionSeed, digest.data(), sizeof(size_t)); + } + } mProposalPrevalidation = job; metrics.NewMeter({"herder", "prevalidate", "started"}, "prevalidation") .Mark(); @@ -1449,6 +1636,11 @@ HerderImpl::startProposalPrevalidation( "Pre-validating candidates for ledger {} on the executed state " "of ledger {}: {} pulled, {} tasks", state->mLedgerSeq + 1, state->mLedgerSeq, count, tasks); + if (job->selectEarly) + { + CLOG_INFO(Herder, "Selecting TX set for ledger {} early: {} pulled", + state->mLedgerSeq + 1, count); + } bool const supportsSoroban = protocolVersionStartsFrom( state->mHeader->ledgerVersion, SOROBAN_PROTOCOL_VERSION); @@ -1461,8 +1653,11 @@ HerderImpl::startProposalPrevalidation( for (size_t task = 0; task < tasks; ++task) { mApp.postOnBackgroundThread( + // The network ID by reference to the application's: the + // transaction frames made below keep a reference to it, and + // outlive this task. [job, state, payload, windows, supportsSoroban, validationLedgerSeq, - networkID = mApp.getNetworkID(), + &networkID = mApp.getNetworkID(), &app = mApp, &appConnector = mApp.getAppConnector(), &metrics, task, tasks]() { ZoneScopedN("proposal prevalidation"); { @@ -1477,6 +1672,8 @@ HerderImpl::startProposalPrevalidation( // Results go to the job a few at a time; once it is stopped // they are of no use to anyone. std::vector> batch; + std::vector> + timeDependentBatch; auto publish = [&]() { std::lock_guard lock(job->mutex); if (!job->stop.load()) @@ -1485,8 +1682,13 @@ HerderImpl::startProposalPrevalidation( job->published.end(), std::make_move_iterator(batch.begin()), std::make_move_iterator(batch.end())); + job->publishedTimeDependent.insert( + job->publishedTimeDependent.end(), + std::make_move_iterator(timeDependentBatch.begin()), + std::make_move_iterator(timeDependentBatch.end())); } batch.clear(); + timeDependentBatch.clear(); }; try { @@ -1494,10 +1696,12 @@ HerderImpl::startProposalPrevalidation( // Must not throw out of call_once. try { - job->candidates = selectPrevalidationCandidates( + auto selected = selectPrevalidationCandidates( OverlayIPC::parseTopTransactions(*payload), windows, supportsSoroban, networkID, - *state->mTxHashes, job->stop); + *state->mTxHashes, job->stop, job->selectEarly); + job->candidates = std::move(selected.mToValidate); + job->pulled = std::move(selected.mPulled); } catch (std::exception const& e) { @@ -1521,9 +1725,18 @@ HerderImpl::startProposalPrevalidation( break; } auto const& tx = candidates[i]; + // Only a job that selects a set has candidates + // whose validity depends on the close time: they + // are validated for the one the set assumes. + bool const timeDependent = + job->selectEarly && + txValidityDependsOnCloseTime(tx->getEnvelope()); + uint64_t const closeTimeOffset = + timeDependent ? job->assumedCloseTimeOffset : 0; TxValidationResult result; result.mValid = - tx->checkValid(appConnector, *view, 0, 0, 0, + tx->checkValid(appConnector, *view, 0, + closeTimeOffset, closeTimeOffset, diagnostics, validationLedgerSeq) ->isSuccess(); if (result.mValid) @@ -1536,8 +1749,9 @@ HerderImpl::startProposalPrevalidation( header, feeSource.current()); } } - batch.emplace_back(tx->getFullHash(), result); - if (batch.size() >= 32) + (timeDependent ? timeDependentBatch : batch) + .emplace_back(tx->getFullHash(), result); + if (batch.size() + timeDependentBatch.size() >= 32) { publish(); } @@ -1550,21 +1764,52 @@ HerderImpl::startProposalPrevalidation( CLOG_WARNING(Herder, "Proposal pre-validation failed: {}", e.what()); batch.clear(); + timeDependentBatch.clear(); std::lock_guard lock(job->mutex); job->stop.store(true); } publish(); + bool complete = false; + { + std::lock_guard lock(job->mutex); + // Complete only if every task ran to its end: a stopped + // job (by the build, or by a failure) is not. + if (--job->activeTasks == 0 && job->pendingTasks == 0 && + !job->stop.load()) + { + job->finishedAfterNs = + std::chrono::duration_cast< + std::chrono::nanoseconds>( + std::chrono::steady_clock::now() - job->start) + .count(); + complete = true; + } + } + if (!complete || !job->selectEarly) + { + return; + } + // Every candidate has its result and no other task touches + // the job's transactions any more: select the set. + std::shared_ptr early; + try + { + early = selectEarlyTxSet(app, *job, state); + } + catch (std::exception const& e) + { + // The ledger gets a set built the usual way. + CLOG_WARNING(Herder, + "Could not select the TX set for ledger {} " + "ahead of time: {}", + state->mLedgerSeq + 1, e.what()); + } std::lock_guard lock(job->mutex); - // Complete only if every task ran to its end: a stopped job - // (by the build, or by a failure) is not. - if (--job->activeTasks == 0 && job->pendingTasks == 0 && - !job->stop.load()) + if (early && !job->stop.load()) { - job->finishedAfterNs = - std::chrono::duration_cast( - std::chrono::steady_clock::now() - job->start) - .count(); + job->early = std::move(early); } + job->selectionDone.store(true); }, "Herder: proposal prevalidation"); } @@ -1644,13 +1889,19 @@ HerderImpl::expectedTxSetBuildDuration() const // Until this node has built a set of some size: what a build costs per // transaction on the slower of the machines measured (about 0.17 s for // 4,000 transactions with most candidates validated ahead of time). + return std::max(mLastTxSetBuildDuration, + expectedTxSetBuildDuration(mLastClosedTxSetSize)); +} + +std::chrono::milliseconds +HerderImpl::expectedTxSetBuildDuration(size_t txs) const +{ static std::chrono::nanoseconds const DEFAULT_COST_PER_TX{40'000}; auto const perTx = mTxSetBuildCostPerTx > std::chrono::nanoseconds::zero() ? mTxSetBuildCostPerTx : DEFAULT_COST_PER_TX; - auto const bySize = std::chrono::duration_cast( - perTx * static_cast(mLastClosedTxSetSize)); - return std::max(mLastTxSetBuildDuration, bySize); + return std::chrono::duration_cast( + perTx * static_cast(txs)); } std::chrono::milliseconds @@ -2557,42 +2808,8 @@ HerderImpl::setupTriggerNextLedger() }, &VirtualTimer::onFailureNoop); - // Prepare early only if the trigger is still ahead. A due trigger - // fires now and builds itself; both timers would fire at the same - // instant in an unspecified order (event sequence numbers are per - // timer), so the leader could build once to prepare and again at the - // trigger. - if (getSCP().isValidator() && !triggerDue) - { - if (mHerderSCPDriver.isLocalNodeRoundLeader(nextIndex, 1)) - { - // Validate against the close time we expect at the trigger, - // not the earlier wall-clock time at which we start work. - auto closeTime = std::max( - minCandidateCt, - ConsensusTime::fromSystemTime( - mApp.getClock().system_now() + - std::chrono::duration_cast< - VirtualClock::system_time_point::duration>( - triggerTime - now), - lcl.header.ledgerVersion)); - // Build shortly before the trigger rather than right away: - // the set then holds what arrived in the meantime, and is - // still built and on its way to the peers when consensus - // starts (see makeProposalValue). This also yields out of - // ledger close before touching its read view. - mPrepareTxSetTimer.expires_at(std::max( - now, triggerTime - - prepareTxSetLeadTime( - milliseconds, expectedTxSetBuildDuration()))); - mPrepareTxSetTimer.async_wait( - [this, nextIndex, closeTime]() { - prepareTxSet(static_cast(nextIndex), - closeTime); - }, - &VirtualTimer::onFailureNoop); - } - } + scheduleTxSetPreparation(nextIndex, triggerTime, triggerDue, now, + milliseconds, minCandidateCt); } #ifdef BUILD_TESTS @@ -2600,6 +2817,72 @@ HerderImpl::setupTriggerNextLedger() #endif } +void +HerderImpl::scheduleTxSetPreparation( + uint64_t nextIndex, VirtualClock::time_point triggerTime, bool triggerDue, + VirtualClock::time_point now, std::chrono::milliseconds expectedCloseTime, + ConsensusTime minCandidateCt) +{ + // Arm the prepare timer only if the trigger is still ahead. A due + // trigger fires now and builds itself; both timers would fire at the + // same instant in an unspecified order (event sequence numbers are per + // timer), so the leader could build once to prepare and again at the + // trigger. What a due trigger can still use is a set this node selected + // ahead of time: that one is completed right here, before the trigger's + // handler runs. + bool const mayHaveEarlyTxSet = + mProposalPrevalidation && mProposalPrevalidation->selectEarly; + if (!getSCP().isValidator() || (triggerDue && !mayHaveEarlyTxSet) || + !mHerderSCPDriver.isLocalNodeRoundLeader(nextIndex, 1)) + { + return; + } + auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); + // Validate against the close time we expect at the trigger, not the + // earlier wall-clock time at which we start work. + auto closeTime = std::max( + minCandidateCt, ConsensusTime::fromSystemTime( + mApp.getClock().system_now() + + std::chrono::duration_cast< + VirtualClock::system_time_point::duration>( + triggerTime - now), + lcl.header.ledgerVersion)); + // Build shortly before the trigger rather than right away: the set then + // holds what arrived in the meantime, and is still built and on its way + // to the peers when consensus starts (see makeProposalValue). This also + // yields out of ledger close before touching its read view. + auto leadTime = + prepareTxSetLeadTime(expectedCloseTime, expectedTxSetBuildDuration()); +#ifdef BUILD_TESTS + if (mPrepareLeadTimeForTesting) + { + leadTime = *mPrepareLeadTimeForTesting; + } +#endif + auto const prepareAt = std::max(now, triggerTime - leadTime); + // If that build would have to start right now (the previous ledger + // closed less than a lead time before the trigger, or after it), its + // time is on the ledger's critical path. This node may have selected + // the set while the previous ledger was in its ballot: then it only has + // to be completed. Whenever a build still fits before the trigger it is + // preferred, because it holds what arrived since. + bool prepared = false; + if (buildMustStartNow(triggerDue, prepareAt, now)) + { + prepared = + prepareEarlyTxSet(static_cast(nextIndex), closeTime); + } + if (!prepared && !triggerDue) + { + mPrepareTxSetTimer.expires_at(prepareAt); + mPrepareTxSetTimer.async_wait( + [this, nextIndex, closeTime]() { + prepareTxSet(static_cast(nextIndex), closeTime); + }, + &VirtualTimer::onFailureNoop); + } +} + // Returns the ledger index of the oldest ledger that it is safe to delete while // still keeping all the information needed to publish checkpoints static uint32_t @@ -2756,6 +3039,454 @@ HerderImpl::prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) "close time {}", ledgerSeq, mPreparedTxSet->txSet->sizeTxTotal(), closeTime.toString()); + executePreparedTxSet(); +} + +std::shared_ptr +HerderImpl::selectEarlyTxSet( + Application& app, ProposalPrevalidation& job, + std::shared_ptr const& state) +{ + ZoneScoped; + auto& metrics = app.getMetrics(); + auto timer = + metrics.NewTimer({"herder", "txset", "early-selection"}).TimeScope(); + + // What the tasks found. Results for time-dependent transactions hold for + // the close time this selection assumes, and are used here only. + TxValidationCache results; + { + std::lock_guard lock(job.mutex); + if (job.stop.load()) + { + return nullptr; + } + results.reserve(job.published.size() + + job.publishedTimeDependent.size()); + results.insert(job.published.begin(), job.published.end()); + results.insert(job.publishedTimeDependent.begin(), + job.publishedTimeDependent.end()); + } + + auto early = std::make_shared(); + early->ledgerSeq = state->mLedgerSeq + 1; + early->state = state; + early->closeTimeOffset = job.assumedCloseTimeOffset; + // The limits of the ledger before the executed one. The executed value + // carries no upgrades, so they are the executed ledger's as well; the + // node checks that against the closed ledger before it uses the set. + auto const& previous = *state->mPreviousLedger; + early->limits.mLclHeader = previous.getLastClosedLedgerHeader().header; + early->limits.mSorobanConfig = + previous.hasSorobanConfig() ? &previous.getSorobanConfig() : nullptr; + releaseAssert(early->limits.mSorobanConfig); + + // As HerderImpl::buildTxSet: phases, one transaction per source account. + TxFrameList classicTxs; + TxFrameList sorobanTxs; + for (auto const& tx : job.pulled) + { + (tx->isSoroban() ? sorobanTxs : classicTxs).push_back(tx); + } + keepOnePerSourceAccount(classicTxs, sorobanTxs); + PerPhaseTransactionList txPhases; + txPhases.emplace_back(std::move(classicTxs)); + txPhases.emplace_back(std::move(sorobanTxs)); + PerPhaseTransactionList invalidTxPhases; + invalidTxPhases.resize(txPhases.size()); + + // Validity is read from the executed state. Its header has no hash yet; + // validation reads the ledger number and the protocol version. This + // thread validates what the tasks did not (candidates past their + // windows) by itself: the batch executor belongs to the main thread and + // to ledger apply. + LedgerHeaderHistoryEntry executed; + executed.header = *state->mHeader; + TxSetValidationContext const ctx{ + executed, early->limits.mSorobanConfig, + [&state, &metrics]() { return state->makeView(metrics); }, 1, &results}; + auto phases = TxSetBuilder::selectPhases( + txPhases, app, early->limits, ctx, + ApplyTimeOffset::fromSeconds(job.assumedCloseTimeOffset), + invalidTxPhases, job.validTxTargets, "early", job.selectionSeed); + if (job.stop.load()) + { + return nullptr; + } + // What a build checks before its set leaves it (see + // makeTxSetFromTransactions): the structure of the selection. + auto const selectionCheck = TxSetBuilder::checkSelection( + phases, app, ctx, /*isGeneralized=*/true, + ApplyTimeOffset::fromSeconds(job.assumedCloseTimeOffset)); + if (selectionCheck != TxSetValidationResult::VALID) + { + throw std::runtime_error( + fmt::format(FMT_STRING("the selection is not a valid tx set: {:s}"), + toString(selectionCheck))); + } + for (auto const& phase : phases) + { + for (auto const& tx : phase) + { + if (txValidityDependsOnCloseTime(tx->getEnvelope())) + { + early->timeDependentTxs.push_back(tx); + } + } + } + early->txSet = TxSetBuilder::makeUnstamped( + std::move(phases), early->limits.mLclHeader.ledgerVersion); + + // As HerderImpl::buildTxSet: the candidates that failed validation leave + // the mempool, except those with a future sequence number, which are + // chained behind a pending transaction of the same account. + auto view = state->makeView(metrics); + for (auto const& phase : invalidTxPhases) + { + for (auto const& tx : phase) + { + auto acc = view->getAccount(tx->getSourceID()); + if (acc && + tx->getSeqNum() > acc.current().data.account().seqNum + 1) + { + continue; + } + // Found invalid for the close time this selection assumes; the + // set may get another one, at which a time-dependent + // transaction can be valid. Those stay in the mempool. + if (txValidityDependsOnCloseTime(tx->getEnvelope())) + { + continue; + } + early->invalidTxHashes.push_back(tx->getFullHash()); + } + } + early->selectedAt = std::chrono::steady_clock::now(); + metrics.NewMeter({"herder", "txset", "early-selected"}, "txset").Mark(); + CLOG_INFO(Herder, + "Selected TX set for ledger {} early, on the executed state of " + "ledger {}: {} transactions", + early->ledgerSeq, state->mLedgerSeq, early->txSet.sizeTxTotal()); + return early; +} + +bool +HerderImpl::prepareEarlyTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) +{ + ZoneScoped; + releaseAssert(threadIsMain()); + auto job = mProposalPrevalidation; + if (!job || !job->selectEarly || mLedgerManager.isApplying()) + { + return false; + } + auto const lcl = mLedgerManager.getLastClosedLedgerHeader(); + // The job describes the state one particular ledger leaves when it + // closes with one particular value. Only if that is the LCL is its set a + // set for the next ledger: executing a ledger is deterministic, so the + // state the set was selected on is then the state the LCL left (the + // value has no upgrades, see maybeStartSpeculativeApply). + if (ledgerSeq != lcl.header.ledgerSeq + 1 || + job->ledgerSeq != lcl.header.ledgerSeq || + !(job->value == lcl.header.scpValue)) + { + return false; + } + auto& metrics = mApp.getMetrics(); + std::shared_ptr early; + { + std::lock_guard lock(job->mutex); + early = std::move(job->early); + } + if (!early) + { + // The selection has not finished (or failed): the usual build takes + // the validation results that exist. + metrics.NewMeter({"herder", "txset", "early-not-ready"}, "txset") + .Mark(); + return false; + } + auto discard = [&](char const* why) { + metrics.NewMeter({"herder", "txset", "early-discarded"}, "txset") + .Mark(); + CLOG_INFO(Herder, + "Not using the TX set selected early for ledger {}: {}", + ledgerSeq, why); + return false; + }; + { + // The set holds what was in the mempool when the previous ledger + // was executed. That age is the price of not building now, and it + // buys something only if a build would take time: a set of few + // transactions is built in no time from what the mempool holds + // now. In particular an empty selection is never proposed: more may + // have arrived since, and a ledger that applies nothing costs its + // leader the term. + auto minSaving = EARLY_TX_SET_MIN_BUILD_SAVING; +#ifdef BUILD_TESTS + if (mEarlyTxSetMinBuildSavingForTesting) + { + minSaving = *mEarlyTxSetMinBuildSavingForTesting; + } +#endif + auto const txs = early->txSet.sizeTxTotal(); + if (txs == 0 || expectedTxSetBuildDuration(txs) < minSaving) + { + metrics.NewMeter({"herder", "txset", "early-small"}, "txset") + .Mark(); + return discard("a build of so few transactions is quick"); + } + } + if (early->ledgerSeq != ledgerSeq || + buildsLegacyTxSet(lcl.header.ledgerVersion) || + !lcl.header.scpValue.upgrades.empty()) + { + return discard("it is not for this ledger"); + } + // Nothing but an upgrade changes the limits a set is selected within, + // and the executed value had none. Checked all the same. + if (!TxSetBuildLimits::forLCL(mApp).sameAs(early->limits)) + { + return discard("the limits of the ledger changed"); + } + if (!(closeTime > getConsensusTime(lcl.header.scpValue))) + { + return discard("the close time is not after the last one"); + } + auto const closeTimeOffset = + closeTime.toApplyTime() - getApplyTime(lcl.header.scpValue); + if (closeTimeOffset.seconds() != early->closeTimeOffset && + !early->timeDependentTxs.empty()) + { + // The set gets another close time than the one it was validated + // for: its time-dependent transactions are validated again, now on + // the closed ledger. If one of them is no longer valid the set + // would have to be selected again, which is what the build does. + UnorderedMap accountFeeMap; + auto const invalid = TxSetUtils::getInvalidTxListWithErrors( + early->timeDependentTxs, mApp, accountFeeMap, + closeTimeOffset.seconds(), closeTimeOffset.seconds()); + if (!invalid.first.empty()) + { + return discard("a time-dependent transaction is not valid for " + "the close time"); + } + } + + auto timer = + metrics.NewTimer({"herder", "txset", "early-finalize"}).TimeScope(); + auto const txCount = early->txSet.sizeTxTotal(); + std::shared_ptr selfCheck; + auto [txSet, applicableTxSet] = TxSetBuilder::stamp( + std::move(early->txSet), mApp, lcl, closeTimeOffset, selfCheck); +#ifdef BUILD_TESTS + overrideBuiltTxSetForTesting(txSet, selfCheck); +#endif + mLastBuiltTxSet = std::make_pair(txSet->getContentsHash(), closeTime); + if (mTxSetSelfCheckFailed) + { + // A self-check failed before: check before use, as a build does. A + // set that fails is dropped here; the build that follows checks its + // own result (and throws if that fails too). + if (auto const failure = selfCheck->run(mApp)) + { + CLOG_ERROR(Herder, + "TX set selected early for ledger {} failed the " + "builder's self-check: {}", + ledgerSeq, *failure); + metrics.NewMeter({"herder", "txset", "self-check-failed"}, "txset") + .Mark(); + return discard("it failed the builder's self-check"); + } + selfCheck.reset(); + } + // The set is proposed unchanged at the trigger (makeProposalValue), so + // it goes to the peers now. + handOffTxSet(txSet, ledgerSeq, /*pushToPeers=*/true); + startTxSetSelfCheck(selfCheck, ledgerSeq); + mPrepareTxSetTimer.cancel(); + mPreparedTxSet = PreparedTxSet{lcl.hash, + ledgerSeq, + closeTime, + txSet, + std::move(applicableTxSet), + std::move(early->invalidTxHashes)}; + timer.Stop(); + // The job's validation results stay available to a later build on this + // ledger (a rebuild at the trigger, a later round's). + validationCacheFor(lcl); + metrics.NewMeter({"herder", "txset", "early-finalized"}, "txset").Mark(); + metrics.NewTimer({"herder", "txset", "early-lead"}) + .Update(std::chrono::steady_clock::now() - early->selectedAt); + CLOG_INFO(Herder, + "Prepared TX set for ledger {} before trigger: {} transactions, " + "close time {} (selected early)", + ledgerSeq, txCount, closeTime.toString()); + executePreparedTxSet(); + return true; +} + +void +HerderImpl::handOffTxSet(TxSetXDRFrameConstPtr const& txSet, uint32_t ledgerSeq, + bool pushToPeers) +{ + auto t = mApp.getMetrics() + .NewTimer({"herder", "txset", "build-handoff"}) + .TimeScope(); + // Legacy (pre-protocol 20) sets cannot be carried by the Rust overlay. + if (!txSet->isGeneralizedTxSet()) + { + return; + } + // Serialized straight into the IPC message, without copying the set's + // XDR first. The overlay's eager compression runs concurrently in Rust. + if (mApp.getOverlayManager().cacheTxSet( + txSet->getContentsHash(), txSet->getGeneralizedXDR(), ledgerSeq)) + { + mTxSetsStoredByOverlay.insert(txSet->getContentsHash()); + } + // Leader's push: this set is about to be proposed, so send it to the + // peers now. + if (pushToPeers) + { + pushTxSetToPeers(txSet, ledgerSeq); + } +} + +void +HerderImpl::executePreparedTxSet() +{ + if (!mPreparedTxSet || !mPreparedTxSet->txSet || + !mPreparedTxSet->applicableTxSet) + { + return; + } +#ifdef BUILD_TESTS + // The trigger proposes something else than the prepared set. + if (mProposedTxSetOverride || + mApp.getConfig().TESTING_PROPOSE_RANDOM_VALUES) + { + return; + } +#endif + auto const lcl = mLedgerManager.getLastClosedLedgerHeader(); + if (mPreparedTxSet->ledgerSeq != lcl.header.ledgerSeq + 1 || + mPreparedTxSet->previousLedgerHash != lcl.hash || + !getSCP().isValidator()) + { + return; + } + // The value makeProposalValue builds at the trigger, if nothing changes + // until then. + auto const value = makeStellarValue( + mPreparedTxSet->txSet->getContentsHash(), mPreparedTxSet->closeTime, + proposalUpgrades(lcl), mApp.getConfig().NODE_SEED); + startSpeculativeApply(mPreparedTxSet->ledgerSeq, + Value(xdr::xdr_to_opaque(value)), + mPreparedTxSet->txSet); +} + +void +HerderImpl::startTxSetSelfCheck(std::shared_ptr check, + uint32_t ledgerSeq) +{ + if (!check) + { + return; + } + mApp.postOnBackgroundThread( + [this, check = std::move(check), ledgerSeq, &app = mApp]() { + ZoneScopedN("tx set self-check"); + auto const failure = check->run(app); + if (!failure) + { + app.getMetrics() + .NewMeter({"herder", "txset", "self-check-passed"}, "txset") + .Mark(); + return; + } + app.postOnMainThread( + [this, check, ledgerSeq, reason = *failure]() { + txSetSelfCheckFailed(check->getTxSet()->getContentsHash(), + ledgerSeq, reason); + }, + "tx set self-check failed"); + }, + "Herder: tx set self-check"); +} + +void +HerderImpl::txSetSelfCheckFailed(Hash const& txSetHash, uint32_t ledgerSeq, + std::string const& reason) +{ + releaseAssert(threadIsMain()); + CLOG_ERROR(Herder, + "TX set {} built for ledger {} failed the builder's " + "self-check: {}", + hexAbbrev(txSetHash), ledgerSeq, reason); + CLOG_ERROR(Herder, "{}", REPORT_INTERNAL_BUG); + mApp.getMetrics() + .NewMeter({"herder", "txset", "self-check-failed"}, "txset") + .Mark(); + // No more unchecked sets from this process. + mTxSetSelfCheckFailed = true; + if (mPreparedTxSet && mPreparedTxSet->txSet && + mPreparedTxSet->txSet->getContentsHash() == txSetHash) + { + // Not proposed yet: the trigger builds again, and checks what it + // builds before using it. + discardPreparedTxSet(); + } +} + +#ifdef BUILD_TESTS +void +HerderImpl::overrideBuiltTxSetForTesting( + TxSetXDRFrameConstPtr& txSet, + std::shared_ptr& selfCheck) +{ + if (!mBuiltTxSetOverrideForTesting) + { + return; + } + txSet = mBuiltTxSetOverrideForTesting(txSet); + if (selfCheck) + { + selfCheck = selfCheck->withTxSetForTesting(txSet); + } +} +#endif + +xdr::xvector +HerderImpl::proposalUpgrades(LedgerHeaderHistoryEntry const& lcl) +{ + auto newUpgrades = emptyUpgradeSteps; + + // see if we need to include some upgrades + std::vector upgrades; + { + auto ledgerView = mApp.getLedgerManager().getLCLView(); + upgrades = mUpgrades.createUpgradesFor(lcl.header, *ledgerView, + mApp.getConfig()); + } + for (auto const& upgrade : upgrades) + { + Value v(xdr::xdr_to_opaque(upgrade)); + if (v.size() >= UpgradeType::max_size()) + { + CLOG_ERROR( + Herder, + "HerderImpl::triggerNextLedger exceeded size for upgrade " + "step (got {} ) for upgrade type {}", + v.size(), upgrade.type()); + CLOG_ERROR(Herder, "{}", REPORT_INTERNAL_BUG); + } + else + { + newUpgrades.emplace_back(v.begin(), v.end()); + } + } + return newUpgrades; } void @@ -2798,7 +3529,6 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime, PerPhaseTransactionList txPhases; // Get TXs from Rust overlay - auto& overlayMgr = mApp.getOverlayManager(); // This build validates every candidate itself. stopSignaturePrewarm(); auto& metrics = mApp.getMetrics(); @@ -2842,31 +3572,8 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime, classicTxs.push_back(txFrame); } } - // The mempool is fee-ordered and sequence-number-oblivious, so it can - // hand us several transactions from one source account (e.g. a chained - // pair). A tx set may only contain one tx per source account, so keep - // the lowest sequence number per account and let the others wait for a - // later ledger. - auto onePerSourceAccount = [](TxFrameList& txs) { - std::unordered_map firstBySource; - TxFrameList kept; - for (auto const& tx : txs) - { - auto [it, inserted] = - firstBySource.emplace(tx->getSourceID(), kept.size()); - if (inserted) - { - kept.push_back(tx); - } - else if (tx->getSeqNum() < kept[it->second]->getSeqNum()) - { - kept[it->second] = tx; - } - } - txs = std::move(kept); - }; - onePerSourceAccount(classicTxs); - onePerSourceAccount(sorobanTxs); + // One transaction per source account. + keepOnePerSourceAccount(classicTxs, sorobanTxs); txPhases.emplace_back(std::move(classicTxs)); if (supportsSoroban) @@ -2885,6 +3592,15 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime, uint64_t sigCacheHits = 0; uint64_t sigCacheMisses = 0; PubKeyUtils::getVerifySigCacheCounts(sigCacheHits, sigCacheMisses); + // The builder's check of its own set does not hold up the set: it runs + // in the background (startTxSetSelfCheck), unless one has failed before. + std::shared_ptr selfCheck; + // Only where every node validates the set when it applies the ledger: + // before that protocol the followers validate before they vote, and + // this node records its own set as valid for them (cacheValidTxSet). + bool const deferSelfCheck = + !legacyTxSet && !mTxSetSelfCheckFailed && + protocolValidatesTxSetsAtApply(lcl.header.ledgerVersion); std::tie(proposedSet, applicableProposedSet) = makeTxSetFromTransactions( txPhases, mApp, closeTimeOffset, invalidTxPhases #ifdef BUILD_TESTS @@ -2893,32 +3609,19 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime, #endif , [&](TxSetXDRFrameConstPtr const& txSet) { - auto t = metrics.NewTimer({"herder", "txset", "build-handoff"}) - .TimeScope(); - // Hand the final XDR to the overlay before the builder's remaining - // validation. Its eager compression runs concurrently in Rust. - if (txSet->isGeneralizedTxSet()) - { - // Serialized straight into the IPC message, without copying - // the set's XDR first. - if (overlayMgr.cacheTxSet(txSet->getContentsHash(), - txSet->getGeneralizedXDR(), - ledgerSeq)) - { - mTxSetsStoredByOverlay.insert(txSet->getContentsHash()); - } - // Leader's push: this set is about to be proposed, so send - // it to the peers now, while the builder's remaining - // validation and the proposal's persistence still run. If - // that validation fails nothing is proposed, and peers drop - // the unused set. - if (pushToPeers) - { - pushTxSetToPeers(txSet, ledgerSeq); - } - } + // Hand the final XDR to the overlay, and with `pushToPeers` to + // the peers, before the builder's check of the set. If that + // check fails before the proposal, nothing is proposed, and + // peers drop the unused set. + handOffTxSet(txSet, ledgerSeq, pushToPeers); }, - targets, validationCacheFor(lcl)); + targets, validationCacheFor(lcl), + deferSelfCheck ? &selfCheck : nullptr); +#ifdef BUILD_TESTS + overrideBuiltTxSetForTesting(proposedSet, selfCheck); +#endif + mLastBuiltTxSet = std::make_pair(proposedSet->getContentsHash(), closeTime); + startTxSetSelfCheck(selfCheck, ledgerSeq); markSignatureCacheLookups(mApp, "build", sigCacheHits, sigCacheMisses); CLOG_INFO(Herder, "Proposed TX set has {} transactions", proposedSet->sizeTxTotal()); @@ -3475,32 +4178,7 @@ HerderImpl::makeProposalValue(uint32_t ledgerSeqToTrigger) return nullptr; } - auto newUpgrades = emptyUpgradeSteps; - - // see if we need to include some upgrades - std::vector upgrades; - { - auto ledgerView = mApp.getLedgerManager().getLCLView(); - upgrades = mUpgrades.createUpgradesFor(lcl.header, *ledgerView, - mApp.getConfig()); - } - for (auto const& upgrade : upgrades) - { - Value v(xdr::xdr_to_opaque(upgrade)); - if (v.size() >= UpgradeType::max_size()) - { - CLOG_ERROR( - Herder, - "HerderImpl::triggerNextLedger exceeded size for upgrade " - "step (got {} ) for upgrade type {}", - v.size(), upgrade.type()); - CLOG_ERROR(Herder, "{}", REPORT_INTERNAL_BUG); - } - else - { - newUpgrades.emplace_back(v.begin(), v.end()); - } - } + auto const newUpgrades = proposalUpgrades(lcl); #ifdef BUILD_TESTS if (mApp.getConfig().TESTING_PROPOSE_RANDOM_VALUES && diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index c4f1b9369..4f43e7fbd 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -152,6 +152,13 @@ class HerderImpl : public Herder // apply thread for speculative execution (see // LedgerManager::startSpeculativeApply), unless it was already. void maybeStartSpeculativeApply(SCPEnvelope const& envelope); + // Hands the ledger `slotIndex` with `value` to the apply thread for + // speculative execution if it is the ledger after the LCL, nothing is + // being applied and it was not handed over already. `txSet` is the + // value's transaction set if the caller holds it; otherwise it is looked + // up among the sets this node has received. + void startSpeculativeApply(uint64_t slotIndex, Value const& value, + TxSetXDRFrameConstPtr txSet); #ifdef BUILD_TESTS TxSubmitStatus recvTransaction(TransactionFrameBasePtr tx, @@ -190,6 +197,21 @@ class HerderImpl : public Herder mGetTopTransactionsForTesting; friend class LeaderBuildTestAccess; friend class ProposalPrevalidationTestAccess; + // When set, replaces the lead time before the trigger at which the + // first-round leader builds (prepareTxSetLeadTime): a lead time as long + // as the target makes every build one that has to start at the close, + // which is when a set selected early is used. + std::optional mPrepareLeadTimeForTesting; + // When set, replaces EARLY_TX_SET_MIN_BUILD_SAVING: zero lets a test use + // a set of a few transactions that was selected early. + std::optional + mEarlyTxSetMinBuildSavingForTesting; + + // When set, a set this node just built as a leader is replaced by what + // this returns, for the proposal and for the builder's self-check alike: + // lets a test make the node build a broken set. + std::function + mBuiltTxSetOverrideForTesting; std::optional mMaxClassicTxSize; std::optional mMaxTxSizeOverride; @@ -345,6 +367,8 @@ class HerderImpl : public Herder // rose (every node's first term under load starts that way), and a lead // time sized for it would make the first ledger of the term late. std::chrono::milliseconds expectedTxSetBuildDuration() const; + // What a build of a set of `txs` transactions costs at this node's rate. + std::chrono::milliseconds expectedTxSetBuildDuration(size_t txs) const; std::optional mPreparedTxSet; // `pushToPeers`: this is the set the node is about to propose as a round // leader, so the overlay sends it to every peer as soon as its XDR exists @@ -359,6 +383,60 @@ class HerderImpl : public Herder void prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); void discardPreparedTxSet(); ValueWrapperPtr makeProposalValue(uint32_t ledgerSeq); + // The upgrades a value proposed now on top of `lcl` carries. + xdr::xvector + proposalUpgrades(LedgerHeaderHistoryEntry const& lcl); + // Hands a set this node built to the overlay, which keeps it for peers + // that ask and, with `pushToPeers`, sends it to every peer at once. + void handOffTxSet(TxSetXDRFrameConstPtr const& txSet, uint32_t ledgerSeq, + bool pushToPeers); + + // The leader executes its own set as soon as it can. + // + // A prepared set is proposed unchanged at the trigger, with the close + // time it was prepared for. So the value this node will propose is known + // as soon as the set is: the node hands the ledger to the apply thread + // right then (startSpeculativeApply) instead of when it emits its first + // ballot statement at the trigger, and the execution runs beside the + // set's dissemination and the wait for the trigger. If the trigger ends + // up proposing another value (the prepared set was dropped, the upgrades + // to propose changed), the statement it emits replaces the execution, as + // any change of vote does. Called whenever mPreparedTxSet has been set. + void executePreparedTxSet(); + + // The builder's self-check (see TxSetSelfCheck) of a set built by this + // node as a leader runs on a background thread, so that it holds up + // neither the push of the set nor its proposal. If it fails, the set may + // already be proposed: see txSetSelfCheckFailed. + void startTxSetSelfCheck(std::shared_ptr check, + uint32_t ledgerSeq); + // Called on the main thread when a self-check failed, or when this + // node's own execution of a set it built found the set invalid. The + // failure is logged and counted. A set that has not been proposed yet is + // dropped, + // so that the trigger builds another one. A proposed set cannot be + // recalled; if it is invalid, every node finds that at apply and closes + // the ledger without applying it. From now on this node checks every set + // before it uses it (mTxSetSelfCheckFailed), where a failure throws. + void txSetSelfCheckFailed(Hash const& txSetHash, uint32_t ledgerSeq, + std::string const& reason); + bool mTxSetSelfCheckFailed{false}; + // The last set this node built itself (by a build, or by completing a + // set it selected early), and the close time it built it for. When the + // node's own apply thread executes that set with that close time and + // finds it invalid, that is a verdict on this node's builder (see + // startSpeculativeApply). With another close time it is not: another + // leader may propose this node's set for a time it was not built for. + std::optional> mLastBuiltTxSet; + // Keys the tie-break seed of early selections; derived from the node + // seed on first use. + std::optional mTieBreakSecret; +#ifdef BUILD_TESTS + // Applies mBuiltTxSetOverrideForTesting to a set that was just built. + void overrideBuiltTxSetForTesting( + TxSetXDRFrameConstPtr& txSet, + std::shared_ptr& selfCheck); +#endif // Whether a ledger closing on an LCL with this protocol uses a legacy // (non-generalized) tx set, which every validator builds locally. static bool buildsLegacyTxSet(uint32_t lclLedgerVersion); @@ -408,6 +486,40 @@ class HerderImpl : public Herder // ExecutedLedgerState). When N has closed with the value that was // executed, the build of N+1 takes those results instead of validating // the same transactions on the main thread (see mValidationCache). + // + // Early selection. The node that expects to lead round 1 of N+1 goes one + // step further: when the validation tasks are done, the last of them + // selects the set for N+1 on its own thread, from everything the job + // pulled, against the executed state of N (validity) and the limits of + // N-1 (which are N's: the executed value carries no upgrades). The + // result (EarlyTxSet) lacks only what does not exist before N closes: + // the hash of N as the set's parent, and with it the set's own hash. + // When N has closed with the executed value and the build of N+1 would + // have to start at once, the node completes the selection instead of + // building (prepareEarlyTxSet): the build is off the critical path of + // the ledger. Transactions that reach the mempool after the job's pull + // wait for the next ledger. + struct EarlyTxSet + { + // The ledger the set is for (N+1). + uint32_t ledgerSeq{0}; + UnstampedTxSet txSet; + // The state the set was selected on; keeps `limits` alive. + std::shared_ptr state; + // The limits the selection assumed. They are compared with those of + // the closed ledger before the set is used. + TxSetBuildLimits limits; + // The close time the set was validated for, as an apply-time offset + // from the executed ledger. + uint64_t closeTimeOffset{0}; + // The transactions of the set whose validity depends on the close + // time: validated again if the set gets another one. + std::vector timeDependentTxs; + // Candidates found invalid, to remove from the mempool if the set + // is proposed. + std::vector invalidTxHashes; + std::chrono::steady_clock::time_point selectedAt; + }; struct ProposalPrevalidation { // The executed ledger (N) and its value: the results hold on top of @@ -419,16 +531,75 @@ class HerderImpl : public Herder // Decoded and selected once, by whichever task gets there first. std::once_flag selected; std::vector candidates; + // Early selection only. Whether this job selects a set, the close + // time it assumes for it (as an apply-time offset from N), how many + // valid candidates per phase the selection validates, and every + // pulled transaction that is not in N, in mempool order (written + // with `candidates`). + bool selectEarly{false}; + uint64_t assumedCloseTimeOffset{0}; + // Drawn on the main thread: randomizes the order of candidates + // with equal fee rates in the selection. + size_t selectionSeed{0}; + std::vector validTxTargets; + std::vector pulled; // Guards everything below. Tasks hand over their results in small // batches, so whoever stops the job takes what is there and never // waits for a task: nothing is published after `stop` is set. std::mutex mutex; std::vector> published; + // Results for candidates whose validity depends on the close time, + // validated for the assumed one. Only the early selection uses + // them; they never enter mValidationCache. + std::vector> publishedTimeDependent; size_t pendingTasks{0}; size_t activeTasks{0}; + // The selected set, once it exists and until it is taken. + std::shared_ptr early; // Nanoseconds from `start` until the last task finished, or -1. std::atomic finishedAfterNs{-1}; + // Set when the selection has run to its end, with or without a + // result. + std::atomic selectionDone{false}; }; + // Runs on the thread of the job's last validation task. Returns null if + // the job was stopped meanwhile. + static std::shared_ptr + selectEarlyTxSet(Application& app, ProposalPrevalidation& job, + std::shared_ptr const& state); + // Called when the previous ledger has closed and this node leads round 1 + // of `ledgerSeq`: if the running pre-validation job selected a set for + // it, and the selection holds on top of the LCL (the LCL closed with the + // executed value, the limits are the ones assumed, the time-dependent + // transactions are valid for `closeTime`), names the LCL as its parent, + // hands it to the overlay and the peers and keeps it as the prepared + // set. Returns false, and leaves the build to the usual path, otherwise. + bool prepareEarlyTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); + // A set selected early is used only if building a set of its size is + // expected to take at least this long (see prepareEarlyTxSet). + static constexpr std::chrono::milliseconds EARLY_TX_SET_MIN_BUILD_SAVING{ + 20}; + // The part of setupTriggerNextLedger that decides how the first-round + // leader of `nextIndex` gets its set ready before the trigger at + // `triggerTime`: by completing a set it selected early, or by arming + // the timer for a build a lead time ahead; neither if the trigger is + // due and there is no early set (the trigger then builds). + void scheduleTxSetPreparation(uint64_t nextIndex, + VirtualClock::time_point triggerTime, + bool triggerDue, VirtualClock::time_point now, + std::chrono::milliseconds expectedCloseTime, + ConsensusTime minCandidateCt); + // Whether the build of the next set has to start as soon as the previous + // ledger has closed: its trigger is already due, or the time to start a + // build of the usual lead time (`prepareAt`) has come. Only then does a + // set selected early replace the build; a build that still fits before + // the trigger holds what arrived since the selection. + static bool + buildMustStartNow(bool triggerDue, VirtualClock::time_point prepareAt, + VirtualClock::time_point now) + { + return triggerDue || prepareAt <= now; + } std::shared_ptr mProposalPrevalidation; // Whether the local node would lead round 1 or 2 of the ledger after // `slotIndex` if `slotIndex` closes with `value`. diff --git a/src/herder/ParallelTxSetBuilder.cpp b/src/herder/ParallelTxSetBuilder.cpp index bbb9d552e..1935c02ec 100644 --- a/src/herder/ParallelTxSetBuilder.cpp +++ b/src/herder/ParallelTxSetBuilder.cpp @@ -705,7 +705,8 @@ buildSurgePricedParallelSorobanPhase( TxFrameList const& txFrames, Config const& cfg, SorobanNetworkConfig const& sorobanCfg, std::shared_ptr laneConfig, - std::vector& hadTxNotFittingLane, uint32_t ledgerVersion) + std::vector& hadTxNotFittingLane, uint32_t ledgerVersion, + std::optional seed) { ZoneScoped; // We prefer the transaction sets that are well utilized, but we also want @@ -720,9 +721,11 @@ buildSurgePricedParallelSorobanPhase( // Sort transactions in decreasing inclusion fee order. Look up each // transaction's fee bid and operation count once, not per comparison. - TxFeeComparator txComparator( - /* isGreater */ true, - stellar::rand_uniform(0, std::numeric_limits::max())); + size_t const tieBreakSeed = + seed ? *seed + : stellar::rand_uniform( + 0, std::numeric_limits::max()); + TxFeeComparator txComparator(/* isGreater */ true, tieBreakSeed); std::vector> feeBids; feeBids.reserve(txFrames.size()); for (auto const& tx : txFrames) diff --git a/src/herder/ParallelTxSetBuilder.h b/src/herder/ParallelTxSetBuilder.h index 9ea3feabc..fa718104c 100644 --- a/src/herder/ParallelTxSetBuilder.h +++ b/src/herder/ParallelTxSetBuilder.h @@ -24,6 +24,9 @@ TxStageFrameList buildSurgePricedParallelSorobanPhase( TxFrameList const& txFrames, Config const& cfg, SorobanNetworkConfig const& sorobanCfg, std::shared_ptr laneConfig, - std::vector& hadTxNotFittingLane, uint32_t ledgerVersion); + std::vector& hadTxNotFittingLane, uint32_t ledgerVersion, + // Randomizes the order of transactions with equal fee rates. Drawn from + // the global random engine (main thread only) unless provided. + std::optional seed = std::nullopt); } // namespace stellar diff --git a/src/herder/SurgePricingUtils.cpp b/src/herder/SurgePricingUtils.cpp index edec65d71..2b8c83920 100644 --- a/src/herder/SurgePricingUtils.cpp +++ b/src/herder/SurgePricingUtils.cpp @@ -146,13 +146,17 @@ std::vector SurgePricingPriorityQueue::getMostTopTxsWithinLimits( std::vector const& txs, std::shared_ptr laneConfig, - std::vector& hadTxNotFittingLane, uint32_t ledgerVersion) + std::vector& hadTxNotFittingLane, uint32_t ledgerVersion, + std::optional seed) { ZoneScoped; - SurgePricingPriorityQueue queue( - /* isHighestPriority */ true, laneConfig, - stellar::rand_uniform(0, std::numeric_limits::max())); + size_t const tieBreakSeed = + seed ? *seed + : stellar::rand_uniform( + 0, std::numeric_limits::max()); + SurgePricingPriorityQueue queue(/* isHighestPriority */ true, laneConfig, + tieBreakSeed); bool allFit = true; auto res = queue.countTxsResources(txs, ledgerVersion); diff --git a/src/herder/SurgePricingUtils.h b/src/herder/SurgePricingUtils.h index f6e7fb3b6..95ca192df 100644 --- a/src/herder/SurgePricingUtils.h +++ b/src/herder/SurgePricingUtils.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "transactions/TransactionFrameBase.h" @@ -153,10 +154,14 @@ class SurgePricingPriorityQueue // `hadTxNotFittingLane` is an output parameter that for every lane will // identify whether there was a transaction that didn't fit into that lane's // limit. + // `seed` randomizes the order of transactions with equal fee rates. It + // is drawn from the global random engine (main thread only) unless the + // caller provides one, which a caller on another thread has to. static std::vector getMostTopTxsWithinLimits( std::vector const& txs, std::shared_ptr laneConfig, - std::vector& hadTxNotFittingLane, uint32_t ledgerVersion); + std::vector& hadTxNotFittingLane, uint32_t ledgerVersion, + std::optional seed = std::nullopt); // Returns total amount of resources in all the transactions in this queue. Resource totalResources() const; diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index de090b70c..a9fe96ec9 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -496,31 +496,27 @@ computeLaneBaseFee(TxSetPhase phase, LedgerHeader const& ledgerHeader, } std::shared_ptr -createSurgePricingLangeConfig(TxSetPhase phase, Application& app) +createSurgePricingLaneConfig(TxSetPhase phase, Config const& cfg, + TxSetBuildLimits const& limits) { ZoneScoped; - releaseAssert(threadIsMain()); - releaseAssert(!app.getLedgerManager().isApplying()); - auto const& lclHeader = - app.getLedgerManager().getLastClosedLedgerHeader().header; + auto const& lclHeader = limits.mLclHeader; std::vector hadTxNotFittingLane; std::shared_ptr surgePricingLaneConfig; if (phase == TxSetPhase::CLASSIC) { - auto maxOps = Resource( - {static_cast( - app.getLedgerManager().getLastMaxTxSetSizeOps()), - static_cast(app.getConfig().getClassicByteAllowance())}); + auto maxOps = + Resource({limits.maxTxSetSizeOps(), + static_cast(cfg.getClassicByteAllowance())}); std::optional dexOpsLimit; - if (app.getConfig().MAX_DEX_TX_OPERATIONS_IN_TX_SET) + if (cfg.MAX_DEX_TX_OPERATIONS_IN_TX_SET) { // DEX operations limit implies that DEX transactions should // compete with each other in in a separate fee lane, which // is only possible with generalized tx set. - dexOpsLimit = - Resource({*app.getConfig().MAX_DEX_TX_OPERATIONS_IN_TX_SET, - MAX_CLASSIC_BYTE_ALLOWANCE}); + dexOpsLimit = Resource({*cfg.MAX_DEX_TX_OPERATIONS_IN_TX_SET, + MAX_CLASSIC_BYTE_ALLOWANCE}); } surgePricingLaneConfig = @@ -529,9 +525,9 @@ createSurgePricingLangeConfig(TxSetPhase phase, Application& app) else { releaseAssert(phase == TxSetPhase::SOROBAN); + releaseAssert(limits.mSorobanConfig); - auto limits = app.getLedgerManager().maxLedgerResources( - /* isSoroban */ true); + auto limitsRes = limits.mSorobanConfig->maxLedgerResources(); // When building Soroban tx sets with parallel execution support, // instructions are accounted for by the build logic, not by the surge // pricing config, so we need to relax the instruction limit in surge @@ -539,17 +535,17 @@ createSurgePricingLangeConfig(TxSetPhase phase, Application& app) if (protocolVersionStartsFrom(lclHeader.ledgerVersion, PARALLEL_SOROBAN_PHASE_PROTOCOL_VERSION)) { - limits.setVal(Resource::Type::INSTRUCTIONS, - std::numeric_limits::max()); + limitsRes.setVal(Resource::Type::INSTRUCTIONS, + std::numeric_limits::max()); } - auto byteLimit = std::min( - static_cast(app.getConfig().getSorobanByteAllowance()), - limits.getVal(Resource::Type::TX_BYTE_SIZE)); - limits.setVal(Resource::Type::TX_BYTE_SIZE, byteLimit); + auto byteLimit = + std::min(static_cast(cfg.getSorobanByteAllowance()), + limitsRes.getVal(Resource::Type::TX_BYTE_SIZE)); + limitsRes.setVal(Resource::Type::TX_BYTE_SIZE, byteLimit); surgePricingLaneConfig = - std::make_shared(limits); + std::make_shared(limitsRes); } return surgePricingLaneConfig; } @@ -558,16 +554,18 @@ TxFrameList buildSurgePricedSequentialPhase( TxFrameList const& txs, std::shared_ptr surgePricingLaneConfig, - std::vector& hadTxNotFittingLane, uint32_t ledgerVersion) + std::vector& hadTxNotFittingLane, uint32_t ledgerVersion, + std::optional seed) { ZoneScoped; return SurgePricingPriorityQueue::getMostTopTxsWithinLimits( - txs, surgePricingLaneConfig, hadTxNotFittingLane, ledgerVersion); + txs, surgePricingLaneConfig, hadTxNotFittingLane, ledgerVersion, seed); } std::pair, std::shared_ptr> -applySurgePricing(TxSetPhase phase, TxFrameList const& txs, Application& app +applySurgePricing(TxSetPhase phase, TxFrameList const& txs, Config const& cfg, + TxSetBuildLimits const& limits, std::optional seed #ifdef BUILD_TESTS , bool enforceTxsApplyOrder, @@ -576,10 +574,10 @@ applySurgePricing(TxSetPhase phase, TxFrameList const& txs, Application& app ) { ZoneScoped; - auto surgePricingLaneConfig = createSurgePricingLangeConfig(phase, app); + auto surgePricingLaneConfig = + createSurgePricingLaneConfig(phase, cfg, limits); std::vector hadTxNotFittingLane; - uint32_t ledgerVersion = - app.getLedgerManager().getLastClosedLedgerHeader().header.ledgerVersion; + uint32_t ledgerVersion = limits.mLclHeader.ledgerVersion; bool isParallelSoroban = phase == TxSetPhase::SOROBAN && protocolVersionStartsFrom(ledgerVersion, @@ -623,10 +621,10 @@ applySurgePricing(TxSetPhase phase, TxFrameList const& txs, Application& app else { #endif + releaseAssert(limits.mSorobanConfig); includedTxs = buildSurgePricedParallelSorobanPhase( - txs, app.getConfig(), - app.getLedgerManager().getLastClosedSorobanNetworkConfig(), - surgePricingLaneConfig, hadTxNotFittingLane, ledgerVersion); + txs, cfg, *limits.mSorobanConfig, surgePricingLaneConfig, + hadTxNotFittingLane, ledgerVersion, seed); #ifdef BUILD_TESTS } #endif @@ -634,7 +632,8 @@ applySurgePricing(TxSetPhase phase, TxFrameList const& txs, Application& app else { includedTxs = buildSurgePricedSequentialPhase( - txs, surgePricingLaneConfig, hadTxNotFittingLane, ledgerVersion); + txs, surgePricingLaneConfig, hadTxNotFittingLane, ledgerVersion, + seed); } auto visitIncludedTxs = @@ -676,8 +675,7 @@ applySurgePricing(TxSetPhase phase, TxFrameList const& txs, Application& app }; std::vector lowestLaneFee; - auto const& lclHeader = - app.getLedgerManager().getLastClosedLedgerHeader().header; + auto const& lclHeader = limits.mLclHeader; size_t laneCount = surgePricingLaneConfig->getLaneLimits().size(); lowestLaneFee.resize(laneCount, std::numeric_limits::max()); @@ -882,16 +880,211 @@ makeTxSetFromTransactions( , TxSetReadyCallback const& onTxSetReady, std::vector const& validTxTargets, - TxValidationCache* validationCache) + TxValidationCache* validationCache, + std::shared_ptr* deferredSelfCheck) { releaseAssert(threadIsMain()); releaseAssert(!app.getLedgerManager().isApplying()); releaseAssert(txPhases.size() == invalidTxs.size()); + + // Everything is read from the LCL: validity through the context, the + // limits of the set from its header and Soroban configuration. + auto ctx = TxSetValidationContext::forLCL(app); + ctx.mValidationCache = validationCache; + auto const validatedPhases = TxSetBuilder::selectPhases( + txPhases, app, TxSetBuildLimits::forLCL(app), ctx, closeTimeOffset, + invalidTxs, validTxTargets, "build", std::nullopt +#ifdef BUILD_TESTS + , + enforceTxsApplyOrder, parallelSorobanOrder, + disableTxValidationForLegacyScenario +#endif + ); + + auto const& lclHeader = app.getLedgerManager().getLastClosedLedgerHeader(); + bool const selfCheckIsDeferred = deferredSelfCheck +#ifdef BUILD_TESTS + && !enforceTxsApplyOrder +#endif + ; + if (selfCheckIsDeferred) + { + // The check of the written set comes after the set is in use. What + // the selection itself can get wrong (two transactions of one + // account, fees, limits) is checked before anything leaves this + // function, as it always was. + auto const result = TxSetBuilder::checkSelection( + validatedPhases, app, ctx, + protocolVersionStartsFrom(lclHeader.header.ledgerVersion, + SOROBAN_PROTOCOL_VERSION), + closeTimeOffset); + if (result != TxSetValidationResult::VALID) + { + throw std::runtime_error(fmt::format( + FMT_STRING( + "Created invalid tx set frame, validation result: {:s}"), + toString(result))); + } + } + auto xdrTimer = + app.getMetrics().NewTimer({"herder", "txset", "build-xdr"}).TimeScope(); + // Preliminary applicable frame - we don't know the contents hash yet, but + // we also don't return this. + std::unique_ptr preliminaryApplicableTxSet( + new ApplicableTxSetFrame(app, lclHeader, validatedPhases, + std::nullopt)); + + // Do the roundtrip through XDR to ensure we never build an incorrect tx set + // for nomination. + auto outputTxSet = preliminaryApplicableTxSet->toWireTxSetFrame(); +#ifdef BUILD_TESTS + if (enforceTxsApplyOrder) + { + // Fill in the contents hash if we're skipping the normal roundtrip + // and validation flow. + preliminaryApplicableTxSet->mContentsHash = + outputTxSet->getContentsHash(); + return std::make_pair(outputTxSet, + std::move(preliminaryApplicableTxSet)); + } +#endif + + xdrTimer.Stop(); + // The final XDR is available now. Start proposal encoding while the + // roundtrip and final validity checks below run; only the local builder + // supplies this callback. The callback must not nominate the set. + if (onTxSetReady) + { + onTxSetReady(outputTxSet); + } + + if (deferredSelfCheck) + { + // What is cheap to check of the written set is checked here: the + // shape of its XDR (no empty component, stage or cluster, ...). + if (outputTxSet->isGeneralizedTxSet() && + !validateTxSetXDRStructure(outputTxSet->getGeneralizedXDR(), + lclHeader.header.ledgerVersion)) + { + throw std::runtime_error( + "Couldn't prepare created tx set frame for apply"); + } + // The caller runs the check (see TxSetSelfCheck) and goes on with + // the set as selected, which is what the check compares the XDR + // with. + *deferredSelfCheck = TxSetBuilder::makeSelfCheck( + app, outputTxSet, lclHeader, validatedPhases, closeTimeOffset); + preliminaryApplicableTxSet->mContentsHash = + outputTxSet->getContentsHash(); + return std::make_pair(outputTxSet, + std::move(preliminaryApplicableTxSet)); + } + + std::vector phaseSizes; + for (auto const& phase : validatedPhases) + { + phaseSizes.push_back(phase.sizeTx()); + } + ApplicableTxSetFrameConstPtr outputApplicableTxSet; + // We already trimmed invalid transactions in an earlier call to + // `trimInvalid`, so the check skips transaction validation. + auto const failure = TxSetBuilder::checkBuiltTxSet( + app, *outputTxSet, TxSetValidationContext::forLCL(app), phaseSizes, + closeTimeOffset.seconds(), &outputApplicableTxSet); + if (failure) + { + throw std::runtime_error(*failure); + } + + return std::make_pair(outputTxSet, std::move(outputApplicableTxSet)); +} + +uint32_t +TxSetBuildLimits::maxTxSetSizeOps() const +{ + // As LedgerManager::getLastMaxTxSetSizeOps. + auto const n = mLclHeader.maxTxSetSize; + return protocolVersionStartsFrom(mLclHeader.ledgerVersion, + ProtocolVersion::V_11) + ? n + : (n * MAX_OPS_PER_TX); +} + +bool +TxSetBuildLimits::sameAs(TxSetBuildLimits const& other) const +{ + auto const& a = mLclHeader; + auto const& b = other.mLclHeader; + if (a.ledgerVersion != b.ledgerVersion || a.baseFee != b.baseFee || + a.baseReserve != b.baseReserve || a.maxTxSetSize != b.maxTxSetSize) + { + return false; + } + if ((mSorobanConfig == nullptr) != (other.mSorobanConfig == nullptr)) + { + return false; + } + if (!mSorobanConfig) + { + return true; + } + auto const& s = *mSorobanConfig; + auto const& t = *other.mSorobanConfig; + return s.maxLedgerResources() == t.maxLedgerResources() && + s.ledgerMaxInstructions() == t.ledgerMaxInstructions() && + s.ledgerMaxDependentTxClusters() == + t.ledgerMaxDependentTxClusters() && + s.ledgerMaxTxCount() == t.ledgerMaxTxCount(); +} + +TxSetBuildLimits +TxSetBuildLimits::forLCL(Application& app) +{ + releaseAssert(threadIsMain()); + auto& lm = app.getLedgerManager(); + TxSetBuildLimits limits; + limits.mLclHeader = lm.getLastClosedLedgerHeader().header; + if (protocolVersionStartsFrom(limits.mLclHeader.ledgerVersion, + SOROBAN_PROTOCOL_VERSION)) + { + limits.mSorobanConfig = &lm.getLastClosedSorobanNetworkConfig(); + } + return limits; +} + +size_t +UnstampedTxSet::sizeTxTotal() const +{ + size_t total = 0; + for (auto const& phase : mPhases) + { + total += phase.sizeTx(); + } + return total; +} + +std::vector +TxSetBuilder::selectPhases( + PerPhaseTransactionList const& txPhases, Application& app, + TxSetBuildLimits const& limits, TxSetValidationContext const& ctx, + ApplyTimeOffset closeTimeOffset, PerPhaseTransactionList& invalidTxs, + std::vector const& validTxTargets, std::string const& timerPrefix, + std::optional tieBreakSeed +#ifdef BUILD_TESTS + , + bool enforceTxsApplyOrder, + txtest::ParallelSorobanOrder const& parallelSorobanOrder, + bool disableTxValidationForLegacyScenario +#endif +) +{ + releaseAssert(txPhases.size() == invalidTxs.size()); releaseAssert(validTxTargets.empty() || validTxTargets.size() == txPhases.size()); releaseAssert(txPhases.size() <= static_cast(TxSetPhase::PHASE_COUNT)); + auto const& cfg = app.getConfig(); std::vector validatedPhases; UnorderedMap accountFeeMap; for (size_t i = 0; i < txPhases.size(); ++i) @@ -911,38 +1104,38 @@ makeTxSetFromTransactions( #ifdef BUILD_TESTS if (disableTxValidationForLegacyScenario) { - REQUIRE(protocolVersionIsBefore(app.getLedgerManager() - .getLastClosedLedgerHeader() - .header.ledgerVersion, + REQUIRE(protocolVersionIsBefore(limits.mLclHeader.ledgerVersion, ProtocolVersion::V_20)); validatedTxs = phaseTxs; } else { #endif - auto trimTimer = app.getMetrics() - .NewTimer({"herder", "txset", "build-trim"}) - .TimeScope(); + auto trimTimer = + app.getMetrics() + .NewTimer({"herder", "txset", timerPrefix + "-trim"}) + .TimeScope(); if (validTxTargets.empty()) { - validatedTxs = TxSetUtils::trimInvalid( - phaseTxs, app, accountFeeMap, closeTimeOffset.seconds(), - closeTimeOffset.seconds(), invalid, validationCache); + validatedTxs = + TxSetUtils::trimInvalid(phaseTxs, app, ctx, accountFeeMap, + closeTimeOffset.seconds(), + closeTimeOffset.seconds(), invalid); } else { // Transactions of limited lanes (DEX) can't fill the phase, // so only generic lane ones count toward the target. - auto laneConfig = createSurgePricingLangeConfig( - static_cast(i), app); + auto laneConfig = createSurgePricingLaneConfig( + static_cast(i), cfg, limits); validatedTxs = TxSetUtils::trimInvalidPrefix( phaseTxs, validTxTargets[i], [&laneConfig](TransactionFrameBase const& tx) { return laneConfig->getLane(tx) == SurgePricingPriorityQueue::GENERIC_LANE; }, - app, accountFeeMap, closeTimeOffset.seconds(), - closeTimeOffset.seconds(), invalid, validationCache); + app, ctx, accountFeeMap, closeTimeOffset.seconds(), + closeTimeOffset.seconds(), invalid); } #ifdef BUILD_TESTS // In tests we shouldn't be really trying to close ledgers with @@ -963,16 +1156,18 @@ makeTxSetFromTransactions( } #endif auto phaseType = static_cast(i); - auto selectTimer = app.getMetrics() - .NewTimer({"herder", "txset", "build-select"}) - .TimeScope(); - auto [includedTxs, inclusionFeeMapBinding] = - applySurgePricing(phaseType, validatedTxs, app + auto selectTimer = + app.getMetrics() + .NewTimer({"herder", "txset", timerPrefix + "-select"}) + .TimeScope(); + auto [includedTxs, inclusionFeeMapBinding] = applySurgePricing( + phaseType, validatedTxs, cfg, limits, + tieBreakSeed ? std::make_optional(*tieBreakSeed + i) : std::nullopt #ifdef BUILD_TESTS - , - enforceTxsApplyOrder, parallelSorobanOrder + , + enforceTxsApplyOrder, parallelSorobanOrder #endif - ); + ); auto inclusionFeeMap = inclusionFeeMapBinding; std::visit( [&validatedPhases, phaseType, inclusionFeeMap](auto&& txs) { @@ -998,88 +1193,172 @@ makeTxSetFromTransactions( }, includedTxs); } + return validatedPhases; +} - auto const& lclHeader = app.getLedgerManager().getLastClosedLedgerHeader(); - auto xdrTimer = - app.getMetrics().NewTimer({"herder", "txset", "build-xdr"}).TimeScope(); - // Preliminary applicable frame - we don't know the contents hash yet, but - // we also don't return this. - std::unique_ptr preliminaryApplicableTxSet( - new ApplicableTxSetFrame(app, lclHeader, validatedPhases, - std::nullopt)); +TxSetValidationResult +TxSetBuilder::checkSelection(std::vector const& phases, + Application& app, + TxSetValidationContext const& ctx, + bool isGeneralized, + ApplyTimeOffset closeTimeOffset) +{ + ZoneScoped; + auto timer = app.getMetrics() + .NewTimer({"herder", "txset", "build-selection-check"}) + .TimeScope(); + // The transactions were validated by the selection. + return ApplicableTxSetFrame::checkPhasesValid( + app, ctx, phases, isGeneralized, closeTimeOffset.seconds(), + closeTimeOffset.seconds(), /* txsAreValidated */ true); +} - // Do the roundtrip through XDR to ensure we never build an incorrect tx set - // for nomination. - auto outputTxSet = preliminaryApplicableTxSet->toWireTxSetFrame(); -#ifdef BUILD_TESTS - if (enforceTxsApplyOrder) +UnstampedTxSet +TxSetBuilder::makeUnstamped(std::vector&& phases, + uint32_t ledgerVersion) +{ + ZoneScoped; + releaseAssert(phases.size() == + static_cast(TxSetPhase::PHASE_COUNT)); + UnstampedTxSet res; + res.mPhases = std::move(phases); + // The same XDR as ApplicableTxSetFrame::toXDR writes, with the parent + // left empty. + transactionsToGeneralizedTransactionSetXDR(res.mPhases, Hash{}, res.mXDR); + // As a build does before its set leaves it: the shape of the XDR. + if (!validateTxSetXDRStructure(res.mXDR, ledgerVersion)) { - // Fill in the contents hash if we're skipping the normal roundtrip - // and validation flow. - preliminaryApplicableTxSet->mContentsHash = - outputTxSet->getContentsHash(); - return std::make_pair(outputTxSet, - std::move(preliminaryApplicableTxSet)); + throw std::runtime_error( + "Couldn't prepare created tx set frame for apply"); } -#endif + return res; +} - xdrTimer.Stop(); - // The final XDR is available now. Start proposal encoding while the - // roundtrip and final validity checks below run; only the local builder - // supplies this callback. The callback must not nominate the set. - if (onTxSetReady) +std::pair +TxSetBuilder::stamp(UnstampedTxSet&& txSet, Application& app, + LedgerHeaderHistoryEntry const& lcl, + ApplyTimeOffset closeTimeOffset, + std::shared_ptr& selfCheck) +{ + ZoneScoped; + releaseAssert(threadIsMain()); + releaseAssert(!app.getLedgerManager().isApplying()); + releaseAssert(lcl.hash == + app.getLedgerManager().getLastClosedLedgerHeader().hash); + releaseAssert(protocolVersionStartsFrom(lcl.header.ledgerVersion, + SOROBAN_PROTOCOL_VERSION)); + releaseAssert(txSet.mXDR.v() == 1); + txSet.mXDR.v1TxSet().previousLedgerHash = lcl.hash; + // Sizes and hashes the XDR. + auto outputTxSet = TxSetXDRFrame::makeFromWire(std::move(txSet.mXDR)); + selfCheck = + makeSelfCheck(app, outputTxSet, lcl, txSet.mPhases, closeTimeOffset); + std::unique_ptr applicable(new ApplicableTxSetFrame( + app, lcl, txSet.mPhases, outputTxSet->getContentsHash())); + return std::make_pair(outputTxSet, std::move(applicable)); +} + +std::shared_ptr +TxSetBuilder::makeSelfCheck(Application& app, + TxSetXDRFrameConstPtr const& txSet, + LedgerHeaderHistoryEntry const& lcl, + std::vector const& phases, + ApplyTimeOffset closeTimeOffset) +{ + releaseAssert(threadIsMain()); + std::shared_ptr check(new TxSetSelfCheck()); + check->mTxSet = txSet; + check->mLcl = lcl; + if (protocolVersionStartsFrom(lcl.header.ledgerVersion, + SOROBAN_PROTOCOL_VERSION)) { - onTxSetReady(outputTxSet); + check->mSorobanConfig = std::make_shared( + app.getLedgerManager().getLastClosedSorobanNetworkConfig()); + } + for (auto const& phase : phases) + { + check->mPhaseSizes.push_back(phase.sizeTx()); } + check->mCloseTimeOffset = closeTimeOffset.seconds(); + return check; +} +std::optional +TxSetBuilder::checkBuiltTxSet(Application& app, TxSetXDRFrame const& txSet, + TxSetValidationContext const& ctx, + std::vector const& phaseSizes, + uint64_t closeTimeOffset, + ApplicableTxSetFrameConstPtr* roundTripped) +{ + ZoneScoped; auto roundtripTimer = app.getMetrics() .NewTimer({"herder", "txset", "build-roundtrip"}) .TimeScope(); - ApplicableTxSetFrameConstPtr outputApplicableTxSet = - outputTxSet->prepareForApply(app, lclHeader.header); + ApplicableTxSetFrameConstPtr applicable = + txSet.prepareForApply(app, ctx.mLcl.header); roundtripTimer.Stop(); - if (!outputApplicableTxSet) + if (!applicable) { - throw std::runtime_error( - "Couldn't prepare created tx set frame for apply"); + return "Couldn't prepare created tx set frame for apply"; } // Make sure no transactions were lost during the roundtrip and the output // tx set is valid. - bool valid = preliminaryApplicableTxSet->numPhases() == - outputApplicableTxSet->numPhases(); + bool valid = phaseSizes.size() == applicable->numPhases(); if (valid) { - for (size_t i = 0; i < preliminaryApplicableTxSet->numPhases(); ++i) + for (size_t i = 0; i < phaseSizes.size(); ++i) { - valid = valid && preliminaryApplicableTxSet->sizeTx( - static_cast(i)) == - outputApplicableTxSet->sizeTx( - static_cast(i)); + valid = valid && phaseSizes[i] == + applicable->sizeTx(static_cast(i)); } } if (!valid) { - throw std::runtime_error("Created invalid tx set frame - shape is " - "mismatched after roundtrip."); + return "Created invalid tx set frame - shape is mismatched after " + "roundtrip."; } - // We already trimmed invalid transactions in an earlier call to - // `trimInvalid`, so skip transaction validation here auto finalTimer = app.getMetrics() .NewTimer({"herder", "txset", "build-final-check"}) .TimeScope(); - auto validationResult = outputApplicableTxSet->checkValidInternalWithResult( - app, closeTimeOffset.seconds(), closeTimeOffset.seconds(), true); + auto validationResult = applicable->checkValidInternalWithResult( + app, ctx, closeTimeOffset, closeTimeOffset, true); finalTimer.Stop(); if (validationResult != TxSetValidationResult::VALID) { - throw std::runtime_error(fmt::format( + return fmt::format( FMT_STRING("Created invalid tx set frame, validation result: {:s}"), - toString(validationResult))); + toString(validationResult)); } + if (roundTripped) + { + *roundTripped = std::move(applicable); + } + return std::nullopt; +} - return std::make_pair(outputTxSet, std::move(outputApplicableTxSet)); +std::optional +TxSetSelfCheck::run(Application& app) const +{ + // The check does not validate transactions, so it never asks for a view + // of the ledger. + TxSetValidationContext const ctx{ + mLcl, mSorobanConfig.get(), + []() -> std::unique_ptr { + throw std::runtime_error( + "the tx set self-check does not read the ledger"); + }, + 1}; + try + { + return TxSetBuilder::checkBuiltTxSet(app, *mTxSet, ctx, mPhaseSizes, + mCloseTimeOffset, nullptr); + } + catch (std::exception const& e) + { + return fmt::format(FMT_STRING("tx set self-check threw: {}"), e.what()); + } } TxSetXDRFrameConstPtr @@ -2458,7 +2737,20 @@ ApplicableTxSetFrame::checkValidInternalWithResult( releaseAssert(mPhases.size() == 1); } - bool enforceUniqueSourceAccount = needGeneralizedTxSet; + return checkPhasesValid(app, ctx, mPhases, isGeneralizedTxSet(), + lowerBoundCloseTimeOffset, + upperBoundCloseTimeOffset, txsAreValidated); +} + +TxSetValidationResult +ApplicableTxSetFrame::checkPhasesValid( + Application& app, TxSetValidationContext const& ctx, + std::vector const& phases, bool isGeneralized, + uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, + bool txsAreValidated) +{ + auto const& lcl = ctx.mLcl; + bool enforceUniqueSourceAccount = isGeneralized; #ifdef BUILD_TESTS if (app.getRunInOverlayOnlyMode()) { @@ -2470,7 +2762,7 @@ ApplicableTxSetFrame::checkValidInternalWithResult( // Ensure the tx set does not contain multiple txs per source // account std::unordered_set seenAccounts; - for (auto const& phase : mPhases) + for (auto const& phase : phases) { for (auto const& tx : phase) { @@ -2489,7 +2781,7 @@ ApplicableTxSetFrame::checkValidInternalWithResult( bool useCrossPhaseFeeMap = protocolVersionStartsFrom( lcl.header.ledgerVersion, ProtocolVersion::V_26); UnorderedMap accountFeeMap; - for (auto const& phase : mPhases) + for (auto const& phase : phases) { if (!useCrossPhaseFeeMap) { diff --git a/src/herder/TxSetFrame.h b/src/herder/TxSetFrame.h index ef074dced..cda3a084b 100644 --- a/src/herder/TxSetFrame.h +++ b/src/herder/TxSetFrame.h @@ -27,6 +27,8 @@ class AbstractLedgerView; class SorobanNetworkConfig; class TxSetXDRFrame; class ApplicableTxSetFrame; +class TxSetBuilder; +class TxSetSelfCheck; using TxSetXDRFrameConstPtr = std::shared_ptr; using TxSetReadyCallback = std::function; using ApplicableTxSetFrameConstPtr = @@ -152,6 +154,30 @@ struct TxSetValidationContext static TxSetValidationContext forLCL(Application& app); }; +// The limits a transaction set is selected within: those of the ledger it +// goes on top of. Selection (surge pricing, the Soroban stages) only reads +// the ledger through this, so it can run for a ledger that is not the main +// thread's LCL, and on another thread. +struct TxSetBuildLimits +{ + // Header of the ledger the set applies on top of. Selection reads its + // protocol version, base fee and maximum tx set size; its hashes are not + // used. + LedgerHeader mLclHeader; + // Soroban network configuration of that ledger; null before Soroban. + // Whoever makes the limits keeps it alive. + SorobanNetworkConfig const* mSorobanConfig{nullptr}; + + // Capacity of the classic phase, in operations. + uint32_t maxTxSetSizeOps() const; + // Whether a set selected within `other` is selected within these limits + // too: everything selection reads is equal. + bool sameAs(TxSetBuildLimits const& other) const; + + // The limits of the LCL as seen by the main thread. Main thread only. + static TxSetBuildLimits forLCL(Application& app); +}; + using TxFrameList = std::vector; using PerPhaseTransactionList = std::vector; @@ -215,7 +241,16 @@ makeTxSetFromTransactions( std::vector const& validTxTargets = {}, // Results of earlier validations against the LCL (see // `TxValidationCache`); extended with this build's. - TxValidationCache* validationCache = nullptr); + TxValidationCache* validationCache = nullptr, + // The builder checks every set it makes: it reads the XDR back and + // validates the structure of the result (`TxSetSelfCheck`). Without + // `deferredSelfCheck` it does so before returning, and throws if the + // check fails. With it, only the structure of the selection is validated + // before returning (`TxSetBuilder::checkSelection`; a failure throws); + // the check of the written set is not run: it is returned here for the + // caller to run (on any thread), so that it does not hold up the use of + // the set. + std::shared_ptr* deferredSelfCheck = nullptr); #ifdef BUILD_TESTS std::pair @@ -480,7 +515,9 @@ class TxSetPhaseFrame , TxSetReadyCallback const& onTxSetReady, std::vector const& validTxTargets, - TxValidationCache* validationCache); + TxValidationCache* validationCache, + std::shared_ptr* deferredSelfCheck); + friend class TxSetBuilder; #ifdef BUILD_TESTS friend std::pair makeTxSetFromTransactions( @@ -674,7 +711,9 @@ class ApplicableTxSetFrame , TxSetReadyCallback const& onTxSetReady, std::vector const& validTxTargets, - TxValidationCache* validationCache); + TxValidationCache* validationCache, + std::shared_ptr* deferredSelfCheck); + friend class TxSetBuilder; #ifdef BUILD_TESTS friend std::pair makeTxSetFromTransactions( @@ -695,6 +734,17 @@ class ApplicableTxSetFrame ApplicableTxSetFrame(ApplicableTxSetFrame const&) = default; ApplicableTxSetFrame(ApplicableTxSetFrame&&) = default; + // Everything `checkValidInternalWithResult` checks about the phases of a + // set, i.e. all of it but the set's parent and kind: one transaction per + // source account across the phases, and each phase's fees, limits and + // (unless `txsAreValidated`) transactions. Used on its own for a + // selection that has no parent yet. + static TxSetValidationResult + checkPhasesValid(Application& app, TxSetValidationContext const& ctx, + std::vector const& phases, + bool isGeneralized, uint64_t lowerBoundCloseTimeOffset, + uint64_t upperBoundCloseTimeOffset, bool txsAreValidated); + void toXDR(TransactionSet& set) const; void toXDR(GeneralizedTransactionSet& generalizedTxSet) const; @@ -716,4 +766,152 @@ class ApplicableTxSetFrame std::optional mContentsHash; }; +// The check a builder makes of a set it just built, before the set is relied +// on: the XDR is read back into an applicable set, which must have the shape +// of the selection it was written from and pass the structural validation of +// a set on top of the ledger it was built for (limits, fees, order; the +// transactions themselves were validated by the selection). +// +// It holds copies of everything it reads, so it can run on any thread while +// the main thread goes on with the set. +class TxSetSelfCheck +{ + public: + // Runs the check. Returns why the set must not be used, or nullopt if it + // passed. `app` is only read for the network ID and the configuration. + std::optional run(Application& app) const; + + TxSetXDRFrameConstPtr const& + getTxSet() const + { + return mTxSet; + } + +#ifdef BUILD_TESTS + // The same check of another set: for tests that make a builder produce + // a broken one. + std::shared_ptr + withTxSetForTesting(TxSetXDRFrameConstPtr const& txSet) const + { + std::shared_ptr res(new TxSetSelfCheck(*this)); + res->mTxSet = txSet; + return res; + } +#endif + + private: + friend class TxSetBuilder; + TxSetSelfCheck() = default; + + TxSetXDRFrameConstPtr mTxSet; + // The ledger the set was built on top of. + LedgerHeaderHistoryEntry mLcl; + std::shared_ptr mSorobanConfig; + // Number of transactions per phase of the selection. + std::vector mPhaseSizes; + uint64_t mCloseTimeOffset{0}; +}; + +// A generalized transaction set that has been selected but does not name the +// ledger it goes on top of yet: its phases and its XDR with an empty +// `previousLedgerHash`. A leader selects the set for ledger N+1 while ledger N +// is still going through consensus, when N's header hash does not exist; the +// set is completed by `TxSetBuilder::stamp` once N has closed. +struct UnstampedTxSet +{ + std::vector mPhases; + GeneralizedTransactionSet mXDR; + + size_t sizeTxTotal() const; +}; + +// The steps `makeTxSetFromTransactions` is made of, for callers that run them +// apart from each other. +class TxSetBuilder +{ + public: + // Trims the invalid transactions of every phase and applies surge + // pricing, as `makeTxSetFromTransactions` does, but against an arbitrary + // ledger: validity is read through `ctx`, limits from `limits`. Nothing + // of the main thread's or of ledger apply's state is read, so this may + // run on any thread (`ctx` then has to ask for a single validation task). + // `timerPrefix` names the stage timers + // (`herder.txset.-trim` and `-select`). + static std::vector selectPhases( + PerPhaseTransactionList const& txPhases, Application& app, + TxSetBuildLimits const& limits, TxSetValidationContext const& ctx, + ApplyTimeOffset closeTimeOffset, PerPhaseTransactionList& invalidTxs, + std::vector const& validTxTargets, + std::string const& timerPrefix, + // Randomizes the order of transactions with equal fee + // rates. Drawn from the global random engine (main thread + // only) unless provided. + std::optional tieBreakSeed +#ifdef BUILD_TESTS + , + bool enforceTxsApplyOrder = false, + txtest::ParallelSorobanOrder const& parallelSorobanOrder = {}, + bool disableTxValidationForLegacyScenario = false +#endif + ); + + // The structural validation of a selection (ApplicableTxSetFrame's, less + // the parent hash): what a set made of `phases` would be rejected for, + // whatever its parent and its bytes. Cheap next to the check of the + // written set (TxSetSelfCheck): it reads the frames the selection + // already holds. Any thread. + static TxSetValidationResult + checkSelection(std::vector const& phases, Application& app, + TxSetValidationContext const& ctx, bool isGeneralized, + ApplyTimeOffset closeTimeOffset); + + // Writes the XDR of a selection, without a parent, and checks its shape + // for a ledger of protocol `ledgerVersion` (throws if it is malformed). + // Generalized sets only. Any thread. + static UnstampedTxSet makeUnstamped(std::vector&& phases, + uint32_t ledgerVersion); + + // Completes a set selected ahead of time for the ledger after `lcl`, + // which must be the main thread's LCL: names `lcl` as the parent and + // computes the set's hash. The self-check of the result is returned in + // `selfCheck` and has not been run. Main thread only. + static std::pair + stamp(UnstampedTxSet&& txSet, Application& app, + LedgerHeaderHistoryEntry const& lcl, ApplyTimeOffset closeTimeOffset, + std::shared_ptr& selfCheck); + + private: + friend std::pair + makeTxSetFromTransactions( + PerPhaseTransactionList const& txPhases, Application& app, + ApplyTimeOffset closeTimeOffset, + PerPhaseTransactionList& invalidTxsPerPhase +#ifdef BUILD_TESTS + , + bool enforceTxsApplyOrder, + txtest::ParallelSorobanOrder const& parallelSorobanOrder, + bool disableTxValidationForLegacyScenario +#endif + , + TxSetReadyCallback const& onTxSetReady, + std::vector const& validTxTargets, + TxValidationCache* validationCache, + std::shared_ptr* deferredSelfCheck); + friend class TxSetSelfCheck; + + static std::shared_ptr + makeSelfCheck(Application& app, TxSetXDRFrameConstPtr const& txSet, + LedgerHeaderHistoryEntry const& lcl, + std::vector const& phases, + ApplyTimeOffset closeTimeOffset); + // The check itself. `roundTripped`, if given, receives the applicable + // set read back from the XDR. + static std::optional + checkBuiltTxSet(Application& app, TxSetXDRFrame const& txSet, + TxSetValidationContext const& ctx, + std::vector const& phaseSizes, + uint64_t closeTimeOffset, + ApplicableTxSetFrameConstPtr* roundTripped); +}; + } // namespace stellar diff --git a/src/herder/TxSetUtils.cpp b/src/herder/TxSetUtils.cpp index 00483cd04..398fea78e 100644 --- a/src/herder/TxSetUtils.cpp +++ b/src/herder/TxSetUtils.cpp @@ -383,6 +383,18 @@ TxSetUtils::trimInvalid(TxFrameList const& txs, Application& app, releaseAssert(threadIsMain()); auto ctx = TxSetValidationContext::forLCL(app); ctx.mValidationCache = validationCache; + return trimInvalid(txs, app, ctx, accountFeeMap, lowerBoundCloseTimeOffset, + upperBoundCloseTimeOffset, invalidTxs); +} + +TxFrameList +TxSetUtils::trimInvalid(TxFrameList const& txs, Application& app, + TxSetValidationContext const& ctx, + UnorderedMap& accountFeeMap, + uint64_t lowerBoundCloseTimeOffset, + uint64_t upperBoundCloseTimeOffset, + TxFrameList& invalidTxs) +{ invalidTxs = getInvalidTxListWithErrors(txs, app, ctx, accountFeeMap, lowerBoundCloseTimeOffset, upperBoundCloseTimeOffset) @@ -397,6 +409,23 @@ TxSetUtils::trimInvalidPrefix( Application& app, UnorderedMap& accountFeeMap, uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, TxFrameList& invalidTxs, TxValidationCache* validationCache) +{ + releaseAssert(threadIsMain()); + auto ctx = TxSetValidationContext::forLCL(app); + ctx.mValidationCache = validationCache; + return trimInvalidPrefix(txs, targetValidTxs, countsTowardTarget, app, ctx, + accountFeeMap, lowerBoundCloseTimeOffset, + upperBoundCloseTimeOffset, invalidTxs); +} + +TxFrameList +TxSetUtils::trimInvalidPrefix( + TxFrameList const& txs, size_t targetValidTxs, + std::function const& countsTowardTarget, + Application& app, TxSetValidationContext const& ctx, + UnorderedMap& accountFeeMap, + uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, + TxFrameList& invalidTxs) { ZoneScoped; TxFrameList validTxs; @@ -420,8 +449,8 @@ TxSetUtils::trimInvalidPrefix( validated += count; TxFrameList batchInvalid; for (auto const& tx : trimInvalid( - batch, app, accountFeeMap, lowerBoundCloseTimeOffset, - upperBoundCloseTimeOffset, batchInvalid, validationCache)) + batch, app, ctx, accountFeeMap, lowerBoundCloseTimeOffset, + upperBoundCloseTimeOffset, batchInvalid)) { if (!countsTowardTarget || countsTowardTarget(*tx)) { diff --git a/src/herder/TxSetUtils.h b/src/herder/TxSetUtils.h index 09ef4dc7c..9606d5a0a 100644 --- a/src/herder/TxSetUtils.h +++ b/src/herder/TxSetUtils.h @@ -73,6 +73,14 @@ class TxSetUtils uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, TxFrameList& invalidTxs, TxValidationCache* validationCache = nullptr); + // Same, against the ledger state described by `ctx` (which also carries + // the validation cache, if any) instead of the main thread's LCL. + static TxFrameList + trimInvalid(TxFrameList const& txs, Application& app, + TxSetValidationContext const& ctx, + UnorderedMap& accountFeeMap, + uint64_t lowerBoundCloseTimeOffset, + uint64_t upperBoundCloseTimeOffset, TxFrameList& invalidTxs); // Like `trimInvalid`, for candidates in priority order when only the // first `targetValidTxs` valid ones that `countsTowardTarget` accepts are @@ -88,5 +96,15 @@ class TxSetUtils Application& app, UnorderedMap& accountFeeMap, uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, TxFrameList& invalidTxs, TxValidationCache* validationCache = nullptr); + // Same, against the ledger state described by `ctx`. + static TxFrameList + trimInvalidPrefix(TxFrameList const& txs, size_t targetValidTxs, + std::function const& + countsTowardTarget, + Application& app, TxSetValidationContext const& ctx, + UnorderedMap& accountFeeMap, + uint64_t lowerBoundCloseTimeOffset, + uint64_t upperBoundCloseTimeOffset, + TxFrameList& invalidTxs); }; // class TxSetUtils } // namespace stellar diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 012af8cb1..bd1f7a9d3 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -73,6 +73,15 @@ using namespace stellar::txbridge; using namespace stellar::txtest; using namespace historytestutils; +namespace stellar +{ +// LeaderBuildTestAccess::selectLocalFirstLeader (defined further down), for +// the fixtures above it: replaces the second validator of a two-validator +// quorum set by one that sorts behind the local node in the next slot's +// leader order. +void makeLocalNodeFirstLeader(HerderImpl& herder); +} + static TransactionTestFramePtr makeMultiPayment(stellar::TestAccount& destAccount, stellar::TestAccount& src, int nbOps, int64 paymentBase, uint32 extraFee, uint32 feeMult) @@ -2960,6 +2969,10 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps) SECTION("validateValue txSet cached") { auto& herder = static_cast(app->getHerder()); + // Which of this fixture's two validators leads the next slot depends + // on the local node's key, i.e. on the test instance number; the + // expectations below are for the node that builds and proposes. + makeLocalNodeFirstLeader(herder); auto seq = herder.trackingConsensusLedgerIndex() + 1; auto& cache = herder.getHerderSCPDriver().getTxSetValidityCache(); @@ -5544,6 +5557,12 @@ class LeaderBuildTestAccess FAIL("Could not select a first-round local leader"); } }; + +void +makeLocalNodeFirstLeader(HerderImpl& herder) +{ + LeaderBuildTestAccess::selectLocalFirstLeader(herder); +} } TEST_CASE("trigger fallback credits elapsed time up to the first ballot", @@ -6008,10 +6027,17 @@ TEST_CASE("prepare proposal before trigger", "[herder][early-proposal]") tx->addSignature(alice); auto extra = makeTx(bob, alice); mempool = {tx->getEnvelope(), extra->getEnvelope()}; - size_t pulls = 0; - herder.mGetTopTransactionsForTesting = [&](size_t) { - ++pulls; - return mempool; + herder.mGetTopTransactionsForTesting = [&](size_t) { return mempool; }; + // Builds of a tx set since here. (Not pulls of the mempool: the leader + // starts executing a prepared set at once, and that execution pulls + // candidates for the ledger after it.) + auto const buildsBefore = + app->getMetrics().NewTimer({"herder", "txset", "build"}).count(); + auto builds = [&]() { + return app->getMetrics() + .NewTimer({"herder", "txset", "build"}) + .count() - + buildsBefore; }; // Exercise the real post-close hook, including its heartbeat reset after // advancing the fixture clock past the upgrade helper's close time. @@ -6023,7 +6049,7 @@ TEST_CASE("prepare proposal before trigger", "[herder][early-proposal]") [&]() { return LeaderBuildTestAccess::prepared(herder) != nullptr; }, std::chrono::seconds(1)); REQUIRE(clock.now() < trigger); - REQUIRE(pulls == 1); + REQUIRE(builds() == 1); auto prepared = LeaderBuildTestAccess::prepared(herder); auto const preparedCloseTime = LeaderBuildTestAccess::closeTime(herder); REQUIRE(prepared); @@ -6042,7 +6068,7 @@ TEST_CASE("prepare proposal before trigger", "[herder][early-proposal]") testutil::crankUntil( app, [&]() { return herder.getSCP().getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); - REQUIRE(pulls == 1); + REQUIRE(builds() == 1); REQUIRE(!LeaderBuildTestAccess::prepared(herder)); REQUIRE(std::get( herder.getTxSet(prepared->getContentsHash())) == prepared); @@ -6071,7 +6097,7 @@ TEST_CASE("prepare proposal before trigger", "[herder][early-proposal]") xdr::xdr_from_opaque(ballot.value, value); REQUIRE(value.txSetHash == prepared->getContentsHash()); REQUIRE(getConsensusTime(value) == preparedCloseTime); - REQUIRE(pulls == 1); + REQUIRE(builds() == 1); } SECTION("clock regression rejects the prepared future close time") { @@ -6079,7 +6105,7 @@ TEST_CASE("prepare proposal before trigger", "[herder][early-proposal]") // The fallback builds with today's valid close time, and must not // claim that our time-bound transaction is valid for that time. herder.triggerNextLedger(seq, true); - REQUIRE(pulls == 2); + REQUIRE(builds() == 2); REQUIRE(!LeaderBuildTestAccess::prepared(herder)); REQUIRE(std::get( herder.getTxSet(prepared->getContentsHash())) == nullptr); @@ -6089,17 +6115,17 @@ TEST_CASE("prepare proposal before trigger", "[herder][early-proposal]") herder.externalizeValue(TxSetXDRFrame::makeEmpty(lcl), seq, preparedCloseTime, {}, cfg.NODE_SEED); REQUIRE(LeaderBuildTestAccess::prepared(herder) != prepared); - auto const pullsAfterClose = pulls; - // A stale timer/caller cannot propose for the old slot or pull again. + auto const buildsAfterClose = builds(); + // A stale timer/caller cannot propose for the old slot or build again. herder.triggerNextLedger(seq, true); - REQUIRE(pulls == pullsAfterClose); + REQUIRE(builds() == buildsAfterClose); } SECTION("loss of sync discards preparation") { herder.lostSync(); REQUIRE(!LeaderBuildTestAccess::prepared(herder)); herder.triggerNextLedger(seq, true); - REQUIRE(pulls == 1); + REQUIRE(builds() == 1); } SECTION("shutdown cancels preparation") { @@ -6958,17 +6984,20 @@ TEST_CASE("the set prepared before the trigger is proposed unchanged", // More candidates than capacity, but they cannot fill the ledger. mempool = {bad->getEnvelope(), bad->getEnvelope(), bad->getEnvelope()}; } - size_t pulls = 0; - herder.mGetTopTransactionsForTesting = [&](size_t) { - ++pulls; - return mempool; + herder.mGetTopTransactionsForTesting = [&](size_t) { return mempool; }; + // Builds, not pulls of the mempool: the leader starts executing the + // prepared set at once, and that execution pulls candidates for the + // ledger after it. + auto builds = [&]() { + return app->getMetrics().NewTimer({"herder", "txset", "build"}).count(); }; + REQUIRE(builds() == 0); LeaderBuildTestAccess::schedule(herder); testutil::crankUntil( app, [&]() { return LeaderBuildTestAccess::prepared(herder) != nullptr; }, std::chrono::seconds(1)); - REQUIRE(pulls == 1); + REQUIRE(builds() == 1); auto const prepared = LeaderBuildTestAccess::prepared(herder); REQUIRE(prepared->sizeTxTotal() == (invalidFirst ? 0 : 1)); // It was built a short lead time before the trigger, not earlier. @@ -6982,7 +7011,7 @@ TEST_CASE("the set prepared before the trigger is proposed unchanged", app, [&]() { return herder.getSCP().getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); // No second build: the proposal names the prepared set. - REQUIRE(pulls == 1); + REQUIRE(builds() == 1); auto messages = herder.getSCP().getLatestMessagesSend(seq); REQUIRE(!messages.empty()); REQUIRE(messages.front().statement.pledges.type() == SCP_ST_PREPARE); diff --git a/src/herder/test/ProposalPrevalidationTests.cpp b/src/herder/test/ProposalPrevalidationTests.cpp index 0de8f5eea..75efc9dfe 100644 --- a/src/herder/test/ProposalPrevalidationTests.cpp +++ b/src/herder/test/ProposalPrevalidationTests.cpp @@ -8,10 +8,17 @@ // next ledger validates its candidates against it in the background // (HerderImpl::startProposalPrevalidation), and the build of the next set // takes those results instead of validating again (TxValidationCache). +// +// And of what the first in line does beyond that: it selects the next set on +// that state while the ledger is still in its ballot, completes it when the +// ledger has closed (HerderImpl::prepareEarlyTxSet), executes its own set as +// soon as it is prepared (HerderImpl::executePreparedTxSet), and checks the +// sets it builds in the background (TxSetSelfCheck). #include "crypto/SHA.h" #include "herder/Herder.h" #include "herder/HerderImpl.h" +#include "herder/HerderUtils.h" #include "herder/LeaderSchedule.h" #include "herder/LedgerCloseData.h" #include "herder/TxSetFrame.h" @@ -140,6 +147,173 @@ class ProposalPrevalidationTestAccess { return herder.mLastClosedTxSetSize; } + + // Whether the running pre-validation goes on to select a set. + static bool + selectsEarly(HerderImpl& herder) + { + return herder.mProposalPrevalidation && + herder.mProposalPrevalidation->selectEarly; + } + + // Waits until the running pre-validation has selected its set, or has + // given up on it. + static void + waitUntilSelected(HerderImpl& herder) + { + auto job = herder.mProposalPrevalidation; + REQUIRE(job); + REQUIRE(job->selectEarly); + auto const deadline = + std::chrono::steady_clock::now() + std::chrono::seconds(60); + while (!job->selectionDone.load()) + { + REQUIRE(std::chrono::steady_clock::now() < deadline); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + + static bool + hasEarlySet(HerderImpl& herder) + { + auto job = herder.mProposalPrevalidation; + if (!job) + { + return false; + } + std::lock_guard lock(job->mutex); + return job->early != nullptr; + } + + // Lets `f` change the selected set before the node looks at it. + template + static void + withEarlySet(HerderImpl& herder, F&& f) + { + auto job = herder.mProposalPrevalidation; + REQUIRE(job); + std::lock_guard lock(job->mutex); + REQUIRE(job->early); + f(*job->early); + } + + // The transactions the running job decoded as candidates of a set. + static size_t + pulledByJob(HerderImpl& herder) + { + REQUIRE(herder.mProposalPrevalidation); + return herder.mProposalPrevalidation->pulled.size(); + } + + // The close time the running job's set is validated for, as an offset + // from the executed ledger's. + static uint64_t + assumedCloseTimeOffset(HerderImpl& herder) + { + REQUIRE(herder.mProposalPrevalidation); + return herder.mProposalPrevalidation->assumedCloseTimeOffset; + } + + static bool + prepareEarly(HerderImpl& herder, uint32_t ledgerSeq, + ConsensusTime closeTime) + { + return herder.prepareEarlyTxSet(ledgerSeq, closeTime); + } + + // What the first-round leader does a lead time before the trigger. + static void + prepareByBuilding(HerderImpl& herder, uint32_t ledgerSeq, + ConsensusTime closeTime) + { + herder.prepareTxSet(ledgerSeq, closeTime); + } + + static TxSetXDRFrameConstPtr + prepared(HerderImpl& herder) + { + return herder.mPreparedTxSet ? herder.mPreparedTxSet->txSet : nullptr; + } + + static std::vector + preparedInvalidTxs(HerderImpl& herder) + { + REQUIRE(herder.mPreparedTxSet); + return herder.mPreparedTxSet->invalidTxHashes; + } + + // What the node proposes when the trigger fires. + static ValueWrapperPtr + propose(HerderImpl& herder, uint32_t ledgerSeq) + { + return herder.makeProposalValue(ledgerSeq); + } + + // The (slot, value) last handed to the apply thread by the herder. + static std::optional> const& + speculativeValue(HerderImpl& herder) + { + return herder.mSpeculativeApplyValue; + } + + // What the herder does when the node emits `envelope`. + static void + emitted(HerderImpl& herder, SCPEnvelope const& envelope) + { + herder.maybeStartSpeculativeApply(envelope); + } + + static bool + selfCheckHasFailed(HerderImpl& herder) + { + return herder.mTxSetSelfCheckFailed; + } + + static bool + buildMustStartNow(bool triggerDue, VirtualClock::time_point prepareAt, + VirtualClock::time_point now) + { + return HerderImpl::buildMustStartNow(triggerDue, prepareAt, now); + } + + // What setupTriggerNextLedger does for the first-round leader once it + // knows the trigger's time. + static void + schedulePreparation(HerderImpl& herder, uint32_t ledgerSeq, + VirtualClock::time_point triggerTime, bool triggerDue, + ConsensusTime minCloseTime) + { + herder.scheduleTxSetPreparation( + ledgerSeq, triggerTime, triggerDue, herder.mApp.getClock().now(), + herder.mLedgerManager.getExpectedLedgerCloseTime(), minCloseTime); + } + + // Whether a build is scheduled for a lead time before the trigger. + static bool + buildScheduled(HerderImpl& herder) + { + return herder.mPrepareTxSetTimer.seq() != 0; + } + + static void + discardPrepared(HerderImpl& herder) + { + herder.discardPreparedTxSet(); + } + + static std::chrono::milliseconds + prepareLeadTime(HerderImpl& herder) + { + return HerderImpl::prepareTxSetLeadTime( + herder.mLedgerManager.getExpectedLedgerCloseTime(), + herder.expectedTxSetBuildDuration()); + } + + static std::chrono::milliseconds + minBuildSaving() + { + return HerderImpl::EARLY_TX_SET_MIN_BUILD_SAVING; + } }; } @@ -237,14 +411,26 @@ struct PrevalidationFixture std::optional mE; // What the mempool holds; the Herder pulls it in this order. std::vector mMempool; - - PrevalidationFixture() + // How often the Herder pulled it. + size_t mPulls{0}; + + // With `herderSpeculates` the Herder hands ledgers to the apply thread + // itself, where it would in production; the tests still drive the + // speculation of the ledger they close first. + explicit PrevalidationFixture( + bool herderSpeculates = false, + std::optional maxTxSetSize = std::nullopt, + uint32_t protocol = GATE_PROTOCOL) : mCfg(getTestConfig(0, Config::TESTDB_BUCKET_DB_PERSISTENT)) { - mCfg.LEDGER_PROTOCOL_VERSION = GATE_PROTOCOL; - mCfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = GATE_PROTOCOL; + mCfg.LEDGER_PROTOCOL_VERSION = protocol; + mCfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = protocol; + if (maxTxSetSize) + { + mCfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = *maxTxSetSize; + } // The tests drive speculation themselves. - mCfg.EXPERIMENTAL_SPECULATIVE_LEDGER_APPLY = false; + mCfg.EXPERIMENTAL_SPECULATIVE_LEDGER_APPLY = herderSpeculates; REQUIRE(mCfg.backgroundLedgerApply()); mApp = createTestApplication(mClock, mCfg); auto root = mApp->getRoot(); @@ -255,7 +441,12 @@ struct PrevalidationFixture mC.emplace(root->create("C", balance)); mD.emplace(root->create("D", balance)); mE.emplace(root->create("E", balance)); + // The sets of these tests hold a few transactions: use them even + // though a build of that size would be quick. + herder().mEarlyTxSetMinBuildSavingForTesting = + std::chrono::milliseconds(0); herder().mGetTopTransactionsForTesting = [this](size_t) { + ++mPulls; std::vector envelopes; for (auto const& tx : mMempool) { @@ -268,6 +459,47 @@ struct PrevalidationFixture ~PrevalidationFixture() { herder().mGetTopTransactionsForTesting = nullptr; + herder().mBuiltTxSetOverrideForTesting = nullptr; + } + + // Cranks until `done`, giving background threads time to post to the + // main thread. + template + void + crankUntil(F&& done) + { + auto const deadline = + std::chrono::steady_clock::now() + std::chrono::seconds(60); + while (!done()) + { + REQUIRE(std::chrono::steady_clock::now() < deadline); + mClock.crank(false); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + + void + crankFor(std::chrono::milliseconds duration) + { + auto const deadline = std::chrono::steady_clock::now() + duration; + while (std::chrono::steady_clock::now() < deadline) + { + mClock.crank(false); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + + // Speculates on `closing` as the next ledger, lets the node select the + // set for the ledger after it, and closes the ledger with `closing`. + void + closeWithEarlySelection(TxSetXDRFrameConstPtr const& closing) + { + REQUIRE(speculate(closing, true)); + REQUIRE(Access::selectsEarly(herder())); + Access::waitUntilFinished(herder()); + Access::waitUntilSelected(herder()); + REQUIRE(Access::hasEarlySet(herder())); + externalize(closing); } HerderImpl& @@ -1144,3 +1376,1652 @@ TEST_CASE("leaders keep their term and pre-validate in a network", .mGetTopTransactionsForTesting = nullptr; } } + +// --------------------------------------------------------------------------- +// Early selection: the set for ledger N+1 is selected while ledger N is in +// its ballot and completed when N has closed. +// --------------------------------------------------------------------------- + +namespace +{ +int64_t +txsetMeter(Application& app, std::string const& name) +{ + return meter(app, "txset", name, "txset"); +} + +StellarValue +stellarValueOf(ValueWrapperPtr const& value) +{ + REQUIRE(value); + StellarValue sv; + REQUIRE(toStellarValue(value->getValue(), sv)); + return sv; +} +} + +TEST_CASE("the set selected during the previous ballot is proposed unchanged", + "[herder][txset][prevalidation][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& lm = f.lm(); + auto& a = *f.mA; + auto& b = *f.mB; + auto& c = *f.mC; + auto& d = *f.mD; + auto const aSeq = a.getLastSequenceNumber(); + auto const bSeq = b.getLastSequenceNumber(); + auto const cSeq = c.getLastSequenceNumber(); + auto const dSeq = d.getLastSequenceNumber(); + + // Ledger N and the candidates for N+1 of the pre-validation test above: + // A's next payment is only valid once N has applied, D's other payment + // only before, B's either way, C's never. + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto closingD = paymentWithSeq(app, d, dSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto staleD = paymentWithSeq(app, d, dSeq + 1, 2); + auto plainB = paymentWithSeq(app, b, bSeq + 1); + auto futureC = paymentWithSeq(app, c, cSeq + 5); + auto closing = makeUnvalidatedTxSet(app, {closingA, closingD}); + f.mMempool = {closingA, closingD, nextA, staleD, plainB, futureC}; + + auto const selectedBefore = txsetMeter(app, "early-selected"); + auto const finalizedBefore = txsetMeter(app, "early-finalized"); + REQUIRE(f.speculate(closing, true)); + REQUIRE(Access::selectsEarly(herder)); + Access::waitUntilFinished(herder); + Access::waitUntilSelected(herder); + REQUIRE(Access::hasEarlySet(herder)); + REQUIRE(txsetMeter(app, "early-selected") == selectedBefore + 1); + // N's own transactions, still in the mempool, are not candidates. + REQUIRE(Access::pulledByJob(herder) == 4); + // Nothing of this touched the last closed ledger or prepared a set. + REQUIRE(!Access::prepared(herder)); + auto const pulls = f.mPulls; + + f.externalize(closing); + auto const lcl = lm.getLastClosedLedgerHeader(); + // What reaches the mempool after the job's pull waits for a later set. + auto lateC = paymentWithSeq(app, c, cSeq + 1); + f.mMempool = {nextA, staleD, plainB, futureC, lateC}; + auto const closeTime = f.nextCloseTime(); + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), closeTime)); + REQUIRE(txsetMeter(app, "early-finalized") == finalizedBefore + 1); + + auto prepared = Access::prepared(herder); + REQUIRE(prepared); + // It names the ledger that closed meanwhile as its parent... + REQUIRE(prepared->previousLedgerHash() == lcl.hash); + // ... holds what is valid after that ledger, and nothing of it ... + auto applicable = prepared->prepareForApply(app, lcl.header); + REQUIRE(applicable); + REQUIRE(txHashes(applicable) == hashesOf({nextA, plainB})); + // ... and is a valid set on top of it, transactions included. + REQUIRE(applicable->checkValid(app, 1, 1)); + // D's stale payment leaves the mempool with the proposal; C's waits + // behind its account's sequence number. + REQUIRE(Access::preparedInvalidTxs(herder) == + std::vector{staleD->getFullHash()}); + // The mempool was not pulled again, and the job's results are there for + // a later build on this ledger. + REQUIRE(f.mPulls == pulls); + REQUIRE(!Access::running(herder)); + REQUIRE(Access::cachedResults(herder) == 4); + + // The trigger proposes that very set, for the close time it was + // completed for, without building. + auto const proposed = stellarValueOf(Access::propose(herder, f.nextSeq())); + REQUIRE(proposed.txSetHash == prepared->getContentsHash()); + REQUIRE(getConsensusTime(proposed) == closeTime); + REQUIRE(f.mPulls == pulls); + + // And the ledger applies it. + auto const skipped = applyCounter(app, "txset-skipped"); + f.externalize(prepared); + REQUIRE(applyCounter(app, "txset-skipped") == skipped); + REQUIRE(a.getLastSequenceNumber() == aSeq + 2); + REQUIRE(b.getLastSequenceNumber() == bSeq + 1); + REQUIRE(c.getLastSequenceNumber() == cSeq); +} + +// What ledger N does to a candidate's accounts decides whether the candidate +// can go into N+1. A set selected on N's executed state must hold what a build +// on the closed ledger N holds. +TEST_CASE("a set selected early holds what a build on the closed ledger does", + "[herder][txset][prevalidation][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto& c = *f.mC; + auto& d = *f.mD; + auto& e = *f.mE; + auto const root = app.getRoot()->getPublicKey(); + auto const aSeq = a.getLastSequenceNumber(); + auto const bSeq = b.getLastSequenceNumber(); + auto const dSeq = d.getLastSequenceNumber(); + auto const eSeq = e.getLastSequenceNumber(); + + // Ledger N: A pays; D pays away all it can spare; E merges into the root. + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto const spare = + d.getBalance() - app.getLedgerManager().getLastMinBalance(0) - 100; + REQUIRE(spare > 0); + auto drainD = paymentWithSeq(app, d, dSeq + 1, spare); + auto mergeE = transactionFromOperations(app, e.getSecretKey(), eSeq + 1, + {accountMerge(root)}, 100); + auto closing = makeUnvalidatedTxSet(app, {closingA, drainD, mergeE}); + // Candidates for N+1: A's next two payments (chained), D's next payment + // (D cannot pay the fee after N), E's (E is gone after N), and B's payment + // with C paying the fee. + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto laterA = paymentWithSeq(app, a, aSeq + 3); + auto brokeD = paymentWithSeq(app, d, dSeq + 2); + auto goneE = paymentWithSeq(app, e, eSeq + 2); + auto bumped = feeBump(app, c, paymentWithSeq(app, b, bSeq + 1), 1000); + f.mMempool = {closingA, drainD, mergeE, nextA, + laterA, brokeD, goneE, bumped}; + + f.closeWithEarlySelection(closing); + auto const lcl = f.lm().getLastClosedLedgerHeader(); + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + auto prepared = Access::prepared(herder); + REQUIRE(prepared); + auto applicable = prepared->prepareForApply(app, lcl.header); + REQUIRE(applicable); + auto const early = txHashes(applicable); + REQUIRE(early == hashesOf({nextA, bumped})); + REQUIRE(applicable->checkValid(app, 1, 1)); + + // The build on the closed ledger, validating everything itself. + Access::clearCache(herder); + f.mMempool = {nextA, laterA, brokeD, goneE, bumped}; + REQUIRE(f.build() == early); +} + +TEST_CASE("a set selected early is not used when it does not hold", + "[herder][txset][prevalidation][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + auto const bSeq = b.getLastSequenceNumber(); + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto plainB = paymentWithSeq(app, b, bSeq + 1); + auto closing = makeUnvalidatedTxSet(app, {closingA}); + f.mMempool = {closingA, nextA, plainB}; + auto const discardedBefore = txsetMeter(app, "early-discarded"); + auto const finalizedBefore = txsetMeter(app, "early-finalized"); + auto const seq = f.nextSeq(); + auto const closeTime = f.nextCloseTime(); + + auto selectFor = [&](TxSetXDRFrameConstPtr const& txSet) { + REQUIRE(f.speculate(txSet, true)); + Access::waitUntilFinished(herder); + Access::waitUntilSelected(herder); + REQUIRE(Access::hasEarlySet(herder)); + }; + + SECTION("another set closes the ledger") + { + selectFor(closing); + // B's payment closes the ledger instead: A did not move. + f.externalize(makeUnvalidatedTxSet(app, {plainB})); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + // The selected set held A's second payment and B's, neither of which + // is valid on the ledger that closed. + f.mMempool = {closingA, nextA, plainB}; + REQUIRE(f.build() == hashesOf({closingA})); + } + SECTION("the same set closes the ledger with another close time") + { + // Another value: not the ledger that was executed. + selectFor(closing); + auto later = ConsensusTime::fromApplyTime(ApplyTime::fromTimePoint( + f.lm().getLastClosedLedgerHeader().header.scpValue.closeTime + 2)); + app.getHerder().externalizeValue(closing, seq, later, + emptyUpgradeSteps); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + } + SECTION("the executed set was invalid") + { + // The ledger applies nothing, and its leader forfeits its term. + auto bad = paymentWithSeq(app, b, bSeq + 10); + auto invalid = makeUnvalidatedTxSet(app, {closingA, bad}); + auto state = f.speculate(invalid, true); + REQUIRE(state); + REQUIRE(state->mTxSetSkipped); + REQUIRE(!Access::running(herder)); + f.externalize(invalid); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + } + SECTION("the executed value carries upgrades") + { + LedgerUpgrade upgrade(LEDGER_UPGRADE_BASE_FEE); + upgrade.newBaseFee() = 200; + xdr::xvector upgrades; + upgrades.emplace_back(LedgerTestUtils::toUpgradeType(upgrade)); + REQUIRE(f.speculate(closing, true, upgrades)); + REQUIRE(!Access::running(herder)); + app.getHerder().externalizeValue(closing, seq, closeTime, upgrades); + REQUIRE(f.lm().getLastClosedLedgerHeader().header.baseFee == 200); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + } + SECTION("the limits of the closed ledger are not the ones it assumed") + { + selectFor(closing); + Access::withEarlySet(herder, [](auto& early) { + early.limits.mLclHeader.maxTxSetSize += 1; + }); + f.externalize(closing); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + REQUIRE(txsetMeter(app, "early-discarded") == discardedBefore + 1); + // The job's validation results are still good for the build. + f.mMempool = {nextA, plainB}; + auto const before = cacheCounts(app); + REQUIRE(f.build() == hashesOf({nextA, plainB})); + auto const after = cacheCounts(app); + REQUIRE(after.mHit - before.mHit == 2); + REQUIRE(after.mMiss - before.mMiss == 0); + } + SECTION("it is for the ledger after the executed one only") + { + selectFor(closing); + f.externalize(closing); + REQUIRE( + !Access::prepareEarly(herder, f.nextSeq() + 1, f.nextCloseTime())); + // Another ledger closes on top before the node gets to use it. + f.externalize(makeUnvalidatedTxSet(app, {plainB})); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + } + SECTION("it can only be taken once") + { + selectFor(closing); + f.externalize(closing); + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(txsetMeter(app, "early-finalized") == finalizedBefore + 1); + } + SECTION("nothing is selected by a job that was stopped") + { + // Enough candidates to keep the background tasks busy for a moment. + f.mMempool = {closingA}; + for (int i = 0; i < 400; ++i) + { + f.mMempool.push_back(nextA); + } + REQUIRE(f.speculate(closing, true)); + f.externalize(closing); + // Whether or not the selection got done in time, the node either + // has a valid prepared set or none. + bool const used = + Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime()); + REQUIRE(used == (Access::prepared(herder) != nullptr)); + f.mMempool = {nextA}; + REQUIRE(f.build() == hashesOf({nextA})); + REQUIRE(!Access::running(herder)); + } +} + +TEST_CASE("time-dependent transactions of a set selected early follow its " + "close time", + "[herder][txset][prevalidation][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto& c = *f.mC; + auto const aSeq = a.getLastSequenceNumber(); + auto closing = + makeUnvalidatedTxSet(app, {paymentWithSeq(app, a, aSeq + 1)}); + auto plain = paymentWithSeq(app, a, aSeq + 2); + + // The ledger being closed gets the last close time plus one second. The + // set for the ledger after it is validated for the close time its + // trigger is expected to have: some seconds later. + auto const executedCloseTime = + f.lm().getLastClosedLedgerHeader().header.scpValue.closeTime + 1; + auto closeTimeAt = [&](uint64_t offset) { + return ConsensusTime::fromApplyTime( + ApplyTime::fromTimePoint(executedCloseTime + offset)); + }; + // B's payment is valid for an hour from now; C's only from an hour on. + PreconditionsV2 untilLater; + untilLater.timeBounds.activate().maxTime = executedCloseTime + 3600; + auto bounded = transactionWithV2Precondition(app, b, 1, 100, untilLater); + PreconditionsV2 notYet; + notYet.timeBounds.activate().minTime = executedCloseTime + 3600; + auto tooEarly = transactionWithV2Precondition(app, c, 1, 100, notYet); + REQUIRE(txValidityDependsOnCloseTime(bounded->getEnvelope())); + REQUIRE(txValidityDependsOnCloseTime(tooEarly->getEnvelope())); + f.mMempool = {plain, bounded, tooEarly}; + + REQUIRE(f.speculate(closing, true)); + Access::waitUntilFinished(herder); + Access::waitUntilSelected(herder); + REQUIRE(Access::hasEarlySet(herder)); + auto const assumed = Access::assumedCloseTimeOffset(herder); + REQUIRE(assumed >= 1); + REQUIRE(assumed < 3600); + f.externalize(closing); + auto const lcl = f.lm().getLastClosedLedgerHeader(); + auto const discardedBefore = txsetMeter(app, "early-discarded"); + + auto preparedTxs = [&]() { + auto prepared = Access::prepared(herder); + REQUIRE(prepared); + auto applicable = prepared->prepareForApply(app, lcl.header); + REQUIRE(applicable); + return txHashes(applicable); + }; + + SECTION("the close time it was validated for") + { + REQUIRE( + Access::prepareEarly(herder, f.nextSeq(), closeTimeAt(assumed))); + REQUIRE(preparedTxs() == hashesOf({plain, bounded})); + // C's payment was not valid for that close time. It is not removed + // from the mempool for it: the set could have got another one. + REQUIRE(Access::preparedInvalidTxs(herder).empty()); + } + SECTION("another close time at which they are still valid") + { + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), + closeTimeAt(assumed + 7))); + REQUIRE(preparedTxs() == hashesOf({plain, bounded})); + auto applicable = + Access::prepared(herder)->prepareForApply(app, lcl.header); + REQUIRE(applicable->checkValid(app, assumed + 7, assumed + 7)); + } + SECTION("a close time at which one of them is not valid any more") + { + // The set is not used as it is ... + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), closeTimeAt(3601))); + REQUIRE(!Access::prepared(herder)); + REQUIRE(txsetMeter(app, "early-discarded") == discardedBefore + 1); + // ... and the build for that close time leaves B's payment out and + // takes C's. + auto built = Access::build(herder, f.nextSeq(), closeTimeAt(3601)); + REQUIRE(built.second); + REQUIRE(txHashes(built.second) == hashesOf({plain, tooEarly})); + } + // Results for a particular close time never reach the cache that later + // builds on this ledger share. + REQUIRE(Access::cachedResults(herder) <= 1); +} + +// --------------------------------------------------------------------------- +// The leader executes its own set as soon as it is prepared. +// --------------------------------------------------------------------------- + +TEST_CASE("the leader executes its prepared set before the trigger", + "[herder][txset][speculative-apply][early-txset]") +{ + PrevalidationFixture f(/*herderSpeculates=*/true); + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& lm = f.lm(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + auto const bSeq = b.getLastSequenceNumber(); + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto plainB = paymentWithSeq(app, b, bSeq + 1); + auto closing = makeUnvalidatedTxSet(app, {closingA}); + f.mMempool = {closingA, nextA, plainB}; + f.closeWithEarlySelection(closing); + f.mMempool = {nextA, plainB}; + + auto const seq = f.nextSeq(); + auto const closeTime = f.nextCloseTime(); + auto const started = applyCounter(app, "speculation-started"); + auto const hits = applyCounter(app, "speculation-hit"); + auto const misses = applyCounter(app, "speculation-miss"); + + auto afterPreparing = [&]() { + auto prepared = Access::prepared(herder); + REQUIRE(prepared); + // The ledger is with the apply thread already, although the node + // has not proposed anything yet. + REQUIRE(applyCounter(app, "speculation-started") == started + 1); + while (lm.isSpeculativelyApplying()) + { + f.mClock.crank(true); + } + REQUIRE(lm.getLastClosedLedgerNum() == seq - 1); + // It is the very value the trigger proposes ... + auto const proposal = Access::propose(herder, seq); + REQUIRE(proposal); + REQUIRE(Access::speculativeValue(herder)); + REQUIRE(Access::speculativeValue(herder)->first == seq); + REQUIRE(Access::speculativeValue(herder)->second == + proposal->getValue()); + REQUIRE(stellarValueOf(proposal).txSetHash == + prepared->getContentsHash()); + // ... so the vote the node then emits does not start it again. + SCPEnvelope vote; + vote.statement.slotIndex = seq; + vote.statement.pledges.type(SCP_ST_PREPARE); + vote.statement.pledges.prepare().ballot.counter = 1; + vote.statement.pledges.prepare().ballot.value = proposal->getValue(); + Access::emitted(herder, vote); + REQUIRE(applyCounter(app, "speculation-started") == started + 1); + + SECTION("its value externalizes: the execution is committed") + { + f.externalize(prepared); + REQUIRE(applyCounter(app, "speculation-hit") == hits + 1); + REQUIRE(applyCounter(app, "speculation-miss") == misses); + REQUIRE(a.getLastSequenceNumber() == aSeq + 2); + REQUIRE(b.getLastSequenceNumber() == bSeq + 1); + } + SECTION("another value externalizes: the execution is dropped") + { + f.externalize(makeUnvalidatedTxSet(app, {plainB})); + REQUIRE(applyCounter(app, "speculation-hit") == hits); + REQUIRE(applyCounter(app, "speculation-miss") == misses + 1); + REQUIRE(a.getLastSequenceNumber() == aSeq + 1); + REQUIRE(b.getLastSequenceNumber() == bSeq + 1); + } + SECTION("the node votes for another value: that one is executed") + { + auto other = makeUnvalidatedTxSet(app, {plainB}); + herder.getPendingEnvelopes().putTxSet(other->getContentsHash(), seq, + other); + auto const otherValue = f.valueFor(other); + vote.statement.pledges.prepare().ballot.value = + xdr::xdr_to_opaque(otherValue); + Access::emitted(herder, vote); + REQUIRE(applyCounter(app, "speculation-started") == started + 2); + while (lm.isSpeculativelyApplying()) + { + f.mClock.crank(true); + } + f.externalize(other); + REQUIRE(applyCounter(app, "speculation-hit") == hits + 1); + REQUIRE(a.getLastSequenceNumber() == aSeq + 1); + REQUIRE(b.getLastSequenceNumber() == bSeq + 1); + } + }; + + SECTION("a set selected early") + { + REQUIRE(Access::prepareEarly(herder, seq, closeTime)); + afterPreparing(); + } + SECTION("a set built before the trigger") + { + Access::prepareByBuilding(herder, seq, closeTime); + afterPreparing(); + } +} + +TEST_CASE("a prepared set is not executed early when it cannot be", + "[herder][txset][speculative-apply][early-txset]") +{ + SECTION("speculative apply is off") + { + PrevalidationFixture f(/*herderSpeculates=*/false); + auto& app = *f.mApp; + auto& a = *f.mA; + f.mMempool = {paymentWithSeq(app, a, a.getLastSequenceNumber() + 1)}; + auto const started = applyCounter(app, "speculation-started"); + Access::prepareByBuilding(f.herder(), f.nextSeq(), f.nextCloseTime()); + REQUIRE(Access::prepared(f.herder())); + REQUIRE(applyCounter(app, "speculation-started") == started); + REQUIRE(!Access::speculativeValue(f.herder())); + } + SECTION("the node is about to propose another set") + { + PrevalidationFixture f(/*herderSpeculates=*/true); + auto& app = *f.mApp; + auto& a = *f.mA; + f.mMempool = {paymentWithSeq(app, a, a.getLastSequenceNumber() + 1)}; + f.herder().mProposedTxSetOverride = + [&](LedgerHeaderHistoryEntry const& lcl) { + return TxSetXDRFrame::makeEmpty(lcl); + }; + auto const started = applyCounter(app, "speculation-started"); + Access::prepareByBuilding(f.herder(), f.nextSeq(), f.nextCloseTime()); + REQUIRE(Access::prepared(f.herder())); + REQUIRE(applyCounter(app, "speculation-started") == started); + f.herder().mProposedTxSetOverride = nullptr; + } +} + +// --------------------------------------------------------------------------- +// The builder's self-check runs beside the use of the set. +// --------------------------------------------------------------------------- + +TEST_CASE("the builder checks the set it built", "[herder][txset][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& a = *f.mA; + auto& b = *f.mB; + auto txA = paymentWithSeq(app, a, a.getLastSequenceNumber() + 1); + auto txB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + auto const lcl = f.lm().getLastClosedLedgerHeader(); + PerPhaseTransactionList phases{{txA, txB}, {}}; + PerPhaseTransactionList invalid(2); + + // Built without the check: the caller gets it to run. + std::shared_ptr check; + bool handedOut = false; + auto built = makeTxSetFromTransactions( + phases, app, ApplyTimeOffset::fromSeconds(1), invalid, false, {}, false, + [&](TxSetXDRFrameConstPtr const&) { handedOut = true; }, {}, nullptr, + &check); + REQUIRE(handedOut); + REQUIRE(built.first); + REQUIRE(built.second); + REQUIRE(check); + REQUIRE(check->getTxSet() == built.first); + REQUIRE(built.second->getContentsHash() == built.first->getContentsHash()); + REQUIRE(txHashes(built.second) == hashesOf({txA, txB})); + + SECTION("passes on the set that was built") + { + REQUIRE(!check->run(app)); + // the same set as a build that checks before it returns + auto checked = makeTxSetFromTransactions( + phases, app, ApplyTimeOffset::fromSeconds(1), invalid); + REQUIRE(checked.first->getContentsHash() == + built.first->getContentsHash()); + } + SECTION("runs on another thread") + { + std::optional failure{"not run"}; + std::thread t([&]() { failure = check->run(app); }); + t.join(); + REQUIRE(!failure); + } + SECTION("a set that lost a transaction") + { + auto broken = + check->withTxSetForTesting(makeUnvalidatedTxSet(app, {txA})); + auto const failure = broken->run(app); + REQUIRE(failure); + REQUIRE(failure->find("shape is mismatched") != std::string::npos); + } + SECTION("a set for another ledger") + { + // The same transactions on top of the ledger before. (Checked off + // the main thread, as the node does: on the main thread reading + // such a set back is an assertion failure.) + auto xdr = built.first->getGeneralizedXDR(); + xdr.v1TxSet().previousLedgerHash = lcl.header.previousLedgerHash; + auto other = check->withTxSetForTesting( + TxSetXDRFrame::makeFromWire(std::move(xdr))); + std::optional failure; + std::thread t([&]() { failure = other->run(app); }); + t.join(); + REQUIRE(failure); + REQUIRE(failure->find("PREVIOUS_LEDGER_HASH_MISMATCH") != + std::string::npos); + } + SECTION("a set with a base fee below the ledger's") + { + auto cheap = testtxset::makeNonValidatedGeneralizedTxSet( + {{std::make_pair(std::make_optional(1), + std::vector{txA, txB})}, + {}}, + app, lcl.hash); + REQUIRE(cheap.first); + REQUIRE(check->withTxSetForTesting(cheap.first)->run(app)); + } +} + +TEST_CASE("a set that fails its self-check after it was handed out", + "[herder][txset][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto& c = *f.mC; + auto const aSeq = a.getLastSequenceNumber(); + auto const cSeq = c.getLastSequenceNumber(); + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + auto closing = makeUnvalidatedTxSet(app, {closingA}); + f.mMempool = {closingA, nextA, plainB}; + f.closeWithEarlySelection(closing); + f.mMempool = {nextA, plainB}; + auto const seq = f.nextSeq(); + auto const closeTime = f.nextCloseTime(); + auto const failedBefore = txsetMeter(app, "self-check-failed"); + auto const passedBefore = txsetMeter(app, "self-check-passed"); + + // What the node "builds": a set that is not what it selected. The one + // transaction it holds is not valid either (a sequence number too far + // ahead), so ledger apply refuses it. + auto broken = makeUnvalidatedTxSet(app, {paymentWithSeq(app, c, cSeq + 5)}); + auto buildBroken = [&]() { + herder.mBuiltTxSetOverrideForTesting = + [&](TxSetXDRFrameConstPtr const&) { return broken; }; + }; + auto failed = [&]() { + return txsetMeter(app, "self-check-failed") == failedBefore + 1; + }; + + SECTION("a set that passes stays prepared") + { + REQUIRE(Access::prepareEarly(herder, seq, closeTime)); + auto prepared = Access::prepared(herder); + f.crankUntil([&]() { + return txsetMeter(app, "self-check-passed") == passedBefore + 1; + }); + REQUIRE(Access::prepared(herder) == prepared); + REQUIRE(!Access::selfCheckHasFailed(herder)); + REQUIRE(txsetMeter(app, "self-check-failed") == failedBefore); + } + SECTION("selected early, caught before the proposal") + { + buildBroken(); + REQUIRE(Access::prepareEarly(herder, seq, closeTime)); + // The set is out (prepared, pushed) before the check has run. + REQUIRE(Access::prepared(herder) == broken); + herder.mBuiltTxSetOverrideForTesting = nullptr; + f.crankUntil(failed); + // Dropped: the trigger builds another one, checked before use. + REQUIRE(!Access::prepared(herder)); + REQUIRE(Access::selfCheckHasFailed(herder)); + auto const pulls = f.mPulls; + auto const proposed = stellarValueOf(Access::propose(herder, seq)); + REQUIRE(f.mPulls == pulls + 1); + REQUIRE(proposed.txSetHash != broken->getContentsHash()); + auto txSet = herder.getTxSet(proposed.txSetHash); + auto const* frame = std::get_if(&txSet); + REQUIRE(frame); + REQUIRE(*frame); + auto applicable = (*frame)->prepareForApply( + app, f.lm().getLastClosedLedgerHeader().header); + REQUIRE(applicable); + REQUIRE(txHashes(applicable) == hashesOf({nextA, plainB})); + // That build ran its check before returning: none in the + // background. + f.crankFor(std::chrono::milliseconds(50)); + REQUIRE(txsetMeter(app, "self-check-passed") == passedBefore); + } + SECTION("built before the trigger, caught before the proposal") + { + buildBroken(); + Access::prepareByBuilding(herder, seq, closeTime); + REQUIRE(Access::prepared(herder) == broken); + herder.mBuiltTxSetOverrideForTesting = nullptr; + f.crankUntil(failed); + REQUIRE(!Access::prepared(herder)); + REQUIRE(Access::selfCheckHasFailed(herder)); + } + SECTION("caught after the proposal") + { + buildBroken(); + REQUIRE(Access::prepareEarly(herder, seq, closeTime)); + // The trigger fires before the check is back: the set is proposed. + auto const proposed = stellarValueOf(Access::propose(herder, seq)); + REQUIRE(proposed.txSetHash == broken->getContentsHash()); + herder.mBuiltTxSetOverrideForTesting = nullptr; + f.crankUntil(failed); + REQUIRE(Access::selfCheckHasFailed(herder)); + // It cannot be recalled. Every node validates it when it applies + // the ledger, this one included: the ledger closes without it. + auto const skipped = applyCounter(app, "txset-skipped"); + f.externalize(broken); + REQUIRE(applyCounter(app, "txset-skipped") == skipped + 1); + REQUIRE(c.getLastSequenceNumber() == cSeq); + // From now on a set selected early is checked before it is used: + // a broken one is dropped there. + auto nextNextA = paymentWithSeq(app, a, aSeq + 3); + f.mMempool = {nextA, nextNextA, plainB}; + f.closeWithEarlySelection(makeUnvalidatedTxSet(app, {nextA})); + broken = makeUnvalidatedTxSet(app, {paymentWithSeq(app, c, cSeq + 5)}); + buildBroken(); + auto const discarded = txsetMeter(app, "early-discarded"); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + REQUIRE(txsetMeter(app, "early-discarded") == discarded + 1); + REQUIRE(txsetMeter(app, "self-check-failed") == failedBefore + 2); + herder.mBuiltTxSetOverrideForTesting = nullptr; + // The build that takes its place checks its own result. + f.mMempool = {nextNextA, plainB}; + REQUIRE(f.build() == hashesOf({nextNextA, plainB})); + } +} + +// A network in which every build has to start when the previous ledger +// closes (the lead time is as long as the target), so that leaders use the +// sets they selected during the previous ballot whenever they have one, and +// in which every ledger carries a payment that is only valid once the +// previous one has applied. +TEST_CASE("leaders propose sets selected during the previous ballot in a " + "network", + "[herder][leader-schedule][leader-term][prevalidation][early-txset]" + "[simulation]") +{ + Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); + auto simulation = Topologies::core(4, 0.75, networkID, [](int i) { + auto cfg = getTestConfig(i, Config::TESTDB_BUCKET_DB_PERSISTENT); + cfg.LEDGER_PROTOCOL_VERSION = GATE_PROTOCOL; + cfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = GATE_PROTOCOL; + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; + // Leaves the background selection the time a real ballot does: the + // commit of every ledger takes a moment of real time. + cfg.ARTIFICIALLY_DELAY_LEDGER_CLOSE_FOR_TESTING = + std::chrono::milliseconds(30); + return cfg; + }); + auto nodes = simulation->getNodes(); + auto nodeIDs = simulation->getNodeIDs(); + simulation->startAllNodes(); + + auto const rootKey = nodes[0]->getRoot()->getSecretKey(); + auto const rootID = rootKey.getPublicKey(); + auto const firstSeq = nodes[0]->getRoot()->getLastSequenceNumber(); + std::vector payments; + for (int i = 1; i <= 400; ++i) + { + payments.push_back(transactionFromOperations(*nodes[0], rootKey, + firstSeq + i, + {payment(rootID, 1)}, 100) + ->getEnvelope()); + } + auto rootSeq = [&](Application& app) { + auto root = app.getLedgerManager().getLCLView()->getAccount(rootID); + REQUIRE(root); + return root.current().data.account().seqNum; + }; + for (auto const& node : nodes) + { + auto* app = node.get(); + auto& herder = static_cast(app->getHerder()); + herder.mPrepareLeadTimeForTesting = std::chrono::seconds(3600); + herder.mEarlyTxSetMinBuildSavingForTesting = + std::chrono::milliseconds(0); + // The mempool as the overlay keeps it: a closed ledger's payments + // are gone, the next few are there. While a ledger is in its + // ballot its own payment is still the first one. + herder.mGetTopTransactionsForTesting = [&, app](size_t) { + std::vector top; + auto const next = static_cast(rootSeq(*app) - firstSeq); + for (size_t i = next; i < next + 4 && i < payments.size(); ++i) + { + top.push_back(payments[i]); + } + return top; + }; + } + + auto& lm = nodes[1]->getLedgerManager(); + auto prev = lm.getLastClosedLedgerHeader(); + uint32_t const target = prev.header.ledgerSeq + 40; + Hash const emptyResults = xdrSha256(TransactionResultSet{}); + size_t withTransactions = 0; + size_t termStartsWithTransactions = 0; + simulation->crankUntil( + [&]() { + auto const cur = lm.getLastClosedLedgerHeader(); + if (cur.header.ledgerSeq == prev.header.ledgerSeq) + { + return false; + } + REQUIRE(cur.header.ledgerSeq == prev.header.ledgerSeq + 1); + if (cur.header.txSetResultHash != emptyResults) + { + ++withTransactions; + if (cur.header.ledgerSeq % LEADER_TERM_LEDGERS == 0) + { + ++termStartsWithTransactions; + } + } + prev = cur; + return cur.header.ledgerSeq >= target; + }, + 60 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(prev.header.ledgerSeq >= target); + auto const paid = rootSeq(*nodes[1]) - firstSeq; + simulation->crankUntil( + [&]() { return simulation->haveAllExternalized(target, 2); }, + 10 * simulation->getExpectedLedgerCloseTime(), false); + + int64_t selected = 0; + int64_t finalized = 0; + int64_t notReady = 0; + int64_t discarded = 0; + int64_t checksPassed = 0; + int64_t speculationHits = 0; + int64_t speculationMisses = 0; + int64_t timeouts = 0; + for (auto const& node : nodes) + { + selected += txsetMeter(*node, "early-selected"); + finalized += txsetMeter(*node, "early-finalized"); + notReady += txsetMeter(*node, "early-not-ready"); + discarded += txsetMeter(*node, "early-discarded"); + checksPassed += txsetMeter(*node, "self-check-passed"); + speculationHits += applyCounter(*node, "speculation-hit"); + speculationMisses += applyCounter(*node, "speculation-miss"); + timeouts += static_cast( + node->getMetrics() + .NewHistogram({"scp", "timeout", "leader"}) + .sum() + + node->getMetrics() + .NewHistogram({"scp", "timeout", "prepare"}) + .sum()); + // No leader proposed a set that apply found invalid, and no set + // failed its builder's check. + REQUIRE(applyCounter(*node, "txset-skipped") == 0); + REQUIRE(txsetMeter(*node, "self-check-failed") == 0); + } + INFO(fmt::format("ledgers with txs {} (term starts {}), sets selected " + "early {}, used {}, not ready {}, discarded {}, checks " + "passed {}, speculation hits {} misses {}, timeouts {}", + withTransactions, termStartsWithTransactions, selected, + finalized, notReady, discarded, checksPassed, + speculationHits, speculationMisses, timeouts)); + // Every ledger that carried transactions carried exactly the one + // payment that was valid on top of the ledger before it: a set selected + // on the executed state of the previous ledger is a set for the closed + // one. That includes the first ledger of each term, whose leader + // executed the previous ledger as a follower. + REQUIRE(withTransactions >= 30); + REQUIRE(paid == static_cast(withTransactions)); + REQUIRE(termStartsWithTransactions >= 4); + // Leaders did use sets they had selected early, and none had to be + // thrown away: nothing changes limits or close-time validity here. + REQUIRE(finalized >= 10); + REQUIRE(finalized <= selected); + REQUIRE(discarded == 0); + REQUIRE(checksPassed > 0); + // Leaders executed their own sets ahead of their proposal, and those + // executions were committed. + REQUIRE(speculationHits > 0); + + for (auto const& node : nodes) + { + auto& herder = static_cast(node->getHerder()); + herder.mGetTopTransactionsForTesting = nullptr; + herder.mPrepareLeadTimeForTesting.reset(); + herder.mEarlyTxSetMinBuildSavingForTesting.reset(); + } +} + +// --------------------------------------------------------------------------- +// More of the early selection: capacity, the Soroban phase, one account in +// both phases, limits, and when it is used. +// --------------------------------------------------------------------------- + +TEST_CASE("a set selected early is surge priced and validated past the " + "pre-validation window", + "[herder][txset][prevalidation][early-txset]") +{ + // Room for two operations per ledger: a build validates the first three + // candidates (5/4 of the capacity) and goes on only if some are invalid. + PrevalidationFixture f(/*herderSpeculates=*/false, /*maxTxSetSize=*/2); + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto& c = *f.mC; + auto& d = *f.mD; + auto& e = *f.mE; + REQUIRE(f.lm().getLastMaxTxSetSizeOps() == 2); + auto const root = app.getRoot()->getPublicKey(); + auto pay = [&](TestAccount& from, int64_t seqDelta, uint32_t fee) { + return transactionFromOperations( + app, from.getSecretKey(), from.getLastSequenceNumber() + seqDelta, + {payment(root, 1)}, fee); + }; + auto closingA = pay(a, 1, 100); + auto closing = makeUnvalidatedTxSet(app, {closingA}); + // After the closing ledger's own transaction, the mempool holds B's + // payment with a sequence number too far ahead, then three valid + // payments with different fees. The pre-validation window ends before + // the last (and best paying) one. + auto futureB = pay(b, 5, 500); + auto payC = pay(c, 1, 300); + auto payD = pay(d, 1, 200); + auto payE = pay(e, 1, 400); + f.mMempool = {closingA, futureB, payC, payD, payE}; + auto const validatedBefore = + meter(app, "prevalidate", "txs", "transaction"); + + f.closeWithEarlySelection(closing); + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + // The tasks validated the window; E's payment was validated by the + // selection itself, when B's turned out invalid. + REQUIRE(meter(app, "prevalidate", "txs", "transaction") == + validatedBefore + 3); + auto prepared = Access::prepared(herder); + REQUIRE(prepared); + auto const lcl = f.lm().getLastClosedLedgerHeader(); + auto applicable = prepared->prepareForApply(app, lcl.header); + REQUIRE(applicable); + // The two best paying ones fit. + auto const early = txHashes(applicable); + REQUIRE(early == hashesOf({payC, payE})); + REQUIRE(applicable->checkValid(app, 1, 1)); + // B's payment waits behind its account's sequence number. + REQUIRE(Access::preparedInvalidTxs(herder).empty()); + + // The same set as a build on the closed ledger selects. + Access::clearCache(herder); + f.mMempool = {futureB, payC, payD, payE}; + REQUIRE(f.build() == early); +} + +TEST_CASE("a set selected early can hold Soroban transactions", + "[herder][txset][prevalidation][early-txset][soroban]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + auto uploader = app.getRoot()->create( + "S", app.getLedgerManager().getLastMinBalance(2) + 1'000'000'000); + auto const sSeq = uploader.getLastSequenceNumber(); + SorobanResources resources; + resources.instructions = 1'000'000; + resources.diskReadBytes = 1000; + resources.writeBytes = 1000; + auto upload = createUploadWasmTx(app, uploader, 100, + DEFAULT_TEST_RESOURCE_FEE, resources); + REQUIRE(upload->isSoroban()); + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + auto closing = makeUnvalidatedTxSet(app, {closingA}); + f.mMempool = {closingA, upload, plainB}; + + f.closeWithEarlySelection(closing); + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + auto prepared = Access::prepared(herder); + REQUIRE(prepared); + auto const lcl = f.lm().getLastClosedLedgerHeader(); + auto applicable = prepared->prepareForApply(app, lcl.header); + REQUIRE(applicable); + REQUIRE(applicable->sizeTx(TxSetPhase::CLASSIC) == 1); + REQUIRE(applicable->sizeTx(TxSetPhase::SOROBAN) == 1); + auto const early = txHashes(applicable); + REQUIRE(early == hashesOf({upload, plainB})); + REQUIRE(applicable->checkValid(app, 1, 1)); + + // The build on the closed ledger agrees ... + Access::clearCache(herder); + f.mMempool = {upload, plainB}; + REQUIRE(f.build() == early); + // ... and the ledger applies the set. + auto const skipped = applyCounter(app, "txset-skipped"); + f.externalize(prepared); + REQUIRE(applyCounter(app, "txset-skipped") == skipped); + REQUIRE(uploader.getLastSequenceNumber() == sSeq + 1); +} + +// A set holds one transaction per source account, across its phases. The +// mempool can hold an account's classic and Soroban transaction side by side +// (two nodes accepted one each). +TEST_CASE("candidates of one account in both phases", + "[herder][txset][prevalidation][early-txset][soroban]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + auto both = app.getRoot()->create( + "S", app.getLedgerManager().getLastMinBalance(2) + 1'000'000'000); + auto const sSeq = both.getLastSequenceNumber(); + SorobanResources resources; + resources.instructions = 1'000'000; + resources.diskReadBytes = 1000; + resources.writeBytes = 1000; + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + auto classic = paymentWithSeq(app, both, sSeq + 1); + auto soroban = + createUploadWasmTx(app, both, 100, DEFAULT_TEST_RESOURCE_FEE, resources, + std::nullopt, 0, std::nullopt, sSeq + 1); + auto laterClassic = paymentWithSeq(app, both, sSeq + 2); + REQUIRE(soroban->getSeqNum() == classic->getSeqNum()); + + SECTION("a build keeps one of them") + { + SECTION("the same sequence number: the classic one") + { + f.mMempool = {soroban, classic, plainB}; + REQUIRE(f.build() == hashesOf({classic, plainB})); + } + SECTION("the Soroban one comes first in the account's sequence") + { + f.mMempool = {laterClassic, soroban, plainB}; + REQUIRE(f.build() == hashesOf({soroban, plainB})); + } + SECTION("the classic one comes first in the account's sequence") + { + auto laterSoroban = createUploadWasmTx( + app, both, 100, DEFAULT_TEST_RESOURCE_FEE, resources, + std::nullopt, 0, std::nullopt, sSeq + 2); + f.mMempool = {laterSoroban, classic, plainB}; + REQUIRE(f.build() == hashesOf({classic, plainB})); + } + } + SECTION("so does a set selected early") + { + auto closingA = paymentWithSeq(app, a, aSeq + 1); + f.mMempool = {closingA, soroban, classic, plainB}; + f.closeWithEarlySelection(makeUnvalidatedTxSet(app, {closingA})); + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + auto applicable = Access::prepared(herder)->prepareForApply( + app, f.lm().getLastClosedLedgerHeader().header); + REQUIRE(applicable); + REQUIRE(txHashes(applicable) == hashesOf({classic, plainB})); + REQUIRE(applicable->checkValid(app, 1, 1)); + } +} + +TEST_CASE("the limits a tx set is selected within", + "[herder][txset][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto const limits = TxSetBuildLimits::forLCL(app); + REQUIRE(limits.mSorobanConfig); + REQUIRE(limits.sameAs(limits)); + REQUIRE(limits.maxTxSetSizeOps() == f.lm().getLastMaxTxSetSizeOps()); + REQUIRE(limits.mLclHeader == f.lm().getLastClosedLedgerHeader().header); + + auto differs = [&](TxSetBuildLimits const& other) { + return !limits.sameAs(other) && !other.sameAs(limits); + }; + auto withHeader = [&](std::function change) { + auto other = limits; + change(other.mLclHeader); + return other; + }; + // What selection reads of the header ... + REQUIRE(differs(withHeader([](LedgerHeader& h) { h.ledgerVersion -= 1; }))); + REQUIRE(differs(withHeader([](LedgerHeader& h) { h.baseFee += 1; }))); + REQUIRE(differs(withHeader([](LedgerHeader& h) { h.baseReserve += 1; }))); + REQUIRE(differs(withHeader([](LedgerHeader& h) { h.maxTxSetSize += 1; }))); + // ... and what it does not: every ledger changes these. + REQUIRE(limits.sameAs(withHeader([](LedgerHeader& h) { + h.ledgerSeq += 1; + h.feePool += 100; + h.idPool += 1; + h.scpValue.closeTime += 1; + h.previousLedgerHash = sha256("another ledger"); + h.bucketListHash = sha256("another bucket list"); + h.txSetResultHash = sha256("other results"); + }))); + + // The Soroban limits of the ledger. + auto withoutSoroban = limits; + withoutSoroban.mSorobanConfig = nullptr; + REQUIRE(differs(withoutSoroban)); + auto withSoroban = [&](SorobanNetworkConfig& config, + std::function change) { + config = *limits.mSorobanConfig; + change(config); + auto other = limits; + other.mSorobanConfig = &config; + return other; + }; + SorobanNetworkConfig config = *limits.mSorobanConfig; + REQUIRE(limits.sameAs(withSoroban(config, [](SorobanNetworkConfig&) {}))); + REQUIRE(differs(withSoroban( + config, [](SorobanNetworkConfig& c) { c.mLedgerMaxTxCount += 1; }))); + REQUIRE(differs(withSoroban(config, [](SorobanNetworkConfig& c) { + c.mLedgerMaxInstructions += 1; + }))); + REQUIRE(differs(withSoroban(config, [](SorobanNetworkConfig& c) { + c.mLedgerMaxDependentTxClusters += 1; + }))); + REQUIRE(differs(withSoroban(config, [](SorobanNetworkConfig& c) { + c.mLedgerMaxTransactionsSizeBytes += 1; + }))); + REQUIRE(differs(withSoroban(config, [](SorobanNetworkConfig& c) { + c.mLedgerMaxDiskReadEntries += 1; + }))); +} + +TEST_CASE("a set selected early replaces only a build that has to start at " + "once", + "[herder][txset][early-txset]") +{ + VirtualClock clock; + auto const now = clock.now(); + auto const ms = [](int n) { return std::chrono::milliseconds(n); }; + // The trigger is due when the previous ledger closes. + REQUIRE(Access::buildMustStartNow(true, now, now)); + REQUIRE(Access::buildMustStartNow(true, now + ms(100), now)); + // The trigger is ahead, but less than a build's lead time away. + REQUIRE(Access::buildMustStartNow(false, now, now)); + REQUIRE(Access::buildMustStartNow(false, now - ms(1), now)); + // There is time for a build before the trigger: it holds what arrives + // in the meantime, and is preferred. + REQUIRE(!Access::buildMustStartNow(false, now + ms(1), now)); + REQUIRE(!Access::buildMustStartNow(false, now + ms(500), now)); +} + +TEST_CASE("the upgrades due are part of the value the leader executes early", + "[herder][txset][speculative-apply][early-txset]") +{ + PrevalidationFixture f(/*herderSpeculates=*/true); + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + f.mMempool = {paymentWithSeq(app, a, a.getLastSequenceNumber() + 1)}; + Upgrades::UpgradeParameters params; + params.mUpgradeTime = VirtualClock::system_time_point{}; + params.mBaseFee = 250; + herder.setUpgrades(params); + auto const seq = f.nextSeq(); + auto const started = applyCounter(app, "speculation-started"); + + Access::prepareByBuilding(herder, seq, f.nextCloseTime()); + REQUIRE(Access::prepared(herder)); + REQUIRE(applyCounter(app, "speculation-started") == started + 1); + while (f.lm().isSpeculativelyApplying()) + { + f.mClock.crank(true); + } + auto const proposal = Access::propose(herder, seq); + auto const proposed = stellarValueOf(proposal); + REQUIRE(proposed.upgrades.size() == 1); + REQUIRE(Access::speculativeValue(herder)); + REQUIRE(Access::speculativeValue(herder)->second == proposal->getValue()); +} + +// Executing a ledger starts with the full validation of its set: the same +// check every other node runs when it applies the ledger. When the leader's +// own execution of its prepared set is done before the trigger, an invalid +// set is not proposed. +TEST_CASE("a prepared set that the leader finds invalid when executing it " + "is not proposed", + "[herder][txset][speculative-apply][early-txset]") +{ + PrevalidationFixture f(/*herderSpeculates=*/true); + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto& c = *f.mC; + auto nextA = paymentWithSeq(app, a, a.getLastSequenceNumber() + 1); + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + f.mMempool = {nextA, plainB}; + auto const seq = f.nextSeq(); + auto const failedBefore = txsetMeter(app, "self-check-failed"); + auto const passedBefore = txsetMeter(app, "self-check-passed"); + auto const skippedBefore = applyCounter(app, "txset-skipped"); + + // What the node "builds" has the shape of its selection (two classic + // transactions) and passes the structural check, but one of its + // transactions is not valid: only validating the transactions shows it. + auto invalid = makeUnvalidatedTxSet( + app, {nextA, paymentWithSeq(app, c, c.getLastSequenceNumber() + 5)}); + herder.mBuiltTxSetOverrideForTesting = [&](TxSetXDRFrameConstPtr const&) { + return invalid; + }; + Access::prepareByBuilding(herder, seq, f.nextCloseTime()); + herder.mBuiltTxSetOverrideForTesting = nullptr; + REQUIRE(Access::prepared(herder) == invalid); + + f.crankUntil([&]() { + return txsetMeter(app, "self-check-failed") == failedBefore + 1 && + txsetMeter(app, "self-check-passed") == passedBefore + 1; + }); + // The apply thread skipped the set, and the herder dropped it. + REQUIRE(applyCounter(app, "txset-skipped") == skippedBefore + 1); + REQUIRE(!Access::prepared(herder)); + REQUIRE(Access::selfCheckHasFailed(herder)); + REQUIRE(f.lm().getLastClosedLedgerNum() == seq - 1); + // The trigger builds a set of its own. + auto const proposed = stellarValueOf(Access::propose(herder, seq)); + REQUIRE(proposed.txSetHash != invalid->getContentsHash()); + auto txSet = herder.getTxSet(proposed.txSetHash); + auto const* frame = std::get_if(&txSet); + REQUIRE(frame); + REQUIRE(*frame); + auto applicable = (*frame)->prepareForApply( + app, f.lm().getLastClosedLedgerHeader().header); + REQUIRE(applicable); + REQUIRE(txHashes(applicable) == hashesOf({nextA, plainB})); +} + +TEST_CASE("the application shuts down while a set is being checked or " + "selected", + "[herder][txset][prevalidation][early-txset]") +{ + auto f = std::make_unique(); + auto& app = *f->mApp; + auto& herder = f->herder(); + auto& a = *f->mA; + auto const aSeq = a.getLastSequenceNumber(); + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + + SECTION("the self-check of a set built before the trigger") + { + f->mMempool = {closingA}; + Access::prepareByBuilding(herder, f->nextSeq(), f->nextCloseTime()); + REQUIRE(Access::prepared(herder)); + f.reset(); + } + SECTION("the selection") + { + f->mMempool = {closingA}; + for (int i = 0; i < 400; ++i) + { + f->mMempool.push_back(nextA); + } + REQUIRE(f->speculate(makeUnvalidatedTxSet(app, {closingA}), true)); + REQUIRE(Access::selectsEarly(herder)); + Access::waitUntilFinished(herder); + // The validation is done; the selection may still be running. + f.reset(); + } + SECTION("the self-check of a set selected early") + { + f->mMempool = {closingA, nextA}; + f->closeWithEarlySelection(makeUnvalidatedTxSet(app, {closingA})); + REQUIRE(Access::prepareEarly(herder, f->nextSeq(), f->nextCloseTime())); + f.reset(); + } +} + +// --------------------------------------------------------------------------- +// When a set selected early is used. +// --------------------------------------------------------------------------- + +TEST_CASE("a set selected early is used only if a build of its size takes " + "time", + "[herder][txset][prevalidation][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + auto closing = makeUnvalidatedTxSet(app, {closingA}); + auto const smallBefore = txsetMeter(app, "early-small"); + auto const discardedBefore = txsetMeter(app, "early-discarded"); + // The rule as the node applies it, not the tests' override. + herder.mEarlyTxSetMinBuildSavingForTesting.reset(); + REQUIRE(Access::minBuildSaving() > std::chrono::milliseconds(0)); + + SECTION("an empty selection is never proposed") + { + // All the mempool held when the previous ledger was executed were + // that ledger's own transactions. + f.mMempool = {closingA}; + herder.mEarlyTxSetMinBuildSavingForTesting = + std::chrono::milliseconds(0); + f.closeWithEarlySelection(closing); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + REQUIRE(txsetMeter(app, "early-small") == smallBefore + 1); + // What arrived during the ballot goes into the build. + f.mMempool = {nextA, plainB}; + REQUIRE(f.build() == hashesOf({nextA, plainB})); + } + SECTION("a selection of a few transactions is left to a build") + { + f.mMempool = {closingA, nextA, plainB}; + f.closeWithEarlySelection(closing); + // A build of two transactions costs next to nothing at this node's + // rate (or at the rate assumed before it has built anything). + Access::setBuildHistory(herder, std::chrono::milliseconds(1), + std::chrono::microseconds(40)); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(!Access::prepared(herder)); + REQUIRE(txsetMeter(app, "early-small") == smallBefore + 1); + REQUIRE(txsetMeter(app, "early-discarded") == discardedBefore + 1); + // The build still has the job's validation results. + f.mMempool = {nextA, plainB}; + auto const before = cacheCounts(app); + REQUIRE(f.build() == hashesOf({nextA, plainB})); + auto const after = cacheCounts(app); + REQUIRE(after.mHit - before.mHit == 2); + } + SECTION("a selection whose build would take time is used") + { + f.mMempool = {closingA, nextA, plainB}; + f.closeWithEarlySelection(closing); + // At 10 ms per transaction, two transactions are worth it ... + Access::setBuildHistory(herder, std::chrono::milliseconds(1), + Access::minBuildSaving() / 2); + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(Access::prepared(herder)); + REQUIRE(txsetMeter(app, "early-small") == smallBefore); + } + SECTION("just below the threshold it is not") + { + f.mMempool = {closingA, nextA, plainB}; + f.closeWithEarlySelection(closing); + Access::setBuildHistory(herder, std::chrono::milliseconds(1), + Access::minBuildSaving() / 2 - + std::chrono::milliseconds(1)); + REQUIRE(!Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + REQUIRE(txsetMeter(app, "early-small") == smallBefore + 1); + } +} + +// HerderImpl::scheduleTxSetPreparation is what setupTriggerNextLedger does +// for the first-round leader once the previous ledger has closed and the next +// trigger's time is known. +TEST_CASE("how the leader gets its set ready after the previous ledger closed", + "[herder][txset][prevalidation][early-txset]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + auto closing = makeUnvalidatedTxSet(app, {closingA}); + f.mMempool = {closingA, nextA, plainB}; + auto const finalizedBefore = txsetMeter(app, "early-finalized"); + auto const lead = Access::prepareLeadTime(herder); + REQUIRE(lead > std::chrono::milliseconds(0)); + auto now = [&]() { return f.mClock.now(); }; + + SECTION("with a set selected during the previous ballot") + { + f.closeWithEarlySelection(closing); + auto const pulls = f.mPulls; + SECTION("the trigger is already due: the set is completed at once") + { + Access::schedulePreparation(herder, f.nextSeq(), now(), true, + f.nextCloseTime()); + REQUIRE(Access::prepared(herder)); + REQUIRE(!Access::buildScheduled(herder)); + REQUIRE(txsetMeter(app, "early-finalized") == finalizedBefore + 1); + REQUIRE(f.mPulls == pulls); + } + SECTION("the trigger is less than a lead time away: the same") + { + Access::schedulePreparation(herder, f.nextSeq(), now() + lead / 2, + false, f.nextCloseTime()); + REQUIRE(Access::prepared(herder)); + REQUIRE(!Access::buildScheduled(herder)); + REQUIRE(txsetMeter(app, "early-finalized") == finalizedBefore + 1); + } + SECTION("there is time for a build before the trigger: it is " + "scheduled, a lead time ahead") + { + Access::schedulePreparation(herder, f.nextSeq(), + now() + lead + std::chrono::seconds(1), + false, f.nextCloseTime()); + REQUIRE(!Access::prepared(herder)); + REQUIRE(Access::buildScheduled(herder)); + REQUIRE(txsetMeter(app, "early-finalized") == finalizedBefore); + // Nothing was built yet. + REQUIRE(f.mPulls == pulls); + } + Access::discardPrepared(herder); + } + SECTION("without one") + { + f.externalize(closing); + SECTION("a due trigger builds by itself: nothing is prepared or " + "scheduled") + { + Access::schedulePreparation(herder, f.nextSeq(), now(), true, + f.nextCloseTime()); + REQUIRE(!Access::prepared(herder)); + REQUIRE(!Access::buildScheduled(herder)); + } + SECTION("a trigger ahead: a build is scheduled") + { + Access::schedulePreparation(herder, f.nextSeq(), now() + lead / 2, + false, f.nextCloseTime()); + REQUIRE(!Access::prepared(herder)); + REQUIRE(Access::buildScheduled(herder)); + } + Access::discardPrepared(herder); + } + SECTION("the selection is not usable: a build is scheduled as before") + { + // (the ledger closed with another set) + REQUIRE(f.speculate(closing, true)); + Access::waitUntilFinished(herder); + Access::waitUntilSelected(herder); + f.externalize(makeUnvalidatedTxSet(app, {plainB})); + Access::schedulePreparation(herder, f.nextSeq(), now() + lead / 2, + false, f.nextCloseTime()); + REQUIRE(!Access::prepared(herder)); + REQUIRE(Access::buildScheduled(herder)); + Access::discardPrepared(herder); + } +} + +// The verdict of the node's own execution is about a set this node built, +// for the close time it built it for. +TEST_CASE("the verdict of the leader on its own set", + "[herder][txset][speculative-apply][early-txset]") +{ + PrevalidationFixture f(/*herderSpeculates=*/true); + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto& c = *f.mC; + auto const aSeq = a.getLastSequenceNumber(); + auto const failedBefore = txsetMeter(app, "self-check-failed"); + auto const skippedBefore = applyCounter(app, "txset-skipped"); + auto vote = [&](uint32_t seq, StellarValue const& value) { + SCPEnvelope envelope; + envelope.statement.slotIndex = seq; + envelope.statement.pledges.type(SCP_ST_PREPARE); + envelope.statement.pledges.prepare().ballot.counter = 1; + envelope.statement.pledges.prepare().ballot.value = + xdr::xdr_to_opaque(value); + Access::emitted(herder, envelope); + }; + auto executed = [&]() { + while (f.lm().isSpeculativelyApplying()) + { + f.mClock.crank(true); + } + f.crankFor(std::chrono::milliseconds(20)); + }; + + SECTION("a set selected early that is invalid is dropped before the " + "proposal") + { + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto nextA = paymentWithSeq(app, a, aSeq + 2); + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + f.mMempool = {closingA, nextA, plainB}; + f.closeWithEarlySelection(makeUnvalidatedTxSet(app, {closingA})); + // Same shape as the selection, one transaction not valid. + auto invalid = makeUnvalidatedTxSet( + app, + {nextA, paymentWithSeq(app, c, c.getLastSequenceNumber() + 5)}); + herder.mBuiltTxSetOverrideForTesting = + [&](TxSetXDRFrameConstPtr const&) { return invalid; }; + REQUIRE(Access::prepareEarly(herder, f.nextSeq(), f.nextCloseTime())); + herder.mBuiltTxSetOverrideForTesting = nullptr; + REQUIRE(Access::prepared(herder) == invalid); + f.crankUntil([&]() { + return txsetMeter(app, "self-check-failed") == failedBefore + 1; + }); + REQUIRE(applyCounter(app, "txset-skipped") == skippedBefore + 1); + REQUIRE(!Access::prepared(herder)); + REQUIRE(Access::selfCheckHasFailed(herder)); + } + SECTION("a verdict that comes after the proposal is recorded") + { + auto nextA = paymentWithSeq(app, a, aSeq + 1); + auto plainB = paymentWithSeq(app, b, b.getLastSequenceNumber() + 1); + f.mMempool = {nextA, plainB}; + auto invalid = makeUnvalidatedTxSet( + app, + {nextA, paymentWithSeq(app, c, c.getLastSequenceNumber() + 5)}); + herder.mBuiltTxSetOverrideForTesting = + [&](TxSetXDRFrameConstPtr const&) { return invalid; }; + auto const seq = f.nextSeq(); + Access::prepareByBuilding(herder, seq, f.nextCloseTime()); + herder.mBuiltTxSetOverrideForTesting = nullptr; + // The trigger fires before the execution has got anywhere. + auto const proposed = stellarValueOf(Access::propose(herder, seq)); + REQUIRE(proposed.txSetHash == invalid->getContentsHash()); + f.crankUntil([&]() { + return txsetMeter(app, "self-check-failed") == failedBefore + 1; + }); + REQUIRE(Access::selfCheckHasFailed(herder)); + // The ledger closes without applying the set, here as everywhere. + f.externalize(invalid); + REQUIRE(f.lm().getLastClosedLedgerNum() == seq); + REQUIRE(a.getLastSequenceNumber() == aSeq); + } + SECTION("an invalid set of another node says nothing about this node") + { + auto bad = paymentWithSeq(app, c, c.getLastSequenceNumber() + 5); + auto foreign = makeUnvalidatedTxSet(app, {bad}); + auto const seq = f.nextSeq(); + herder.getPendingEnvelopes().putTxSet(foreign->getContentsHash(), seq, + foreign); + vote(seq, f.valueFor(foreign)); + executed(); + REQUIRE(applyCounter(app, "txset-skipped") == skippedBefore + 1); + REQUIRE(txsetMeter(app, "self-check-failed") == failedBefore); + REQUIRE(!Access::selfCheckHasFailed(herder)); + } + SECTION("nor does this node's set under a close time it was not built " + "for") + { + // Valid for the close time the node built it for; another leader + // puts it forward for a later one, at which its time-bounded + // transaction has expired. + auto const builtFor = f.nextCloseTime(); + PreconditionsV2 cond; + cond.timeBounds.activate().maxTime = builtFor.toApplyTime().timePoint(); + auto bounded = transactionWithV2Precondition(app, b, 1, 100, cond); + f.mMempool = {bounded}; + auto const seq = f.nextSeq(); + Access::prepareByBuilding(herder, seq, builtFor); + auto own = Access::prepared(herder); + REQUIRE(own); + REQUIRE(own->sizeTxTotal() == 1); + executed(); + REQUIRE(applyCounter(app, "txset-skipped") == skippedBefore); + herder.getPendingEnvelopes().putTxSet(own->getContentsHash(), seq, own); + auto const later = ConsensusTime::fromApplyTime( + ApplyTime::fromTimePoint(builtFor.toApplyTime().timePoint() + 5)); + vote(seq, herder.makeStellarValue(own->getContentsHash(), later, + emptyUpgradeSteps, f.mCfg.NODE_SEED)); + executed(); + REQUIRE(applyCounter(app, "txset-skipped") == skippedBefore + 1); + REQUIRE(txsetMeter(app, "self-check-failed") == failedBefore); + REQUIRE(!Access::selfCheckHasFailed(herder)); + REQUIRE(Access::prepared(herder) == own); + } +} + +// What the selection itself can get wrong is checked before the set leaves +// the builder, also when the check of the written set comes later. +TEST_CASE("a build checks its selection before the set leaves it", + "[herder][txset][early-txset][soroban]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto both = app.getRoot()->create( + "S", app.getLedgerManager().getLastMinBalance(2) + 1'000'000'000); + auto const sSeq = both.getLastSequenceNumber(); + SorobanResources resources; + resources.instructions = 1'000'000; + resources.diskReadBytes = 1000; + resources.writeBytes = 1000; + TransactionFrameBasePtr classic = paymentWithSeq(app, both, sSeq + 1); + TransactionFrameBasePtr soroban = + createUploadWasmTx(app, both, 100, DEFAULT_TEST_RESOURCE_FEE, resources, + std::nullopt, 0, std::nullopt, sSeq + 1); + // Candidates as the herder must not hand them over: one account in both + // phases. + PerPhaseTransactionList phases{{classic}, {soroban}}; + PerPhaseTransactionList invalid(2); + bool handedOut = false; + auto onReady = [&](TxSetXDRFrameConstPtr const&) { handedOut = true; }; + + SECTION("with the check of the written set deferred") + { + std::shared_ptr check; + REQUIRE_THROWS_WITH( + makeTxSetFromTransactions( + phases, app, ApplyTimeOffset::fromSeconds(1), invalid, false, + {}, false, onReady, {}, nullptr, &check), + Catch::Matchers::Contains("MULTIPLE_TXS_PER_SOURCE_ACCOUNT")); + // Nothing was handed to the overlay or the peers. + REQUIRE(!handedOut); + REQUIRE(!check); + } + SECTION("with the check before returning, as always") + { + REQUIRE_THROWS_WITH( + makeTxSetFromTransactions(phases, app, + ApplyTimeOffset::fromSeconds(1), invalid, + false, {}, false, onReady), + Catch::Matchers::Contains("MULTIPLE_TXS_PER_SOURCE_ACCOUNT")); + } +} + +// Before tx sets are validated at apply, the other nodes validate a set +// before they vote for it, and the leader vouches for its own: there the +// builder checks the written set before it returns, as it always did. +TEST_CASE("the check of the written set is deferred only where sets are " + "validated at apply", + "[herder][txset][early-txset]") +{ + auto const deferred = GENERATE(false, true); + PrevalidationFixture f(/*herderSpeculates=*/false, std::nullopt, + deferred ? GATE_PROTOCOL : GATE_PROTOCOL - 1); + auto& app = *f.mApp; + auto& a = *f.mA; + REQUIRE(protocolValidatesTxSetsAtApply( + f.lm().getLastClosedLedgerHeader().header.ledgerVersion) == + deferred); + auto const passedBefore = txsetMeter(app, "self-check-passed"); + f.mMempool = {paymentWithSeq(app, a, a.getLastSequenceNumber() + 1)}; + Access::prepareByBuilding(f.herder(), f.nextSeq(), f.nextCloseTime()); + REQUIRE(Access::prepared(f.herder())); + if (deferred) + { + f.crankUntil([&]() { + return txsetMeter(app, "self-check-passed") == passedBefore + 1; + }); + } + else + { + f.crankFor(std::chrono::milliseconds(50)); + REQUIRE(txsetMeter(app, "self-check-passed") == passedBefore); + } +}