diff --git a/README.md b/README.md index d6f248438..7973a937c 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,28 @@ -# stellar-core: leader schedule, leader tx-set push and apply-time tx-set validation (experimental branch) +# stellar-core: leader schedule with terms, leader tx-set push, apply-time tx-set validation, transactions sent to the leaders only (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 is not compatible with nodes that nominate. -The benchmark is now **48 validators (16 organizations), 5,000 TPS, 1,000 ms target block time**. -The latest experiment (vote without validating the tx set, validate and execute at apply) was -measured there on commit `0c9a02e56`, the code of this branch, on 2026-11-11. "Before" is the -previous experiment on this branch (the leader's tx-set push), commit `8f7768be7`, measured on the -same day on the same fixture. One run per build. +The latest two experiments (leader terms with pre-validation, and transactions sent to the +upcoming leaders only) were measured on 2026-11-12 at an **800 ms target block time on 16-core +instances**: at 48 validators (16 organizations) and 8,000 TPS, and at the goal of this work, +**90 validators (30 organizations) and 10,000 TPS**. One run per build. + +- **48 validators, 8,000 TPS:** with both experiments every window is on target (block time p75 + 804-805 ms) and a node's overlay traffic is 6.2 MB/s. Without them the branch drifts and misses + its second window (846, then 869 ms) at 21 MB/s. +- **90 validators, 10,000 TPS:** not reached yet. With both experiments the first window is at 980 + 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 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 three experiments: +construction) plus five 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. @@ -58,13 +66,156 @@ construction) plus three experiments: - Details, safety argument and review findings: [#5498](https://github.com/stellar/stellar-core/pull/5498). -Unchanged by all three: the ballot protocol's accept, confirm and commit rules. For a valid tx +4. **Leader terms, pre-validation during the previous ballot, one build before the trigger** + ([#5502](https://github.com/stellar/stellar-core/pull/5502)). No protocol gate, like the + schedule it changes. + - **Terms.** The node whose value closed a ledger leads the next one, for up to 5 ledgers; a + term ends on a ledger number divisible by 5. The order of a slot is seeded with the hash of + the ledger before the last closed one, so it is known one ledger earlier. A leader loses its + term when its round times out or its ledger applies nothing. + - **Pre-validation.** The nodes that expect to lead the next ledger check the validity of their + mempool's top candidates against the state the speculative execution left, on background + threads, while the executed ledger is still in its ballot. The results are used only if the + ledger closes with the value that was executed. + - **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. + - **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 + first leader. One function in Core decides the list (`HerderImpl::computeTxFloodTargets`). + - **Keep and follow.** The accepting node keeps the transaction until it is externalized. When + the targets change it goes to a new target only while it is fresh (under 200 ms) or once + three ledgers closed without it. If a target is not a directly connected, identified peer, + the node floods as before. + - **Admission check.** A submitted transaction is checked against the last closed ledger + before it is sent on, with one pending transaction per source account. Before, a node + flooded whatever it was given. + - **Identity.** An overlay proves which validator it belongs to with a binding of its + 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 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. +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. ## Performance results -Target block time 1,000 ms, 5,000 TPS, 48 validators in 4 regions. +### 800 ms target on 16-core instances: 48 validators, 8,000 TPS + +c6id.8xlarge (16 cores), 4 regions, SAC payments, 150 ms simulated apply, 900 s of load; one run +per build, all on 2026-11-12. "Base" is this branch up to experiment 3. + +| Windows 1 / 2 / 3 / 4 | Base (`0c9a02e56`) | + leader terms and pre-validation (#5502, `deec2d87c`) | + transactions to leaders only (#5501, `6b3c7ae0e`) | +|---|---:|---:|---:| +| Block time windows on target | window 1 only; window 2 missed, the load stops there | **all four**, on all 48 nodes | **all four**, on all 48 nodes | +| p75, average over 48 nodes (limit 960 ms) | 846 / 869 ms, rising | 807 / 806 / 805 / 805 ms | **805 / 804 / 804 / 804 ms** | +| p99, average over 48 nodes (limit 1,600 ms) | 1,368 / 1,480 ms | 1,153 / 1,188 / 1,100 / 1,249 ms | 1,170 / 1,209 / 1,039 / 1,172 ms | +| Time per ledger | 0.84-0.85 s | 0.81 s | 0.81 s | +| Leader builds per ledger; build time | 1.9; 0.16 s | 1.0; 0.16 s, before the trigger | 1.0; 0.15 s, before the trigger | +| Trigger to first ballot | 0.34-0.36 s | 0.11 s | 0.10-0.12 s | +| First ballot to externalize | 0.29 s | 0.29 s | 0.29 s | +| Externalize to ledger closed | 0.04 s | 0.05 s | 0.04 s | +| **Trigger to ledger closed** | 0.67-0.69 s | **0.44-0.45 s** | **0.43-0.44 s** | +| Tx-set validation at apply | 0.08 s | 0.08 s | 0.10 s (signatures verified inline) | +| **Overlay traffic per node, each way** | 21.0 MB/s | 21.2 MB/s | **6.2 MB/s** | +| Overlay messages received per node | 20,000 /s | 20,500 /s | 12,550 /s | +| Transaction adverts sent per node | 7,848 /s | 7,848 /s | 0 | +| Leader and ballot timeouts, empty tx sets | 0 | 0 | 0 | +| Run verdict | FAIL (block time) | FAIL (inclusion check only) | FAIL (inclusion check only) | + +- **Leader terms and the single early build take the build off the critical path**: trigger to + ledger closed falls from 0.68 to 0.44 s, and the branch holds 800 ms at 8,000 TPS for the full + 15 minutes instead of drifting. +- **Sending transactions to the leaders only removes 71% of the overlay traffic** at the same + block time. Both runs sit at the floor of this fixture (the stages fill 0.44 s of the 0.8 s + cycle), so this fixture cannot show a block-time difference between them. +- What is left of the traffic is SCP (12,100 messages per second per node), the leader's pushed + set (about 5 MB per ledger) and 3.3 transaction bodies per transaction. The build measured + here re-sent every pending transaction when the targets changed; the head of #5501 no longer + does (2.5 bodies per transaction in the next table's run). +- **Inclusion.** The check is an exact comparison (see [The inclusion check](#the-inclusion-check)). + #5502 alone held 80 transactions more than the 7,680,000 offered, the run with #5501's first + version 6,610 more (0.086%): copies of transactions that reach a node after their ledger + closed, included a second time, which only the test's "overlay-only" mode permits. With the + head's rule for re-sends the next run had 81 on 3,870,249. + +### 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. + +| 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) | + +- **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 + limit, and the network runs 12% faster (0.90 s against 1.02 s per ledger). +- The gain is in trigger to first ballot: the leader's mempool holds about one ledger of + candidates instead of a backlog of 20,200, so its build is shorter and three of four builds + finish before the trigger again. +- **Overlay traffic is 15.7 MB/s per node instead of 43.** At 90 validators most of what is left + is SCP: 35,700 messages per second per node. +- 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. + +### Where a ledger's time goes at 90 validators and 10,000 TPS + +From the logs of twelve nodes (three per region) in the run with all five experiments, 56 +ledgers led by one of them: + +| Step on the leader | Mean | | +|---|---:|---| +| Previous ledger closed to proposal | 0.29 s | The build starts when the previous ledger closes (it needs that ledger) and takes 0.24 s for 8,500 transactions; the set is pushed while the build finishes | +| Proposal to consensus | 0.53 s | Of which, on a follower: 0.17 s until the set is in hand, then 0.34-0.36 s of ballot | +| Consensus to ledger closed | 0.07 s | The commit of the speculatively executed ledger | +| **Sum** | **0.89 s** | Target 0.80 s. 41 of the 56 cycles took longer than 0.8 s | + +1. **The build is on the critical path again: 0.29 s.** It cannot start before the previous + ledger has closed, and the cycle has no slack left for it, so the trigger is already due when + it ends (the proposal went out within 20 ms of the build's end on half of the ledgers that + had an early build). +2. **The leader's set takes 0.19 s to reach a follower's overlay**, 0.13 s of it even within the + leader's own region: the leader sends the full set (5.7 MB of XDR on average, compressed + once with zstd) to each of its 89 peers, up to 506 MB per ledger before compression. A + follower cannot vote before it has the set. Whether the sender's CPU or its network limits + this was not measured. +3. **The ballot is 0.34-0.36 s** from the set in hand to consensus. It is message delay between + four regions: proposal to consensus is 0.43 s when the leader is in us-east-1, 0.58 s from + us-west-2 and 0.60 s from ap-southeast-1, and a ledger led from ap-southeast-1 takes 0.96 s + against 0.83 s from us-east-1. +4. **A follower's execution ends just before consensus**: 0.15 s of validation (signatures + verified inline) plus the simulated apply, 0.31 s after the set arrived. +5. The main thread is not saturated (a posted task waits 14 ms on average). + +### Earlier benchmark: 1,000 ms target, 5,000 TPS, 48 validators, 4-core instances + +The measurement of experiment 3 (vote without validating the tx set, validate and execute at +apply), on commit `0c9a02e56` on 2026-11-11. "Before" is the previous experiment on this branch +(the leader's tx-set push), commit `8f7768be7`, measured on the same day on the same fixture. +One run per build. | | Before: tx-set push (`8f7768be7`) | With apply-time validation (`0c9a02e56`) | |---|---|---| @@ -219,9 +370,9 @@ generators had submitted exactly 4,800,000 and none was rejected. interrupted reads it survived, and the "before" build, with the unfixed reader, had no such event in its 30 minutes either. Whether the change makes the event more likely is not known. -### Where a ledger's time goes now +### Where a ledger's time went at 1,000 ms and 5,000 TPS -What is left of the 0.76-0.84 s from trigger to ledger closed: +What was left of the 0.76-0.84 s from trigger to ledger closed after experiment 3: 1. **Trigger to first ballot: 0.41-0.47 s.** Most of it is the leader's build (0.24-0.25 s), on the critical path. The first-round leader also builds once before the trigger and discards @@ -258,6 +409,26 @@ The first two experiments were measured on a smaller fixture (10 organizations, ## How it was measured +The sections below describe the earlier benchmark (1,000 ms, 5,000 TPS). The 800 ms runs of +2026-11-12 differ from it in these points only: + +| | 48 validators, 8,000 TPS | 90 validators, 10,000 TPS | +|---|---|---| +| Instance type | c6id.8xlarge: 32 vCPU, 16 cores, the same processor | the same | +| Organizations; validators per region | 16; 12 | 30; 22 or 23 | +| Connectivity | full mesh, 47 peers | full mesh, 89 peers | +| Target ledger close time | 800 ms | 800 ms | +| Offered load | 8,000 TPS, about 167 per validator | 10,080 TPS, 112 per validator | +| Maximum transactions per ledger | 8,000 | 10,000 | +| Simulated apply, per operation | 23 us (150 ms per ledger at the offered load) | 18 us | +| Window limits | p75 in [640, 960) ms, p99 at most 1,600 ms | the same | +| Validation tasks (one per core but one) | 15 | 15 | +| Logs | not saved; twelve nodes were read while the network was up | the same | +| Builds | `0c9a02e56` (base), `deec2d87c` (#5502), `6b3c7ae0e` (#5501 on #5502) | `8aa7f6bf0` (#5502), `6ab837c27` (#5501 on #5502) | + +`--block-time-ms 800 --soroban-tps 8000` (or `--org-count 30 --soroban-tps 10000`), +`--instance-type c6id.8xlarge` and no `--save-logs`; everything else as in the command below. + All runs use the `block_time_check` mission of perfnet, SDF's tooling for performance networks on AWS. Each run launches a fresh network, brings it to the target settings, runs the load, judges it, and terminates the network. @@ -379,6 +550,28 @@ Defaults that matter: validators as generators, `--scp-timeout-ms 2000`, ## Known limitations of the prototype +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. +- A node whose targets are not all directly connected, identified peers floods its transactions + as before, so a sparse topology gains nothing. +- The first leader of a new term is predicted one ledger ahead. If the ledger in between costs + that node its turn, another node leads and gets the pending transactions only at the close. +- After a leader failure the next round's leader lacks the transactions that are older than + 200 ms until that ledger closes; they reach it after it. +- A restarted node forgets the transactions it accepted and had not seen included. Before, every + node held them. +- A peer can fill a leader's mempool with invalid transactions that outbid valid ones; the leader + drops them when it builds. A refused submission returns an error without its result code. +- Followers verify the set's signatures at apply (0.10 s at 6,400 transactions, 0.14 s at 8,500, + on 15 tasks). On 4-core instances that cost made the change miss a 1,000 ms target at 5,000 TPS + unless validation also used the hyper-threads (`BATCH_EXECUTOR_TASKS`). +- Copies of a transaction that reach a node after its ledger closed are counted + (`overlay.flood.tx-late-copy`), not dropped. + +Leader terms and pre-validation: see [#5502](https://github.com/stellar/stellar-core/pull/5502). +The schedule change has no protocol gate, and a term gives one node five ledgers in a row. + Apply-time validation and speculative execution: - A nondeterministic validity check used to cost liveness at most. It now forks the ledger. The check's node-local inputs are the binary's protocol version, the network passphrase and, in test @@ -424,7 +617,10 @@ Leader schedule: Unchanged from stellar-core: see [INSTALL.md](./INSTALL.md), [CONTRIBUTING.md](./CONTRIBUTING.md) and [`./docs`](./docs). -Test status of this branch's code (`0c9a02e56`, the same tree as the merge of #5498): +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. + +Test status of the branch up to experiment 3 (`0c9a02e56`, the same tree as the merge of #5498): - The full test suite passes (615 test cases). - With `--all-versions` the suite passes on `a33da62c4`, the commit before the IPC fix. On `0c9a02e56` one test case of the leader-schedule change failed ("PendingEnvelopes diff --git a/docs/metrics.md b/docs/metrics.md index 0f290a46f..0b2d839a9 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -62,6 +62,9 @@ crypto.verify.miss | meter | number of signature cach crypto.verify.total | meter | sum of both hits and misses crypto.verify.tx-valid-hit | meter | signature cache hits that occurred while validating transactions (outside of background signature validation) crypto.verify.tx-valid-total | meter | sum of both hits and misses during transaction validation (outside of background signature validation) +herder.tx-admission.account-busy | meter | submitted transactions refused because their source account already has a pending transaction accepted by this node +herder.tx-admission.check | timer | time spent checking a submitted transaction against the last closed ledger before it is sent on +herder.tx-admission.invalid | meter | submitted transactions refused because they are invalid against the last closed ledger herder.txset.build | timer | time spent building the tx set proposed at nomination from the transaction queues herder.txset.validate | timer | time spent turning a received tx set into an applicable tx set and validating it on a validity-cache miss herder.txset.validate-sig-cache-hit | meter | signature cache hits while validating a received tx set (herder.txset.validate) @@ -166,6 +169,11 @@ overlay.flood.broadcast | meter | message sent as broadcas overlay.flood.duplicate_recv | meter | number of bytes of flooded messages that have already been received overlay.flood.unique_recv | meter | number of bytes of flooded messages that have not yet been received overlay.flood.tx-batch-size | histogram | number of transactions in a batch +overlay.flood.tx-direct-recv | meter | transaction bodies received without a demand (sent directly by a peer), kept and not relayed +overlay.flood.tx-late-copy | meter | transactions admitted although one of the last few externalized ledgers already has them (a copy that arrived late) +overlay.flood.tx-direct-sent | meter | transaction bodies sent straight to a target leader, without an advert +overlay.flood.tx-direct-sent-bytes | meter | bytes of those bodies +overlay.flood.tx-local-flooded | meter | own transactions advertised to every peer because no target list was set or a target was not directly connected overlay.inbound.attempt | meter | inbound connection attempted (accepted on socket) overlay.inbound.drop | meter | inbound connection dropped overlay.inbound.establish | meter | inbound connection established (added to pending) @@ -179,6 +187,8 @@ overlay.message.broadcast | meter | message broadcasted overlay.message.read | meter | message received overlay.message.write | meter | message sent overlay.message.drop | meter | message dropped due to load-shedding +overlay.node-id.identified | counter | connected peers that proved which validator they belong to +overlay.node-id.rejected | meter | validator identity claims from peers with a signature that does not bind them to the connection overlay.outbound.attempt | meter | outbound connection attempted (socket opened) overlay.outbound.cancel | meter | outbound connection cancelled overlay.outbound.drop | meter | outbound connection dropped diff --git a/docs/stellar-core_example.cfg b/docs/stellar-core_example.cfg index c53968c5f..a25807f38 100644 --- a/docs/stellar-core_example.cfg +++ b/docs/stellar-core_example.cfg @@ -291,6 +291,30 @@ EXPERIMENTAL_PARALLEL_TX_SET_DOWNLOAD = false # also enabled. BACKGROUND_TX_SIG_VERIFICATION = true +# TX_FLOOD_TARGET_LEADERS (integer) default 2 +# Experimental. How many upcoming round leaders get the transactions this +# node accepts (the leader of the node's current round of the next ledger and +# the ones after it; one ledger before a new leader term starts, also that +# term's expected first leader). The node sends them the transactions +# directly instead of advertising them to every peer. 0 = every validator of +# the leader schedule (no adverts either, but every validator holds every +# transaction before the leader's set arrives). +TX_FLOOD_TARGET_LEADERS = 2 + +# TX_FLOOD_LEGACY_ADVERTS (boolean) default false +# Experimental. If true, no target list is used: transactions are advertised +# to every peer and fetched on demand (the behaviour before +# TX_FLOOD_TARGET_LEADERS existed). +TX_FLOOD_LEGACY_ADVERTS = false + +# BATCH_EXECUTOR_TASKS (integer) default 0 +# Experimental. Number of tasks (worker threads) used for CPU-bound batch +# work: transaction validation when building and when applying a transaction +# set, and the background signature verification. 0 = one per physical core +# minus one. A larger value uses hyper-thread siblings once the physical +# cores are taken. +BATCH_EXECUTOR_TASKS = 0 + # NTP_DRIFT_CHECK_SERVER (string) default "pool.ntp.org" # Hostname of an NTP server that the node periodically queries (about every ten # minutes) to detect and warn about drift of the local clock. This is detection diff --git a/overlay/src/flood/mod.rs b/overlay/src/flood/mod.rs index cdf067e88..6bce126a8 100644 --- a/overlay/src/flood/mod.rs +++ b/overlay/src/flood/mod.rs @@ -8,6 +8,7 @@ mod inv_tracker; mod mempool; mod pending_requests; mod tx_buffer; +pub(crate) mod tx_router; mod txset; pub(crate) mod txset_encoding; pub(crate) mod txset_push; @@ -19,6 +20,7 @@ pub use inv_tracker::InvTracker; pub use mempool::Mempool; pub use pending_requests::PendingRequests; pub use tx_buffer::TxBuffer; +pub use tx_router::{NodeId, SendPlan, TxRouter}; pub use txset::{CachedTxSet, Hash256, TxSetCache}; pub use txset_encoding::TxSetData; pub use txset_push::{PushRateLimiter, PushRecord, UnclaimedSetBudget}; diff --git a/overlay/src/flood/tx_router.rs b/overlay/src/flood/tx_router.rs new file mode 100644 index 000000000..494b4f21f --- /dev/null +++ b/overlay/src/flood/tx_router.rs @@ -0,0 +1,946 @@ +//! Decides where this node's transactions go. +//! +//! Core knows the leader schedule and tells the overlay which validators get +//! transactions now (the "targets", usually the leaders of the current and +//! the next round), or that there is no such list ("everyone": the INV/GETDATA +//! flood to all peers). The router keeps the transactions this node accepted +//! from its own Core until they are externalized, removed or expire, and +//! works out what has to be sent when one is added, when the targets change +//! and when a peer proves which validator it is. +//! +//! Transactions go directly (the body, without an advert) only when every +//! target is this node itself or a connected peer that identified itself. +//! Otherwise the node floods them as before, so that they reach every mempool +//! whatever the topology. +//! +//! Pure bookkeeping: no I/O, no clock of its own. The owner holds it in a +//! mutex that is never held across an await. + +use std::collections::{HashMap, VecDeque}; +use std::hash::Hash; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use super::mempool::TxHash; +use crate::wire::ValidatedTx; + +/// A validator's public key (the NodeID's ed25519 key). +pub type NodeId = [u8; 32]; + +/// At most this many accepted transactions are kept for sending again; the +/// oldest is dropped first. Same bound as the mempool. +pub const MAX_LOCAL_TXS: usize = 100_000; + +/// An accepted transaction is given up after this long. Same age as the +/// mempool's. +pub const LOCAL_TX_MAX_AGE: Duration = Duration::from_secs(300); + +/// A transaction is not sent again to a peer that got it less than this long +/// ago on the same connection: a target that stays or soon is a target again +/// still has it. Sending again after that covers a body the peer dropped +/// under load (it has no way to ask for it), at a bounded cost while a +/// backlog keeps many transactions pending. +pub const RESEND_INTERVAL: Duration = Duration::from_secs(60); + +/// When the targets change, a kept transaction that already went to its +/// targets is sent to a validator that newly became one only if it may still +/// be wanted there: +/// * it is younger than [`FOLLOW_TARGETS_MAX_AGE`], so it may have missed +/// the set the previous targets were building when it reached them, or +/// * [`FOLLOW_TARGETS_AFTER_LEDGERS`] ledgers externalized here since it +/// was accepted and none of them had it. Three, counted on this node: the +/// first may have been built before the transaction was accepted, the +/// second before it reached the leader (this node may see ledgers late, +/// and the transaction takes a network hop), the third was not. Or +/// * it is older than [`FOLLOW_TARGETS_ANYWAY_AFTER`]: no ledger closes +/// (rounds are timing out), and the leaders of later rounds need it. +/// +/// Anything else is most likely in the set that is in consensus right now. +/// Sending it on would put a copy into the new target's mempool that, on a +/// node that is running late, arrives after that set externalized there: +/// the new target then holds a finalized transaction and offers it again. +/// +/// The age bound has to stay well below the time from a leader's build to +/// the externalize of its set (about 0.5 s at 800 ms ledgers): a fresh +/// transaction that did make the set must reach the new target before that. +pub const FOLLOW_TARGETS_MAX_AGE: Duration = Duration::from_millis(200); +pub const FOLLOW_TARGETS_AFTER_LEDGERS: u64 = 3; +pub const FOLLOW_TARGETS_ANYWAY_AFTER: Duration = Duration::from_secs(3); + +/// Hashes of recently externalized transactions that are remembered, to +/// count copies that arrive after their transaction is final (see +/// [`TxRouter::was_finalized`]): about six ledgers of 10,000. +pub const MAX_FINALIZED_TXS: usize = 60_000; + +/// Bodies remembered as "received directly from a peer" (see +/// [`TxRouter::note_direct`]). A target receives every node's transactions, +/// so this is about 20 s of a 5,000 TPS load; they are dropped earlier when +/// they are externalized. +pub const MAX_DIRECT_TXS: usize = 100_000; + +/// What to send, as decided by the router. The caller does the I/O. +#[derive(Debug)] +pub struct SendPlan

{ + /// Bodies to send straight to a peer. + pub direct: Vec<(P, Vec>)>, + /// Transactions to advertise to every peer (the INV/GETDATA flood). + pub flood: Vec>, +} + +impl

Default for SendPlan

{ + fn default() -> Self { + Self { + direct: Vec::new(), + flood: Vec::new(), + } + } +} + +impl

SendPlan

{ + pub fn is_empty(&self) -> bool { + self.direct.is_empty() && self.flood.is_empty() + } +} + +/// A transaction that arrived as a body nobody here asked for. +struct DirectTx { + tx: Arc, + /// Advertised to this node's peers since (it joined a flood). + relayed: bool, +} + +struct LocalTx

{ + tx: Arc, + added: Instant, + /// The router's ledger count when it was accepted. + added_at_ledger: u64, + /// A plan has looked at it under a direct route: from then on it only + /// follows the targets under the rule of [`FOLLOW_TARGETS_MAX_AGE`]. + planned: bool, + /// A send was lost (failed, or the peer went away): the next plan sends + /// to every target that has no record of it, whatever its age. + redeliver: bool, + /// Advertised to every peer: it is in every mempool that wanted it, so + /// nothing is left to send. + flooded: bool, + /// Peers the body was sent to, and when. + sent: Vec<(P, Instant)>, + /// Set when a plan found nothing more to send for this transaction: the + /// route generation it was planned under, and when the first of those + /// sends is old enough to be repeated. Until then, and while the route + /// has not changed, a plan skips it without looking at the peers. + settled: Option<(u64, Instant)>, +} + +/// Where the transactions go right now. +#[derive(Debug, PartialEq, Eq)] +enum Route

{ + /// Advertise to every peer. + Flood, + /// Send the body to these peers only (none if this node is the only + /// target). + Direct(Vec

), +} + +pub struct TxRouter

{ + /// This node's own validator key, once Core has sent it. + local_node: Option, + /// Validators that get transactions now; `None` = every peer. + targets: Option>, + /// Connected peers that proved which validator they are. + node_of_peer: HashMap, + /// Transactions this node accepted from its own Core. + local: HashMap>, + /// The same transactions, oldest first (stale entries are skipped). + order: VecDeque<(Instant, TxHash)>, + max_local: usize, + /// Changes whenever the targets or the identified peers do. + generation: u64, + /// Ledgers externalized since the router was created (as told by Core). + ledgers: u64, + /// Transactions peers sent here directly. They were not relayed (the + /// sender delivers to every target itself); they are kept so that this + /// node can join in, and serve the body, if the sender later has to + /// flood one. + direct: lru::LruCache, + /// Transactions of the last few externalized ledgers. + finalized: lru::LruCache, +} + +impl Default for TxRouter

{ + fn default() -> Self { + Self::new() + } +} + +impl TxRouter

{ + pub fn new() -> Self { + Self::with_capacity(MAX_LOCAL_TXS) + } + + pub fn with_capacity(max_local: usize) -> Self { + Self { + local_node: None, + targets: None, + node_of_peer: HashMap::new(), + local: HashMap::new(), + order: VecDeque::new(), + max_local, + generation: 0, + ledgers: 0, + direct: lru::LruCache::new(std::num::NonZeroUsize::new(MAX_DIRECT_TXS).unwrap()), + finalized: lru::LruCache::new( + std::num::NonZeroUsize::new(MAX_FINALIZED_TXS).unwrap(), + ), + } + } + + pub fn set_local_node(&mut self, node: NodeId) { + self.local_node = Some(node); + } + + /// Replace the targets (`None` = every peer). Returns whether they + /// changed; the caller then asks for [`Self::plan_all`]. + pub fn set_targets(&mut self, targets: Option>) -> bool { + if self.targets == targets { + return false; + } + self.targets = targets; + self.generation += 1; + true + } + + /// A connected peer proved that it is `node`. + pub fn peer_identified(&mut self, peer: P, node: NodeId) { + if self.node_of_peer.insert(peer, node) != Some(node) { + self.generation += 1; + } + } + + pub fn identified_peer_count(&self) -> usize { + self.node_of_peer.len() + } + + /// The validator a connected peer proved to be. + pub fn node_of(&self, peer: &P) -> Option<&NodeId> { + self.node_of_peer.get(peer) + } + + /// The peer is gone. Whatever was sent to it may not have arrived, and a + /// restarted node has an empty mempool: forget what it was sent. + pub fn peer_disconnected(&mut self, peer: &P) { + self.node_of_peer.remove(peer); + self.generation += 1; + for entry in self.local.values_mut() { + let before = entry.sent.len(); + entry.sent.retain(|(p, _)| p != peer); + if entry.sent.len() != before { + entry.redeliver = true; + entry.settled = None; + } + } + } + + /// A send to `peer` failed: these transactions were not delivered. + pub fn mark_unsent(&mut self, peer: &P, hashes: &[TxHash]) { + for hash in hashes { + if let Some(entry) = self.local.get_mut(hash) { + entry.settled = None; + entry.redeliver = true; + entry.sent.retain(|(p, _)| p != peer); + } + } + } + + /// Core accepted `tx` from a client. Keeps it and returns what to send + /// for it now. A transaction that is already kept is not sent again. + pub fn add_local(&mut self, tx: Arc, now: Instant) -> SendPlan

{ + let hash = *tx.hash(); + if self.local.contains_key(&hash) { + return SendPlan::default(); + } + self.evict_expired(now); + while self.local.len() >= self.max_local { + if !self.evict_oldest() { + break; + } + } + self.order.push_back((now, hash)); + let mut entry = LocalTx { + tx, + added: now, + added_at_ledger: self.ledgers, + planned: false, + redeliver: false, + flooded: false, + sent: Vec::new(), + settled: None, + }; + let route = self.route(); + let mut plan = SendPlan::default(); + Self::plan_one( + &route, + self.generation, + self.ledgers, + &mut entry, + now, + &mut plan, + ); + self.local.insert(hash, entry); + plan + } + + /// A peer sent `tx` directly (a body this node did not demand). + pub fn note_direct(&mut self, tx: Arc) { + self.direct.put(*tx.hash(), DirectTx { tx, relayed: false }); + } + + /// A peer advertises `hash`. If this node got that transaction directly + /// and has not relayed it, its origin has fallen back to flooding: + /// returns the transaction to advertise to the other peers, once. + pub fn join_flood(&mut self, hash: &TxHash) -> Option> { + let entry = self.direct.peek_mut(hash)?; + if entry.relayed { + return None; + } + entry.relayed = true; + Some(Arc::clone(&entry.tx)) + } + + /// The body of a transaction this node accepted or received directly, + /// for a peer that demands it after it left the short-lived buffer of + /// flooded transactions. + pub fn body(&self, hash: &TxHash) -> Option> { + if let Some(entry) = self.local.get(hash) { + return Some(Arc::clone(&entry.tx)); + } + self.direct.peek(hash).map(|entry| Arc::clone(&entry.tx)) + } + + /// Whether any transaction received directly is being kept. + pub fn has_direct(&self) -> bool { + !self.direct.is_empty() + } + + /// These transactions could not be advertised (no peer was connected): + /// they are flooded again at the next plan. + pub fn mark_unflooded(&mut self, hashes: &[TxHash]) { + for hash in hashes { + if let Some(entry) = self.local.get_mut(hash) { + entry.settled = None; + entry.flooded = false; + } + } + } + + /// Core dropped these transactions (or they are externalized: see + /// [`Self::ledger_externalized`]). + pub fn remove(&mut self, hashes: &[TxHash]) { + for hash in hashes { + self.local.remove(hash); + self.direct.pop(hash); + } + // `order` is cleaned lazily: an entry whose transaction is gone, or + // was added again later, is skipped when it reaches the front. + } + + /// A ledger externalized with these transactions. The caller then asks + /// for [`Self::plan_all`]: transactions that were held back from new + /// targets may be due now. + pub fn ledger_externalized(&mut self, hashes: &[TxHash]) { + self.remove(hashes); + for hash in hashes { + self.finalized.put(*hash, ()); + } + self.ledgers += 1; + } + + /// Whether `hash` was in one of the last few externalized ledgers: a + /// body that arrives now is a late copy. (This node's mempool does not + /// know: it would keep the copy and offer it to the next set it builds, + /// where validation against the ledger rejects it.) + pub fn was_finalized(&self, hash: &TxHash) -> bool { + self.finalized.contains(hash) + } + + /// What to send for every kept transaction, after the targets or the + /// identified peers changed, or a ledger externalized. + pub fn plan_all(&mut self, now: Instant) -> SendPlan

{ + self.evict_expired(now); + let route = self.route(); + let mut plan = SendPlan::default(); + // Oldest first, so that a target receives them in arrival order. + let hashes: Vec = self + .order + .iter() + .filter(|(added, hash)| self.local.get(hash).is_some_and(|e| e.added == *added)) + .map(|(_, hash)| *hash) + .collect(); + let generation = self.generation; + let ledgers = self.ledgers; + for hash in hashes { + if let Some(entry) = self.local.get_mut(&hash) { + Self::plan_one(&route, generation, ledgers, entry, now, &mut plan); + } + } + plan + } + + pub fn len(&self) -> usize { + self.local.len() + } + + pub fn is_empty(&self) -> bool { + self.local.is_empty() + } + + pub fn contains(&self, hash: &TxHash) -> bool { + self.local.contains_key(hash) + } + + /// Whether transactions currently go directly to targets. + pub fn is_direct(&self) -> bool { + matches!(self.route(), Route::Direct(_)) + } + + fn route(&self) -> Route

{ + let Some(targets) = &self.targets else { + return Route::Flood; + }; + let mut peers = Vec::new(); + for target in targets { + if self.local_node.as_ref() == Some(target) { + continue; + } + let before = peers.len(); + for (peer, node) in &self.node_of_peer { + if node == target && !peers.contains(peer) { + peers.push(*peer); + } + } + if peers.len() == before { + // A target this node cannot reach directly: flood, so that + // the transaction gets there through other nodes. + return Route::Flood; + } + } + Route::Direct(peers) + } + + fn plan_one( + route: &Route

, + generation: u64, + ledgers: u64, + entry: &mut LocalTx

, + now: Instant, + plan: &mut SendPlan

, + ) { + if entry.flooded { + return; + } + if let Some((planned, due)) = entry.settled { + if planned == generation && now < due { + return; + } + } + match route { + Route::Flood => { + entry.flooded = true; + entry.sent.clear(); + plan.flood.push(Arc::clone(&entry.tx)); + } + Route::Direct(peers) => { + // See FOLLOW_TARGETS_MAX_AGE. + let follows = !entry.planned + || entry.redeliver + || now.saturating_duration_since(entry.added) < FOLLOW_TARGETS_MAX_AGE + || now.saturating_duration_since(entry.added) >= FOLLOW_TARGETS_ANYWAY_AFTER + || ledgers.saturating_sub(entry.added_at_ledger) + >= FOLLOW_TARGETS_AFTER_LEDGERS; + let mut held_back = false; + let mut next_due = now + RESEND_INTERVAL; + for peer in peers { + let sent_at = entry.sent.iter().find(|(p, _)| p == peer).map(|(_, at)| *at); + match sent_at { + Some(at) if now.saturating_duration_since(at) < RESEND_INTERVAL => { + next_due = next_due.min(at + RESEND_INTERVAL); + continue; + } + // Sent long ago: again (see RESEND_INTERVAL). + Some(_) => {} + None if !follows => { + held_back = true; + continue; + } + None => {} + } + entry.sent.retain(|(p, _)| p != peer); + entry.sent.push((*peer, now)); + match plan.direct.iter_mut().find(|(p, _)| p == peer) { + Some((_, txs)) => txs.push(Arc::clone(&entry.tx)), + None => plan.direct.push((*peer, vec![Arc::clone(&entry.tx)])), + } + } + entry.planned = true; + entry.redeliver = false; + // A transaction that is held back is looked at again by + // every plan until it went out or is gone. + entry.settled = if held_back { + None + } else { + Some((generation, next_due)) + }; + } + } + } + + fn evict_expired(&mut self, now: Instant) { + while let Some((added, hash)) = self.order.front().copied() { + let live = self.local.get(&hash).is_some_and(|e| e.added == added); + if live && now.saturating_duration_since(added) <= LOCAL_TX_MAX_AGE { + break; + } + self.order.pop_front(); + if live { + self.local.remove(&hash); + } + } + } + + /// Drops the oldest kept transaction. Returns false if there is none. + fn evict_oldest(&mut self) -> bool { + while let Some((added, hash)) = self.order.pop_front() { + if self.local.get(&hash).is_some_and(|e| e.added == added) { + self.local.remove(&hash); + return true; + } + } + false + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::xdr::tests::valid_transaction_xdr; + + type Router = TxRouter; + + const ME: NodeId = [1; 32]; + const LEADER1: NodeId = [2; 32]; + const LEADER2: NodeId = [3; 32]; + const OTHER: NodeId = [4; 32]; + const P1: u32 = 10; + const P2: u32 = 20; + const P3: u32 = 30; + + fn tx(seq: i64) -> Arc { + ValidatedTx::from_core_trusted(valid_transaction_xdr(100, seq, 1), 100, 1).unwrap() + } + + /// A router that knows itself and three identified peers. + fn mesh() -> Router { + let mut router = Router::new(); + router.set_local_node(ME); + router.peer_identified(P1, LEADER1); + router.peer_identified(P2, LEADER2); + router.peer_identified(P3, OTHER); + router + } + + fn direct_hashes(plan: &SendPlan, peer: u32) -> Vec { + plan.direct + .iter() + .filter(|(p, _)| *p == peer) + .flat_map(|(_, txs)| txs.iter().map(|t| *t.hash())) + .collect() + } + + #[test] + fn without_targets_a_transaction_is_flooded_once() { + let mut router = mesh(); + let now = Instant::now(); + let a = tx(1); + let plan = router.add_local(a.clone(), now); + assert!(plan.direct.is_empty()); + assert_eq!(plan.flood.len(), 1); + assert_eq!(plan.flood[0].hash(), a.hash()); + // Flooded: every mempool has it, nothing is ever sent again. + assert!(router.plan_all(now).is_empty()); + router.set_targets(Some(vec![LEADER1, LEADER2])); + assert!(router.plan_all(now).is_empty()); + // Added again: still kept, nothing to send. + assert!(router.add_local(a, now).is_empty()); + assert_eq!(router.len(), 1); + } + + #[test] + fn with_targets_the_body_goes_to_the_targets_only() { + let mut router = mesh(); + assert!(router.set_targets(Some(vec![LEADER1, LEADER2]))); + assert!(router.is_direct()); + let now = Instant::now(); + let a = tx(1); + let plan = router.add_local(a.clone(), now); + assert!(plan.flood.is_empty()); + assert_eq!(direct_hashes(&plan, P1), vec![*a.hash()]); + assert_eq!(direct_hashes(&plan, P2), vec![*a.hash()]); + assert!(direct_hashes(&plan, P3).is_empty()); + // Same targets: not sent again. + assert!(!router.set_targets(Some(vec![LEADER1, LEADER2]))); + assert!(router.plan_all(now).is_empty()); + } + + #[test] + fn a_pending_transaction_follows_the_targets() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1, LEADER2])); + let now = Instant::now(); + let a = tx(1); + let b = tx(2); + router.add_local(a.clone(), now); + router.add_local(b.clone(), now); + // `a` is externalized; `b` was not included, and not in the two + // ledgers after it either. + router.ledger_externalized(&[*a.hash()]); + assert!(router.was_finalized(a.hash())); + assert!(!router.was_finalized(b.hash())); + router.ledger_externalized(&[]); + router.ledger_externalized(&[]); + assert!(router.set_targets(Some(vec![OTHER, LEADER1]))); + let plan = router.plan_all(now + Duration::from_secs(1)); + // The new target gets `b`; the one that stays a target has it. + assert_eq!(direct_hashes(&plan, P3), vec![*b.hash()]); + assert!(direct_hashes(&plan, P1).is_empty()); + assert!(direct_hashes(&plan, P2).is_empty()); + assert!(plan.flood.is_empty()); + // Back to an earlier target long after: it is sent again. + router.set_targets(Some(vec![LEADER2])); + let later = now + RESEND_INTERVAL + Duration::from_secs(1); + let plan = router.plan_all(later); + assert_eq!(direct_hashes(&plan, P2), vec![*b.hash()]); + } + + #[test] + fn this_node_as_a_target_needs_no_send() { + let mut router = mesh(); + router.set_targets(Some(vec![ME, LEADER2])); + let now = Instant::now(); + let plan = router.add_local(tx(1), now); + assert_eq!(plan.direct.len(), 1); + assert_eq!(plan.direct[0].0, P2); + // The only target: kept, nothing to send, and not flooded. + router.set_targets(Some(vec![ME])); + assert!(router.is_direct()); + let plan = router.add_local(tx(2), now); + assert!(plan.is_empty()); + assert_eq!(router.len(), 2); + } + + #[test] + fn an_unreachable_target_means_flooding() { + let mut router = Router::new(); + router.set_local_node(ME); + router.peer_identified(P1, LEADER1); + // LEADER2 is not a connected, identified peer. + router.set_targets(Some(vec![LEADER1, LEADER2])); + assert!(!router.is_direct()); + let now = Instant::now(); + let a = tx(1); + let plan = router.add_local(a.clone(), now); + assert!(plan.direct.is_empty()); + assert_eq!(plan.flood.len(), 1); + } + + #[test] + fn a_transaction_sent_directly_is_flooded_when_a_target_becomes_unreachable() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1, LEADER2])); + let now = Instant::now(); + let a = tx(1); + assert_eq!(router.add_local(a.clone(), now).direct.len(), 2); + // The next targets include a node that never identified itself. + router.set_targets(Some(vec![[9; 32], LEADER1])); + let plan = router.plan_all(now); + assert!(plan.direct.is_empty()); + assert_eq!(plan.flood.len(), 1); + assert_eq!(plan.flood[0].hash(), a.hash()); + // Once flooded it is done. + router.set_targets(Some(vec![LEADER1, LEADER2])); + assert!(router.plan_all(now).is_empty()); + } + + #[test] + fn identification_completes_the_route_and_sends_what_is_pending() { + let mut router = Router::new(); + router.set_local_node(ME); + router.set_targets(Some(vec![LEADER1])); + assert!(!router.is_direct()); + router.peer_identified(P1, LEADER1); + assert!(router.is_direct()); + let now = Instant::now(); + let a = tx(1); + assert_eq!(direct_hashes(&router.add_local(a.clone(), now), P1), vec![*a.hash()]); + } + + #[test] + fn a_transaction_follows_new_targets_only_while_fresh_or_once_it_missed_three_ledgers() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1, LEADER2])); + let start = Instant::now(); + let old = tx(1); + assert_eq!(router.add_local(old.clone(), start).direct.len(), 2); + + // The targets change while `old` is most likely in the set its + // targets are voting on; `fresh` was accepted just before the change + // and may have missed that set. + let change = start + Duration::from_millis(700); + let fresh = tx(2); + router.add_local(fresh.clone(), change - Duration::from_millis(100)); + let stale = tx(3); + router.add_local( + stale.clone(), + change - FOLLOW_TARGETS_MAX_AGE - Duration::from_millis(1), + ); + assert!(router.set_targets(Some(vec![LEADER1, OTHER]))); + let plan = router.plan_all(change); + assert_eq!(direct_hashes(&plan, P3), vec![*fresh.hash()]); + assert!(direct_hashes(&plan, P1).is_empty()); + // Asking again changes nothing: `old` is still held back. + assert!(router.plan_all(change + Duration::from_millis(50)).is_empty()); + + // The ledger that was in consensus externalizes without `old`: its + // set may have been built before `old` was accepted. The next one + // may have been built before `old` reached the leader. + router.ledger_externalized(&[]); + assert!(router.plan_all(change + Duration::from_millis(400)).is_empty()); + router.ledger_externalized(&[]); + assert!(router.plan_all(change + Duration::from_millis(1200)).is_empty()); + // The third was built with `old` in the mempool and did not have it + // either: now it goes to the new target, once, and so does the one + // that was just too old at the change. + router.ledger_externalized(&[]); + let plan = router.plan_all(change + Duration::from_millis(2000)); + assert_eq!(direct_hashes(&plan, P3), vec![*old.hash(), *stale.hash()]); + assert!(direct_hashes(&plan, P1).is_empty()); + assert!(router.plan_all(change + Duration::from_millis(2100)).is_empty()); + } + + #[test] + fn a_held_back_transaction_follows_the_targets_when_no_ledger_closes() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1, LEADER2])); + let start = Instant::now(); + let a = tx(1); + router.add_local(a.clone(), start); + // Two rounds time out: the leader of the third round is new. + assert!(router.set_targets(Some(vec![LEADER2, OTHER]))); + assert!(router.plan_all(start + Duration::from_secs(2)).is_empty()); + let plan = router.plan_all(start + FOLLOW_TARGETS_ANYWAY_AFTER); + assert_eq!(direct_hashes(&plan, P3), vec![*a.hash()]); + assert!(direct_hashes(&plan, P2).is_empty()); + } + + #[test] + fn a_removal_by_core_is_not_a_ledger() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1])); + let start = Instant::now(); + let a = tx(1); + let dropped = tx(2); + router.add_local(a.clone(), start); + router.add_local(dropped.clone(), start); + router.set_targets(Some(vec![LEADER1, LEADER2])); + // Core drops transactions for its own reasons, any number of times: + // that neither counts as a ledger nor marks them final. + for _ in 0..5 { + router.remove(&[*dropped.hash()]); + } + assert!(!router.was_finalized(dropped.hash())); + assert!(router.plan_all(start + Duration::from_secs(1)).is_empty()); + } + + #[test] + fn a_transaction_that_externalizes_is_never_sent_to_the_new_targets() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1])); + let start = Instant::now(); + let a = tx(1); + router.add_local(a.clone(), start); + // The node closes its ledger late: the targets change 600 ms after + // `a` went to the leader, which has it in the set in consensus. + assert!(router.set_targets(Some(vec![LEADER1, LEADER2]))); + assert!(router.plan_all(start + Duration::from_millis(600)).is_empty()); + // That set externalizes two ledgers later (this node sees ledgers + // late): `a` is gone, and LEADER2 never got a copy that could have + // arrived after it. + router.ledger_externalized(&[]); + assert!(router.plan_all(start + Duration::from_millis(900)).is_empty()); + router.ledger_externalized(&[]); + assert!(router.plan_all(start + Duration::from_millis(1700)).is_empty()); + router.ledger_externalized(&[*a.hash()]); + assert!(router.plan_all(start + Duration::from_secs(2)).is_empty()); + assert!(router.was_finalized(a.hash())); + } + + #[test] + fn a_transaction_this_node_led_follows_the_targets_under_the_same_rule() { + let mut router = mesh(); + // This node is the only target: nothing to send. + router.set_targets(Some(vec![ME])); + let start = Instant::now(); + let a = tx(1); + assert!(router.add_local(a.clone(), start).is_empty()); + // Its term ends while `a` may be in its own set in consensus. + router.set_targets(Some(vec![LEADER1])); + assert!(router.plan_all(start + Duration::from_millis(600)).is_empty()); + router.ledger_externalized(&[]); + router.ledger_externalized(&[]); + router.ledger_externalized(&[]); + let plan = router.plan_all(start + Duration::from_secs(2)); + assert_eq!(direct_hashes(&plan, P1), vec![*a.hash()]); + } + + #[test] + fn a_reconnected_target_gets_the_pending_transactions_again() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1])); + let now = Instant::now(); + let a = tx(1); + assert_eq!(router.add_local(a.clone(), now).direct.len(), 1); + router.set_targets(Some(vec![LEADER2])); + router.plan_all(now); + // LEADER1 restarts while it is not a target, then is one again, + // within the interval in which nothing is sent twice. + router.peer_disconnected(&P1); + assert!(router.node_of(&P1).is_none()); + router.peer_identified(P1, LEADER1); + router.set_targets(Some(vec![LEADER1])); + let plan = router.plan_all(now + Duration::from_secs(1)); + assert_eq!(direct_hashes(&plan, P1), vec![*a.hash()]); + } + + #[test] + fn a_failed_send_is_retried_at_the_next_plan() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1])); + let now = Instant::now(); + let a = tx(1); + router.add_local(a.clone(), now); + assert!(router.plan_all(now).is_empty()); + router.mark_unsent(&P1, &[*a.hash()]); + assert_eq!(direct_hashes(&router.plan_all(now), P1), vec![*a.hash()]); + } + + #[test] + fn two_peers_of_one_validator_both_get_the_body() { + let mut router = mesh(); + router.peer_identified(P3, LEADER1); + router.set_targets(Some(vec![LEADER1])); + let plan = router.add_local(tx(1), Instant::now()); + let mut peers: Vec = plan.direct.iter().map(|(p, _)| *p).collect(); + peers.sort(); + assert_eq!(peers, vec![P1, P3]); + } + + #[test] + fn kept_transactions_are_bounded_and_expire() { + let mut router = Router::with_capacity(2); + router.set_local_node(ME); + router.peer_identified(P1, LEADER1); + router.set_targets(Some(vec![OTHER])); // unreachable: no sends recorded + let now = Instant::now(); + let (a, b, c) = (tx(1), tx(2), tx(3)); + router.add_local(a.clone(), now); + router.add_local(b.clone(), now + Duration::from_secs(1)); + router.add_local(c.clone(), now + Duration::from_secs(2)); + // The oldest was dropped for the third. + assert_eq!(router.len(), 2); + assert!(!router.contains(a.hash())); + assert!(router.contains(b.hash()) && router.contains(c.hash())); + // Age: `b` expires first. + router.plan_all(now + Duration::from_secs(1) + LOCAL_TX_MAX_AGE + Duration::from_millis(1)); + assert!(!router.contains(b.hash())); + assert!(router.contains(c.hash())); + router.plan_all(now + Duration::from_secs(2) + LOCAL_TX_MAX_AGE + Duration::from_millis(1)); + assert!(router.is_empty()); + } + + #[test] + fn a_directly_received_transaction_joins_a_flood_once_and_can_be_served() { + let mut router = mesh(); + let (a, b) = (tx(1), tx(2)); + assert!(!router.has_direct()); + router.note_direct(a.clone()); + assert!(router.has_direct()); + // Not advertised by anyone: nothing to do. Kept, so a demand for it + // can be answered. + assert!(router.join_flood(b.hash()).is_none()); + assert_eq!(router.body(a.hash()).unwrap().hash(), a.hash()); + // First advert: join the flood. Later adverts: already done. + assert_eq!(router.join_flood(a.hash()).unwrap().hash(), a.hash()); + assert!(router.join_flood(a.hash()).is_none()); + assert!(router.body(a.hash()).is_some()); + // Externalized: forgotten. + router.remove(&[*a.hash()]); + assert!(router.body(a.hash()).is_none()); + assert!(!router.has_direct()); + // A transaction this node accepted itself can be served as well. + router.add_local(b.clone(), Instant::now()); + assert!(router.body(b.hash()).is_some()); + } + + #[test] + fn a_plan_with_unchanged_targets_repeats_a_send_only_after_the_interval() { + // With every validator as a target the list never changes: a body a + // peer dropped is only recovered by the periodic plan. + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1, LEADER2, OTHER])); + let now = Instant::now(); + let a = tx(1); + assert_eq!(router.add_local(a.clone(), now).direct.len(), 3); + for secs in [1, 10, 59] { + assert!(router.plan_all(now + Duration::from_secs(secs)).is_empty()); + } + let plan = router.plan_all(now + RESEND_INTERVAL); + assert_eq!(plan.direct.len(), 3); + assert!(router.plan_all(now + RESEND_INTERVAL + Duration::from_secs(1)).is_empty()); + // A failed send is repeated at the next plan, whatever the interval. + router.mark_unsent(&P2, &[*a.hash()]); + let plan = router.plan_all(now + RESEND_INTERVAL + Duration::from_secs(2)); + assert_eq!(direct_hashes(&plan, P2), vec![*a.hash()]); + assert!(direct_hashes(&plan, P1).is_empty()); + } + + #[test] + fn a_flood_that_reached_nobody_is_repeated() { + let mut router = Router::new(); + let now = Instant::now(); + let a = tx(1); + assert_eq!(router.add_local(a.clone(), now).flood.len(), 1); + assert!(router.plan_all(now).is_empty()); + // No peer was connected when it was to be advertised. + router.mark_unflooded(&[*a.hash()]); + assert_eq!(router.plan_all(now).flood.len(), 1); + assert!(router.plan_all(now).is_empty()); + } + + #[test] + fn a_removed_transaction_added_again_is_a_new_one() { + let mut router = mesh(); + router.set_targets(Some(vec![LEADER1])); + let now = Instant::now(); + let a = tx(1); + router.add_local(a.clone(), now); + router.remove(&[*a.hash()]); + assert!(router.is_empty()); + assert!(router.plan_all(now).is_empty()); + // Submitted again later: sent again, and it expires by its new age. + let later = now + Duration::from_secs(200); + assert_eq!(router.add_local(a.clone(), later).direct.len(), 1); + router.plan_all(now + LOCAL_TX_MAX_AGE + Duration::from_secs(1)); + assert!(router.contains(a.hash())); + router.plan_all(later + LOCAL_TX_MAX_AGE + Duration::from_secs(1)); + assert!(router.is_empty()); + } +} diff --git a/overlay/src/ipc/messages.rs b/overlay/src/ipc/messages.rs index d326a82e3..7ffb64682 100644 --- a/overlay/src/ipc/messages.rs +++ b/overlay/src/ipc/messages.rs @@ -62,6 +62,17 @@ pub enum MessageType { /// Payload: [hash:32][slotSeq:4] PushTxSet = 14, + /// This node's validator identity, in answer to `OverlayIdentity`: the + /// node key's signature over + /// `SHA256(networkID || "stellar-overlay-peer-id" || peerId)`. + /// Payload: [networkID:32][nodeID:32][signature:64] + SetNodeIdentity = 15, + + /// The validators that get this node's transactions now. + /// Payload: [everyone:1][count:4][nodeID:32]... + /// `everyone` = 1: flood to all peers (the list is ignored). + SetTxTargets = 16, + // ═══ Overlay → Core (Critical Path) ═══ /// Received SCP envelope from network ScpReceived = 100, @@ -80,6 +91,11 @@ pub enum MessageType { /// Overlay metrics snapshot response (JSON payload) OverlayMetricsResponse = 105, + + /// The overlay's libp2p peer id, sent once at startup so that Core can + /// sign the identity binding (`SetNodeIdentity`). + /// Payload: [peerId bytes] + OverlayIdentity = 106, } impl TryFrom for MessageType { @@ -100,11 +116,14 @@ impl TryFrom for MessageType { 12 => Ok(MessageType::CacheTxSet), 13 => Ok(MessageType::RequestOverlayMetrics), 14 => Ok(MessageType::PushTxSet), + 15 => Ok(MessageType::SetNodeIdentity), + 16 => Ok(MessageType::SetTxTargets), 100 => Ok(MessageType::ScpReceived), 101 => Ok(MessageType::TopTxsResponse), 102 => Ok(MessageType::PeerRequestsScpState), 103 => Ok(MessageType::TxSetAvailable), 105 => Ok(MessageType::OverlayMetricsResponse), + 106 => Ok(MessageType::OverlayIdentity), _ => Err(InvalidMessageType(value)), } } @@ -302,6 +321,9 @@ mod tests { MessageType::CacheTxSet, MessageType::RequestOverlayMetrics, MessageType::PushTxSet, + MessageType::SetNodeIdentity, + MessageType::SetTxTargets, + MessageType::OverlayIdentity, MessageType::ScpReceived, MessageType::TopTxsResponse, MessageType::PeerRequestsScpState, @@ -373,6 +395,15 @@ mod tests { MessageType::RequestOverlayMetrics ); assert_eq!(MessageType::try_from(14).unwrap(), MessageType::PushTxSet); + assert_eq!( + MessageType::try_from(15).unwrap(), + MessageType::SetNodeIdentity + ); + assert_eq!(MessageType::try_from(16).unwrap(), MessageType::SetTxTargets); + assert_eq!( + MessageType::try_from(106).unwrap(), + MessageType::OverlayIdentity + ); assert_eq!( MessageType::try_from(100).unwrap(), MessageType::ScpReceived @@ -401,7 +432,8 @@ mod tests { assert!(MessageType::try_from(9).is_err()); // gap between 8 and 10 assert!(MessageType::try_from(99).is_err()); assert!(MessageType::try_from(104).is_err()); - assert!(MessageType::try_from(106).is_err()); + assert!(MessageType::try_from(17).is_err()); + assert!(MessageType::try_from(107).is_err()); assert!(MessageType::try_from(u32::MAX).is_err()); } } diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index d936fd513..effb1017e 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -12,8 +12,8 @@ //! QUIC provides independent loss recovery per stream. use crate::flood::{ - GetData, InvBatch, InvBatcher, InvEntry, InvTracker, PendingRequests, PushRateLimiter, - PushRecord, TxBuffer, TxSetData, TxStreamMessage, + GetData, InvBatch, InvBatcher, InvEntry, InvTracker, NodeId, PendingRequests, PushRateLimiter, + PushRecord, SendPlan, TxBuffer, TxRouter, TxSetData, TxStreamMessage, }; #[cfg(test)] use crate::integrated::CoreCommand; @@ -47,6 +47,100 @@ pub const CONTROL_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/contr pub const TX_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/tx/1.0.0"); // This experimental protocol requires zstd for every transaction-set response. pub const TXSET_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/txset/zstd/2.0.0"); +/// One-shot stream on which an overlay tells a peer which validator it +/// belongs to: one frame, `[nodeID:32][signature:64]` (see `NodeIdentity`), +/// answered with one byte once it has been read. +pub const NODE_ID_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/node-id/1.0.0"); + +/// Domain separator of the identity binding that Core signs. +const NODE_ID_BINDING_TAG: &[u8] = b"stellar-overlay-peer-id"; +const NODE_ID_FRAME_LEN: usize = 32 + 64; + +/// Transaction bodies sent directly to a peer are collected for this long, +/// so that a burst of submissions (a client or load generator step) becomes +/// one write per peer instead of one per transaction. +const DIRECT_SEND_BATCH_INTERVAL: Duration = Duration::from_millis(10); + +/// A peer's queue of direct sends is written at once when it reaches this +/// many transactions. +const DIRECT_SEND_BATCH_MAX_TXS: usize = 256; + +/// At most this many transactions wait for one peer whose stream is not +/// taking them (one sender per peer writes at a time). What does not fit is +/// handed back to the router as not sent. +const DIRECT_SEND_MAX_QUEUED_TXS: usize = 20_000; + +/// How often the router is asked whether anything is owed although nothing +/// changed: a send that failed, a body old enough to be sent again, a flood +/// that found no peer. +const DIRECT_REPLAN_INTERVAL: Duration = Duration::from_secs(2); + +/// Direct sends per peer: what is queued, and which peers have a sender task +/// writing right now. +#[derive(Default)] +struct DirectOut { + queued: HashMap>>, + sending: HashSet, +} + +/// A peer's validator identity claim must arrive within this long of its +/// stream opening. +const NODE_ID_READ_TIMEOUT: Duration = Duration::from_secs(10); + +/// Acknowledgement bytes on the node id stream: the claim was dealt with +/// (accepted, kept for later, or refused), or the receiver could not use it +/// yet and the sender should repeat it. +const NODE_ID_ACK_TAKEN: u8 = 1; +const NODE_ID_ACK_RETRY: u8 = 0; + +/// Identity claims handled per connection. An honest peer sends one per +/// connection it establishes (two when both sides dial at once) and one more +/// if its Core's identity arrives late. +const MAX_NODE_ID_CLAIMS_PER_PEER: u32 = 8; + +/// This node's validator identity, as sent by Core: the node key's signature +/// over [`node_id_binding`] of this overlay's own peer id. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NodeIdentity { + pub network_id: [u8; 32], + pub node_id: NodeId, + pub signature: [u8; 64], +} + +/// What a validator signs to say "the overlay with this peer id is mine": +/// `SHA256(networkID || tag || peerId)`. The peer id is the one QUIC +/// authenticated, so the statement cannot be replayed by another peer. +pub fn node_id_binding(network_id: &[u8; 32], peer: &PeerId) -> [u8; 32] { + let peer_bytes = peer.to_bytes(); + let mut message = Vec::with_capacity(32 + NODE_ID_BINDING_TAG.len() + peer_bytes.len()); + message.extend_from_slice(network_id); + message.extend_from_slice(NODE_ID_BINDING_TAG); + message.extend_from_slice(&peer_bytes); + crate::xdr::sha256_hash(&message) +} + +/// Whether `signature` is `node_id`'s signature binding it to `peer`. +fn verify_node_id_binding( + network_id: &[u8; 32], + peer: &PeerId, + node_id: &NodeId, + signature: &[u8; 64], +) -> bool { + let Ok(key) = libp2p::identity::ed25519::PublicKey::try_from_bytes(node_id) else { + return false; + }; + key.verify(&node_id_binding(network_id, peer), signature) +} + +/// Identity state: ours, and claims from peers that arrived before Core told +/// us the network id needed to check them. +#[derive(Default)] +struct IdentityState { + ours: Option, + unverified: HashMap, + /// Claims handled per connected peer (see MAX_NODE_ID_CLAIMS_PER_PEER). + claims: HashMap, +} /// Message frame: 4-byte length prefix + payload /// Max message size: 16MB (for large TX sets) @@ -120,6 +214,14 @@ pub enum OverlayCommand { hash: [u8; 32], data: Arc, }, + /// Core accepted this transaction. The router already holds it; this + /// carries out what it decided at that moment. + SendLocalTx(SendPlan), + /// The targets or the identified peers changed: send what the router now + /// says is owed. + FlushLocalTxs, + /// Core sent this node's validator identity: tell every peer. + SetNodeIdentity(NodeIdentity), /// Connect to a peer by address (bootstrap — PeerId unknown) Dial(Multiaddr), /// Connect to a known peer by PeerId (reconnect — deduplicates automatically) @@ -196,6 +298,11 @@ pub struct OverlayHandle { cmd_tx: mpsc::Sender, txset_send_slots: Arc, txset_send_bytes: Arc, + /// Shared with the overlay task. The App updates it in the order of + /// Core's messages (a transaction accepted, transactions externalized, + /// new targets), then sends a command to act on it. + tx_router: Arc>>, + local_peer_id: PeerId, } impl OverlayHandle { @@ -221,6 +328,84 @@ impl OverlayHandle { } } + /// This overlay's own libp2p peer id (what Core's identity binding signs). + pub fn local_peer_id(&self) -> PeerId { + self.local_peer_id + } + + /// Core accepted `tx`: keep it until it is externalized, and work out + /// where it goes now. Synchronous, so that it is ordered with + /// [`Self::ledger_externalized`] and [`Self::set_tx_targets`]; the returned + /// plan is carried out with [`Self::send_local_tx`]. + pub fn add_local_tx(&self, tx: Arc) -> SendPlan { + self.tx_router + .lock() + .unwrap() + .add_local(tx, Instant::now()) + } + + /// Carry out a plan from [`Self::add_local_tx`]. + pub async fn send_local_tx(&self, plan: SendPlan) { + if plan.is_empty() { + return; + } + if let Err(e) = self.cmd_tx.send(OverlayCommand::SendLocalTx(plan)).await { + warn!( + "Overlay command channel closed, failed to send SendLocalTx: {}", + e + ); + } + } + + /// A ledger externalized with these transactions: stop sending them. + /// The caller then calls [`Self::flush_local_txs`]: transactions that + /// were held back from new targets may be due. + pub fn ledger_externalized(&self, hashes: &[[u8; 32]]) { + self.tx_router.lock().unwrap().ledger_externalized(hashes); + } + + /// Core dropped these transactions: stop sending them. + pub fn forget_local_txs(&self, hashes: &[[u8; 32]]) { + self.tx_router.lock().unwrap().remove(hashes); + } + + /// Core's list of validators that get transactions now (`None` = every + /// peer). Returns whether it changed; if so, call + /// [`Self::flush_local_txs`]. + pub fn set_tx_targets(&self, targets: Option>) -> bool { + self.tx_router.lock().unwrap().set_targets(targets) + } + + /// Send whatever the router says is owed after a change. + pub async fn flush_local_txs(&self) { + if let Err(e) = self.cmd_tx.send(OverlayCommand::FlushLocalTxs).await { + warn!( + "Overlay command channel closed, failed to send FlushLocalTxs: {}", + e + ); + } + } + + /// Core sent this node's validator identity. + pub async fn set_node_identity(&self, identity: NodeIdentity) { + if let Err(e) = self + .cmd_tx + .send(OverlayCommand::SetNodeIdentity(identity)) + .await + { + warn!( + "Overlay command channel closed, failed to send SetNodeIdentity: {}", + e + ); + } + } + + /// Number of transactions kept for sending (accepted from Core, not yet + /// externalized). + pub fn local_tx_count(&self) -> usize { + self.tx_router.lock().unwrap().len() + } + pub async fn fetch_txset(&self, hash: [u8; 32], slot: u32) { self.send_fetch_txset(hash, slot, false).await; } @@ -447,6 +632,15 @@ struct SharedState { pending_getdata: RwLock, /// TX buffer for responding to GETDATA requests tx_buffer: RwLock, + /// Where this node's own transactions go (shared with the App through + /// `OverlayHandle`). Never held across an await. + tx_router: Arc>>, + /// Validator identities: ours and unchecked claims. Never held across + /// an await. + identity: std::sync::Mutex, + /// Transaction bodies queued for a direct send, per peer (see + /// DIRECT_SEND_BATCH_INTERVAL). Never held across an await. + direct_out: std::sync::Mutex, /// Overlay metrics (shared with App for IPC reporting) metrics: Arc, } @@ -457,6 +651,7 @@ impl SharedState { mempool: MempoolHandle, control: Control, metrics: Arc, + tx_router: Arc>>, ) -> Self { Self { peer_streams: RwLock::new(HashMap::new()), @@ -484,6 +679,9 @@ impl SharedState { inv_tracker: RwLock::new(InvTracker::new()), pending_getdata: RwLock::new(PendingRequests::new()), tx_buffer: RwLock::new(TxBuffer::new()), + tx_router, + identity: std::sync::Mutex::new(IdentityState::default()), + direct_out: std::sync::Mutex::new(DirectOut::default()), metrics, } } @@ -587,11 +785,13 @@ pub fn create_overlay( // Unbounded channel for critical events (SCP, TxSet) - never drop let (event_tx, event_rx) = mpsc::unbounded_channel(); + let tx_router = Arc::new(std::sync::Mutex::new(TxRouter::new())); let state = Arc::new(SharedState::new( event_tx, mempool, control.clone(), metrics, + Arc::clone(&tx_router), )); let overlay = StellarOverlay { @@ -606,6 +806,8 @@ pub fn create_overlay( cmd_tx, txset_send_slots: Arc::new(Semaphore::new(MAX_OUTSTANDING_TXSET_SENDS)), txset_send_bytes: Arc::new(Semaphore::new(MAX_OUTSTANDING_TXSET_BYTES)), + tx_router, + local_peer_id: peer_id, }; Ok((handle, event_rx, overlay)) @@ -646,13 +848,22 @@ impl StellarOverlay { .control .accept(TXSET_PROTOCOL) .expect("TxSet protocol registered once"); + let node_id_incoming = self + .control + .accept(NODE_ID_PROTOCOL) + .expect("node id protocol registered once"); let state = self.state.clone(); + tokio::spawn(handle_inbound_node_id_streams( + node_id_incoming, + state.clone(), + )); tokio::spawn(handle_inbound_scp_streams(control_incoming, state.clone())); tokio::spawn(handle_inbound_tx_streams(tx_incoming, state.clone())); tokio::spawn(handle_inbound_txset_streams(txset_incoming, state.clone())); // Spawn INV/GETDATA housekeeping task tokio::spawn(inv_getdata_housekeeping_task(state.clone())); + tokio::spawn(direct_send_flush_task(state.clone())); loop { tokio::select! { @@ -694,6 +905,15 @@ impl StellarOverlay { OverlayCommand::PushTxSet { hash, data } => { self.push_txset(hash, data).await; } + OverlayCommand::SendLocalTx(plan) => { + self.send_local_tx_plan(plan).await; + } + OverlayCommand::FlushLocalTxs => { + flush_local_txs(&self.state).await; + } + OverlayCommand::SetNodeIdentity(identity) => { + self.set_node_identity(identity).await; + } OverlayCommand::Dial(addr) => { info!("Dialing peer at {}", addr); self.state.metrics.connection_pending.fetch_add(1, Ordering::Relaxed); @@ -827,6 +1047,16 @@ impl StellarOverlay { "Duplicate connection to {} (now {}), skipping stream setup", peer_id, num_established ); + // The peer may have seen its side of the earlier + // connection close and forgotten who this node is. + let identity = self.state.identity.lock().unwrap().ours.clone(); + if let Some(identity) = identity { + tokio::spawn(send_node_identity( + self.control.clone(), + peer_id, + identity, + )); + } } } @@ -877,6 +1107,26 @@ impl StellarOverlay { .lock() .unwrap() .forget_peer(&peer_id); + // The peer is no longer a reachable target, and whatever + // was sent to it may be lost. + { + let mut ids = self.state.identity.lock().unwrap(); + ids.unverified.remove(&peer_id); + ids.claims.remove(&peer_id); + } + // Whatever was queued for the peer is not sent; the + // router forgets its sends to the peer just below. + self.state.direct_out.lock().unwrap().queued.remove(&peer_id); + let identified = { + let mut router = self.state.tx_router.lock().unwrap(); + router.peer_disconnected(&peer_id); + router.identified_peer_count() + }; + self.state + .metrics + .node_id_identified + .store(identified as i64, Ordering::Relaxed); + flush_local_txs(&self.state).await; // Notify main loop to clean up any pending requests for this peer if let Err(e) = self.state.event_tx.send(OverlayEvent::PeerDisconnected { peer_id: peer_id.clone(), @@ -1014,11 +1264,11 @@ impl StellarOverlay { } } - /// Broadcast TX to all connected peers - /// Broadcast TX using INV/GETDATA protocol (bandwidth efficient) + /// Flood a transaction to all connected peers with the INV/GETDATA + /// protocol, unless it was seen before. (Core's own transactions go + /// through the router instead, see `send_local_tx_plan`.) async fn broadcast_tx(&mut self, tx: Arc) { let hash = *tx.hash(); - let fee_per_op = tx.fee_per_op(); // Dedup check { @@ -1033,34 +1283,57 @@ impl StellarOverlay { .memory_flood_known .store(seen.len() as i64, Ordering::Relaxed); } + advertise_txs_to_all(&self.state, &[tx]).await; + } - // Store TX in buffer for GETDATA responses + /// Carry out the router's plan for transactions Core accepted. + async fn send_local_tx_plan(&mut self, plan: SendPlan) { { - let mut buffer = self.state.tx_buffer.write().await; - buffer.insert(Arc::clone(&tx)); - } - - let streams = self.state.peer_streams.read().await; - let peers: Vec<_> = streams.keys().cloned().collect(); - drop(streams); - - if peers.is_empty() { - debug!("TX_INV: No peers to announce TX {:02x?}...", &hash[..4]); - return; + // A local transaction is known from now on: a copy from a peer + // is a duplicate. + let mut seen = self.state.tx_seen.write().await; + for tx in plan + .flood + .iter() + .chain(plan.direct.iter().flat_map(|(_, txs)| txs.iter())) + { + seen.put(*tx.hash(), ()); + } + self.state + .metrics + .memory_flood_known + .store(seen.len() as i64, Ordering::Relaxed); } + execute_send_plan(&self.state, plan).await; + } - debug!( - "TX_INV: Announcing TX {:02x?}... ({} bytes) to {} peers via INV", - &hash[..4], - tx.bytes().len(), - peers.len() + /// Core told us which validator this overlay belongs to: keep it, check + /// the claims that were waiting for the network id, and tell every peer. + async fn set_node_identity(&mut self, identity: NodeIdentity) { + info!( + "NODE_ID: This overlay belongs to validator {:02x?}...", + &identity.node_id[..4] ); + let waiting: Vec<(PeerId, (NodeId, [u8; 64]))> = { + let mut ids = self.state.identity.lock().unwrap(); + ids.ours = Some(identity.clone()); + ids.unverified.drain().collect() + }; self.state - .metrics - .flood_advertised - .fetch_add(peers.len() as u64, Ordering::Relaxed); - - queue_inv(&self.state, &peers, InvEntry { hash, fee_per_op }).await; + .tx_router + .lock() + .unwrap() + .set_local_node(identity.node_id); + for (peer, (node_id, signature)) in waiting { + accept_node_id_claim(&self.state, peer, node_id, signature).await; + } + let peers: Vec = self.state.peer_streams.read().await.keys().cloned().collect(); + for peer in peers { + let control = self.control.clone(); + let identity = identity.clone(); + tokio::spawn(async move { send_node_identity(control, peer, identity).await }); + } + flush_local_txs(&self.state).await; } /// Fetch TX set from a peer - preferring the peer who sent us the SCP message referencing it. @@ -1402,6 +1675,13 @@ async fn open_streams_to_peer(control: Control, state: Arc, peer_id } } + // Tell the peer which validator this overlay belongs to, if Core has + // said so already (otherwise `set_node_identity` does it later). + let identity = state.identity.lock().unwrap().ours.clone(); + if let Some(identity) = identity { + tokio::spawn(send_node_identity(state.control.clone(), peer_id, identity)); + } + // Request SCP state from newly connected peer info!("Peer {} streams opened, sending SCP state request", peer_id); let ledger_seq: u32 = 0; @@ -1650,6 +1930,411 @@ async fn write_framed(stream: &mut Stream, data: &[u8]) -> io::Result<()> { framed_io::write_frame_parts(stream, &[data]).await } +/// Advertise `txs` to every connected peer (the INV/GETDATA flood) and keep +/// them ready for the demands that follow. Returns false if there was no +/// peer to advertise them to. +async fn advertise_txs_to_all(state: &Arc, txs: &[Arc]) -> bool { + if txs.is_empty() { + return true; + } + { + let mut buffer = state.tx_buffer.write().await; + for tx in txs { + buffer.insert(Arc::clone(tx)); + } + } + let peers: Vec = state.peer_streams.read().await.keys().cloned().collect(); + if peers.is_empty() { + debug!("TX_INV: No peers to announce {} TXs to", txs.len()); + return false; + } + debug!( + "TX_INV: Announcing {} TXs to {} peers via INV", + txs.len(), + peers.len() + ); + state + .metrics + .flood_advertised + .fetch_add((peers.len() * txs.len()) as u64, Ordering::Relaxed); + for tx in txs { + let entry = InvEntry { + hash: *tx.hash(), + fee_per_op: tx.fee_per_op(), + }; + queue_inv(state, &peers, entry).await; + } + true +} + +/// Carry out a router plan: bodies straight to the target peers, adverts to +/// everyone for what has to be flooded. +async fn execute_send_plan(state: &Arc, plan: SendPlan) { + let SendPlan { direct, flood } = plan; + if !flood.is_empty() { + if advertise_txs_to_all(state, &flood).await { + state + .metrics + .flood_tx_local_flooded + .fetch_add(flood.len() as u64, Ordering::Relaxed); + } else { + // Nobody to tell yet: try again when the targets or the peers + // change. + let hashes: Vec<[u8; 32]> = flood.iter().map(|tx| *tx.hash()).collect(); + state.tx_router.lock().unwrap().mark_unflooded(&hashes); + } + } + // Queue the bodies per peer; `direct_send_flush_task` writes them a few + // milliseconds later, a full queue is written now. + let mut overflow: Vec<(PeerId, Vec<[u8; 32]>)> = Vec::new(); + let mut full: Vec = Vec::new(); + { + let mut out = state.direct_out.lock().unwrap(); + for (peer, txs) in direct { + let queue = out.queued.entry(peer).or_default(); + let room = DIRECT_SEND_MAX_QUEUED_TXS.saturating_sub(queue.len()); + if txs.len() > room { + overflow.push((peer, txs[room..].iter().map(|tx| *tx.hash()).collect())); + } + queue.extend(txs.into_iter().take(room)); + if queue.len() >= DIRECT_SEND_BATCH_MAX_TXS { + full.push(peer); + } + } + } + if !overflow.is_empty() { + // The peer is not reading: these were not sent. The router offers + // them again at a later plan. + let mut router = state.tx_router.lock().unwrap(); + for (peer, hashes) in &overflow { + router.mark_unsent(peer, hashes); + } + } + for peer in full { + start_direct_sender(state, peer); + } +} + +/// Starts the sender task of `peer` unless one is running: it writes what is +/// queued for the peer, batch after batch, and ends when the queue is empty. +/// One task per peer, so a peer that stops reading holds one task and a +/// bounded queue, and a slow peer does not delay another one or the caller. +fn start_direct_sender(state: &Arc, peer: PeerId) { + { + let mut out = state.direct_out.lock().unwrap(); + if out.queued.get(&peer).map_or(true, |q| q.is_empty()) || !out.sending.insert(peer) { + return; + } + } + let state = Arc::clone(state); + tokio::spawn(async move { + loop { + let txs = { + let mut out = state.direct_out.lock().unwrap(); + match out.queued.remove(&peer) { + Some(txs) if !txs.is_empty() => txs, + _ => { + out.sending.remove(&peer); + return; + } + } + }; + send_tx_bodies(&state, peer, txs).await; + } + }); +} + +/// Writes the queued direct sends every DIRECT_SEND_BATCH_INTERVAL, and asks +/// the router every DIRECT_REPLAN_INTERVAL whether anything is owed. +async fn direct_send_flush_task(state: Arc) { + let mut interval = tokio::time::interval(DIRECT_SEND_BATCH_INTERVAL); + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + let mut last_replan = Instant::now(); + loop { + interval.tick().await; + if last_replan.elapsed() >= DIRECT_REPLAN_INTERVAL { + last_replan = Instant::now(); + flush_local_txs(&state).await; + } + let peers: Vec = { + let out = state.direct_out.lock().unwrap(); + out.queued + .iter() + .filter(|(peer, queue)| !queue.is_empty() && !out.sending.contains(*peer)) + .map(|(peer, _)| *peer) + .collect() + }; + for peer in peers { + start_direct_sender(&state, peer); + } + } +} + +/// Ask the router what is owed after the targets or the identified peers +/// changed, and send it. +async fn flush_local_txs(state: &Arc) { + let plan = state.tx_router.lock().unwrap().plan_all(Instant::now()); + if plan.is_empty() { + return; + } + debug!( + "TX_DIRECT_FLUSH: {} peers get bodies, {} TXs flooded", + plan.direct.len(), + plan.flood.len() + ); + execute_send_plan(state, plan).await; +} + +/// Send transaction bodies straight to `peer`, without adverts, as batches +/// of complete TX frames (the framing of a GETDATA response). If a write +/// fails, the router is told that the remaining ones were not delivered. +async fn send_tx_bodies(state: &SharedState, peer: PeerId, txs: Vec>) { + let mut batch = Vec::new(); + let mut next = 0; + while next < txs.len() { + batch.clear(); + let first = next; + let mut payload_bytes = 0; + while next < txs.len() { + let len = tx_frame_len(&txs[next]); + if next > first && batch.len() + len > TX_RESPONSE_BATCH_BYTES { + break; + } + let parts = txs[next].flood_frame_parts(); + let payload = parts.iter().map(|part| part.len()).sum::(); + let Ok(prefix) = u32::try_from(payload) else { + warn!("TX_DIRECT: transaction too large to frame, skipping"); + next += 1; + continue; + }; + batch.extend_from_slice(&prefix.to_be_bytes()); + for part in parts { + batch.extend_from_slice(part); + } + payload_bytes += payload; + next += 1; + } + let count = next - first; + let frames = framed_io::Frames::Batch(&batch); + let result = send_peer_frames(state, peer, StreamType::Tx, frames).await; + if record_tx_response_write(state, peer, result, count, payload_bytes) { + state + .metrics + .flood_tx_direct_sent + .fetch_add(count as u64, Ordering::Relaxed); + state + .metrics + .flood_tx_direct_sent_bytes + .fetch_add(payload_bytes as u64, Ordering::Relaxed); + } else { + let unsent: Vec<[u8; 32]> = txs[first..].iter().map(|tx| *tx.hash()).collect(); + state + .tx_router + .lock() + .unwrap() + .mark_unsent(&peer, &unsent); + return; + } + } +} + +/// Tell `peer` which validator this overlay belongs to, and wait for it to +/// acknowledge the frame. Retried a few times: the stream library drops an +/// inbound stream when several arrive faster than they are accepted (every +/// peer identifies itself at the same moment when a network starts), and a +/// dropped stream is not always an error on the writing side. +async fn send_node_identity(control: Control, peer: PeerId, identity: NodeIdentity) { + const ATTEMPTS: u32 = 12; + const MAX_DELAY: Duration = Duration::from_secs(5); + let mut delay = Duration::from_millis(100); + for attempt in 1..=ATTEMPTS { + match try_send_node_identity(control.clone(), peer, &identity).await { + Ok(()) => return, + Err(e) if e.kind() == io::ErrorKind::Unsupported => { + // A peer without this protocol never identifies itself + // either; transactions are flooded when it is a target. + debug!("NODE_ID: {} does not take node ids: {}", peer, e); + return; + } + Err(e) => { + debug!( + "NODE_ID: Sending the node id to {} failed (attempt {}): {}", + peer, attempt, e + ); + } + } + tokio::time::sleep(delay).await; + delay = (delay * 2).min(MAX_DELAY); + } + // About half a minute of trying. If the connection is still up, this + // node floods its transactions whenever that validator is a target. + warn!( + "NODE_ID: Could not tell {} which validator this overlay belongs to", + peer + ); +} + +/// One attempt of [`send_node_identity`]. +async fn try_send_node_identity( + mut control: Control, + peer: PeerId, + identity: &NodeIdentity, +) -> io::Result<()> { + use futures::AsyncWriteExt; + let mut stream = control + .open_stream(peer, NODE_ID_PROTOCOL) + .await + .map_err(|e| match e { + libp2p_stream::OpenStreamError::UnsupportedProtocol(_) => { + io::Error::new(io::ErrorKind::Unsupported, e) + } + other => io::Error::new(io::ErrorKind::NotConnected, other), + })?; + let mut frame = [0u8; NODE_ID_FRAME_LEN]; + frame[..32].copy_from_slice(&identity.node_id); + frame[32..].copy_from_slice(&identity.signature); + write_framed(&mut stream, &frame).await?; + let mut ack = [0u8; 1]; + tokio::time::timeout(NODE_ID_READ_TIMEOUT, stream.read_exact(&mut ack)) + .await + .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "no acknowledgement"))??; + let _ = stream.close().await; + if ack[0] != NODE_ID_ACK_TAKEN { + // The peer read the frame but could not use it yet (it had not + // registered this connection): send it again. + return Err(io::Error::new(io::ErrorKind::WouldBlock, "not taken yet")); + } + Ok(()) +} + +/// Peers telling us which validator they belong to. +async fn handle_inbound_node_id_streams(mut incoming: IncomingStreams, state: Arc) { + while let Some((peer_id, mut stream)) = incoming.next().await { + let state = state.clone(); + tokio::spawn(async move { + let read = tokio::time::timeout( + NODE_ID_READ_TIMEOUT, + read_framed_with_limit(&mut stream, NODE_ID_FRAME_LEN), + ); + let frame = match read.await { + Ok(Ok(frame)) if frame.len() == NODE_ID_FRAME_LEN => frame, + Ok(Ok(frame)) => { + debug!( + "NODE_ID: Ignoring a node id frame of {} bytes from {}", + frame.len(), + peer_id + ); + return; + } + Ok(Err(e)) => { + debug!("NODE_ID: Node id stream from {} failed: {}", peer_id, e); + return; + } + Err(_) => { + debug!("NODE_ID: Node id stream from {} sent nothing", peer_id); + return; + } + }; + let mut node_id = [0u8; 32]; + node_id.copy_from_slice(&frame[..32]); + let mut signature = [0u8; 64]; + signature.copy_from_slice(&frame[32..]); + let taken = accept_node_id_claim(&state, peer_id, node_id, signature).await; + { + use futures::AsyncWriteExt; + let ack = if taken { + NODE_ID_ACK_TAKEN + } else { + NODE_ID_ACK_RETRY + }; + let _ = stream.write_all(&[ack]).await; + let _ = stream.flush().await; + } + }); + } +} + +/// A peer claims to be validator `node_id`. Accept it if the signature +/// binds that validator to the peer id of this connection; keep it for later +/// if Core has not told us the network id yet. +/// Returns false if the claim could not be used yet and should be repeated. +async fn accept_node_id_claim( + state: &Arc, + peer: PeerId, + node_id: NodeId, + signature: [u8; 64], +) -> bool { + // Nothing to do for a claim that is already accepted (a peer repeats it + // on every connection it establishes). + if state.tx_router.lock().unwrap().node_of(&peer) == Some(&node_id) { + return true; + } + // Only a connected peer can be a target. The peer's stream can get here + // before this side has registered the connection: the sender repeats the + // claim. + if !state.peer_streams.read().await.contains_key(&peer) { + return false; + } + let network_id = { + let mut ids = state.identity.lock().unwrap(); + let known = ids.ours.as_ref().map(|ours| ours.network_id); + match known { + Some(network_id) => { + // A signature check, a log line and a pass over the pending + // transactions per claim: bound what one connection can ask + // for. (A waiting claim is counted when it is checked.) + let claims = ids.claims.entry(peer).or_insert(0); + if *claims >= MAX_NODE_ID_CLAIMS_PER_PEER { + return true; + } + *claims += 1; + network_id + } + None => { + ids.unverified.insert(peer, (node_id, signature)); + return true; + } + } + }; + if !verify_node_id_binding(&network_id, &peer, &node_id, &signature) { + state + .metrics + .node_id_rejected + .fetch_add(1, Ordering::Relaxed); + warn!( + "NODE_ID_REJECT: Peer {} claims validator {:02x?}... with a signature that does not bind it to this connection", + peer, + &node_id[..4] + ); + return true; + } + info!( + "NODE_ID: Peer {} is validator {:02x?}...", + peer, + &node_id[..4] + ); + let mut identified = { + let mut router = state.tx_router.lock().unwrap(); + router.peer_identified(peer, node_id); + router.identified_peer_count() + }; + // The connection may have closed between the check above and now; its + // close handler has then already run and would not undo this. + if !state.peer_streams.read().await.contains_key(&peer) { + let mut router = state.tx_router.lock().unwrap(); + router.peer_disconnected(&peer); + identified = router.identified_peer_count(); + state.identity.lock().unwrap().claims.remove(&peer); + } + state + .metrics + .node_id_identified + .store(identified as i64, Ordering::Relaxed); + // The peer may be a target that could not be reached until now. + flush_local_txs(state).await; + true +} + /// Flush INV batch for a specific peer async fn flush_inv_batch_to_peer(state: &Arc, peer: PeerId) { let batch = { @@ -1960,6 +2645,51 @@ async fn handle_inv_batch(state: &Arc, peer_id: &PeerId, batch: Inv peer_id ); + // A transaction this node got as a direct send (and so did not relay) + // is now being advertised: its origin has fallen back to flooding, which + // has to pass through this node too. Advertise it to the other peers, + // once. + let join_flood: Vec> = { + let mut router = state.tx_router.lock().unwrap(); + if router.has_direct() { + batch + .entries + .iter() + .filter_map(|entry| router.join_flood(&entry.hash)) + .collect() + } else { + Vec::new() + } + }; + if !join_flood.is_empty() { + // Ready for the demands that follow the adverts. + { + let mut buffer = state.tx_buffer.write().await; + for tx in &join_flood { + buffer.insert(Arc::clone(tx)); + } + } + let peers: Vec = state + .peer_streams + .read() + .await + .keys() + .filter(|p| *p != peer_id) + .cloned() + .collect(); + debug!( + "TX_RELAY: Joining the flood of {} TXs received directly earlier", + join_flood.len() + ); + for tx in join_flood { + let entry = InvEntry { + hash: *tx.hash(), + fee_per_op: tx.fee_per_op(), + }; + queue_inv(state, &peers, entry).await; + } + } + // Each structure is locked once per batch rather than once per entry, and // the two locks are never held together. Entries keep their batch order. // Skip TXs we already have; they are not recorded as sources. @@ -2093,7 +2823,14 @@ async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) if bytes < TX_RESPONSE_BATCH_BYTES { let mut buffer = state.tx_buffer.write().await; for hash in hashes.by_ref() { - let Some(tx) = buffer.get(&hash) else { + // The buffer keeps a flooded transaction for a short time + // only. One this node accepted itself, or received directly, + // is kept until it is externalized, and may be demanded long + // after it arrived (a flood that starts later). + let found = buffer + .get(&hash) + .or_else(|| state.tx_router.lock().unwrap().body(&hash)); + let Some(tx) = found else { state .metrics .flood_unfulfilled_unknown @@ -2234,9 +2971,22 @@ async fn handle_tx_response(state: &Arc, peer_id: &PeerId, tx: Arc< .metrics .flood_unique_recv .fetch_add(tx_len, Ordering::Relaxed); + // A first copy of a transaction that one of the last ledgers already + // has. It is in the mempool now, and stays until a build finds it + // invalid (or, where ledgers are not applied, includes it again). + if state.tx_router.lock().unwrap().was_finalized(&hash) { + state.metrics.flood_tx_late_copy.fetch_add(1, Ordering::Relaxed); + debug!( + "TX_LATE_COPY: TX {:02x?}... from {} arrived after it externalized", + &hash[..4], + peer_id + ); + } - // Remove from pending requests and measure pull latency - { + // Remove from pending requests and measure pull latency. A body this + // node demanded arrived over the flood; one it did not ask for was sent + // directly by a node that is sending to its targets only. + let solicited = { let mut pending = state.pending_getdata.write().await; if let Some(req) = pending.remove(&hash) { let pull_us = req.first_sent_at.elapsed().as_micros() as u64; @@ -2248,8 +2998,11 @@ async fn handle_tx_response(state: &Arc, peer_id: &PeerId, tx: Arc< .metrics .flood_tx_pull_latency_count .fetch_add(1, Ordering::Relaxed); + true + } else { + false } - } + }; // Store in buffer for responding to others' GETDATA { @@ -2264,6 +3017,24 @@ async fn handle_tx_response(state: &Arc, peer_id: &PeerId, tx: Arc< peer_id ); + if !solicited { + // A direct send: the sender delivers it to every target itself, so + // it is kept for this node's own tx sets and not relayed. Remember + // it in case its origin floods it later (see `handle_inv_batch`). + state + .metrics + .flood_tx_direct_recv + .fetch_add(1, Ordering::Relaxed); + state.tx_router.lock().unwrap().note_direct(Arc::clone(&tx)); + debug!( + "TX_DIRECT_RECV: TX {:02x?}... from {} kept, not relayed", + &hash[..4], + peer_id + ); + record_recv_transaction(state, recv_start); + return; + } + // RELAY: Announce to other peers via INV let peers_to_announce: Vec = { let streams = state.peer_streams.read().await; @@ -2292,7 +3063,11 @@ async fn handle_tx_response(state: &Arc, peer_id: &PeerId, tx: Arc< queue_inv(state, &peers_to_announce, InvEntry { hash, fee_per_op }).await; } - // Record recv-transaction timing + record_recv_transaction(state, recv_start); +} + +/// Record recv-transaction timing +fn record_recv_transaction(state: &SharedState, recv_start: std::time::Instant) { let elapsed_us = recv_start.elapsed().as_micros() as u64; state .metrics @@ -2646,6 +3421,9 @@ mod quic_window_experiment; #[cfg(test)] mod txset_push_tests; +#[cfg(test)] +mod tx_direct_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 361001c85..b9d201f9b 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -514,6 +514,24 @@ async fn received_tx_is_announced_to_every_peer_except_known_sources() { .await .record_source(*tx.hash(), advertiser); + // A body nobody here demanded was sent directly by a node that delivers + // to its targets itself: it is kept, and announced to nobody. + let direct = test_tx(2); + handle_tx_response(&state, &source, Arc::clone(&direct)).await; + assert!(state.tx_seen.read().await.contains(direct.hash())); + { + let mut batcher = state.inv_batcher.write().await; + for peer in &others[1..] { + assert!(batcher.flush(peer).is_none(), "direct send was relayed"); + } + } + + // A body this node demanded (the flood) is relayed. + state + .pending_getdata + .write() + .await + .insert(*tx.hash(), source); handle_tx_response(&state, &source, Arc::clone(&tx)).await; let mut batcher = state.inv_batcher.write().await; @@ -562,7 +580,15 @@ async fn inv_batches_completed_by_relay_or_local_broadcast_reach_the_peer() { } let tx = test_tx(i64::from(round) + 1); if round == 0 { - handle_tx_response(&sender.state, &PeerId::random(), tx).await; + // A relay: the body answers this node's own demand. + let source = PeerId::random(); + sender + .state + .pending_getdata + .write() + .await + .insert(*tx.hash(), source); + handle_tx_response(&sender.state, &source, tx).await; } else { sender.handle.broadcast_tx(tx).await; } diff --git a/overlay/src/libp2p_overlay/tx_direct_tests.rs b/overlay/src/libp2p_overlay/tx_direct_tests.rs new file mode 100644 index 000000000..88de735d3 --- /dev/null +++ b/overlay/src/libp2p_overlay/tx_direct_tests.rs @@ -0,0 +1,426 @@ +//! Tests for sending transactions to the target validators only, and for +//! the identity binding that ties a peer connection to a validator. + +use super::*; +use libp2p::identity::ed25519; + +const NETWORK_ID: [u8; 32] = [7; 32]; + +/// One overlay node on loopback with a validator key of its own. +struct Node { + handle: OverlayHandle, + state: Arc, + metrics: Arc, + peer_id: PeerId, + port: u16, + validator: ed25519::Keypair, + admissions: mpsc::UnboundedReceiver, + /// Transactions read from `admissions` so far. + admitted: HashSet<[u8; 32]>, + _events: mpsc::UnboundedReceiver, +} + +impl Node { + fn node_id(&self) -> NodeId { + self.validator.public().to_bytes() + } + + /// What Core sends in `SetNodeIdentity`: this node's validator key + /// signing the binding to this overlay's peer id. + fn identity(&self) -> NodeIdentity { + sign_identity(&self.validator, &NETWORK_ID, &self.peer_id) + } + + async fn identify(&self) { + self.handle.set_node_identity(self.identity()).await; + } + + fn identified_peers(&self) -> usize { + self.state.tx_router.lock().unwrap().identified_peer_count() + } + + /// Core accepted `tx`. + async fn submit(&self, tx: &Arc) { + let plan = self.handle.add_local_tx(Arc::clone(tx)); + self.handle.send_local_tx(plan).await; + } + + async fn set_targets(&self, targets: Option>) { + if self.handle.set_tx_targets(targets) { + self.handle.flush_local_txs().await; + } + } + + /// Transactions newly admitted to this node's mempool within `window`. + async fn admitted_within(&mut self, window: Duration) -> Vec<[u8; 32]> { + let deadline = tokio::time::Instant::now() + window; + let mut hashes = Vec::new(); + while let Ok(Some(command)) = tokio::time::timeout_at(deadline, self.admissions.recv()).await + { + if let CoreCommand::SubmitTx { tx, .. } = command { + self.admitted.insert(*tx.hash()); + hashes.push(*tx.hash()); + } + } + hashes + } + + /// Wait until `tx` has been admitted to this node's mempool (in any + /// order relative to other transactions). + async fn expect_admitted(&mut self, tx: &Arc) { + let deadline = tokio::time::Instant::now() + Duration::from_secs(10); + while !self.admitted.contains(tx.hash()) { + match tokio::time::timeout_at(deadline, self.admissions.recv()).await { + Ok(Some(CoreCommand::SubmitTx { tx: got, .. })) => { + if got.hash() == tx.hash() { + assert_eq!(got.bytes(), tx.bytes()); + } + self.admitted.insert(*got.hash()); + } + Ok(Some(_)) => {} + _ => panic!("transaction {:02x?} was not admitted", &tx.hash()[..4]), + } + } + } + + fn counter(&self, pick: impl Fn(&OverlayMetrics) -> &AtomicU64) -> u64 { + pick(&self.metrics).load(Ordering::Relaxed) + } +} + +fn sign_identity(validator: &ed25519::Keypair, network_id: &[u8; 32], peer: &PeerId) -> NodeIdentity { + let signature: [u8; 64] = validator + .sign(&node_id_binding(network_id, peer)) + .try_into() + .unwrap(); + NodeIdentity { + network_id: *network_id, + node_id: validator.public().to_bytes(), + signature, + } +} + +async fn start_node(port: u16) -> Node { + let keypair = Keypair::generate_ed25519(); + let peer_id = keypair.public().to_peer_id(); + let metrics = Arc::new(OverlayMetrics::new()); + let (handle, events, admissions, overlay) = + create_test_overlay(keypair, Arc::clone(&metrics)).unwrap(); + let state = Arc::clone(&overlay.state); + tokio::spawn(async move { overlay.run("127.0.0.1", port).await }); + tokio::time::sleep(Duration::from_millis(100)).await; + Node { + handle, + state, + metrics, + peer_id, + port, + validator: ed25519::Keypair::generate(), + admissions, + admitted: HashSet::new(), + _events: events, + } +} + +/// Dial `listener` from `dialer` and wait until both sides have streams. +async fn connect(dialer: &Node, listener: &Node) { + let addr: Multiaddr = format!("/ip4/127.0.0.1/udp/{}/quic-v1", listener.port) + .parse() + .unwrap(); + dialer.handle.dial(addr).await; + let deadline = Instant::now() + Duration::from_secs(10); + loop { + let a = dialer + .state + .peer_streams + .read() + .await + .contains_key(&listener.peer_id); + let b = listener + .state + .peer_streams + .read() + .await + .contains_key(&dialer.peer_id); + if a && b { + break; + } + assert!(Instant::now() < deadline, "peers did not connect"); + tokio::time::sleep(Duration::from_millis(20)).await; + } + // Let the SCP state exchange of a new connection settle. + tokio::time::sleep(Duration::from_millis(200)).await; +} + +async fn wait_until(what: &str, condition: impl Fn() -> bool) { + let deadline = Instant::now() + Duration::from_secs(10); + while !condition() { + assert!(Instant::now() < deadline, "timed out waiting for {what}"); + tokio::time::sleep(Duration::from_millis(20)).await; + } +} + +/// The key scenario: in a full mesh a transaction reaches the two targets +/// and nobody else, without a single advert; a pending transaction follows +/// the targets; an externalized one is not sent again. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_transaction_goes_to_the_targets_only_and_follows_them() { + let origin = start_node(19951).await; + let mut leader1 = start_node(19952).await; + let mut leader2 = start_node(19953).await; + let mut other = start_node(19954).await; + connect(&origin, &leader1).await; + connect(&origin, &leader2).await; + connect(&origin, &other).await; + connect(&leader1, &leader2).await; + connect(&leader1, &other).await; + connect(&leader2, &other).await; + + // Identities arrive one node at a time, so some claims reach a node + // before its own identity (and with it the network id) does. + for node in [&leader1, &leader2, &other, &origin] { + node.identify().await; + } + for node in [&origin, &leader1, &leader2, &other] { + wait_until("every peer to be identified", || { + node.identified_peers() == 3 + }) + .await; + } + { + let router = origin.state.tx_router.lock().unwrap(); + assert_eq!(router.node_of(&leader1.peer_id), Some(&leader1.node_id())); + assert_eq!(router.node_of(&other.peer_id), Some(&other.node_id())); + } + + origin + .set_targets(Some(vec![leader1.node_id(), leader2.node_id()])) + .await; + let included = test_tx(1); + let pending = test_tx(2); + origin.submit(&included).await; + origin.submit(&pending).await; + + leader1.expect_admitted(&included).await; + leader1.expect_admitted(&pending).await; + leader2.expect_admitted(&included).await; + leader2.expect_admitted(&pending).await; + // Nobody else gets them: the origin sent no advert, and the targets do + // not relay what was sent to them directly. + assert!(other.admitted_within(Duration::from_millis(700)).await.is_empty()); + assert_eq!(origin.counter(|m| &m.flood_advertised), 0); + assert_eq!(origin.counter(|m| &m.flood_tx_direct_sent), 4); + assert_eq!(origin.counter(|m| &m.flood_tx_local_flooded), 0); + for leader in [&leader1, &leader2] { + assert_eq!(leader.counter(|m| &m.flood_tx_direct_recv), 2); + assert_eq!(leader.counter(|m| &m.flood_advertised), 0); + assert_eq!(leader.counter(|m| &m.flood_demanded), 0); + } + + // `included` externalizes; the next ledger's leaders are `other` and + // `leader1`. `pending` is not new any more and may be in the set the + // old targets are voting on: it does not move yet. + origin.handle.ledger_externalized(&[*included.hash()]); + assert_eq!(origin.handle.local_tx_count(), 1); + origin + .set_targets(Some(vec![other.node_id(), leader1.node_id()])) + .await; + assert!(other.admitted_within(Duration::from_millis(300)).await.is_empty()); + assert_eq!(origin.counter(|m| &m.flood_tx_direct_sent), 4); + // The next two ledgers do not have it either: now it moves, and only + // to the new target. + origin.handle.ledger_externalized(&[]); + origin.handle.ledger_externalized(&[]); + origin.handle.flush_local_txs().await; + other.expect_admitted(&pending).await; + assert!(other.admitted_within(Duration::from_millis(500)).await.is_empty()); + assert_eq!(origin.counter(|m| &m.flood_tx_direct_sent), 5); + assert_eq!(leader1.counter(|m| &m.flood_tx_direct_recv), 2); + assert_eq!(origin.counter(|m| &m.flood_advertised), 0); + + // A copy that reaches a node after its transaction externalized there is + // admitted all the same (the mempool does not know), and counted. + let late = test_tx(3); + other.handle.ledger_externalized(&[*late.hash()]); + assert_eq!(other.counter(|m| &m.flood_tx_late_copy), 0); + origin.submit(&late).await; + other.expect_admitted(&late).await; + wait_until("the late copy to be counted", || { + other.counter(|m| &m.flood_tx_late_copy) == 1 + }) + .await; + assert_eq!(leader1.counter(|m| &m.flood_tx_late_copy), 0); +} + +/// Without a target list, and when a target is not an identified peer, a +/// transaction is flooded as before and reaches every node. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn without_reachable_targets_a_transaction_is_flooded() { + let origin = start_node(19956).await; + let mut relay = start_node(19957).await; + let mut far = start_node(19958).await; + // A chain: the origin is not connected to `far`. + connect(&origin, &relay).await; + connect(&relay, &far).await; + for node in [&origin, &relay, &far] { + node.identify().await; + } + wait_until("the origin to identify its peer", || { + origin.identified_peers() == 1 + }) + .await; + + // No targets yet: flood. + let first = test_tx(1); + origin.submit(&first).await; + relay.expect_admitted(&first).await; + far.expect_admitted(&first).await; + assert_eq!(origin.counter(|m| &m.flood_tx_local_flooded), 1); + assert_eq!(origin.counter(|m| &m.flood_tx_direct_sent), 0); + + // A target the origin has no connection to: flood, through the relay. + origin + .set_targets(Some(vec![relay.node_id(), far.node_id()])) + .await; + let second = test_tx(2); + origin.submit(&second).await; + relay.expect_admitted(&second).await; + far.expect_admitted(&second).await; + assert_eq!(origin.counter(|m| &m.flood_tx_local_flooded), 2); + assert_eq!(origin.counter(|m| &m.flood_tx_direct_sent), 0); +} + +/// A node that got a transaction directly does not relay it, but joins in +/// when the origin later has to flood it (a new target it cannot reach). +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_directly_received_transaction_joins_a_later_flood() { + let origin = start_node(19961).await; + let mut relay = start_node(19962).await; + let mut far = start_node(19963).await; + connect(&origin, &relay).await; + connect(&relay, &far).await; + for node in [&origin, &relay, &far] { + node.identify().await; + } + wait_until("the origin to identify its peer", || { + origin.identified_peers() == 1 + }) + .await; + + origin.set_targets(Some(vec![relay.node_id()])).await; + let tx = test_tx(1); + origin.submit(&tx).await; + relay.expect_admitted(&tx).await; + assert!(far.admitted_within(Duration::from_millis(700)).await.is_empty()); + assert_eq!(relay.counter(|m| &m.flood_tx_direct_recv), 1); + + // Time passes: the transaction is long gone from the short-lived buffers + // that serve a flood's demands. + *origin.state.tx_buffer.write().await = TxBuffer::new(); + *relay.state.tx_buffer.write().await = TxBuffer::new(); + + // The next leader is `far`, which the origin cannot reach: it floods + // the pending transaction, and the relay, which already has it, passes + // the advert on and serves the body. + origin.set_targets(Some(vec![far.node_id()])).await; + far.expect_admitted(&tx).await; + assert_eq!(origin.counter(|m| &m.flood_tx_local_flooded), 1); +} + +/// An identity claim only counts if the validator's key signed it for this +/// very connection. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_forged_identity_claim_is_rejected() { + let honest = start_node(19966).await; + let mut forger = start_node(19967).await; + let mut replayer = start_node(19968).await; + let victim = ed25519::Keypair::generate(); + connect(&forger, &honest).await; + connect(&replayer, &honest).await; + honest.identify().await; + + // Claims the victim's node id, signed with the forger's own key. + let mut forged = sign_identity(&forger.validator, &NETWORK_ID, &forger.peer_id); + forged.node_id = victim.public().to_bytes(); + forger.handle.set_node_identity(forged).await; + // Replays a binding the victim signed for another peer id. + let replayed = sign_identity(&victim, &NETWORK_ID, &forger.peer_id); + replayer.handle.set_node_identity(replayed).await; + + wait_until("both claims to be rejected", || { + honest.counter(|m| &m.node_id_rejected) == 2 + }) + .await; + assert_eq!(honest.identified_peers(), 0); + + // A binding for another network does not count either. + let other_network = sign_identity(&victim, &[8; 32], &replayer.peer_id); + replayer + .handle + .set_node_identity(NodeIdentity { + network_id: NETWORK_ID, + ..other_network + }) + .await; + wait_until("the third claim to be rejected", || { + honest.counter(|m| &m.node_id_rejected) == 3 + }) + .await; + assert_eq!(honest.identified_peers(), 0); + + // A peer cannot keep this node busy with claims: beyond a few per + // connection they are not even checked. + for _ in 0..(MAX_NODE_ID_CLAIMS_PER_PEER + 4) { + let forged = sign_identity(&forger.validator, &NETWORK_ID, &replayer.peer_id); + forger.handle.set_node_identity(forged).await; + } + tokio::time::sleep(Duration::from_millis(700)).await; + assert_eq!( + honest.counter(|m| &m.node_id_rejected), + 2 + u64::from(MAX_NODE_ID_CLAIMS_PER_PEER), + "the three before (one of them the forger's), and the forger's up to its limit" + ); + assert_eq!(honest.identified_peers(), 0); + + // With the victim as the target, nothing is sent directly to either + // impostor: the transaction is flooded instead. + honest + .set_targets(Some(vec![victim.public().to_bytes()])) + .await; + let tx = test_tx(1); + honest.submit(&tx).await; + forger.expect_admitted(&tx).await; + replayer.expect_admitted(&tx).await; + assert_eq!(honest.counter(|m| &m.flood_tx_direct_sent), 0); + assert_eq!(honest.counter(|m| &m.flood_tx_local_flooded), 1); +} + +#[test] +fn the_binding_depends_on_network_and_peer() { + let peer_a = Keypair::generate_ed25519().public().to_peer_id(); + let peer_b = Keypair::generate_ed25519().public().to_peer_id(); + let validator = ed25519::Keypair::generate(); + let identity = sign_identity(&validator, &NETWORK_ID, &peer_a); + assert!(verify_node_id_binding( + &NETWORK_ID, + &peer_a, + &identity.node_id, + &identity.signature + )); + assert!(!verify_node_id_binding( + &NETWORK_ID, + &peer_b, + &identity.node_id, + &identity.signature + )); + assert!(!verify_node_id_binding( + &[8; 32], + &peer_a, + &identity.node_id, + &identity.signature + )); + assert_ne!( + node_id_binding(&NETWORK_ID, &peer_a), + node_id_binding(&NETWORK_ID, &peer_b) + ); +} diff --git a/overlay/src/main.rs b/overlay/src/main.rs index 55d4dc1a2..7b542a118 100644 --- a/overlay/src/main.rs +++ b/overlay/src/main.rs @@ -26,7 +26,8 @@ use stellar_overlay::flood::{ use stellar_overlay::integrated::{Overlay, OverlayHandle}; use stellar_overlay::ipc::{CoreIpc, Message, MessageType}; use stellar_overlay::libp2p_overlay::{ - create_overlay, OverlayEvent as LibP2pOverlayEvent, OverlayHandle as LibP2pOverlayHandle, + create_overlay, NodeIdentity, OverlayEvent as LibP2pOverlayEvent, + OverlayHandle as LibP2pOverlayHandle, }; use stellar_overlay::metrics::OverlayMetrics; use stellar_overlay::wire::ValidatedTx; @@ -560,6 +561,17 @@ impl App { async fn run(mut self) { info!("Overlay started, processing Core messages"); + // Ask Core to bind this overlay's peer id to the validator's key; + // it answers with `SetNodeIdentity`. + let peer_id = self.libp2p_handle.local_peer_id().to_bytes(); + if let Err(e) = self + .core_ipc + .sender + .send(Message::new(MessageType::OverlayIdentity, peer_id)) + { + error!("Failed to send the overlay identity to Core: {}", e); + } + // Safety-net reconnect timer: re-dial all configured peers every 30s. // Uses PeerId-based dials for known peers (libp2p skips if already connected). // Falls back to address-based dials for peers we haven't connected to yet. @@ -1422,13 +1434,62 @@ impl App { // Add to mempool self.overlay_handle.submit_tx(Arc::clone(&tx)); - // Broadcast TX via libp2p QUIC (dedicated stream) + // Core accepted this transaction from a client: this node + // keeps it and sends it until it is externalized, to the + // targets Core named or, without targets, to every peer. + // The router is updated here, in the order of Core's + // messages; only the sending is left to a task. + let plan = self.libp2p_handle.add_local_tx(tx); + let handle = self.libp2p_handle.clone(); + tokio::spawn(async move { + handle.send_local_tx(plan).await; + }); + } + + MessageType::SetNodeIdentity => { + // Payload: [networkID:32][nodeID:32][signature:64] + if msg.payload.len() != 128 { + warn!( + "SetNodeIdentity payload has {} bytes, expected 128", + msg.payload.len() + ); + return true; + } + let mut identity = NodeIdentity { + network_id: [0; 32], + node_id: [0; 32], + signature: [0; 64], + }; + identity.network_id.copy_from_slice(&msg.payload[0..32]); + identity.node_id.copy_from_slice(&msg.payload[32..64]); + identity.signature.copy_from_slice(&msg.payload[64..128]); let handle = self.libp2p_handle.clone(); tokio::spawn(async move { - handle.broadcast_tx(tx).await; + handle.set_node_identity(identity).await; }); } + MessageType::SetTxTargets => { + // Payload: [everyone:1][count:4][nodeID:32]... + let Some(targets) = parse_tx_targets(&msg.payload) else { + warn!( + "SetTxTargets payload malformed ({} bytes)", + msg.payload.len() + ); + return true; + }; + match &targets { + Some(nodes) => debug!("TX_TARGETS: {} validators get transactions", nodes.len()), + None => debug!("TX_TARGETS: every peer gets transactions"), + } + if self.libp2p_handle.set_tx_targets(targets) { + let handle = self.libp2p_handle.clone(); + tokio::spawn(async move { + handle.flush_local_txs().await; + }); + } + } + MessageType::RequestScpState => { // Core is asking us to request SCP state from peers // Payload is ledger sequence (u32, 4 bytes) @@ -1520,6 +1581,21 @@ impl App { } } + // Nothing is left to send for them. Core uses this + // message with a zero set hash to drop transactions for + // its own reasons; only a real set is a ledger. + if tx_set_hash == [0u8; 32] { + self.libp2p_handle.forget_local_txs(&tx_hashes); + } else { + // Transactions this ledger did not have may now be + // due at targets they were held back from. + self.libp2p_handle.ledger_externalized(&tx_hashes); + let handle = self.libp2p_handle.clone(); + tokio::spawn(async move { + handle.flush_local_txs().await; + }); + } + // Remove TXs from mempool and WAIT for completion // This prevents race where next nomination queries stale mempool if !tx_hashes.is_empty() { @@ -1732,6 +1808,31 @@ impl App { } } +/// Parse a `SetTxTargets` payload: `[everyone:1][count:4][nodeID:32]...`. +/// Returns `None` if it is malformed, `Some(None)` for "every peer". +fn parse_tx_targets(payload: &[u8]) -> Option>> { + if payload.len() < 5 { + return None; + } + let everyone = match payload[0] { + 0 => false, + 1 => true, + _ => return None, + }; + let count = u32::from_le_bytes(payload[1..5].try_into().unwrap()) as usize; + if payload.len() != 5 + count.checked_mul(32)? { + return None; + } + if everyone { + return Some(None); + } + let nodes = payload[5..] + .chunks_exact(32) + .map(|chunk| chunk.try_into().unwrap()) + .collect(); + Some(Some(nodes)) +} + fn setup_logging(level: &str) { use tracing_subscriber::{fmt, EnvFilter}; @@ -2371,6 +2472,95 @@ mod tests { Message::new(MessageType::TxSetExternalized, payload) } + fn submit_tx_message(tx: &ValidatedTx) -> Message { + let mut payload = tx.fee().to_le_bytes().to_vec(); + payload.extend_from_slice(&tx.num_ops().to_le_bytes()); + payload.extend_from_slice(tx.bytes()); + Message::new(MessageType::SubmitTx, payload) + } + + fn tx_targets_payload(everyone: bool, nodes: &[[u8; 32]]) -> Vec { + let mut payload = vec![everyone as u8]; + payload.extend_from_slice(&(nodes.len() as u32).to_le_bytes()); + for node in nodes { + payload.extend_from_slice(node); + } + payload + } + + #[test] + fn tx_targets_payload_is_parsed_strictly() { + let (a, b) = ([1u8; 32], [2u8; 32]); + assert_eq!( + parse_tx_targets(&tx_targets_payload(false, &[a, b])), + Some(Some(vec![a, b])) + ); + // "Everyone", with or without a list. + assert_eq!(parse_tx_targets(&tx_targets_payload(true, &[])), Some(None)); + assert_eq!(parse_tx_targets(&tx_targets_payload(true, &[a])), Some(None)); + // An empty list is a list: nobody else gets the transactions. + assert_eq!( + parse_tx_targets(&tx_targets_payload(false, &[])), + Some(Some(vec![])) + ); + // Malformed: too short, a bad flag, a count that does not match. + assert_eq!(parse_tx_targets(&[]), None); + assert_eq!(parse_tx_targets(&[0, 0, 0, 0]), None); + let mut bad_flag = tx_targets_payload(false, &[a]); + bad_flag[0] = 2; + assert_eq!(parse_tx_targets(&bad_flag), None); + let mut truncated = tx_targets_payload(false, &[a, b]); + truncated.pop(); + assert_eq!(parse_tx_targets(&truncated), None); + let mut wrong_count = tx_targets_payload(false, &[a]); + wrong_count[1..5].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(parse_tx_targets(&wrong_count), None); + } + + #[tokio::test] + async fn a_submitted_transaction_is_kept_until_core_reports_it_externalized() { + let (mut app, _core) = test_app(); + let (first, second) = (test_mempool_tx(1), test_mempool_tx(2)); + assert!(app.handle_core_message(submit_tx_message(&first)).await); + assert!(app.handle_core_message(submit_tx_message(&second)).await); + // Kept for sending again, and in the mempool for this node's own sets. + assert_eq!(app.libp2p_handle.local_tx_count(), 2); + assert_eq!(app.overlay_handle.get_top_txs(10).await.unwrap().len(), 2); + // Submitting it again changes nothing. + assert!(app.handle_core_message(submit_tx_message(&first)).await); + assert_eq!(app.libp2p_handle.local_tx_count(), 2); + + app.handle_core_message(remove_tx_message([1; 32], *first.hash())) + .await; + assert_eq!(app.libp2p_handle.local_tx_count(), 1); + let top = app.overlay_handle.get_top_txs(10).await.unwrap(); + assert_eq!(top.len(), 1); + assert_eq!(top[0].hash(), second.hash()); + } + + #[tokio::test] + async fn malformed_identity_and_target_messages_are_ignored() { + let (mut app, _core) = test_app(); + for payload in [vec![], vec![0u8; 127], vec![0u8; 129]] { + assert!( + app.handle_core_message(Message::new(MessageType::SetNodeIdentity, payload)) + .await + ); + } + assert!( + app.handle_core_message(Message::new(MessageType::SetTxTargets, vec![7, 0, 0])) + .await + ); + // A well-formed list is taken; the same list again is not a change. + let payload = tx_targets_payload(false, &[[3; 32]]); + assert!( + app.handle_core_message(Message::new(MessageType::SetTxTargets, payload.clone())) + .await + ); + assert!(!app.libp2p_handle.set_tx_targets(Some(vec![[3; 32]]))); + assert!(app.libp2p_handle.set_tx_targets(None)); + } + #[tokio::test] async fn externalization_removes_queued_admission_without_remembering_hashes() { let (mut app, _core) = test_app(); diff --git a/overlay/src/metrics.rs b/overlay/src/metrics.rs index 4cc5e94c5..2b2555baa 100644 --- a/overlay/src/metrics.rs +++ b/overlay/src/metrics.rs @@ -111,6 +111,28 @@ pub struct OverlayMetrics { /// overlay.txset-push.evicted — unrequested tx sets evicted by the unclaimed-set limits pub txset_push_evicted: AtomicU64, + // Transactions sent to the target leaders only + /// overlay.flood.tx-direct-sent — transaction bodies sent straight to a + /// target (no advert) + pub flood_tx_direct_sent: AtomicU64, + /// overlay.flood.tx-direct-sent-bytes — their bytes + pub flood_tx_direct_sent_bytes: AtomicU64, + /// overlay.flood.tx-direct-recv — bodies received without a demand (sent + /// directly by a peer), kept and not relayed + pub flood_tx_direct_recv: AtomicU64, + /// overlay.flood.tx-late-copy — transactions admitted although one of + /// the last externalized ledgers already has them (a copy that arrived + /// late) + pub flood_tx_late_copy: AtomicU64, + /// overlay.flood.tx-local-flooded — own transactions advertised to every + /// peer because a target was not directly reachable or none was known + pub flood_tx_local_flooded: AtomicU64, + /// overlay.node-id.rejected — identity claims with a bad signature + pub node_id_rejected: AtomicU64, + /// overlay.node-id.identified — connected peers that proved which + /// validator they are (gauge) + pub node_id_identified: AtomicI64, + // Receive timers (per message type, tracked as sum_us + count) /// overlay.recv.scp-message — time processing SCP messages pub recv_scp_sum_us: AtomicU64, @@ -172,6 +194,13 @@ impl Default for OverlayMetrics { txset_push_request_skipped: AtomicU64::new(0), txset_push_dropped: AtomicU64::new(0), txset_push_evicted: AtomicU64::new(0), + flood_tx_direct_sent: AtomicU64::new(0), + flood_tx_direct_sent_bytes: AtomicU64::new(0), + flood_tx_direct_recv: AtomicU64::new(0), + flood_tx_late_copy: AtomicU64::new(0), + flood_tx_local_flooded: AtomicU64::new(0), + node_id_rejected: AtomicU64::new(0), + node_id_identified: AtomicI64::new(0), recv_scp_sum_us: AtomicU64::new(0), recv_scp_count: AtomicU64::new(0), fetch_txset_sum_us: AtomicU64::new(0), @@ -239,6 +268,13 @@ impl OverlayMetrics { txset_push_request_skipped: self.txset_push_request_skipped.load(ORD), txset_push_dropped: self.txset_push_dropped.load(ORD), txset_push_evicted: self.txset_push_evicted.load(ORD), + flood_tx_direct_sent: self.flood_tx_direct_sent.load(ORD), + flood_tx_direct_sent_bytes: self.flood_tx_direct_sent_bytes.load(ORD), + flood_tx_direct_recv: self.flood_tx_direct_recv.load(ORD), + flood_tx_late_copy: self.flood_tx_late_copy.load(ORD), + flood_tx_local_flooded: self.flood_tx_local_flooded.load(ORD), + node_id_rejected: self.node_id_rejected.load(ORD), + node_id_identified: self.node_id_identified.load(ORD), recv_scp_sum_us: self.recv_scp_sum_us.load(ORD), recv_scp_count: self.recv_scp_count.load(ORD), fetch_txset_sum_us: self.fetch_txset_sum_us.load(ORD), @@ -314,6 +350,13 @@ pub struct MetricsSnapshot { pub txset_push_request_skipped: u64, pub txset_push_dropped: u64, pub txset_push_evicted: u64, + pub flood_tx_direct_sent: u64, + pub flood_tx_direct_sent_bytes: u64, + pub flood_tx_direct_recv: u64, + pub flood_tx_late_copy: u64, + pub flood_tx_local_flooded: u64, + pub node_id_rejected: u64, + pub node_id_identified: i64, pub recv_scp_sum_us: u64, pub recv_scp_count: u64, pub fetch_txset_sum_us: u64, diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index c2726cda9..859b1d13f 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -12,6 +12,7 @@ #include "herder/FilteredEntries.h" #include "herder/HerderPersistence.h" #include "herder/HerderUtils.h" +#include "herder/LeaderSchedule.h" #include "herder/LedgerCloseData.h" #include "herder/QuorumIntersectionChecker.h" #include "herder/RustQuorumCheckerAdaptor.h" @@ -35,6 +36,7 @@ #include "scp/LocalNode.h" #include "scp/Slot.h" #include "simulation/LoadGenerator.h" +#include "transactions/EventManager.h" #include "transactions/MutableTransactionResult.h" #include "transactions/SignatureUtils.h" #include "transactions/TransactionFrameBase.h" @@ -198,6 +200,44 @@ selectPrewarmCandidates(std::vector&& envelopes, return selected; } +// The candidates a pre-validation works on: the first `windows[phase]` +// transactions of each phase in mempool order, leaving out those of the +// 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 +selectPrevalidationCandidates(std::vector&& envelopes, + std::vector const& windows, + bool supportsSoroban, Hash const& networkID, + UnorderedSet const& closingTxs, + std::atomic const& stop) +{ + std::vector selected; + std::vector taken(windows.size(), 0); + for (auto const& env : envelopes) + { + if (stop.load(std::memory_order_relaxed)) + { + break; + } + size_t const phase = isSorobanEnvelope(env) ? 1 : 0; + if (phase >= windows.size() || taken[phase] >= windows[phase] || + (phase == 1 && !supportsSoroban) || + txValidityDependsOnCloseTime(env)) + { + continue; + } + auto tx = TransactionFrameBase::makeTransactionFromWire(networkID, env); + if (closingTxs.find(tx->getFullHash()) != closingTxs.end()) + { + continue; + } + ++taken[phase]; + selected.push_back(std::move(tx)); + } + return selected; +} + // The number of transactions in each phase of a generalized tx set. std::vector txCountPerPhase(TxSetXDRFrame const& txSet) @@ -391,6 +431,379 @@ HerderImpl::lostSync() discardPreparedTxSet(); mHerderSCPDriver.stateChanged(); setState(Herder::State::HERDER_SYNCING_STATE); + // Without the network's latest ledger this node does not know who leads. + updateTxFloodTargets(); +} + +std::optional> +HerderImpl::computeTxFloodTargets() +{ + releaseAssert(threadIsMain()); + if (mApp.getConfig().TX_FLOOD_LEGACY_ADVERTS || !isTracking() || + !mLedgerManager.isSynced()) + { + return std::nullopt; + } + auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); + // The schedule is only known for the slot after the LCL. While this + // node applies the ledger the network just externalized that is still + // the slot it knows; further behind, its leaders are history. + if (trackingConsensusLedgerIndex() > + static_cast(lcl.header.ledgerSeq) + 1 || + buildsLegacyTxSet(lcl.header.ledgerVersion)) + { + return std::nullopt; + } + uint64_t const slot = static_cast(lcl.header.ledgerSeq) + 1; + // A node that has not started the slot (or does not run SCP at all, like + // a watcher) is at round 0: the first round's leader comes first. + uint64_t const round = std::max(1, getSCP().getLeaderRound(slot)); + uint64_t const leaders = mApp.getConfig().TX_FLOOD_TARGET_LEADERS; + if (leaders == 0) + { + // 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); + if (everyone.empty()) + { + return std::nullopt; + } + std::sort(everyone.begin(), everyone.end()); + return everyone; + } + std::vector targets; + // 1. The leaders of the node's current round of the next slot and of + // the rounds after it. + for (uint64_t r = round; r < round + leaders; ++r) + { + if (r > UINT32_MAX) + { + break; + } + auto leader = + mHerderSCPDriver.getRoundLeader(slot, static_cast(r)); + if (!leader) + { + return std::nullopt; + } + if (std::find(targets.begin(), targets.end(), *leader) != targets.end()) + { + // Once around the schedule: every validator is in. + break; + } + targets.emplace_back(*leader); + } + // 2. With leader terms (LeaderSchedule.h) the node that closes the next + // ledger leads the one after it too, so the list above already names + // it. Not when the slot after next starts a new term (every multiple + // of LEADER_TERM_LEDGERS): its first leader is the first node of the + // order seeded with the LCL's hash, known from now on. That node + // builds its set, and validates its candidates before that, while + // the next ledger is still in consensus: it has to receive the + // transactions a ledger ahead. (A prediction: if the next ledger + // costs that very node its turn, another node leads, and gets the + // transactions when the ledger closes.) + if ((slot + 1) % LEADER_TERM_LEDGERS == 0) + { + auto const next = computeLeaderSchedule( + lcl.hash, mHerderSCPDriver.getLeaderSchedule(slot)); + if (!next.empty() && + std::find(targets.begin(), targets.end(), next.front()) == + targets.end()) + { + targets.emplace_back(next.front()); + } + } + if (targets.empty()) + { + return std::nullopt; + } + return targets; +} + +void +HerderImpl::updateTxFloodTargets() +{ + if (mApp.isStopping()) + { + // Also reached from posted callbacks. + return; + } + if (isTracking()) + { + auto const next = + static_cast(mLedgerManager.getLastClosedLedgerNum()) + 1; + if (trackingConsensusLedgerIndex() <= next && + getSCP().getLeaderRound(next) == UINT32_MAX) + { + // The slot is decided for this node by a v-blocking set that + // committed (a node that got there through its own ballot keeps + // its round) and no round leads it any more. Whatever is + // pending goes to the next slot's leaders when this ledger + // closes. (A node that is further behind than that goes on: it + // has to learn that it no longer knows the leaders.) + return; + } + } + auto targets = computeTxFloodTargets(); + if (targets == mTxFloodTargets) + { + return; + } + if (targets) + { + std::string names; + for (auto const& node : *targets) + { + names += (names.empty() ? "" : ", ") + + mApp.getConfig().toShortString(node); + } + CLOG_DEBUG(Herder, "Transactions now go to: {}", names); + } + else + { + CLOG_DEBUG(Herder, "Transactions now go to every peer"); + } + if (!mApp.getOverlayManager().setTxFloodTargets(targets)) + { + // Not delivered (no overlay connection): try again at the next + // change rather than assume the overlay knows. + return; + } +#ifdef BUILD_TESTS + mTxFloodTargetHistoryForTesting.push_back( + describeTxFloodTargetsForTesting(targets)); + if (mTxFloodTargetHistoryForTesting.size() > 4096) + { + mTxFloodTargetHistoryForTesting.pop_front(); + } +#endif + mTxFloodTargets = std::move(targets); +} + +#ifdef BUILD_TESTS +HerderImpl::TxFloodTargetUpdate +HerderImpl::describeTxFloodTargetsForTesting( + std::optional> targets) +{ + TxFloodTargetUpdate res; + res.lcl = mLedgerManager.getLastClosedLedgerHeader(); + res.slot = static_cast(res.lcl.header.ledgerSeq) + 1; + res.round = isTracking() ? getSCP().getLeaderRound(res.slot) : 0; + res.targets = std::move(targets); + return res; +} +#endif + +namespace +{ +// The source account and sequence number of an envelope (the inner +// transaction's for a fee bump), as TransactionFrame reports them. +std::pair +envelopeSourceAndSeq(TransactionEnvelope const& env) +{ + switch (env.type()) + { + case ENVELOPE_TYPE_TX_V0: + { + AccountID res; + res.ed25519() = env.v0().tx.sourceAccountEd25519; + return {res, env.v0().tx.seqNum}; + } + case ENVELOPE_TYPE_TX: + return {toAccountID(env.v1().tx.sourceAccount), env.v1().tx.seqNum}; + case ENVELOPE_TYPE_TX_FEE_BUMP: + { + auto const& inner = env.feeBump().tx.innerTx.v1().tx; + return {toAccountID(inner.sourceAccount), inner.seqNum}; + } + default: + // TransactionFrameBase::makeTransactionFromWire aborts here too. + abort(); + } +} +} + +void +HerderImpl::forgetSettledLocalPendingTxs() +{ + auto const lclSeq = mLedgerManager.getLastClosedLedgerNum(); + for (auto it = mLocalPendingTxs.begin(); it != mLocalPendingTxs.end();) + { + if (it->second.externalizedIn != 0 && + it->second.externalizedIn <= lclSeq) + { + it = mLocalPendingTxs.erase(it); + } + else + { + ++it; + } + } +} + +void +HerderImpl::forgetLocalPendingTxs(std::vector const& txHashes) +{ + if (mLocalPendingTxs.empty() || txHashes.empty()) + { + return; + } + UnorderedSet hashes(txHashes.begin(), txHashes.end()); + for (auto it = mLocalPendingTxs.begin(); it != mLocalPendingTxs.end();) + { + if (hashes.find(it->second.fullHash) != hashes.end()) + { + it = mLocalPendingTxs.erase(it); + } + else + { + ++it; + } + } +} + +TxSubmitStatus +HerderImpl::checkTxAdmission(TransactionFrameBasePtr const& tx, + bool skipValidityCheck) +{ + releaseAssert(threadIsMain()); + auto& metrics = mApp.getMetrics(); + // Overlay-only mode (load tests that skip apply) switches off the + // sequence-number check and the tx set's one-per-account rule; the + // one-per-account rule here goes with them. + bool const trackPending = +#ifdef BUILD_TESTS + !mApp.getRunInOverlayOnlyMode() && +#endif + // The overlay drops fee bumps (unsupported), so one would block its + // account here until the record times out. + tx->getEnvelope().type() != ENVELOPE_TYPE_TX_FEE_BUMP; + auto const now = mApp.getClock().now(); + if (trackPending) + { + auto it = mLocalPendingTxs.find(tx->getSourceID()); + if (it != mLocalPendingTxs.end() && + now - it->second.acceptedAt >= LOCAL_TX_PENDING_TIMEOUT) + { + mLocalPendingTxs.erase(it); + it = mLocalPendingTxs.end(); + } + if (it != mLocalPendingTxs.end()) + { + // The record is normally settled by the set that includes the + // transaction or another one of its account. The sequence number + // can also be used up without either (a BumpSequence operation in + // someone else's transaction, a transaction the overlay lost): + // then the pending one is dead, and the account is free. + auto account = + mLedgerManager.getLCLView()->getAccount(tx->getSourceID()); + if (account && + account.current().data.account().seqNum >= it->second.seqNum) + { + mApp.getOverlayManager().removeTransactions( + {it->second.fullHash}); + mLocalPendingTxs.erase(it); + it = mLocalPendingTxs.end(); + } + } + if (it != mLocalPendingTxs.end()) + { + if (it->second.fullHash == tx->getFullHash()) + { + return TxSubmitStatus::TX_STATUS_DUPLICATE; + } + // One transaction per source account at a time: any number of + // transactions of one account can each be valid against the + // LCL, but only one of them can apply. + metrics.NewMeter({"herder", "tx-admission", "account-busy"}, "tx") + .Mark(); + return TxSubmitStatus::TX_STATUS_TRY_AGAIN_LATER; + } + } + if (!skipValidityCheck) + { + auto timer = + metrics.NewTimer({"herder", "tx-admission", "check"}).TimeScope(); + // The check the leader's build runs on every candidate, against the + // same kind of state: this node's last closed ledger. + auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); + auto ledgerView = mLedgerManager.getLCLView(); + std::optional validationLedgerSeq; + if (protocolVersionStartsFrom(lcl.header.ledgerVersion, + ProtocolVersion::V_19)) + { + 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); + if (!result->isSuccess()) + { + CLOG_DEBUG(Herder, "Rejecting transaction {} from {}: {}", + hexAbbrev(tx->getFullHash()), + KeyUtils::toShortString(tx->getSourceID()), + xdr::xdr_traits::enum_name( + result->getResultCode())); + metrics.NewMeter({"herder", "tx-admission", "invalid"}, "tx") + .Mark(); + return TxSubmitStatus::TX_STATUS_ERROR; + } + } + if (trackPending) + { + mLocalPendingTxs[tx->getSourceID()] = + LocalPendingTx{tx->getFullHash(), tx->getSeqNum(), now, 0}; + } + return TxSubmitStatus::TX_STATUS_PENDING; +} + +std::vector +HerderImpl::settleLocalPendingTxs(TxSetXDRFrame const& txSet, + uint32_t txSetLedgerSeq) +{ + std::vector stale; + if (mLocalPendingTxs.empty()) + { + return stale; + } + txSet.forEachTransactionEnvelope([&](TransactionEnvelope const& env) { + auto const [source, seqNum] = envelopeSourceAndSeq(env); + auto it = mLocalPendingTxs.find(source); + if (it == mLocalPendingTxs.end() || seqNum < it->second.seqNum) + { + // Not an account with a pending transaction here, or a + // transaction that comes before it. + return; + } + // The account's sequence number will be at or past the pending + // transaction's once this set is applied: it is this one (the + // overlay drops it with the rest of the set), or it can no longer + // apply. The record itself stays until the ledger is closed + // (forgetSettledLocalPendingTxs): submissions are checked against + // the LCL, which still shows the old sequence number until then, so + // a retry of this transaction would pass as new. + stale.emplace_back(it->second.fullHash); + it->second.externalizedIn = txSetLedgerSeq; + }); + auto const now = mApp.getClock().now(); + for (auto it = mLocalPendingTxs.begin(); it != mLocalPendingTxs.end();) + { + if (now - it->second.acceptedAt >= LOCAL_TX_PENDING_TIMEOUT) + { + it = mLocalPendingTxs.erase(it); + } + else + { + ++it; + } + } + return stale; } SCP& @@ -481,6 +894,7 @@ HerderImpl::shutdown() { discardPreparedTxSet(); stopSignaturePrewarm(); + stopProposalPrevalidation(nullptr); mTrackingTimer.cancel(); mOutOfSyncTimer.cancel(); mTriggerTimer.cancel(); @@ -536,6 +950,15 @@ HerderImpl::processExternalized(uint64 slotIndex, StellarValue const& value, static_cast(slotIndex), *externalizedSet); #endif } + if (externalizedSet) + { + // Pending transactions of this node that the set made stale (their + // account's sequence number was used by another transaction) are + // dropped along with the set's own. + auto stale = settleLocalPendingTxs(*externalizedSet, + static_cast(slotIndex)); + txHashes.insert(txHashes.end(), stale.begin(), stale.end()); + } mApp.getOverlayManager().notifyTxSetExternalized(value.txSetHash, txHashes); { @@ -586,6 +1009,12 @@ HerderImpl::processExternalized(uint64 slotIndex, StellarValue const& value, } mLedgerManager.valueExternalized(ledgerData, isLatestSlot); + // The node may now know of ledgers it has not closed (it is applying, + // or catching up): the targets depend on how far behind its LCL is. Not + // from inside the externalize callback (the schedule may need a quorum + // rebuild). + mApp.postOnMainThread([this]() { updateTxFloodTargets(); }, + "Herder: update tx flood targets"); } void @@ -917,8 +1346,329 @@ HerderImpl::maybeStartSpeculativeApply(SCPEnvelope const& envelope) stopSignaturePrewarm(); } mSpeculativeApplyValue = std::make_pair(st.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. + validationCacheFor(lcl); + // A node that may lead the next ledger wants the state this execution + // 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() && + !buildsLegacyTxSet(lcl.header.ledgerVersion) && + mayLeadAfter(st.slotIndex, sv, /*appliedTransactions=*/true)) + { + onExecuted = [this](std::shared_ptr state) { + startProposalPrevalidation(std::move(state)); + }; + } mLedgerManager.startSpeculativeApply( - LedgerCloseData(static_cast(st.slotIndex), *txSet, sv)); + LedgerCloseData(static_cast(st.slotIndex), *txSet, sv), + std::move(onExecuted)); +} + +bool +HerderImpl::mayLeadAfter(uint64_t slotIndex, StellarValue const& value, + bool appliedTransactions) +{ + auto const schedule = mHerderSCPDriver.getExpectedLeaderSchedule( + slotIndex + 1, value, appliedTransactions); + auto const& self = getSCP().getLocalNodeID(); + return (schedule.size() > 0 && schedule[0] == self) || + (schedule.size() > 1 && schedule[1] == self); +} + +void +HerderImpl::startProposalPrevalidation( + std::shared_ptr state) +{ + ZoneScoped; + releaseAssert(threadIsMain()); + if (!state || mApp.isStopping()) + { + return; + } + auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); + // Only while the executed ledger is still the next one to close and this + // node is voting for the value that was executed. A ledger that applied + // nothing ends its leader's term, and a skipped set means the state is + // not the one the candidates would be applied on. + StellarValue votedValue; + if (!isTracking() || state->mLedgerSeq != lcl.header.ledgerSeq + 1 || + state->mTxSetSkipped || !state->mValue.upgrades.empty() || + !mSpeculativeApplyValue || + mSpeculativeApplyValue->first != state->mLedgerSeq || + !toStellarValue(mSpeculativeApplyValue->second, votedValue) || + !(votedValue == state->mValue) || + !mayLeadAfter(state->mLedgerSeq, state->mValue, state->mAppliedTxs > 0)) + { + return; + } + stopProposalPrevalidation(nullptr); + // The pre-validation verifies the same signatures, and more. + if (mSignaturePrewarm && mSignaturePrewarm->ledgerSeq > state->mLedgerSeq) + { + stopSignaturePrewarm(); + } + + auto& metrics = mApp.getMetrics(); + auto const windows = validTxTargets(lcl.header.ledgerVersion); + // The executed ledger's transactions are still in the mempool: look + // past that many more. + auto payload = [&]() { + auto timer = + metrics.NewTimer({"herder", "prevalidate", "pull"}).TimeScope(); + return std::make_shared const>( + pullTopTransactionsPayload(2 * maxTxSetCandidates() + + state->mTxHashes->size())); + }(); + uint32_t count = 0; + if (payload->size() >= 4) + { + std::memcpy(&count, payload->data(), 4); + } + if (count == 0) + { + return; + } + + size_t const workers = static_cast(mApp.getConfig().WORKER_THREADS); + size_t const tasks = std::max( + 1, std::min(mApp.getBatchExecutor().preferredTaskCount(), + workers > 2 ? workers - 2 : 1)); + auto job = std::make_shared(); + job->ledgerSeq = state->mLedgerSeq; + job->value = state->mValue; + job->start = std::chrono::steady_clock::now(); + job->pendingTasks = tasks; + mProposalPrevalidation = job; + metrics.NewMeter({"herder", "prevalidate", "started"}, "prevalidation") + .Mark(); + CLOG_DEBUG(Herder, + "Pre-validating candidates for ledger {} on the executed state " + "of ledger {}: {} pulled, {} tasks", + state->mLedgerSeq + 1, state->mLedgerSeq, count, tasks); + + bool const supportsSoroban = protocolVersionStartsFrom( + state->mHeader->ledgerVersion, SOROBAN_PROTOCOL_VERSION); + std::optional validationLedgerSeq; + if (protocolVersionStartsFrom(state->mHeader->ledgerVersion, + ProtocolVersion::V_19)) + { + validationLedgerSeq = state->mLedgerSeq + 1; + } + for (size_t task = 0; task < tasks; ++task) + { + mApp.postOnBackgroundThread( + [job, state, payload, windows, supportsSoroban, validationLedgerSeq, + networkID = mApp.getNetworkID(), + &appConnector = mApp.getAppConnector(), &metrics, task, tasks]() { + ZoneScopedN("proposal prevalidation"); + { + std::lock_guard lock(job->mutex); + --job->pendingTasks; + if (job->stop.load()) + { + return; + } + ++job->activeTasks; + } + // Results go to the job a few at a time; once it is stopped + // they are of no use to anyone. + std::vector> batch; + auto publish = [&]() { + std::lock_guard lock(job->mutex); + if (!job->stop.load()) + { + job->published.insert( + job->published.end(), + std::make_move_iterator(batch.begin()), + std::make_move_iterator(batch.end())); + } + batch.clear(); + }; + try + { + std::call_once(job->selected, [&]() { + // Must not throw out of call_once. + try + { + job->candidates = selectPrevalidationCandidates( + OverlayIPC::parseTopTransactions(*payload), + windows, supportsSoroban, networkID, + *state->mTxHashes, job->stop); + } + catch (std::exception const& e) + { + CLOG_WARNING(Herder, + "Proposal pre-validation could not " + "read its candidates: {}", + e.what()); + job->candidates.clear(); + std::lock_guard lock(job->mutex); + job->stop.store(true); + } + }); + auto const& candidates = job->candidates; + auto view = state->makeView(metrics); + auto const& header = *state->mHeader; + auto diagnostics = DiagnosticEventManager::createDisabled(); + for (size_t i = task; i < candidates.size(); i += tasks) + { + if (job->stop.load(std::memory_order_relaxed)) + { + break; + } + auto const& tx = candidates[i]; + TxValidationResult result; + result.mValid = + tx->checkValid(appConnector, *view, 0, 0, 0, + diagnostics, validationLedgerSeq) + ->isSuccess(); + if (result.mValid) + { + auto feeSource = + view->getAccount(tx->getFeeSourceID()); + if (feeSource) + { + result.mFeeSourceBalance = getAvailableBalance( + header, feeSource.current()); + } + } + batch.emplace_back(tx->getFullHash(), result); + if (batch.size() >= 32) + { + publish(); + } + } + } + catch (std::exception const& e) + { + // The build validates whatever has no result. What this + // task had not published yet is dropped with it. + CLOG_WARNING(Herder, "Proposal pre-validation failed: {}", + e.what()); + batch.clear(); + std::lock_guard lock(job->mutex); + job->stop.store(true); + } + publish(); + 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::steady_clock::now() - job->start) + .count(); + } + }, + "Herder: proposal prevalidation"); + } +} + +void +HerderImpl::stopProposalPrevalidation(LedgerHeaderHistoryEntry const* lcl) +{ + if (!mProposalPrevalidation) + { + return; + } + auto job = std::move(mProposalPrevalidation); + mProposalPrevalidation.reset(); + std::vector> results; + int64_t finishedAfterNs = -1; + { + // Take what the tasks have handed over so far. Nothing is published + // after this, so there is nothing to wait for: tasks that have not + // started find `stop` set and leave, running ones after their + // current transaction. + std::lock_guard lock(job->mutex); + finishedAfterNs = job->finishedAfterNs.load(); + job->stop.store(true); + results.swap(job->published); + } + auto& metrics = mApp.getMetrics(); + metrics.NewTimer({"herder", "prevalidate", "duration"}) + .Update(finishedAfterNs >= 0 + ? std::chrono::nanoseconds(finishedAfterNs) + : std::chrono::duration_cast( + std::chrono::steady_clock::now() - job->start)); + metrics.NewMeter({"herder", "prevalidate", "txs"}, "transaction") + .Mark(results.size()); + if (finishedAfterNs < 0) + { + metrics + .NewMeter({"herder", "prevalidate", "incomplete"}, "prevalidation") + .Mark(); + } + // The results describe the ledger that was executed. They hold on top of + // the LCL only if that is the ledger, closed with the very same value: + // executing a ledger is deterministic, so the state it was validated on + // is then the state the LCL left (the value has no upgrades, see + // maybeStartSpeculativeApply). + if (!lcl || lcl->header.ledgerSeq != job->ledgerSeq || + !(lcl->header.scpValue == job->value) || + mValidationCache.lclHash != lcl->hash) + { + metrics + .NewMeter({"herder", "prevalidate", "discarded"}, "prevalidation") + .Mark(); + return; + } + metrics.NewMeter({"herder", "prevalidate", "used"}, "prevalidation").Mark(); + for (auto& r : results) + { + mValidationCache.results.emplace(std::move(r)); + } +} + +TxValidationCache* +HerderImpl::validationCacheFor(LedgerHeaderHistoryEntry const& lcl) +{ + if (mValidationCache.lclHash != lcl.hash) + { + mValidationCache.lclHash = lcl.hash; + TxValidationCache().swap(mValidationCache.results); + } + stopProposalPrevalidation(&lcl); + return &mValidationCache.results; +} + +std::chrono::milliseconds +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). + 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); +} + +std::chrono::milliseconds +HerderImpl::prepareTxSetLeadTime(std::chrono::milliseconds expectedCloseTime, + std::chrono::milliseconds lastBuild) +{ + using std::chrono::milliseconds; + // What a pushed set needs to reach the peers (measured: about 0.1 s + // across regions for a 4,000-transaction set). + milliseconds const pushAllowance{100}; + auto lead = + std::min(expectedCloseTime / 4, milliseconds(250)); + if (lastBuild > milliseconds::zero()) + { + lead = std::max(lead, lastBuild + lastBuild / 4 + + pushAllowance); + } + return std::min(lead, expectedCloseTime / 2); } TxSubmitStatus @@ -930,15 +1680,27 @@ HerderImpl::recvTransaction(TransactionFrameBasePtr tx, bool submittedFromSelf ) { ZoneScoped; + CLOG_TRACE(Herder, "recv transaction {} for {}", + hexAbbrev(tx->getFullHash()), + KeyUtils::toShortString(tx->getSourceID())); + + // This node vouches for what it passes on: the transaction goes to the + // next leaders only, and they do not check it before they build. + bool skipValidityCheck = false; +#ifdef BUILD_TESTS + skipValidityCheck = isLoadgenTx; +#endif + auto const admission = checkTxAdmission(tx, skipValidityCheck); + if (admission != TxSubmitStatus::TX_STATUS_PENDING) + { + return admission; + } #ifdef BUILD_TESTS if (submittedFromSelf) { mLedgerManager.recordTxSubmission(tx->getContentsHash()); } #endif - CLOG_TRACE(Herder, "recv transaction {} for {}", - hexAbbrev(tx->getFullHash()), - KeyUtils::toShortString(tx->getSourceID())); auto const& env = tx->getEnvelope(); mApp.getOverlayManager().broadcastTransaction(env, tx->getFullFee(), @@ -1496,6 +2258,29 @@ HerderImpl::lastClosedLedgerIncreased(bool latest, TxSetXDRFrameConstPtr txSet, { releaseAssert(threadIsMain()); discardPreparedTxSet(); + // What the next build has to be ready for (see + // expectedTxSetBuildDuration). + mLastClosedTxSetSize = txSet ? txSet->sizeTxTotal() : 0; + { + // A pre-validation is for the ledger that was executed + // speculatively; if another one closed, its results are of no use. + auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); + if (mProposalPrevalidation && + (mProposalPrevalidation->ledgerSeq != lcl.header.ledgerSeq || + !(mProposalPrevalidation->value == lcl.header.scpValue))) + { + stopProposalPrevalidation(nullptr); + } + // Results computed on the previous ledger are of no use either, + // whether or not this node builds on the new one. + if (mValidationCache.lclHash != lcl.hash) + { + mValidationCache.lclHash = lcl.hash; + TxValidationCache().swap(mValidationCache.results); + } + } + // The LCL now shows the sequence numbers the closed set used. + forgetSettledLocalPendingTxs(); // Ensure potential upgrades are handled in overlay maybeHandleUpgrade(); @@ -1517,6 +2302,9 @@ HerderImpl::lastClosedLedgerIncreased(bool latest, TxSetXDRFrameConstPtr txSet, releaseAssert(mLedgerManager.isSynced()); setupTriggerNextLedger(); + // The new ledger's hash seeds the next slot's leader schedule: the + // transactions this node holds go to the new leaders now. + updateTxFloodTargets(); // Now that the new ledger is closed, process any already-buffered SCP // envelopes for the next slot. Posted to the main thread so control @@ -1788,9 +2576,15 @@ HerderImpl::setupTriggerNextLedger() VirtualClock::system_time_point::duration>( triggerTime - now), lcl.header.ledgerVersion)); - // Yield out of ledger close before touching its read view. - // This is main-thread work; only its scheduling changes. - mPrepareTxSetTimer.expires_at(now); + // 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), @@ -1953,19 +2747,15 @@ HerderImpl::prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) { return; } - mPreparedTxSet = buildTxSet(ledgerSeq, closeTime, /*pushToPeers=*/false); + // The prepared set is proposed unchanged at the trigger + // (makeProposalValue), so it goes to the peers as soon as its XDR + // exists: they can have it before the proposal does. + mPreparedTxSet = buildTxSet(ledgerSeq, closeTime, /*pushToPeers=*/true); CLOG_INFO(Herder, "Prepared TX set for ledger {} before trigger: {} transactions, " "close time {}", ledgerSeq, mPreparedTxSet->txSet->sizeTxTotal(), closeTime.toString()); - // A capacity-limited prepared set is proposed unchanged at the trigger - // (makeProposalValue), so peers can have it before the proposal exists. - // Any other prepared set is rebuilt at the trigger and is not pushed. - if (mPreparedTxSet->capacityLimited && mPreparedTxSet->applicableTxSet) - { - pushTxSetToPeers(mPreparedTxSet->txSet, ledgerSeq); - } } void @@ -2128,7 +2918,7 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime, } } }, - targets); + targets, validationCacheFor(lcl)); markSignatureCacheLookups(mApp, "build", sigCacheHits, sigCacheMisses); CLOG_INFO(Herder, "Proposed TX set has {} transactions", proposedSet->sizeTxTotal()); @@ -2163,31 +2953,27 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime, } } - // If all valid candidates fit, refresh at the trigger so an early empty - // or small snapshot doesn't prevent collecting transactions during the - // timer wait. Reuse a snapshot when selection actually excluded valid - // candidates. Invalid transactions do not establish excess demand. - // Candidates past a phase's validated prefix count as valid here. There - // are such candidates only when the prefix already held more valid ones - // than the phase can include, so they don't change the result (except - // in a phase that can include nothing: it validates nothing, and counts - // as capacity limited whenever it has candidates). - size_t validCandidates = 0; - for (size_t i = 0; i < txPhases.size(); ++i) - { - validCandidates += txPhases[i].size() - invalidTxPhases[i].size(); - } - bool capacityLimited = validCandidates > proposedSet->sizeTxTotal(); + auto const buildDuration = + std::chrono::steady_clock::now() - txSetBuildStart; mSCPMetrics.mTxSetBuild.Update( - std::chrono::duration_cast( - std::chrono::steady_clock::now() - txSetBuildStart)); + std::chrono::duration_cast(buildDuration)); + // The next build is scheduled far enough ahead of its trigger to fit one + // like this (see prepareTxSetLeadTime). + mLastTxSetBuildDuration = + std::chrono::duration_cast(buildDuration); + if (proposedSet->sizeTxTotal() >= 100) + { + mTxSetBuildCostPerTx = + std::chrono::duration_cast( + buildDuration) / + static_cast(proposedSet->sizeTxTotal()); + } return PreparedTxSet{lcl.hash, ledgerSeq, closeTime, proposedSet, std::move(applicableProposedSet), - std::move(invalidTxHashes), - capacityLimited}; + std::move(invalidTxHashes)}; } // called to take a position during the next round @@ -2287,7 +3073,7 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, } // There is no nomination: the leader of each round (a deterministic - // schedule seeded by the LCL hash) proposes straight into the ballot + // schedule derived from the LCL's header) proposes straight into the ballot // protocol. The value is only built if this node leads a round. mHerderSCPDriver.startSlot( ledgerSeqToTrigger, @@ -2586,8 +3372,7 @@ HerderImpl::makeProposalValue(uint32_t ledgerSeqToTrigger) auto prepared = std::move(mPreparedTxSet); mPreparedTxSet.reset(); mPrepareTxSetTimer.cancel(); - if (!prepared || !prepared->capacityLimited || - prepared->ledgerSeq != ledgerSeqToTrigger || + if (!prepared || prepared->ledgerSeq != ledgerSeqToTrigger || prepared->previousLedgerHash != lcl.hash || ctValidityOffset(prepared->closeTime) != std::chrono::milliseconds::zero()) @@ -2629,6 +3414,9 @@ HerderImpl::makeProposalValue(uint32_t ledgerSeqToTrigger) "from the mempool", prepared->invalidTxHashes.size()); mApp.getOverlayManager().removeTransactions(prepared->invalidTxHashes); + // Those among them that this node accepted itself no longer hold + // their accounts. + forgetLocalPendingTxs(prepared->invalidTxHashes); } if (!applicableProposedSet) diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index cd22b658d..c4f1b9369 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -9,6 +9,7 @@ #include "herder/LedgerCloseData.h" #include "herder/PendingEnvelopes.h" #include "herder/QuorumIntersectionChecker.h" +#include "herder/TxSetFrame.h" #include "herder/Upgrades.h" #include "ledger/LedgerHeaderUtils.h" #include "overlay/NetworkConstants.h" @@ -67,6 +68,49 @@ class HerderImpl : public Herder void lostSync(); + // A transaction accepted from a client blocks its source account on this + // node for this long at most (the overlay stops sending it after the + // same time, see LOCAL_TX_MAX_AGE in overlay/src/flood/tx_router.rs). + static constexpr std::chrono::seconds LOCAL_TX_PENDING_TIMEOUT{300}; + + // THE place that decides which nodes get the transactions this node + // accepts. Returns the validators to send them to, or nullopt for + // "every peer" (the flood). With the leader schedule: the leaders of the + // node's current round and of the next one for the slot after the LCL, + // and, when the slot after that starts a new leader term, its expected + // first leader. + // Everyone if the node is not tracking or not synced, if there is no + // schedule, or if the next ledger uses a legacy tx set (which every + // validator builds for itself). + std::optional> computeTxFloodTargets(); + // Recomputes the targets and tells the overlay if they changed. Called + // when a ledger closes, when SCP enters a round, and when sync is lost. + void updateTxFloodTargets(); +#ifdef BUILD_TESTS + // What the overlay was last told (nullopt = every peer). + std::optional> const& + getTxFloodTargetsForTesting() const + { + return mTxFloodTargets; + } + // Every change of the targets sent to the overlay, with all the target + // function had to go on: the LCL at that time (the schedule of `slot`, + // the slot after it, is a function of its header, and the seeded order + // of the slot after that a function of its hash) and the node's round + // in `slot` (0 = not started). + struct TxFloodTargetUpdate + { + uint64_t slot; + uint32_t round; + std::optional> targets; + LedgerHeaderHistoryEntry lcl; + }; + // `targets` with the node's present state. + TxFloodTargetUpdate describeTxFloodTargetsForTesting( + std::optional> targets); + std::deque mTxFloodTargetHistoryForTesting; +#endif + HerderImpl(Application& app); ~HerderImpl(); @@ -145,6 +189,7 @@ class HerderImpl : public Herder std::function(size_t)> mGetTopTransactionsForTesting; friend class LeaderBuildTestAccess; + friend class ProposalPrevalidationTestAccess; std::optional mMaxClassicTxSize; std::optional mMaxTxSizeOverride; @@ -273,8 +318,33 @@ class HerderImpl : public Herder TxSetXDRFrameConstPtr txSet; ApplicableTxSetFrameConstPtr applicableTxSet; std::vector invalidTxHashes; - bool capacityLimited; }; + // How long before the trigger the first-round leader builds its set, so + // that the set is built and on its way to the peers when consensus + // starts; what arrives in between waits for the next ledger. + // * At least a quarter of the target close time, but no more than + // 250 ms of a long one. + // * Long enough for the build this node expects (`expectedBuild`, see + // expectedTxSetBuildDuration; with a quarter to spare) plus the time + // the pushed set needs to reach the peers. + // * Never more than half of the target close time. + static std::chrono::milliseconds + prepareTxSetLeadTime(std::chrono::milliseconds expectedCloseTime, + std::chrono::milliseconds expectedBuild); + // How long this node's latest tx set build took (zero before the first), + // what it cost per transaction of the set it built (zero until it has + // built a set of some size), and how many transactions the last closed + // ledger's set held. + std::chrono::milliseconds mLastTxSetBuildDuration{0}; + std::chrono::nanoseconds mTxSetBuildCostPerTx{0}; + size_t mLastClosedTxSetSize{0}; + // What the next build is expected to take: a build like this node's last + // one, or what a set the size of the last closed ledger's costs at this + // node's rate, whichever is longer. The second matters for a node that + // has not built for a while: its last build may be from before the load + // 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; 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 @@ -328,6 +398,65 @@ class HerderImpl : public Herder std::vector const& pendingPerPhase); std::shared_ptr mSignaturePrewarm; + // Pre-validation of the next ledger's candidates. + // + // A node that expects to lead ledger N+1 (in round 1, or in round 2 if + // the first leader times out) starts validating its candidates as soon as + // it has executed ledger N speculatively, while N is still going through + // consensus: background tasks run `checkValid` on the mempool's top + // transactions against the state that execution left (see + // 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). + struct ProposalPrevalidation + { + // The executed ledger (N) and its value: the results hold on top of + // N if and only if N closes with this value. + uint32_t ledgerSeq{0}; + StellarValue value; + std::chrono::steady_clock::time_point start; + std::atomic stop{false}; + // Decoded and selected once, by whichever task gets there first. + std::once_flag selected; + std::vector candidates; + // 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; + size_t pendingTasks{0}; + size_t activeTasks{0}; + // Nanoseconds from `start` until the last task finished, or -1. + std::atomic finishedAfterNs{-1}; + }; + std::shared_ptr mProposalPrevalidation; + // Whether the local node would lead round 1 or 2 of the ledger after + // `slotIndex` if `slotIndex` closes with `value`. + bool mayLeadAfter(uint64_t slotIndex, StellarValue const& value, + bool appliedTransactions); + // Called on the main thread when the speculative execution of a ledger + // has finished (`state` is null if it was not executed). + void startProposalPrevalidation( + std::shared_ptr state); + // Stops the running pre-validation, if any. With `lcl`, its results are + // moved into mValidationCache if they hold on top of `lcl`. + void stopProposalPrevalidation(LedgerHeaderHistoryEntry const* lcl); + + // Validation results for candidates of the next ledger, computed against + // the last closed ledger `lclHash`: by the pre-validation above, and by + // every build on that ledger. A later build on the same ledger (the build + // at the trigger after an early one, a later round's) only validates what + // it has not seen. + struct ValidationCache + { + Hash lclHash; + TxValidationCache results; + }; + ValidationCache mValidationCache; + // The cache for a build on top of `lcl`, emptied if it was for another + // ledger and topped up with the pre-validation's results. + TxValidationCache* validationCacheFor(LedgerHeaderHistoryEntry const& lcl); + // Tx sets the overlay holds on stable storage: received from it, or // handed to it after building them. persistSCPState leaves those out of // the database; after a restart they come back from the overlay (see @@ -420,6 +549,42 @@ class HerderImpl : public Herder VirtualTimer mTriggerTimer; VirtualTimer mPrepareTxSetTimer; + // What updateTxFloodTargets last told the overlay (nullopt = everyone, + // which is also what the overlay starts with). + std::optional> mTxFloodTargets; + + // Transactions this node accepted from clients and has not seen + // externalized, by source account: one per account at a time. Not used + // in overlay-only mode. + struct LocalPendingTx + { + Hash fullHash; + SequenceNumber seqNum; + VirtualClock::time_point acceptedAt; + // The ledger whose set uses up its sequence number, once such a set + // externalized (0 before); the record goes when that ledger is + // closed. + uint32_t externalizedIn; + }; + UnorderedMap mLocalPendingTxs; + // The checks a transaction passes before this node takes it and sends it + // on: one pending transaction per source account, and checkValid + // against the LCL (skipped if `skipValidityCheck`). + TxSubmitStatus checkTxAdmission(TransactionFrameBasePtr const& tx, + bool skipValidityCheck); + // Drops the records of accounts whose transaction externalized in + // `txSet` or timed out; returns the hashes of pending transactions that + // can no longer apply (another transaction of the account used the + // sequence number), for the overlay to drop. + std::vector settleLocalPendingTxs(TxSetXDRFrame const& txSet, + uint32_t txSetLedgerSeq); + // Drops the records settleLocalPendingTxs marked, once the ledger that + // settled them is the LCL. + void forgetSettledLocalPendingTxs(); + // Drops the records of these transactions (removed from the mempool + // because they failed validation in this node's own build). + void forgetLocalPendingTxs(std::vector const& txHashes); + VirtualTimer mOutOfSyncTimer; VirtualTimer mTxSetGarbageCollectTimer; diff --git a/src/herder/HerderSCPDriver.cpp b/src/herder/HerderSCPDriver.cpp index e663c438c..9d269e464 100644 --- a/src/herder/HerderSCPDriver.cpp +++ b/src/herder/HerderSCPDriver.cpp @@ -1080,15 +1080,18 @@ HerderSCPDriver::getLeaderSchedule(uint64_t slotIndex) auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); if (slotIndex != static_cast(lcl.header.ledgerSeq) + 1) { - // The seed is the hash of the previous ledger's header: unknown for - // any slot but the next one. + // The schedule is derived from the previous ledger's header (who + // closed it, and whether it applied): unknown for any slot but the + // next one. See getExpectedLeaderSchedule for the slot after that. return empty; } // Force a pending rebuild of the transitive quorum, if any mPendingEnvelopes.isNodeDefinitelyInQuorum(mSCP.getLocalNodeID()); auto const& quorum = mPendingEnvelopes.getCurrentlyTrackedQuorum(); auto& cache = mLeaderSchedule; + bool const haveResults = ledgersHaveResults(); if (cache.mSlotIndex != slotIndex || cache.mSeed != lcl.hash || + cache.mLedgersHaveResults != haveResults || cache.mQuorumSize != quorum.size() || cache.mQuorumRebuildCount != mPendingEnvelopes.getQuorumRebuildCount()) { @@ -1098,7 +1101,9 @@ HerderSCPDriver::getLeaderSchedule(uint64_t slotIndex) { nodes.emplace_back(n.first); } - cache.mSchedule = computeLeaderSchedule(lcl.hash, std::move(nodes)); + cache.mSchedule = computeNextLeaderSchedule( + lcl.header, std::move(nodes), haveResults); + cache.mLedgersHaveResults = haveResults; cache.mSlotIndex = slotIndex; cache.mSeed = lcl.hash; cache.mQuorumSize = quorum.size(); @@ -1107,6 +1112,46 @@ HerderSCPDriver::getLeaderSchedule(uint64_t slotIndex) return cache.mSchedule; } +bool +HerderSCPDriver::ledgersHaveResults() const +{ +#ifdef BUILD_TESTS + // Overlay-only mode closes ledgers without applying them. + return !mApp.getRunInOverlayOnlyMode(); +#else + return true; +#endif +} + +std::vector +HerderSCPDriver::getExpectedLeaderSchedule(uint64_t slotIndex, + StellarValue const& closingValue, + bool appliedTransactions) +{ + releaseAssert(threadIsMain()); + auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); + if (slotIndex != static_cast(lcl.header.ledgerSeq) + 2) + { + return {}; + } + // The tracked quorum as it is: this is called while SCP emits an + // envelope, where a pending rebuild (database reads) does not belong, + // and a prediction made on a slightly older node set is still one. + auto const& quorum = mPendingEnvelopes.getCurrentlyTrackedQuorum(); + std::vector nodes; + nodes.reserve(quorum.size()); + for (auto const& n : quorum) + { + nodes.emplace_back(n.first); + } + // The ledger in between (LCL+1) will name the LCL's hash as its previous + // ledger hash: that is the seed of the slot after it. + return computeLeaderSchedule( + lcl.hash, std::move(nodes), + leaderTermOutcome(lcl.header.ledgerSeq + 1, closingValue, + appliedTransactions || !ledgersHaveResults())); +} + std::optional HerderSCPDriver::getRoundLeader(uint64 slotIndex, uint32 round) { @@ -1177,6 +1222,48 @@ HerderSCPDriver::measureAndRecordBallotBlockedOnTxSet(uint64_t slotIndex, mSCPMetrics.mBallotBlockedOnTxSet.Update(std::chrono::milliseconds(0)); } +ValueWrapperPtr +HerderSCPDriver::reproposeValue(uint64_t slotIndex, uint32 round, + Value const& value) +{ + // The leader of a later round keeps the value it already votes for (its + // transaction set is known to the nodes that voted for it), but puts it + // forward under its own signature: the node whose value closes a ledger + // leads the next one (see leaderTermOutcome), and a leader whose round + // timed out should not be that node. SCP only asks while the node is + // not tied to the value (it has not accepted it as prepared); after + // that the value, and with it the term, stays as it is. + StellarValue sv; + if (round <= 1 || !toStellarValue(value, sv) || + sv.ext.v() == STELLAR_VALUE_BASIC || isEmptyTxSetStellarValue(sv)) + { + return nullptr; + } + auto const& self = mSCP.getLocalNodeID(); + if (getLcValueSignature(sv).nodeID == self) + { + return nullptr; + } + // Only a set this node holds: its signature makes it answerable for the + // set (a ledger that applies nothing costs the signer its term). + auto txSet = mPendingEnvelopes.getTxSet(sv.txSetHash); + if (auto const* frame = std::get_if(&txSet); + !frame || !*frame) + { + return nullptr; + } + mApp.getMetrics().NewMeter({"scp", "leader", "resigned"}, "value").Mark(); + auto resigned = + mHerder.makeStellarValue(sv.txSetHash, getConsensusTime(sv), + sv.upgrades, mApp.getConfig().NODE_SEED); + CLOG_INFO(Herder, + "Slot {} round {}: proposing the value of {} under our own " + "signature", + slotIndex, round, + mApp.getConfig().toShortString(getLcValueSignature(sv).nodeID)); + return wrapStellarValue(resigned); +} + void HerderSCPDriver::proposedValue(uint64_t slotIndex, uint32 round, Value const& value) @@ -1193,6 +1280,11 @@ HerderSCPDriver::enteredRound(uint64_t slotIndex, uint32 round, bool timedOut) { it->second.mMaxRound = std::max(it->second.mMaxRound, round); } + // The transactions this node holds follow the round: its leader and the + // next one get them. Not from inside this SCP state change (the schedule + // may need a quorum rebuild, see below). + mApp.postOnMainThread([this]() { mHerder.updateTxFloodTargets(); }, + "Herder: update tx flood targets"); if (round > 1) { // Only read the cached schedule here: this is called from inside SCP diff --git a/src/herder/HerderSCPDriver.h b/src/herder/HerderSCPDriver.h index 1067c0575..939bc1ec7 100644 --- a/src/herder/HerderSCPDriver.h +++ b/src/herder/HerderSCPDriver.h @@ -110,9 +110,9 @@ class HerderSCPDriver : public SCPDriver void valueExternalized(uint64_t slotIndex, Value const& value) override; // Deterministic leader schedule: the leader of `round` for `slotIndex`. - // The schedule is a permutation of the transitive quorum seeded by the - // hash of the last closed ledger header, so it is only defined for the - // next ledger to close (LCL+1). + // The schedule is derived from the header of the last closed ledger and + // the transitive quorum (see computeNextLeaderSchedule), so it is only + // defined for the next ledger to close (LCL+1). bool isProposalPending(uint64 slotIndex, NodeID const& leader) const override; std::optional getRoundLeader(uint64 slotIndex, @@ -122,6 +122,18 @@ class HerderSCPDriver : public SCPDriver std::vector const& getLeaderSchedule(uint64_t slotIndex); // True if the local node leads `round` of slotIndex. bool isLocalNodeRoundLeader(uint64_t slotIndex, uint32_t round); + // The schedule `slotIndex` = LCL+2 will have if LCL+1 closes with + // `closingValue` and applies transactions or not. Empty for any other + // slot. This is a prediction: nothing in consensus depends on it. + std::vector + getExpectedLeaderSchedule(uint64_t slotIndex, + StellarValue const& closingValue, + bool appliedTransactions); + // False when ledgers close without recording transaction results (the + // test-only overlay-only mode). + bool ledgersHaveResults() const; + ValueWrapperPtr reproposeValue(uint64_t slotIndex, uint32 round, + Value const& value) override; // Update metrics indicating that a value was replaced with an empty-tx-set // value for slotIndex. @@ -338,6 +350,7 @@ class HerderSCPDriver : public SCPDriver Hash mSeed; size_t mQuorumSize{0}; uint64_t mQuorumRebuildCount{0}; + bool mLedgersHaveResults{true}; std::vector mSchedule; }; LeaderScheduleCache mLeaderSchedule; diff --git a/src/herder/LeaderSchedule.cpp b/src/herder/LeaderSchedule.cpp index cc9599a71..8f3f4efc6 100644 --- a/src/herder/LeaderSchedule.cpp +++ b/src/herder/LeaderSchedule.cpp @@ -4,6 +4,7 @@ #include "herder/LeaderSchedule.h" #include "crypto/SHA.h" +#include "ledger/LedgerHeaderUtils.h" #include "util/XDROperators.h" #include "xdrpp/marshal.h" #include @@ -35,4 +36,69 @@ computeLeaderSchedule(Hash const& seed, std::vector nodes) } return res; } + +LeaderTermOutcome +leaderTermOutcome(uint32_t ledgerSeq, StellarValue const& value, + bool appliedTransactions) +{ + LeaderTermOutcome outcome; + if (value.ext.v() == STELLAR_VALUE_BASIC) + { + return outcome; + } + auto const& signer = getLcValueSignature(value).nodeID; + if (isEmptyTxSetStellarValue(value) || !appliedTransactions) + { + outcome.mForfeited = signer; + } + else if ((static_cast(ledgerSeq) + 1) % LEADER_TERM_LEDGERS != 0) + { + outcome.mIncumbent = signer; + } + return outcome; +} + +LeaderTermOutcome +leaderTermOutcome(LedgerHeader const& lcl, bool ledgersHaveResults) +{ + static Hash const emptyResults = xdrSha256(TransactionResultSet{}); + return leaderTermOutcome(lcl.ledgerSeq, lcl.scpValue, + !ledgersHaveResults || + lcl.txSetResultHash != emptyResults); +} + +std::vector +computeLeaderSchedule(Hash const& seed, std::vector nodes, + LeaderTermOutcome const& outcome) +{ + auto schedule = computeLeaderSchedule(seed, std::move(nodes)); + // Both keep the seeded order of everyone else. + if (outcome.mIncumbent) + { + auto it = + std::find(schedule.begin(), schedule.end(), *outcome.mIncumbent); + if (it != schedule.end()) + { + std::rotate(schedule.begin(), it, it + 1); + } + } + if (outcome.mForfeited) + { + auto it = + std::find(schedule.begin(), schedule.end(), *outcome.mForfeited); + if (it != schedule.end()) + { + std::rotate(it, it + 1, schedule.end()); + } + } + return schedule; +} + +std::vector +computeNextLeaderSchedule(LedgerHeader const& lcl, std::vector nodes, + bool ledgersHaveResults) +{ + return computeLeaderSchedule(lcl.previousLedgerHash, std::move(nodes), + leaderTermOutcome(lcl, ledgersHaveResults)); +} } diff --git a/src/herder/LeaderSchedule.h b/src/herder/LeaderSchedule.h index 9042d90ea..ddcba75d5 100644 --- a/src/herder/LeaderSchedule.h +++ b/src/herder/LeaderSchedule.h @@ -4,7 +4,9 @@ #pragma once +#include "xdr/Stellar-ledger.h" #include "xdr/Stellar-types.h" +#include #include namespace stellar @@ -18,9 +20,74 @@ namespace stellar // node leads twice. // // The order depends only on `seed` and the *set* of nodes, so every node that -// agrees on both computes the same schedule: with a symmetric quorum, seeding -// with the hash of the last closed ledger header gives the whole network the -// same leader for each round of the next ledger. +// agrees on both computes the same schedule. std::vector computeLeaderSchedule(Hash const& seed, std::vector nodes); + +// Leader terms: the node whose value closed a ledger leads the next one too, +// for up to LEADER_TERM_LEDGERS ledgers in a row. A stable leader can prepare +// its next transaction set while the previous ledger is still in its ballot +// (see HerderImpl::startProposalPrevalidation). +// +// Terms end on fixed ledger numbers (every multiple of LEADER_TERM_LEDGERS +// starts a new one), so that the schedule of a slot is a function of the +// previous ledger's header alone, as before. +constexpr uint32_t LEADER_TERM_LEDGERS = 5; + +// What the ledger `ledgerSeq`, closed with `value`, means for the leader of +// the next ledger. +// * `mIncumbent`: the node that keeps leading: the node that signed +// `value`. Set unless the term is over or was forfeited. +// * `mForfeited`: the node that lost its term early and goes to the end of +// the next ledger's order: the node that signed `value`, when +// - `value` is an empty-tx-set value (its leader's transaction set never +// arrived, so consensus dropped it), or +// - the ledger applied no transaction (`appliedTransactions` is false). +// That covers a ledger whose transaction set was found invalid at +// apply, and an honestly empty ledger: nothing is lost by rotating +// after one. +// There is no incumbent when the next ledger starts a new term (every +// multiple of LEADER_TERM_LEDGERS); a forfeit counts there too. Neither is +// set when `value` is unsigned (the genesis ledger). +// +// A leader whose round times out loses the term as well, without being named +// here: the leader of the round that closes the ledger signs the value it +// proposes, and so becomes the incumbent. (Exception: a later round's leader +// that has already accepted the previous leader's value as prepared keeps +// that value and its signature, see HerderSCPDriver::reproposeValue.) +struct LeaderTermOutcome +{ + std::optional mIncumbent; + std::optional mForfeited; +}; +LeaderTermOutcome leaderTermOutcome(uint32_t ledgerSeq, + StellarValue const& value, + bool appliedTransactions); + +// The same from the header of a closed ledger, where "applied no transaction" +// reads: `txSetResultHash` is the hash of an empty result set. Only checked +// with `ledgersHaveResults`; without it (the test-only overlay-only mode, +// where no ledger records results) every ledger counts as having applied. +LeaderTermOutcome leaderTermOutcome(LedgerHeader const& lcl, + bool ledgersHaveResults); + +// The leader schedule of the ledger after `lcl`. +// +// The order is seeded with `lcl.previousLedgerHash`, the hash of the ledger +// *before* `lcl`. `lcl`'s own hash would do as well for agreement, but it is +// only known once `lcl` has closed; with the older hash, the order for the +// next ledger is already known while `lcl` is going through consensus. +// +// An incumbent (see leaderTermOutcome) that is one of `nodes` leads round 1, +// and a node that forfeited its term comes last; the seeded order of the +// other nodes is kept. +std::vector computeNextLeaderSchedule(LedgerHeader const& lcl, + std::vector nodes, + bool ledgersHaveResults); + +// The same, for a ledger that has not closed yet: `seed` is the hash of the +// ledger before it and `outcome` what it is expected to mean for its leader. +std::vector computeLeaderSchedule(Hash const& seed, + std::vector nodes, + LeaderTermOutcome const& outcome); } diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index bf1602762..de090b70c 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -881,7 +881,8 @@ makeTxSetFromTransactions( #endif , TxSetReadyCallback const& onTxSetReady, - std::vector const& validTxTargets) + std::vector const& validTxTargets, + TxValidationCache* validationCache) { releaseAssert(threadIsMain()); releaseAssert(!app.getLedgerManager().isApplying()); @@ -926,7 +927,7 @@ makeTxSetFromTransactions( { validatedTxs = TxSetUtils::trimInvalid( phaseTxs, app, accountFeeMap, closeTimeOffset.seconds(), - closeTimeOffset.seconds(), invalid); + closeTimeOffset.seconds(), invalid, validationCache); } else { @@ -941,7 +942,7 @@ makeTxSetFromTransactions( SurgePricingPriorityQueue::GENERIC_LANE; }, app, accountFeeMap, closeTimeOffset.seconds(), - closeTimeOffset.seconds(), invalid); + closeTimeOffset.seconds(), invalid, validationCache); } #ifdef BUILD_TESTS // In tests we shouldn't be really trying to close ledgers with @@ -1925,6 +1926,40 @@ TxSetPhaseFrame::sortedForApply(Hash const& txSetHash) const } } +bool +txValidityDependsOnCloseTime(TransactionEnvelope const& env) +{ + auto timeBounded = [](TimeBounds const& tb) { + return tb.minTime != 0 || tb.maxTime != 0; + }; + auto conditional = [&](Preconditions const& cond) { + switch (cond.type()) + { + case PRECOND_NONE: + return false; + case PRECOND_TIME: + return timeBounded(cond.timeBounds()); + case PRECOND_V2: + return (cond.v2().timeBounds && + timeBounded(*cond.v2().timeBounds)) || + cond.v2().minSeqAge != 0; + } + // An unknown kind of precondition: assume the worst. + return true; + }; + switch (env.type()) + { + case ENVELOPE_TYPE_TX_V0: + return env.v0().tx.timeBounds && timeBounded(*env.v0().tx.timeBounds); + case ENVELOPE_TYPE_TX: + return conditional(env.v1().tx.cond); + case ENVELOPE_TYPE_TX_FEE_BUMP: + return conditional(env.feeBump().tx.innerTx.v1().tx.cond); + default: + return true; + } +} + TxSetValidationContext TxSetValidationContext::forLCL(Application& app) { diff --git a/src/herder/TxSetFrame.h b/src/herder/TxSetFrame.h index 1feb222ae..ef074dced 100644 --- a/src/herder/TxSetFrame.h +++ b/src/herder/TxSetFrame.h @@ -103,6 +103,28 @@ enum class TxSetValidationResult // Returns a string representation of the validation result for logging. std::string toString(TxSetValidationResult result); +// What validating one transaction against a ledger found: whether +// `checkValid` passed and, if it did, the available balance of the fee source +// (which the caller compares with the fees of all the transactions it pays +// for). +struct TxValidationResult +{ + bool mValid{false}; + std::optional mFeeSourceBalance; +}; + +// Validation results by full transaction hash, for transactions validated +// against one particular ledger. It only holds transactions whose validity +// does not depend on the close time of the ledger they go into (see +// `txValidityDependsOnCloseTime`), so a result is the same for every set +// built on that ledger. Whoever owns the cache makes sure it is only used +// with the ledger its results were computed on. +using TxValidationCache = UnorderedMap; + +// Whether the outcome of validating `env` can depend on the close time it is +// validated for: it has time bounds or a minimum sequence age. +bool txValidityDependsOnCloseTime(TransactionEnvelope const& env); + // The ledger state a transaction set is validated against: the ledger the // set would apply on top of. Validation only reads the state through this // context, so it can run wherever the context's views can be used: the main @@ -120,6 +142,11 @@ struct TxSetValidationContext // Upper bound on the number of tasks the per-transaction checks are // split into. 1 runs them on the calling thread. size_t mMaxTasks; + // Results of earlier validations against this ledger, if any: a + // transaction found in it is not validated again, and the results of the + // others are added to it. Only read while the validation tasks run, and + // only written by the calling thread. + TxValidationCache* mValidationCache{nullptr}; // The LCL as seen by the main thread. Main thread only. static TxSetValidationContext forLCL(Application& app); @@ -185,7 +212,10 @@ makeTxSetFromTransactions( #endif , TxSetReadyCallback const& onTxSetReady = {}, - std::vector const& validTxTargets = {}); + std::vector const& validTxTargets = {}, + // Results of earlier validations against the LCL (see + // `TxValidationCache`); extended with this build's. + TxValidationCache* validationCache = nullptr); #ifdef BUILD_TESTS std::pair @@ -449,7 +479,8 @@ class TxSetPhaseFrame #endif , TxSetReadyCallback const& onTxSetReady, - std::vector const& validTxTargets); + std::vector const& validTxTargets, + TxValidationCache* validationCache); #ifdef BUILD_TESTS friend std::pair makeTxSetFromTransactions( @@ -642,7 +673,8 @@ class ApplicableTxSetFrame #endif , TxSetReadyCallback const& onTxSetReady, - std::vector const& validTxTargets); + std::vector const& validTxTargets, + TxValidationCache* validationCache); #ifdef BUILD_TESTS friend std::pair makeTxSetFromTransactions( diff --git a/src/herder/TxSetUtils.cpp b/src/herder/TxSetUtils.cpp index 2405d40aa..00483cd04 100644 --- a/src/herder/TxSetUtils.cpp +++ b/src/herder/TxSetUtils.cpp @@ -16,11 +16,13 @@ #include "main/Application.h" #include "main/Config.h" #include "main/ErrorMessages.h" +#include "medida/meter.h" #include "transactions/MutableTransactionResult.h" #include "transactions/TransactionUtils.h" #include "util/BatchExecutor.h" #include "util/GlobalChecks.h" #include "util/Logging.h" +#include "util/MetricsRegistry.h" #include "util/ProtocolVersion.h" #include "util/UnorderedSet.h" #include "util/XDRCereal.h" @@ -222,11 +224,15 @@ TxSetUtils::getInvalidTxListWithErrors( std::vector>> txValidationResult( txs.size()); auto& appConnector = app.getAppConnector(); + // The tasks only read the cache; what they validated themselves is + // added to it below, by this thread. + TxValidationCache const* const cache = ctx.mValidationCache; + std::vector fromCache(cache ? txs.size() : 0, 0); app.getBatchExecutor().executeBatchOverRanges( txs.size(), taskCount, - [&appConnector, &txs, &ledgerViews, &txValidationResult, - lowerBoundCloseTimeOffset, upperBoundCloseTimeOffset, + [&appConnector, &txs, &ledgerViews, &txValidationResult, cache, + &fromCache, lowerBoundCloseTimeOffset, upperBoundCloseTimeOffset, validationLedgerSeq](size_t begin, size_t end, size_t rangeIndex) { auto const& view = *ledgerViews.at(rangeIndex); auto const header = view.getLedgerHeader().current(); @@ -238,7 +244,18 @@ TxSetUtils::getInvalidTxListWithErrors( // cache it here, in parallel, rather than there. Each frame // is in one range only, and the batch completes before the // main thread reads it. - txs[i]->getFullHash(); + auto const& fullHash = txs[i]->getFullHash(); + if (cache) + { + auto it = cache->find(fullHash); + if (it != cache->end()) + { + txValidationResult[i] = {it->second.mValid, + it->second.mFeeSourceBalance}; + fromCache[i] = 1; + continue; + } + } auto res = txs[i]->checkValid(appConnector, view, 0, lowerBoundCloseTimeOffset, upperBoundCloseTimeOffset, @@ -257,6 +274,30 @@ TxSetUtils::getInvalidTxListWithErrors( } }); + if (ctx.mValidationCache) + { + size_t hits = 0; + for (size_t i = 0; i < txs.size(); ++i) + { + if (fromCache[i]) + { + ++hits; + } + else if (!txValidityDependsOnCloseTime(txs[i]->getEnvelope())) + { + ctx.mValidationCache->emplace( + txs[i]->getFullHash(), + TxValidationResult{txValidationResult[i].first, + txValidationResult[i].second}); + } + } + auto& metrics = app.getMetrics(); + metrics.NewMeter({"herder", "txset", "validation-cache-hit"}, "tx") + .Mark(hits); + metrics.NewMeter({"herder", "txset", "validation-cache-miss"}, "tx") + .Mark(txs.size() - hits); + } + TxFrameListWithErrors invalidTxsWithError; auto& [invalidTxs, errorCode] = invalidTxsWithError; errorCode = TxSetValidationResult::VALID; @@ -336,9 +377,13 @@ TxSetUtils::trimInvalid(TxFrameList const& txs, Application& app, UnorderedMap& accountFeeMap, uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, - TxFrameList& invalidTxs) + TxFrameList& invalidTxs, + TxValidationCache* validationCache) { - invalidTxs = getInvalidTxListWithErrors(txs, app, accountFeeMap, + releaseAssert(threadIsMain()); + auto ctx = TxSetValidationContext::forLCL(app); + ctx.mValidationCache = validationCache; + invalidTxs = getInvalidTxListWithErrors(txs, app, ctx, accountFeeMap, lowerBoundCloseTimeOffset, upperBoundCloseTimeOffset) .first; @@ -351,7 +396,7 @@ TxSetUtils::trimInvalidPrefix( std::function const& countsTowardTarget, Application& app, UnorderedMap& accountFeeMap, uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, - TxFrameList& invalidTxs) + TxFrameList& invalidTxs, TxValidationCache* validationCache) { ZoneScoped; TxFrameList validTxs; @@ -374,9 +419,9 @@ TxSetUtils::trimInvalidPrefix( txs.begin() + validated + count); validated += count; TxFrameList batchInvalid; - for (auto const& tx : - trimInvalid(batch, app, accountFeeMap, lowerBoundCloseTimeOffset, - upperBoundCloseTimeOffset, batchInvalid)) + for (auto const& tx : trimInvalid( + batch, app, accountFeeMap, lowerBoundCloseTimeOffset, + upperBoundCloseTimeOffset, batchInvalid, validationCache)) { if (!countsTowardTarget || countsTowardTarget(*tx)) { diff --git a/src/herder/TxSetUtils.h b/src/herder/TxSetUtils.h index 4e459efe2..09ef4dc7c 100644 --- a/src/herder/TxSetUtils.h +++ b/src/herder/TxSetUtils.h @@ -71,7 +71,8 @@ class TxSetUtils trimInvalid(TxFrameList const& txs, Application& app, UnorderedMap& accountFeeMap, uint64_t lowerBoundCloseTimeOffset, - uint64_t upperBoundCloseTimeOffset, TxFrameList& invalidTxs); + uint64_t upperBoundCloseTimeOffset, TxFrameList& invalidTxs, + TxValidationCache* validationCache = nullptr); // Like `trimInvalid`, for candidates in priority order when only the // first `targetValidTxs` valid ones that `countsTowardTarget` accepts are @@ -86,6 +87,6 @@ class TxSetUtils countsTowardTarget, Application& app, UnorderedMap& accountFeeMap, uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset, - TxFrameList& invalidTxs); + TxFrameList& invalidTxs, TxValidationCache* validationCache = nullptr); }; // class TxSetUtils } // namespace stellar diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 983bf7b7c..012af8cb1 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -4,6 +4,7 @@ #include "bucket/BucketIndexUtils.h" #include "herder/HerderImpl.h" +#include "herder/LeaderSchedule.h" #include "herder/LedgerCloseData.h" #include "herder/test/TestTxSetUtils.h" #include "main/Application.h" @@ -5423,6 +5424,22 @@ class LeaderBuildTestAccess herder.setupTriggerNextLedger(); } + // When the first-round leader builds its set, and how long before the + // trigger that is. + static VirtualClock::time_point + prepareTime(HerderImpl& herder) + { + return herder.mPrepareTxSetTimer.expiry_time(); + } + + static std::chrono::milliseconds + prepareLeadTime(HerderImpl& herder) + { + return HerderImpl::prepareTxSetLeadTime( + herder.mLedgerManager.getExpectedLedgerCloseTime(), + herder.expectedTxSetBuildDuration()); + } + // The leader's full build (mempool pull through final checks). static TxSetXDRFrameConstPtr build(HerderImpl& herder, uint32_t ledgerSeq, ConsensusTime closeTime) @@ -5483,11 +5500,32 @@ class LeaderBuildTestAccess selectLocalFirstLeader(HerderImpl& herder) { auto& scp = herder.getSCP(); - auto const& lcl = - herder.mApp.getLedgerManager().getLastClosedLedgerHeader(); + auto& lm = herder.mApp.getLedgerManager(); + // A node whose own ledger applied nothing has forfeited its term and + // comes last in the next ledger's order, whatever the seed. Test + // setup reaches such ledgers (upgrade helpers close empty ledgers + // under the local node's signature): close one that applies a + // transaction, after which the node leads again or, at the start of + // a term, takes its place in the seeded order. + if (leaderTermOutcome(lm.getLastClosedLedgerHeader().header, true) + .mForfeited == scp.getLocalNodeID()) + { + auto root = herder.mApp.getRoot(); + txtest::closeLedger( + herder.mApp, + {root->tx({txtest::payment(root->getPublicKey(), 1)})}); + REQUIRE( + !leaderTermOutcome(lm.getLastClosedLedgerHeader().header, true) + .mForfeited); + } + auto const& lcl = lm.getLastClosedLedgerHeader(); auto qset = scp.getLocalQuorumSet(); REQUIRE(qset.validators.size() == 2); - for (int i = 0; i < 100; ++i) + // The local node's key is fixed (the instance number and the day's + // seed decide it) and may sort behind nearly every other key: allow + // enough candidates for the absent peer to find one that sorts + // behind even such a key. + for (int i = 0; i < 2000; ++i) { qset.validators = { scp.getLocalNodeID(), @@ -6107,7 +6145,7 @@ TEST_CASE("only round leaders construct local proposals", auto& scp = herder.getSCP(); auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto const seq = lcl.header.ledgerSeq + 1; - // The schedule is seeded by the hash of the last closed ledger. + // The schedule is derived from the last closed ledger. if (!previous) { previous = lcl.hash; @@ -6147,8 +6185,30 @@ TEST_CASE("only round leaders construct local proposals", app, [&]() { return done; }, std::chrono::seconds(1)); REQUIRE(done); REQUIRE(clock.now() < herder.getTriggerTimer().expiry_time()); + // Nobody builds right away: the first-round leader does shortly + // before the trigger, so that its set holds what arrives until then. + REQUIRE(pulls == 0); + REQUIRE(!LeaderBuildTestAccess::prepared(herder)); + if (role == 0) + { + auto const lead = LeaderBuildTestAccess::prepareLeadTime(herder); + REQUIRE(lead > std::chrono::milliseconds(0)); + REQUIRE(lead <= std::chrono::milliseconds(250)); + auto const prepareAt = LeaderBuildTestAccess::prepareTime(herder); + REQUIRE(prepareAt == herder.getTriggerTimer().expiry_time() - lead); + REQUIRE(clock.now() < prepareAt); + clock.setCurrentVirtualTime(prepareAt); + testutil::crankUntil( + app, + [&]() { + return LeaderBuildTestAccess::prepared(herder) != nullptr; + }, + std::chrono::seconds(1)); + } + REQUIRE(clock.now() < herder.getTriggerTimer().expiry_time()); REQUIRE(pulls == (role == 0 ? 1 : 0)); REQUIRE(bool(LeaderBuildTestAccess::prepared(herder)) == (role == 0)); + auto const preparedSet = LeaderBuildTestAccess::prepared(herder); REQUIRE(scp.getLeaderRound(seq) == 0); REQUIRE(scp.getLatestMessagesSend(seq).empty()); auto const trigger = herder.getTriggerTimer().expiry_time(); @@ -6158,10 +6218,10 @@ TEST_CASE("only round leaders construct local proposals", app, [&]() { return scp.getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); REQUIRE(scp.getLeaderRound(seq) == 1); - // The first leader refreshes its empty early snapshot. Followers - // pull the mempool once, only to pre-verify signatures; they do not - // build or publish a set. - REQUIRE(pulls == (role == 0 ? 2 : 1)); + // The first leader proposes the set it prepared, without pulling + // again. Followers pull the mempool once, only to pre-verify + // signatures; they do not build or publish a set. + REQUIRE(pulls == 1); REQUIRE(scp.getLatestMessagesSend(seq).empty() == (role != 0)); // With the first leader silent, real leader timers eventually @@ -6172,13 +6232,18 @@ TEST_CASE("only round leaders construct local proposals", std::chrono::seconds(30)); REQUIRE(scp.getLeaderRound(seq) == static_cast(role + 1)); REQUIRE(driver.isLocalNodeRoundLeader(seq, scp.getLeaderRound(seq))); - REQUIRE(pulls == 2); + REQUIRE(pulls == (role == 0 ? 1 : 2)); auto messages = scp.getLatestMessagesSend(seq); REQUIRE(messages.front().statement.pledges.type() == SCP_ST_PREPARE); auto const& proposed = messages.front().statement.pledges.prepare().ballot.value; StellarValue nominated; xdr::xdr_from_opaque(proposed, nominated); + if (role == 0) + { + REQUIRE(preparedSet); + REQUIRE(nominated.txSetHash == preparedSet->getContentsHash()); + } auto txSet = std::get( herder.getTxSet(nominated.txSetHash)); REQUIRE(txSet); @@ -6278,6 +6343,204 @@ TEST_CASE("a follower promoted after losing sync still proposes", REQUIRE(checkedFollower); } +// A leader whose round times out should not keep its term: the node whose +// value closes a ledger leads the next one. So when the leader of a later +// round puts forward the value it already votes for (the first leader's), it +// does so under its own signature, unless it is already tied to that value. +TEST_CASE("the leader of a later round signs the value it proposes again", + "[herder][early-proposal][leader-term]") +{ + // Three nodes. The first leader's proposal reaches the local node, which + // votes for it, and the round times out with the local node next in line. + // * not tied: all three nodes are needed. The third node votes for + // something else, so the first leader's value is not accepted as + // prepared: the local node proposes it again under its own signature. + // * tied: any two nodes are a quorum, so the first leader's and the + // local node's votes make the value accepted as prepared. The first + // leader may have confirmed it: the local node keeps it as it is. + auto const tied = GENERATE(false, true); + CAPTURE(tied); + std::vector keys; + SCPQuorumSet qset; + qset.threshold = tied ? 2 : 3; + for (int i = 0; i < 3; ++i) + { + keys.push_back(SecretKey::fromSeed( + sha256(fmt::format("early-proposal-node-{}", i)))); + qset.validators.push_back(keys.back().getPublicKey()); + } + + bool checkedSecondLeader = false; + for (int i = 0; i < 3 && !checkedSecondLeader; ++i) + { + VirtualClock clock; + clock.setCurrentVirtualTime(VirtualClock::from_time_t(1000)); + auto cfg = getTestConfig(i); + cfg.HTTP_PORT = 0; + cfg.MANUAL_CLOSE = false; + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = false; + cfg.FORCE_OLD_STYLE_PREPARE_START_TRIGGER_TIMER = true; + cfg.NODE_SEED = keys[i]; + cfg.QUORUM_SET = qset; + auto app = createTestApplication(clock, cfg); + auto& herder = static_cast(app->getHerder()); + auto& scp = herder.getSCP(); + auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); + auto const seq = lcl.header.ledgerSeq + 1; + auto& driver = herder.getHerderSCPDriver(); + if (!driver.isLocalNodeRoundLeader(seq, 2)) + { + continue; + } + checkedSecondLeader = true; + auto const firstLeader = driver.getRoundLeader(seq, 1); + REQUIRE(firstLeader); + REQUIRE(!(*firstLeader == scp.getLocalNodeID())); + auto keyOf = [&](NodeID const& id) { + auto it = + std::find_if(keys.begin(), keys.end(), [&](SecretKey const& k) { + return k.getPublicKey() == id; + }); + REQUIRE(it != keys.end()); + return *it; + }; + auto const firstKey = keyOf(*firstLeader); + auto const thirdKey = + *std::find_if(keys.begin(), keys.end(), [&](SecretKey const& k) { + return !(k.getPublicKey() == *firstLeader) && + !(k.getPublicKey() == scp.getLocalNodeID()); + }); + auto& resigned = + app->getMetrics().NewMeter({"scp", "leader", "resigned"}, "value"); + + herder.getHerderSCPDriver().recordBallotStart(lcl.header.ledgerSeq); + size_t pulls = 0; + herder.mGetTopTransactionsForTesting = [&](size_t) { + ++pulls; + return std::vector{}; + }; + LeaderBuildTestAccess::schedule(herder); + clock.setCurrentVirtualTime(herder.getTriggerTimer().expiry_time()); + testutil::crankUntil( + app, [&]() { return scp.getLeaderRound(seq) != 0; }, + std::chrono::seconds(1)); + REQUIRE(scp.getLeaderRound(seq) == 1); + REQUIRE(scp.getLatestMessagesSend(seq).empty()); + + // An empty set (the same on every node), proposed by `signer` at + // counter 1. + auto txSet = + makeTxSetFromTransactions(TxFrameList{}, *app, ApplyTimeOffset{}) + .first; + herder.getPendingEnvelopes().addTxSet(txSet->getContentsHash(), seq, + txSet); + auto const closeTime = + std::max(ConsensusTime::fromSystemTime(clock.system_now(), + lcl.header.ledgerVersion), + getConsensusTime(lcl.header.scpValue) + .next(lcl.header.ledgerVersion)); + auto prepareFrom = [&](SecretKey const& signer, ConsensusTime ct) { + auto const value = herder.makeStellarValue( + txSet->getContentsHash(), ct, emptyUpgradeSteps, signer); + SCPEnvelope env; + env.statement.slotIndex = seq; + env.statement.nodeID = signer.getPublicKey(); + env.statement.pledges.type(SCP_ST_PREPARE); + auto& prep = env.statement.pledges.prepare(); + prep.ballot.counter = 1; + prep.ballot.value = xdr::xdr_to_opaque(value); + // every node uses the same (normalized) quorum set + prep.quorumSetHash = scp.getLocalNode()->getQuorumSetHash(); + herder.signEnvelope(signer, env); + return std::make_pair(env, value); + }; + auto const [proposal, firstValue] = prepareFrom(firstKey, closeTime); + auto const firstBallotValue = + proposal.statement.pledges.prepare().ballot.value; + REQUIRE(herder.recvSCPEnvelope(proposal) == + Herder::ENVELOPE_STATUS_READY); + + auto ballotOf = [&]() -> std::optional { + auto messages = scp.getLatestMessagesSend(seq); + if (messages.empty() || + messages.front().statement.pledges.type() != SCP_ST_PREPARE) + { + return std::nullopt; + } + return messages.front().statement.pledges.prepare().ballot; + }; + // The local node votes for the first leader's value as it is. + testutil::crankUntil( + app, [&]() { return ballotOf().has_value(); }, + std::chrono::seconds(1)); + REQUIRE(ballotOf()); + REQUIRE(ballotOf()->counter == 1); + REQUIRE(ballotOf()->value == firstBallotValue); + if (!tied) + { + // The third node is at counter 1 with a value of its own: the + // local node has now heard from a quorum, and the first leader's + // value has two votes out of the three it needs. + auto const [other, otherValue] = + prepareFrom(thirdKey, closeTime.next(lcl.header.ledgerVersion)); + REQUIRE(herder.recvSCPEnvelope(other) == + Herder::ENVELOPE_STATUS_READY); + } + + // Nothing more arrives. The round times out and the local node + // leads round 2 without building anything. + testutil::crankUntil( + app, [&]() { return ballotOf() && ballotOf()->counter == 2; }, + std::chrono::seconds(60)); + REQUIRE(ballotOf()); + REQUIRE(ballotOf()->counter == 2); + REQUIRE(scp.getLeaderRound(seq) == 2); + StellarValue proposed; + xdr::xdr_from_opaque(ballotOf()->value, proposed); + // the same transaction set, close time and upgrades either way + REQUIRE(proposed.txSetHash == firstValue.txSetHash); + REQUIRE(getConsensusTime(proposed) == getConsensusTime(firstValue)); + REQUIRE(proposed.upgrades == firstValue.upgrades); + REQUIRE(getLcValueSignature(firstValue).nodeID == *firstLeader); + REQUIRE(driver.validateValue(seq, ballotOf()->value, true) == + SCPDriver::kFullyValidatedValue); + auto const outcome = leaderTermOutcome(seq, proposed, true); + bool const insideTerm = (seq + 1) % LEADER_TERM_LEDGERS != 0; + if (tied) + { + // kept as it is: if it closes the ledger, the first leader + // keeps its term + REQUIRE(ballotOf()->value == firstBallotValue); + REQUIRE(resigned.count() == 0); + if (insideTerm) + { + REQUIRE(outcome.mIncumbent == *firstLeader); + } + } + else + { + // under the local node's signature: if it closes the ledger, the + // local node leads the next one, and the first leader does not + REQUIRE(getLcValueSignature(proposed).nodeID == + scp.getLocalNodeID()); + REQUIRE(!(ballotOf()->value == firstBallotValue)); + REQUIRE(resigned.count() == 1); + if (insideTerm) + { + REQUIRE(outcome.mIncumbent == scp.getLocalNodeID()); + } + } + // The mempool was only pulled to pre-verify signatures: no set was + // built. + REQUIRE(pulls >= 1); + REQUIRE( + app->getMetrics().NewTimer({"herder", "txset", "build"}).count() == + 0); + herder.mGetTopTransactionsForTesting = nullptr; + } + REQUIRE(checkedSecondLeader); +} + TEST_CASE("a leader build that completes a fetch can externalize", "[herder][early-proposal]") { @@ -6658,7 +6921,10 @@ TEST_CASE("signature prewarm at the ballot covers the next ledger", herder.mGetTopTransactionsForTesting = nullptr; } -TEST_CASE("early underfilled proposals refresh at the trigger", +// The first-round leader builds once, shortly before the trigger, and +// proposes that set as it is: what arrives after the build waits for the +// next ledger, whether or not the set had room for it. +TEST_CASE("the set prepared before the trigger is proposed unchanged", "[herder][early-proposal]") { auto const invalidFirst = GENERATE(false, true); @@ -6686,7 +6952,7 @@ TEST_CASE("early underfilled proposals refresh at the trigger", auto extra = bob.tx({payment(alice, 100)}); auto bad = txtest::getGenesisAccount(*app, 2).tx({payment(alice, 100)}); getSignatures(bad).clear(); - std::vector mempool; + std::vector mempool{tx->getEnvelope()}; if (invalidFirst) { // More candidates than capacity, but they cannot fill the ledger. @@ -6703,24 +6969,31 @@ TEST_CASE("early underfilled proposals refresh at the trigger", [&]() { return LeaderBuildTestAccess::prepared(herder) != nullptr; }, std::chrono::seconds(1)); REQUIRE(pulls == 1); - REQUIRE(LeaderBuildTestAccess::prepared(herder)->sizeTxTotal() == 0); - // Transactions arrive while we are waiting for the normal trigger. + auto const prepared = LeaderBuildTestAccess::prepared(herder); + REQUIRE(prepared->sizeTxTotal() == (invalidFirst ? 0 : 1)); + // It was built a short lead time before the trigger, not earlier. + REQUIRE(clock.now() >= herder.getTriggerTimer().expiry_time() - + LeaderBuildTestAccess::prepareLeadTime(herder)); + REQUIRE(clock.now() < herder.getTriggerTimer().expiry_time()); + // Transactions arrive while we are waiting for the trigger. mempool = {tx->getEnvelope(), extra->getEnvelope()}; clock.setCurrentVirtualTime(herder.getTriggerTimer().expiry_time()); testutil::crankUntil( app, [&]() { return herder.getSCP().getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); - REQUIRE(pulls == 2); - // Check the actual set cached for the proposal, independently of benchmark - // instrumentation. Both newly arrived transactions must be present. - auto expected = makeTxSetFromTransactions(TxFrameList{tx, extra}, *app, - ApplyTimeOffset{}) - .first; - REQUIRE(expected->sizeTxTotal() == 2); - auto nominated = std::get( - herder.getTxSet(expected->getContentsHash())); + // No second build: the proposal names the prepared set. + REQUIRE(pulls == 1); + auto messages = herder.getSCP().getLatestMessagesSend(seq); + REQUIRE(!messages.empty()); + REQUIRE(messages.front().statement.pledges.type() == SCP_ST_PREPARE); + StellarValue proposed; + xdr::xdr_from_opaque( + messages.front().statement.pledges.prepare().ballot.value, proposed); + REQUIRE(proposed.txSetHash == prepared->getContentsHash()); + auto nominated = + std::get(herder.getTxSet(proposed.txSetHash)); REQUIRE(nominated); - REQUIRE(nominated->sizeTxTotal() == 2); + REQUIRE(nominated->sizeTxTotal() == (invalidFirst ? 0 : 1)); REQUIRE(!LeaderBuildTestAccess::prepared(herder)); herder.mGetTopTransactionsForTesting = nullptr; } diff --git a/src/herder/test/LeaderRoundsTests.cpp b/src/herder/test/LeaderRoundsTests.cpp index ad1db120e..530d22a5a 100644 --- a/src/herder/test/LeaderRoundsTests.cpp +++ b/src/herder/test/LeaderRoundsTests.cpp @@ -3,8 +3,8 @@ // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 // Network-level tests of consensus without nomination: the leader of each -// round comes from a deterministic schedule seeded by the hash of the last -// closed ledger header (see herder/LeaderSchedule.h). +// round comes from a deterministic schedule derived from the last closed +// ledger header (see herder/LeaderSchedule.h). #include "crypto/SHA.h" #include "herder/Herder.h" @@ -38,13 +38,15 @@ acceleratedConfig(int configNum) struct ClosedLedger { - Hash mPreviousHash; + LedgerHeader mPrevious; NodeID mProposer; + Hash mResultHash; }; // Cranks until `app` closed `lastLedger`, recording for every ledger it -// closes on the way the hash of the ledger before it and the node that signed -// the externalized value (its proposer). +// closes on the way the header of the ledger before it (what its leader +// schedule is derived from) and the node that signed the externalized value +// (its proposer). std::map crankAndRecord(Simulation::pointer const& simulation, Application::pointer const& app, uint32_t lastLedger, @@ -63,7 +65,8 @@ crankAndRecord(Simulation::pointer const& simulation, { REQUIRE(cur.header.previousLedgerHash == prev.hash); res[cur.header.ledgerSeq] = ClosedLedger{ - prev.hash, getLcValueSignature(cur.header.scpValue).nodeID}; + prev.header, getLcValueSignature(cur.header.scpValue).nodeID, + cur.header.txSetResultHash}; } prev = cur; } @@ -107,15 +110,33 @@ TEST_CASE("leader schedule drives consensus on a healthy network", timeouts += leaderTimeouts(n) + ballotTimeouts(n); } - // Every node computes the same schedule from the previous ledger's hash, - // and the value that externalizes is the one its round 1 leader proposed - // (unless a round timed out somewhere, e.g. on a busy test machine). + // Every node computes the same schedule from the previous ledger's + // header, and the value that externalizes is the one its round 1 leader + // proposed (unless a round timed out somewhere, e.g. on a busy test + // machine). These ledgers are empty, so no leader keeps its term: the + // node that closed the previous ledger goes last, and the order seeded + // by the ledger before it is otherwise kept. std::set proposers; int64_t notFirstLeader = 0; REQUIRE(closed.size() >= 10); for (auto const& c : closed) { - auto schedule = computeLeaderSchedule(c.second.mPreviousHash, nodeIDs); + auto schedule = + computeNextLeaderSchedule(c.second.mPrevious, nodeIDs, true); + auto expected = computeLeaderSchedule( + c.second.mPrevious.previousLedgerHash, nodeIDs); + if (c.second.mPrevious.scpValue.ext.v() != STELLAR_VALUE_BASIC) + { + auto const previousProposer = + getLcValueSignature(c.second.mPrevious.scpValue).nodeID; + auto it = + std::find(expected.begin(), expected.end(), previousProposer); + REQUIRE(it != expected.end()); + std::rotate(it, it + 1, expected.end()); + // so two ledgers in a row are never led by the same node here + REQUIRE(schedule.front() != previousProposer); + } + REQUIRE(schedule == expected); REQUIRE(schedule.size() == 4); REQUIRE(std::find(schedule.begin(), schedule.end(), c.second.mProposer) != schedule.end()); @@ -200,7 +221,8 @@ TEST_CASE("leader schedule fails over when a validator is down", for (auto const& c : closed) { // the schedule covers the whole quorum set, including the dead node - auto schedule = computeLeaderSchedule(c.second.mPreviousHash, allNodes); + auto schedule = + computeNextLeaderSchedule(c.second.mPrevious, allNodes, true); REQUIRE(schedule.size() == 3); // every ledger closed anyway, with a live node's proposal REQUIRE(c.second.mProposer != dead); diff --git a/src/herder/test/LeaderScheduleTests.cpp b/src/herder/test/LeaderScheduleTests.cpp index c5bc3d149..abe4e92ee 100644 --- a/src/herder/test/LeaderScheduleTests.cpp +++ b/src/herder/test/LeaderScheduleTests.cpp @@ -10,6 +10,7 @@ #include "util/XDROperators.h" #include #include +#include #include #include @@ -121,3 +122,295 @@ TEST_CASE("leader schedule is fair across seeds", "[herder][leader-schedule]") REQUIRE(f.second < expectedPair * 1.4); } } + +namespace +{ +// A closed ledger's header as far as the schedule reads it: `proposer` signed +// its value, and it applied transactions or not. +LedgerHeader +closedLedger(uint32_t seq, NodeID const& proposer, bool appliedTransactions, + Hash const& previousLedgerHash) +{ + LedgerHeader header; + header.ledgerSeq = seq; + header.previousLedgerHash = previousLedgerHash; + header.scpValue.ext.v(STELLAR_VALUE_SIGNED); + header.scpValue.ext.lcValueSignature().nodeID = proposer; + header.txSetResultHash = appliedTransactions + ? sha256("some results") + : xdrSha256(TransactionResultSet{}); + return header; +} +} + +TEST_CASE("leader term outcome", "[herder][leader-schedule][leader-term]") +{ + auto nodes = makeNodes(7); + NodeID const proposer = nodes[2]; + Hash const prev = sha256("ledger 100"); + // 101 + 1 is not a multiple of the term length + REQUIRE(LEADER_TERM_LEDGERS == 5); + auto header = closedLedger(101, proposer, true, prev); + + SECTION("the node whose value closed the ledger keeps leading") + { + for (auto const& outcome : + {leaderTermOutcome(header, true), leaderTermOutcome(header, false), + leaderTermOutcome(101, header.scpValue, true)}) + { + REQUIRE(outcome.mIncumbent == proposer); + REQUIRE(!outcome.mForfeited); + } + } + SECTION("millisecond values name their proposer too") + { + header.scpValue.ext.v(STELLAR_VALUE_SIGNED_MS); + header.scpValue.ext.signedMsValue().lcValueSignature.nodeID = nodes[4]; + REQUIRE(leaderTermOutcome(header, true).mIncumbent == nodes[4]); + } + SECTION("a term ends on every multiple of the term length") + { + for (uint32_t seq = 100; seq < 100 + 3 * LEADER_TERM_LEDGERS; ++seq) + { + header.ledgerSeq = seq; + bool const nextStartsTerm = (seq + 1) % LEADER_TERM_LEDGERS == 0; + auto const outcome = leaderTermOutcome(header, true); + REQUIRE(outcome.mIncumbent.has_value() == !nextStartsTerm); + // the end of a term is not a forfeit + REQUIRE(!outcome.mForfeited); + } + } + SECTION("a ledger that applied nothing forfeits the term") + { + // an invalid ("skipped") transaction set, or an empty one + auto skipped = closedLedger(101, proposer, false, prev); + for (auto const& outcome : + {leaderTermOutcome(skipped, true), + leaderTermOutcome(101, skipped.scpValue, false)}) + { + REQUIRE(!outcome.mIncumbent); + REQUIRE(outcome.mForfeited == proposer); + } + // unless ledgers never record results (overlay-only mode) + REQUIRE(leaderTermOutcome(skipped, false).mIncumbent == proposer); + REQUIRE(!leaderTermOutcome(skipped, false).mForfeited); + // also at the end of a term: the new term's order is seeded, and + // must not start with the node whose ledger applied nothing + skipped.ledgerSeq = 104; + REQUIRE(!leaderTermOutcome(skipped, true).mIncumbent); + REQUIRE(leaderTermOutcome(skipped, true).mForfeited == proposer); + } + SECTION("a consensus-level empty-tx-set value forfeits the term") + { + header.scpValue.ext.v(STELLAR_VALUE_EMPTY_TX_SET); + header.scpValue.ext.proposedValue().lcValueSignature.nodeID = proposer; + for (bool haveResults : {true, false}) + { + auto const outcome = leaderTermOutcome(header, haveResults); + REQUIRE(!outcome.mIncumbent); + REQUIRE(outcome.mForfeited == proposer); + } + header.scpValue.ext.v(STELLAR_VALUE_EMPTY_TX_SET_MS); + header.scpValue.ext.proposedMsValue().lcValueSignature.nodeID = + proposer; + REQUIRE(leaderTermOutcome(header, false).mForfeited == proposer); + } + SECTION("an unsigned value names nobody") + { + header.scpValue.ext.v(STELLAR_VALUE_BASIC); + REQUIRE(!leaderTermOutcome(header, true).mIncumbent); + REQUIRE(!leaderTermOutcome(header, true).mForfeited); + } +} + +TEST_CASE("next leader schedule", "[herder][leader-schedule][leader-term]") +{ + auto nodes = makeNodes(9); + NodeID const proposer = nodes[5]; + Hash const prev = sha256("ledger 100"); + auto header = closedLedger(101, proposer, true, prev); + auto const seeded = computeLeaderSchedule(prev, nodes); + auto others = seeded; + others.erase(std::find(others.begin(), others.end(), proposer)); + + SECTION("the incumbent leads round 1, the seeded order follows") + { + auto schedule = computeNextLeaderSchedule(header, nodes, true); + REQUIRE(schedule.size() == nodes.size()); + REQUIRE(schedule[0] == proposer); + REQUIRE(std::vector(schedule.begin() + 1, schedule.end()) == + others); + } + SECTION("the order is seeded by the hash of the ledger before") + { + // Known one ledger early: nothing else of the closed ledger's + // header moves the order. + auto fresh = closedLedger(104, proposer, true, prev); + REQUIRE(computeNextLeaderSchedule(fresh, nodes, true) == seeded); + fresh.txSetResultHash = sha256("other results"); + fresh.scpValue.txSetHash = sha256("other set"); + fresh.bucketListHash = sha256("other state"); + REQUIRE(computeNextLeaderSchedule(fresh, nodes, true) == seeded); + fresh.previousLedgerHash = sha256("another ledger 103"); + REQUIRE(computeNextLeaderSchedule(fresh, nodes, true) == + computeLeaderSchedule(fresh.previousLedgerHash, nodes)); + } + SECTION("a leader that forfeited its term goes last") + { + // whatever the seeded order says, including when it would have put + // that node first + for (auto const& forfeiter : nodes) + { + auto skipped = closedLedger(101, forfeiter, false, prev); + auto schedule = computeNextLeaderSchedule(skipped, nodes, true); + REQUIRE(schedule.size() == nodes.size()); + REQUIRE(schedule.back() == forfeiter); + REQUIRE(schedule.front() != forfeiter); + auto rest = seeded; + rest.erase(std::find(rest.begin(), rest.end(), forfeiter)); + REQUIRE(std::vector(schedule.begin(), schedule.end() - 1) == + rest); + } + } + SECTION("a new term takes the seeded order as it is") + { + auto last = closedLedger(104, proposer, true, prev); + REQUIRE(computeNextLeaderSchedule(last, nodes, true) == seeded); + } + SECTION("unless the last ledger of the old term forfeited") + { + for (auto const& forfeiter : nodes) + { + auto last = closedLedger(104, forfeiter, false, prev); + auto schedule = computeNextLeaderSchedule(last, nodes, true); + REQUIRE(schedule.back() == forfeiter); + REQUIRE(schedule.front() != forfeiter); + } + } + SECTION("an incumbent or forfeiter outside the node set is ignored") + { + auto fewer = nodes; + fewer.erase(std::find(fewer.begin(), fewer.end(), proposer)); + REQUIRE(computeNextLeaderSchedule(header, fewer, true) == + computeLeaderSchedule(prev, fewer)); + auto skipped = closedLedger(101, proposer, false, prev); + REQUIRE(computeNextLeaderSchedule(skipped, fewer, true) == + computeLeaderSchedule(prev, fewer)); + } + SECTION("every node computes the same schedule") + { + for (bool applied : {true, false}) + { + auto closed = closedLedger(101, proposer, applied, prev); + auto schedule = computeNextLeaderSchedule(closed, nodes, true); + for (int i = 0; i < 10; i++) + { + auto shuffled = nodes; + stellar::shuffle(shuffled.begin(), shuffled.end(), + getGlobalRandomEngine()); + REQUIRE(computeNextLeaderSchedule(closed, shuffled, true) == + schedule); + } + } + } + SECTION("the prediction made during the ballot matches the schedule") + { + // While ledger 101 is in its ballot, its header does not exist yet: + // the seed (the hash of ledger 100) and the value do. + REQUIRE( + computeLeaderSchedule( + prev, nodes, leaderTermOutcome(101, header.scpValue, true)) == + computeNextLeaderSchedule(header, nodes, true)); + auto skipped = closedLedger(101, proposer, false, prev); + REQUIRE( + computeLeaderSchedule( + prev, nodes, leaderTermOutcome(101, skipped.scpValue, false)) == + computeNextLeaderSchedule(skipped, nodes, true)); + } +} + +TEST_CASE("leader terms over a chain of ledgers", + "[herder][leader-schedule][leader-term]") +{ + // Every ledger is closed by its round 1 leader, except where + // `roundOneFails` says the leader timed out and round 2's leader closed + // it, or `appliesNothing` says its set was skipped. + size_t const n = 10; + auto nodes = makeNodes(n); + auto run = [&](std::function roundOneFails, + std::function appliesNothing) { + std::map leaders; + std::map proposers; + LedgerHeader lcl = + closedLedger(1, nodes[0], false, sha256("genesis parent")); + for (uint32_t seq = 2; seq < 400; ++seq) + { + auto schedule = computeNextLeaderSchedule(lcl, nodes, true); + REQUIRE(schedule.size() == n); + leaders[seq] = schedule[0]; + auto const& proposer = + roundOneFails(seq) ? schedule[1] : schedule[0]; + proposers[seq] = proposer; + lcl = closedLedger(seq, proposer, !appliesNothing(seq), + xdrSha256(lcl)); + } + return std::make_pair(leaders, proposers); + }; + auto never = [](uint32_t) { return false; }; + + SECTION("a healthy leader leads a whole term, and no longer") + { + auto [leaders, proposers] = run(never, never); + std::set seen; + for (uint32_t seq = 5; seq + LEADER_TERM_LEDGERS < 400; + seq += LEADER_TERM_LEDGERS) + { + for (uint32_t i = 1; i < LEADER_TERM_LEDGERS; ++i) + { + REQUIRE(leaders[seq + i] == leaders[seq]); + } + seen.insert(leaders[seq]); + } + // terms are handed out by the seeded order: everyone gets some + REQUIRE(seen.size() == n); + } + SECTION("a timeout hands the rest of the term to the node that took over") + { + // round 1 fails in the second ledger of every term + auto fails = [](uint32_t seq) { + return seq % LEADER_TERM_LEDGERS == 1; + }; + auto [leaders, proposers] = run(fails, never); + for (uint32_t seq = 10; seq + LEADER_TERM_LEDGERS < 400; + seq += LEADER_TERM_LEDGERS) + { + // the term's first leader led two ledgers, and lost the second + REQUIRE(leaders[seq + 1] == leaders[seq]); + REQUIRE(proposers[seq + 1] != leaders[seq]); + // from the next ledger on, the node that closed it leads + for (uint32_t i = 2; i < LEADER_TERM_LEDGERS; ++i) + { + REQUIRE(leaders[seq + i] == proposers[seq + 1]); + REQUIRE(leaders[seq + i] != leaders[seq]); + } + } + } + SECTION("a ledger that applied nothing rotates the leader at once") + { + auto skipped = [](uint32_t seq) { + return seq % LEADER_TERM_LEDGERS == 2; + }; + auto [leaders, proposers] = run(never, skipped); + for (uint32_t seq = 10; seq + LEADER_TERM_LEDGERS < 400; + seq += LEADER_TERM_LEDGERS) + { + REQUIRE(leaders[seq + 1] == leaders[seq]); + REQUIRE(leaders[seq + 2] == leaders[seq]); + // The ledger after the skipped one is never led by the node + // whose set was skipped, and the node that takes over keeps the + // rest of the term. + REQUIRE(leaders[seq + 3] != leaders[seq]); + REQUIRE(leaders[seq + 4] == leaders[seq + 3]); + } + } +} diff --git a/src/herder/test/PendingEnvelopesTests.cpp b/src/herder/test/PendingEnvelopesTests.cpp index 6b8085ade..5a7ae2d82 100644 --- a/src/herder/test/PendingEnvelopesTests.cpp +++ b/src/herder/test/PendingEnvelopesTests.cpp @@ -123,6 +123,23 @@ createAppWithFollowerPeer(Config const& baseCfg) FollowerPeerApp res; res.mPeer = SecretKey::pseudoRandomForTesting(); Config cfg(baseCfg); + if (i > 0) + { + // A node's place in the leader order is decided by its key. The + // test configuration's key (derived from the instance number and + // the day's seed) can be one that sorts behind nearly every + // other key: no random peer would ever come after it. Try + // another local key as well, not only another peer. + auto const oldKey = cfg.NODE_SEED.getPublicKey(); + cfg.NODE_SEED = SecretKey::pseudoRandomForTesting(); + for (auto& validator : cfg.QUORUM_SET.validators) + { + if (validator == oldKey) + { + validator = cfg.NODE_SEED.getPublicKey(); + } + } + } cfg.QUORUM_SET.validators.emplace_back(res.mPeer.getPublicKey()); res.mClock = std::make_unique(); res.mApp = createTestApplication(*res.mClock, cfg); diff --git a/src/herder/test/ProposalPrevalidationTests.cpp b/src/herder/test/ProposalPrevalidationTests.cpp new file mode 100644 index 000000000..0de8f5eea --- /dev/null +++ b/src/herder/test/ProposalPrevalidationTests.cpp @@ -0,0 +1,1146 @@ +// Copyright 2026 Stellar Development Foundation and contributors. Licensed +// under the Apache License, Version 2.0. See the COPYING file at the root +// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 + +// Tests of the leader's head start on the next transaction set: the state a +// speculative execution leaves is handed to the Herder +// (LedgerManager::startSpeculativeApply), the node that expects to lead the +// 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). + +#include "crypto/SHA.h" +#include "herder/Herder.h" +#include "herder/HerderImpl.h" +#include "herder/LeaderSchedule.h" +#include "herder/LedgerCloseData.h" +#include "herder/TxSetFrame.h" +#include "herder/test/TestTxSetUtils.h" +#include "ledger/ImmutableLedgerView.h" +#include "ledger/LedgerHeaderUtils.h" +#include "ledger/LedgerManager.h" +#include "ledger/test/LedgerTestUtils.h" +#include "main/Application.h" +#include "medida/meter.h" +#include "simulation/Simulation.h" +#include "simulation/Topologies.h" +#include "test/Catch2.h" +#include "test/TestAccount.h" +#include "test/TestUtils.h" +#include "test/TxTests.h" +#include "test/test.h" +#include "util/MetricsRegistry.h" +#include "util/ProtocolVersion.h" +#include "util/XDROperators.h" +#include +#include +#include +#include + +using namespace stellar; +using namespace stellar::txtest; + +namespace stellar +{ +class ProposalPrevalidationTestAccess +{ + public: + // What HerderImpl::maybeStartSpeculativeApply records when the node + // votes for `value`. + static void + setVotedValue(HerderImpl& herder, uint64_t slot, StellarValue const& value) + { + herder.mSpeculativeApplyValue = + std::make_pair(slot, Value(xdr::xdr_to_opaque(value))); + } + + static void + start(HerderImpl& herder, std::shared_ptr state) + { + herder.startProposalPrevalidation(std::move(state)); + } + + static bool + running(HerderImpl& herder) + { + return herder.mProposalPrevalidation != nullptr; + } + + // Waits for the background tasks of the running pre-validation. + static void + waitUntilFinished(HerderImpl& herder) + { + auto job = herder.mProposalPrevalidation; + REQUIRE(job); + auto const deadline = + std::chrono::steady_clock::now() + std::chrono::seconds(60); + while (job->finishedAfterNs.load() < 0) + { + REQUIRE(std::chrono::steady_clock::now() < deadline); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + + static std::pair + build(HerderImpl& herder, uint32_t ledgerSeq, ConsensusTime closeTime) + { + auto built = herder.buildTxSet(ledgerSeq, closeTime, false); + return std::make_pair(built.txSet, std::move(built.applicableTxSet)); + } + + static size_t + cachedResults(HerderImpl& herder) + { + return herder.mValidationCache.results.size(); + } + + static void + clearCache(HerderImpl& herder) + { + herder.mValidationCache = HerderImpl::ValidationCache{}; + } + + static bool + mayLeadAfter(HerderImpl& herder, uint64_t slot, StellarValue const& value, + bool appliedTransactions) + { + return herder.mayLeadAfter(slot, value, appliedTransactions); + } + + static std::chrono::milliseconds + prepareLeadTime(std::chrono::milliseconds expectedCloseTime, + std::chrono::milliseconds lastBuild) + { + return HerderImpl::prepareTxSetLeadTime(expectedCloseTime, lastBuild); + } + + // What the node expects its next build to take, given what it knows. + static std::chrono::milliseconds + expectedBuild(HerderImpl& herder) + { + return herder.expectedTxSetBuildDuration(); + } + + static void + setBuildHistory(HerderImpl& herder, std::chrono::milliseconds lastBuild, + std::chrono::nanoseconds costPerTx) + { + herder.mLastTxSetBuildDuration = lastBuild; + herder.mTxSetBuildCostPerTx = costPerTx; + } + + static void + setLastClosedTxSetSize(HerderImpl& herder, size_t txs) + { + herder.mLastClosedTxSetSize = txs; + } + + static size_t + lastClosedTxSetSize(HerderImpl& herder) + { + return herder.mLastClosedTxSetSize; + } +}; +} + +namespace +{ +using Access = ProposalPrevalidationTestAccess; + +uint32_t const GATE_PROTOCOL = + static_cast(APPLY_TIME_TX_SET_VALIDATION_PROTOCOL_VERSION); + +int64_t +meter(Application& app, std::string const& domain, std::string const& name, + std::string const& unit) +{ + return static_cast( + app.getMetrics().NewMeter({"herder", domain, name}, unit).count()); +} + +int64_t +applyCounter(Application& app, std::string const& name) +{ + return app.getMetrics().NewCounter({"ledger", "apply", name}).count(); +} + +struct CacheCounts +{ + int64_t mHit; + int64_t mMiss; +}; + +CacheCounts +cacheCounts(Application& app) +{ + return CacheCounts{meter(app, "txset", "validation-cache-hit", "tx"), + meter(app, "txset", "validation-cache-miss", "tx")}; +} + +TxSetXDRFrameConstPtr +makeUnvalidatedTxSet(Application& app, + std::vector const& txs) +{ + auto res = testtxset::makeNonValidatedGeneralizedTxSet( + {{std::make_pair(std::nullopt, txs)}, {}}, app, + app.getLedgerManager().getLastClosedLedgerHeader().hash); + REQUIRE(res.first); + return res.first; +} + +TransactionFrameBasePtr +paymentWithSeq(Application& app, TestAccount& from, SequenceNumber seq, + int64_t amount = 1) +{ + return transactionFromOperations( + app, from.getSecretKey(), seq, + {payment(app.getRoot()->getPublicKey(), amount)}, 100); +} + +std::set +txHashes(ApplicableTxSetFrameConstPtr const& txSet) +{ + std::set res; + for (auto const& phase : txSet->getPhases()) + { + for (auto const& tx : phase) + { + res.insert(tx->getFullHash()); + } + } + return res; +} + +std::set +hashesOf(std::vector const& txs) +{ + std::set res; + for (auto const& tx : txs) + { + res.insert(tx->getFullHash()); + } + return res; +} + +// A single validator (so it leads every ledger) whose ledgers apply on the +// ledger apply thread, with four funded accounts. Speculation and +// pre-validation are driven by the test, one step at a time. +struct PrevalidationFixture +{ + VirtualClock mClock; + Config mCfg; + Application::pointer mApp; + std::optional mA; + std::optional mB; + std::optional mC; + std::optional mD; + std::optional mE; + // What the mempool holds; the Herder pulls it in this order. + std::vector mMempool; + + PrevalidationFixture() + : mCfg(getTestConfig(0, Config::TESTDB_BUCKET_DB_PERSISTENT)) + { + mCfg.LEDGER_PROTOCOL_VERSION = GATE_PROTOCOL; + mCfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = GATE_PROTOCOL; + // The tests drive speculation themselves. + mCfg.EXPERIMENTAL_SPECULATIVE_LEDGER_APPLY = false; + REQUIRE(mCfg.backgroundLedgerApply()); + mApp = createTestApplication(mClock, mCfg); + auto root = mApp->getRoot(); + auto const balance = + mApp->getLedgerManager().getLastMinBalance(2) + 10'000'000; + mA.emplace(root->create("A", balance)); + mB.emplace(root->create("B", balance)); + mC.emplace(root->create("C", balance)); + mD.emplace(root->create("D", balance)); + mE.emplace(root->create("E", balance)); + herder().mGetTopTransactionsForTesting = [this](size_t) { + std::vector envelopes; + for (auto const& tx : mMempool) + { + envelopes.push_back(tx->getEnvelope()); + } + return envelopes; + }; + } + + ~PrevalidationFixture() + { + herder().mGetTopTransactionsForTesting = nullptr; + } + + HerderImpl& + herder() + { + return static_cast(mApp->getHerder()); + } + + LedgerManager& + lm() + { + return mApp->getLedgerManager(); + } + + uint32_t + nextSeq() + { + return lm().getLastClosedLedgerNum() + 1; + } + + ConsensusTime + nextCloseTime() + { + return ConsensusTime::fromApplyTime(ApplyTime::fromTimePoint( + lm().getLastClosedLedgerHeader().header.scpValue.closeTime + 1)); + } + + StellarValue + valueFor(TxSetXDRFrameConstPtr const& txSet, + xdr::xvector const& upgrades = emptyUpgradeSteps) + { + return herder().makeStellarValue(txSet->getContentsHash(), + nextCloseTime(), upgrades, + mCfg.NODE_SEED); + } + + // Executes the next ledger with `txSet` on the apply thread without + // closing it, as a node does when it votes for the value, and returns + // the state the execution left. + std::shared_ptr + speculate(TxSetXDRFrameConstPtr const& txSet, bool prevalidate, + xdr::xvector const& upgrades = emptyUpgradeSteps) + { + auto const value = valueFor(txSet, upgrades); + // By the time a node votes, SCP has asked for the slot's leaders, + // which brings the tracked quorum (the node set) up to date. + REQUIRE(herder().getHerderSCPDriver().getLeaderSchedule(nextSeq()) == + std::vector{mCfg.NODE_SEED.getPublicKey()}); + Access::setVotedValue(herder(), nextSeq(), value); + std::shared_ptr executed; + bool called = false; + lm().startSpeculativeApply( + LedgerCloseData(nextSeq(), txSet, value), + [&](std::shared_ptr state) { + called = true; + executed = state; + if (prevalidate) + { + Access::start(herder(), state); + } + }); + while (lm().isSpeculativelyApplying()) + { + mClock.crank(true); + } + REQUIRE(called); + return executed; + } + + // Externalizes the value `speculate` used for `txSet`, which closes the + // ledger. + void + externalize(TxSetXDRFrameConstPtr const& txSet) + { + mApp->getHerder().externalizeValue(txSet, nextSeq(), nextCloseTime(), + emptyUpgradeSteps); + } + + std::set + build() + { + auto built = Access::build(herder(), nextSeq(), nextCloseTime()); + REQUIRE(built.second); + return txHashes(built.second); + } +}; + +AccountEntry +accountIn(AbstractLedgerView const& view, TestAccount& acc) +{ + auto entry = view.getAccount(acc.getPublicKey()); + REQUIRE(entry); + return entry.current().data.account(); +} +} + +TEST_CASE("speculative apply hands over the state it executed", + "[ledger][speculative-apply][prevalidation]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& lm = f.lm(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + auto const aBalance = a.getBalance(); + auto const bSeq = b.getLastSequenceNumber(); + auto const seq = f.nextSeq(); + auto const lclBefore = lm.getLastClosedLedgerHeader(); + + SECTION("a ledger that applies") + { + auto tx = paymentWithSeq(app, a, aSeq + 1); + auto txSet = makeUnvalidatedTxSet(app, {tx}); + auto const value = f.valueFor(txSet); + auto state = f.speculate(txSet, false); + REQUIRE(state); + REQUIRE(state->mLedgerSeq == seq); + REQUIRE(state->mValue == value); + REQUIRE(!state->mTxSetSkipped); + REQUIRE(state->mAppliedTxs == 1); + REQUIRE(state->mTxHashes->size() == 1); + REQUIRE(state->mTxHashes->count(tx->getFullHash()) == 1); + REQUIRE(state->mHeader->ledgerSeq == seq); + REQUIRE(state->mHeader->scpValue == value); + REQUIRE(state->mHeader->previousLedgerHash == lclBefore.hash); + + // The view shows the ledger as executed: A paid and its sequence + // number moved; B is read through from the previous ledger. + auto view = state->makeView(app.getMetrics()); + REQUIRE(view->getLedgerHeader().current().ledgerSeq == seq); + auto const executedA = accountIn(*view, a); + REQUIRE(executedA.seqNum == aSeq + 1); + REQUIRE(executedA.balance == aBalance - 100 - 1); + REQUIRE(accountIn(*view, b).seqNum == bSeq); + // ... while the last closed ledger is untouched. + REQUIRE(lm.getLastClosedLedgerHeader().hash == lclBefore.hash); + REQUIRE(accountIn(*lm.getLCLView(), a).seqNum == aSeq); + + // Once the value externalizes, the closed ledger holds what the + // captured state said, and the state itself did not move. + f.externalize(txSet); + REQUIRE(lm.getLastClosedLedgerNum() == seq); + auto const closedA = accountIn(*lm.getLCLView(), a); + REQUIRE(closedA.seqNum == executedA.seqNum); + REQUIRE(closedA.balance == executedA.balance); + REQUIRE(accountIn(*view, a).seqNum == aSeq + 1); + REQUIRE(applyCounter(app, "speculation-hit") == 1); + } + + SECTION("an erased entry") + { + auto merge = transactionFromOperations( + app, a.getSecretKey(), aSeq + 1, + {accountMerge(app.getRoot()->getPublicKey())}, 100); + auto state = f.speculate(makeUnvalidatedTxSet(app, {merge}), false); + REQUIRE(state); + REQUIRE(state->mAppliedTxs == 1); + auto view = state->makeView(app.getMetrics()); + REQUIRE(!view->getAccount(a.getPublicKey())); + REQUIRE(lm.getLCLView()->getAccount(a.getPublicKey())); + } + + SECTION("a ledger whose transaction set is invalid") + { + auto good = paymentWithSeq(app, a, aSeq + 1); + auto bad = paymentWithSeq(app, b, bSeq + 10); + auto state = f.speculate(makeUnvalidatedTxSet(app, {good, bad}), false); + REQUIRE(state); + REQUIRE(state->mTxSetSkipped); + REQUIRE(state->mAppliedTxs == 0); + REQUIRE(state->mTxHashes->empty()); + auto view = state->makeView(app.getMetrics()); + REQUIRE(accountIn(*view, a).seqNum == aSeq); + REQUIRE(accountIn(*view, a).balance == aBalance); + } + + SECTION("a ledger that is not the next one is not executed") + { + auto txSet = + makeUnvalidatedTxSet(app, {paymentWithSeq(app, a, aSeq + 1)}); + auto const value = f.valueFor(txSet); + bool called = false; + std::shared_ptr state; + lm.startSpeculativeApply( + LedgerCloseData(seq + 1, txSet, value), + [&](std::shared_ptr s) { + called = true; + state = s; + }); + while (lm.isSpeculativelyApplying()) + { + f.mClock.crank(true); + } + REQUIRE(called); + REQUIRE(!state); + } +} + +TEST_CASE("leader pre-validates the candidates of the next ledger", + "[herder][txset][prevalidation]") +{ + 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 const aSeq = a.getLastSequenceNumber(); + auto const bSeq = b.getLastSequenceNumber(); + auto const cSeq = c.getLastSequenceNumber(); + auto const dSeq = d.getLastSequenceNumber(); + + // The ledger being closed (N) holds a payment of A and one of D. + auto closingA = paymentWithSeq(app, a, aSeq + 1); + auto closingD = paymentWithSeq(app, d, dSeq + 1); + // Candidates for the ledger after it: + // * A's next payment: only valid once N has applied, + // * another payment of D with the sequence number N consumes: valid + // before N, not after, + // * B's payment: valid either way, + // * C's payment with a sequence number too far ahead: never valid. + 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); + REQUIRE(staleD->getFullHash() != closingD->getFullHash()); + + auto closing = makeUnvalidatedTxSet(app, {closingA, closingD}); + // N's transactions are still in the mempool while N is in its ballot. + f.mMempool = {closingA, closingD, nextA, staleD, plainB, futureC}; + auto const startedBefore = + meter(app, "prevalidate", "started", "prevalidation"); + auto const usedBefore = meter(app, "prevalidate", "used", "prevalidation"); + auto const discardedBefore = + meter(app, "prevalidate", "discarded", "prevalidation"); + + auto state = f.speculate(closing, true); + REQUIRE(state); + REQUIRE(Access::running(herder)); + REQUIRE(meter(app, "prevalidate", "started", "prevalidation") == + startedBefore + 1); + Access::waitUntilFinished(herder); + + SECTION("the build takes the results when the executed value closes") + { + f.externalize(closing); + // The overlay drops a closed ledger's transactions. + f.mMempool = {nextA, staleD, plainB, futureC}; + auto const before = cacheCounts(app); + auto const built = f.build(); + REQUIRE(built == hashesOf({nextA, plainB})); + auto const after = cacheCounts(app); + // nothing was validated on the main thread + REQUIRE(after.mHit - before.mHit == 4); + REQUIRE(after.mMiss - before.mMiss == 0); + REQUIRE(meter(app, "prevalidate", "used", "prevalidation") == + usedBefore + 1); + REQUIRE(!Access::running(herder)); + + // The same set comes out without the results... + Access::clearCache(herder); + auto const rebuiltBefore = cacheCounts(app); + REQUIRE(f.build() == built); + auto const rebuilt = cacheCounts(app); + REQUIRE(rebuilt.mHit - rebuiltBefore.mHit == 0); + REQUIRE(rebuilt.mMiss - rebuiltBefore.mMiss == 4); + // ... and a later build on the same ledger only validates what is + // new to it. + auto lateB = paymentWithSeq(app, c, cSeq + 1); + f.mMempool.push_back(lateB); + auto const lateBefore = cacheCounts(app); + // C now has two candidates; the lower sequence number is kept. + REQUIRE(f.build() == hashesOf({nextA, plainB, lateB})); + auto const late = cacheCounts(app); + REQUIRE(late.mHit - lateBefore.mHit == 3); + REQUIRE(late.mMiss - lateBefore.mMiss == 1); + } + + SECTION("the results are dropped when another value closes the ledger") + { + // B's payment closes the ledger instead: A and D did not move. + auto other = makeUnvalidatedTxSet(app, {plainB}); + f.externalize(other); + f.mMempool = {closingA, closingD, staleD, futureC, plainB}; + auto const before = cacheCounts(app); + // With the stale results, B's consumed payment would be proposed + // again. + auto const built = f.build(); + REQUIRE(built.size() == 2); + REQUIRE(built.count(closingA->getFullHash()) == 1); + REQUIRE(built.count(plainB->getFullHash()) == 0); + REQUIRE(built.count(futureC->getFullHash()) == 0); + auto const after = cacheCounts(app); + REQUIRE(after.mHit - before.mHit == 0); + REQUIRE(meter(app, "prevalidate", "used", "prevalidation") == + usedBefore); + REQUIRE(meter(app, "prevalidate", "discarded", "prevalidation") == + discardedBefore + 1); + REQUIRE(!Access::running(herder)); + } + + SECTION("the results are not kept past the ledger they are for") + { + f.externalize(closing); + f.mMempool = {nextA, staleD, plainB, futureC}; + REQUIRE(f.build() == hashesOf({nextA, plainB})); + REQUIRE(Access::cachedResults(herder) == 4); + // the next ledger closes; a build on top of it starts from scratch + f.externalize(makeUnvalidatedTxSet(app, {nextA})); + f.mMempool = {plainB, futureC}; + auto const before = cacheCounts(app); + REQUIRE(f.build() == hashesOf({plainB})); + auto const after = cacheCounts(app); + REQUIRE(after.mHit - before.mHit == 0); + REQUIRE(after.mMiss - before.mMiss == 2); + REQUIRE(Access::cachedResults(herder) == 2); + } +} + +TEST_CASE("pre-validation is not started when its results could not be used", + "[herder][txset][prevalidation]") +{ + 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 good = paymentWithSeq(app, a, aSeq + 1); + f.mMempool = {good, paymentWithSeq(app, b, bSeq + 1)}; + + SECTION("the executed set was invalid") + { + auto bad = paymentWithSeq(app, b, bSeq + 10); + auto state = f.speculate(makeUnvalidatedTxSet(app, {good, bad}), true); + REQUIRE(state); + REQUIRE(state->mTxSetSkipped); + REQUIRE(!Access::running(herder)); + } + SECTION("the node votes for another value by the time it has executed") + { + auto txSet = makeUnvalidatedTxSet(app, {good}); + auto const value = f.valueFor(txSet); + auto otherValue = value; + otherValue.closeTime += 1; + Access::setVotedValue(herder, f.nextSeq(), value); + std::shared_ptr executed; + f.lm().startSpeculativeApply( + LedgerCloseData(f.nextSeq(), txSet, value), + [&](std::shared_ptr state) { + executed = state; + Access::setVotedValue(herder, f.nextSeq(), otherValue); + Access::start(herder, state); + }); + while (f.lm().isSpeculativelyApplying()) + { + f.mClock.crank(true); + } + REQUIRE(executed); + REQUIRE(!Access::running(herder)); + } + SECTION("the executed value carries upgrades") + { + // Upgrades are applied when the ledger commits, after the state + // the candidates would be validated on. + LedgerUpgrade upgrade(LEDGER_UPGRADE_BASE_FEE); + upgrade.newBaseFee() = 200; + xdr::xvector upgrades; + upgrades.emplace_back(LedgerTestUtils::toUpgradeType(upgrade)); + auto state = + f.speculate(makeUnvalidatedTxSet(app, {good}), true, upgrades); + REQUIRE(state); + REQUIRE(!state->mValue.upgrades.empty()); + REQUIRE(!Access::running(herder)); + } + SECTION("there is nothing in the mempool") + { + auto txSet = makeUnvalidatedTxSet(app, {good}); + f.mMempool.clear(); + REQUIRE(f.speculate(txSet, true)); + REQUIRE(!Access::running(herder)); + } +} + +// What ledger N does to an account decides whether its next transaction is +// valid; the pre-validated results must say what validating on the closed +// ledger says, for every way N can touch a candidate's accounts. +TEST_CASE("builds with and without pre-validated results agree", + "[herder][txset][prevalidation]") +{ + 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): the first is valid after N, the + // second waits for it, + // * D's next payment: its sequence number is right after N, but D can + // no longer pay the fee, + // * E's next payment: E does not exist after N, + // * B's payment with C paying the fee (a fee bump): valid. + 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); + REQUIRE(!txValidityDependsOnCloseTime(bumped->getEnvelope())); + + f.mMempool = {closingA, drainD, mergeE, nextA, + laterA, brokeD, goneE, bumped}; + auto state = f.speculate(closing, true); + REQUIRE(state); + REQUIRE(state->mAppliedTxs == 3); + REQUIRE(Access::running(herder)); + Access::waitUntilFinished(herder); + f.externalize(closing); + // N applied as the test assumes + REQUIRE(!f.lm().getLCLView()->getAccount(e.getPublicKey())); + REQUIRE(a.getLastSequenceNumber() == aSeq + 1); + REQUIRE(d.getLastSequenceNumber() == dSeq + 1); + + f.mMempool = {nextA, laterA, brokeD, goneE, bumped}; + auto const before = cacheCounts(app); + auto const withResults = f.build(); + auto const after = cacheCounts(app); + // A's later payment is set aside before validation (one transaction per + // source account); the other four come from the pre-validation. + REQUIRE(after.mHit - before.mHit == 4); + REQUIRE(after.mMiss - before.mMiss == 0); + REQUIRE(withResults == hashesOf({nextA, bumped})); + + Access::clearCache(herder); + auto const withoutBefore = cacheCounts(app); + auto const withoutResults = f.build(); + auto const withoutAfter = cacheCounts(app); + REQUIRE(withoutAfter.mHit - withoutBefore.mHit == 0); + REQUIRE(withoutAfter.mMiss - withoutBefore.mMiss == 4); + REQUIRE(withoutResults == withResults); +} + +TEST_CASE("a running pre-validation does not outlive its use", + "[herder][txset][prevalidation]") +{ + auto f = std::make_unique(); + auto& app = *f->mApp; + auto& herder = f->herder(); + auto& a = *f->mA; + auto const aSeq = a.getLastSequenceNumber(); + auto closing = + makeUnvalidatedTxSet(app, {paymentWithSeq(app, a, aSeq + 1)}); + // Enough candidates to keep the background tasks busy for a moment. + auto next = paymentWithSeq(app, a, aSeq + 2); + for (int i = 0; i < 400; ++i) + { + f->mMempool.push_back(next); + } + REQUIRE(f->speculate(closing, true)); + REQUIRE(Access::running(herder)); + + SECTION("the application shuts down") + { + // Destroyed with the job possibly mid-flight: no hang, no crash. + f.reset(); + } + SECTION("the build takes what is there and does not wait") + { + f->externalize(closing); + f->mMempool = {next}; + REQUIRE(f->build() == hashesOf({next})); + REQUIRE(!Access::running(herder)); + auto const counts = cacheCounts(app); + // whether or not the job had got to it, the candidate was validated + // exactly once + REQUIRE(counts.mHit + counts.mMiss >= 1); + } + SECTION("another ledger closes on top") + { + // Another leader's value closes the ledger and this node moves on + // without ever building on it. + f->externalize(closing); + f->externalize(makeUnvalidatedTxSet(app, {next})); + REQUIRE(!Access::running(herder)); + REQUIRE(Access::cachedResults(herder) == 0); + } +} + +TEST_CASE("transactions whose validity depends on the close time", + "[herder][txset][prevalidation]") +{ + TransactionEnvelope v1(ENVELOPE_TYPE_TX); + auto& cond = v1.v1().tx.cond; + REQUIRE(!txValidityDependsOnCloseTime(v1)); + + SECTION("time bounds") + { + cond.type(PRECOND_TIME); + // present but unbounded + REQUIRE(!txValidityDependsOnCloseTime(v1)); + cond.timeBounds().maxTime = 1000; + REQUIRE(txValidityDependsOnCloseTime(v1)); + cond.timeBounds().maxTime = 0; + cond.timeBounds().minTime = 5; + REQUIRE(txValidityDependsOnCloseTime(v1)); + } + SECTION("v2 preconditions") + { + cond.type(PRECOND_V2); + // ledger bounds and sequence conditions depend on the ledger, not + // on its close time + cond.v2().ledgerBounds.activate().maxLedger = 100; + cond.v2().minSeqNum.activate() = 7; + cond.v2().minSeqLedgerGap = 3; + REQUIRE(!txValidityDependsOnCloseTime(v1)); + SECTION("minimum sequence age") + { + cond.v2().minSeqAge = 10; + REQUIRE(txValidityDependsOnCloseTime(v1)); + } + SECTION("time bounds") + { + cond.v2().timeBounds.activate().maxTime = 99; + REQUIRE(txValidityDependsOnCloseTime(v1)); + } + } + SECTION("v0 envelopes") + { + TransactionEnvelope v0(ENVELOPE_TYPE_TX_V0); + REQUIRE(!txValidityDependsOnCloseTime(v0)); + v0.v0().tx.timeBounds.activate().maxTime = 1; + REQUIRE(txValidityDependsOnCloseTime(v0)); + } + SECTION("fee bumps follow their inner transaction") + { + TransactionEnvelope bump(ENVELOPE_TYPE_TX_FEE_BUMP); + bump.feeBump().tx.innerTx.type(ENVELOPE_TYPE_TX); + REQUIRE(!txValidityDependsOnCloseTime(bump)); + auto& inner = bump.feeBump().tx.innerTx.v1().tx.cond; + inner.type(PRECOND_TIME); + inner.timeBounds().maxTime = 1000; + REQUIRE(txValidityDependsOnCloseTime(bump)); + } +} + +TEST_CASE("time-bounded candidates are validated by the build itself", + "[herder][txset][prevalidation]") +{ + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto& b = *f.mB; + auto const aSeq = a.getLastSequenceNumber(); + + auto closing = + makeUnvalidatedTxSet(app, {paymentWithSeq(app, a, aSeq + 1)}); + auto plain = paymentWithSeq(app, a, aSeq + 2); + // valid for an hour from the last close + PreconditionsV2 cond; + cond.timeBounds.activate().maxTime = + f.lm().getLastClosedLedgerHeader().header.scpValue.closeTime + 3600; + auto bounded = transactionWithV2Precondition(app, b, 1, 100, cond); + REQUIRE(txValidityDependsOnCloseTime(bounded->getEnvelope())); + f.mMempool = {plain, bounded}; + + REQUIRE(f.speculate(closing, true)); + Access::waitUntilFinished(herder); + f.externalize(closing); + + auto const before = cacheCounts(app); + REQUIRE(f.build() == hashesOf({plain, bounded})); + auto const after = cacheCounts(app); + REQUIRE(after.mHit - before.mHit == 1); + REQUIRE(after.mMiss - before.mMiss == 1); + // and its result is not kept for a build with another close time + REQUIRE(Access::cachedResults(herder) == 1); +} + +TEST_CASE("the first-round leader builds shortly before the trigger", + "[herder][txset][prevalidation]") +{ + using namespace std::chrono; + auto lead = [](int closeTimeMs, int lastBuildMs) { + return Access::prepareLeadTime(milliseconds(closeTimeMs), + milliseconds(lastBuildMs)); + }; + SECTION("before the node has built anything") + { + // a quarter of the target close time + REQUIRE(lead(800, 0) == milliseconds(200)); + REQUIRE(lead(1000, 0) == milliseconds(250)); + // capped: a long close time does not make transactions wait longer + REQUIRE(lead(5000, 0) == milliseconds(250)); + REQUIRE(lead(0, 0) == milliseconds(0)); + } + SECTION("long enough for a build like the last one and its push") + { + // 180 ms + a quarter + 100 ms for the set to reach the peers + REQUIRE(lead(800, 180) == milliseconds(325)); + REQUIRE(lead(1000, 240) == milliseconds(400)); + // a quick build does not shorten it + REQUIRE(lead(800, 20) == milliseconds(200)); + REQUIRE(lead(5000, 20) == milliseconds(250)); + // on a long close time a slow build still gets its room + REQUIRE(lead(5000, 600) == milliseconds(850)); + } + SECTION("never more than half of the target close time") + { + REQUIRE(lead(800, 400) == milliseconds(400)); + REQUIRE(lead(800, 5000) == milliseconds(400)); + REQUIRE(lead(1000, 500) == milliseconds(500)); + } +} + +// A node that starts a term has not built for a while: its last build says +// little about the next one. What the last closed ledger held does. +TEST_CASE("the lead time of the first build of a term follows the size of the " + "last ledger", + "[herder][txset][prevalidation]") +{ + using namespace std::chrono; + PrevalidationFixture f; + auto& app = *f.mApp; + auto& herder = f.herder(); + auto& a = *f.mA; + auto lead = [&](int closeTimeMs) { + return Access::prepareLeadTime(milliseconds(closeTimeMs), + Access::expectedBuild(herder)); + }; + + SECTION("the herder learns the size of every ledger that closes") + { + auto const aSeq = a.getLastSequenceNumber(); + f.externalize( + makeUnvalidatedTxSet(app, {paymentWithSeq(app, a, aSeq + 1)})); + REQUIRE(Access::lastClosedTxSetSize(herder) == 1); + f.externalize(makeUnvalidatedTxSet(app, {})); + REQUIRE(Access::lastClosedTxSetSize(herder) == 0); + } + SECTION("a node that never built under load") + { + // its last build was of an empty set, long ago + Access::setBuildHistory(herder, milliseconds(1), nanoseconds(0)); + Access::setLastClosedTxSetSize(herder, 0); + REQUIRE(Access::expectedBuild(herder) == milliseconds(1)); + REQUIRE(lead(800) == milliseconds(200)); + // the network now closes ledgers of 9,300 transactions: at the + // default cost per transaction that is a build of 372 ms, more than + // the 200 ms a lead time sized for the old build would leave + Access::setLastClosedTxSetSize(herder, 9300); + REQUIRE(Access::expectedBuild(herder) == milliseconds(372)); + REQUIRE(lead(800) == milliseconds(400)); + // smaller ledgers, smaller head start + Access::setLastClosedTxSetSize(herder, 2000); + REQUIRE(Access::expectedBuild(herder) == milliseconds(80)); + REQUIRE(lead(800) == milliseconds(200)); + REQUIRE(lead(2000) == milliseconds(250)); + } + SECTION("a node that has measured its own builds") + { + // 180 ms for 6,400 transactions: about 28 us each + Access::setBuildHistory(herder, milliseconds(180), nanoseconds(28'125)); + Access::setLastClosedTxSetSize(herder, 6400); + REQUIRE(Access::expectedBuild(herder) == milliseconds(180)); + REQUIRE(lead(800) == milliseconds(325)); + // ledgers grew since its last build: sized for them, at its own rate + Access::setLastClosedTxSetSize(herder, 9600); + REQUIRE(Access::expectedBuild(herder) == milliseconds(270)); + REQUIRE(lead(800) == milliseconds(400)); + // ledgers shrank: the last build still counts until the next one + Access::setLastClosedTxSetSize(herder, 100); + REQUIRE(Access::expectedBuild(herder) == milliseconds(180)); + } + SECTION("a build records what it cost per transaction") + { + // a set too small to say anything leaves the rate alone + Access::setBuildHistory(herder, milliseconds(0), nanoseconds(777)); + f.mMempool = {paymentWithSeq(app, a, a.getLastSequenceNumber() + 1)}; + REQUIRE(f.build().size() == 1); + Access::setLastClosedTxSetSize(herder, 1'000'000); + REQUIRE(Access::expectedBuild(herder) == milliseconds(777)); + } +} + +// A network whose ledgers all carry a transaction: the node whose value +// closed a ledger leads the next one, for up to LEADER_TERM_LEDGERS ledgers, +// and leaders build from pre-validated candidates. +TEST_CASE("leaders keep their term and pre-validate in a network", + "[herder][leader-schedule][leader-term][prevalidation][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; + return cfg; + }); + auto nodes = simulation->getNodes(); + auto nodeIDs = simulation->getNodeIDs(); + simulation->startAllNodes(); + + // Every node's mempool holds the root account's next payments: one fits + // each ledger (one transaction per source account), and the one after it + // only becomes valid when that ledger has applied. + 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(); + static_cast(app->getHerder()) + .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; + }; + } + + // Watch node 1 close ledgers. + 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 insideTerm = 0; + size_t incumbentLed = 0; + size_t incumbentClosed = 0; + std::set proposers; + simulation->crankUntil( + [&]() { + auto const cur = lm.getLastClosedLedgerHeader(); + if (cur.header.ledgerSeq == prev.header.ledgerSeq) + { + return false; + } + REQUIRE(cur.header.ledgerSeq == prev.header.ledgerSeq + 1); + auto const proposer = + getLcValueSignature(cur.header.scpValue).nodeID; + proposers.insert(proposer); + if (cur.header.txSetResultHash != emptyResults) + { + ++withTransactions; + } + auto const schedule = + computeNextLeaderSchedule(prev.header, nodeIDs, true); + REQUIRE(schedule.size() == 4); + // (the genesis ledger has no proposer) + bool const prevApplied = + prev.header.txSetResultHash != emptyResults && + prev.header.scpValue.ext.v() != STELLAR_VALUE_BASIC; + if (cur.header.ledgerSeq % LEADER_TERM_LEDGERS != 0 && prevApplied) + { + // inside a term: the node that closed the previous ledger + // leads this one + ++insideTerm; + auto const incumbent = + getLcValueSignature(prev.header.scpValue).nodeID; + if (schedule[0] == incumbent) + { + ++incumbentLed; + } + if (proposer == incumbent) + { + ++incumbentClosed; + } + } + else if (cur.header.ledgerSeq % LEADER_TERM_LEDGERS == 0 && + prevApplied) + { + // a new term: the seeded order, known a ledger early + REQUIRE(schedule == + computeLeaderSchedule(prev.header.previousLedgerHash, + nodeIDs)); + } + // The node itself derives the next ledger's leaders from the + // header it just closed. + auto& driver = static_cast(nodes[1]->getHerder()) + .getHerderSCPDriver(); + REQUIRE(driver.getLeaderSchedule(cur.header.ledgerSeq + 1) == + computeNextLeaderSchedule(cur.header, nodeIDs, true)); + 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 timeouts = 0; + int64_t prevalidationsUsed = 0; + int64_t cacheHits = 0; + int64_t speculationHits = 0; + for (auto const& node : nodes) + { + auto& metrics = node->getMetrics(); + timeouts += static_cast( + metrics.NewHistogram({"scp", "timeout", "leader"}).sum() + + metrics.NewHistogram({"scp", "timeout", "prepare"}).sum()); + prevalidationsUsed += + meter(*node, "prevalidate", "used", "prevalidation"); + cacheHits += meter(*node, "txset", "validation-cache-hit", "tx"); + speculationHits += applyCounter(*node, "speculation-hit"); + // no leader proposed a set that apply found invalid + REQUIRE(applyCounter(*node, "txset-skipped") == 0); + } + INFO(fmt::format("ledgers with txs {}, inside a term {}, incumbent led " + "{}, incumbent closed {}, timeouts {}, prevalidations " + "used {}, cache hits {}, speculation hits {}", + withTransactions, insideTerm, incumbentLed, + incumbentClosed, timeouts, prevalidationsUsed, cacheHits, + speculationHits)); + // Ledgers carried the payments, one each. + REQUIRE(withTransactions >= 30); + REQUIRE(paid == static_cast(withTransactions)); + // Inside a term the incumbent always leads round 1, and closes the + // ledger unless a round timed out (e.g. on a busy test machine). + REQUIRE(insideTerm >= 20); + REQUIRE(incumbentLed == insideTerm); + REQUIRE(static_cast(insideTerm - incumbentClosed) <= timeouts); + REQUIRE(incumbentClosed * 4 >= insideTerm * 3); + // Terms end: leadership still rotates. + REQUIRE(proposers.size() >= 2); + // Leaders executed their own ledgers speculatively and built the next + // set from candidates validated on that state. + REQUIRE(speculationHits > 0); + REQUIRE(prevalidationsUsed > 0); + REQUIRE(cacheHits > 0); + + for (auto const& node : nodes) + { + static_cast(node->getHerder()) + .mGetTopTransactionsForTesting = nullptr; + } +} diff --git a/src/herder/test/TxFloodTargetsTests.cpp b/src/herder/test/TxFloodTargetsTests.cpp new file mode 100644 index 000000000..5e637728f --- /dev/null +++ b/src/herder/test/TxFloodTargetsTests.cpp @@ -0,0 +1,971 @@ +// Copyright 2026 Stellar Development Foundation and contributors. Licensed +// under the Apache License, Version 2.0. See the COPYING file at the root +// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 + +// Tests of transaction dissemination to the upcoming round leaders: a node +// checks a submitted transaction against its last closed ledger, keeps it, +// and has its overlay send it to the leaders of the current and the next +// round only (HerderImpl::computeTxFloodTargets) instead of flooding it to +// every peer. + +#include "crypto/SHA.h" +#include "herder/Herder.h" +#include "herder/HerderImpl.h" +#include "herder/LeaderSchedule.h" +#include "ledger/LedgerManager.h" +#include "main/Application.h" +#include "overlay/RustOverlayManager.h" +#include "simulation/LoadGenerator.h" +#include "simulation/Simulation.h" +#include "simulation/Topologies.h" +#include "test/Catch2.h" +#include "test/TestAccount.h" +#include "test/TestUtils.h" +#include "test/TxTests.h" +#include "test/test.h" +#include "transactions/TransactionUtils.h" +#include "util/BatchExecutor.h" +#include "util/Logging.h" +#include "util/MetricsRegistry.h" +#include +#include + +using namespace stellar; +using namespace stellar::txtest; + +namespace +{ +int const NUM_ACCOUNTS = 40; +// Config::TX_FLOOD_TARGET_LEADERS's default. +uint32_t const TARGET_LEADERS = 2; + +Config +acceleratedConfig(int configNum) +{ + auto cfg = getTestConfig(configNum); + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; + cfg.GENESIS_TEST_ACCOUNT_COUNT = NUM_ACCOUNTS; + return cfg; +} + +HerderImpl& +herderOf(Application::pointer const& app) +{ + return static_cast(app->getHerder()); +} + +// Network totals of the transaction dissemination counters. +struct TxFloodCounts +{ + // Bodies sent straight to a target, and received without a demand. + int64_t mDirectSent{0}; + int64_t mDirectRecv{0}; + // The flood: adverts sent, bodies demanded, own transactions flooded. + int64_t mAdvertised{0}; + int64_t mDemanded{0}; + int64_t mLocalFlooded{0}; + + std::string + toString() const + { + return fmt::format( + "direct sent {} received {}; advertised {} demanded {} flooded {}", + mDirectSent, mDirectRecv, mAdvertised, mDemanded, mLocalFlooded); + } +}; + +TxFloodCounts +countTxFlood(std::vector const& nodes) +{ + TxFloodCounts res; + for (auto const& app : nodes) + { + app->getOverlayManager().syncOverlayMetrics(); + auto& m = app->getOverlayManager().getOverlayMetrics(); + res.mDirectSent += static_cast(m.mTxDirectSent.count()); + res.mDirectRecv += static_cast(m.mTxDirectRecv.count()); + res.mAdvertised += + static_cast(m.mSendFloodAdvertMeter.count()); + res.mDemanded += static_cast(m.mMessagesDemanded.count()); + res.mLocalFlooded += static_cast(m.mTxLocalFlooded.count()); + } + return res; +} + +int64_t +identifiedPeers(Application::pointer const& app) +{ + app->getOverlayManager().syncOverlayMetrics(); + return app->getOverlayManager() + .getOverlayMetrics() + .mNodeIdIdentified.count(); +} + +// Whether the slot starts a new leader term (LeaderSchedule.h: every +// multiple of LEADER_TERM_LEDGERS does). +bool +startsLeaderTerm(uint64_t slot) +{ + return slot % LEADER_TERM_LEDGERS == 0; +} + +// Whether `update.targets` is what the leader schedule gives for the state +// the node was in. Computed from the LCL alone, with the schedule functions +// (not with the Herder's own schedule cache): +// 1. the leaders of the node's round of the slot after the LCL and of the +// round after it, then +// 2. if the slot after that starts a new leader term: the first node of the +// order seeded with the LCL's hash, unless it is in the list already. +bool +isScheduledTargetList(HerderImpl::TxFloodTargetUpdate const& update, + std::vector const& validators) +{ + if (!update.targets) + { + return false; + } + REQUIRE(update.slot == update.lcl.header.ledgerSeq + 1); + auto const schedule = computeNextLeaderSchedule( + update.lcl.header, validators, /*ledgersHaveResults=*/true); + REQUIRE(schedule.size() == validators.size()); + std::vector expected; + auto const round = std::max(1, update.round); + for (uint64_t r = round; r < uint64_t(round) + TARGET_LEADERS; ++r) + { + auto const& leader = schedule[(r - 1) % schedule.size()]; + if (std::find(expected.begin(), expected.end(), leader) != + expected.end()) + { + break; + } + expected.emplace_back(leader); + } + if (startsLeaderTerm(update.slot + 1)) + { + auto const next = computeLeaderSchedule(update.lcl.hash, validators); + if (std::find(expected.begin(), expected.end(), next.front()) == + expected.end()) + { + expected.emplace_back(next.front()); + } + } + return *update.targets == expected; +} + +// What the lists a node sent say about the first leader of each new term, +// one ledger ahead: for every list sent with the LCL at N, where N+2 starts +// a term and the list of N+2's real leaders is known (a list sent with the +// LCL at N+1), whether it named the node that leads round 1 of N+2. The +// prediction is only wrong when ledger N+1 cost that node its turn (a +// ledger that applies nothing sends its own leader to the end). +struct TermPredictions +{ + int mRight = 0; + int mWrong = 0; + int mWrongWithoutForfeit = 0; +}; + +TermPredictions +checkTermPredictions(std::deque const& history, + std::vector const& validators) +{ + std::map headers; + for (auto const& update : history) + { + headers[update.lcl.header.ledgerSeq] = update.lcl.header; + } + TermPredictions res; + for (auto const& update : history) + { + auto next = headers.find(update.lcl.header.ledgerSeq + 1); + if (!update.targets || !startsLeaderTerm(update.slot + 1) || + next == headers.end()) + { + continue; + } + auto const real = computeNextLeaderSchedule( + next->second, validators, /*ledgersHaveResults=*/true); + if (std::find(update.targets->begin(), update.targets->end(), + real.front()) != update.targets->end()) + { + ++res.mRight; + } + else + { + ++res.mWrong; + if (!leaderTermOutcome(next->second, true).mForfeited) + { + ++res.mWrongWithoutForfeit; + } + } + } + return res; +} + +std::vector +validatorsOf(std::vector const& nodes) +{ + std::vector res; + for (auto const& app : nodes) + { + res.emplace_back(app->getConfig().NODE_SEED.getPublicKey()); + } + return res; +} + +// Whether every node closed ledger `seq`. (Simulation::haveAllExternalized +// throws once the nodes are more than a few ledgers past the number it is +// asked about, which happens here while waiting for something else.) +bool +allClosed(std::vector const& nodes, uint32_t seq) +{ + return std::all_of(nodes.begin(), nodes.end(), [&](auto const& app) { + return app->getLedgerManager().getLastClosedLedgerNum() >= seq; + }); +} + +int64_t +appliedTxs(Application::pointer const& app) +{ + return static_cast( + app->getMetrics() + .NewCounter({"ledger", "apply", "success"}) + .count()); +} +} + +TEST_CASE("transactions go to the round leaders only", + "[herder][tx-flood-targets][simulation]") +{ + // 4 validators, every node connected to every other one. + auto simulation = + Topologies::core(4, 0.75, sha256(getTestConfig().NETWORK_PASSPHRASE), + acceleratedConfig); + simulation->startAllNodes(); + auto nodes = simulation->getNodes(); + + // Let the network close a few ledgers, every overlay learn which + // validator each of its peers is, and every Herder publish targets. + simulation->crankUntil( + [&]() { + if (!allClosed(nodes, 4)) + { + return false; + } + for (auto const& app : nodes) + { + if (identifiedPeers(app) != 3 || + !herderOf(app).getTxFloodTargetsForTesting()) + { + return false; + } + } + return true; + }, + std::chrono::seconds(120), false); + for (auto const& app : nodes) + { + REQUIRE(identifiedPeers(app) == 3); + } + auto const before = countTxFlood(nodes); + INFO("before: " << before.toString()); + // Nothing was submitted so far. + REQUIRE(before.mDirectSent == 0); + REQUIRE(before.mAdvertised == 0); + + // The one function that decides: the leaders of two rounds of the slot + // after the LCL (two of the four nodes) and, one ledger before a new + // leader term, that term's first leader. + for (auto const& app : nodes) + { + auto& herder = herderOf(app); + auto const state = herder.describeTxFloodTargetsForTesting( + herder.computeTxFloodTargets()); + REQUIRE(state.targets); + REQUIRE(isScheduledTargetList(state, validatorsOf(nodes))); + REQUIRE(state.targets->size() >= TARGET_LEADERS); + REQUIRE(state.targets->size() <= + TARGET_LEADERS + (startsLeaderTerm(state.slot + 1) ? 1 : 0)); + } + + // Submit payments at every node over several ledgers, each from its own + // account. A node is often not a leader for the ledger it submits in. + auto const appliedBefore = appliedTxs(nodes[0]); + int submitted = 0; + auto const startLedger = + nodes[0]->getLedgerManager().getLastClosedLedgerNum(); + for (int batch = 0; batch < 8; ++batch) + { + for (size_t n = 0; n < nodes.size(); ++n) + { + auto source = getGenesisAccount(*nodes[n], submitted); + auto dest = getGenesisAccount(*nodes[n], NUM_ACCOUNTS - 1); + auto tx = source.tx({payment(dest.getPublicKey(), 1000)}); + REQUIRE(nodes[n]->getHerder().recvTransaction(tx, false) == + TxSubmitStatus::TX_STATUS_PENDING); + ++submitted; + } + auto const next = + nodes[0]->getLedgerManager().getLastClosedLedgerNum() + 1; + simulation->crankUntil( + [&]() { return allClosed(nodes, next); }, + std::chrono::seconds(60), false); + } + REQUIRE(submitted < NUM_ACCOUNTS - 1); + + // Every transaction is applied on every node, although only the leaders + // ever saw it outside a tx set. + simulation->crankUntil( + [&]() { + for (auto const& app : nodes) + { + if (appliedTxs(app) - appliedBefore < submitted) + { + return false; + } + } + return true; + }, + std::chrono::seconds(120), false); + for (auto const& app : nodes) + { + REQUIRE(appliedTxs(app) - appliedBefore == submitted); + } + // Go on until a node was sent a list with a third node: the first + // leader of a new term that is not one of the two round leaders of the + // ledger before it (every second term start, with four nodes). + auto sawNextTermLeader = [&]() { + for (auto const& app : nodes) + { + for (auto const& update : + herderOf(app).mTxFloodTargetHistoryForTesting) + { + if (update.targets && update.targets->size() > TARGET_LEADERS) + { + return true; + } + } + } + return false; + }; + for (int i = 0; i < 150 && !sawNextTermLeader(); ++i) + { + auto const next = + nodes[0]->getLedgerManager().getLastClosedLedgerNum() + 1; + simulation->crankUntil([&]() { return allClosed(nodes, next); }, + std::chrono::seconds(60), false); + } + auto const ledgers = + nodes[0]->getLedgerManager().getLastClosedLedgerNum() - startLedger; + + auto const after = countTxFlood(nodes); + INFO("after: " << after.toString() << ", " << submitted + << " transactions in " << ledgers << " ledgers"); + // The network is a full mesh of identified validators, so nothing was + // advertised, demanded or flooded: every transaction was sent as a body + // to the targets. + REQUIRE(after.mAdvertised == 0); + REQUIRE(after.mDemanded == 0); + REQUIRE(after.mLocalFlooded == 0); + // At least one send per transaction that its submitter did not lead + // itself; at most three targets (two round leaders and the next term's + // first leader) per ledger it stayed pending. + REQUIRE(after.mDirectSent > 0); + // Nothing is sent twice to a peer within the test's run time (60 s + // between sends), so every body sent is one body received. + REQUIRE(after.mDirectRecv == after.mDirectSent); + REQUIRE(after.mDirectSent <= + static_cast(submitted) * (TARGET_LEADERS + 1) * + (ledgers + 1)); + + // Every target list a node sent to its overlay during the run is what + // the schedule gives for the state the node was in: two nodes, and a + // third one only in the ledger before a new term. The test ran through + // at least one term start. + int checked = 0; + int beforeNewTerm = 0; + int withNextTermLeader = 0; + TermPredictions predictions; + for (auto const& app : nodes) + { + for (auto const& update : + herderOf(app).mTxFloodTargetHistoryForTesting) + { + if (!update.targets) + { + continue; + } + INFO(fmt::format("slot {}, round {}, {} targets", update.slot, + update.round, update.targets->size())); + REQUIRE(isScheduledTargetList(update, validatorsOf(nodes))); + ++checked; + if (startsLeaderTerm(update.slot + 1)) + { + ++beforeNewTerm; + withNextTermLeader += + update.targets->size() > TARGET_LEADERS ? 1 : 0; + } + else + { + REQUIRE(update.targets->size() <= TARGET_LEADERS); + } + } + auto const p = checkTermPredictions( + herderOf(app).mTxFloodTargetHistoryForTesting, + validatorsOf(nodes)); + predictions.mRight += p.mRight; + predictions.mWrong += p.mWrong; + predictions.mWrongWithoutForfeit += p.mWrongWithoutForfeit; + } + INFO(fmt::format("target lists checked {}, in the ledger before a new " + "term {} ({} with a third node); the new term's first " + "leader was in the list {} times, not {} times ({} " + "without a forfeit)", + checked, beforeNewTerm, withNextTermLeader, + predictions.mRight, predictions.mWrong, + predictions.mWrongWithoutForfeit)); + REQUIRE(beforeNewTerm > 0); + REQUIRE(withNextTermLeader > 0); + // A node that was told one ledger ahead who leads the new term was told + // right, unless the ledger in between cost that node its turn. + REQUIRE(predictions.mRight > 0); + REQUIRE(predictions.mWrongWithoutForfeit == 0); +} + +TEST_CASE("transactions go to every validator when no leader count is set", + "[herder][tx-flood-targets][simulation]") +{ + // TX_FLOOD_TARGET_LEADERS = 0: every validator is a target. Still no + // adverts and no demands, and every transaction goes to each of the + // three other nodes exactly once. + auto simulation = Topologies::core( + 4, 0.75, sha256(getTestConfig().NETWORK_PASSPHRASE), [](int i) { + auto cfg = acceleratedConfig(i); + cfg.TX_FLOOD_TARGET_LEADERS = 0; + return cfg; + }); + simulation->startAllNodes(); + auto nodes = simulation->getNodes(); + simulation->crankUntil( + [&]() { + if (!allClosed(nodes, 4)) + { + return false; + } + for (auto const& app : nodes) + { + if (identifiedPeers(app) != 3 || + !herderOf(app).getTxFloodTargetsForTesting()) + { + return false; + } + } + return true; + }, + std::chrono::seconds(120), false); + for (auto const& app : nodes) + { + auto targets = herderOf(app).computeTxFloodTargets(); + REQUIRE(targets); + REQUIRE(targets->size() == 4); + REQUIRE(std::is_sorted(targets->begin(), targets->end())); + // The same list whatever the slot, so it was sent once. + REQUIRE(herderOf(app).mTxFloodTargetHistoryForTesting.size() == 1); + } + + auto const appliedBefore = appliedTxs(nodes[0]); + int submitted = 0; + for (int batch = 0; batch < 5; ++batch) + { + for (size_t n = 0; n < nodes.size(); ++n) + { + auto source = getGenesisAccount(*nodes[n], submitted); + auto dest = getGenesisAccount(*nodes[n], NUM_ACCOUNTS - 1); + auto tx = source.tx({payment(dest.getPublicKey(), 1000)}); + REQUIRE(nodes[n]->getHerder().recvTransaction(tx, false) == + TxSubmitStatus::TX_STATUS_PENDING); + ++submitted; + } + auto const next = + nodes[0]->getLedgerManager().getLastClosedLedgerNum() + 1; + simulation->crankUntil([&]() { return allClosed(nodes, next); }, + std::chrono::seconds(60), false); + } + simulation->crankUntil( + [&]() { + for (auto const& app : nodes) + { + if (appliedTxs(app) - appliedBefore < submitted) + { + return false; + } + } + return true; + }, + std::chrono::seconds(120), false); + auto const counts = countTxFlood(nodes); + INFO(counts.toString()); + REQUIRE(counts.mAdvertised == 0); + REQUIRE(counts.mDemanded == 0); + REQUIRE(counts.mLocalFlooded == 0); + REQUIRE(counts.mDirectSent == 3 * static_cast(submitted)); + REQUIRE(counts.mDirectRecv == counts.mDirectSent); + for (auto const& app : nodes) + { + REQUIRE(appliedTxs(app) - appliedBefore == submitted); + } +} + +TEST_CASE("transactions are included while a leader is down", + "[herder][tx-flood-targets][simulation]") +{ + // 3 validators, threshold 2, one never started. With leader terms the + // node that closed a ledger leads the next one too, so the dead node + // only leads round 1 when a term ends (every fifth ledger, or after a + // ledger that applied nothing), but it is the round 2 leader of about + // every other slot. A target that is not a connected, identified peer + // makes the live nodes flood, and the round moves the targets on. + Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); + auto simulation = + std::make_shared(networkID, acceleratedConfig); + std::vector keys; + for (int i = 0; i < 3; i++) + { + keys.push_back( + SecretKey::fromSeed(sha256("NODE_SEED_" + std::to_string(i)))); + } + SCPQuorumSet qSet; + qSet.threshold = 2; + std::vector allNodes; + for (auto const& k : keys) + { + qSet.validators.push_back(k.getPublicKey()); + allNodes.push_back(k.getPublicKey()); + } + simulation->addNode(keys[0], qSet); + simulation->addNode(keys[1], qSet); + simulation->addPendingConnection(keys[0].getPublicKey(), + keys[1].getPublicKey()); + simulation->startAllNodes(); + auto nodes = simulation->getNodes(); + simulation->crankUntil( + [&]() { return allClosed(nodes, 4); }, + std::chrono::seconds(300), false); + + auto const appliedBefore = appliedTxs(nodes[0]); + int submitted = 0; + // Whether a node sent its overlay a target list at round 2 or later. + auto sawLaterRound = [&]() { + for (auto const& app : nodes) + { + for (auto const& update : + herderOf(app).mTxFloodTargetHistoryForTesting) + { + if (update.targets && update.round >= 2 && + update.round != UINT32_MAX) + { + return true; + } + } + } + return false; + }; + // 10 ledgers with transactions. Then ledgers without any, until the + // dead node led a round 1: an empty ledger ends its leader's term, and + // the dead node is the next slot's first leader half of the time. + for (int batch = 0; batch < 60 && (batch < 10 || !sawLaterRound()); + ++batch) + { + for (size_t n = 0; batch < 10 && n < nodes.size(); ++n) + { + auto source = getGenesisAccount(*nodes[n], submitted); + auto dest = getGenesisAccount(*nodes[n], NUM_ACCOUNTS - 1); + auto tx = source.tx({payment(dest.getPublicKey(), 1000)}); + REQUIRE(nodes[n]->getHerder().recvTransaction(tx, false) == + TxSubmitStatus::TX_STATUS_PENDING); + ++submitted; + } + auto const next = + nodes[0]->getLedgerManager().getLastClosedLedgerNum() + 1; + simulation->crankUntil([&]() { return allClosed(nodes, next); }, + std::chrono::seconds(120), false); + } + simulation->crankUntil( + [&]() { + return appliedTxs(nodes[0]) - appliedBefore >= submitted && + appliedTxs(nodes[1]) - appliedBefore >= submitted; + }, + std::chrono::seconds(300), false); + REQUIRE(appliedTxs(nodes[0]) - appliedBefore == submitted); + REQUIRE(appliedTxs(nodes[1]) - appliedBefore == submitted); + + // Every target list a node sent to its overlay is what the schedule + // gives for the state the node was in then: see isScheduledTargetList. + // Some lists were sent at round 2 or later. + int checked = 0; + int laterRounds = 0; + TermPredictions predictions; + for (auto const& app : nodes) + { + for (auto const& update : + herderOf(app).mTxFloodTargetHistoryForTesting) + { + if (!update.targets) + { + continue; + } + INFO(fmt::format("slot {}, round {}, {} targets", update.slot, + update.round, update.targets->size())); + REQUIRE(isScheduledTargetList(update, allNodes)); + ++checked; + if (update.round >= 2 && update.round != UINT32_MAX) + { + ++laterRounds; + } + } + auto const p = checkTermPredictions( + herderOf(app).mTxFloodTargetHistoryForTesting, allNodes); + predictions.mRight += p.mRight; + predictions.mWrong += p.mWrong; + predictions.mWrongWithoutForfeit += p.mWrongWithoutForfeit; + } + INFO(fmt::format("target lists checked {}, at round 2 or later {}; a " + "new term's first leader was in the list one ledger " + "ahead {} times, not {} times ({} without a forfeit)", + checked, laterRounds, predictions.mRight, + predictions.mWrong, predictions.mWrongWithoutForfeit)); + // An update is only sent when the targets change, so there are fewer + // than one per node and ledger. + REQUIRE(checked >= 10); + REQUIRE(laterRounds > 0); + REQUIRE(predictions.mWrongWithoutForfeit == 0); + auto const counts = countTxFlood(nodes); + INFO(counts.toString()); + // Both paths were used: direct while both targets were alive, the flood + // while the dead node was one of them. + REQUIRE(counts.mLocalFlooded > 0); +} + +TEST_CASE("no transaction is included twice in overlay-only mode", + "[herder][tx-flood-targets][simulation]") +{ + // Overlay-only mode (the mode of the network-level load tests) closes + // ledgers without applying them, so nothing rejects a transaction that + // a leader includes a second time: every copy that reaches a mempool + // after its transaction externalized shows up as a duplicate. + auto simulation = Topologies::core( + 4, 0.75, sha256(getTestConfig().NETWORK_PASSPHRASE), [](int i) { + auto cfg = acceleratedConfig(i); + cfg.ARTIFICIALLY_GENERATE_LOAD_FOR_TESTING = true; + cfg.GENESIS_TEST_ACCOUNT_COUNT = 2000; + return cfg; + }); + simulation->startAllNodes(); + auto nodes = simulation->getNodes(); + simulation->crankUntil( + [&]() { + if (!allClosed(nodes, 4)) + { + return false; + } + for (auto const& app : nodes) + { + if (identifiedPeers(app) != 3 || + !herderOf(app).getTxFloodTargetsForTesting()) + { + return false; + } + } + return true; + }, + std::chrono::seconds(120), false); + for (auto const& app : nodes) + { + app->setRunInOverlayOnlyMode(true); + } + + uint32_t const accountsPerNode = 400; + uint32_t const txsPerNode = 300; + auto completedRuns = [](Application& app) { + return app.getMetrics() + .NewMeter({"loadgen", "run", "complete"}, "run") + .count(); + }; + std::vector before; + for (size_t i = 0; i < nodes.size(); ++i) + { + before.emplace_back(completedRuns(*nodes[i])); + nodes[i]->getLoadGenerator().generateLoad(GeneratedLoadConfig::txLoad( + LoadGenMode::PAY, accountsPerNode, txsPerNode, /* txRate */ 10, + /* offset */ static_cast(i) * accountsPerNode)); + } + + // Every ledger node 0 closes: its transactions, by full hash. + std::map firstIn; + std::vector twice; + size_t included = 0; + uint32_t ledgers = 0; + uint32_t missed = 0; + uint32_t lastSeq = nodes[0]->getLedgerManager().getLastClosedLedgerNum(); + auto record = [&]() { + auto const& lcl = nodes[0]->getLedgerManager().getLastClosedLedgerHeader(); + if (lcl.header.ledgerSeq == lastSeq) + { + return; + } + missed += lcl.header.ledgerSeq - lastSeq - 1; + lastSeq = lcl.header.ledgerSeq; + auto res = herderOf(nodes[0]).getTxSet(lcl.header.scpValue.txSetHash); + auto const* txSet = std::get_if(&res); + if (!txSet || !*txSet) + { + return; + } + ++ledgers; + for (auto const& hash : (*txSet)->getTransactionFullHashes()) + { + ++included; + auto [it, isNew] = firstIn.emplace(hash, lastSeq); + if (!isNew) + { + twice.emplace_back(fmt::format( + "{} in ledger {} (slot % 5 = {}) and again in {} (slot % 5 " + "= {})", + hexAbbrev(hash), it->second, it->second % 5, lastSeq, + lastSeq % 5)); + } + } + }; + auto loadDone = [&]() { + for (size_t i = 0; i < nodes.size(); ++i) + { + if (completedRuns(*nodes[i]) != before[i] + 1) + { + return false; + } + } + return true; + }; + auto deadline = + std::chrono::steady_clock::now() + std::chrono::seconds(150); + while (!loadDone() && std::chrono::steady_clock::now() < deadline) + { + simulation->crankForAtMost(std::chrono::milliseconds(20), false); + record(); + } + bool const completed = loadDone(); + // A few more ledgers: a late copy is included by whoever leads next. + auto const last = nodes[0]->getLedgerManager().getLastClosedLedgerNum() + 12; + deadline = std::chrono::steady_clock::now() + std::chrono::seconds(60); + while (!allClosed(nodes, last) && + std::chrono::steady_clock::now() < deadline) + { + simulation->crankForAtMost(std::chrono::milliseconds(20), false); + record(); + } + std::string loadgen; + for (auto const& app : nodes) + { + auto& m = app->getMetrics(); + loadgen += fmt::format( + " [attempted {} rejected {} complete {} failed {}]", + m.NewMeter({"loadgen", "txn", "attempted"}, "txn").count(), + m.NewMeter({"loadgen", "txn", "rejected"}, "txn").count(), + m.NewMeter({"loadgen", "run", "complete"}, "run").count(), + m.NewMeter({"loadgen", "run", "failed"}, "run").count()); + } + + std::string list; + for (size_t i = 0; i < twice.size() && i < 40; ++i) + { + list += "\n " + twice[i]; + } + INFO(fmt::format("{} ledgers with transactions ({} not seen), {} " + "inclusions of {} distinct transactions, {} submitted; " + "loadgen per node:{}; included twice: {}{}", + ledgers, missed, included, firstIn.size(), + nodes.size() * txsPerNode, loadgen, twice.size(), list)); + REQUIRE(completed); + REQUIRE(missed == 0); + REQUIRE(firstIn.size() == nodes.size() * txsPerNode); + REQUIRE(twice.empty()); +} + +TEST_CASE("experiment options: legacy adverts and batch task count", + "[herder][tx-flood-targets]") +{ + VirtualClock clock; + auto cfg = getTestConfig(); + cfg.TX_FLOOD_LEGACY_ADVERTS = true; + cfg.BATCH_EXECUTOR_TASKS = 7; + auto app = createTestApplication(clock, cfg); + // Without the option a single node's target list is itself (see the + // last section of the next test); with it there is no list at all, and + // the overlay floods with adverts as before. + REQUIRE(!herderOf(app).computeTxFloodTargets()); + // Seven validation tasks whatever the machine's physical core count. + REQUIRE(app->getBatchExecutor().preferredTaskCount() == 7); +} + +TEST_CASE("a node checks a transaction before it sends it on", + "[herder][tx-flood-targets]") +{ + VirtualClock clock; + auto cfg = getTestConfig(); + auto app = createTestApplication(clock, cfg); + auto& herder = herderOf(app); + auto& lm = app->getLedgerManager(); + auto root = app->getRoot(); + auto const balance = lm.getLastMinBalance(2) + 10'000'000; + auto a = root->create("a", balance); + auto b = root->create("b", balance); + auto c = root->create("c", balance); + auto submit = [&](TransactionFrameBasePtr const& tx) { + return herder.recvTransaction(tx, false); + }; + auto& invalid = + app->getMetrics().NewMeter({"herder", "tx-admission", "invalid"}, "tx"); + auto& busy = app->getMetrics().NewMeter( + {"herder", "tx-admission", "account-busy"}, "tx"); + + SECTION("a valid transaction is accepted, once per source account") + { + auto tx1 = a.tx({payment(b, 100)}); + REQUIRE(submit(tx1) == TxSubmitStatus::TX_STATUS_PENDING); + REQUIRE(submit(tx1) == TxSubmitStatus::TX_STATUS_DUPLICATE); + // Another transaction of the same account, valid on its own against + // the last closed ledger (same sequence number): only one of the + // two can apply. + auto tx2 = a.tx({payment(c, 100)}, tx1->getSeqNum()); + REQUIRE(tx2->getFullHash() != tx1->getFullHash()); + REQUIRE(submit(tx2) == TxSubmitStatus::TX_STATUS_TRY_AGAIN_LATER); + REQUIRE(busy.count() == 1); + // Other accounts are not affected. + REQUIRE(submit(b.tx({payment(a, 100)})) == + TxSubmitStatus::TX_STATUS_PENDING); + REQUIRE(invalid.count() == 0); + + SECTION("until it is externalized") + { + closeLedger(*app, {tx1}); + auto tx3 = a.tx({payment(c, 100)}); + REQUIRE(tx3->getSeqNum() == tx1->getSeqNum() + 1); + REQUIRE(submit(tx3) == TxSubmitStatus::TX_STATUS_PENDING); + } + SECTION("or another transaction of the account externalizes") + { + // tx2 was refused here, but reached the ledger through another + // node: tx1 can no longer apply, and the account is free. + closeLedger(*app, {tx2}); + auto tx3 = a.tx({payment(c, 100)}, tx2->getSeqNum() + 1); + REQUIRE(submit(tx3) == TxSubmitStatus::TX_STATUS_PENDING); + } + SECTION("or the sequence number is used up some other way") + { + // Another account's transaction bumps a's sequence number past + // tx1's. No transaction of a externalizes, but tx1 is dead. + auto bump = c.tx({a.op(bumpSequence(tx1->getSeqNum() + 5))}); + bump->addSignature(a.getSecretKey()); + closeLedger(*app, {bump}); + REQUIRE(a.getLastSequenceNumber() == tx1->getSeqNum() + 5); + auto tx3 = a.tx({payment(c, 100)}); + REQUIRE(submit(tx3) == TxSubmitStatus::TX_STATUS_PENDING); + REQUIRE(busy.count() == 1); + } + SECTION("an unrelated ledger does not free the account") + { + closeLedger(*app, {c.tx({payment(a, 100)})}); + REQUIRE(submit(tx2) == TxSubmitStatus::TX_STATUS_TRY_AGAIN_LATER); + } + SECTION("or it times out") + { + clock.setCurrentVirtualTime( + clock.now() + HerderImpl::LOCAL_TX_PENDING_TIMEOUT - + std::chrono::seconds(1)); + REQUIRE(submit(tx2) == TxSubmitStatus::TX_STATUS_TRY_AGAIN_LATER); + clock.setCurrentVirtualTime(clock.now() + std::chrono::seconds(1)); + REQUIRE(submit(tx2) == TxSubmitStatus::TX_STATUS_PENDING); + } + } + + SECTION("an invalid transaction is refused") + { + auto expectInvalid = [&](TransactionFrameBasePtr const& tx) { + auto const count = invalid.count(); + REQUIRE(submit(tx) == TxSubmitStatus::TX_STATUS_ERROR); + REQUIRE(invalid.count() == count + 1); + }; + SECTION("bad signature") + { + auto tx = a.tx({payment(b, 100)}); + tx->getMutableEnvelope().v1().signatures.clear(); + tx->addSignature(b.getSecretKey()); + expectInvalid(tx); + } + SECTION("sequence number already used") + { + expectInvalid(a.tx({payment(b, 100)}, a.getLastSequenceNumber())); + } + SECTION("sequence number in the future") + { + expectInvalid( + a.tx({payment(b, 100)}, a.getLastSequenceNumber() + 2)); + } + SECTION("fee too low") + { + auto tx = transactionFromOperations( + *app, a.getSecretKey(), a.nextSequenceNumber(), + {payment(b, 100)}, 1); + expectInvalid(tx); + } + SECTION("no such account") + { + auto tx = transactionFromOperations( + *app, getAccount("ghost"), 1, {payment(b, 100)}); + expectInvalid(tx); + } + // A refused transaction does not block its account. + REQUIRE( + submit(a.tx({payment(b, 100)}, a.getLastSequenceNumber() + 1)) == + TxSubmitStatus::TX_STATUS_PENDING); + } + + SECTION("pregenerated load skips the validity check") + { + auto tx = a.tx({payment(b, 100)}); + tx->getMutableEnvelope().v1().signatures.clear(); + REQUIRE(herder.recvTransaction(tx, false, /*isLoadgenTx=*/true) == + TxSubmitStatus::TX_STATUS_PENDING); + REQUIRE(invalid.count() == 0); + } + + SECTION("overlay-only mode does not limit an account to one transaction") + { + app->setRunInOverlayOnlyMode(true); + auto tx1 = a.tx({payment(b, 100)}); + auto tx2 = a.tx({payment(c, 100)}); + REQUIRE(submit(tx1) == TxSubmitStatus::TX_STATUS_PENDING); + // The sequence-number check is off in this mode too (checkValid). + REQUIRE(submit(tx2) == TxSubmitStatus::TX_STATUS_PENDING); + // The validity check still runs. + auto tx3 = b.tx({payment(a, 100)}); + tx3->getMutableEnvelope().v1().signatures.clear(); + REQUIRE(submit(tx3) == TxSubmitStatus::TX_STATUS_ERROR); + app->setRunInOverlayOnlyMode(false); + } + + SECTION("the targets of a single node, and of one that lost sync") + { + // A node that is its own quorum leads every round: it is the only + // target, so nothing is sent anywhere. + auto targets = herder.computeTxFloodTargets(); + REQUIRE(targets); + REQUIRE(*targets == + std::vector{app->getConfig().NODE_SEED.getPublicKey()}); + // Without the network's latest ledger the node does not know who + // leads: everyone. + herder.lostSync(); + REQUIRE(!herder.computeTxFloodTargets()); + REQUIRE(!herder.getTxFloodTargetsForTesting()); + } +} diff --git a/src/ledger/ImmutableLedgerView.cpp b/src/ledger/ImmutableLedgerView.cpp index 46161215a..e76f08a91 100644 --- a/src/ledger/ImmutableLedgerView.cpp +++ b/src/ledger/ImmutableLedgerView.cpp @@ -454,4 +454,29 @@ ApplyLedgerView::ApplyLedgerView(ImmutableLedgerDataPtr state, : ImmutableLedgerView(std::move(state), metrics) { } + +std::unique_ptr +ExecutedLedgerState::makeView(MetricsRegistry& metrics) const +{ + releaseAssert(mHeader && mPreviousLedger && mChanges); + return std::make_unique( + mHeader, + [changes = mChanges](LedgerKey const& key) + -> std::optional> { + if (key.type() != ACCOUNT) + { + // Not captured: the previous ledger's version could be + // stale. + throw std::logic_error( + "executed ledger state only holds account entries"); + } + auto it = changes->find(key); + if (it == changes->end()) + { + return std::nullopt; + } + return it->second; + }, + ApplyLedgerView(mPreviousLedger, metrics)); +} } diff --git a/src/ledger/ImmutableLedgerView.h b/src/ledger/ImmutableLedgerView.h index 86e9d713c..eaf760ebe 100644 --- a/src/ledger/ImmutableLedgerView.h +++ b/src/ledger/ImmutableLedgerView.h @@ -274,6 +274,60 @@ class SorobanPreApplyLedgerView : public AbstractLedgerView ApplyLedgerView mLclView; }; +// The ledger state right after a ledger was executed (fees charged, +// transactions applied) and before it is committed: the snapshot of the +// ledger before it plus the entries the execution changed. The commit adds +// what the ledger's upgrades change, eviction, and the header's hashes; up to +// those, this is the state the ledger leaves behind. +// +// Everything in here is immutable and shared, so the state can be read from +// any thread, each through a view of its own (`makeView`). It is captured by +// the ledger apply thread from a speculative execution (see +// LedgerManager::startSpeculativeApply) for the node that expects to lead +// the next ledger, which validates its candidate transactions against it +// while the executed ledger is still going through consensus. +// +// Only what transaction validation reads is kept: the header and the account +// entries the execution changed. A view of this state refuses (throws on) any +// other kind of entry rather than answer with the previous ledger's version. +// The Soroban network configuration is the previous ledger's. What +// validation reads of it only changes with an upgrade, and upgrades are +// applied by the commit, so a state captured for a value that carries +// upgrades must not be used. (The commit also rewrites bookkeeping entries of +// the configuration on every ledger, the state size window and the eviction +// iterator; validation does not read those: from protocol 23 it uses the flat +// write fee.) +struct ExecutedLedgerState +{ + // Account entries the execution created or changed; nullptr for erased + // ones. + using Changes = UnorderedMap>; + + uint32_t mLedgerSeq{0}; + // The value the ledger was executed with. + StellarValue mValue; + // True if the value's transaction set was found invalid, so that the + // ledger applies nothing (see LedgerManagerImpl::validateTxSetForApply). + bool mTxSetSkipped{false}; + // Number of transactions the ledger applied. In the test-only + // overlay-only mode, which applies nothing: the size of its set. + size_t mAppliedTxs{0}; + // The ledger's header as execution left it: sequence number, value and + // fee pool are final; upgrades and the hashes computed by the commit are + // not in it. + std::shared_ptr mHeader; + // Snapshot of the ledger before. + ImmutableLedgerDataPtr mPreviousLedger; + std::shared_ptr mChanges; + // Full hashes of the transactions of the ledger's set. + std::shared_ptr const> mTxHashes; + + // A read-only view of the state, for validating transactions. Each view + // is for one thread. + std::unique_ptr + makeView(MetricsRegistry& metrics) const; +}; + // Immutable wrapper for a complete ledger state snapshot. // This object provides read-only access to all components of a full ledger // state at a specific ledger sequence. All components are instantiated together diff --git a/src/ledger/LedgerManager.h b/src/ledger/LedgerManager.h index 483820858..74d18659a 100644 --- a/src/ledger/LedgerManager.h +++ b/src/ledger/LedgerManager.h @@ -18,6 +18,7 @@ namespace stellar { class LedgerCloseData; +struct ExecutedLedgerState; class Database; class SorobanMetricsRegistry; struct SorobanApplyMetrics; @@ -440,7 +441,22 @@ class LedgerManager // commit the result. If another value externalizes, or another ledger is // handed in, the execution is dropped. Must not be called while a ledger // is applying. - virtual void startSpeculativeApply(LedgerCloseData const& ledgerData) = 0; + // + // With `onExecuted`, the apply thread captures the state the execution + // left (see ExecutedLedgerState) and hands it to `onExecuted` on the main + // thread once the execution has finished; nullptr if the ledger was not + // executed after all. The state stays valid whatever becomes of the + // execution: whether it describes the last closed ledger is for the + // caller to check, by comparing the value that externalized. + using SpeculativeApplyExecuted = + std::function)>; + virtual void startSpeculativeApply(LedgerCloseData const& ledgerData, + SpeculativeApplyExecuted onExecuted) = 0; + void + startSpeculativeApply(LedgerCloseData const& ledgerData) + { + startSpeculativeApply(ledgerData, SpeculativeApplyExecuted{}); + } // True while the apply thread may be executing a ledger speculatively. // Like isApplying(), this tells the main thread that the resources ledger // apply uses (e.g. the batch executor) are taken. diff --git a/src/ledger/LedgerManagerImpl.cpp b/src/ledger/LedgerManagerImpl.cpp index e29a98981..abf426eaa 100644 --- a/src/ledger/LedgerManagerImpl.cpp +++ b/src/ledger/LedgerManagerImpl.cpp @@ -1869,7 +1869,8 @@ LedgerManagerImpl::applyLedger(LedgerCloseData const& ledgerData, } void -LedgerManagerImpl::startSpeculativeApply(LedgerCloseData const& ledgerData) +LedgerManagerImpl::startSpeculativeApply(LedgerCloseData const& ledgerData, + SpeculativeApplyExecuted onExecuted) { releaseAssert(threadIsMain()); releaseAssert(mApp.getConfig().backgroundLedgerApply()); @@ -1878,12 +1879,18 @@ LedgerManagerImpl::startSpeculativeApply(LedgerCloseData const& ledgerData) ++mSpeculationsExecuting; mApplyState.getMetrics().mSpeculationStarted.inc(); mApp.postOnLedgerApplyThread( - [this, ledgerData]() { - speculativelyExecuteLedger(ledgerData); + [this, ledgerData, onExecuted = std::move(onExecuted)]() mutable { + auto state = speculativelyExecuteLedger( + ledgerData, static_cast(onExecuted)); mApp.postOnMainThread( - [this]() { + [this, state = std::move(state), + onExecuted = std::move(onExecuted)]() { releaseAssert(mSpeculationsExecuting > 0); --mSpeculationsExecuting; + if (onExecuted) + { + onExecuted(state); + } }, "speculative apply executed"); }, @@ -1919,8 +1926,63 @@ LedgerManagerImpl::abortSpeculativeApply() future.wait(); } -void -LedgerManagerImpl::speculativelyExecuteLedger(LedgerCloseData const& ledgerData) +std::shared_ptr +LedgerManagerImpl::captureExecutedState(LedgerExecution const& execution) const +{ + ZoneScoped; + auto state = std::make_shared(); + auto const& ledgerData = execution.mLedgerData; + state->mLedgerSeq = ledgerData.getLedgerSeq(); + state->mValue = ledgerData.getValue(); + state->mTxSetSkipped = execution.mTxSetSkipped; + { + // Not left active: the commit loads the header again. + auto header = execution.mLtx->loadHeader(); + state->mHeader = std::make_shared(header.current()); + } + state->mPreviousLedger = mApplyState.getLedgerState(); + + // Transaction validation only reads accounts (see ExecutedLedgerState). + auto changes = std::make_shared(); + execution.mLtx->forEachChangedEntry([&changes](LedgerKey const& key, + LedgerEntry const* entry) { + if (key.type() == ACCOUNT) + { + changes->emplace(key, + entry ? std::make_shared(*entry) + : nullptr); + } + }); + state->mChanges = std::move(changes); + + auto txHashes = std::make_shared>(); + if (!execution.mTxSetSkipped && execution.mApplicableTxSet) + { + txHashes->reserve(execution.mApplicableTxSet->sizeTxTotal()); + for (auto const& phase : execution.mApplicableTxSet->getPhases()) + { + for (auto const& tx : phase) + { + txHashes->insert(tx->getFullHash()); + } + } + } + // What the ledger applied: its results or, in overlay-only mode (which + // executes nothing and records no results), its transaction set. + state->mAppliedTxs = execution.mTxResultSet.results.size(); +#ifdef BUILD_TESTS + if (mApp.getRunInOverlayOnlyMode()) + { + state->mAppliedTxs = txHashes->size(); + } +#endif + state->mTxHashes = std::move(txHashes); + return state; +} + +std::shared_ptr +LedgerManagerImpl::speculativelyExecuteLedger(LedgerCloseData const& ledgerData, + bool captureState) { ZoneScoped; // At most one ledger is executed ahead of consensus: a newer request @@ -1928,7 +1990,7 @@ LedgerManagerImpl::speculativelyExecuteLedger(LedgerCloseData const& ledgerData) abortSpeculativeExecution(); if (mApp.isStopping()) { - return; + return nullptr; } { // The main thread asked for this on top of its LCL. Apply's own @@ -1939,7 +2001,7 @@ LedgerManagerImpl::speculativelyExecuteLedger(LedgerCloseData const& ledgerData) if (ledgerData.getLedgerSeq() != lcl.header.ledgerSeq + 1 || ledgerData.getTxSet()->previousLedgerHash() != lcl.hash) { - return; + return nullptr; } } try @@ -1953,6 +2015,29 @@ LedgerManagerImpl::speculativelyExecuteLedger(LedgerCloseData const& ledgerData) CLOG_ERROR(Ledger, "Speculative execution of ledger {} failed: {}", ledgerData.getLedgerSeq(), e.what()); mApplyState.markAbortedApplying(); + return nullptr; + } + if (!captureState) + { + return nullptr; + } + try + { + // On the apply thread, between the execution and its commit: timed, + // so that its cost is on record. + auto timer = mApp.getMetrics() + .NewTimer({"ledger", "apply", "capture-state"}) + .TimeScope(); + return captureExecutedState(*mSpeculativeExecution); + } + catch (std::exception const& e) + { + // Only costs the next leader its head start. + CLOG_WARNING(Ledger, + "Could not capture the state of the speculative " + "execution of ledger {}: {}", + ledgerData.getLedgerSeq(), e.what()); + return nullptr; } } diff --git a/src/ledger/LedgerManagerImpl.h b/src/ledger/LedgerManagerImpl.h index 1b034aafd..fbc81def6 100644 --- a/src/ledger/LedgerManagerImpl.h +++ b/src/ledger/LedgerManagerImpl.h @@ -414,7 +414,13 @@ class LedgerManagerImpl : public LedgerManager // Apply thread only: the ledger executed ahead of consensus, if any. std::unique_ptr mSpeculativeExecution; - void speculativelyExecuteLedger(LedgerCloseData const& ledgerData); + // Returns the state the execution left if `captureState` is set and the + // ledger was executed. + std::shared_ptr + speculativelyExecuteLedger(LedgerCloseData const& ledgerData, + bool captureState); + std::shared_ptr + captureExecutedState(LedgerExecution const& execution) const; // Returns the speculative execution if it is one of `ledgerData`, and // drops it otherwise. std::unique_ptr @@ -696,7 +702,9 @@ class LedgerManagerImpl : public LedgerManager void applyLedger(LedgerCloseData const& ledgerData, bool calledViaExternalize) override; - void startSpeculativeApply(LedgerCloseData const& ledgerData) override; + using LedgerManager::startSpeculativeApply; + void startSpeculativeApply(LedgerCloseData const& ledgerData, + SpeculativeApplyExecuted onExecuted) override; bool isSpeculativelyApplying() const override; void abortSpeculativeApply() override; void completeLedgerClose(uint32_t ledgerSeq, bool calledViaExternalize, diff --git a/src/ledger/LedgerTxn.cpp b/src/ledger/LedgerTxn.cpp index d29d99627..807b79629 100644 --- a/src/ledger/LedgerTxn.cpp +++ b/src/ledger/LedgerTxn.cpp @@ -1516,6 +1516,36 @@ LedgerTxn::Impl::getDelta() return delta; } +void +LedgerTxn::forEachChangedEntry( + std::function const& f) const +{ + getImpl()->forEachChangedEntry(f); +} + +void +LedgerTxn::Impl::forEachChangedEntry( + std::function const& f) const +{ + abortIfWrongThread("forEachChangedEntry"); + throwIfChild(); + if (!mActive.empty()) + { + throw std::runtime_error( + "LedgerTxn has active entries: its changes can't be read"); + } + for (auto const& kv : mEntry) + { + auto const& key = kv.first; + if (key.type() != InternalLedgerEntryType::LEDGER_ENTRY) + { + continue; + } + f(key.ledgerKey(), + kv.second.isDeleted() ? nullptr : &kv.second->ledgerEntry()); + } +} + EntryIterator LedgerTxn::Impl::getEntryIterator(EntryMap const& entries) const { diff --git a/src/ledger/LedgerTxn.h b/src/ledger/LedgerTxn.h index ae05fae66..0802eabb4 100644 --- a/src/ledger/LedgerTxn.h +++ b/src/ledger/LedgerTxn.h @@ -832,6 +832,16 @@ class LedgerTxn : public AbstractLedgerTxn LedgerTxnDelta getDelta() override; + // Calls `f` for every ledger entry this LedgerTxn created, changed or + // erased, with its key and its current value (nullptr if erased). Unlike + // getChanges and getDelta this does not seal the LedgerTxn and does not + // update last-modified sequence numbers: it is for looking at the state + // of a LedgerTxn that will be worked on further. Requires that there is + // no child and that no entry is active. + void forEachChangedEntry( + std::function const& f) + const; + UnorderedMap getOffersByAccountAndAsset(AccountID const& account, Asset const& asset) override; diff --git a/src/ledger/LedgerTxnImpl.h b/src/ledger/LedgerTxnImpl.h index 54102cdbb..69eb29859 100644 --- a/src/ledger/LedgerTxnImpl.h +++ b/src/ledger/LedgerTxnImpl.h @@ -419,6 +419,11 @@ class LedgerTxn::Impl // - the entry cache may be, but is not guaranteed to be, cleared. LedgerTxnDelta getDelta(); + // forEachChangedEntry has the strong exception safety guarantee + void forEachChangedEntry( + std::function const& f) + const; + // getOffersByAccountAndAsset has the basic exception safety guarantee. UnorderedMap getOffersByAccountAndAsset(AccountID const& account, Asset const& asset); diff --git a/src/main/ApplicationImpl.cpp b/src/main/ApplicationImpl.cpp index 101e251a6..c8b393bba 100644 --- a/src/main/ApplicationImpl.cpp +++ b/src/main/ApplicationImpl.cpp @@ -111,7 +111,11 @@ ApplicationImpl::ApplicationImpl(VirtualClock& clock, Config const& cfg) : nullptr) , mWorkerThreads() , mEvictionThread() - , mBatchExecutor(std::make_unique()) + , mBatchExecutor([&cfg]() { + auto executor = std::make_unique(); + executor->setPreferredTaskCount(cfg.BATCH_EXECUTOR_TASKS); + return executor; + }()) , mStopSignals(clock.getIOContext(), SIGINT) , mStarted(false) , mStopping(false) diff --git a/src/main/Config.cpp b/src/main/Config.cpp index c13c75163..50e994f44 100644 --- a/src/main/Config.cpp +++ b/src/main/Config.cpp @@ -180,6 +180,9 @@ Config::Config() : NODE_SEED(SecretKey::random()) DISABLE_TX_META_FOR_TESTING = false; BACKGROUND_TX_SIG_VERIFICATION = true; FORCE_OLD_STYLE_PREPARE_START_TRIGGER_TIMER = false; + TX_FLOOD_TARGET_LEADERS = 2; + TX_FLOOD_LEGACY_ADVERTS = false; + BATCH_EXECUTOR_TASKS = 0; NTP_DRIFT_CHECK_SERVER = "pool.ntp.org"; BUCKETLIST_DB_INDEX_PAGE_SIZE_EXPONENT = 14; // 2^14 == 16 kb BUCKETLIST_DB_INDEX_CUTOFF = 20; // 20 mb @@ -1239,6 +1242,16 @@ Config::processConfig(std::shared_ptr t) FORCE_OLD_STYLE_PREPARE_START_TRIGGER_TIMER = readBool(item); }}, + {"TX_FLOOD_TARGET_LEADERS", + [&]() { + TX_FLOOD_TARGET_LEADERS = readInt(item); + }}, + {"TX_FLOOD_LEGACY_ADVERTS", + [&]() { TX_FLOOD_LEGACY_ADVERTS = readBool(item); }}, + {"BATCH_EXECUTOR_TASKS", + [&]() { + BATCH_EXECUTOR_TASKS = readInt(item, 0, 256); + }}, {"NTP_DRIFT_CHECK_SERVER", [&]() { NTP_DRIFT_CHECK_SERVER = readString(item); }}, {"ARTIFICIALLY_DELAY_LEDGER_CLOSE_FOR_TESTING", diff --git a/src/main/Config.h b/src/main/Config.h index 45fbfbca2..623f760dd 100644 --- a/src/main/Config.h +++ b/src/main/Config.h @@ -566,6 +566,31 @@ class Config : public std::enable_shared_from_this // is an emergency fallback and defaults to false. bool FORCE_OLD_STYLE_PREPARE_START_TRIGGER_TIMER; + // How many upcoming round leaders get the transactions this node + // accepts: the leader of the node's current round of the next slot and + // the ones after it; one slot before a new leader term starts, also + // that term's expected first leader (see + // HerderImpl::computeTxFloodTargets). The node + // sends them the transaction bodies directly instead of advertising them + // to every peer. 0 means every validator of the leader schedule: no + // adverts either, but every validator holds every transaction before + // the leader's set arrives (and can verify its signatures in the + // background). Experimental; defaults to 2. + uint32_t TX_FLOOD_TARGET_LEADERS; + + // Experimental. true: no target list at all; transactions are advertised + // to every peer and fetched on demand, as before TX_FLOOD_TARGET_LEADERS + // existed. For comparing the two ways of sending with one build. + bool TX_FLOOD_LEGACY_ADVERTS; + + // Experimental. Number of tasks (worker threads) the batch executor + // splits CPU-bound work into: transaction validation in the leader's + // build, in the apply-time check and in the pre-validation of the next + // proposal, and the signature prewarm. 0 (the + // default) = one per physical core, minus one. A larger value uses + // hyper-thread siblings once the physical cores are taken. + uint32_t BATCH_EXECUTOR_TASKS; + // Hostname of an NTP server to periodically query in order to detect drift // of this node's local clock. This is detection only: core never adjusts // the system clock. Defaults to a pool.ntp.org; set to the empty string to diff --git a/src/main/test/CommandHandlerTests.cpp b/src/main/test/CommandHandlerTests.cpp index 2ac68a67b..90736e53a 100644 --- a/src/main/test/CommandHandlerTests.cpp +++ b/src/main/test/CommandHandlerTests.cpp @@ -16,7 +16,9 @@ #include "transactions/TransactionBridge.h" #include "transactions/TransactionUtils.h" #include "util/Decoder.h" +#include "ledger/LedgerManager.h" #include "util/Math.h" +#include "util/ProtocolVersion.h" #include "xdr/Stellar-ledger-entries.h" #include "xdr/Stellar-transaction.h" #include "xdrpp/marshal.h" @@ -36,6 +38,19 @@ TEST_CASE_VERSIONS("transaction envelope bridge", "[commandhandler]") auto baseFee = app->getLedgerManager().getLastTxFee(); std::string const PENDING_RESULT = "{\"status\":\"PENDING\"}\n"; + // A node checks a submitted transaction against its last closed ledger + // before it takes it (HerderImpl::checkTxAdmission); the endpoint + // reports a refusal without the result XDR. + std::string const ERROR_RESULT = "{\"status\":\"ERROR\"}\n"; + // V1 and fee-bump envelopes are valid from protocol 13. + auto v1Result = [&]() { + return protocolVersionStartsFrom(app->getLedgerManager() + .getLastClosedLedgerHeader() + .header.ledgerVersion, + ProtocolVersion::V_13) + ? PENDING_RESULT + : ERROR_RESULT; + }; auto errorResult = [](TransactionResultCode resultCode, int64_t fee) { TransactionResult txRes; txRes.feeCharged = fee; @@ -114,8 +129,21 @@ TEST_CASE_VERSIONS("transaction envelope bridge", "[commandhandler]") timeBoundsTest(timeBounds, PENDING_RESULT); } - // Invalid timebounds are not rejected here: the tx endpoint no - // longer validates transactions synchronously on submission. + SECTION("too early") + { + xdr::pointer timeBounds; + timeBounds.activate().minTime = getTestDate(3, 1, 2017); + timeBounds.activate().maxTime = getTestDate(4, 1, 2017); + timeBoundsTest(timeBounds, ERROR_RESULT); + } + + SECTION("too late") + { + xdr::pointer timeBounds; + timeBounds.activate().minTime = getTestDate(29, 12, 2016); + timeBounds.activate().maxTime = getTestDate(30, 12, 2016); + timeBoundsTest(timeBounds, ERROR_RESULT); + } } auto createV1 = [&]() { @@ -132,15 +160,13 @@ TEST_CASE_VERSIONS("transaction envelope bridge", "[commandhandler]") return env; }; - // The tx endpoint no longer validates transactions synchronously on - // submission (the Rust overlay mempool does that asynchronously), so - // envelopes are accepted as PENDING at every protocol version, including - // versions that would previously have rejected them with txNOT_SUPPORTED. + // Checked on submission: refused at the protocol versions that do not + // support the envelope type (txNOT_SUPPORTED). SECTION("new-style transaction v1") { for_all_versions(*app, [&]() { closeLedgerOn(*app, 2, 1, 1, 2017); - REQUIRE(submit(createV1()) == PENDING_RESULT); + REQUIRE(submit(createV1()) == v1Result()); }); } @@ -163,7 +189,7 @@ TEST_CASE_VERSIONS("transaction envelope bridge", "[commandhandler]") for_all_versions(*app, [&]() { closeLedgerOn(*app, 2, 1, 1, 2017); - REQUIRE(submit(createFeeBump()) == PENDING_RESULT); + REQUIRE(submit(createFeeBump()) == v1Result()); }); } } diff --git a/src/overlay/IPC.h b/src/overlay/IPC.h index 841968daa..c6de7e870 100644 --- a/src/overlay/IPC.h +++ b/src/overlay/IPC.h @@ -82,6 +82,20 @@ enum class IPCMessageType : uint32_t /// Payload: [hash:32][slotSeq:4] PUSH_TX_SET = 14, + /// This node's validator identity, in answer to OVERLAY_IDENTITY: the + /// node key's signature over + /// SHA256(networkID || "stellar-overlay-peer-id" || peerId). The overlay + /// shows it to its peers, which is how a peer connection is tied to a + /// NodeID. + /// Payload: [networkID:32][nodeID:32][signature:64] + SET_NODE_IDENTITY = 15, + + /// The validators that get this node's transactions now (see + /// HerderImpl::computeTxFloodTargets). + /// Payload: [everyone:1][count:4][nodeID:32]... + /// everyone = 1: flood to all peers (the list is ignored). + SET_TX_TARGETS = 16, + // ═══ Overlay → Core (Critical Path) ═══ /// Received SCP envelope from network @@ -106,6 +120,11 @@ enum class IPCMessageType : uint32_t /// Overlay metrics snapshot (JSON payload) /// Response to REQUEST_OVERLAY_METRICS OVERLAY_METRICS_RESPONSE = 105, + + /// The overlay's libp2p peer id, sent once at startup so that Core can + /// sign the identity binding (SET_NODE_IDENTITY). + /// Payload: [peerId bytes] + OVERLAY_IDENTITY = 106, }; /** diff --git a/src/overlay/OverlayIPC.cpp b/src/overlay/OverlayIPC.cpp index 3b593e782..c5f0a4666 100644 --- a/src/overlay/OverlayIPC.cpp +++ b/src/overlay/OverlayIPC.cpp @@ -395,6 +395,22 @@ OverlayIPC::handleMessage(IPCMessage const& msg) break; } + case IPCMessageType::OVERLAY_IDENTITY: + { + if (mOnOverlayIdentity && !msg.payload.empty() && + msg.payload.size() <= 128) + { + mOnOverlayIdentity(msg.payload); + } + else + { + CLOG_WARNING(Overlay, + "Ignoring OVERLAY_IDENTITY ({} bytes payload)", + msg.payload.size()); + } + break; + } + case IPCMessageType::TOP_TXS_RESPONSE: { // Response to getTopTransactions - wake up waiting thread @@ -847,6 +863,54 @@ OverlayIPC::requestScpState(uint32_t ledgerSeq) mChannel->send(msg); } +void +OverlayIPC::setNodeIdentity(Hash const& networkID, NodeID const& nodeID, + Signature const& signature) +{ + if (!mChannel || !mChannel->isConnected()) + { + return; + } + releaseAssert(signature.size() == 64); + IPCMessage msg; + msg.type = IPCMessageType::SET_NODE_IDENTITY; + msg.payload.resize(128); + std::memcpy(msg.payload.data(), networkID.data(), 32); + std::memcpy(msg.payload.data() + 32, nodeID.ed25519().data(), 32); + std::memcpy(msg.payload.data() + 64, signature.data(), 64); + std::lock_guard lock(mSendMutex); + mChannel->send(msg); +} + +bool +OverlayIPC::setTxTargets(std::optional> const& targets) +{ + if (!mChannel || !mChannel->isConnected()) + { + return false; + } + IPCMessage msg; + msg.type = IPCMessageType::SET_TX_TARGETS; + uint32_t const count = + targets ? static_cast(targets->size()) : 0; + msg.payload.resize(5 + static_cast(count) * 32); + msg.payload[0] = targets ? 0 : 1; + std::memcpy(msg.payload.data() + 1, &count, 4); + for (uint32_t i = 0; i < count; ++i) + { + std::memcpy(msg.payload.data() + 5 + static_cast(i) * 32, + (*targets)[i].ed25519().data(), 32); + } + std::lock_guard lock(mSendMutex); + return mChannel->send(msg); +} + +void +OverlayIPC::setOnOverlayIdentity(OverlayIdentityCallback cb) +{ + mOnOverlayIdentity = std::move(cb); +} + void OverlayIPC::setOnSCPReceived(SCPReceivedCallback cb) { diff --git a/src/overlay/OverlayIPC.h b/src/overlay/OverlayIPC.h index 5bdba8387..c3bd5990c 100644 --- a/src/overlay/OverlayIPC.h +++ b/src/overlay/OverlayIPC.h @@ -50,6 +50,11 @@ class OverlayIPC using TxSetReceivedCallback = std::function; + /// Callback when the overlay reports its libp2p peer id at startup + /// (called on the IPC reader thread) + using OverlayIdentityCallback = + std::function const& peerId)>; + /** * Create an OverlayIPC instance. * @@ -218,6 +223,24 @@ class OverlayIPC */ void pushTxSet(Hash const& hash, uint32_t slotIndex); + /** + * Tell the overlay which validator it belongs to: `signature` is the + * node key's signature binding `nodeID` to the overlay's peer id (see + * RustOverlayManager::signOverlayIdentity). The overlay shows it to its + * peers. + */ + void setNodeIdentity(Hash const& networkID, NodeID const& nodeID, + Signature const& signature); + + /** + * Tell the overlay which validators get this node's transactions now. + * nullopt = every peer (flood). Returns whether it was sent. + */ + bool setTxTargets(std::optional> const& targets); + + /// Set callback for the overlay's peer id report + void setOnOverlayIdentity(OverlayIdentityCallback cb); + /// Set callback for received SCP envelopes void setOnSCPReceived(SCPReceivedCallback cb); @@ -281,6 +304,7 @@ class OverlayIPC SCPReceivedCallback mOnSCPReceived; ScpStateRequestCallback mOnScpStateRequest; TxSetReceivedCallback mOnTxSetReceived; + OverlayIdentityCallback mOnOverlayIdentity; // For synchronous request/response (getTopTransactions) std::mutex mRequestMutex; diff --git a/src/overlay/OverlayMetrics.cpp b/src/overlay/OverlayMetrics.cpp index fbf16589c..307068011 100644 --- a/src/overlay/OverlayMetrics.cpp +++ b/src/overlay/OverlayMetrics.cpp @@ -69,6 +69,20 @@ OverlayMetrics::OverlayMetrics(Application& app) {"overlay", "txset-push", "dropped"}, "txset")) , mTxSetPushEvicted(app.getMetrics().NewMeter( {"overlay", "txset-push", "evicted"}, "txset")) + , mTxDirectSent(app.getMetrics().NewMeter( + {"overlay", "flood", "tx-direct-sent"}, "transaction")) + , mTxDirectSentBytes(app.getMetrics().NewMeter( + {"overlay", "flood", "tx-direct-sent-bytes"}, "byte")) + , mTxDirectRecv(app.getMetrics().NewMeter( + {"overlay", "flood", "tx-direct-recv"}, "transaction")) + , mTxLateCopy(app.getMetrics().NewMeter( + {"overlay", "flood", "tx-late-copy"}, "transaction")) + , mTxLocalFlooded(app.getMetrics().NewMeter( + {"overlay", "flood", "tx-local-flooded"}, "transaction")) + , mNodeIdRejected(app.getMetrics().NewMeter( + {"overlay", "node-id", "rejected"}, "claim")) + , mNodeIdIdentified( + app.getMetrics().NewCounter({"overlay", "node-id", "identified"})) { } } diff --git a/src/overlay/OverlayMetrics.h b/src/overlay/OverlayMetrics.h index 3fbb7475d..cfaa696ef 100644 --- a/src/overlay/OverlayMetrics.h +++ b/src/overlay/OverlayMetrics.h @@ -74,5 +74,14 @@ struct OverlayMetrics medida::Meter& mTxSetPushRequestSkipped; medida::Meter& mTxSetPushDropped; medida::Meter& mTxSetPushEvicted; + + // Transactions sent to the target leaders only (tx-flood-to-leaders) + medida::Meter& mTxDirectSent; + medida::Meter& mTxDirectSentBytes; + medida::Meter& mTxDirectRecv; + medida::Meter& mTxLateCopy; + medida::Meter& mTxLocalFlooded; + medida::Meter& mNodeIdRejected; + medida::Counter& mNodeIdIdentified; }; } diff --git a/src/overlay/RustOverlayManager.cpp b/src/overlay/RustOverlayManager.cpp index 024f11433..f01015ce3 100644 --- a/src/overlay/RustOverlayManager.cpp +++ b/src/overlay/RustOverlayManager.cpp @@ -4,6 +4,8 @@ #include "overlay/RustOverlayManager.h" #include "herder/Herder.h" +#include "crypto/SHA.h" +#include "crypto/SecretKey.h" #include "herder/TxSetFrame.h" #include "lib/json/json.h" #include "main/Application.h" @@ -88,6 +90,19 @@ RustOverlayManager::start() "RustOverlayManager: TxSetReceived"); }); + mOverlayIPC->setOnOverlayIdentity( + [this](std::vector const& peerId) { + // Called from the IPC reader thread. The config is immutable + // and signing is a pure function of it, so no hop to the main + // thread is needed. The node key itself stays in Core. + auto const& nodeSeed = mApp.getConfig().NODE_SEED; + auto const binding = + overlayIdentityBinding(mApp.getNetworkID(), peerId); + mOverlayIPC->setNodeIdentity(mApp.getNetworkID(), + nodeSeed.getPublicKey(), + nodeSeed.sign(binding)); + }); + if (!mOverlayIPC->start()) { CLOG_ERROR(Overlay, "Failed to start Rust overlay process"); @@ -246,6 +261,29 @@ RustOverlayManager::cacheTxSet(Hash const& txSetHash, return false; } +bool +RustOverlayManager::setTxFloodTargets( + std::optional> const& targets) +{ + if (mOverlayIPC && !mShuttingDown) + { + return mOverlayIPC->setTxTargets(targets); + } + return false; +} + +Hash +RustOverlayManager::overlayIdentityBinding(Hash const& networkID, + std::vector const& peerId) +{ + static std::string const tag = "stellar-overlay-peer-id"; + SHA256 hasher; + hasher.add(networkID); + hasher.add(ByteSlice(tag.data(), tag.size())); + hasher.add(ByteSlice(peerId.data(), peerId.size())); + return hasher.finish(); +} + bool RustOverlayManager::storesTxSets() const { @@ -425,6 +463,19 @@ RustOverlayManager::syncOverlayMetrics() markDelta(m.mTxSetPushDropped, "txset_push_dropped"); markDelta(m.mTxSetPushEvicted, "txset_push_evicted"); + // Transactions sent to the target leaders only + markDelta(m.mTxDirectSent, "flood_tx_direct_sent"); + markDelta(m.mTxDirectSentBytes, "flood_tx_direct_sent_bytes"); + markDelta(m.mTxDirectRecv, "flood_tx_direct_recv"); + markDelta(m.mTxLateCopy, "flood_tx_late_copy"); + markDelta(m.mTxLocalFlooded, "flood_tx_local_flooded"); + markDelta(m.mNodeIdRejected, "node_id_rejected"); + if (root.isMember("node_id_identified")) + { + auto val = root["node_id_identified"].asInt64(); + m.mNodeIdIdentified.inc(val - m.mNodeIdIdentified.count()); + } + // Connection lifecycle — these aren't registered as medida meters on // the C++ side yet, so they'll just be tracked by the existing counters. // The inbound/outbound attempt/establish/drop are already covered diff --git a/src/overlay/RustOverlayManager.h b/src/overlay/RustOverlayManager.h index 24fd668e7..1af7439f8 100644 --- a/src/overlay/RustOverlayManager.h +++ b/src/overlay/RustOverlayManager.h @@ -94,6 +94,19 @@ class RustOverlayManager } #endif + // Tell the overlay which validators get this node's transactions now: + // it sends the transactions Core accepts (and the ones still pending) + // straight to those nodes instead of flooding them. nullopt = every peer + // (the flood). Only HerderImpl::updateTxFloodTargets decides this. + // Returns whether the overlay was told. + bool setTxFloodTargets(std::optional> const& targets); + + // What a validator signs to say "the overlay with this libp2p peer id is + // mine": SHA256(networkID || "stellar-overlay-peer-id" || peerId). The + // tag keeps it apart from everything else the node key signs. + static Hash overlayIdentityBinding(Hash const& networkID, + std::vector const& peerId); + // Whether the overlay process writes every TX set to stable storage // before it hands it to Core (a set received from a peer) or broadcasts // anything that refers to it (a set cached by Core). When it does, Core diff --git a/src/scp/BallotProtocol.cpp b/src/scp/BallotProtocol.cpp index 01bc51c56..7fb2a5e0a 100644 --- a/src/scp/BallotProtocol.cpp +++ b/src/scp/BallotProtocol.cpp @@ -549,6 +549,19 @@ BallotProtocol::progressRoundStep() /*proposal=*/true) != SCPDriver::kInvalidValue) { Value v = mCurrentBallot->getBallot().value; + // The application may put an equivalent value forward instead, + // but only while nothing ties the node to this exact one: once + // it has accepted a ballot for it as prepared, other nodes may + // have confirmed it and be bound to it, and a replacement could + // only split the round between the two. + if (!(mPrepared && mPrepared->getBallot().value == v)) + { + if (auto replacement = + driver.reproposeValue(slotIndex, round, v)) + { + v = replacement->getValue(); + } + } driver.proposedValue(slotIndex, round, v); return bumpState(v, round); } diff --git a/src/scp/SCPDriver.h b/src/scp/SCPDriver.h index a41af7d78..a66ae4973 100644 --- a/src/scp/SCPDriver.h +++ b/src/scp/SCPDriver.h @@ -251,6 +251,19 @@ class SCPDriver { } + // `reproposeValue` is called when the local node, as the leader of + // `round`, is about to put forward `value`, the value it already votes + // for, rather than building a new one. It may return an equivalent value + // to propose instead (e.g. the same content under the local node's own + // signature), or nullptr to propose `value` as it is. Not called once + // the node has accepted a ballot for `value` as prepared: it then keeps + // the value, like the nodes that may have confirmed it. + virtual ValueWrapperPtr + reproposeValue(uint64 slotIndex, uint32 round, Value const& value) + { + return nullptr; + } + // the following methods are used for monitoring of the SCP subsystem // most implementation don't really need to do anything with these diff --git a/src/scp/test/SCPTests.cpp b/src/scp/test/SCPTests.cpp index 6feb5d763..d6c7a93bb 100644 --- a/src/scp/test/SCPTests.cpp +++ b/src/scp/test/SCPTests.cpp @@ -216,6 +216,14 @@ class TestSCP : public SCPDriver mEnteredRounds.emplace_back(round, timedOut); } + ValueWrapperPtr + reproposeValue(uint64 slotIndex, uint32 round, Value const& value) override + { + mReproposeCalls.emplace_back(round, value); + return mReproposeValueOverride ? mReproposeValueOverride(round, value) + : ValueWrapperPtr(); + } + bool isProposalPending(uint64, NodeID const&) const override { @@ -228,6 +236,11 @@ class TestSCP : public SCPDriver int mValuesBuilt{0}; std::vector> mProposedValues; std::vector> mEnteredRounds; + // What the driver's reproposeValue was asked, and what it answers + // (nothing by default: the value is proposed as it is). + std::vector> mReproposeCalls; + std::function + mReproposeValueOverride; Hash getHashOf(std::vector> const& vals) const override @@ -3660,6 +3673,31 @@ TEST_CASE("leader rounds core5", "[scp][leaderschedule]") // x, not its own z: the transaction set of x is already out verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, x2); REQUIRE(scp.mValuesBuilt == 0); + // the application was offered to put an equivalent value forward + REQUIRE(scp.mReproposeCalls == + std::vector>{{2, xValue}}); + REQUIRE(scp.mProposedValues.back() == + std::make_pair(uint32(2), xValue)); + } + SECTION("to the local node, which proposes the application's " + "replacement for the value it voted for") + { + // e.g. the same content under the new leader's signature + schedule = {v1NodeID, v0NodeID, v2NodeID, v3NodeID, v4NodeID}; + scp.mReproposeValueOverride = [&](uint32 round, Value const& v) { + REQUIRE(round == 2); + REQUIRE(v == xValue); + return scp.wrapValue(kValue); + }; + scp.fireTimer(Slot::BALLOT_PROTOCOL_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, + SCPBallot(2, kValue)); + REQUIRE(scp.mValuesBuilt == 0); + REQUIRE(scp.mReproposeCalls.size() == 1); + REQUIRE(scp.mProposedValues.back() == + std::make_pair(uint32(2), kValue)); } SECTION("to the local node, whose current value is not proposable") { @@ -3677,6 +3715,33 @@ TEST_CASE("leader rounds core5", "[scp][leaderschedule]") } } + SECTION("a leader tied to a value it accepted as prepared keeps it") + { + // v2 and v3 vote for the round 1 leader's x as the local node does: + // a quorum, so the local node accepts (1, x) as prepared. Other + // nodes may confirm it and be bound to x, so when the local node + // leads round 2 the application is not offered a replacement. + schedule = {v1NodeID, v0NodeID, v2NodeID, v3NodeID, v4NodeID}; + scp.mReproposeValueOverride = [&](uint32, Value const&) { + return scp.wrapValue(kValue); + }; + REQUIRE(!scp.startSlot(0, zValue)); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, x1)); + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, x1, &x1); + REQUIRE(scp.hasBallotTimer()); + + scp.fireTimer(Slot::BALLOT_PROTOCOL_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.size() == 3); + verifyPrepare(scp.mEnvs[2], v0SecretKey, qSetHash0, 0, x2, &x1); + REQUIRE(scp.mReproposeCalls.empty()); + REQUIRE(scp.mValuesBuilt == 0); + } + SECTION("a confirmed prepared value wins over the leader's") { schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; diff --git a/src/util/BatchExecutor.cpp b/src/util/BatchExecutor.cpp index 716bc5c2c..d50894773 100644 --- a/src/util/BatchExecutor.cpp +++ b/src/util/BatchExecutor.cpp @@ -244,6 +244,10 @@ BatchExecutor::preferredTaskCount() const return *mPreferredTaskCountForTesting; } #endif + if (auto const configured = mPreferredTaskCountOverride.load()) + { + return configured; + } // As this is meant to be used for parallelizing CPU-heavy work, we want to // only run the tasks on the physical cores (when physical core info is // available). @@ -255,6 +259,12 @@ BatchExecutor::preferredTaskCount() const return concurrency > 1 ? concurrency - 1 : 1; } +void +BatchExecutor::setPreferredTaskCount(size_t count) +{ + mPreferredTaskCountOverride.store(count); +} + #ifdef BUILD_TESTS void BatchExecutor::setPreferredTaskCountForTesting(size_t count) diff --git a/src/util/BatchExecutor.h b/src/util/BatchExecutor.h index fb116e558..85a975430 100644 --- a/src/util/BatchExecutor.h +++ b/src/util/BatchExecutor.h @@ -84,6 +84,11 @@ class BatchExecutor : private NonMovableOrCopyable // oversubscription. size_t preferredTaskCount() const; + // Makes `preferredTaskCount` return `count` (0 restores the default). + // More tasks than physical cores run on hyper-thread siblings. Set once, + // before the first batch (Config::BATCH_EXECUTOR_TASKS). + void setPreferredTaskCount(size_t count); + #ifdef BUILD_TESTS // Overrides the value returned by `preferredTaskCount`, so that tests can // pin the parallelism independently of the machine they run on. @@ -118,6 +123,8 @@ class BatchExecutor : private NonMovableOrCopyable // First `mPhysicalCoreCount` entries of `mPinCpuOrder` are guaranteed to be // on distinct physical cores by the pinning order assignment procedure. size_t mPhysicalCoreCount{0}; + // Config::BATCH_EXECUTOR_TASKS; 0 = one per physical core, minus one. + std::atomic mPreferredTaskCountOverride{0}; // Task that is being currently executed by the workers. std::function const* mRunTask{nullptr};