diff --git a/Cargo.lock b/Cargo.lock
index 23b4413290..b45d7b694f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -581,6 +581,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5add81bb678e6cb321aff7fa0dc7689ad82b112dbc032cea19f91d6b8e3582b9"
dependencies = [
"find-msvc-tools",
+ "jobserver",
+ "libc",
"shlex",
]
@@ -1793,6 +1795,16 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6"
+[[package]]
+name = "jobserver"
+version = "0.1.35"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3"
+dependencies = [
+ "getrandom 0.4.3",
+ "libc",
+]
+
[[package]]
name = "js-sys"
version = "0.3.77"
@@ -2655,6 +2667,12 @@ dependencies = [
"spki 0.8.1",
]
+[[package]]
+name = "pkg-config"
+version = "0.3.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
+
[[package]]
name = "polling"
version = "3.11.0"
@@ -4254,6 +4272,7 @@ dependencies = [
"toml",
"tracing",
"tracing-subscriber",
+ "zstd",
]
[[package]]
@@ -5569,3 +5588,31 @@ dependencies = [
"quote",
"syn 2.0.119",
]
+
+[[package]]
+name = "zstd"
+version = "0.13.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
+dependencies = [
+ "zstd-safe",
+]
+
+[[package]]
+name = "zstd-safe"
+version = "7.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "64d80649ab6db9d9f6f9c80a40becd948eda4714a0a5ac8c4d157a32231c7882"
+dependencies = [
+ "zstd-sys",
+]
+
+[[package]]
+name = "zstd-sys"
+version = "2.1.0+zstd.1.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0ef0a8027ec3ee71300ab3bcbcd0393f434aa72b91ca6d635a39941deae8eea0"
+dependencies = [
+ "cc",
+ "pkg-config",
+]
diff --git a/README.md b/README.md
index 0126b7350e..f6d812cfd7 100644
--- a/README.md
+++ b/README.md
@@ -1,34 +1,248 @@
-
-
-
-
Creating equitable access to the global financial system
-
Stellar Core
-
-
-
-
+# stellar-core: leader schedule without SCP nomination (experimental branch)
-Stellar-core is a replicated state machine that maintains a local copy of a cryptographic ledger and processes transactions against it, in consensus with a set of peers.
-It implements the [Stellar Consensus Protocol](https://github.com/stellar/stellar-core/blob/master/src/scp/README.md), a _federated_ consensus protocol.
-It is written in C++20 and runs on Linux, OSX and Windows.
-Learn more by reading the [overview document](https://github.com/stellar/stellar-core/blob/master/docs/README.md).
+**This branch is an experiment. It is not production code and must not run on a real network.**
+It has no feature flag, no protocol gate, and it is not compatible with nodes that nominate.
-# Documentation
+Results below were measured on commit `daa965f4c` on 2026-11-10 and are one run per setting.
-Documentation of the code's layout and abstractions, as well as for the
-functionality available, can be found in
-[`./docs`](https://github.com/stellar/stellar-core/tree/master/docs).
+## What this branch is
-# Installation
+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 one change: **the SCP nomination phase is deleted and replaced by a leader
+schedule that every node computes locally.** Draft PR:
+[#5495](https://github.com/stellar/stellar-core/pull/5495).
-See [Installation](./INSTALL.md)
+- **Schedule.** For slot N, the nodes of the transitive quorum are sorted by
+ `SHA256(hash of ledger header N-1 || node id)`. The leader of round r is entry `(r-1) mod n`.
+- **Rounds.** A round is a ballot counter. The round's leader builds its value and goes straight to
+ the ballot protocol with `(r, value)`. The other nodes vote for the leader's ballot once it passes
+ proposal validation. A timeout (the ballot timeout settings) hands the slot to the next leader.
+- **Unchanged.** The ballot protocol's accept, confirm and commit rules, value validation, apply,
+ ledger state and meta.
-# Contributing
+The question it answers: how much of a ledger's time is nomination, and what limits block time once
+nomination is gone?
-See [Contributing](./CONTRIBUTING.md)
+## Performance results
-# Running tests
+| Target block time | Load | Verdict | Block time measured (average over 30 nodes) |
+|---|---|---|---|
+| **2,000 ms** | 3,000 TPS | **PASS**, all four windows | p75 2,066-2,109 ms, p99 2,286-2,324 ms |
+| **1,000 ms** | 3,000 TPS | **FAIL**, first window | p75 1,386 ms (limit 1,200), p99 1,599 ms |
-See [running tests](./CONTRIBUTING.md#running-tests)
+In short: at 2 s the network keeps up and has more slack per ledger than with nomination. At 1 s it
+settles at about 1.33 s per ledger. Nothing breaks; the stages of one ledger simply add up to more
+than a second.
-[](https://deepwiki.com/stellar/stellar-core)
+### 2,000 ms target: PASS
+
+Load ran its full 900 s. Every node passed every window.
+
+| Window | p75, average (range over nodes) | p99, average (range over nodes) |
+|---|---|---|
+| 1 | 2,109 ms (2,086-2,128) | 2,324 ms (2,282-2,359) |
+| 2 | 2,066 ms (2,058-2,076) | 2,286 ms (2,239-2,328) |
+| 3 | 2,082 ms (2,067-2,091) | 2,296 ms (2,251-2,333) |
+| 4 | 2,094 ms (2,080-2,105) | 2,303 ms (2,262-2,344) |
+
+Checks, all passed:
+- **Inclusion:** every node's ledgers hold exactly the 2,880,000 offered transactions.
+- **No fork**, all 30 nodes in sync, still closing ledgers after the load.
+- **Error counters zero** (invalid signatures, invariant failures, publish failures, invalid values,
+ lost sync), no node restarts.
+- **No wasted rounds:** every ledger was decided by the first leader. 0 leader timeouts, 0 ballot
+ timeouts, 0 empty transaction sets.
+
+Against the same fixture on #5459's head `ebdd73635` (with nomination), run the same day on the same
+instance type:
+
+| 5-minute means, median node | #5459 `ebdd73635` | This branch `daa965f4c` |
+|---|---:|---:|
+| Verdict | PASS | PASS |
+| p75 per window | 2,112 / 2,099 / 2,101 / 2,099 ms | 2,109 / 2,066 / 2,082 / 2,094 ms |
+| p99 per window | 2,334 / 2,391 / 2,324 / 2,354 ms | 2,324 / 2,286 / 2,296 / 2,303 ms |
+| Ledgers per 5 minutes | 147-148 | 149-150 |
+| Transactions per ledger | 6,092 | 6,003 |
+| Trigger to first ballot | 1.21 s | 0.88 s |
+| First ballot to externalize | 0.29 s | 0.43 s |
+| **Trigger to externalize** | **1.50 s** | **1.31 s** |
+| Leader tx-set build | 0.28 s | 0.27 s |
+| Tx-set fetch (follower) | 0.24 s | 0.26 s |
+| Tx-set validation (follower) | 0.20 s | 0.19 s |
+| Ledger close | 0.164 s | 0.162 s |
+
+- Block time is the same on both, as expected: when the network keeps up, the 2 s trigger sets the
+ cadence.
+- The time from trigger to externalize is 0.19 s shorter. The part before the first ballot that is
+ not build, fetch or validation fell from about 0.48 s (nomination rounds) to about 0.15 s.
+- Some of that came back in the ballot phase. Nodes now start their ballot at different times, as
+ each finishes validating the leader's set, so the early ones wait for a quorum of the others.
+
+### 1,000 ms target: FAIL
+
+The first 5-minute window missed on all 30 nodes, and the test stops the load at the first miss.
+
+| Window 1 | Value |
+|---|---|
+| p75, average (range over nodes) | 1,386 ms (1,375-1,399); the limit is below 1,200 ms |
+| p99, average (range over nodes) | 1,599 ms (1,582-1,628); the limit is 2,000 ms |
+| Ledgers per 5 minutes | 225 (1.33 s each) |
+| Transactions per ledger | 3,750, the cap, on every ledger |
+| Leader timeouts, ballot timeouts, empty tx sets | 0, 0, 0 |
+| Error counters, in sync, no fork, still closing | all passed |
+
+The network was over capacity at this block time: 3,000 TPS x 1.33 s is 4,000 transactions offered
+per ledger against a cap of 3,750, so the mempool backed up and every ledger started the instant
+the previous one closed. Inclusion is not checked after an early stop. No baseline was run at this
+setting.
+
+### Where a ledger's time goes
+
+| Stage (5-minute means, median node) | 2,000 ms target (6,000 txs) | 1,000 ms target (3,750 txs) |
+|---|---:|---:|
+| Leader builds the tx set | 0.27 s | 0.25 s |
+| Trigger to first ballot (includes the build, the fetch and validation) | 0.88 s | 0.70 s |
+| First ballot to externalize | 0.43 s | 0.41 s |
+| Ledger close (150 ms / 187 ms of it is simulated apply) | 0.16 s | 0.20 s |
+| **Sum** | **1.47 s** | **1.30 s** |
+
+Halving the transaction set took only 0.17 s off the cycle. What remains is mostly cross-region
+round trips, not CPU:
+
+1. **Tx-set dissemination takes about 2.5 round trips.** A follower receives the proposal, requests
+ the set from the proposer, and the response takes two flights. Seen from us-west-2, the fetch
+ took 27 ms from the same region, 150 ms from us-east-1, 301 ms from eu-central-1 and 362 ms from
+ ap-southeast-1. Nothing is pushed, and transactions are sent in full although followers already
+ hold nearly all of them.
+2. **Followers validate before they vote, and the ballot then needs four one-way trips.**
+3. **About 70 ms pass between the leader finishing its set and the proposal reaching a peer in the
+ same region.** SCP state, including the tx set, is written to the database before each
+ broadcast. That this write accounts for most of the 70 ms is inferred, not timed.
+4. **The leader's build is on the critical path.** At 1 s nothing can be built ahead of the trigger.
+
+## How it was measured
+
+Both 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.
+
+### Network topology
+
+| | |
+|---|---|
+| Validators | 30, all validating, no watchers |
+| Organizations | 10 home domains of 3 validators each, all `HIGH` quality |
+| Quorum set | Generated by stellar-core from the home domains: 7 of 10 organizations, 2 of 3 validators within each |
+| Regions | 4 AWS regions: eu-central-1 (8 nodes), ap-southeast-1 (8), us-east-1 (7), us-west-2 (7) |
+| Placement | 9 organizations have their three validators in three different regions; one has two in us-east-1 and one in ap-southeast-1. The layout was identical in both runs |
+| Connectivity | Full mesh: every node is configured with the other 29 as its only peers |
+| Round-trip times seen from us-west-2 | us-east-1 about 60 ms, eu-central-1 about 147 ms, ap-southeast-1 about 180 ms |
+
+### Hardware and software
+
+| | |
+|---|---|
+| Instance type | c6id.2xlarge: 8 vCPU (Intel Ice Lake, with SHA extensions), 16 GiB RAM, local NVMe |
+| Build | Commit `daa965f4c`, the perftests package `30.0.1-3674.daa965f4ce.noble~perf.leader.schedule.no.nomination~perftests`. All 30 nodes reported that commit in their build label |
+| Overlay | The Rust overlay of this stack: QUIC, one connection per peer |
+| Ledger protocol | 30, upgraded from genesis during preparation |
+| Database | SQLite |
+
+### Node configuration
+
+Beyond identity, peers and home domains, every node ran with:
+
+```
+ARTIFICIALLY_GENERATE_LOAD_FOR_TESTING=true
+UNSAFE_QUORUM=true
+FAILURE_SAFETY=0
+GENESIS_TEST_ACCOUNT_COUNT=1000000
+TARGET_PEER_CONNECTIONS=29
+MAX_ADDITIONAL_PEER_CONNECTIONS=87
+PREFERRED_PEERS_ONLY=true
+FLOOD_DEMAND_BACKOFF_DELAY_MS=1000
+INVARIANT_CHECKS=[]
+TESTING_IGNORE_LEDGER_TIME_UPGRADE_BOUNDS=true
+TESTING_MAX_CLASSIC_BYTE_ALLOWANCE=1048576
+TESTING_MAX_SOROBAN_BYTE_ALLOWANCE=9437184
+LOADGEN_PREGENERATED_TRANSACTIONS_FILE="/var/lib/stellar-core/loadgen-txs.xdr"
+OP_APPLY_SLEEP_TIME_DURATION_FOR_TESTING=[25] # [50] in the 1,000 ms run
+OP_APPLY_SLEEP_TIME_WEIGHT_FOR_TESTING=[100]
+```
+
+`EXPERIMENTAL_PARALLEL_TX_SET_DOWNLOAD` was off (the default). Catchup and automatic restart are
+off, so a node that crashes or falls behind stays that way and fails the run.
+
+### Network settings (applied by upgrade before the load)
+
+| Setting | 2,000 ms run | 1,000 ms run |
+|---|---:|---:|
+| Target ledger close time | 2,000 ms | 1,000 ms |
+| SCP timeouts (ballot and nomination, initial and increment) | 2,000 ms | 2,000 ms |
+| Maximum transactions per ledger (125% of the offered load per block) | 7,500 | 3,750 |
+| Maximum tx-set size in bytes | 10 MiB (1 MiB classic + 9 MiB Soroban) | same |
+| Soroban limits | raised to fit the load | raised to fit the load |
+
+The leader timer of this branch uses the ballot timeout, so a round's leader had 2 s (4 s with its
+one extension) in both runs.
+
+### Load
+
+| | |
+|---|---|
+| Transactions | Stellar Asset Contract (SAC) payments, pregenerated; no classic transactions |
+| Rate | 3,000 TPS offered: every validator generates 100 TPS |
+| Accounts | 1,000,000 genesis accounts, 33,333 per generator |
+| Duration | 60 s warm-up, then 900 s measured |
+| Apply | Skipped ("overlay-only" mode) and replaced by a sleep per operation, sized as a 150 ms budget per ledger at the offered load: 25 us per operation at 2,000 ms, 50 us at 1,000 ms |
+
+Commands (the two runs differ only in the block time):
+
+```
+block_time_check.py \
+ --stellar-core-version 30.0.1-3674.daa965f4ce.noble~perf.leader.schedule.no.nomination~perftests \
+ --block-time-ms 2000 \ # 1000 for the second run
+ --soroban-tps 3000 --soroban-type sac --classic-tps 0 \
+ --simulate-apply-budget-ms 150 --load-duration-s 900 \
+ --instance-type c6id.2xlarge --save-logs always
+```
+
+Defaults that matter: 10 organizations, validators as generators, `--scp-timeout-ms 2000`,
+`--max-tx-set-bytes 10485760`, 1,000,000 genesis accounts, seed 0.
+
+### How a run is judged
+
+- Ledger close times are read per node from Prometheus (scraped every 10 s) and judged over
+ 5-minute windows after the warm-up.
+- **PASS** needs, on every node in every window: p75 within [0.8 T, 1.2 T), p99 at most 2 T, and at
+ least one close per 2 T on average, where T is the target block time. It also needs every node's
+ ledgers to hold exactly the offered transactions, zero error counters, all nodes in sync, no
+ fork, and the network still closing ledgers afterwards.
+- The load stops at the first window that misses, so a FAIL can rest on one window.
+
+### Limits of these numbers
+
+- One run per setting, and one baseline run (at 2,000 ms only).
+- Apply is simulated, so these are consensus and overlay numbers, not end-to-end ledger close.
+- `scp.timing.trigger-to-ballot` records 0 when a follower votes before its own trigger fires, so it
+ is not strictly comparable with the nomination build. Triggers were on time in the 2,000 ms run.
+- The per-stage timeline and the fetch times by region come from one node's logs at 2,000 ms and
+ four nodes' logs at 1,000 ms.
+
+## Known limitations of the prototype
+
+- The leader schedule's node set is whatever transitive quorum each node currently knows. It
+ assumes a symmetric, static quorum.
+- Upgrades: a value needs a quorum of nodes that armed the same upgrades; there is no stripping or
+ merging. A follower follows a leader's empty-tx-set ballot under the weaker ballot validation.
+- Leader grinding through the header hash and fairness between organizations are out of scope.
+- The leader timer extension is broader than intended, and a proposal that arrives just after a
+ timeout is wasted. Neither occurred in these runs.
+- A build with `--disable-tests` does not compile at this commit (test headers reached from
+ `herder/HerderImpl.cpp`).
+
+## Building and the rest of the documentation
+
+Unchanged from stellar-core: see [INSTALL.md](./INSTALL.md), [CONTRIBUTING.md](./CONTRIBUTING.md)
+and [`./docs`](./docs). The full test suite passes on `daa965f4c`, also with `--all-versions`.
diff --git a/docs/metrics.md b/docs/metrics.md
index b37a5c24bc..098823e7bc 100644
--- a/docs/metrics.md
+++ b/docs/metrics.md
@@ -64,6 +64,23 @@ crypto.verify.tx-valid-hit | meter | signature cache hits tha
crypto.verify.tx-valid-total | meter | sum of both hits and misses during transaction validation (outside of background signature validation)
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)
+herder.txset.validate-sig-cache-miss | meter | signatures verified (not in the cache) while validating a received tx set
+herder.txset.build-pull | timer | tx set build phase: pulling and decoding the mempool's top transactions
+herder.txset.build-frames | timer | tx set build phase: making transaction frames, splitting them into phases, one per source account
+herder.txset.build-trim | timer | tx set build phase: validating candidates (each phase until it has enough valid ones)
+herder.txset.build-select | timer | tx set build phase: surge pricing and building the parallel Soroban phase
+herder.txset.build-xdr | timer | tx set build phase: building the set's XDR
+herder.txset.build-handoff | timer | tx set build phase: handing the built set to the overlay
+herder.txset.build-roundtrip | timer | tx set build phase: re-reading the built set from its XDR
+herder.txset.build-final-check | timer | tx set build phase: final structural check of the built set
+herder.txset.build-sig-cache-hit | meter | signature cache hits while a leader's build validates its candidates
+herder.txset.build-sig-cache-miss | meter | signatures verified (not in the cache) while a leader's build validates its candidates
+herder.prewarm.pull | timer | main thread time of a signature prewarm's mempool pull (the overlay round trip; decoding runs in the prewarm's tasks)
+herder.prewarm.duration | timer | time from a signature prewarm's start until its background tasks finished (or it was stopped)
+herder.prewarm.txs | meter | transactions whose signatures a signature prewarm checked
+herder.prewarm.verified | meter | signatures a signature prewarm verified (were not in the cache yet)
+herder.prewarm.incomplete | meter | signature prewarms stopped (by a build, a validation or a newer prewarm) before they finished
herder.pending[-soroban]-txs.age0 | counter | number of gen0 pending transactions
herder.pending[-soroban]-txs.age1 | counter | number of gen1 pending transactions
herder.pending[-soroban]-txs.age2 | counter | number of gen2 pending transactions
@@ -130,7 +147,7 @@ overlay.delay.async-write | timer | time between each messag
overlay.delay.write-queue | timer | time between each message's entry and exit from peer write queue
overlay.error.read | meter | error while receiving a message
overlay.error.write | meter | error while sending a message
-overlay.fetch.txset | timer | time to complete fetching of a txset
+overlay.fetch.txset | timer | time the Rust overlay took to fetch a txset from a peer (synced from the overlay as per-batch averages)
overlay.fetch.qset | timer | time to complete fetching of a qset
overlay.fetch.unique-recv | meter | number of bytes of fetched messages that have not yet been received
overlay.fetch.duplicate-recv | meter | number of bytes of fetched messages that have already been received
@@ -181,6 +198,8 @@ scp.envelope.receive | meter | SCP message received
scp.envelope.sign | meter | envelope signed
scp.envelope.validsig | meter | envelope signature verified
scp.fetch.envelope | timer | time to complete fetching of an envelope
+scp.fetch.txset | timer | time from requesting a txset an SCP envelope needs to the overlay delivering it (not counting sets this node built itself)
+scp.fetch.txset-retry | counter | txset requests repeated because the set had not arrived
scp.memory.cumulative-statements | counter | number of known SCP statements known
scp.nomination.combinecandidates | meter | number of candidates per call
scp.pending.discarded | counter | number of discarded envelopes
diff --git a/docs/rust-overlay/ipc.md b/docs/rust-overlay/ipc.md
index dfac36fa36..a96778308a 100644
--- a/docs/rust-overlay/ipc.md
+++ b/docs/rust-overlay/ipc.md
@@ -35,8 +35,14 @@ Source:
| `length` | 4 | Payload length in bytes. Native-endian. |
| `payload`| `length` | Type-specific bytes. May be empty. |
-Maximum payload: **16 MB** (`messages.rs:154`). Frames larger than that
-are rejected with `InvalidData`.
+Maximum payload: **64 MiB** (`MAX_PAYLOAD_SIZE` in `messages.rs`, which must
+equal `ipc::MAX_PAYLOAD_BYTES` in Core's `src/overlay/IPC.h`). Readers reject
+larger frames (`InvalidData` in Rust; Core stops reading the stream), and
+neither side sends one: `CoreSender::send` refuses it with
+`IpcError::PayloadTooLarge`, Core's `sendMessage` returns false, and the
+overlay truncates a top transactions response to the longest prefix that fits.
+A mempool pull of 2x the tx-set capacity plus a pending set's transactions
+(22,700 SAC payments, about 17.2 MB) exceeded the old 16 MiB limit.
> Native-endian is unusual for a wire protocol but safe here because
> Core and Overlay always run on the same host. The libp2p network
diff --git a/docs/rust-overlay/transport.md b/docs/rust-overlay/transport.md
index 55274af13a..95dbefc955 100644
--- a/docs/rust-overlay/transport.md
+++ b/docs/rust-overlay/transport.md
@@ -74,20 +74,15 @@ QUIC connection. Send priorities are set in Quinn, the QUIC implementation:
| Protocol ID | Messages | Send priority |
|-------------|----------|---------------|
| `/stellar/control/1.0.0` | SCP envelopes, `GET_SCP_STATE`, `GET_TX_SET` | **2 — highest** |
-| `/stellar/txset/1.0.0` | Tx-set responses | **1** |
+| `/stellar/txset/zstd/2.0.0` | Compressed tx-set responses | **1** |
| `/stellar/tx/1.0.0` | Transaction INV, GETDATA, TX | **0 — lowest** |
SCP and tx-set requests share one FIFO stream; there is no separate priority
between messages on that stream. `DONT_HAVE` handling is not implemented.
-Older peers remain supported. An explicit rejection of the control protocol
-causes the sender to negotiate `/stellar/scp/1.0.0` for SCP and SCP-state
-requests, at priority 2. Tx-set requests then use a separate stream with the
-legacy `/stellar/txset/1.0.0` protocol, also at priority 2. This fallback opens
-on demand and is reused, preserving request isolation from responses while
-using four outbound streams for old peers. Connection errors do not trigger
-protocol downgrade. Updated receivers continue accepting both legacy
-protocols, including tx-set requests on `/stellar/txset/1.0.0`.
+These routes are mandatory on this experimental branch. There is no fallback
+to the old SCP or transaction-set protocols. Transaction-set responses always
+use zstd; see [TX set fetching](txset-fetching.md) for framing and cache ownership.
In addition, libp2p's `Identify` protocol runs as `/stellar/1.0.0`
(`libp2p_overlay.rs:380`). It exchanges peer-id + listen addresses on
@@ -105,8 +100,7 @@ as independent tasks, so:
- INV batches and TX response data flow on `/stellar/tx/1.0.0` only.
- Between updated peers, tx-set fetch requests share the control stream with
SCP, so a large response to the same peer cannot hold up a request behind
- its frame or stream lock. With legacy peers, a separate request stream
- preserves that isolation as described above.
+ its frame or stream lock.
- Loss recovery on one stream does not require another stream to wait for
that stream's missing bytes.
diff --git a/docs/rust-overlay/txset-fetching.md b/docs/rust-overlay/txset-fetching.md
index 193c26d48b..1e4b899022 100644
--- a/docs/rust-overlay/txset-fetching.md
+++ b/docs/rust-overlay/txset-fetching.md
@@ -6,15 +6,56 @@ it establishes demand with `RequestTxSet`.
## Wire formats and routes
-Network messages use a four-byte big-endian frame length followed by a
-`StellarMessage`. `GetTxSet` contains the 32-byte hash; `GeneralizedTxSet`
-contains the set XDR. The response hash is computed from the canonical bytes
-after strict decoding, without re-encoding the set.
+Requests use a four-byte big-endian frame length followed by a
+`StellarMessage::GetTxSet` containing the 32-byte hash. Responses require
+`/stellar/txset/zstd/2.0.0` and contain an outer four-byte big-endian frame
+length, a four-byte big-endian original XDR length, and exactly one zstd frame.
+There is no raw encoding or legacy protocol fallback on this experimental branch.
+
+The decoder bounds both encoded and original sizes, requires one complete zstd
+frame and an exact output length, and strict-parses `GeneralizedTransactionSet`.
+The response hash is computed from the recovered canonical XDR without
+re-encoding. The XDR limit remains 16 MiB minus four bytes; the encoded-frame
+limit also permits zstd's worst-case expansion for incompressible input.
Requests share the highest-priority control route with SCP. Responses use the
-next-priority tx-set route. Legacy-only peers use separate request and response
-streams under the old tx-set protocol, while SCP uses its old protocol. All
-routes share QUIC connection limits. See [transport](transport.md).
+next-priority tx-set route. All routes share QUIC connection limits.
+See [transport](transport.md).
+
+## Encoding ownership
+
+Only nomination leaders construct local proposals. Followers start SCP with a
+lazy value supplier and vote for received leader proposals without building a
+private set or compressing it. At the trigger a follower pulls its mempool's top
+transactions once and verifies their master-key signatures on background
+threads, so that validating the leader's set hits the signature cache (cold, it
+costs about six times as much). The prewarm stops when validation of a received
+set or a local build starts. A node promoted by a
+nomination timeout builds only if SCP needs its own value; it rechecks ledger
+state and chooses a current close time then. Subsequent rounds reuse that value.
+First-round leaders may prepare before the trigger; later leaders do no
+speculative preparation. Nomination stops on externalization even for followers
+that never constructed a local value.
+
+Ledgers closing on a pre-protocol-20 LCL are the exception. Their legacy
+`TransactionSet`s cannot be carried by this overlay, so every validator builds
+its own value at the trigger, as before leader-only construction. With an empty
+mempool (genesis and the upgrade-only ledgers before protocol 20) the set depends
+only on the previous ledger, so each validator already holds the leader's set.
+
+Core sends `CacheTxSet` as soon as a locally constructed proposal's final XDR is
+available, before the builder's roundtrip and final validation. Rust eagerly
+compresses it at zstd level 1 on a blocking worker while Core continues those
+checks. App awaits the prepared representation before processing another IPC
+message or application event; the network dispatcher remains independent.
+Repeated local publication of an already cached hash reuses its encoding.
+
+A receiving overlay retains the original encoded allocation while decompressing
+and strict-parsing on a blocking worker. The cache shares an immutable
+`Arc` containing both original XDR and encoded bytes. Peer responses
+borrow those encoded bytes directly, so relays never recompress received sets.
+Encoding is complete before cache insertion; sending has no lazy initialization
+or compression decision. Core continues receiving uncompressed XDR over IPC.
## Core requests and network prefetches
@@ -49,13 +90,21 @@ the attempt returns without reserving the hash.
A failed write removes only its own reservation. Disconnect cleanup removes
reservations assigned to that peer. A received response clears its matching
-reservation and records fetch latency and the requested slot. This does not
-provide a timeout or automatic retry for a silent peer.
+reservation and records fetch latency and the requested slot. `LedgerClosed`
+drops reservations older than the retained-slot window (`sequence - 12`).
+
+Rust has no request timer. Core repeats a `RequestTxSet` for a set it has not
+received after one second, then every second, logging a warning on every
+doubling of the request count (and counting `scp.fetch.txset-retry`).
+A request for a hash Core is still waiting for is a retry: it replaces the
+reservation and goes to another connected peer, preferring the SCP source only
+if it was not the previous target, else a random one. Core stops fetching when
+the slots waiting for the set are purged.
## Cache and externalization
The cache holds up to 100 sets in App. Each entry contains its content hash,
-canonical XDR, and ledger sequence. Capacity eviction uses insertion order;
+canonical XDR, encoded response, and ledger sequence. Capacity eviction uses insertion order;
updating an existing hash does not move it. `LedgerClosed` evicts entries older
than `sequence - 12`, using saturating subtraction. Pending Core demand expires
against the same retained-slot boundary.
@@ -72,6 +121,8 @@ See [mempool](mempool.md).
## Remaining limitations
-- A peer that stays connected but silent can leave a fetch pending indefinitely.
-- A cache miss at the selected peer has no explicit negative response or retry.
+- A cache miss at the selected peer has no explicit negative response; the
+ requester only notices through Core's one-second retry.
- Selecting one connected peer does not guarantee it has the requested body.
+- Legacy (pre-protocol 20) `TransactionSet`s cannot be fetched at all; see
+ encoding ownership.
diff --git a/overlay/Cargo.toml b/overlay/Cargo.toml
index 04b665747e..1ad9ab16af 100644
--- a/overlay/Cargo.toml
+++ b/overlay/Cargo.toml
@@ -29,6 +29,9 @@ libp2p = { version = "0.54", features = ["tokio", "macros", "identify", "quic"]
libp2p-stream = "0.2.0-alpha"
futures = "0.3"
+# TX set response compression
+zstd = { version = "0.13.3", default-features = false }
+
# Logging
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
diff --git a/overlay/src/flood/mod.rs b/overlay/src/flood/mod.rs
index 9e796310e4..ecefbc36db 100644
--- a/overlay/src/flood/mod.rs
+++ b/overlay/src/flood/mod.rs
@@ -9,11 +9,13 @@ mod mempool;
mod pending_requests;
mod tx_buffer;
mod txset;
+pub(crate) mod txset_encoding;
pub use inv_batcher::InvBatcher;
-pub use inv_messages::{GetData, InvBatch, InvEntry, TxStreamMessage};
+pub use inv_messages::{GetData, InvBatch, InvEntry, TxStreamMessage, INV_BATCH_MAX_SIZE};
pub use inv_tracker::InvTracker;
pub use mempool::Mempool;
pub use pending_requests::PendingRequests;
pub use tx_buffer::TxBuffer;
pub use txset::{CachedTxSet, Hash256, TxSetCache};
+pub use txset_encoding::TxSetData;
diff --git a/overlay/src/flood/txset.rs b/overlay/src/flood/txset.rs
index d8d487a52f..95beb3c44b 100644
--- a/overlay/src/flood/txset.rs
+++ b/overlay/src/flood/txset.rs
@@ -1,5 +1,6 @@
//! TX set cache for peer request/response handling.
+use super::TxSetData;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
@@ -11,9 +12,9 @@ pub type Hash256 = [u8; 32];
pub struct CachedTxSet {
/// The TX set hash (SHA256 of XDR)
pub hash: Hash256,
- /// Immutable XDR shared by the cache and pending peer responses. Keep the
- /// Vec allocation when caching; fanout clones only the Arc.
- pub xdr: Arc>,
+ /// Immutable XDR and prepared zstd response shared by the cache and
+ /// pending peer responses. Fanout clones only the Arc.
+ pub xdr: Arc,
/// Ledger sequence this was built for
pub ledger_seq: u32,
}
@@ -85,7 +86,7 @@ mod tests {
let tx_set = CachedTxSet {
hash: [1u8; 32],
- xdr: vec![1, 2, 3].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![1, 2, 3]).unwrap()),
ledger_seq: 100,
};
@@ -105,12 +106,12 @@ mod tests {
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()),
ledger_seq: 100,
});
cache.insert(CachedTxSet {
hash: [2u8; 32],
- xdr: vec![].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()),
ledger_seq: 200,
});
@@ -126,12 +127,12 @@ mod tests {
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()),
ledger_seq: 100,
});
cache.insert(CachedTxSet {
hash: [2u8; 32],
- xdr: vec![].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()),
ledger_seq: 101,
});
@@ -140,7 +141,7 @@ mod tests {
// Insert 3rd - should evict one
cache.insert(CachedTxSet {
hash: [3u8; 32],
- xdr: vec![].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()),
ledger_seq: 102,
});
@@ -165,14 +166,14 @@ mod tests {
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![1, 2, 3].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![1, 2, 3]).unwrap()),
ledger_seq: 100,
});
// Insert with same hash but different data
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![4, 5, 6].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![4, 5, 6]).unwrap()),
ledger_seq: 200,
});
@@ -188,7 +189,7 @@ mod tests {
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![1, 2, 3].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![1, 2, 3]).unwrap()),
ledger_seq: 100,
});
assert_eq!(cache.len(), 1, "Cache is full");
@@ -197,7 +198,7 @@ mod tests {
// not evict the entry or shrink the cache below capacity.
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![4, 5, 6].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![4, 5, 6]).unwrap()),
ledger_seq: 102,
});
@@ -216,12 +217,12 @@ mod tests {
// Insert, then overwrite the same hash with a newer ledger_seq.
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![1, 2, 3].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![1, 2, 3]).unwrap()),
ledger_seq: 100,
});
cache.insert(CachedTxSet {
hash: [1u8; 32],
- xdr: vec![4, 5, 6].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![4, 5, 6]).unwrap()),
ledger_seq: 200,
});
@@ -239,12 +240,12 @@ mod tests {
// cache exactly at capacity (not one over).
cache.insert(CachedTxSet {
hash: [2u8; 32],
- xdr: vec![].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()),
ledger_seq: 300,
});
cache.insert(CachedTxSet {
hash: [3u8; 32],
- xdr: vec![].into(),
+ xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()),
ledger_seq: 400,
});
diff --git a/overlay/src/flood/txset_encoding.rs b/overlay/src/flood/txset_encoding.rs
new file mode 100644
index 0000000000..a9bab83f9f
--- /dev/null
+++ b/overlay/src/flood/txset_encoding.rs
@@ -0,0 +1,176 @@
+//! Immutable TX set bytes and their mandatory zstd response encoding.
+//!
+//! Inside the length-prefixed response: a big-endian u32 XDR length followed
+//! by exactly one zstd frame. Only locally built sets are compressed; received
+//! sets retain the original response bytes for forwarding.
+use std::io;
+
+pub(crate) const MAX_TXSET_XDR_SIZE: usize = 16 * 1024 * 1024 - 4;
+
+/// Allow zstd's worst-case expansion while retaining the original XDR limit.
+pub(crate) fn max_encoded_size() -> usize {
+ 4 + zstd::zstd_safe::compress_bound(MAX_TXSET_XDR_SIZE)
+}
+
+#[derive(Debug)]
+pub struct TxSetData {
+ xdr: Vec,
+ encoded: Vec,
+}
+
+impl TxSetData {
+ /// Compress a locally constructed set eagerly, on a blocking worker.
+ pub fn from_local(xdr: Vec) -> io::Result {
+ if xdr.len() > MAX_TXSET_XDR_SIZE {
+ return Err(io::Error::new(
+ io::ErrorKind::InvalidInput,
+ "TX set exceeds XDR limit",
+ ));
+ }
+ let compressed = zstd::bulk::compress(&xdr, 1)?;
+ let mut encoded = Vec::with_capacity(4 + compressed.len());
+ encoded.extend_from_slice(&(xdr.len() as u32).to_be_bytes());
+ encoded.extend_from_slice(&compressed);
+ Ok(Self { xdr, encoded })
+ }
+
+ pub fn as_slice(&self) -> &[u8] {
+ &self.xdr
+ }
+
+ pub fn len(&self) -> usize {
+ self.xdr.len()
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.xdr.is_empty()
+ }
+
+ /// Borrow the exact cached response; sending never invokes a compressor.
+ pub fn encoded(&self) -> &[u8] {
+ &self.encoded
+ }
+}
+
+/// Decode on a blocking worker and retain the received allocation unchanged.
+/// The caller must also strict-parse the recovered GeneralizedTransactionSet.
+pub(crate) fn decode(frame: Vec) -> io::Result {
+ let invalid = || io::Error::new(io::ErrorKind::InvalidData, "invalid TX set encoding");
+ if frame.len() < 4 || frame.len() > max_encoded_size() {
+ return Err(invalid());
+ }
+ let length = u32::from_be_bytes(frame[..4].try_into().unwrap()) as usize;
+ // Check before creating a decoder or allocating its output.
+ if length > MAX_TXSET_XDR_SIZE {
+ return Err(invalid());
+ }
+ let compressed = &frame[4..];
+ if zstd::zstd_safe::find_frame_compressed_size(compressed).map_err(|_| invalid())?
+ != compressed.len()
+ {
+ return Err(invalid());
+ }
+ let mut decoder = zstd::bulk::Decompressor::new()?;
+ decoder.window_log_max(24)?;
+ let xdr = decoder.decompress(compressed, length)?;
+ if xdr.len() != length {
+ return Err(invalid());
+ }
+ Ok(TxSetData {
+ xdr,
+ encoded: frame,
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use rand::{RngCore, SeedableRng};
+ use std::sync::Arc;
+
+ #[test]
+ fn always_compresses_including_empty_and_incompressible_sets() {
+ let mut random = vec![0; MAX_TXSET_XDR_SIZE];
+ rand::rngs::StdRng::seed_from_u64(17).fill_bytes(&mut random);
+ for xdr in [vec![], vec![17], random, vec![0; MAX_TXSET_XDR_SIZE]] {
+ let data = TxSetData::from_local(xdr.clone()).unwrap();
+ let frame = data.encoded().to_vec();
+ assert!(frame.len() <= max_encoded_size());
+ assert_eq!(
+ u32::from_be_bytes(frame[..4].try_into().unwrap()) as usize,
+ xdr.len()
+ );
+ assert_eq!(zstd::bulk::decompress(&frame[4..], xdr.len()).unwrap(), xdr);
+ let received = decode(frame.clone()).unwrap();
+ assert_eq!(received.as_slice(), xdr);
+ assert_eq!(received.encoded(), frame);
+ }
+ }
+
+ #[test]
+ fn fanout_shares_the_prepared_encoding() {
+ let data = Arc::new(TxSetData::from_local(vec![13; 4 * 1024 * 1024]).unwrap());
+ let peers: Vec<_> = (0..29).map(|_| Arc::clone(&data)).collect();
+ for peer in peers {
+ assert!(std::ptr::eq(
+ data.encoded().as_ptr(),
+ peer.encoded().as_ptr()
+ ));
+ }
+ }
+
+ #[test]
+ fn received_encoding_is_retained_without_recompression() {
+ let xdr = vec![13; 4 * 1024 * 1024];
+ // Deliberately use another compression level. Relays must preserve these
+ // bytes even when their own encoder would produce a different frame.
+ let frame = [
+ (xdr.len() as u32).to_be_bytes().as_slice(),
+ zstd::bulk::compress(&xdr, 7).unwrap().as_slice(),
+ ]
+ .concat();
+ let allocation = frame.as_ptr();
+ let expected = frame.clone();
+ let received = decode(frame).unwrap();
+ assert_eq!(received.as_slice(), xdr);
+ assert_eq!(received.encoded(), expected);
+ assert_eq!(received.encoded().as_ptr(), allocation);
+ }
+
+ #[test]
+ fn rejects_truncation_corruption_length_mismatch_and_legacy_raw() {
+ let frame = TxSetData::from_local(vec![13; 4096]).unwrap().encoded;
+ for end in 0..frame.len() {
+ assert!(decode(frame[..end].to_vec()).is_err());
+ }
+ let mut corrupt = frame.clone();
+ corrupt[4] ^= 0xff;
+ assert!(decode(corrupt).is_err());
+ for length in [0, 4095, 4097, MAX_TXSET_XDR_SIZE + 1, 4096 | (1 << 31)] {
+ let mut bad = frame.clone();
+ bad[..4].copy_from_slice(&(length as u32).to_be_bytes());
+ assert!(decode(bad).is_err());
+ }
+ let mut trailing = frame.clone();
+ trailing.push(0);
+ assert!(decode(trailing).is_err());
+ let mut concatenated = frame.clone();
+ concatenated.extend_from_slice(&frame[4..]);
+ assert!(decode(concatenated).is_err());
+ let raw = [19u32.to_be_bytes().as_slice(), &[19; 19]].concat();
+ assert!(decode(raw).is_err());
+ }
+
+ #[test]
+ fn compression_does_not_allow_oversized_raw_sets() {
+ let oversized = vec![0; MAX_TXSET_XDR_SIZE + 1];
+ assert!(TxSetData::from_local(oversized.clone()).is_err());
+ let frame = [
+ (oversized.len() as u32).to_be_bytes().as_slice(),
+ zstd::bulk::compress(&oversized, 1).unwrap().as_slice(),
+ ]
+ .concat();
+ assert!(decode(frame).is_err());
+ assert!(decode(vec![0; max_encoded_size() + 1]).is_err());
+ }
+}
diff --git a/overlay/src/ipc/messages.rs b/overlay/src/ipc/messages.rs
index 1313186826..9930f96138 100644
--- a/overlay/src/ipc/messages.rs
+++ b/overlay/src/ipc/messages.rs
@@ -127,8 +127,13 @@ impl Message {
}
}
-/// Maximum payload size (16 MB) - sanity check to prevent OOM
-const MAX_PAYLOAD_SIZE: usize = 16 * 1024 * 1024;
+/// Largest payload either side accepts in one message (64 MiB); a larger
+/// length is treated as a corrupt stream and ends the connection. Must equal
+/// `ipc::MAX_PAYLOAD_BYTES` in Core's src/overlay/IPC.h. A mempool pull (up to
+/// 2x the tx-set capacity plus a pending set's transactions, e.g. 22,700 SAC
+/// payments of ~750 bytes) must fit; `CoreSender` never sends a larger payload
+/// and truncates top-transaction responses to this size.
+pub(crate) const MAX_PAYLOAD_SIZE: usize = 64 * 1024 * 1024;
/// Header size: 4 bytes type + 4 bytes length
const HEADER_SIZE: usize = 8;
@@ -228,7 +233,7 @@ mod tests {
// Create a message with payload size > MAX_PAYLOAD_SIZE
let mut buf = Vec::new();
buf.extend_from_slice(&1u32.to_ne_bytes()); // BroadcastScp
- buf.extend_from_slice(&(20 * 1024 * 1024u32).to_ne_bytes()); // 20MB > 16MB limit
+ buf.extend_from_slice(&((MAX_PAYLOAD_SIZE + 1) as u32).to_ne_bytes()); // over the limit
let mut cursor = Cursor::new(buf);
let result = MessageCodec::read(&mut cursor);
diff --git a/overlay/src/ipc/transport.rs b/overlay/src/ipc/transport.rs
index 7c96aa613f..c62e013b96 100644
--- a/overlay/src/ipc/transport.rs
+++ b/overlay/src/ipc/transport.rs
@@ -6,9 +6,9 @@ use std::os::unix::net::{UnixListener, UnixStream};
use std::path::Path;
use std::sync::Arc;
use tokio::sync::mpsc;
-use tracing::{debug, error, info};
+use tracing::{debug, error, info, warn};
-use super::messages::{Message, MessageCodec, MessageType};
+use super::messages::{Message, MessageCodec, MessageType, MAX_PAYLOAD_SIZE};
/// Error type for IPC operations
#[derive(Debug)]
@@ -16,6 +16,9 @@ pub enum IpcError {
Io(std::io::Error),
ConnectionClosed,
ChannelClosed,
+ /// A payload over `MAX_PAYLOAD_SIZE`: Core would reject the message and
+ /// drop the connection, so it isn't sent.
+ PayloadTooLarge(usize),
}
impl std::fmt::Display for IpcError {
@@ -24,6 +27,11 @@ impl std::fmt::Display for IpcError {
IpcError::Io(e) => write!(f, "IPC I/O error: {}", e),
IpcError::ConnectionClosed => write!(f, "IPC connection closed"),
IpcError::ChannelClosed => write!(f, "IPC channel closed"),
+ IpcError::PayloadTooLarge(n) => write!(
+ f,
+ "IPC payload of {} bytes is over the {} byte limit; not sent",
+ n, MAX_PAYLOAD_SIZE
+ ),
}
}
}
@@ -47,6 +55,33 @@ impl From for IpcError {
}
}
+/// Encode a top transactions response, [count:4] then [len:4][tx:len] per
+/// transaction, from the longest prefix of `txs` whose encoding fits in
+/// `max_payload` bytes. Returns the payload and how many transactions it
+/// holds.
+fn encode_top_txs_payload(txs: &[&[u8]], max_payload: usize) -> (Vec, usize) {
+ let mut size = 4;
+ let mut included = 0;
+ for tx in txs {
+ if size + 4 + tx.len() > max_payload {
+ break;
+ }
+ size += 4 + tx.len();
+ included += 1;
+ }
+ let mut payload = Vec::with_capacity(size);
+
+ // Count
+ payload.extend_from_slice(&(included as u32).to_le_bytes());
+
+ // Each TX: [len:4][data:len]
+ for tx in &txs[..included] {
+ payload.extend_from_slice(&(tx.len() as u32).to_le_bytes());
+ payload.extend_from_slice(tx);
+ }
+ (payload, included)
+}
+
/// Handle for sending messages to Core.
#[derive(Clone)]
pub struct CoreSender {
@@ -54,8 +89,13 @@ pub struct CoreSender {
}
impl CoreSender {
- /// Send a message to Core. Never blocks.
+ /// Send a message to Core. Never blocks. A payload over
+ /// `MAX_PAYLOAD_SIZE` is refused rather than sent: Core would treat it as
+ /// a corrupt stream and close the connection.
pub fn send(&self, msg: Message) -> Result<(), IpcError> {
+ if msg.payload.len() > MAX_PAYLOAD_SIZE {
+ return Err(IpcError::PayloadTooLarge(msg.payload.len()));
+ }
self.tx.send(msg).map_err(|_| IpcError::ChannelClosed)
}
@@ -66,19 +106,18 @@ impl CoreSender {
/// Convenience: send top transactions response
/// Payload: [count:4][len1:4][tx1:len1][len2:4][tx2:len2]...
+ /// The response holds the longest prefix of `txs` that fits in
+ /// `MAX_PAYLOAD_SIZE` (the highest-priority transactions come first).
pub fn send_top_txs_response(&self, txs: &[&[u8]]) -> Result<(), IpcError> {
- let total_size: usize = 4 + txs.iter().map(|tx| 4 + tx.len()).sum::();
- let mut payload = Vec::with_capacity(total_size);
-
- // Count
- payload.extend_from_slice(&(txs.len() as u32).to_le_bytes());
-
- // Each TX: [len:4][data:len]
- for tx in txs {
- payload.extend_from_slice(&(tx.len() as u32).to_le_bytes());
- payload.extend_from_slice(tx);
+ let (payload, included) = encode_top_txs_payload(txs, MAX_PAYLOAD_SIZE);
+ if included < txs.len() {
+ warn!(
+ "Top transactions response truncated to {} of {} transactions ({} bytes) to fit the IPC payload limit",
+ included,
+ txs.len(),
+ payload.len()
+ );
}
-
self.send(Message::new(MessageType::TopTxsResponse, payload))
}
@@ -735,4 +774,46 @@ mod tests {
let count = u32::from_le_bytes(received.payload[0..4].try_into().unwrap());
assert_eq!(count, 2);
}
+
+ #[test]
+ fn test_encode_top_txs_payload_truncates_to_limit() {
+ let tx1 = vec![0x11; 10];
+ let tx2 = vec![0x22; 10];
+ let tx3 = vec![0x33; 10];
+ let txs: Vec<&[u8]> = vec![&tx1, &tx2, &tx3];
+
+ // Everything fits: 4 + 3 * (4 + 10) = 46 bytes.
+ let (payload, included) = encode_top_txs_payload(&txs, 46);
+ assert_eq!(included, 3);
+ assert_eq!(payload.len(), 46);
+ assert_eq!(u32::from_le_bytes(payload[0..4].try_into().unwrap()), 3);
+
+ // One byte short: only the first two fit, and the count says so.
+ let (payload, included) = encode_top_txs_payload(&txs, 45);
+ assert_eq!(included, 2);
+ assert_eq!(payload.len(), 32);
+ assert_eq!(u32::from_le_bytes(payload[0..4].try_into().unwrap()), 2);
+ assert_eq!(&payload[8..18], &tx1[..]);
+ assert_eq!(&payload[22..32], &tx2[..]);
+
+ // Nothing fits but the count.
+ let (payload, included) = encode_top_txs_payload(&txs, 17);
+ assert_eq!(included, 0);
+ assert_eq!(payload, 0u32.to_le_bytes().to_vec());
+ }
+
+ #[tokio::test]
+ async fn test_send_refuses_payload_over_limit() {
+ let (overlay_side, _core_side) = StdUnixStream::pair().unwrap();
+ let ipc = CoreIpc::from_stream(overlay_side).unwrap();
+
+ let msg = Message::new(MessageType::ScpReceived, vec![0u8; MAX_PAYLOAD_SIZE + 1]);
+ match ipc.sender.send(msg) {
+ Err(IpcError::PayloadTooLarge(n)) => assert_eq!(n, MAX_PAYLOAD_SIZE + 1),
+ other => panic!("expected PayloadTooLarge, got {:?}", other.map(|_| ())),
+ }
+ // A normal payload is still sent.
+ let msg = Message::new(MessageType::ScpReceived, vec![1, 2, 3]);
+ assert!(ipc.sender.send(msg).is_ok());
+ }
}
diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs
index 3ec67a470a..a46b6e56b9 100644
--- a/overlay/src/libp2p_overlay.rs
+++ b/overlay/src/libp2p_overlay.rs
@@ -12,7 +12,8 @@
//! QUIC provides independent loss recovery per stream.
use crate::flood::{
- GetData, InvBatch, InvBatcher, InvEntry, InvTracker, PendingRequests, TxBuffer, TxStreamMessage,
+ GetData, InvBatch, InvBatcher, InvEntry, InvTracker, PendingRequests, TxBuffer, TxSetData,
+ TxStreamMessage,
};
#[cfg(test)]
use crate::integrated::CoreCommand;
@@ -42,14 +43,14 @@ use tracing::{debug, error, info, trace, warn};
mod framed_io;
// Protocol identifiers for dedicated streams
-pub const SCP_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/scp/1.0.0");
pub const CONTROL_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/control/1.0.0");
pub const TX_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/tx/1.0.0");
-pub const TXSET_PROTOCOL: StreamProtocol = StreamProtocol::new("/stellar/txset/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");
/// Message frame: 4-byte length prefix + payload
/// Max message size: 16MB (for large TX sets)
-const MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024;
+const MAX_MESSAGE_SIZE: usize = crate::flood::txset_encoding::MAX_TXSET_XDR_SIZE + 4;
// Bound queued + active bulk sends. Admission waits in the caller, never in
// the swarm loop: a backpressured peer must not stop SCP or swarm polling.
@@ -72,7 +73,7 @@ pub enum OverlayEvent {
/// Received TX set response
TxSetReceived {
hash: [u8; 32],
- data: Vec,
+ data: Arc,
from: PeerId,
/// Slot the set was requested for; None if the response was
/// unsolicited (no pending request recorded).
@@ -95,12 +96,19 @@ pub enum OverlayCommand {
BroadcastScp(Vec),
/// Broadcast a validated TX to all peers
BroadcastTx(Arc),
- /// Request TX set from a peer (picks best peer)
- FetchTxSet { hash: [u8; 32], slot: u32 },
+ /// Request TX set from a peer (picks best peer). `retry` re-requests a
+ /// set that a previous request has not delivered, from another peer.
+ FetchTxSet {
+ hash: [u8; 32],
+ slot: u32,
+ retry: bool,
+ },
+ /// Drop TX set request reservations for slots below `min_slot`
+ PruneTxSetRequests { min_slot: u32 },
/// Send TX set to a specific peer (response to their request)
SendTxSet {
hash: [u8; 32],
- data: Arc>,
+ data: Arc,
to: PeerId,
permits: (OwnedSemaphorePermit, OwnedSemaphorePermit),
},
@@ -132,15 +140,11 @@ struct PeerOutboundStreams {
scp: Mutex>,
tx: Mutex >,
txset: Mutex >,
- // Only opened for peers that reject the combined control protocol. Their
- // requests still need isolation from response writes, using the old ID.
- legacy_txset_request: Mutex >,
}
struct OutboundStream {
stream: Stream,
- // Remember negotiation on the cached stream, including after reopening.
- // Legacy peers accept requests only on their TxSet protocol.
+ #[cfg(test)]
protocol: StreamProtocol,
}
@@ -150,7 +154,6 @@ impl PeerOutboundStreams {
scp: Mutex::new(None),
tx: Mutex::new(None),
txset: Mutex::new(None),
- legacy_txset_request: Mutex::new(None),
}
}
}
@@ -213,9 +216,20 @@ impl OverlayHandle {
}
pub async fn fetch_txset(&self, hash: [u8; 32], slot: u32) {
+ self.send_fetch_txset(hash, slot, false).await;
+ }
+
+ /// Re-request a set an earlier request has not delivered. Unlike
+ /// `fetch_txset`, this replaces an outstanding reservation and prefers
+ /// another connected peer.
+ pub async fn refetch_txset(&self, hash: [u8; 32], slot: u32) {
+ self.send_fetch_txset(hash, slot, true).await;
+ }
+
+ async fn send_fetch_txset(&self, hash: [u8; 32], slot: u32, retry: bool) {
if let Err(e) = self
.cmd_tx
- .send(OverlayCommand::FetchTxSet { hash, slot })
+ .send(OverlayCommand::FetchTxSet { hash, slot, retry })
.await
{
warn!(
@@ -225,8 +239,23 @@ impl OverlayHandle {
}
}
- pub async fn send_txset(&self, hash: [u8; 32], data: impl Into>>, to: PeerId) {
- let data = data.into();
+ /// Forget TX set requests for slots below `min_slot`. A request that is
+ /// never answered (e.g. a set no peer can serve) otherwise stays reserved
+ /// until its peer disconnects.
+ pub async fn prune_txset_requests(&self, min_slot: u32) {
+ if let Err(e) = self
+ .cmd_tx
+ .send(OverlayCommand::PruneTxSetRequests { min_slot })
+ .await
+ {
+ warn!(
+ "Overlay command channel closed, failed to send PruneTxSetRequests: {}",
+ e
+ );
+ }
+ }
+
+ pub async fn send_txset(&self, hash: [u8; 32], data: Arc, to: PeerId) {
// Include the StellarMessage discriminant in the existing wire limit.
if data.len() > MAX_MESSAGE_SIZE - 4 {
warn!(
@@ -539,73 +568,21 @@ impl StellarOverlay {
self.run_event_loop().await;
}
- pub async fn run_event_loop(self) {
- self.run_event_loop_with_control(
- #[cfg(test)]
- true,
- )
- .await;
- }
-
- // Keeping the legacy-only mode in this helper lets interoperability tests
- // exercise real protocol negotiation; production always advertises control.
- async fn run_event_loop_with_control(mut self, #[cfg(test)] accept_control: bool) {
- #[cfg(not(test))]
- let accept_control = true;
- // Accept incoming streams for each protocol
- let scp_incoming = match self.control.accept(SCP_PROTOCOL) {
- Ok(incoming) => incoming,
- Err(e) => {
- error!(
- "Failed to accept SCP protocol streams: {:?}. Overlay cannot function.",
- e
- );
- return;
- }
- };
- let tx_incoming = match self.control.accept(TX_PROTOCOL) {
- Ok(incoming) => incoming,
- Err(e) => {
- error!(
- "Failed to accept TX protocol streams: {:?}. Overlay cannot function.",
- e
- );
- return;
- }
- };
- let txset_incoming = match self.control.accept(TXSET_PROTOCOL) {
- Ok(incoming) => incoming,
- Err(e) => {
- error!(
- "Failed to accept TxSet protocol streams: {:?}. Overlay cannot function.",
- e
- );
- return;
- }
- };
-
- let control_incoming = if accept_control {
- match self.control.accept(CONTROL_PROTOCOL) {
- Ok(incoming) => Some(incoming),
- Err(e) => {
- error!("Failed to accept control protocol: {:?}", e);
- return;
- }
- }
- } else {
- None
- };
-
- // Spawn inbound stream handlers
+ pub async fn run_event_loop(mut self) {
+ let control_incoming = self
+ .control
+ .accept(CONTROL_PROTOCOL)
+ .expect("control protocol registered once");
+ let tx_incoming = self
+ .control
+ .accept(TX_PROTOCOL)
+ .expect("TX protocol registered once");
+ let txset_incoming = self
+ .control
+ .accept(TXSET_PROTOCOL)
+ .expect("TxSet protocol registered once");
let state = self.state.clone();
- tokio::spawn(handle_inbound_scp_streams(
- scp_incoming,
- state.clone(),
- false,
- ));
- if let Some(incoming) = control_incoming {
- tokio::spawn(handle_inbound_scp_streams(incoming, state.clone(), true));
- }
+ 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()));
@@ -631,8 +608,11 @@ impl StellarOverlay {
OverlayCommand::BroadcastTx(tx) => {
self.broadcast_tx(tx).await;
}
- OverlayCommand::FetchTxSet { hash, slot } => {
- self.fetch_txset(hash, slot).await;
+ OverlayCommand::FetchTxSet { hash, slot, retry } => {
+ self.fetch_txset(hash, slot, retry).await;
+ }
+ OverlayCommand::PruneTxSetRequests { min_slot } => {
+ self.prune_txset_requests(min_slot).await;
}
OverlayCommand::SendTxSet { hash, data, to, permits } => {
let state = Arc::clone(&self.state);
@@ -999,81 +979,68 @@ impl StellarOverlay {
.flood_advertised
.fetch_add(peers.len() as u64, Ordering::Relaxed);
- let inv_entry = InvEntry { hash, fee_per_op };
-
- // Add to batcher for each peer, send batch immediately when full
- for peer in &peers {
- let batch_to_send = {
- let mut batcher = self.state.inv_batcher.write().await;
- batcher.add(*peer, inv_entry.clone())
- };
- if let Some(batch) = batch_to_send {
- send_inv_batch(&self.state, *peer, batch).await;
- }
- }
+ queue_inv(&self.state, &peers, InvEntry { hash, fee_per_op }).await;
}
- /// Fetch TX set from a peer - preferring the peer who sent us the SCP message referencing it
- async fn fetch_txset(&mut self, hash: [u8; 32], slot: u32) {
- // Check if we're already fetching this TxSet from a connected peer (dedup)
- {
+ /// Fetch TX set from a peer - preferring the peer who sent us the SCP message referencing it.
+ /// A `retry` replaces an outstanding request (which never delivered) and
+ /// goes to a random connected peer other than the previous one: that may
+ /// have been silent or not had the set, and there is no negative response.
+ async fn fetch_txset(&mut self, hash: [u8; 32], slot: u32, retry: bool) {
+ // Read each shared map on its own; never nest these locks.
+ let previous_peer = {
let pending = self.state.pending_txset_requests.read().await;
- if let Some((pending_peer, _, _)) = pending.get(&hash) {
- // Check if that peer is still connected
- let streams = self.state.peer_streams.read().await;
- if streams.contains_key(pending_peer) {
- debug!(
- "TXSET_FETCH_SKIP: TxSet {:02x?}... already being fetched from {}, skipping duplicate",
- &hash[..4], pending_peer
- );
- return;
- }
- // Otherwise, peer disconnected - we'll re-request below
- }
- }
-
- // First check if we know which peer has this TX set (from SCP message)
+ pending.get(&hash).map(|(peer, _, _)| *peer)
+ };
let known_source = {
let sources = self.state.txset_sources.read().await;
sources.peek(&hash).cloned()
};
-
- let peer = if let Some(source_peer) = known_source {
- // Verify this peer is still connected
- let streams = self.state.peer_streams.read().await;
- if streams.contains_key(&source_peer) {
- info!(
- "TXSET_FETCH: Fetching TX set {:02x?}... from known source {}",
- &hash[..4],
- source_peer
+ let streams = self.state.peer_streams.read().await;
+ // Check if we're already fetching this TxSet from a connected peer (dedup)
+ if let Some(pending_peer) = previous_peer {
+ if !retry && streams.contains_key(&pending_peer) {
+ debug!(
+ "TXSET_FETCH_SKIP: TxSet {:02x?}... already being fetched from {}, skipping duplicate",
+ &hash[..4], pending_peer
);
- source_peer
- } else {
- // Source peer disconnected, fall back to any peer
- match streams.keys().next().cloned() {
- Some(p) => {
- info!("TXSET_FETCH: Fetching TX set {:02x?}... from fallback peer {} (source {} disconnected)",
- &hash[..4], p, source_peer);
- p
- }
- None => {
- warn!(
- "TXSET_FETCH_FAIL: No peers to fetch TX set {:02x?}... from",
- &hash[..4]
- );
- return;
- }
- }
+ return;
}
+ // Otherwise the peer disconnected or the request is being retried.
+ }
+ // Avoid the previous peer on a retry unless it is the only one left.
+ let avoid = previous_peer.filter(|p| retry && streams.keys().any(|other| other != p));
+ let eligible = |p: &PeerId| streams.contains_key(p) && Some(*p) != avoid;
+
+ // Prefer the peer that referenced the set in an SCP message. A retry
+ // picks a random other peer instead: preferring the source again would
+ // send every other retry back to a source that may not answer.
+ let peer = if let Some(source_peer) = known_source.filter(|p| !retry && eligible(p)) {
+ info!(
+ "TXSET_FETCH: Fetching TX set {:02x?}... from known source {}{}",
+ &hash[..4],
+ source_peer,
+ if retry { " (retry)" } else { "" }
+ );
+ source_peer
} else {
- // No known source, pick any connected peer
- let streams = self.state.peer_streams.read().await;
- match streams.keys().next().cloned() {
+ // No usable known source: pick a connected peer, at random on a
+ // retry so repeated retries spread over the peers.
+ let mut candidates = streams.keys().filter(|p| eligible(p));
+ let choice = if retry {
+ use rand::seq::IteratorRandom;
+ candidates.choose(&mut rand::thread_rng()).cloned()
+ } else {
+ candidates.next().cloned()
+ };
+ match choice {
Some(p) => {
info!(
- "TXSET_FETCH: Fetching TX set {:02x?}... from random peer {} (no known source)",
+ "TXSET_FETCH: Fetching TX set {:02x?}... from peer {} (source {:?}{})",
&hash[..4],
- p
+ p,
+ known_source,
+ if retry { ", retry" } else { "" }
);
p
}
@@ -1086,6 +1053,7 @@ impl StellarOverlay {
}
}
};
+ drop(streams);
// Keep selection and reservation in the dispatcher, before spawning
// the write, so repeated FetchTxSet commands cannot race past dedup.
@@ -1123,6 +1091,20 @@ impl StellarOverlay {
});
}
+ /// Forget requests for slots below `min_slot` that were never answered.
+ async fn prune_txset_requests(&mut self, min_slot: u32) {
+ let mut pending = self.state.pending_txset_requests.write().await;
+ let before = pending.len();
+ pending.retain(|_, (_, _, slot)| *slot >= min_slot);
+ if pending.len() < before {
+ debug!(
+ "Pruned {} unanswered TX set requests below slot {}",
+ before - pending.len(),
+ min_slot
+ );
+ }
+ }
+
/// Request SCP state from all connected peers
pub async fn request_scp_state_from_all_peers(&mut self, ledger_seq: u32) {
let streams = self.state.peer_streams.read().await;
@@ -1161,7 +1143,7 @@ async fn send_txset_response(
state: Arc,
peer: PeerId,
hash: [u8; 32],
- data: Arc>,
+ data: Arc,
) {
info!(
"TXSET_SEND: Sending TX set {:02x?}... ({} bytes) to {}",
@@ -1170,16 +1152,9 @@ async fn send_txset_response(
peer
);
- // `data` is a tx set we already validated on entry (from a peer) or
- // built locally (trusted core). Keep the payload shared through admission
- // and transmission, writing the small discriminant separately under the
- // same stream lock as the payload.
- let discriminant = (stellar_xdr::MessageType::GeneralizedTxSet as i32).to_be_bytes();
- let bytes = discriminant.len() + data.len();
-
- match send_to_peer_stream_parts(&state, peer, StreamType::TxSet, &[&discriminant, &data]).await
- {
- Ok(_) => {
+ let bytes = data.encoded().len();
+ match send_to_peer_stream(&state, peer, StreamType::TxSet, data.encoded()).await {
+ Ok(()) => {
state.metrics.send_txset.fetch_add(1, Ordering::Relaxed);
state.metrics.message_write.fetch_add(1, Ordering::Relaxed);
state
@@ -1187,7 +1162,7 @@ async fn send_txset_response(
.byte_write
.fetch_add(bytes as u64, Ordering::Relaxed);
info!(
- "TXSET_SEND_OK: Successfully sent TX set {:02x?}... ({} bytes on wire) to {}",
+ "TXSET_SEND_OK: Queued TX set {:02x?}... ({} bytes on wire) to {}",
&hash[..4],
bytes,
peer
@@ -1264,7 +1239,6 @@ enum StreamType {
Tx,
TxSet,
TxSetRequest,
- LegacyTxSetRequest,
}
impl StreamType {
@@ -1272,41 +1246,37 @@ impl StreamType {
match self {
StreamType::Control | StreamType::TxSetRequest => CONTROL_PROTOCOL,
StreamType::Tx => TX_PROTOCOL,
- StreamType::TxSet | StreamType::LegacyTxSetRequest => TXSET_PROTOCOL,
+ StreamType::TxSet => TXSET_PROTOCOL,
}
}
fn priority(self) -> i32 {
match self {
- Self::Control | Self::TxSetRequest | Self::LegacyTxSetRequest => 2,
+ Self::Control | Self::TxSetRequest => 2,
Self::TxSet => 1,
Self::Tx => 0,
}
}
}
-/// Negotiate the route, then set actual QUIC priority before application bytes
-/// are written. Only an explicit unsupported-protocol response permits fallback.
+/// Open the required protocol and set QUIC priority before application bytes.
async fn open_peer_stream(
mut control: Control,
peer: PeerId,
kind: StreamType,
) -> io::Result {
- let mut protocol = kind.protocol();
- let result = control.open_stream(peer, protocol.clone()).await;
- let stream = match result {
- Err(libp2p_stream::OpenStreamError::UnsupportedProtocol(_))
- if protocol == CONTROL_PROTOCOL =>
- {
- protocol = SCP_PROTOCOL;
- control.open_stream(peer, protocol.clone()).await
- }
- result => result,
- }
- .map_err(|e| io::Error::new(io::ErrorKind::NotConnected, e))?;
+ let protocol = kind.protocol();
+ let stream = control
+ .open_stream(peer, protocol.clone())
+ .await
+ .map_err(|e| io::Error::new(io::ErrorKind::NotConnected, e))?;
stream.set_priority(kind.priority())?;
debug!(%peer, %protocol, priority = kind.priority(), "Opened outbound stream");
- Ok(OutboundStream { stream, protocol })
+ Ok(OutboundStream {
+ stream,
+ #[cfg(test)]
+ protocol,
+ })
}
/// Send message to a specific peer's stream only if already open (for flooding)
@@ -1329,7 +1299,6 @@ async fn try_send_to_existing_stream(
StreamType::Control | StreamType::TxSetRequest => &peer_streams.scp,
StreamType::Tx => &peer_streams.tx,
StreamType::TxSet => &peer_streams.txset,
- StreamType::LegacyTxSetRequest => &peer_streams.legacy_txset_request,
};
let mut stream_guard = stream_mutex.lock().await;
@@ -1367,13 +1336,30 @@ async fn send_to_peer_stream(
send_to_peer_stream_parts(state, peer_id, stream_type, &[data]).await
}
-/// All parts form one frame, including across partial writes and retries.
-/// The per-route lock stays held until that entire frame has been written.
+/// Send one frame made of `parts`, without assembling them into one buffer.
async fn send_to_peer_stream_parts(
state: &SharedState,
peer_id: PeerId,
stream_type: StreamType,
parts: &[&[u8]],
+) -> io::Result<()> {
+ send_peer_frames(state, peer_id, stream_type, framed_io::Frames::One(parts)).await
+}
+
+/// Write `frames` to the peer's stream for `stream_type`, opening it if
+/// needed. The per-route lock stays held until every frame has been written,
+/// including across partial writes, so frames never interleave mid-frame.
+///
+/// A failed write drops the stream and retries the whole of `frames` on a
+/// reopened one (3 attempts). For a `Frames::Batch` this can deliver complete
+/// frames that reached the peer before the failure a second time; TX
+/// receivers drop such duplicates via `tx_seen`. The peer's reader on the old
+/// stream sees it end, possibly mid-frame, and exits.
+async fn send_peer_frames(
+ state: &SharedState,
+ peer_id: PeerId,
+ stream_type: StreamType,
+ frames: framed_io::Frames<'_>,
) -> io::Result<()> {
// Retry up to 2 times (3 attempts total) for reliability
const MAX_RETRIES: usize = 2;
@@ -1396,7 +1382,6 @@ async fn send_to_peer_stream_parts(
StreamType::Control | StreamType::TxSetRequest => &peer_streams.scp,
StreamType::Tx => &peer_streams.tx,
StreamType::TxSet => &peer_streams.txset,
- StreamType::LegacyTxSetRequest => &peer_streams.legacy_txset_request,
};
let mut stream_guard = stream_mutex.lock().await;
@@ -1449,19 +1434,7 @@ async fn send_to_peer_stream_parts(
}
let stream = stream_guard.as_mut().unwrap();
- if stream_type == StreamType::TxSetRequest && stream.protocol != CONTROL_PROTOCOL {
- // Never send a request to an old SCP reader: it would silently
- // discard it. Keep requests isolated from legacy response writes.
- drop(stream_guard);
- return Box::pin(send_to_peer_stream_parts(
- state,
- peer_id,
- StreamType::LegacyTxSetRequest,
- parts,
- ))
- .await;
- }
- match framed_io::write_frame_parts(&mut stream.stream, parts).await {
+ match frames.write(&mut stream.stream).await {
Ok(()) => return Ok(()),
Err(e) => {
// Clear the broken stream
@@ -1506,6 +1479,26 @@ async fn flush_inv_batch_to_peer(state: &Arc, peer: PeerId) {
}
}
+/// Queue `entry` for every peer in `peers` under one INV batcher lock, rather
+/// than one lock round trip per peer, then send the batches it completed.
+/// `send_inv_batch` only spawns the write, so they still go out at once.
+async fn queue_inv(state: &Arc, peers: &[PeerId], entry: InvEntry) {
+ let full_batches: Vec<(PeerId, InvBatch)> = {
+ let mut batcher = state.inv_batcher.write().await;
+ peers
+ .iter()
+ .filter_map(|peer| {
+ batcher
+ .add(*peer, entry.clone())
+ .map(|batch| (*peer, batch))
+ })
+ .collect()
+ };
+ for (peer, batch) in full_batches {
+ send_inv_batch(state, peer, batch).await;
+ }
+}
+
/// Send an INV batch to a peer
async fn send_inv_batch(state: &Arc, peer: PeerId, batch: InvBatch) {
let batch_size = batch.entries.len() as u64;
@@ -1548,15 +1541,22 @@ async fn send_inv_batch(state: &Arc, peer: PeerId, batch: InvBatch)
}
/// Read length-prefixed frame from stream
-async fn read_framed(stream: &mut Stream) -> io::Result> {
+async fn read_framed(stream: &mut R) -> io::Result> {
+ read_framed_with_limit(stream, MAX_MESSAGE_SIZE).await
+}
+
+async fn read_framed_with_limit(
+ stream: &mut R,
+ limit: usize,
+) -> io::Result> {
let mut len_buf = [0u8; 4];
stream.read_exact(&mut len_buf).await?;
let len = u32::from_be_bytes(len_buf) as usize;
- if len > MAX_MESSAGE_SIZE {
+ if len > limit {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
- format!("message too large: {} > {}", len, MAX_MESSAGE_SIZE),
+ format!("message too large: {} > {}", len, limit),
));
}
@@ -1566,11 +1566,7 @@ async fn read_framed(stream: &mut Stream) -> io::Result> {
}
/// Handle inbound SCP streams from peers
-async fn handle_inbound_scp_streams(
- mut incoming: IncomingStreams,
- state: Arc,
- accepts_txset_requests: bool,
-) {
+async fn handle_inbound_scp_streams(mut incoming: IncomingStreams, state: Arc) {
while let Some((peer_id, mut stream)) = incoming.next().await {
info!("SCP_STREAM: Accepted inbound SCP stream from {}", peer_id);
state
@@ -1599,9 +1595,7 @@ async fn handle_inbound_scp_streams(
};
let scp_envelope = match message {
- stellar_xdr::StellarMessage::GetTxSet(hash)
- if accepts_txset_requests =>
- {
+ stellar_xdr::StellarMessage::GetTxSet(hash) => {
handle_txset_request(&state, peer_id, hash.0);
continue;
}
@@ -1713,14 +1707,24 @@ async fn handle_inbound_scp_streams(
}
}
+/// Inbound TX streams carry many small frames, and GETDATA responses arrive
+/// as batches of them. Reading through a buffer copies whatever QUIC has
+/// received in one call, instead of two reads (each taking the connection
+/// lock) per frame. It never waits for more than is available, and larger
+/// frames bypass it once it is empty.
+fn buffered_tx_stream(stream: R) -> futures::io::BufReader {
+ futures::io::BufReader::with_capacity(TX_RESPONSE_BATCH_BYTES, stream)
+}
+
/// Handle inbound TX streams from peers
async fn handle_inbound_tx_streams(mut incoming: IncomingStreams, state: Arc) {
- while let Some((peer_id, mut stream)) = incoming.next().await {
+ while let Some((peer_id, stream)) = incoming.next().await {
info!("TX_STREAM: Accepted inbound TX stream from {}", peer_id);
state.metrics.inbound_live.fetch_add(1, Ordering::Relaxed);
let state = state.clone();
tokio::spawn(async move {
+ let mut stream = buffered_tx_stream(stream);
loop {
match read_framed(&mut stream).await {
Ok(data) => {
@@ -1730,7 +1734,7 @@ async fn handle_inbound_tx_streams(mut incoming: IncomingStreams, state: Arc {
state.metrics.error_read.fetch_add(1, Ordering::Relaxed);
@@ -1745,18 +1749,13 @@ async fn handle_inbound_tx_streams(mut incoming: IncomingStreams, state: Arc,
- peer_id: &PeerId,
- data: &[u8],
- stream: &mut Stream,
-) {
+async fn handle_tx_stream_message(state: &Arc, peer_id: &PeerId, data: &[u8]) {
match TxStreamMessage::decode(data) {
Ok(TxStreamMessage::InvBatch(batch)) => {
handle_inv_batch(state, peer_id, batch).await;
}
Ok(TxStreamMessage::GetData(getdata)) => {
- handle_getdata(state, peer_id, getdata, stream).await;
+ handle_getdata(state, peer_id, getdata);
}
Ok(TxStreamMessage::Tx(tx)) => {
handle_tx_response(state, peer_id, tx).await;
@@ -1778,29 +1777,29 @@ async fn handle_inv_batch(state: &Arc, peer_id: &PeerId, batch: Inv
peer_id
);
- let mut to_request: Vec<[u8; 32]> = Vec::new();
-
- for entry in batch.entries {
- // Check if we already have this TX
- {
- let seen = state.tx_seen.read().await;
- if seen.contains(&entry.hash) {
- // Already have it, just record this peer as a source (for relay tracking)
- continue;
- }
- }
-
- // Record this peer as a source for round-robin GETDATA
- let is_first = {
- let mut tracker = state.inv_tracker.write().await;
- tracker.record_source(entry.hash, *peer_id)
- };
-
- // If this is the first INV for this TX, we should request it
- if is_first {
- to_request.push(entry.hash);
- }
- }
+ // 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.
+ let unseen: Vec<[u8; 32]> = {
+ let seen = state.tx_seen.read().await;
+ batch
+ .entries
+ .iter()
+ .map(|entry| entry.hash)
+ .filter(|hash| !seen.contains(hash))
+ .collect()
+ };
+ // Record this peer as a source for round-robin GETDATA retries, and
+ // request only TXs it is the first to advertise.
+ let to_request: Vec<[u8; 32]> = if unseen.is_empty() {
+ Vec::new()
+ } else {
+ let mut tracker = state.inv_tracker.write().await;
+ unseen
+ .into_iter()
+ .filter(|hash| tracker.record_source(*hash, *peer_id))
+ .collect()
+ };
// Send GETDATA for TXs we don't have
if !to_request.is_empty() {
@@ -1855,67 +1854,156 @@ async fn handle_inv_batch(state: &Arc, peer_id: &PeerId, batch: Inv
}
/// Handle GETDATA message - respond with requested TXs
-async fn handle_getdata(
- state: &Arc,
- peer_id: &PeerId,
- getdata: GetData,
- _stream: &mut Stream,
-) {
+fn handle_getdata(state: &Arc, peer_id: &PeerId, getdata: GetData) {
debug!(
"TX_GETDATA_RECV: Peer {} requesting {} TXs",
peer_id,
getdata.hashes.len()
);
- for hash in getdata.hashes {
- // Look up TX in our buffer
- let tx = {
- let mut buffer = state.tx_buffer.write().await;
- buffer.get(&hash)
- };
+ let state = Arc::clone(state);
+ let peer = *peer_id;
+ tokio::spawn(async move { serve_getdata(&state, peer, getdata.hashes).await });
+}
- if let Some(tx) = tx {
- state
- .metrics
- .flood_fulfilled
- .fetch_add(1, Ordering::Relaxed);
- // Send TX response. Buffered bytes were validated on entry, so
- // framing them (concat) yields valid wire XDR by construction.
- let encoded = tx.to_flood_frame();
+/// GETDATA responses are written as batches of complete TX frames of at most
+/// this many bytes. Inbound TX streams are read through a buffer of the same
+/// size, so one read can take in a whole batch.
+const TX_RESPONSE_BATCH_BYTES: usize = 64 * 1024;
+
+/// A transaction's length-prefixed `StellarMessage::Transaction` frame size.
+fn tx_frame_len(tx: &ValidatedTx) -> usize {
+ 4 + tx
+ .flood_frame_parts()
+ .iter()
+ .map(|part| part.len())
+ .sum::()
+}
- let state_clone = Arc::clone(state);
- let peer_clone = *peer_id;
- tokio::spawn(async move {
- if let Err(e) =
- send_to_peer_stream(&state_clone, peer_clone, StreamType::Tx, &encoded).await
- {
- state_clone
- .metrics
- .error_write
- .fetch_add(1, Ordering::Relaxed);
- warn!("Failed to send TX to {}: {}", peer_clone, e);
- } else {
- state_clone
+/// Serve one demand in order, as one job that retains only one bounded batch
+/// while a peer is backpressured. Every TX still has its own wire frame.
+/// Batching avoids one task, stream lock and short QUIC write per transaction.
+///
+/// Each batch is looked up under one TX buffer lock, which is released before
+/// its frames are copied (straight from the shared TX bytes) and written.
+/// Lookups are lazy: batch k+1 is looked up after batch k is written, so a
+/// peer backpressured for longer than the buffer's retention gets misses for
+/// the rest rather than late responses; it re-demands after its timeout.
+///
+/// The job stops at the first write that fails after `send_peer_frames`'
+/// retries, usually because the peer disconnected. If the connection is in
+/// fact still up (a reset stream that also failed to reopen), the requester
+/// re-demands the rest after its response timeout.
+async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) {
+ let mut hashes = hashes.into_iter();
+ // A hit that did not fit the previous batch; it starts the next one.
+ let mut carried: Option> = None;
+ let mut txs: Vec> = Vec::new();
+ let mut batch = Vec::new();
+ loop {
+ txs.clear();
+ let mut bytes = 0;
+ if let Some(tx) = carried.take() {
+ bytes += tx_frame_len(&tx);
+ txs.push(tx);
+ }
+ 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 {
+ state
.metrics
- .message_write
+ .flood_unfulfilled_unknown
.fetch_add(1, Ordering::Relaxed);
- state_clone
- .metrics
- .byte_write
- .fetch_add(encoded.len() as u64, Ordering::Relaxed);
- debug!("TX_SEND: Sent TX {:02x?}... to {}", &hash[..4], peer_clone);
+ trace!(
+ "TX_GETDATA_MISS: Don't have TX {:02x?}... for {}",
+ &hash[..4],
+ peer
+ );
+ continue;
+ };
+ let len = tx_frame_len(&tx);
+ if !txs.is_empty() && bytes + len > TX_RESPONSE_BATCH_BYTES {
+ carried = Some(tx);
+ break;
}
- });
+ bytes += len;
+ txs.push(tx);
+ if bytes >= TX_RESPONSE_BATCH_BYTES {
+ break;
+ }
+ }
+ }
+ let Some(first) = txs.first() else {
+ return;
+ };
+ state
+ .metrics
+ .flood_fulfilled
+ .fetch_add(txs.len() as u64, Ordering::Relaxed);
+ let written = if bytes > TX_RESPONSE_BATCH_BYTES {
+ // Only a lone transaction can exceed the budget. Write it as the
+ // existing segmented frame, straight from the shared TX bytes.
+ let parts = first.flood_frame_parts();
+ let result = send_to_peer_stream_parts(state, peer, StreamType::Tx, &parts).await;
+ record_tx_response_write(state, peer, result, 1, bytes - 4)
} else {
+ batch.clear();
+ batch.reserve_exact(bytes);
+ for tx in &txs {
+ let parts = tx.flood_frame_parts();
+ // Within the batch budget, so the length fits the u32 prefix.
+ let payload = parts.iter().map(|part| part.len()).sum::();
+ batch.extend_from_slice(&(payload as u32).to_be_bytes());
+ for part in parts {
+ batch.extend_from_slice(part);
+ }
+ }
+ let frames = framed_io::Frames::Batch(&batch);
+ let result = send_peer_frames(state, peer, StreamType::Tx, frames).await;
+ record_tx_response_write(state, peer, result, txs.len(), bytes - 4 * txs.len())
+ };
+ if !written {
+ let remaining = hashes.len() + usize::from(carried.is_some());
+ if remaining > 0 {
+ debug!(
+ "TX_GETDATA_ABANDON: Not serving {} more requested hashes to {} after a failed write",
+ remaining, peer
+ );
+ }
+ return;
+ }
+ }
+}
+
+/// Returns whether the write succeeded.
+fn record_tx_response_write(
+ state: &SharedState,
+ peer: PeerId,
+ result: io::Result<()>,
+ messages: usize,
+ bytes: usize,
+) -> bool {
+ match result {
+ Ok(()) => {
state
.metrics
- .flood_unfulfilled_unknown
- .fetch_add(1, Ordering::Relaxed);
- trace!(
- "TX_GETDATA_MISS: Don't have TX {:02x?}... for {}",
- &hash[..4],
- peer_id
- );
+ .message_write
+ .fetch_add(messages as u64, Ordering::Relaxed);
+ state
+ .metrics
+ .byte_write
+ .fetch_add(bytes as u64, Ordering::Relaxed);
+ debug!("TX_SEND: Sent {} TXs to {}", messages, peer);
+ true
+ }
+ Err(e) => {
+ state
+ .metrics
+ .error_write
+ .fetch_add(messages as u64, Ordering::Relaxed);
+ warn!("Failed to send {} TXs to {}: {}", messages, peer, e);
+ false
}
}
}
@@ -2018,18 +2106,7 @@ async fn handle_tx_response(state: &Arc, peer_id: &PeerId, tx: Arc<
peers_to_announce.len()
);
- let inv_entry = InvEntry { hash, fee_per_op };
-
- // Add to batcher for each peer, send batch immediately when full
- for peer in &peers_to_announce {
- let batch_to_send = {
- let mut batcher = state.inv_batcher.write().await;
- batcher.add(*peer, inv_entry.clone())
- };
- if let Some(batch) = batch_to_send {
- send_inv_batch(state, *peer, batch).await;
- }
- }
+ queue_inv(state, &peers_to_announce, InvEntry { hash, fee_per_op }).await;
}
// Record recv-transaction timing
@@ -2045,7 +2122,7 @@ async fn handle_tx_response(state: &Arc, peer_id: &PeerId, tx: Arc<
state.metrics.update_recv_transaction_max(elapsed_us);
}
-/// Both the control protocol and the legacy TxSet protocol accept requests.
+/// Requests arrive on the control stream, separately from bulk responses.
fn handle_txset_request(state: &SharedState, peer_id: PeerId, hash: [u8; 32]) {
info!(
"TXSET_REQ_IN: Received TxSet request for {:02x?}... from {}",
@@ -2063,7 +2140,49 @@ fn handle_txset_request(state: &SharedState, peer_id: PeerId, hash: [u8; 32]) {
}
}
-/// Handle inbound TxSet streams from peers
+/// Deliver verified XDR together with its original compressed encoding.
+async fn deliver_txset(state: &SharedState, peer_id: PeerId, txset_data: Arc) {
+ let hash = crate::xdr::sha256_hash(txset_data.as_slice());
+
+ // Clear pending request flag and measure fetch latency
+ let slot = {
+ let mut pending = state.pending_txset_requests.write().await;
+ if let Some((_, request_time, slot)) = pending.remove(&hash) {
+ let fetch_us = request_time.elapsed().as_micros() as u64;
+ state
+ .metrics
+ .fetch_txset_sum_us
+ .fetch_add(fetch_us, Ordering::Relaxed);
+ state
+ .metrics
+ .fetch_txset_count
+ .fetch_add(1, Ordering::Relaxed);
+ Some(slot)
+ } else {
+ None
+ }
+ };
+
+ info!(
+ "TXSET_RECV: Received TxSet {:02x?}... ({} bytes) from {} (was_pending={})",
+ &hash[..4],
+ txset_data.len(),
+ peer_id,
+ slot.is_some()
+ );
+ if let Err(e) = state.event_tx.send(OverlayEvent::TxSetReceived {
+ hash,
+ data: txset_data,
+ from: peer_id,
+ slot,
+ }) {
+ warn!(
+ "Failed to forward TxSetReceived event from {}: {}",
+ peer_id, e
+ );
+ }
+}
+
async fn handle_inbound_txset_streams(mut incoming: IncomingStreams, state: Arc) {
while let Some((peer_id, mut stream)) = incoming.next().await {
debug!("Accepted inbound TxSet stream from {}", peer_id);
@@ -2072,82 +2191,31 @@ async fn handle_inbound_txset_streams(mut incoming: IncomingStreams, state: Arc<
tokio::spawn(async move {
loop {
- match read_framed(&mut stream).await {
+ match read_framed_with_limit(
+ &mut stream,
+ crate::flood::txset_encoding::max_encoded_size(),
+ )
+ .await
+ {
Ok(data) => {
state.metrics.message_read.fetch_add(1, Ordering::Relaxed);
state
.metrics
.byte_read
.fetch_add(data.len() as u64, Ordering::Relaxed);
- let message = match crate::xdr::parse_stellar_message(&data) {
- Ok(message) => message,
- Err(e) => {
- warn!(
- "TXSET_PARSE_ERR: Dropping malformed TxSet stream message from {}: {}",
- peer_id, e
- );
- continue;
- }
- };
-
- match message {
- stellar_xdr::StellarMessage::GetTxSet(hash) => {
- handle_txset_request(&state, peer_id, hash.0);
- }
- stellar_xdr::StellarMessage::GeneralizedTxSet(_tx_set) => {
- // The message was strict-decoded above, so the
- // bytes after the 4-byte discriminant are the
- // canonical tx set; hash the original bytes
- // rather than re-encoding.
- let txset_data = data[4..].to_vec();
- let hash = crate::xdr::sha256_hash(&txset_data);
-
- // Clear pending request flag and measure fetch latency
- let slot = {
- let mut pending = state.pending_txset_requests.write().await;
- if let Some((_, request_time, slot)) = pending.remove(&hash) {
- let fetch_us = request_time.elapsed().as_micros() as u64;
- state
- .metrics
- .fetch_txset_sum_us
- .fetch_add(fetch_us, Ordering::Relaxed);
- state
- .metrics
- .fetch_txset_count
- .fetch_add(1, Ordering::Relaxed);
- Some(slot)
- } else {
- None
- }
- };
-
- info!(
- "TXSET_RECV: Received TxSet {:02x?}... ({} bytes) from {} (was_pending={})",
- &hash[..4],
- txset_data.len(),
- peer_id,
- slot.is_some()
- );
- if let Err(e) = state.event_tx.send(OverlayEvent::TxSetReceived {
- hash,
- data: txset_data,
- from: peer_id,
- slot,
- }) {
- warn!(
- "Failed to forward TxSetReceived event from {}: {}",
- peer_id, e
- );
- }
- }
- other => {
- warn!(
- "TXSET_PARSE_ERR: Dropping unexpected {} on TxSet stream from {}",
- other.name(),
- peer_id
- );
- }
- }
+ // Preserve the received encoding through verification and
+ // cache insertion. Forwarding never compresses it again.
+ let decoded = tokio::task::spawn_blocking(move || {
+ use stellar_xdr::{GeneralizedTransactionSet, Limits, ReadXdr};
+ let data = crate::flood::txset_encoding::decode(data)?;
+ GeneralizedTransactionSet::from_xdr(data.as_slice(), Limits::none())
+ .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
+ Ok::<_, io::Error>(Arc::new(data))
+ })
+ .await;
+ match decoded {
+ Ok(Ok(data)) => deliver_txset(&state, peer_id, data).await,
+ result => warn!("TXSET_PARSE_ERR: Dropping malformed compressed TxSet from {}: {:?}", peer_id, result), }
}
Err(e) => {
state.metrics.error_read.fetch_add(1, Ordering::Relaxed);
@@ -2903,7 +2971,7 @@ mod tests {
request_received = true;
// Node1 responds with TxSet data
- handle1.send_txset(hash, txset_data.clone(), from).await;
+ handle1.send_txset(hash, Arc::new(TxSetData::from_local(txset_data.clone()).unwrap()), from).await;
}
}
_ = tokio::time::sleep(Duration::from_millis(10)) => {}
@@ -2923,7 +2991,7 @@ mod tests {
Some(event) = events2.recv() => {
if let OverlayEvent::TxSetReceived { hash, data, .. } = event {
assert_eq!(hash, requested_hash);
- assert_eq!(data, txset_data);
+ assert_eq!(data.as_slice(), txset_data);
response_received = true;
}
}
@@ -3687,7 +3755,7 @@ async fn test_txset_request_and_response() {
Some(event) = events1.recv() => {
if let OverlayEvent::TxSetRequested { hash, from } = event {
assert_eq!(hash, requested_hash, "Request should have correct hash");
- handle1.send_txset(hash, txset_data.clone(), from).await;
+ handle1.send_txset(hash, Arc::new(TxSetData::from_local(txset_data.clone()).unwrap()), from).await;
responded = true;
}
}
@@ -3708,7 +3776,7 @@ async fn test_txset_request_and_response() {
Some(event) = events2.recv() => {
if let OverlayEvent::TxSetReceived { hash, data, .. } = event {
assert_eq!(hash, requested_hash, "Received hash should match");
- assert_eq!(data, txset_data, "Received data should match");
+ assert_eq!(data.as_slice(), txset_data, "Received data should match");
received = true;
}
}
@@ -3804,7 +3872,7 @@ async fn test_txset_multiple_concurrent_requests() {
received_hashes.insert(hash);
// Respond to each request
let data = format!("txset for {:?}", &hash[..4]).into_bytes();
- handle1.send_txset(hash, data, from).await;
+ handle1.send_txset(hash, Arc::new(TxSetData::from_local(data).unwrap()), from).await;
}
}
_ = tokio::time::sleep(Duration::from_millis(10)) => {}
@@ -4139,7 +4207,11 @@ async fn test_concurrent_scp_and_txset_writes_to_same_peer() {
let txset_task = tokio::spawn(async move {
txset_started_clone.store(true, Ordering::SeqCst);
handle1_txset
- .send_txset(txset_hash, txset_data, peer2_id)
+ .send_txset(
+ txset_hash,
+ Arc::new(TxSetData::from_local(txset_data).unwrap()),
+ peer2_id,
+ )
.await;
});
@@ -4267,7 +4339,11 @@ async fn test_pending_txset_cleanup_on_disconnect() {
// Now have node2 respond with the TxSet
// This verifies the pending cleanup works when response is received
handle2
- .send_txset(txset_hash, txset_data.clone(), peer1_id)
+ .send_txset(
+ txset_hash,
+ Arc::new(TxSetData::from_local(txset_data.clone()).unwrap()),
+ peer1_id,
+ )
.await;
// Verify node1 receives the TxSet response
@@ -4277,7 +4353,7 @@ async fn test_pending_txset_cleanup_on_disconnect() {
tokio::select! {
Some(event) = events1.recv() => {
if let OverlayEvent::TxSetReceived { hash, data, .. } = event {
- if hash == txset_hash && data == txset_data {
+ if hash == txset_hash && data.as_slice() == txset_data {
got_response = true;
}
}
diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs
index fde3941fc2..361001c855 100644
--- a/overlay/src/libp2p_overlay/dispatch_tests.rs
+++ b/overlay/src/libp2p_overlay/dispatch_tests.rs
@@ -26,7 +26,9 @@ async fn dispatcher_with_blocked_write(command: &str) {
match command {
"txset" => {
let (hash, data) = test_txset_xdr(1);
- handle.send_txset(hash, data, peer).await;
+ handle
+ .send_txset(hash, Arc::new(TxSetData::from_local(data).unwrap()), peer)
+ .await;
}
"fetch" => handle.fetch_txset([1; 32], 1).await,
// Awaiting a direct send must preserve ordering within an SCP-state
@@ -90,10 +92,6 @@ struct TestNode {
impl TestNode {
async fn start() -> Self {
- Self::start_with_control(true).await
- }
-
- async fn start_with_control(accept_control: bool) -> Self {
let (handle, events, admissions, mut overlay) =
create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new()))
.unwrap();
@@ -116,7 +114,7 @@ impl TestNode {
})
.await
.expect("listener did not start");
- let task = tokio::spawn(overlay.run_event_loop_with_control(accept_control));
+ let task = tokio::spawn(overlay.run_event_loop());
Self {
handle,
events,
@@ -169,6 +167,669 @@ impl Drop for TestNode {
}
}
+#[tokio::test]
+async fn getdata_batches_preserve_frames_and_leave_control_responsive() {
+ use stellar_xdr::{
+ HostFunction, InvokeHostFunctionOp, Limits, Operation, OperationBody, ReadXdr,
+ TransactionEnvelope, WriteXdr,
+ };
+
+ let mut sender = TestNode::start().await;
+ let mut receiver = TestNode::start().await;
+ sender.connect(&receiver).await;
+
+ let mut transactions = Vec::new();
+ for sequence in 0..160 {
+ let bytes = crate::xdr::tests::valid_transaction_xdr(1000, sequence, 16);
+ transactions.push(ValidatedTx::from_core_trusted(bytes, 1000, 16).unwrap());
+ }
+ // A wire-valid upload larger than the batching budget exercises the
+ // standalone write between two batches. Ledger validation is unrelated
+ // to this transport test.
+ let mut envelope = TransactionEnvelope::from_xdr(
+ crate::xdr::tests::valid_transaction_xdr(1000, 200, 1),
+ Limits::none(),
+ )
+ .unwrap();
+ let TransactionEnvelope::Tx(v1) = &mut envelope else {
+ unreachable!()
+ };
+ v1.tx.operations = vec![Operation {
+ source_account: None,
+ body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
+ host_function: HostFunction::UploadContractWasm(vec![0; 70_000].try_into().unwrap()),
+ auth: Default::default(),
+ }),
+ }]
+ .try_into()
+ .unwrap();
+ let large =
+ ValidatedTx::from_core_trusted(envelope.to_xdr(Limits::none()).unwrap(), 1000, 1).unwrap();
+ transactions.insert(80, large);
+ let mut demand = GetData::new();
+ demand.push([0xaa; 32]);
+ for tx in &transactions {
+ sender.state.tx_buffer.write().await.insert(Arc::clone(tx));
+ demand.push(*tx.hash());
+ }
+ demand.push([0xbb; 32]);
+
+ let streams = sender.streams_to(receiver.peer).await;
+ let blocked = streams.tx.lock().await;
+ for (encoded, _) in demand.encode_chunked().unwrap() {
+ send_to_peer_stream(&receiver.state, sender.peer, StreamType::Tx, &encoded)
+ .await
+ .unwrap();
+ }
+ // A second demand on the same incoming stream is a barrier: its misses
+ // prove the reader dispatched both requests while the first TX writer is
+ // still blocked. The original per-TX task path has already looked up all
+ // transactions by this point; a bounded response job retains only its next
+ // batch. Use unknown hashes so the barrier needs no response stream.
+ let mut barrier = GetData::new();
+ for byte in 0xc0..0xe0 {
+ barrier.push([byte; 32]);
+ }
+ for (encoded, _) in barrier.encode_chunked().unwrap() {
+ send_to_peer_stream(&receiver.state, sender.peer, StreamType::Tx, &encoded)
+ .await
+ .unwrap();
+ }
+ tokio::time::timeout(Duration::from_secs(5), async {
+ while sender
+ .state
+ .metrics
+ .flood_unfulfilled_unknown
+ .load(Ordering::Relaxed)
+ < 32
+ || sender.state.metrics.flood_fulfilled.load(Ordering::Relaxed) == 0
+ {
+ tokio::task::yield_now().await;
+ }
+ })
+ .await
+ .expect("GETDATA reader stopped progressing behind a blocked TX writer");
+ assert!(
+ sender.state.metrics.flood_fulfilled.load(Ordering::Relaxed) < transactions.len() as u64,
+ "blocked response retained every requested transaction"
+ );
+ // Lookups hold the TX buffer lock per batch, never across a write.
+ assert!(
+ sender.state.tx_buffer.try_write().is_ok(),
+ "a blocked GETDATA write holds the TX buffer lock"
+ );
+ let scp = test_scp_envelope_xdr(987);
+ sender.handle.broadcast_scp(scp.clone()).await;
+ tokio::time::timeout(Duration::from_secs(5), async {
+ loop {
+ if let Some(OverlayEvent::ScpReceived { envelope, .. }) = receiver.events.recv().await {
+ if envelope == scp {
+ break;
+ }
+ }
+ }
+ })
+ .await
+ .expect("a blocked GETDATA response held up the control stream");
+ assert!(receiver._admissions.try_recv().is_err());
+ drop(blocked);
+
+ tokio::time::timeout(Duration::from_secs(5), async {
+ for expected in &transactions {
+ let Some(CoreCommand::SubmitTx { tx, .. }) = receiver._admissions.recv().await else {
+ panic!("expected transaction admission");
+ };
+ assert_eq!(tx.bytes(), expected.bytes());
+ assert_eq!(tx.hash(), expected.hash());
+ }
+ })
+ .await
+ .expect("batched, oversized, and trailing transactions must all arrive");
+ assert_eq!(
+ sender.state.metrics.flood_fulfilled.load(Ordering::Relaxed),
+ transactions.len() as u64
+ );
+ assert_eq!(
+ sender
+ .state
+ .metrics
+ .flood_unfulfilled_unknown
+ .load(Ordering::Relaxed),
+ 34
+ );
+ sender.stop().await;
+ receiver.stop().await;
+}
+
+/// A wire-valid transaction carrying a `wasm_len`-byte contract upload, for
+/// controlling frame sizes. Ledger validity is irrelevant to the transport.
+fn upload_tx(sequence: i64, wasm_len: usize) -> Arc {
+ use stellar_xdr::{
+ HostFunction, InvokeHostFunctionOp, Limits, Operation, OperationBody, ReadXdr,
+ TransactionEnvelope, WriteXdr,
+ };
+ let mut envelope = TransactionEnvelope::from_xdr(
+ crate::xdr::tests::valid_transaction_xdr(1000, sequence, 1),
+ Limits::none(),
+ )
+ .unwrap();
+ let TransactionEnvelope::Tx(v1) = &mut envelope else {
+ unreachable!()
+ };
+ v1.tx.operations = vec![Operation {
+ source_account: None,
+ body: OperationBody::InvokeHostFunction(InvokeHostFunctionOp {
+ host_function: HostFunction::UploadContractWasm(vec![0; wasm_len].try_into().unwrap()),
+ auth: Default::default(),
+ }),
+ }]
+ .try_into()
+ .unwrap();
+ ValidatedTx::from_core_trusted(envelope.to_xdr(Limits::none()).unwrap(), 1000, 1).unwrap()
+}
+
+#[tokio::test]
+async fn getdata_batch_edges_preserve_order_and_bytes() {
+ let mut sender = TestNode::start().await;
+ let mut receiver = TestNode::start().await;
+ sender.connect(&receiver).await;
+
+ let small = |sequence| {
+ let bytes = crate::xdr::tests::valid_transaction_xdr(1000, sequence, 16);
+ ValidatedTx::from_core_trusted(bytes, 1000, 16).unwrap()
+ };
+ // XDR sizes are multiples of 4, so this lands exactly on the budget.
+ let exact = |sequence| {
+ let probe = tx_frame_len(&upload_tx(sequence, 60_000));
+ let tx = upload_tx(sequence, 60_000 + TX_RESPONSE_BATCH_BYTES - probe);
+ assert_eq!(tx_frame_len(&tx), TX_RESPONSE_BATCH_BYTES);
+ tx
+ };
+ // An oversized TX as the first hit, then another straight after it; a
+ // batch closed by an exactly budget-sized frame, which then goes out as a
+ // batch of its own; batches spanning several writes; an oversized TX
+ // carried over from a full batch at the end. Misses are interleaved.
+ let mut transactions = vec![upload_tx(1, 70_000), upload_tx(2, 70_000)];
+ transactions.extend((10..13).map(small));
+ transactions.push(exact(20));
+ transactions.push(exact(21));
+ transactions.extend((30..130).map(small));
+ transactions.push(upload_tx(3, 100_000));
+ let mut demand = vec![[0xaa; 32]];
+ for (index, tx) in transactions.iter().enumerate() {
+ sender.state.tx_buffer.write().await.insert(Arc::clone(tx));
+ demand.push(*tx.hash());
+ if index % 40 == 3 {
+ demand.push([index as u8; 32]);
+ }
+ }
+ let misses = demand.len() - transactions.len();
+
+ serve_getdata(&sender.state, receiver.peer, demand).await;
+
+ tokio::time::timeout(Duration::from_secs(5), async {
+ for expected in &transactions {
+ let Some(CoreCommand::SubmitTx { tx, .. }) = receiver._admissions.recv().await else {
+ panic!("expected transaction admission");
+ };
+ assert_eq!(tx.bytes(), expected.bytes());
+ }
+ })
+ .await
+ .expect("every served transaction must arrive, in demand order");
+ let metrics = &sender.state.metrics;
+ let served = transactions.len() as u64;
+ assert_eq!(metrics.flood_fulfilled.load(Ordering::Relaxed), served);
+ assert_eq!(metrics.message_write.load(Ordering::Relaxed), served);
+ assert_eq!(metrics.error_write.load(Ordering::Relaxed), 0);
+ assert_eq!(
+ metrics.flood_unfulfilled_unknown.load(Ordering::Relaxed),
+ misses as u64
+ );
+ sender.stop().await;
+ receiver.stop().await;
+}
+
+#[tokio::test]
+async fn failed_getdata_batch_is_rewritten_whole_on_a_reopened_stream() {
+ use futures::AsyncWriteExt;
+
+ let mut sender = TestNode::start().await;
+ let mut receiver = TestNode::start().await;
+ sender.connect(&receiver).await;
+ let transactions: Vec<_> = (0..200)
+ .map(|sequence| {
+ let bytes = crate::xdr::tests::valid_transaction_xdr(1000, sequence, 16);
+ ValidatedTx::from_core_trusted(bytes, 1000, 16).unwrap()
+ })
+ .collect();
+ for tx in &transactions {
+ sender.state.tx_buffer.write().await.insert(Arc::clone(tx));
+ }
+ // Finish the cached TX stream: the first batch write then fails, and
+ // send_peer_frames must reopen the stream and write that batch again.
+ {
+ let streams = sender.streams_to(receiver.peer).await;
+ let mut cached = streams.tx.lock().await;
+ cached.as_mut().unwrap().stream.close().await.unwrap();
+ }
+
+ let demand = transactions.iter().map(|tx| *tx.hash()).collect();
+ serve_getdata(&sender.state, receiver.peer, demand).await;
+
+ tokio::time::timeout(Duration::from_secs(5), async {
+ for expected in &transactions {
+ let Some(CoreCommand::SubmitTx { tx, .. }) = receiver._admissions.recv().await else {
+ panic!("expected transaction admission");
+ };
+ assert_eq!(tx.bytes(), expected.bytes());
+ }
+ })
+ .await
+ .expect("the batch that hit the closed stream must be delivered on a new one");
+ let metrics = &sender.state.metrics;
+ assert_eq!(metrics.error_write.load(Ordering::Relaxed), 0);
+ assert_eq!(
+ metrics.message_write.load(Ordering::Relaxed),
+ transactions.len() as u64
+ );
+ assert_eq!(
+ receiver
+ .state
+ .metrics
+ .flood_duplicate_recv
+ .load(Ordering::Relaxed),
+ 0,
+ "a frame was delivered twice"
+ );
+ sender.stop().await;
+ receiver.stop().await;
+}
+
+#[tokio::test]
+async fn getdata_job_stops_after_a_failed_write() {
+ let (_handle, _events, _admissions, overlay) =
+ create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())).unwrap();
+ let state = Arc::clone(&overlay.state);
+ let mut hashes = Vec::new();
+ for sequence in 0..200 {
+ let bytes = crate::xdr::tests::valid_transaction_xdr(1000, sequence, 16);
+ let tx = ValidatedTx::from_core_trusted(bytes, 1000, 16).unwrap();
+ hashes.push(*tx.hash());
+ state.tx_buffer.write().await.insert(tx);
+ }
+ // A peer without outbound streams, as after a disconnect: every write
+ // fails at once. The demand spans several batches.
+ serve_getdata(&state, PeerId::random(), hashes.clone()).await;
+
+ let failed = state.metrics.error_write.load(Ordering::Relaxed);
+ let attempted = state.metrics.flood_fulfilled.load(Ordering::Relaxed);
+ assert!(failed > 0, "the first batch must have been attempted");
+ assert_eq!(attempted, failed, "only the first batch was attempted");
+ assert!(
+ attempted < hashes.len() as u64,
+ "the job kept serving a peer it could not write to"
+ );
+ assert_eq!(state.metrics.message_write.load(Ordering::Relaxed), 0);
+}
+
+#[tokio::test]
+async fn received_tx_is_announced_to_every_peer_except_known_sources() {
+ use crate::flood::{InvEntry, INV_BATCH_MAX_SIZE};
+
+ let (_handle, _events, _admissions, overlay) =
+ create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())).unwrap();
+ let state = Arc::clone(&overlay.state);
+ let source = PeerId::random();
+ let advertiser = PeerId::random();
+ let others: Vec = (0..5).map(|_| PeerId::random()).collect();
+ {
+ let mut streams = state.peer_streams.write().await;
+ for peer in others.iter().chain([&source, &advertiser]) {
+ streams.insert(*peer, Arc::new(PeerOutboundStreams::new()));
+ }
+ }
+ // One peer's batch is one entry short of full, so this TX completes it.
+ let full = others[0];
+ {
+ let mut batcher = state.inv_batcher.write().await;
+ for index in 0..INV_BATCH_MAX_SIZE - 1 {
+ let mut hash = [0xa5; 32];
+ hash[..8].copy_from_slice(&(index as u64).to_be_bytes());
+ assert!(batcher
+ .add(
+ full,
+ InvEntry {
+ hash,
+ fee_per_op: 1
+ }
+ )
+ .is_none());
+ }
+ }
+ let tx = test_tx(1);
+ state
+ .inv_tracker
+ .write()
+ .await
+ .record_source(*tx.hash(), advertiser);
+
+ handle_tx_response(&state, &source, Arc::clone(&tx)).await;
+
+ let mut batcher = state.inv_batcher.write().await;
+ // The completed batch was handed to a sender, not left queued.
+ assert!(batcher.flush(&full).is_none());
+ for peer in &others[1..] {
+ let batch = batcher
+ .flush(peer)
+ .expect("INV queued for every other peer");
+ assert_eq!(batch.entries.len(), 1);
+ assert_eq!(batch.entries[0].hash, *tx.hash());
+ }
+ assert!(batcher.flush(&source).is_none(), "INV echoed to the sender");
+ assert!(
+ batcher.flush(&advertiser).is_none(),
+ "INV sent to a peer that already advertised the TX"
+ );
+}
+
+#[tokio::test]
+async fn inv_batches_completed_by_relay_or_local_broadcast_reach_the_peer() {
+ use crate::flood::{InvEntry, INV_BATCH_MAX_SIZE};
+
+ let mut sender = TestNode::start().await;
+ let mut receiver = TestNode::start().await;
+ sender.connect(&receiver).await;
+ for round in 0..2u8 {
+ // Fill the receiver's pending batch to one short of full. The
+ // housekeeping flush needs 100 ms, so only the completing TX can
+ // send it; if that send were lost, these hashes would never arrive.
+ {
+ let mut batcher = sender.state.inv_batcher.write().await;
+ for index in 0..INV_BATCH_MAX_SIZE - 1 {
+ let mut hash = [round; 32];
+ hash[..8].copy_from_slice(&(index as u64).to_be_bytes());
+ assert!(batcher
+ .add(
+ receiver.peer,
+ InvEntry {
+ hash,
+ fee_per_op: 1
+ }
+ )
+ .is_none());
+ }
+ }
+ let tx = test_tx(i64::from(round) + 1);
+ if round == 0 {
+ handle_tx_response(&sender.state, &PeerId::random(), tx).await;
+ } else {
+ sender.handle.broadcast_tx(tx).await;
+ }
+ let expected = (u64::from(round) + 1) * INV_BATCH_MAX_SIZE as u64;
+ tokio::time::timeout(Duration::from_secs(5), async {
+ while receiver
+ .state
+ .metrics
+ .flood_demanded
+ .load(Ordering::Relaxed)
+ < expected
+ {
+ tokio::time::sleep(Duration::from_millis(5)).await;
+ }
+ })
+ .await
+ .unwrap_or_else(|_| panic!("round {round}: the completed INV batch never arrived"));
+ }
+ sender.stop().await;
+ receiver.stop().await;
+}
+
+#[tokio::test]
+async fn txset_protocol_is_mandatory_and_survives_reopening() {
+ let mut sender = TestNode::start().await;
+ let mut receiver = TestNode::start().await;
+ sender.connect(&receiver).await;
+ let streams = sender.streams_to(receiver.peer).await;
+ for legacy in ["/stellar/txset/1.0.0", "/stellar/txset/zstd/1.0.0"] {
+ let mut control = sender.state.control.clone();
+ assert!(matches!(
+ control
+ .open_stream(receiver.peer, StreamProtocol::new(legacy))
+ .await,
+ Err(libp2p_stream::OpenStreamError::UnsupportedProtocol(_))
+ ));
+ }
+ let (hash, xdr) = large_txset();
+ let shared = Arc::new(TxSetData::from_local(xdr.clone()).unwrap());
+ for reopen in [false, true] {
+ if reopen {
+ streams.txset.lock().await.take();
+ }
+ send_to_peer_stream(
+ &sender.state,
+ receiver.peer,
+ StreamType::TxSet,
+ shared.encoded(),
+ )
+ .await
+ .unwrap();
+ assert!(shared.encoded().len() < xdr.len() / 2);
+ {
+ let guard = streams.txset.lock().await;
+ let outbound = guard.as_ref().unwrap();
+ assert_eq!(outbound.protocol, TXSET_PROTOCOL);
+ assert_eq!(outbound.stream.priority().unwrap(), 1);
+ }
+ tokio::time::timeout(Duration::from_secs(10), async {
+ loop {
+ if let OverlayEvent::TxSetReceived {
+ hash: got, data, ..
+ } = receiver.events.recv().await.unwrap()
+ {
+ assert_eq!(got, hash);
+ assert_eq!(data.as_slice(), xdr);
+ assert_eq!(data.encoded(), shared.encoded());
+ break;
+ }
+ }
+ })
+ .await
+ .expect("compressed response was not delivered intact");
+ }
+ sender.stop().await;
+ receiver.stop().await;
+}
+
+#[tokio::test]
+async fn relay_forwards_original_compressed_bytes() {
+ let mut leader = TestNode::start().await;
+ let mut relay = TestNode::start().await;
+ let mut follower = TestNode::start().await;
+ leader.connect(&relay).await;
+ relay.connect(&follower).await;
+ let (hash, xdr) = large_txset();
+ // A different zstd level makes accidental re-encoding detectable.
+ let encoded = [
+ (xdr.len() as u32).to_be_bytes().as_slice(),
+ zstd::bulk::compress(&xdr, 7).unwrap().as_slice(),
+ ]
+ .concat();
+ assert_ne!(
+ encoded,
+ TxSetData::from_local(xdr.clone()).unwrap().encoded()
+ );
+ send_to_peer_stream(&leader.state, relay.peer, StreamType::TxSet, &encoded)
+ .await
+ .unwrap();
+ let received = tokio::time::timeout(Duration::from_secs(10), async {
+ loop {
+ if let OverlayEvent::TxSetReceived {
+ hash: got, data, ..
+ } = relay.events.recv().await.unwrap()
+ {
+ assert_eq!(got, hash);
+ assert_eq!(data.encoded(), encoded);
+ break data;
+ }
+ }
+ })
+ .await
+ .unwrap();
+ // Forward the very object a receiving application puts in its cache.
+ relay.handle.send_txset(hash, received, follower.peer).await;
+ tokio::time::timeout(Duration::from_secs(10), async {
+ loop {
+ if let OverlayEvent::TxSetReceived {
+ hash: got, data, ..
+ } = follower.events.recv().await.unwrap()
+ {
+ assert_eq!(got, hash);
+ assert_eq!(data.as_slice(), xdr);
+ assert_eq!(data.encoded(), encoded);
+ break;
+ }
+ }
+ })
+ .await
+ .unwrap();
+ leader.stop().await;
+ relay.stop().await;
+ follower.stop().await;
+}
+
+#[tokio::test]
+async fn malformed_compressed_txsets_are_dropped_without_losing_next_frame() {
+ let mut sender = TestNode::start().await;
+ let mut receiver = TestNode::start().await;
+ sender.connect(&receiver).await;
+ // Well-formed compression is insufficient: XDR must also strict-decode.
+ let malformed = Arc::new(TxSetData::from_local(vec![0xff; 1000]).unwrap());
+ send_to_peer_stream(
+ &sender.state,
+ receiver.peer,
+ StreamType::TxSet,
+ malformed.encoded(),
+ )
+ .await
+ .unwrap();
+ let (hash, xdr) = test_txset_xdr(19);
+ let valid = TxSetData::from_local(xdr.clone()).unwrap();
+ // The old raw escape is rejected, as are corrupt compressed payloads.
+ let raw = [
+ ((xdr.len() as u32) | (1 << 31)).to_be_bytes().as_slice(),
+ &xdr,
+ ]
+ .concat();
+ for bad in [raw, vec![0xff; 20]] {
+ send_to_peer_stream(&sender.state, receiver.peer, StreamType::TxSet, &bad)
+ .await
+ .unwrap();
+ }
+ send_to_peer_stream(
+ &sender.state,
+ receiver.peer,
+ StreamType::TxSet,
+ valid.encoded(),
+ )
+ .await
+ .unwrap();
+ tokio::time::timeout(Duration::from_secs(5), async {
+ loop {
+ if let OverlayEvent::TxSetReceived {
+ hash: got, data, ..
+ } = receiver.events.recv().await.unwrap()
+ {
+ assert_eq!(got, hash, "malformed XDR was forwarded");
+ assert_eq!(data.as_slice(), xdr);
+ break;
+ }
+ }
+ })
+ .await
+ .expect("a bad compressed frame prevented the next response");
+ sender.stop().await;
+ receiver.stop().await;
+}
+
+#[tokio::test]
+async fn inv_batch_demands_only_first_unseen_advertisements_in_order() {
+ use crate::flood::{InvBatch, InvEntry};
+
+ let advertiser = TestNode::start().await;
+ let mut receiver = TestNode::start().await;
+ advertiser.connect(&receiver).await;
+ let txs: Vec<_> = (1..=6).map(test_tx).collect();
+ for tx in &txs {
+ advertiser
+ .state
+ .tx_buffer
+ .write()
+ .await
+ .insert(Arc::clone(tx));
+ }
+ let hash = |index: usize| *txs[index].hash();
+ // txs[1] was already received; txs[2] was first advertised by another
+ // peer, which owns its demand; txs[5] is never advertised.
+ receiver.state.tx_seen.write().await.put(hash(1), ());
+ let other = PeerId::random();
+ assert!(receiver
+ .state
+ .inv_tracker
+ .write()
+ .await
+ .record_source(hash(2), other));
+
+ // Unsorted, with a repeat: demands must follow first-advertisement order.
+ let order = [4, 1, 0, 2, 4, 3];
+ let batch = InvBatch {
+ entries: order
+ .iter()
+ .map(|&index| InvEntry {
+ hash: hash(index),
+ fee_per_op: 0,
+ })
+ .collect(),
+ };
+ handle_inv_batch(&receiver.state, &advertiser.peer, batch).await;
+
+ // Counted when the demand is built, before any response can arrive. A
+ // demand for the seen TX would be counted here even though the receiver
+ // would then drop the response as a duplicate.
+ let demanded = [4, 0, 3];
+ assert_eq!(
+ receiver
+ .state
+ .metrics
+ .flood_demanded
+ .load(Ordering::Relaxed),
+ demanded.len() as u64
+ );
+ {
+ let tracker = receiver.state.inv_tracker.read().await;
+ let sources = |index: usize| tracker.peek_sources(&hash(index)).cloned();
+ // A seen TX is not tracked; a repeat does not add the peer twice;
+ // a later advertiser is recorded behind the first.
+ assert_eq!(sources(1), None);
+ assert_eq!(sources(4), Some(vec![advertiser.peer]));
+ assert_eq!(sources(2), Some(vec![other, advertiser.peer]));
+ assert_eq!(sources(5), None);
+ }
+ // The advertiser serves the one GETDATA in its hash order.
+ tokio::time::timeout(Duration::from_secs(5), async {
+ for index in demanded {
+ let Some(CoreCommand::SubmitTx { tx, .. }) = receiver._admissions.recv().await else {
+ panic!("expected transaction admission");
+ };
+ assert_eq!(tx.hash(), &hash(index), "demand order");
+ }
+ })
+ .await
+ .expect("demanded transactions must arrive");
+ assert!(receiver._admissions.try_recv().is_err());
+ receiver.stop().await;
+}
+
#[tokio::test]
async fn scp_state_sends_preserve_slot_order_without_blocking_other_peers() {
let mut sender = TestNode::start().await;
@@ -280,7 +941,11 @@ async fn blocked_peer_does_not_delay_other_txsets_or_scp_delivery() {
let (hash, data) = large_txset();
sender
.handle
- .send_txset(hash, data.clone(), slow.peer)
+ .send_txset(
+ hash,
+ Arc::new(TxSetData::from_local(data.clone()).unwrap()),
+ slow.peer,
+ )
.await;
// Also exercise reopening: this write can complete only if the dispatcher
@@ -289,12 +954,20 @@ async fn blocked_peer_does_not_delay_other_txsets_or_scp_delivery() {
healthy_streams.txset.lock().await.take();
sender
.handle
- .send_txset(hash, data.clone(), healthy.peer)
+ .send_txset(
+ hash,
+ Arc::new(TxSetData::from_local(data.clone()).unwrap()),
+ healthy.peer,
+ )
.await;
let (second_hash, second_data) = test_txset_xdr(2);
sender
.handle
- .send_txset(second_hash, second_data.clone(), healthy.peer)
+ .send_txset(
+ second_hash,
+ Arc::new(TxSetData::from_local(second_data.clone()).unwrap()),
+ healthy.peer,
+ )
.await;
let scp = test_scp_envelope_xdr(1234);
sender.handle.broadcast_scp(scp.clone()).await;
@@ -314,8 +987,8 @@ async fn blocked_peer_does_not_delay_other_txsets_or_scp_delivery() {
_ => {}
}
}
- assert_eq!(received.get(&hash), Some(&data));
- assert_eq!(received.get(&second_hash), Some(&second_data));
+ assert_eq!(received.get(&hash).unwrap().as_slice(), data);
+ assert_eq!(received.get(&second_hash).unwrap().as_slice(), second_data);
loop {
match slow.events.recv().await.unwrap() {
OverlayEvent::ScpReceived { envelope, .. } if envelope == scp => break,
@@ -339,7 +1012,7 @@ async fn blocked_peer_does_not_delay_other_txsets_or_scp_delivery() {
} = slow.events.recv().await.unwrap()
{
assert_eq!(received_hash, hash);
- assert_eq!(received_data, data);
+ assert_eq!(received_data.as_slice(), data);
break;
}
}
@@ -365,7 +1038,11 @@ async fn txset_fetch_reaches_peer_while_response_to_same_peer_is_blocked() {
let (response_hash, response_data) = test_txset_xdr(1);
sender
.handle
- .send_txset(response_hash, response_data.clone(), receiver.peer)
+ .send_txset(
+ response_hash,
+ Arc::new(TxSetData::from_local(response_data.clone()).unwrap()),
+ receiver.peer,
+ )
.await;
// Use the pre-opened stream, reuse it for another frame, then exercise
// reopening. All three requests must arrive while the response is blocked.
@@ -411,7 +1088,7 @@ async fn txset_fetch_reaches_peer_while_response_to_same_peer_is_blocked() {
receiver.events.recv().await.unwrap()
{
assert_eq!(hash, response_hash);
- assert_eq!(data, response_data);
+ assert_eq!(data.as_slice(), response_data);
break;
}
}
@@ -434,7 +1111,6 @@ async fn quic_transport_priorities_are_applied_to_each_route_and_reopening() {
let guard = mutex.lock().await;
assert_eq!(guard.as_ref().unwrap().stream.priority().unwrap(), expected);
}
- assert!(streams.legacy_txset_request.lock().await.is_none());
streams.txset.lock().await.take();
let (hash, data) = test_txset_xdr(1);
// Await the actual write, rather than command enqueue, to observe reopening.
@@ -442,7 +1118,7 @@ async fn quic_transport_priorities_are_applied_to_each_route_and_reopening() {
&sender.state,
receiver.peer,
StreamType::TxSet,
- &crate::xdr::frame_tx_set(&data),
+ TxSetData::from_local(data.clone()).unwrap().encoded(),
)
.await
.unwrap();
@@ -474,83 +1150,6 @@ async fn quic_transport_priorities_are_applied_to_each_route_and_reopening() {
receiver.stop().await;
}
-#[tokio::test]
-async fn legacy_peer_receives_scp_and_fetches_on_its_supported_routes() {
- let mut sender = TestNode::start().await;
- // This peer advertises only the old protocols, and its SCP reader rejects
- // GetTxSet. A silent send on the wrong route therefore fails this test.
- let mut legacy = TestNode::start_with_control(false).await;
- sender.connect(&legacy).await;
- let streams = sender.streams_to(legacy.peer).await;
- let blocked_response = streams.txset.lock().await;
- // Legacy compatibility must preserve request isolation, including when
- // negotiation and request streams need reopening during a blocked response.
- for id in [17, 18] {
- if id == 18 {
- streams.scp.lock().await.take();
- streams.legacy_txset_request.lock().await.take();
- }
- let hash = [id; 32];
- sender.handle.record_txset_source(hash, legacy.peer).await;
- sender.handle.fetch_txset(hash, id as u32).await;
- let scp = test_scp_envelope_xdr(id as u64);
- sender.handle.broadcast_scp(scp.clone()).await;
- tokio::time::timeout(Duration::from_secs(2), async {
- let mut got_request = false;
- let mut got_scp = false;
- while !got_request || !got_scp {
- match legacy.events.recv().await.unwrap() {
- OverlayEvent::TxSetRequested { hash: h, from } => {
- assert_eq!(h, hash);
- assert_eq!(from, sender.peer);
- got_request = true;
- }
- OverlayEvent::ScpReceived { envelope, .. } if envelope == scp => got_scp = true,
- _ => {}
- }
- }
- })
- .await
- .expect("legacy SCP or fetch waited for a blocked response or used an unsupported route");
- for (mutex, protocol) in [
- (&streams.scp, SCP_PROTOCOL),
- (&streams.legacy_txset_request, TXSET_PROTOCOL),
- ] {
- let guard = mutex.lock().await;
- let stream = guard.as_ref().unwrap();
- assert_eq!(stream.protocol, protocol);
- assert_eq!(stream.stream.priority().unwrap(), 2);
- }
- }
- drop(blocked_response);
-
- // Updated receivers still understand requests arriving on the old route.
- let hash = [17; 32];
- send_to_peer_stream(
- &legacy.state,
- sender.peer,
- StreamType::TxSet,
- &crate::xdr::frame_get_tx_set(hash),
- )
- .await
- .unwrap();
- tokio::time::timeout(Duration::from_secs(2), async {
- loop {
- if let OverlayEvent::TxSetRequested { hash: h, from } =
- sender.events.recv().await.unwrap()
- {
- assert_eq!(h, hash);
- assert_eq!(from, legacy.peer);
- break;
- }
- }
- })
- .await
- .unwrap();
- sender.stop().await;
- legacy.stop().await;
-}
-
async fn bulk_send_admission_is_bounded(byte_limit: bool) {
let (mut handle, _events, _admissions, overlay) =
create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())).unwrap();
@@ -570,8 +1169,14 @@ async fn bulk_send_admission_is_bounded(byte_limit: bool) {
handle.txset_send_slots = Arc::new(Semaphore::new(slots));
handle.txset_send_bytes = Arc::new(Semaphore::new(bytes));
let mut task = tokio::spawn(async move { overlay.run("127.0.0.1", 0).await });
- handle.send_txset(hash, data.clone(), peer).await;
- let waiting = handle.send_txset(hash, data, peer);
+ handle
+ .send_txset(
+ hash,
+ Arc::new(TxSetData::from_local(data.clone()).unwrap()),
+ peer,
+ )
+ .await;
+ let waiting = handle.send_txset(hash, Arc::new(TxSetData::from_local(data).unwrap()), peer);
tokio::pin!(waiting);
assert!(futures::poll!(&mut waiting).is_pending());
tokio::time::timeout(Duration::from_secs(1), handle.ping())
@@ -610,7 +1215,13 @@ async fn failed_txset_send_releases_admission() {
create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())).unwrap();
let mut task = tokio::spawn(async move { overlay.run("127.0.0.1", 0).await });
let (hash, data) = test_txset_xdr(1);
- handle.send_txset(hash, data, PeerId::random()).await;
+ handle
+ .send_txset(
+ hash,
+ Arc::new(TxSetData::from_local(data).unwrap()),
+ PeerId::random(),
+ )
+ .await;
tokio::time::timeout(Duration::from_secs(1), async {
let _all = Arc::clone(&handle.txset_send_slots)
.acquire_many_owned(MAX_OUTSTANDING_TXSET_SENDS as u32)
@@ -641,8 +1252,8 @@ async fn fetch_reservation_is_deduplicated_and_failure_preserves_reassignment()
.await
.insert(peer, Arc::new(PeerOutboundStreams::new()));
let hash = [3; 32];
- overlay.fetch_txset(hash, 12).await;
- overlay.fetch_txset(hash, 12).await;
+ overlay.fetch_txset(hash, 12, false).await;
+ overlay.fetch_txset(hash, 12, false).await;
assert_eq!(
overlay.sends.len(),
1,
@@ -678,12 +1289,90 @@ async fn fetch_reservation_is_deduplicated_and_failure_preserves_reassignment()
}
}
+#[tokio::test]
+async fn retried_fetch_moves_to_another_peer_and_stale_requests_are_pruned() {
+ let (_handle, _events, _admissions, mut overlay) =
+ create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())).unwrap();
+ let peers = [PeerId::random(), PeerId::random()];
+ for peer in peers {
+ overlay
+ .state
+ .peer_streams
+ .write()
+ .await
+ .insert(peer, Arc::new(PeerOutboundStreams::new()));
+ }
+ let hash = [5; 32];
+ let requested_peer = |overlay: &StellarOverlay| {
+ let state = Arc::clone(&overlay.state);
+ async move {
+ state
+ .pending_txset_requests
+ .read()
+ .await
+ .get(&hash)
+ .map(|(peer, _, slot)| (*peer, *slot))
+ }
+ };
+ // The peer that referenced the set in SCP is asked first.
+ overlay
+ .state
+ .txset_sources
+ .write()
+ .await
+ .put(hash, peers[0]);
+ overlay.fetch_txset(hash, 20, false).await;
+ assert_eq!(requested_peer(&overlay).await, Some((peers[0], 20)));
+ // Further prefetches are deduplicated against the outstanding request.
+ overlay.fetch_txset(hash, 20, false).await;
+ assert_eq!(overlay.sends.len(), 1);
+
+ // A retry replaces the unanswered request with one to another peer,
+ // although the SCP source still names the first.
+ overlay.fetch_txset(hash, 21, true).await;
+ assert_eq!(overlay.sends.len(), 2);
+ assert_eq!(requested_peer(&overlay).await, Some((peers[1], 21)));
+ // The next retry again goes to a peer other than the previous target.
+ let third = PeerId::random();
+ overlay
+ .state
+ .peer_streams
+ .write()
+ .await
+ .insert(third, Arc::new(PeerOutboundStreams::new()));
+ overlay.state.peer_streams.write().await.remove(&peers[0]);
+ overlay.fetch_txset(hash, 21, true).await;
+ assert_eq!(requested_peer(&overlay).await, Some((third, 21)));
+ overlay.state.peer_streams.write().await.remove(&third);
+ // With no other peer left, a retry asks the remaining one again.
+ overlay.fetch_txset(hash, 21, true).await;
+ assert_eq!(overlay.sends.len(), 4);
+ assert_eq!(requested_peer(&overlay).await, Some((peers[1], 21)));
+
+ // Requests that are never answered expire by slot.
+ let other = [6; 32];
+ overlay
+ .state
+ .pending_txset_requests
+ .write()
+ .await
+ .insert(other, (peers[1], Instant::now(), 40));
+ overlay.prune_txset_requests(30).await;
+ assert_eq!(requested_peer(&overlay).await, None);
+ assert!(overlay
+ .state
+ .pending_txset_requests
+ .read()
+ .await
+ .contains_key(&other));
+}
+
#[tokio::test]
async fn queued_peer_sends_share_payload_and_release_it_on_drop() {
let (handle, _events, _admissions, mut overlay) =
create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())).unwrap();
let (hash, data) = test_txset_xdr(1);
- let data = Arc::new(data);
+ let data = Arc::new(TxSetData::from_local(data).unwrap());
let retained = Arc::downgrade(&data);
for _ in 0..29 {
handle
diff --git a/overlay/src/libp2p_overlay/framed_io.rs b/overlay/src/libp2p_overlay/framed_io.rs
index 1332b066b0..b5a9e8e846 100644
--- a/overlay/src/libp2p_overlay/framed_io.rs
+++ b/overlay/src/libp2p_overlay/framed_io.rs
@@ -2,6 +2,26 @@
use futures::{AsyncWrite, AsyncWriteExt};
use std::io;
+/// Either one segmented frame or several complete length-prefixed frames.
+/// A batch preserves the wire boundaries of its individual messages.
+#[derive(Clone, Copy)]
+pub(super) enum Frames<'a> {
+ One(&'a [&'a [u8]]),
+ Batch(&'a [u8]),
+}
+
+impl Frames<'_> {
+ pub(super) async fn write(self, writer: &mut W) -> io::Result<()> {
+ match self {
+ Self::One(parts) => write_frame_parts(writer, parts).await,
+ Self::Batch(bytes) => {
+ writer.write_all(bytes).await?;
+ writer.flush().await
+ }
+ }
+ }
+}
+
/// Write a single length-prefixed frame without assembling a bulk copy.
/// Callers sharing a stream must hold its lock over this entire future.
pub(super) async fn write_frame_parts(
@@ -26,6 +46,7 @@ pub(super) async fn write_frame_parts(
#[cfg(test)]
mod tests {
use super::*;
+ use futures::AsyncRead;
use std::pin::Pin;
use std::task::{Context, Poll};
@@ -80,4 +101,114 @@ mod tests {
expected_frames.extend_from_slice(b"next frame");
assert_eq!(writer.bytes, expected_frames);
}
+
+ /// Delivers at most `max` bytes per read and yields between reads, like
+ /// a QUIC stream handing over a batch as packets arrive.
+ struct ShortReader {
+ bytes: Vec,
+ pos: usize,
+ max: usize,
+ yield_next: bool,
+ }
+
+ impl AsyncRead for ShortReader {
+ fn poll_read(
+ mut self: Pin<&mut Self>,
+ cx: &mut Context<'_>,
+ buf: &mut [u8],
+ ) -> Poll> {
+ if self.yield_next {
+ self.yield_next = false;
+ cx.waker().wake_by_ref();
+ return Poll::Pending;
+ }
+ self.yield_next = true;
+ let pos = self.pos;
+ let n = buf.len().min(self.max).min(self.bytes.len() - pos);
+ buf[..n].copy_from_slice(&self.bytes[pos..pos + n]);
+ self.pos += n;
+ Poll::Ready(Ok(n))
+ }
+ }
+
+ #[tokio::test]
+ async fn batched_frames_read_back_through_a_buffered_reader() {
+ // Frames from 1 byte to more than twice the read buffer: the large
+ // ones exercise the reader's bypass once its buffer is drained.
+ let frames: Vec> = (0..40u8).map(|i| vec![i; 1 + 61 * i as usize]).collect();
+ let mut batch = Vec::new();
+ for frame in &frames[..30] {
+ batch.extend_from_slice(&(frame.len() as u32).to_be_bytes());
+ batch.extend_from_slice(frame);
+ }
+ let mut writer = ShortWriter::default();
+ Frames::Batch(&batch).write(&mut writer).await.unwrap();
+ for frame in &frames[30..] {
+ Frames::One(&[frame]).write(&mut writer).await.unwrap();
+ }
+
+ let reader = ShortReader {
+ bytes: writer.bytes,
+ pos: 0,
+ max: 700,
+ yield_next: false,
+ };
+ let mut reader = futures::io::BufReader::with_capacity(1024, reader);
+ for frame in &frames {
+ assert_eq!(
+ &super::super::read_framed(&mut reader).await.unwrap(),
+ frame
+ );
+ }
+ let end = super::super::read_framed(&mut reader).await.unwrap_err();
+ assert_eq!(end.kind(), io::ErrorKind::UnexpectedEof);
+ }
+
+ /// Delivers its bytes, then never has more and never wakes the reader.
+ struct StallingReader {
+ bytes: Vec,
+ pos: usize,
+ }
+
+ impl AsyncRead for StallingReader {
+ fn poll_read(
+ mut self: Pin<&mut Self>,
+ _: &mut Context<'_>,
+ buf: &mut [u8],
+ ) -> Poll> {
+ let pos = self.pos;
+ if pos == self.bytes.len() {
+ return Poll::Pending;
+ }
+ let n = buf.len().min(self.bytes.len() - pos);
+ buf[..n].copy_from_slice(&self.bytes[pos..pos + n]);
+ self.pos += n;
+ Poll::Ready(Ok(n))
+ }
+ }
+
+ #[tokio::test]
+ async fn buffered_tx_stream_takes_in_available_frames_without_waiting_for_more() {
+ let frames = [&b"first"[..], &b"second frame"[..]];
+ let mut bytes = Vec::new();
+ for frame in frames {
+ bytes.extend_from_slice(&(frame.len() as u32).to_be_bytes());
+ bytes.extend_from_slice(frame);
+ }
+ let available = bytes.len();
+ let mut reader = super::super::buffered_tx_stream(StallingReader { bytes, pos: 0 });
+
+ for (index, frame) in frames.into_iter().enumerate() {
+ let read = futures::poll!(Box::pin(super::super::read_framed(&mut reader)));
+ assert!(
+ matches!(&read, Poll::Ready(Ok(data)) if data == frame),
+ "frame {index} was available but not returned: {read:?}"
+ );
+ // One inner read took in both frames.
+ assert_eq!(reader.get_ref().pos, available);
+ }
+ // Only a read past the available data waits.
+ let next = futures::poll!(Box::pin(super::super::read_framed(&mut reader)));
+ assert!(next.is_pending());
+ }
}
diff --git a/overlay/src/main.rs b/overlay/src/main.rs
index 752ea0d62c..5b2dfa3f6a 100644
--- a/overlay/src/main.rs
+++ b/overlay/src/main.rs
@@ -20,7 +20,7 @@ use tracing::{debug, error, info, warn};
use libp2p::identity::Keypair as Libp2pKeypair;
use libp2p::{Multiaddr, PeerId};
use stellar_overlay::config::Config;
-use stellar_overlay::flood::{CachedTxSet, Hash256, TxSetCache};
+use stellar_overlay::flood::{CachedTxSet, Hash256, TxSetCache, TxSetData};
use stellar_overlay::integrated::{Overlay, OverlayHandle};
use stellar_overlay::ipc::{CoreIpc, Message, MessageType};
use stellar_overlay::libp2p_overlay::{
@@ -375,15 +375,15 @@ fn get_cached_tx_set_xdr<'a>(tx_set_cache: &'a TxSetCache, hash: &Hash256) -> Op
tx_set_cache.get(hash).map(|cached| cached.xdr.as_slice())
}
-fn cache_tx_set_xdr(
+fn cache_tx_set(
tx_set_cache: &mut TxSetCache,
current_ledger_seq: u32,
hash: Hash256,
- xdr: Vec,
+ data: Arc,
) {
tx_set_cache.insert(CachedTxSet {
hash,
- xdr: Arc::new(xdr),
+ xdr: data,
ledger_seq: current_ledger_seq,
});
}
@@ -734,7 +734,7 @@ impl App {
// This ensures the TxSet is available when SCP processing resumes
// Stamp with the slot the set was requested for so eviction is
// exact; for an unsolicited set fall back to the next slot.
- cache_tx_set_xdr(
+ cache_tx_set(
&mut self.tx_set_cache,
slot.unwrap_or(self.current_ledger_seq + 1),
hash,
@@ -1015,6 +1015,10 @@ impl App {
hash.copy_from_slice(&msg.payload[0..32]);
let slot = u32::from_le_bytes(msg.payload[32..36].try_into().unwrap());
+ // Core requests a hash once per fetch and re-requests it only
+ // when the earlier request has not delivered. Retry that from
+ // another peer instead of deduplicating against the stuck one.
+ let retry = self.pending_core_tx_sets.contains_key(&hash);
self.pending_core_tx_sets
.entry(hash)
.and_modify(|needed_slot| *needed_slot = (*needed_slot).max(slot))
@@ -1033,12 +1037,17 @@ impl App {
// Not in local cache - request from peers. Spawn so the main
// loop never awaits on the bounded libp2p cmd channel.
info!(
- "TXSET_FETCH_START: TX set {:02x?}... not in cache, fetching from peers",
- &hash[..4]
+ "TXSET_FETCH_START: TX set {:02x?}... not in cache, fetching from peers{}",
+ &hash[..4],
+ if retry { " (retry)" } else { "" }
);
let handle = self.libp2p_handle.clone();
tokio::spawn(async move {
- handle.fetch_txset(hash, slot).await;
+ if retry {
+ handle.refetch_txset(hash, slot).await;
+ } else {
+ handle.fetch_txset(hash, slot).await;
+ }
});
}
}
@@ -1054,13 +1063,14 @@ impl App {
let mut hash = [0u8; 32];
hash.copy_from_slice(&msg.payload[0..32]);
let slot = u32::from_le_bytes(msg.payload[32..36].try_into().unwrap());
- let tx_set_xdr = &msg.payload[36..];
+ let mut payload = msg.payload;
+ let tx_set_xdr = payload.split_off(36);
// Core is trusted for encoding, so we skip decoding. We still
// guard the content hash cheaply: caching bytes under a hash
// that peers would recompute differently makes the tx set
// unfetchable network-wide.
- if !xdr::tx_set_hash_matches(&hash, tx_set_xdr) {
+ if !xdr::tx_set_hash_matches(&hash, &tx_set_xdr) {
warn!(
"TXSET_CACHE_DROP: Dropping TX set {:02x?}... from Core: content hash mismatch",
&hash[..4]
@@ -1075,7 +1085,34 @@ impl App {
tx_set_xdr.len()
);
- cache_tx_set_xdr(&mut self.tx_set_cache, slot, hash, tx_set_xdr.to_vec());
+ // Keep an existing received/prepared representation intact on
+ // repeated IPC publication of the same content hash.
+ let data = if let Some(cached) = self.tx_set_cache.get(&hash) {
+ Arc::clone(&cached.xdr)
+ } else {
+ // Core sends this before final proposal validation. Await
+ // completion on this task while the network and Core keep
+ // running. Requests queue until the ready set is cached.
+ let started = std::time::Instant::now();
+ match tokio::task::spawn_blocking(move || TxSetData::from_local(tx_set_xdr))
+ .await
+ {
+ Ok(Ok(data)) => {
+ info!("TXSET_COMPRESS: Prepared locally built set {:02x?}... raw_bytes={} encoded_bytes={} duration_us={}",
+ &hash[..4], data.len(), data.encoded().len(), started.elapsed().as_micros());
+ Arc::new(data)
+ }
+ result => {
+ error!(
+ "TXSET_CACHE_DROP: Failed to encode local set {:02x?}...: {:?}",
+ &hash[..4],
+ result
+ );
+ return true;
+ }
+ }
+ };
+ cache_tx_set(&mut self.tx_set_cache, slot, hash, data);
self.send_requested_tx_set(&hash);
}
@@ -1149,6 +1186,13 @@ impl App {
.evict_before(ledger_seq.saturating_sub(12));
self.pending_core_tx_sets
.retain(|_, slot| *slot >= ledger_seq.saturating_sub(12));
+ // Requests that were never answered expire with the same
+ // retained-slot window.
+ let handle = self.libp2p_handle.clone();
+ let min_slot = ledger_seq.saturating_sub(12);
+ tokio::spawn(async move {
+ handle.prune_txset_requests(min_slot).await;
+ });
}
}
@@ -2054,13 +2098,65 @@ mod tests {
assert_eq!(top[0].hash(), tx.hash());
}
+ #[tokio::test]
+ async fn local_publication_is_eager_and_preserves_existing_encoding() {
+ let (mut app, _core) = test_app();
+ let (hash, xdr) = test_txset_xdr(29);
+ let mut payload = request_tx_set_payload(&hash, 100);
+ payload.extend_from_slice(&xdr);
+ app.handle_core_message(Message::new(MessageType::CacheTxSet, payload.clone()))
+ .await;
+ let data = Arc::clone(&app.tx_set_cache.get(&hash).unwrap().xdr);
+ // No send has occurred: publication itself prepared the response.
+ assert_eq!(
+ zstd::bulk::decompress(&data.encoded()[4..], data.len()).unwrap(),
+ xdr
+ );
+ app.handle_core_message(Message::new(MessageType::CacheTxSet, payload))
+ .await;
+ assert!(Arc::ptr_eq(
+ &data,
+ &app.tx_set_cache.get(&hash).unwrap().xdr
+ ));
+ }
+
+ #[tokio::test]
+ async fn received_encoding_survives_cache_and_local_duplicate() {
+ let (mut app, _core) = test_app();
+ let (hash, xdr) = test_txset_xdr(30);
+ let received = Arc::new(TxSetData::from_local(xdr.clone()).unwrap());
+ app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
+ hash,
+ data: Arc::clone(&received),
+ from: PeerId::random(),
+ slot: Some(100),
+ })
+ .await;
+ assert!(Arc::ptr_eq(
+ &received,
+ &app.tx_set_cache.get(&hash).unwrap().xdr
+ ));
+ let mut payload = request_tx_set_payload(&hash, 101);
+ payload.extend_from_slice(&xdr);
+ app.handle_core_message(Message::new(MessageType::CacheTxSet, payload))
+ .await;
+ let cached = app.tx_set_cache.get(&hash).unwrap();
+ assert!(Arc::ptr_eq(&received, &cached.xdr));
+ assert_eq!(cached.ledger_seq, 101);
+ }
+
#[tokio::test]
async fn peer_responses_share_cached_storage_and_survive_eviction() {
// Keep the network dispatcher alive but unpolled: responses remain
// queued or awaiting dispatch, as when the transport is delayed.
let (mut app, _core, network) = test_app_with_network();
let (hash, data) = test_txset_xdr(7);
- cache_tx_set_xdr(&mut app.tx_set_cache, 1, hash, data.clone());
+ cache_tx_set(
+ &mut app.tx_set_cache,
+ 1,
+ hash,
+ Arc::new(TxSetData::from_local(data.clone()).unwrap()),
+ );
let cached = Arc::downgrade(&app.tx_set_cache.get(&hash).unwrap().xdr);
for _ in 0..29 {
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetRequested {
@@ -2128,7 +2224,7 @@ mod tests {
let (hash, data) = test_txset_xdr(31);
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash,
- data: data.clone(),
+ data: Arc::new(TxSetData::from_local(data.clone()).unwrap()),
from: PeerId::random(),
slot: Some(100),
})
@@ -2168,7 +2264,7 @@ mod tests {
let (other, other_data) = test_txset_xdr(33);
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash: other,
- data: other_data,
+ data: Arc::new(TxSetData::from_local(other_data).unwrap()),
from: PeerId::random(),
slot: Some(101),
})
@@ -2179,7 +2275,7 @@ mod tests {
for arrival in 0..2 {
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash,
- data: data.clone(),
+ data: Arc::new(TxSetData::from_local(data.clone()).unwrap()),
from: PeerId::random(),
slot: Some(101),
})
@@ -2219,7 +2315,7 @@ mod tests {
for (h, bytes) in [(hash, data.clone()), (other, other_data)] {
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash: h,
- data: bytes,
+ data: Arc::new(TxSetData::from_local(bytes).unwrap()),
from: PeerId::random(),
slot: Some(100),
})
@@ -2235,7 +2331,7 @@ mod tests {
assert!(app.pending_core_tx_sets.contains_key(&hash));
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash,
- data: data.clone(),
+ data: Arc::new(TxSetData::from_local(data.clone()).unwrap()),
from: PeerId::random(),
slot: Some(100),
})
@@ -2270,7 +2366,7 @@ mod tests {
assert_eq!(app.pending_core_tx_sets.get(&future), Some(&120));
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash: old,
- data: data.clone(),
+ data: Arc::new(TxSetData::from_local(data.clone()).unwrap()),
from: PeerId::random(),
slot: Some(100),
})
@@ -2315,7 +2411,7 @@ mod tests {
.await;
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash,
- data: data.clone(),
+ data: Arc::new(TxSetData::from_local(data.clone()).unwrap()),
from: PeerId::random(),
slot: Some(100),
})
@@ -2334,7 +2430,12 @@ mod tests {
// Cache the set, so if the handler wrongly accepted the legacy
// format it would respond with TxSetAvailable below.
- cache_tx_set_xdr(&mut app.tx_set_cache, 1, hash, xdr_bytes);
+ cache_tx_set(
+ &mut app.tx_set_cache,
+ 1,
+ hash,
+ Arc::new(TxSetData::from_local(xdr_bytes).unwrap()),
+ );
// Pre-slotSeq payload: [hash:32] only. The protocol is now
// [hash:32][slotSeq:4]; the short payload must be dropped.
@@ -2411,7 +2512,7 @@ mod tests {
app.handle_libp2p_event(LibP2pOverlayEvent::TxSetReceived {
hash,
- data: xdr_bytes.clone(),
+ data: Arc::new(TxSetData::from_local(xdr_bytes.clone()).unwrap()),
from: PeerId::random(),
slot: Some(100),
})
diff --git a/overlay/src/wire.rs b/overlay/src/wire.rs
index 6680c7fa43..b5332e2ef6 100644
--- a/overlay/src/wire.rs
+++ b/overlay/src/wire.rs
@@ -109,10 +109,19 @@ impl ValidatedTx {
self.fee / i64::from(self.num_ops.max(1))
}
- /// The `StellarMessage::Transaction(..)` wire framing for flooding this tx.
+ /// The `StellarMessage::Transaction(..)` wire framing for flooding this tx,
+ /// as one owned buffer. The reference encoding; the flooding path itself
+ /// writes [`Self::flood_frame_parts`] without this copy.
pub fn to_flood_frame(&self) -> Vec {
xdr::frame_transaction(&self.bytes)
}
+
+ /// The same framing as [`Self::to_flood_frame`], as borrowed parts
+ /// (discriminant, envelope) whose concatenation is the message. Writers
+ /// copy or send these in place, without allocating a framed copy.
+ pub fn flood_frame_parts(&self) -> [&[u8]; 2] {
+ [&xdr::TRANSACTION_DISCRIMINANT, &self.bytes]
+ }
}
impl fmt::Debug for ValidatedTx {
@@ -194,5 +203,7 @@ mod tests {
.to_xdr(Limits::none())
.unwrap();
assert_eq!(tx.to_flood_frame(), expected);
+ assert_eq!(tx.flood_frame_parts().concat(), expected);
+ assert!(std::ptr::eq(tx.flood_frame_parts()[1], tx.bytes()));
}
}
diff --git a/overlay/src/xdr.rs b/overlay/src/xdr.rs
index 8f0ded0bb0..e656071578 100644
--- a/overlay/src/xdr.rs
+++ b/overlay/src/xdr.rs
@@ -74,6 +74,11 @@ pub(crate) fn frame_transaction(envelope_bytes: &[u8]) -> Vec {
frame(MessageType::Transaction, envelope_bytes)
}
+/// The discriminant `frame_transaction` prefixes, for writers that place the
+/// envelope bytes directly instead of first building a framed copy.
+pub(crate) const TRANSACTION_DISCRIMINANT: [u8; 4] =
+ (MessageType::Transaction as i32).to_be_bytes();
+
/// `StellarMessage::ScpMessage(..)` framing over canonical envelope bytes.
pub(crate) fn frame_scp(envelope_bytes: &[u8]) -> Vec {
frame(MessageType::ScpMessage, envelope_bytes)
diff --git a/src/crypto/SecretKey.cpp b/src/crypto/SecretKey.cpp
index ba36b1bb68..b73994c619 100644
--- a/src/crypto/SecretKey.cpp
+++ b/src/crypto/SecretKey.cpp
@@ -354,6 +354,19 @@ PubKeyUtils::seedVerifySigCache(unsigned int seed)
}
}
+void
+PubKeyUtils::getVerifySigCacheCounts(uint64_t& hits, uint64_t& misses)
+{
+ hits = 0;
+ misses = 0;
+ for (auto& shard : gVerifySigCacheShards)
+ {
+ LOCK_GUARD(shard.mMutex, guard);
+ hits += shard.mHits;
+ misses += shard.mMisses;
+ }
+}
+
void
PubKeyUtils::flushVerifySigCacheCounts(uint64_t& hits, uint64_t& misses)
{
diff --git a/src/crypto/SecretKey.h b/src/crypto/SecretKey.h
index c30286e4d0..08e55ce9d8 100644
--- a/src/crypto/SecretKey.h
+++ b/src/crypto/SecretKey.h
@@ -165,6 +165,8 @@ VerifySigResult verifySig(PublicKey const& key, Signature const& signature,
void clearVerifySigCache();
void seedVerifySigCache(unsigned int seed);
void flushVerifySigCacheCounts(uint64_t& hits, uint64_t& misses);
+// The lookup counts since the last flush, without resetting them.
+void getVerifySigCacheCounts(uint64_t& hits, uint64_t& misses);
PublicKey random();
#ifdef BUILD_TESTS
diff --git a/src/herder/Herder.h b/src/herder/Herder.h
index 66207170ee..4492254967 100644
--- a/src/herder/Herder.h
+++ b/src/herder/Herder.h
@@ -200,8 +200,8 @@ class Herder
virtual uint32 getMinLedgerSeqToAskPeers() const = 0;
virtual uint32 getMinLedgerSeqToRemember() const = 0;
- virtual bool isNewerNominationOrBallotSt(SCPStatement const& oldSt,
- SCPStatement const& newSt) = 0;
+ virtual bool isNewerStatement(SCPStatement const& oldSt,
+ SCPStatement const& newSt) = 0;
// Returns sequence number for most recent completed checkpoint that the
// node knows about, as derived from
diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp
index 9d25ad6929..1a66412bb7 100644
--- a/src/herder/HerderImpl.cpp
+++ b/src/herder/HerderImpl.cpp
@@ -29,14 +29,17 @@
#include "main/PersistentState.h"
#include "medida/counter.h"
#include "medida/meter.h"
+#include "overlay/OverlayIPC.h"
#include "overlay/RustOverlayManager.h"
#include "process/ProcessManager.h"
#include "scp/LocalNode.h"
#include "scp/Slot.h"
#include "simulation/LoadGenerator.h"
#include "transactions/MutableTransactionResult.h"
+#include "transactions/SignatureUtils.h"
#include "transactions/TransactionFrameBase.h"
#include "transactions/TransactionUtils.h"
+#include "util/BatchExecutor.h"
#include "util/DebugMetaUtils.h"
#include "util/Decoder.h"
#include "util/LogSlowExecution.h"
@@ -56,12 +59,182 @@
#include
#include
#include
+#include
#include
using namespace std;
namespace stellar
{
+namespace
+{
+// Verifies the signatures in `env` that match the master keys of its source,
+// operation source and fee source accounts. This populates the
+// signature-verification cache that tx-set validation consults with the same
+// (key, signature, hash). Other signers need ledger state; validation verifies
+// them itself. Returns how many signatures it verified (were not cached).
+size_t
+verifyMasterKeySignatures(Hash const& networkID, TransactionEnvelope const& env)
+{
+ // Bound the work per transaction: a signature is only verified against
+ // keys whose hint it matches, but colliding hints could otherwise make a
+ // crafted transaction cost many verifications.
+ size_t budget = 20;
+ size_t verified = 0;
+ auto verify = [&budget, &verified](Hash const& contentsHash,
+ auto const& signatures,
+ std::vector const& keys) {
+ for (auto const& sig : signatures)
+ {
+ for (auto const& key : keys)
+ {
+ if (!SignatureUtils::doesHintMatch(key.ed25519(), sig.hint))
+ {
+ continue;
+ }
+ if (budget == 0)
+ {
+ return;
+ }
+ --budget;
+ auto const res = SignatureUtils::verify(sig, key, contentsHash);
+ if (res.cacheResult ==
+ PubKeyUtils::VerifySigCacheLookupResult::MISS)
+ {
+ ++verified;
+ }
+ if (res.valid)
+ {
+ break;
+ }
+ }
+ }
+ };
+ auto keysOf = [](AccountID const& source, auto const& operations) {
+ std::vector keys{source};
+ for (auto const& op : operations)
+ {
+ if (op.sourceAccount)
+ {
+ auto key = toAccountID(*op.sourceAccount);
+ if (std::find(keys.begin(), keys.end(), key) == keys.end())
+ {
+ keys.emplace_back(std::move(key));
+ }
+ }
+ }
+ return keys;
+ };
+
+ auto frame = TransactionFrameBase::makeTransactionFromWire(networkID, env);
+ switch (env.type())
+ {
+ case ENVELOPE_TYPE_TX_V0:
+ verify(frame->getContentsHash(), env.v0().signatures,
+ keysOf(frame->getSourceID(), env.v0().tx.operations));
+ break;
+ case ENVELOPE_TYPE_TX:
+ verify(frame->getContentsHash(), env.v1().signatures,
+ keysOf(frame->getSourceID(), env.v1().tx.operations));
+ break;
+ case ENVELOPE_TYPE_TX_FEE_BUMP:
+ {
+ verify(frame->getContentsHash(), env.feeBump().signatures,
+ {frame->getFeeSourceID()});
+ TransactionEnvelope inner(ENVELOPE_TYPE_TX);
+ inner.v1() = env.feeBump().tx.innerTx.v1();
+ verified += verifyMasterKeySignatures(networkID, inner);
+ break;
+ }
+ default:
+ break;
+ }
+ return verified;
+}
+
+// Whether an envelope's transaction goes into the Soroban phase, as
+// TransactionFrame::isSoroban decides it (by the first operation).
+bool
+isSorobanEnvelope(TransactionEnvelope const& env)
+{
+ auto const* ops = env.type() == ENVELOPE_TYPE_TX_V0
+ ? &env.v0().tx.operations
+ : env.type() == ENVELOPE_TYPE_TX ? &env.v1().tx.operations
+ : env.type() == ENVELOPE_TYPE_TX_FEE_BUMP
+ ? &env.feeBump().tx.innerTx.v1().tx.operations
+ : nullptr;
+ if (ops == nullptr || ops->empty())
+ {
+ return false;
+ }
+ switch (ops->front().body.type())
+ {
+ case INVOKE_HOST_FUNCTION:
+ case EXTEND_FOOTPRINT_TTL:
+ case RESTORE_FOOTPRINT:
+ return true;
+ default:
+ return false;
+ }
+}
+
+// The first `windows[phase]` envelopes of each phase (classic, then Soroban;
+// a phase without a window is skipped), in mempool order.
+std::vector
+selectPrewarmCandidates(std::vector&& envelopes,
+ std::vector const& windows)
+{
+ std::vector selected;
+ std::vector taken(windows.size(), 0);
+ for (auto& env : envelopes)
+ {
+ size_t const phase = isSorobanEnvelope(env) ? 1 : 0;
+ if (phase < windows.size() && taken[phase] < windows[phase])
+ {
+ ++taken[phase];
+ selected.push_back(std::move(env));
+ }
+ }
+ return selected;
+}
+
+// The number of transactions in each phase of a generalized tx set.
+std::vector
+txCountPerPhase(TxSetXDRFrame const& txSet)
+{
+ std::vector counts;
+ if (!txSet.isGeneralizedTxSet())
+ {
+ return counts;
+ }
+ for (auto const& phase : txSet.getGeneralizedXDR().v1TxSet().phases)
+ {
+ size_t count = 0;
+ if (phase.v() == 0)
+ {
+ for (auto const& component : phase.v0Components())
+ {
+ count += component.txsMaybeDiscountedFee().txs.size();
+ }
+ }
+ else
+ {
+ for (auto const& stage :
+ phase.parallelTxsComponent().executionStages)
+ {
+ for (auto const& cluster : stage)
+ {
+ count += cluster.size();
+ }
+ }
+ }
+ counts.push_back(count);
+ }
+ return counts;
+}
+
+} // namespace
+
// Roughly ~10 minutes of consensus
constexpr uint32 const CLOSE_TIME_DRIFT_LEDGER_WINDOW_SIZE = 120;
// 10 seconds of drift threshold
@@ -306,6 +479,7 @@ void
HerderImpl::shutdown()
{
discardPreparedTxSet();
+ stopSignaturePrewarm();
mTrackingTimer.cancel();
mOutOfSyncTimer.cancel();
mTriggerTimer.cancel();
@@ -349,22 +523,16 @@ HerderImpl::processExternalized(uint64 slotIndex, StellarValue const& value,
releaseAssert(externalizedSet != nullptr);
// Notify overlay to clear TXs from mempool (for RustOverlayManager)
- // Extract TX hashes from the externalized set so Rust can remove them
+ // Extract TX hashes from the externalized set so Rust can remove them.
+ // Hash the envelopes in place: building a frame per transaction would
+ // copy every envelope on the main thread only to hash it.
std::vector txHashes;
if (externalizedSet)
{
- auto txFramesList =
- externalizedSet->createTransactionFrames(mApp.getNetworkID());
- for (auto const& txPhase : txFramesList)
- {
- for (auto const& txFrame : txPhase)
- {
- txHashes.push_back(txFrame->getFullHash());
- }
- }
+ txHashes = externalizedSet->getTransactionFullHashes();
#ifdef BUILD_TESTS
mApp.getLoadGenerator().cleanupAccounts(
- static_cast(slotIndex), txFramesList);
+ static_cast(slotIndex), *externalizedSet);
#endif
}
mApp.getOverlayManager().notifyTxSetExternalized(value.txSetHash, txHashes);
@@ -505,6 +673,12 @@ HerderImpl::valueExternalized(uint64 slotIndex, StellarValue const& value,
{
ZoneScoped;
int const DUMP_SCP_TIMEOUT_SECONDS = 20;
+ // A prewarm for this slot is done serving; the one for the next ledger
+ // (started when this slot's ballot did) keeps running.
+ if (mSignaturePrewarm && mSignaturePrewarm->ledgerSeq <= slotIndex)
+ {
+ stopSignaturePrewarm();
+ }
recordExternalizeAndCheckCloseTimeDrift(slotIndex, value,
mDriftCTSlidingWindow);
@@ -825,6 +999,12 @@ HerderImpl::recvSCPEnvelope(SCPEnvelope const& envelope)
mSCPMetrics.mEnvelopeReceive.Mark();
+ if (envelope.statement.pledges.type() == SCP_ST_NOMINATE)
+ {
+ // There is no nomination phase (see HerderSCPDriver::getRoundLeader).
+ return Herder::ENVELOPE_STATUS_DISCARDED;
+ }
+
// **** first perform checks that do NOT require signature verification
// this allows to fast fail messages that we'd throw away anyways
@@ -1510,11 +1690,14 @@ HerderImpl::setupTriggerNextLedger()
},
&VirtualTimer::onFailureNoop);
- if (getSCP().isValidator())
+ // Prepare early only if the trigger is still ahead. A due trigger
+ // fires now and builds itself; both timers would fire at the same
+ // instant in an unspecified order (event sequence numbers are per
+ // timer), so the leader could build once to prepare and again at the
+ // trigger.
+ if (getSCP().isValidator() && !triggerDue)
{
- auto leaders = getSCP().predictNominationLeaders(
- nextIndex, xdr::xdr_to_opaque(lcl.header.scpValue), 2);
- if (leaders.count(getSCP().getLocalNodeID()))
+ if (mHerderSCPDriver.isLocalNodeRoundLeader(nextIndex, 1))
{
// Validate against the close time we expect at the trigger,
// not the earlier wall-clock time at which we start work.
@@ -1686,6 +1869,11 @@ HerderImpl::prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime)
{
return;
}
+ if (!getSCP().isValidator() ||
+ !mHerderSCPDriver.isLocalNodeRoundLeader(ledgerSeq, 1))
+ {
+ return;
+ }
mPreparedTxSet = buildTxSet(ledgerSeq, closeTime);
CLOG_INFO(Herder,
"Prepared TX set for ledger {} before trigger: {} transactions, "
@@ -1701,6 +1889,15 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime)
auto const lcl = mLedgerManager.getLastClosedLedgerHeader();
releaseAssert(ledgerSeq == lcl.header.ledgerSeq + 1);
releaseAssert(closeTime > getConsensusTime(lcl.header.scpValue));
+ uint32_t const round = getSCP().getLeaderRound(ledgerSeq);
+ bool const activeLeader =
+ round != 0 && mHerderSCPDriver.isLocalNodeRoundLeader(ledgerSeq, round);
+ bool const legacyTxSet = buildsLegacyTxSet(lcl.header.ledgerVersion);
+ CLOG_INFO(Herder, "Building TX set as {} for ledger {}",
+ activeLeader ? fmt::format("round {} leader", round)
+ : legacyTxSet ? std::string("validator (legacy tx set)")
+ : std::string("first-round leader (early)"),
+ ledgerSeq);
// Apply-time offset: see the comment in triggerNextLedger
auto const closeTimeOffset =
closeTime.toApplyTime() - getApplyTime(lcl.header.scpValue);
@@ -1713,20 +1910,16 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime)
// Get TXs from Rust overlay
auto& overlayMgr = mApp.getOverlayManager();
- auto& lm = mApp.getLedgerManager();
- size_t maxCandidates = lm.getLastMaxTxSetSizeOps();
- if (lm.hasLastClosedSorobanNetworkConfig())
- {
- maxCandidates +=
- lm.getLastClosedSorobanNetworkConfig().ledgerMaxTxCount();
- }
- auto txEnvelopes =
-#ifdef BUILD_TESTS
- mGetTopTransactionsForTesting
- ? mGetTopTransactionsForTesting(maxCandidates * 2)
- :
-#endif
- overlayMgr.getTopTransactions(maxCandidates * 2);
+ // This build validates every candidate itself.
+ stopSignaturePrewarm();
+ auto& metrics = mApp.getMetrics();
+ auto txEnvelopes = [&]() {
+ auto t =
+ metrics.NewTimer({"herder", "txset", "build-pull"}).TimeScope();
+ return pullTopTransactions(maxTxSetCandidates() * 2);
+ }();
+ auto framesTimer =
+ metrics.NewTimer({"herder", "txset", "build-frames"}).TimeScope();
CLOG_INFO(Herder, "Got {} transactions from Rust overlay mempool",
txEnvelopes.size());
@@ -1792,11 +1985,39 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime)
txPhases.emplace_back(std::move(sorobanTxs));
}
+ // Validate each phase only until it holds enough valid transactions (see
+ // validTxTargets).
+ auto const targets = validTxTargets(lcl.header.ledgerVersion);
+
PerPhaseTransactionList invalidTxPhases;
invalidTxPhases.resize(txPhases.size());
+ framesTimer.Stop();
+ uint64_t sigCacheHits = 0;
+ uint64_t sigCacheMisses = 0;
+ PubKeyUtils::getVerifySigCacheCounts(sigCacheHits, sigCacheMisses);
std::tie(proposedSet, applicableProposedSet) = makeTxSetFromTransactions(
- txPhases, mApp, closeTimeOffset, invalidTxPhases);
+ txPhases, mApp, closeTimeOffset, invalidTxPhases
+#ifdef BUILD_TESTS
+ ,
+ false, {}, false
+#endif
+ ,
+ [&](TxSetXDRFrameConstPtr const& txSet) {
+ auto t = metrics.NewTimer({"herder", "txset", "build-handoff"})
+ .TimeScope();
+ // Hand the final XDR to the overlay before the builder's remaining
+ // validation. Its eager compression runs concurrently in Rust.
+ if (txSet->isGeneralizedTxSet())
+ {
+ // Serialized straight into the IPC message, without copying
+ // the set's XDR first.
+ overlayMgr.cacheTxSet(txSet->getContentsHash(),
+ txSet->getGeneralizedXDR(), ledgerSeq);
+ }
+ },
+ targets);
+ markSignatureCacheLookups(mApp, "build", sigCacheHits, sigCacheMisses);
CLOG_INFO(Herder, "Proposed TX set has {} transactions",
proposedSet->sizeTxTotal());
@@ -1834,6 +2055,11 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime)
// 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)
{
@@ -1932,15 +2158,315 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
if (!isCtValid)
{
CLOG_WARNING(Herder,
- "Invalid close time selected ({}), skipping nomination",
+ "Invalid close time selected ({}), skipping the slot",
nextCloseTime.toString());
return;
}
// Start the trigger-to-ballot interval only once this node is going to
- // build a proposal and nominate. A skipped trigger must not credit the
- // time spent waiting for peers to the drift allowance.
- mHerderSCPDriver.recordNominationTrigger(ledgerSeqToTrigger);
+ // take part. A skipped trigger must not credit the time spent waiting for
+ // peers to the drift allowance.
+ mHerderSCPDriver.recordTrigger(ledgerSeqToTrigger);
+
+ if (!getSCP().isValidator())
+ {
+ CLOG_DEBUG(Herder, "Non-validating node, not starting the slot (SCP).");
+ return;
+ }
+
+ // There is no nomination: the leader of each round (a deterministic
+ // schedule seeded by the LCL hash) proposes straight into the ballot
+ // protocol. The value is only built if this node leads a round.
+ mHerderSCPDriver.startSlot(
+ ledgerSeqToTrigger,
+ [this, ledgerSeqToTrigger]() {
+ return makeProposalValue(ledgerSeqToTrigger);
+ },
+ /*constructValueNow=*/buildsLegacyTxSet(lcl.header.ledgerVersion));
+
+ // A follower validates the leader's set when it arrives, and most of its
+ // transactions are in this node's mempool too. The prewarm started at the
+ // previous slot's ballot verified the candidates known then; verify the
+ // current ones (mostly cached by now) while waiting for the leader's set,
+ // so that validation finds them in the cache. Skip it if this node already
+ // voted, i.e. validated the leader's value.
+ if (!buildsLegacyTxSet(lcl.header.ledgerVersion) &&
+ !mHerderSCPDriver.isLocalNodeRoundLeader(
+ ledgerSeqToTrigger,
+ std::max(1,
+ getSCP().getLeaderRound(ledgerSeqToTrigger))) &&
+ getSCP().getLatestMessagesSend(ledgerSeqToTrigger).empty())
+ {
+ prewarmSignatureCache(ledgerSeqToTrigger, {});
+ }
+}
+
+size_t
+HerderImpl::maxTxSetCandidates() const
+{
+ auto& lm = mApp.getLedgerManager();
+ size_t maxCandidates = lm.getLastMaxTxSetSizeOps();
+ if (lm.hasLastClosedSorobanNetworkConfig())
+ {
+ maxCandidates +=
+ lm.getLastClosedSorobanNetworkConfig().ledgerMaxTxCount();
+ }
+ return maxCandidates;
+}
+
+std::vector
+HerderImpl::pullTopTransactions(size_t count)
+{
+#ifdef BUILD_TESTS
+ if (mGetTopTransactionsForTesting)
+ {
+ return mGetTopTransactionsForTesting(count);
+ }
+#endif
+ return mApp.getOverlayManager().getTopTransactions(count);
+}
+
+std::vector
+HerderImpl::validTxTargets(uint32_t lclLedgerVersion) const
+{
+ // Selection only uses the best valid candidates up to each phase's
+ // capacity, and validating (mostly: verifying the signatures of) the rest
+ // of the pull is most of the build's cost. A build validates each phase in
+ // mempool order only until it holds 5/4 of its capacity in valid
+ // transactions (more if some are invalid, see
+ // TxSetUtils::trimInvalidPrefix). The slack covers candidates that
+ // selection skips: those that don't fit the remaining resources, and those
+ // the mempool ranks above ones with a higher inclusion fee (it orders by
+ // total fee per operation).
+ auto validationTarget = [](size_t capacity) {
+ return capacity + (capacity + 3) / 4;
+ };
+ std::vector targets{
+ validationTarget(mLedgerManager.getLastMaxTxSetSizeOps())};
+ if (protocolVersionStartsFrom(lclLedgerVersion, SOROBAN_PROTOCOL_VERSION))
+ {
+ targets.push_back(validationTarget(
+ mLedgerManager.hasLastClosedSorobanNetworkConfig()
+ ? mLedgerManager.getLastClosedSorobanNetworkConfig()
+ .ledgerMaxTxCount()
+ : 0));
+ }
+ return targets;
+}
+
+std::vector
+HerderImpl::pullTopTransactionsPayload(size_t count)
+{
+#ifdef BUILD_TESTS
+ if (mGetTopTransactionsForTesting)
+ {
+ // Encode the test mempool's answer as the overlay does.
+ auto envelopes = mGetTopTransactionsForTesting(count);
+ std::vector payload(4);
+ uint32_t const n = static_cast(envelopes.size());
+ std::memcpy(payload.data(), &n, 4);
+ for (auto const& env : envelopes)
+ {
+ auto const bytes = xdr::xdr_to_opaque(env);
+ uint32_t const len = static_cast(bytes.size());
+ auto const at = payload.size();
+ payload.resize(at + 4);
+ std::memcpy(payload.data() + at, &len, 4);
+ payload.insert(payload.end(), bytes.begin(), bytes.end());
+ }
+ return payload;
+ }
+#endif
+ return mApp.getOverlayManager().getTopTransactionsPayload(count);
+}
+
+void
+HerderImpl::prewarmNextLedgerSignatures(uint64_t slotIndex,
+ Hash const& txSetHash)
+{
+ // Once a slot starts balloting, its value is all but decided, and the next
+ // ledger's candidates are the mempool's top minus this value's
+ // transactions (the overlay drops them when the slot externalizes). Every
+ // validator verifies their signatures while the slot finishes balloting,
+ // externalizes and closes: the next leader builds from them, and the
+ // others validate the set it builds.
+ auto const& lcl = mLedgerManager.getLastClosedLedgerHeader();
+ if (!getSCP().isValidator() || !isTracking() ||
+ slotIndex != lcl.header.ledgerSeq + 1 ||
+ buildsLegacyTxSet(lcl.header.ledgerVersion))
+ {
+ return;
+ }
+ std::vector pending;
+ auto txSet = mPendingEnvelopes.getTxSet(txSetHash);
+ if (auto const* frame = std::get_if(&txSet);
+ frame && *frame)
+ {
+ pending = txCountPerPhase(**frame);
+ }
+ prewarmSignatureCache(static_cast(slotIndex + 1), pending);
+}
+
+void
+HerderImpl::prewarmSignatureCache(uint32_t ledgerSeq,
+ std::vector const& pendingPerPhase)
+{
+ ZoneScoped;
+ stopSignaturePrewarm();
+ // The build for `ledgerSeq` validates the first validTxTargets() of each
+ // phase of its pull (more only if some are invalid), and the leader's set
+ // is selected from them.
+ auto const& lcl = mLedgerManager.getLastClosedLedgerHeader();
+ auto windows = validTxTargets(lcl.header.ledgerVersion);
+ size_t pending = 0;
+ for (size_t i = 0; i < windows.size() && i < pendingPerPhase.size(); ++i)
+ {
+ windows[i] += pendingPerPhase[i];
+ pending += pendingPerPhase[i];
+ }
+ auto& metrics = mApp.getMetrics();
+ // Only the overlay round trip runs on the main thread; the tasks decode.
+ auto payload = [&]() {
+ auto timer =
+ metrics.NewTimer({"herder", "prewarm", "pull"}).TimeScope();
+ return std::make_shared const>(
+ pullTopTransactionsPayload(2 * maxTxSetCandidates() + pending));
+ }();
+ uint32_t count = 0;
+ if (payload->size() >= 4)
+ {
+ std::memcpy(&count, payload->data(), 4);
+ }
+ if (count == 0)
+ {
+ return;
+ }
+ auto prewarm = std::make_shared();
+ prewarm->ledgerSeq = ledgerSeq;
+ prewarm->start = std::chrono::steady_clock::now();
+ // The batch executor (validation) is idle until the next build or the
+ // leader's set; use as many background tasks as it would, but leave
+ // worker threads for long background jobs such as bucket merges.
+ 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));
+ prewarm->runningTasks = tasks;
+ mSignaturePrewarm = prewarm;
+ CLOG_DEBUG(Herder,
+ "Pre-verifying signatures for ledger {}: {} pulled, windows {} "
+ "classic / {} Soroban, {} tasks",
+ ledgerSeq, count, windows[0],
+ windows.size() > 1 ? windows[1] : 0, tasks);
+ for (size_t task = 0; task < tasks; ++task)
+ {
+ mApp.postOnBackgroundThread(
+ [prewarm, payload, windows, networkID = mApp.getNetworkID(), task,
+ tasks]() {
+ ZoneScopedN("signature prewarm");
+ if (!prewarm->stop.load(std::memory_order_relaxed))
+ {
+ std::call_once(prewarm->selected, [&]() {
+ prewarm->candidates = selectPrewarmCandidates(
+ OverlayIPC::parseTopTransactions(*payload),
+ windows);
+ });
+ auto const& candidates = prewarm->candidates;
+ for (size_t i = task; i < candidates.size(); i += tasks)
+ {
+ if (prewarm->stop.load(std::memory_order_relaxed))
+ {
+ break;
+ }
+ prewarm->verifiedSignatures +=
+ verifyMasterKeySignatures(networkID, candidates[i]);
+ ++prewarm->checkedTxs;
+ }
+ }
+ if (prewarm->runningTasks.fetch_sub(1) == 1)
+ {
+ // The last task: free the candidates here rather than on
+ // the main thread when the prewarm is stopped.
+ std::vector().swap(
+ prewarm->candidates);
+ prewarm->finishedAfterNs =
+ std::chrono::duration_cast(
+ std::chrono::steady_clock::now() - prewarm->start)
+ .count();
+ }
+ },
+ "Herder: signature prewarm");
+ }
+}
+
+void
+HerderImpl::stopSignaturePrewarm()
+{
+ if (!mSignaturePrewarm)
+ {
+ return;
+ }
+ auto prewarm = std::move(mSignaturePrewarm);
+ mSignaturePrewarm.reset();
+ prewarm->stop.store(true, std::memory_order_relaxed);
+ // Tasks still running stop after their current transaction; what they
+ // did so far is recorded.
+ auto const finishedAfterNs = prewarm->finishedAfterNs.load();
+ auto& metrics = mApp.getMetrics();
+ metrics.NewTimer({"herder", "prewarm", "duration"})
+ .Update(finishedAfterNs >= 0
+ ? std::chrono::nanoseconds(finishedAfterNs)
+ : std::chrono::duration_cast(
+ std::chrono::steady_clock::now() - prewarm->start));
+ metrics.NewMeter({"herder", "prewarm", "txs"}, "transaction")
+ .Mark(prewarm->checkedTxs.load());
+ metrics.NewMeter({"herder", "prewarm", "verified"}, "signature")
+ .Mark(prewarm->verifiedSignatures.load());
+ if (finishedAfterNs < 0)
+ {
+ metrics.NewMeter({"herder", "prewarm", "incomplete"}, "prewarm").Mark();
+ }
+}
+
+bool
+HerderImpl::buildsLegacyTxSet(uint32_t lclLedgerVersion)
+{
+ // Ledgers closing on a pre-Soroban LCL use legacy (non-generalized) tx
+ // sets, which the Rust overlay can neither fetch nor serve: a follower
+ // can only obtain the leader's set by building it. Every validator
+ // therefore constructs its value at the trigger for these ledgers, as
+ // before leader-only construction. With an empty mempool (genesis and
+ // the upgrade-only ledgers up to protocol 20) the set depends only on the
+ // previous ledger, so all validators build the same set as the leader.
+ return protocolVersionIsBefore(lclLedgerVersion, SOROBAN_PROTOCOL_VERSION);
+}
+
+ValueWrapperPtr
+HerderImpl::makeProposalValue(uint32_t ledgerSeqToTrigger)
+{
+ // A follower can be promoted long after the trigger. Recheck the ledger
+ // state and select a fresh close time when SCP actually needs our value.
+ // Herder tracking was checked at the trigger. Do not require it again:
+ // a node that lost sync while the slot stalled (e.g. waiting for a tx set
+ // it cannot fetch) must still propose once promoted, like a value built at
+ // the trigger would have been, or the slot can stall forever.
+ if (!mLedgerManager.isSynced() || mLedgerManager.isApplying())
+ {
+ return nullptr;
+ }
+ auto lcl = mLedgerManager.getLastClosedLedgerHeader();
+ if (ledgerSeqToTrigger != lcl.header.ledgerSeq + 1)
+ {
+ return nullptr;
+ }
+ auto nextCloseTime = std::max(
+ ConsensusTime::fromSystemTime(mApp.getClock().system_now(),
+ lcl.header.ledgerVersion),
+ getConsensusTime(lcl.header.scpValue).next(lcl.header.ledgerVersion));
+ if (ctValidityOffset(nextCloseTime) != std::chrono::milliseconds::zero())
+ {
+ return nullptr;
+ }
// Consume the private snapshot only for the ledger it was built against.
// Keep its close time: changing it would invalidate time-bound transactions
@@ -1985,7 +2511,7 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
if (!applicableProposedSet)
{
releaseAssert(!mApp.getConfig().FORCE_SCP);
- return;
+ return nullptr;
}
// New proposed tx set must be valid, so we explicitly populate tx set
@@ -1994,12 +2520,19 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
closeTimeOffset);
#ifdef BUILD_TESTS
- if (mNominatedTxSetOverride)
+ if (mProposedTxSetOverride)
{
- proposedSet = mNominatedTxSetOverride(lcl);
+ proposedSet = mProposedTxSetOverride(lcl);
CLOG_INFO(Herder, "Nominating overridden tx set {} for slot {}",
hexAbbrev(proposedSet->getContentsHash()),
lcl.header.ledgerSeq + 1);
+ // The overlay only cached the set built above; peers fetch this one.
+ if (proposedSet->isGeneralizedTxSet())
+ {
+ mApp.getOverlayManager().cacheTxSet(
+ proposedSet->getContentsHash(),
+ proposedSet->getGeneralizedXDR(), lcl.header.ledgerSeq + 1);
+ }
}
#endif
@@ -2014,20 +2547,6 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
mPendingEnvelopes.addTxSet(txSetHash, lcl.header.ledgerSeq + 1,
proposedSet);
- // Cache the TX set in Rust overlay so it can serve it to other peers.
- // When a peer receives an SCP message referencing this TX set hash,
- // they'll request it via TX set fetching, and Rust needs the XDR.
- // Note: Rust overlay only supports GeneralizedTransactionSet (protocol >=
- // 20)
- if (proposedSet->isGeneralizedTxSet())
- {
- GeneralizedTransactionSet xdrTxSet;
- proposedSet->toXDR(xdrTxSet);
- auto xdrBytes = xdr::xdr_to_opaque(xdrTxSet);
- mApp.getOverlayManager().cacheTxSet(txSetHash, xdrBytes,
- lcl.header.ledgerSeq + 1);
- }
-
lcl = mLedgerManager.getLastClosedLedgerHeader();
// use the slot index from ledger manager here as our vote is based off
// the last closed ledger stored in ledger manager
@@ -2039,7 +2558,7 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
// applying
if (ledgerSeqToTrigger != slotIndex || mLedgerManager.isApplying())
{
- return;
+ return nullptr;
}
auto newUpgrades = emptyUpgradeSteps;
@@ -2069,22 +2588,13 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
}
}
- getHerderSCPDriver().recordSCPEvent(slotIndex, true);
-
- // If we are not a validating node we stop here and don't start nomination
- if (!getSCP().isValidator())
- {
- CLOG_DEBUG(Herder, "Non-validating node, skipping nomination (SCP).");
- return;
- }
-
#ifdef BUILD_TESTS
- if (mApp.getConfig().TESTING_NOMINATE_RANDOM_VALUES &&
+ if (mApp.getConfig().TESTING_PROPOSE_RANDOM_VALUES &&
getHerderSCPDriver().protocolAllowsEmptyTxSetValues())
{
txSetHash = HashUtils::pseudoRandomForTesting();
CLOG_INFO(Herder,
- "TESTING_NOMINATE_RANDOM_VALUES: nominating slot {} "
+ "TESTING_PROPOSE_RANDOM_VALUES: proposing slot {} "
"with random tx-set hash {}",
slotIndex, hexAbbrev(txSetHash));
}
@@ -2092,8 +2602,7 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
StellarValue newProposedValue = makeStellarValue(
txSetHash, nextCloseTime, newUpgrades, mApp.getConfig().NODE_SEED);
- mHerderSCPDriver.nominate(slotIndex, newProposedValue, proposedSet,
- lcl.header.scpValue);
+ return mHerderSCPDriver.wrapStellarValue(newProposedValue);
}
void
@@ -3112,9 +3621,9 @@ HerderImpl::makeStellarValue(Hash const& txSetHash, ConsensusTime closeTime,
}
bool
-HerderImpl::isNewerNominationOrBallotSt(SCPStatement const& oldSt,
- SCPStatement const& newSt)
+HerderImpl::isNewerStatement(SCPStatement const& oldSt,
+ SCPStatement const& newSt)
{
- return getSCP().isNewerNominationOrBallotSt(oldSt, newSt);
+ return getSCP().isNewerStatement(oldSt, newSt);
}
}
diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h
index bebbce06d6..f46ad8bf7d 100644
--- a/src/herder/HerderImpl.h
+++ b/src/herder/HerderImpl.h
@@ -15,8 +15,11 @@
#include "util/Timer.h"
#include "util/UnorderedMap.h"
#include "util/XDROperators.h"
+#include
+#include
#include
#include
+#include
#include
#include
@@ -135,7 +138,7 @@ class HerderImpl : public Herder
// still run through their production paths.
std::function(size_t)>
mGetTopTransactionsForTesting;
- friend class EarlyNominationTestAccess;
+ friend class LeaderBuildTestAccess;
std::optional mMaxClassicTxSize;
std::optional mMaxTxSizeOverride;
@@ -159,7 +162,7 @@ class HerderImpl : public Herder
// When set, this node nominates the tx set returned by this function
// (given the LCL) instead of the one it builds.
std::function
- mNominatedTxSetOverride;
+ mProposedTxSetOverride;
#endif
std::vector getSCPStateForPeer(uint32 ledgerSeq) override;
@@ -192,8 +195,8 @@ class HerderImpl : public Herder
uint32_t getMinLedgerSeqToRemember() const override;
- bool isNewerNominationOrBallotSt(SCPStatement const& oldSt,
- SCPStatement const& newSt) override;
+ bool isNewerStatement(SCPStatement const& oldSt,
+ SCPStatement const& newSt) override;
uint32_t getMostRecentCheckpointSeq() override;
@@ -269,7 +272,56 @@ class HerderImpl : public Herder
PreparedTxSet buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime);
void prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime);
void discardPreparedTxSet();
+ ValueWrapperPtr makeProposalValue(uint32_t ledgerSeq);
+ // Whether a ledger closing on an LCL with this protocol uses a legacy
+ // (non-generalized) tx set, which every validator builds locally.
+ static bool buildsLegacyTxSet(uint32_t lclLedgerVersion);
+ // Upper bound on the transactions a tx set for the next ledger can hold.
+ size_t maxTxSetCandidates() const;
+ // How many valid candidates a build validates per phase (classic, then
+ // Soroban if the protocol supports it): 5/4 of the phase's capacity.
+ std::vector validTxTargets(uint32_t lclLedgerVersion) const;
+ std::vector pullTopTransactions(size_t count);
+ // The same pull as encoded by the overlay (see OverlayIPC), to decode
+ // off the main thread.
+ std::vector pullTopTransactionsPayload(size_t count);
+
+ // A signature prewarm: background tasks verifying the signatures of the
+ // candidates that the build of `ledgerSeq` validates first (and that the
+ // leader's set for it is selected from), warming the signature cache for
+ // that build and for validating the leader's set.
+ struct SignaturePrewarm
+ {
+ uint32_t ledgerSeq{0};
+ 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;
+ std::atomic checkedTxs{0};
+ std::atomic verifiedSignatures{0};
+ std::atomic runningTasks{0};
+ // Nanoseconds from `start` until the last task finished, or -1.
+ std::atomic finishedAfterNs{-1};
+ };
+ // Starts a prewarm for ledger `ledgerSeq`, replacing any running one.
+ // `pendingPerPhase` counts the transactions per phase that are still in
+ // the mempool but not candidates for `ledgerSeq` (those of the value being
+ // externalized): the prewarm looks past that many more.
+ void prewarmSignatureCache(uint32_t ledgerSeq,
+ std::vector const& pendingPerPhase);
+ std::shared_ptr mSignaturePrewarm;
+ public:
+ // Called when slot `slotIndex` starts its ballot protocol with a value
+ // whose tx set is `txSetHash`: prewarms the next ledger's candidates while
+ // this slot finishes balloting, externalizes and closes.
+ void prewarmNextLedgerSignatures(uint64_t slotIndex, Hash const& txSetHash);
+ // Stops a running signature prewarm and records its metrics: called when
+ // validation or a local build is about to verify the signatures itself.
+ void stopSignaturePrewarm();
+
+ private:
// Compute the trigger-timer anchor point using the local node's
// prepare-start timestamp for the previous slot. Returns a pessimistic
// estimate (now - expectedClose) if no prepare-start is recorded.
diff --git a/src/herder/HerderSCPDriver.cpp b/src/herder/HerderSCPDriver.cpp
index d487c7d0bd..4a04b8217f 100644
--- a/src/herder/HerderSCPDriver.cpp
+++ b/src/herder/HerderSCPDriver.cpp
@@ -8,6 +8,7 @@
#include "crypto/SHA.h"
#include "crypto/SecretKey.h"
#include "herder/HerderImpl.h"
+#include "herder/LeaderSchedule.h"
#include "herder/LedgerCloseData.h"
#include "herder/PendingEnvelopes.h"
#include "ledger/LedgerHeaderUtils.h"
@@ -54,10 +55,6 @@ HerderSCPDriver::SCPMetrics::SCPMetrics(Application& app)
, mValueValid(app.getMetrics().NewMeter({"scp", "value", "valid"}, "value"))
, mValueInvalid(
app.getMetrics().NewMeter({"scp", "value", "invalid"}, "value"))
- , mCombinedCandidates(app.getMetrics().NewMeter(
- {"scp", "nomination", "combinecandidates"}, "value"))
- , mNominateToPrepare(
- app.getMetrics().NewTimer({"scp", "timing", "nominated"}))
, mPrepareToExternalize(
app.getMetrics().NewTimer({"scp", "timing", "externalized"}))
, mTriggerToBallot(
@@ -88,13 +85,14 @@ HerderSCPDriver::HerderSCPDriver(Application& app, HerderImpl& herder,
, mSCP{*this, mApp.getConfig().NODE_SEED.getPublicKey(),
mApp.getConfig().NODE_IS_VALIDATOR, mApp.getConfig().QUORUM_SET}
, mSCPMetrics{mApp}
- , mNominateTimeout{mApp.getMetrics().NewHistogram(
- {"scp", "timeout", "nominate"})}
+ , mLeaderTimeout{mApp.getMetrics().NewHistogram(
+ {"scp", "timeout", "leader"})}
+ , mLeaderRound{mApp.getMetrics().NewHistogram({"scp", "leader", "round"})}
, mPrepareTimeout{mApp.getMetrics().NewHistogram(
{"scp", "timeout", "prepare"})}
, mUniqueValues{mApp.getMetrics().NewHistogram(
{"scp", "slot", "values-referenced"})}
- , mLedgerSeqNominating(0)
+ , mLedgerSeqProposing(0)
, mTxSetValidCache(TXSETVALID_CACHE_SIZE)
{
}
@@ -406,7 +404,7 @@ HerderSCPDriver::validatePastOrFutureValue(
SCPDriver::ValidationLevel
HerderSCPDriver::validateValueAgainstLocalState(uint64_t slotIndex,
StellarValue const& b,
- bool nomination) const
+ bool proposal) const
{
ZoneScoped;
releaseAssert(threadIsMain());
@@ -437,13 +435,13 @@ HerderSCPDriver::validateValueAgainstLocalState(uint64_t slotIndex,
return SCPDriver::kInvalidValue;
}
- if (nomination)
+ if (proposal)
{
- // Empty-tx-set values should only appear in balloting, and so
- // are considered invalid during nomination.
+ // Empty-tx-set values only come out of balloting (a node
+ // gives up on a download): nobody proposes one.
CLOG_DEBUG(Herder,
"HerderSCPDriver::validateValue i: {} rejecting "
- "empty-tx-set value during nomination",
+ "empty-tx-set value as a proposal",
slotIndex);
return SCPDriver::kInvalidValue;
}
@@ -609,14 +607,14 @@ HerderSCPDriver::deserializeAndValidateStellarValue(uint64_t slotIndex,
}
void
-HerderSCPDriver::extractValidUpgrades(StellarValue& sv, bool nomination) const
+HerderSCPDriver::extractValidUpgrades(StellarValue& sv, bool proposal) const
{
LedgerUpgradeType lastUpgradeType = LEDGER_UPGRADE_VERSION;
LedgerUpgradeType thisUpgradeType;
bool first = true;
for (auto it = sv.upgrades.begin(); it != sv.upgrades.end();)
{
- if (!mUpgrades.isValid(*it, thisUpgradeType, nomination, mApp))
+ if (!mUpgrades.isValid(*it, thisUpgradeType, proposal, mApp))
{
it = sv.upgrades.erase(it);
}
@@ -635,7 +633,7 @@ HerderSCPDriver::extractValidUpgrades(StellarValue& sv, bool nomination) const
SCPDriver::ValidationLevel
HerderSCPDriver::validateValue(uint64_t slotIndex, Value const& value,
- bool nomination) const
+ bool proposal) const
{
ZoneScoped;
releaseAssert(threadIsMain());
@@ -648,11 +646,11 @@ HerderSCPDriver::validateValue(uint64_t slotIndex, Value const& value,
}
SCPDriver::ValidationLevel res =
- validateValueAgainstLocalState(slotIndex, b, nomination);
+ validateValueAgainstLocalState(slotIndex, b, proposal);
if (res != SCPDriver::kInvalidValue)
{
auto origSize = b.upgrades.size();
- extractValidUpgrades(b, nomination);
+ extractValidUpgrades(b, proposal);
if (b.upgrades.size() != origSize)
{
CLOG_TRACE(Herder,
@@ -674,27 +672,6 @@ HerderSCPDriver::validateValue(uint64_t slotIndex, Value const& value,
return res;
}
-ValueWrapperPtr
-HerderSCPDriver::extractValidValue(uint64_t slotIndex, Value const& value)
-{
- ZoneScoped;
- StellarValue b;
- if (!deserializeAndValidateStellarValue(slotIndex, value, b))
- {
- return nullptr;
- }
-
- ValueWrapperPtr res;
- if (validateValueAgainstLocalState(slotIndex, b, true) >=
- SCPDriver::kStructurallyValidValue)
- {
- extractValidUpgrades(b, true);
- res = wrapStellarValue(b);
- }
-
- return res;
-}
-
// value marshaling
std::string
@@ -776,18 +753,6 @@ void
HerderSCPDriver::timerCallbackWrapper(uint64_t slotIndex, int timerID,
std::function cb)
{
-#ifdef BUILD_TESTS
- if (timerID == Slot::NOMINATION_EMIT_TIMER)
- {
- if (!mHerder.isTracking() ||
- mHerder.nextConsensusLedgerIndex() == slotIndex)
- {
- cb();
- }
- return;
- }
-#endif
-
// reschedule timers for future slots when tracking
if (mHerder.isTracking() && mHerder.nextConsensusLedgerIndex() != slotIndex)
{
@@ -811,11 +776,8 @@ HerderSCPDriver::timerCallbackWrapper(uint64_t slotIndex, int timerID,
}
else
{
- if (!SCPTiming.mPrepareStart)
- {
- // Timeout happened between nominate and first prepare
- ++SCPTiming.mNominationTimeoutCount;
- }
+ // Timed out waiting for the round leader's proposal
+ ++SCPTiming.mLeaderTimeoutCount;
}
}
@@ -876,7 +838,7 @@ HerderSCPDriver::stopTimer(uint64 slotIndex, int timerID)
static uint32_t const MAX_TIMEOUT_MS = (30 * 60) * 1000;
std::chrono::milliseconds
-HerderSCPDriver::computeTimeout(uint32 roundNumber, bool isNomination)
+HerderSCPDriver::computeTimeout(uint32 roundNumber)
{
releaseAssertOrThrow(roundNumber > 0);
@@ -891,18 +853,8 @@ HerderSCPDriver::computeTimeout(uint32 roundNumber, bool isNomination)
{
auto const& networkConfig =
mLedgerManager.getLastClosedSorobanNetworkConfig();
- if (isNomination)
- {
- initialTimeoutMS =
- networkConfig.nominationTimeoutInitialMilliseconds();
- incrementMS =
- networkConfig.nominationTimeoutIncrementMilliseconds();
- }
- else
- {
- initialTimeoutMS = networkConfig.ballotTimeoutInitialMilliseconds();
- incrementMS = networkConfig.ballotTimeoutIncrementMilliseconds();
- }
+ initialTimeoutMS = networkConfig.ballotTimeoutInitialMilliseconds();
+ incrementMS = networkConfig.ballotTimeoutIncrementMilliseconds();
}
auto timeoutMS = initialTimeoutMS + (roundNumber - 1) * incrementMS;
@@ -913,284 +865,6 @@ HerderSCPDriver::computeTimeout(uint32 roundNumber, bool isNomination)
return std::chrono::milliseconds(timeoutMS);
}
-#ifdef BUILD_TESTS
-std::chrono::milliseconds
-HerderSCPDriver::getNominationEmitDelayForTesting() const
-{
- return mApp.getConfig().ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING;
-}
-#endif
-
-// returns true if l < r
-// lh, rh are the hashes of l,h
-static bool
-compareTxSets(ApplicableTxSetFrameConstPtr const& l,
- ApplicableTxSetFrameConstPtr const& r, Hash const& lh,
- Hash const& rh, std::optional lEncodedSize,
- std::optional rEncodedSize, LedgerHeader const& header,
- Hash const& s)
-{
- if (!l && !r)
- {
- // Do not have either tx set. Compare hashes
- return lessThanXored(lh, rh, s);
- }
-
- if (!l || !r)
- {
- // If one exists, choose it
- return !l;
- }
-
- auto lSize = l->size(header);
- auto rSize = r->size(header);
- if (lSize != rSize)
- {
- return lSize < rSize;
- }
- if (protocolVersionStartsFrom(header.ledgerVersion,
- SOROBAN_PROTOCOL_VERSION))
- {
- auto lBids = l->getTotalInclusionFees();
- auto rBids = r->getTotalInclusionFees();
- if (lBids != rBids)
- {
- return lBids < rBids;
- }
- }
- if (protocolVersionStartsFrom(header.ledgerVersion, ProtocolVersion::V_11))
- {
- auto lFee = l->getTotalFees(header);
- auto rFee = r->getTotalFees(header);
- if (lFee != rFee)
- {
- return lFee < rFee;
- }
- }
- if (protocolVersionStartsFrom(header.ledgerVersion,
- SOROBAN_PROTOCOL_VERSION))
- {
- if (lEncodedSize.value() != rEncodedSize.value())
- {
- // Look for the smallest encoded size.
- return lEncodedSize.value() > rEncodedSize.value();
- }
- }
- return lessThanXored(lh, rh, s);
-}
-
-ValueWrapperPtr
-HerderSCPDriver::combineCandidates(uint64_t slotIndex,
- ValueWrapperPtrSet const& candidates)
-{
- ZoneScoped;
- CLOG_DEBUG(Herder, "Combining {} candidates", candidates.size());
- mSCPMetrics.mCombinedCandidates.Mark(candidates.size());
-
- std::map upgrades;
-
- std::set aggSet;
-
- releaseAssert(!mLedgerManager.isApplying());
- releaseAssert(threadIsMain());
- auto const& lcl = mLedgerManager.getLastClosedLedgerHeader();
-
- Hash candidatesHash;
-
- std::vector candidateValues;
-
- for (auto const& c : candidates)
- {
- candidateValues.emplace_back();
- StellarValue& sv = candidateValues.back();
- Value const& val = c->getValue();
- uint256 const valHash = sha256(val);
-
- if (!toStellarValue(val, sv))
- {
- throw std::runtime_error(fmt::format(
- "HerderSCPDriver::combineCandidates: cannot parse candidate "
- "value with hash {}",
- binToHex(valHash)));
- }
-
- candidatesHash ^= valHash;
-
- for (auto const& upgrade : sv.upgrades)
- {
- LedgerUpgrade lupgrade;
- try
- {
- xdr::xdr_from_opaque(upgrade, lupgrade);
- }
- catch (...)
- {
- throw std::runtime_error(
- fmt::format("HerderSCPDriver::combineCandidates: cannot "
- "parse upgrade in candidate with hash {}",
- binToHex(valHash)));
- }
- auto it = upgrades.find(lupgrade.type());
- if (it == upgrades.end())
- {
- upgrades.emplace(std::make_pair(lupgrade.type(), lupgrade));
- }
- else
- {
- LedgerUpgrade& clUpgrade = it->second;
- switch (lupgrade.type())
- {
- case LEDGER_UPGRADE_VERSION:
- // pick the highest version
- clUpgrade.newLedgerVersion() =
- std::max(clUpgrade.newLedgerVersion(),
- lupgrade.newLedgerVersion());
- break;
- case LEDGER_UPGRADE_BASE_FEE:
- // take the max fee
- clUpgrade.newBaseFee() =
- std::max(clUpgrade.newBaseFee(), lupgrade.newBaseFee());
- break;
- case LEDGER_UPGRADE_MAX_TX_SET_SIZE:
- // take the max tx set size
- clUpgrade.newMaxTxSetSize() =
- std::max(clUpgrade.newMaxTxSetSize(),
- lupgrade.newMaxTxSetSize());
- break;
- case LEDGER_UPGRADE_BASE_RESERVE:
- // take the max base reserve
- clUpgrade.newBaseReserve() = std::max(
- clUpgrade.newBaseReserve(), lupgrade.newBaseReserve());
- break;
- case LEDGER_UPGRADE_FLAGS:
- clUpgrade.newFlags() =
- std::max(clUpgrade.newFlags(), lupgrade.newFlags());
- break;
- case LEDGER_UPGRADE_CONFIG:
- if (clUpgrade.newConfig().contractID <
- lupgrade.newConfig().contractID)
- {
- clUpgrade.newConfig() = lupgrade.newConfig();
- }
- else if (clUpgrade.newConfig().contractID ==
- lupgrade.newConfig().contractID &&
- clUpgrade.newConfig().contentHash <
- lupgrade.newConfig().contentHash)
- {
- clUpgrade.newConfig() = lupgrade.newConfig();
- }
- break;
- case LEDGER_UPGRADE_MAX_SOROBAN_TX_SET_SIZE:
- clUpgrade.newMaxSorobanTxSetSize() =
- std::max(clUpgrade.newMaxSorobanTxSetSize(),
- lupgrade.newMaxSorobanTxSetSize());
- break;
- default:
- // should never get there with values that are not valid
- throw std::runtime_error("invalid upgrade step");
- }
- }
- }
- }
-
- StellarValue comp;
- // take the txSet with the biggest size, highest xored hash that we have
- {
- auto highest = candidateValues.cend();
- TxSetXDRFrameConstPtr highestTxSet;
- ApplicableTxSetFrameConstPtr highestApplicableTxSet;
- for (auto it = candidateValues.cbegin(); it != candidateValues.cend();
- ++it)
- {
- auto const& sv = *it;
- TxSetXDRFrameConstPtr cTxSet;
- auto const cTxSetResult = mPendingEnvelopes.getTxSet(sv.txSetHash);
- if (auto const* ptr =
- std::get_if(&cTxSetResult))
- {
- cTxSet = *ptr;
- }
- // else: EmptyTxSet -> cTxSet stays null, handled by existing
-
- // Prefer applicable tx sets. `compareTxSets` down-ranks
- // non-applicable ones. Without CAP-0083 every candidate here is
- // applicable (a mismatched one can't be ratified), so the
- // non-applicable case doesn't arise. Under CAP-0083 a
- // non-applicable candidate may be selected and is replaced with an
- // empty-tx-set value during balloting.
- ApplicableTxSetFrameConstPtr cApplicableTxSet = nullptr;
- if (cTxSet && cTxSet->previousLedgerHash() == lcl.hash)
- {
- cApplicableTxSet = cTxSet->prepareForApply(mApp, lcl.header);
- }
-
- if (highest == candidateValues.cend() ||
- compareTxSets(highestApplicableTxSet, cApplicableTxSet,
- highest->txSetHash, sv.txSetHash,
- highestTxSet ? std::make_optional(
- highestTxSet->encodedSize())
- : std::nullopt,
- cTxSet ? std::make_optional(cTxSet->encodedSize())
- : std::nullopt,
- lcl.header, candidatesHash))
- {
- highest = it;
- highestTxSet = cTxSet;
- highestApplicableTxSet = std::move(cApplicableTxSet);
- }
- }
- if (highest == candidateValues.cend())
- {
- throw std::runtime_error(
- "No highest candidate transaction set found");
- }
- comp = *highest;
- }
- comp.upgrades.clear();
- for (auto const& upgrade : upgrades)
- {
- Value v(xdr::xdr_to_opaque(upgrade.second));
- comp.upgrades.emplace_back(v.begin(), v.end());
- }
-
- auto res = wrapStellarValue(comp);
- return res;
-}
-
-bool
-HerderSCPDriver::hasUpgrades(Value const& v)
-{
- StellarValue sv;
- if (!toStellarValue(v, sv))
- {
- return false;
- }
- return !sv.upgrades.empty();
-}
-
-ValueWrapperPtr
-HerderSCPDriver::stripAllUpgrades(Value const& v)
-{
- StellarValue sv;
- if (!toStellarValue(v, sv))
- {
- return nullptr;
- }
-
- // Remove all upgrades
- sv.upgrades.clear();
-
- // Serialize back to Value
- return wrapStellarValue(sv);
-}
-
-uint32_t
-HerderSCPDriver::getUpgradeNominationTimeoutLimit() const
-{
- return mUpgrades.getParameters().mNominationTimeoutLimit.value_or(
- std::numeric_limits::max());
-}
-
std::optional
HerderSCPDriver::getTxSetDownloadWaitTime(Value const& v) const
{
@@ -1253,16 +927,11 @@ HerderSCPDriver::valueExternalized(uint64_t slotIndex, Value const& value)
logQuorumInformationAndUpdateMetrics(slotIndex - 2);
}
- if (mCurrentValue)
- {
- // stop nomination
- // this may or may not be the ledger that is currently externalizing
- // in both cases, we want to stop nomination as:
- // either we're closing the current ledger (typical case)
- // or we're going to trigger catchup from history
- mSCP.stopNomination(mLedgerSeqNominating);
- mCurrentValue.reset();
- }
+ // this may or may not be the ledger the local node started: either
+ // way its own proposal is obsolete (the ledger is closing, or catchup
+ // is about to trigger)
+ mCurrentValue.reset();
+ mLedgerSeqProposing = 0;
if (!mHerder.isTracking())
{
@@ -1337,24 +1006,106 @@ HerderSCPDriver::logQuorumInformationAndUpdateMetrics(uint64_t index)
}
void
-HerderSCPDriver::nominate(uint64_t slotIndex, StellarValue const& value,
- TxSetXDRFrameConstPtr proposedSet,
- StellarValue const& previousValue)
+HerderSCPDriver::startSlot(uint64_t slotIndex, ProposalValueSupplier makeValue,
+ bool constructValueNow)
{
ZoneScoped;
- mCurrentValue = wrapStellarValue(value);
- mLedgerSeqNominating = static_cast(slotIndex);
+ if (mLedgerSeqProposing != slotIndex)
+ {
+ mCurrentValue.reset();
+ }
+ mLedgerSeqProposing = static_cast(slotIndex);
+ if (constructValueNow && !mCurrentValue)
+ {
+ auto value = makeValue();
+ // Publishing the tx set may externalize this slot. Do not restart it.
+ if (mLedgerSeqProposing != slotIndex)
+ {
+ return;
+ }
+ // A null value is retried by the supplier below if this node becomes
+ // a leader.
+ mCurrentValue = std::move(value);
+ }
+ mSCP.startSlot(slotIndex,
+ [this, slotIndex,
+ makeValue = std::move(makeValue)]() -> ValueWrapperPtr {
+ if (mLedgerSeqProposing != slotIndex)
+ {
+ return nullptr;
+ }
+ if (!mCurrentValue)
+ {
+ auto value = makeValue();
+ // Publishing the tx set may externalize a pending
+ // value.
+ if (mLedgerSeqProposing != slotIndex)
+ {
+ return nullptr;
+ }
+ mCurrentValue = std::move(value);
+ }
+ return mCurrentValue;
+ });
+}
+
+std::vector const&
+HerderSCPDriver::getLeaderSchedule(uint64_t slotIndex)
+{
+ static std::vector const empty;
+ releaseAssert(threadIsMain());
+ 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.
+ return empty;
+ }
+ // Force a pending rebuild of the transitive quorum, if any
+ mPendingEnvelopes.isNodeDefinitelyInQuorum(mSCP.getLocalNodeID());
+ auto const& quorum = mPendingEnvelopes.getCurrentlyTrackedQuorum();
+ auto& cache = mLeaderSchedule;
+ if (cache.mSlotIndex != slotIndex || cache.mSeed != lcl.hash ||
+ cache.mQuorumSize != quorum.size() ||
+ cache.mQuorumRebuildCount != mPendingEnvelopes.getQuorumRebuildCount())
+ {
+ std::vector nodes;
+ nodes.reserve(quorum.size());
+ for (auto const& n : quorum)
+ {
+ nodes.emplace_back(n.first);
+ }
+ cache.mSchedule = computeLeaderSchedule(lcl.hash, std::move(nodes));
+ cache.mSlotIndex = slotIndex;
+ cache.mSeed = lcl.hash;
+ cache.mQuorumSize = quorum.size();
+ cache.mQuorumRebuildCount = mPendingEnvelopes.getQuorumRebuildCount();
+ }
+ return cache.mSchedule;
+}
- auto valueHash = xdrSha256(mCurrentValue->getValue());
- CLOG_DEBUG(Herder,
- "HerderSCPDriver::triggerNextLedger txSet.size: {} "
- "previousLedgerHash: {} value: {} slot: {}",
- proposedSet->sizeTxTotal(),
- hexAbbrev(proposedSet->previousLedgerHash()),
- hexAbbrev(valueHash), slotIndex);
+std::optional
+HerderSCPDriver::getRoundLeader(uint64 slotIndex, uint32 round)
+{
+ auto const& schedule = getLeaderSchedule(slotIndex);
+ if (schedule.empty() || round == 0)
+ {
+ return std::nullopt;
+ }
+ return schedule[(round - 1) % schedule.size()];
+}
+
+bool
+HerderSCPDriver::isProposalPending(uint64 slotIndex, NodeID const& leader) const
+{
+ return mPendingEnvelopes.hasPendingEnvelopeFrom(slotIndex, leader);
+}
- auto prevValue = xdr::xdr_to_opaque(previousValue);
- mSCP.nominate(slotIndex, mCurrentValue, prevValue);
+bool
+HerderSCPDriver::isLocalNodeRoundLeader(uint64_t slotIndex, uint32_t round)
+{
+ auto leader = getRoundLeader(slotIndex, round);
+ return leader && *leader == mSCP.getLocalNodeID();
}
SCPQuorumSetPtr
@@ -1404,22 +1155,55 @@ HerderSCPDriver::measureAndRecordBallotBlockedOnTxSet(uint64_t slotIndex,
}
void
-HerderSCPDriver::nominatingValue(uint64_t slotIndex, Value const& value)
+HerderSCPDriver::proposedValue(uint64_t slotIndex, uint32 round,
+ Value const& value)
{
- CLOG_DEBUG(Herder, "nominatingValue i:{} v: {}", slotIndex,
+ CLOG_DEBUG(Herder, "proposedValue i:{} round:{} v: {}", slotIndex, round,
getValueString(value));
}
void
-HerderSCPDriver::updatedCandidateValue(uint64_t slotIndex, Value const& value)
+HerderSCPDriver::enteredRound(uint64_t slotIndex, uint32 round, bool timedOut)
{
+ auto it = mSCPExecutionTimes.find(slotIndex);
+ if (it != mSCPExecutionTimes.end())
+ {
+ it->second.mMaxRound = std::max(it->second.mMaxRound, round);
+ }
+ if (round > 1)
+ {
+ // Only read the cached schedule here: this is called from inside SCP
+ // state changes (including state restore), where a quorum rebuild
+ // (database reads) does not belong.
+ auto const& cache = mLeaderSchedule;
+ std::string leader = "unknown";
+ if (cache.mSlotIndex == slotIndex && !cache.mSchedule.empty())
+ {
+ leader = toShortString(
+ cache.mSchedule[(round - 2) % cache.mSchedule.size()]);
+ }
+ CLOG_INFO(Herder, "Slot {} moved to round {}{} (round {} leader: {})",
+ slotIndex, round, timedOut ? " after a timeout" : "",
+ round - 1, leader);
+ }
}
void
HerderSCPDriver::startedBallotProtocol(uint64_t slotIndex,
SCPBallot const& ballot)
{
- recordSCPEvent(slotIndex, false);
+ recordBallotStart(slotIndex);
+ // Prewarm the next ledger's signatures, after SCP has finished processing
+ // (and emitting) this step: the mempool pull waits for the overlay.
+ StellarValue sv;
+ if (toStellarValue(ballot.value, sv))
+ {
+ mApp.postOnMainThread(
+ [this, slotIndex, txSetHash = sv.txSetHash]() {
+ mHerder.prewarmNextLedgerSignatures(slotIndex, txSetHash);
+ },
+ "Herder: prewarm next ledger signatures");
+ }
}
void
HerderSCPDriver::acceptedBallotPrepared(uint64_t slotIndex,
@@ -1521,40 +1305,34 @@ HerderSCPDriver::getExternalizeLag(NodeID const& id) const
}
void
-HerderSCPDriver::recordNominationTrigger(uint64_t slotIndex)
+HerderSCPDriver::recordTrigger(uint64_t slotIndex)
{
auto& timing = mSCPExecutionTimes[slotIndex];
- if (!timing.mTriggerStart && !timing.mNominationStart)
+ if (!timing.mTriggerStart)
{
timing.mTriggerStart = mApp.getClock().now();
+ if (timing.mPrepareStart)
+ {
+ // The node already voted for the round leader's proposal before
+ // its own trigger fired.
+ mSCPMetrics.mTriggerToBallot.Update(std::chrono::nanoseconds(0));
+ }
}
}
void
-HerderSCPDriver::recordSCPEvent(uint64_t slotIndex, bool isNomination)
+HerderSCPDriver::recordBallotStart(uint64_t slotIndex)
{
-
auto& timing = mSCPExecutionTimes[slotIndex];
VirtualClock::time_point start = mApp.getClock().now();
- if (isNomination)
- {
- if (!timing.mNominationStart)
- {
- timing.mNominationStart = start;
- }
- }
- else
+ // The first entry into ballot ends the trigger-to-ballot interval
+ if (!timing.mPrepareStart && timing.mTriggerStart &&
+ start >= *timing.mTriggerStart)
{
- // The first entry into ballot ends the trigger-to-ballot interval
- if (!timing.mPrepareStart && timing.mTriggerStart &&
- start >= *timing.mTriggerStart)
- {
- mSCPMetrics.mTriggerToBallot.Update(start - *timing.mTriggerStart);
- }
- timing.mPrepareStart =
- std::make_optional(start);
+ mSCPMetrics.mTriggerToBallot.Update(start - *timing.mTriggerStart);
}
+ timing.mPrepareStart = std::make_optional(start);
}
void
@@ -1647,33 +1425,20 @@ HerderSCPDriver::recordSCPExecutionMetrics(uint64_t slotIndex)
auto& SCPTiming = SCPTimingIt->second;
- mNominateTimeout.Update(SCPTiming.mNominationTimeoutCount);
+ mLeaderTimeout.Update(SCPTiming.mLeaderTimeoutCount);
mPrepareTimeout.Update(SCPTiming.mPrepareTimeoutCount);
-
- if (SCPTiming.mNominationTimeoutCount > 0)
+ if (SCPTiming.mMaxRound > 0)
{
- auto const leaders = getSCP().getNominationLeaders(slotIndex);
- std::string leaderStr;
- for (auto const& leader : leaders)
- {
- if (!leaderStr.empty())
- {
- leaderStr += ", ";
- }
- leaderStr += toShortString(leader);
- }
- CLOG_INFO(Herder,
- "Nomination for slot {} timed out {} time(s) with "
- "the following round leaders: [{}]",
- slotIndex, SCPTiming.mNominationTimeoutCount, leaderStr);
+ mLeaderRound.Update(SCPTiming.mMaxRound);
}
- // Compute nomination time
- if (SCPTiming.mNominationStart && SCPTiming.mPrepareStart)
+ if (SCPTiming.mLeaderTimeoutCount > 0)
{
- recordLogTiming(*SCPTiming.mNominationStart, *SCPTiming.mPrepareStart,
- mSCPMetrics.mNominateToPrepare, "Nominate", threshold,
- slotIndex);
+ CLOG_INFO(Herder,
+ "Slot {} timed out {} time(s) waiting for a round leader "
+ "(externalized in round {})",
+ slotIndex, SCPTiming.mLeaderTimeoutCount,
+ SCPTiming.mMaxRound);
}
// Compute prepare time
@@ -1931,6 +1696,12 @@ HerderSCPDriver::checkAndCacheTxSetValid(TxSetXDRFrame const& txSet,
if (pRes == nullptr)
{
ZoneNamedN(txSetValidityMissZone, "txset validity cache miss", true);
+ // Validation verifies the remaining signatures itself, on all cores;
+ // a concurrent signature prewarm would only compete with it.
+ mHerder.stopSignaturePrewarm();
+ uint64_t sigCacheHits = 0;
+ uint64_t sigCacheMisses = 0;
+ PubKeyUtils::getVerifySigCacheCounts(sigCacheHits, sigCacheMisses);
auto validationTime = mSCPMetrics.mTxSetValidation.TimeScope();
// The invariant here is that we only validate tx sets nominated
@@ -1957,6 +1728,8 @@ HerderSCPDriver::checkAndCacheTxSetValid(TxSetXDRFrame const& txSet,
res = applicableTxSet->checkValid(mApp, closeTimeOffset.seconds(),
closeTimeOffset.seconds());
}
+ markSignatureCacheLookups(mApp, "validate", sigCacheHits,
+ sigCacheMisses);
mTxSetValidCache.put(key, res);
return res;
@@ -1979,81 +1752,13 @@ HerderSCPDriver::TxSetValidityKeyHash::operator()(
return res;
}
-uint64
-HerderSCPDriver::getNodeWeight(NodeID const& nodeID, SCPQuorumSet const& qset,
- bool const isLocalNode) const
-{
- releaseAssert(!mLedgerManager.isApplying());
- Config const& cfg = mApp.getConfig();
- bool const unsupportedProtocol = protocolVersionIsBefore(
- mApp.getLedgerManager()
- .getLastClosedLedgerHeader()
- .header.ledgerVersion,
- APPLICATION_SPECIFIC_NOMINATION_LEADER_ELECTION_PROTOCOL_VERSION);
- if (unsupportedProtocol || !cfg.VALIDATOR_WEIGHT_CONFIG.has_value() ||
- cfg.FORCE_OLD_STYLE_LEADER_ELECTION)
- {
- // Fall back on old weight algorithm if any of the following are true:
- // 1. The network has not yet upgraded to
- // APPLICATION_SPECIFIC_NOMINATION_LEADER_ELECTION_PROTOCOL_VERSION,
- // 2. The node is using manual quorum set configuration, or
- // 3. The node has the FORCE_OLD_STYLE_LEADER_ELECTION flag
- // set
- return SCPDriver::getNodeWeight(nodeID, qset, isLocalNode);
- }
-
- ValidatorWeightConfig const& vwc =
- mApp.getConfig().VALIDATOR_WEIGHT_CONFIG.value();
-
- auto entryIt = vwc.mValidatorEntries.find(nodeID);
- if (entryIt == vwc.mValidatorEntries.end())
- {
- // This shouldn't be possible as the validator entries should contain
- // all validators in the config. For this to happen, `getNodeWeight`
- // would have to be called with a non-validator `nodeID`. Throw if
- // building tests, and otherwise fall back on the old algorithm.
- throw std::runtime_error(
- fmt::format(FMT_STRING("Validator entry not found for node {}"),
- toShortString(nodeID)));
- }
-
- ValidatorEntry const& entry = entryIt->second;
- auto homeDomainSizeIt = vwc.mHomeDomainSizes.find(entry.mHomeDomain);
- if (homeDomainSizeIt == vwc.mHomeDomainSizes.end())
- {
- // This shouldn't be possible as the home domain sizes should contain
- // all home domains in the config. For this to happen, `getNodeWeight`
- // would have to be called with a non-validator, or the config parser
- // would have to allow a validator without a home domain. Throw if
- // building tests, and otherwise fall back on the old algorithm.
- throw std::runtime_error(
- fmt::format(FMT_STRING("Home domain size not found for domain {}"),
- entry.mHomeDomain));
- }
-
- auto qualityWeightIt = vwc.mQualityWeights.find(entry.mQuality);
- if (qualityWeightIt == vwc.mQualityWeights.end())
- {
- // This shouldn't be possible as the quality weights should contain all
- // quality levels in the config.
- throw std::runtime_error(
- fmt::format(FMT_STRING("Quality weight not found for quality {}"),
- static_cast(entry.mQuality)));
- }
-
- // Node's weight is its quality's weight divided by the number of nodes in
- // its home domain
- releaseAssert(homeDomainSizeIt->second > 0);
- return qualityWeightIt->second / homeDomainSizeIt->second;
-}
-
std::optional
-HerderSCPDriver::getNominationTimeouts(uint64_t slotIndex) const
+HerderSCPDriver::getLeaderTimeouts(uint64_t slotIndex) const
{
auto it = mSCPExecutionTimes.find(slotIndex);
if (it != mSCPExecutionTimes.end())
{
- return it->second.mNominationTimeoutCount;
+ return it->second.mLeaderTimeoutCount;
}
return std::nullopt;
}
diff --git a/src/herder/HerderSCPDriver.h b/src/herder/HerderSCPDriver.h
index ec3b1bc9a2..1067c0575f 100644
--- a/src/herder/HerderSCPDriver.h
+++ b/src/herder/HerderSCPDriver.h
@@ -34,11 +34,6 @@ class VirtualTimer;
struct StellarValue;
struct SCPEnvelope;
-// First protocol version supporting the application-specific weight function
-// for SCP leader election.
-ProtocolVersion constexpr APPLICATION_SPECIFIC_NOMINATION_LEADER_ELECTION_PROTOCOL_VERSION =
- ProtocolVersion::V_22;
-
class HerderSCPDriver : public SCPDriver
{
public:
@@ -57,8 +52,10 @@ class HerderSCPDriver : public SCPDriver
}
void recordSCPExecutionMetrics(uint64_t slotIndex);
- void recordNominationTrigger(uint64_t slotIndex);
- void recordSCPEvent(uint64_t slotIndex, bool isNomination);
+ // The local trigger for slotIndex fired.
+ void recordTrigger(uint64_t slotIndex);
+ // The local node entered the ballot protocol for slotIndex.
+ void recordBallotStart(uint64_t slotIndex);
void recordSCPExternalizeEvent(uint64_t slotIndex, NodeID const& id,
bool forceUpdateSelf);
@@ -72,9 +69,7 @@ class HerderSCPDriver : public SCPDriver
// value validation
SCPDriver::ValidationLevel validateValue(uint64_t slotIndex,
Value const& value,
- bool nomination) const override;
- ValueWrapperPtr extractValidValue(uint64_t slotIndex,
- Value const& value) override;
+ bool proposal) const override;
// value marshaling
std::string toShortString(NodeID const& pk) const override;
@@ -106,42 +101,47 @@ class HerderSCPDriver : public SCPDriver
std::function cb) override;
void stopTimer(uint64 slotIndex, int timerID) override;
- std::chrono::milliseconds computeTimeout(uint32 roundNumber,
- bool isNomination) override;
-#ifdef BUILD_TESTS
- std::chrono::milliseconds getNominationEmitDelayForTesting() const override;
-#endif
+ std::chrono::milliseconds computeTimeout(uint32 roundNumber) override;
// hashing support
Hash getHashOf(std::vector> const& vals) const override;
// core SCP
- ValueWrapperPtr
- combineCandidates(uint64_t slotIndex,
- ValueWrapperPtrSet const& candidates) override;
void valueExternalized(uint64_t slotIndex, Value const& value) override;
- bool hasUpgrades(Value const& v) override;
- ValueWrapperPtr stripAllUpgrades(Value const& v) override;
- uint32_t getUpgradeNominationTimeoutLimit() const 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).
+ bool isProposalPending(uint64 slotIndex,
+ NodeID const& leader) const override;
+ std::optional getRoundLeader(uint64 slotIndex,
+ uint32 round) override;
+ // The whole schedule for slotIndex (round r is led by entry
+ // (r - 1) % size). Empty when the schedule is not defined for the slot.
+ std::vector const& getLeaderSchedule(uint64_t slotIndex);
+ // True if the local node leads `round` of slotIndex.
+ bool isLocalNodeRoundLeader(uint64_t slotIndex, uint32_t round);
// Update metrics indicating that a value was replaced with an empty-tx-set
// value for slotIndex.
void noteEmptyTxSetValueReplaced(uint64_t slotIndex) override;
- // Submit a value to consider for slotIndex
- // previousValue is the value from slotIndex-1
- void nominate(uint64_t slotIndex, StellarValue const& value,
- TxSetXDRFrameConstPtr proposedSet,
- StellarValue const& previousValue);
+ // Start consensus on slotIndex. SCP calls makeValue only when the local
+ // node leads a round and needs its own value, unless constructValueNow
+ // is set: then the value is constructed first (and reused if this node
+ // becomes a leader).
+ void startSlot(uint64_t slotIndex, ProposalValueSupplier makeValue,
+ bool constructValueNow = false);
SCPQuorumSetPtr getQSet(Hash const& qSetHash) override;
// listeners
void ballotDidHearFromQuorum(uint64_t slotIndex,
SCPBallot const& ballot) override;
- void nominatingValue(uint64_t slotIndex, Value const& value) override;
- void updatedCandidateValue(uint64_t slotIndex, Value const& value) override;
+ void proposedValue(uint64_t slotIndex, uint32 round,
+ Value const& value) override;
+ void enteredRound(uint64_t slotIndex, uint32 round, bool timedOut) override;
void startedBallotProtocol(uint64_t slotIndex,
SCPBallot const& ballot) override;
void acceptedBallotPrepared(uint64_t slotIndex,
@@ -196,13 +196,6 @@ class HerderSCPDriver : public SCPDriver
getTxSetDownloadWaitTime(Value const& v) const override;
std::chrono::milliseconds getTxSetDownloadTimeout() const override;
- // Application-specific weight function. This function uses the quality
- // levels from automatic quorum set generation to determine the weight of a
- // validator. It is designed to ensure that:
- // 1. Orgs of equal quality have equal chances of winning leader election.
- // 2. Higher quality orgs win more frequently than lower quality orgs.
- uint64 getNodeWeight(NodeID const& nodeID, SCPQuorumSet const& qset,
- bool isLocalNode) const override;
// For caching TxSet validity. Consist of {lcl.hash, txSetHash,
// lowerBoundCloseTimeOffset, upperBoundCloseTimeOffset}
using TxSetValidityKey = std::tuple;
@@ -216,12 +209,14 @@ class HerderSCPDriver : public SCPDriver
LedgerHeaderHistoryEntry const& lcl,
ApplyTimeOffset closeTimeOffset) const;
- // Get the number of nomination timeouts that occurred for a given slot
- std::optional getNominationTimeouts(uint64_t slotIndex) const;
+ // Get the number of times the local node timed out waiting for a round
+ // leader in a given slot
+ std::optional getLeaderTimeouts(uint64_t slotIndex) const;
// Elapsed local time from the trigger through entry into ballot, measured
- // on the steady clock. Includes construction and incomplete nomination
- // rounds. Missing timing history does not establish any allowance.
+ // on the steady clock. Includes construction and rounds whose leader
+ // never showed up. Missing timing history does not establish any
+ // allowance.
std::chrono::milliseconds
getTriggerToBallotDuration(uint64_t slotIndex) const;
@@ -264,15 +259,12 @@ class HerderSCPDriver : public SCPDriver
medida::Meter& mValueValid;
medida::Meter& mValueInvalid;
- // listeners
- medida::Meter& mCombinedCandidates;
-
- // Timers for nomination and ballot protocols
- medida::Timer& mNominateToPrepare;
+ // Timer for the ballot protocol
medida::Timer& mPrepareToExternalize;
- // Local trigger to first entry into ballot (proposal construction
- // plus nomination); the drift allowance of the trigger anchor
+ // Local trigger to first entry into ballot (proposal construction, or
+ // waiting for the round leader's proposal); the drift allowance of
+ // the trigger anchor
medida::Timer& mTriggerToBallot;
// Timers tracking externalize messages
@@ -297,8 +289,10 @@ class HerderSCPDriver : public SCPDriver
SCPMetrics mSCPMetrics;
- // Nomination timeouts per ledger
- medida::Histogram& mNominateTimeout;
+ // Timeouts waiting for a round leader, per ledger
+ medida::Histogram& mLeaderTimeout;
+ // Round in which the ledger externalized (1 = first leader)
+ medida::Histogram& mLeaderRound;
// Prepare timeouts per ledger
medida::Histogram& mPrepareTimeout;
// Unique values referenced per ledger
@@ -310,11 +304,12 @@ class HerderSCPDriver : public SCPDriver
struct SCPTiming
{
std::optional mTriggerStart;
- std::optional mNominationStart;
std::optional mPrepareStart;
- // Nomination timeouts before first prepare
- int64_t mNominationTimeoutCount{0};
+ // Timeouts waiting for a round leader's proposal
+ int64_t mLeaderTimeoutCount{0};
+ // Highest round the local node entered
+ uint32_t mMaxRound{0};
// Prepare timeouts before externalize
int64_t mPrepareTimeoutCount{0};
@@ -328,13 +323,25 @@ class HerderSCPDriver : public SCPDriver
};
// Map of time points for each slot to measure key protocol metrics:
- // * nomination to first prepare
+ // * trigger to first prepare
// * first prepare to externalize
std::map mSCPExecutionTimes;
- uint32_t mLedgerSeqNominating;
+ // Slot the local node started, and the value it built for it (if any)
+ uint32_t mLedgerSeqProposing;
ValueWrapperPtr mCurrentValue;
+ // Leader schedule of the next ledger to close
+ struct LeaderScheduleCache
+ {
+ uint64_t mSlotIndex{0};
+ Hash mSeed;
+ size_t mQuorumSize{0};
+ uint64_t mQuorumRebuildCount{0};
+ std::vector mSchedule;
+ };
+ LeaderScheduleCache mLeaderSchedule;
+
// timers used by SCP
// indexed by slotIndex, timerID
std::map>> mSCPTimers;
@@ -351,7 +358,7 @@ class HerderSCPDriver : public SCPDriver
SCPDriver::ValidationLevel
validateValueAgainstLocalState(uint64_t slotIndex, StellarValue const& sv,
- bool nomination) const;
+ bool proposal) const;
SCPDriver::ValidationLevel
validatePastOrFutureValue(uint64_t slotIndex, StellarValue const& b,
@@ -376,6 +383,6 @@ class HerderSCPDriver : public SCPDriver
bool deserializeAndValidateStellarValue(uint64_t slotIndex,
Value const& value,
StellarValue& sv) const;
- void extractValidUpgrades(StellarValue& sv, bool nomination) const;
+ void extractValidUpgrades(StellarValue& sv, bool proposal) const;
};
}
diff --git a/src/herder/HerderUtils.cpp b/src/herder/HerderUtils.cpp
index 62322fe363..47d73de616 100644
--- a/src/herder/HerderUtils.cpp
+++ b/src/herder/HerderUtils.cpp
@@ -5,11 +5,15 @@
#include "herder/HerderUtils.h"
#include "crypto/Hex.h"
#include "crypto/KeyUtils.h"
+#include "crypto/SecretKey.h"
#include "lib/json/json.h"
+#include "main/Application.h"
#include "main/Config.h"
+#include "medida/meter.h"
#include "rust/RustVecXdrMarshal.h"
#include "scp/LocalNode.h"
#include "scp/Slot.h"
+#include "util/MetricsRegistry.h"
#include "xdr/Stellar-contract.h"
#include "xdr/Stellar-ledger.h"
#include "xdrpp/types.h"
@@ -172,4 +176,24 @@ parseQuorumMapFromJson(std::string const& jsonPath)
return qmap;
}
+void
+markSignatureCacheLookups(Application& app, std::string const& prefix,
+ uint64_t hits, uint64_t misses)
+{
+ uint64_t hitsNow = 0;
+ uint64_t missesNow = 0;
+ PubKeyUtils::getVerifySigCacheCounts(hitsNow, missesNow);
+ // A flush in between (the app's metrics sync, on the main thread like
+ // the callers) would have reset them.
+ if (hitsNow >= hits && missesNow >= misses)
+ {
+ auto& metrics = app.getMetrics();
+ metrics
+ .NewMeter({"herder", "txset", prefix + "-sig-cache-hit"}, "lookup")
+ .Mark(hitsNow - hits);
+ metrics
+ .NewMeter({"herder", "txset", prefix + "-sig-cache-miss"}, "lookup")
+ .Mark(missesNow - misses);
+ }
+}
}
diff --git a/src/herder/HerderUtils.h b/src/herder/HerderUtils.h
index 4274e5a788..465acffe32 100644
--- a/src/herder/HerderUtils.h
+++ b/src/herder/HerderUtils.h
@@ -46,4 +46,12 @@ toQuorumIntersectionMap(QuorumTracker::QuorumMap const& qmap);
QuorumIntersectionChecker::QuorumSetMap
parseQuorumMapFromJson(std::string const& jsonPath);
+
+class Application;
+// Marks the signature cache lookups made since PubKeyUtils::
+// getVerifySigCacheCounts returned `hits` and `misses` on the meters
+// herder.txset.-sig-cache-hit and -miss. Lookups on other threads
+// in the meantime count too.
+void markSignatureCacheLookups(Application& app, std::string const& prefix,
+ uint64_t hits, uint64_t misses);
}
diff --git a/src/herder/LeaderSchedule.cpp b/src/herder/LeaderSchedule.cpp
new file mode 100644
index 0000000000..cc9599a718
--- /dev/null
+++ b/src/herder/LeaderSchedule.cpp
@@ -0,0 +1,38 @@
+// 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
+
+#include "herder/LeaderSchedule.h"
+#include "crypto/SHA.h"
+#include "util/XDROperators.h"
+#include "xdrpp/marshal.h"
+#include
+#include
+
+namespace stellar
+{
+
+std::vector
+computeLeaderSchedule(Hash const& seed, std::vector nodes)
+{
+ std::vector> keyed;
+ keyed.reserve(nodes.size());
+ for (auto& node : nodes)
+ {
+ SHA256 hasher;
+ hasher.add(seed);
+ hasher.add(xdr::xdr_to_opaque(node));
+ keyed.emplace_back(hasher.finish(), std::move(node));
+ }
+ std::sort(keyed.begin(), keyed.end());
+ keyed.erase(std::unique(keyed.begin(), keyed.end()), keyed.end());
+
+ std::vector res;
+ res.reserve(keyed.size());
+ for (auto& k : keyed)
+ {
+ res.emplace_back(std::move(k.second));
+ }
+ return res;
+}
+}
diff --git a/src/herder/LeaderSchedule.h b/src/herder/LeaderSchedule.h
new file mode 100644
index 0000000000..9042d90eaa
--- /dev/null
+++ b/src/herder/LeaderSchedule.h
@@ -0,0 +1,26 @@
+// 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
+
+#pragma once
+
+#include "xdr/Stellar-types.h"
+#include
+
+namespace stellar
+{
+
+// Deterministic leader schedule (experimental; replaces SCP nomination).
+//
+// Returns `nodes` (duplicates removed) ordered by SHA256(seed || nodeID),
+// ties (impossible in practice) broken by node ID. Round r (1-based) of the
+// slot is led by entry (r - 1) % size, so every node gets a turn before any
+// 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.
+std::vector computeLeaderSchedule(Hash const& seed,
+ std::vector nodes);
+}
diff --git a/src/herder/ParallelTxSetBuilder.cpp b/src/herder/ParallelTxSetBuilder.cpp
index dacb0ebdf5..bbb9d552e2 100644
--- a/src/herder/ParallelTxSetBuilder.cpp
+++ b/src/herder/ParallelTxSetBuilder.cpp
@@ -718,15 +718,24 @@ buildSurgePricedParallelSorobanPhase(
// Simplify the transactions to the minimum necessary amount of data.
auto builderTxs = prepareBuilderTxs(txFrames);
- // Sort transactions in decreasing inclusion fee order.
+ // Sort transactions in decreasing inclusion fee order. Look up each
+ // transaction's fee bid and operation count once, not per comparison.
TxFeeComparator txComparator(
/* isGreater */ true,
stellar::rand_uniform(0, std::numeric_limits::max()));
+ std::vector> feeBids;
+ feeBids.reserve(txFrames.size());
+ for (auto const& tx : txFrames)
+ {
+ feeBids.emplace_back(tx->getInclusionFee(), tx->getNumOperations());
+ }
std::vector sortedTxOrder(txFrames.size());
std::iota(sortedTxOrder.begin(), sortedTxOrder.end(), 0);
std::sort(sortedTxOrder.begin(), sortedTxOrder.end(),
- [&txFrames, &txComparator](size_t a, size_t b) {
- return txComparator(txFrames[a], txFrames[b]);
+ [&txFrames, &feeBids, &txComparator](size_t a, size_t b) {
+ return txComparator(txFrames[a], feeBids[a].first,
+ feeBids[a].second, txFrames[b],
+ feeBids[b].first, feeBids[b].second);
});
// Precompute per-transaction resources to avoid repeated virtual calls
diff --git a/src/herder/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp
index 204fa49860..82b35f300f 100644
--- a/src/herder/PendingEnvelopes.cpp
+++ b/src/herder/PendingEnvelopes.cpp
@@ -15,6 +15,7 @@
#include "util/GlobalChecks.h"
#include "util/Logging.h"
#include "util/MetricsRegistry.h"
+#include "util/ProtocolVersion.h"
#include "util/UnorderedSet.h"
#include
#include
@@ -35,6 +36,7 @@ PendingEnvelopes::PendingEnvelopes(Application& app, HerderImpl& herder)
, mValueSizeCache(TXSET_CACHE_SIZE + QSET_CACHE_SIZE)
, mRebuildQuorum(true)
, mQuorumTracker(mApp.getConfig().NODE_SEED.getPublicKey())
+ , mTxSetFetchRetryTimer(app)
, mProcessedCount(
app.getMetrics().NewCounter({"scp", "pending", "processed"}))
, mDiscardedCount(
@@ -43,8 +45,10 @@ PendingEnvelopes::PendingEnvelopes(Application& app, HerderImpl& herder)
app.getMetrics().NewCounter({"scp", "pending", "fetching"}))
, mReadyCount(app.getMetrics().NewCounter({"scp", "pending", "ready"}))
, mFetchDuration(app.getMetrics().NewTimer({"scp", "fetch", "envelope"}))
- , mFetchTxSetTimer(app.getMetrics().NewTimer({"overlay", "fetch", "txset"}))
+ , mFetchTxSetTimer(app.getMetrics().NewTimer({"scp", "fetch", "txset"}))
, mFetchQsetTimer(app.getMetrics().NewTimer({"overlay", "fetch", "qset"}))
+ , mTxSetFetchRetries(
+ app.getMetrics().NewCounter({"scp", "fetch", "txset-retry"}))
, mCostPerSlot(app.getMetrics().NewHistogram({"scp", "cost", "per-slot"}))
{
}
@@ -265,6 +269,11 @@ PendingEnvelopes::addTxSet(Hash const& hash, uint64 lastSeenSlotIndex,
CLOG_TRACE(Herder, "Add TxSet {}", hexAbbrev(hash));
putTxSet(hash, lastSeenSlotIndex, txset);
+ // A locally built set can be one we are fetching: a promoted leader can
+ // build the set it was waiting for, and nobody can serve a legacy set over
+ // the Rust overlay. Release its waiting envelopes now. This can reenter
+ // SCP and externalize.
+ completeTxSetFetch(hash, txset, /*deliveredByOverlay=*/false);
}
bool
@@ -274,29 +283,76 @@ PendingEnvelopes::recvTxSet(Hash const& hash, TxSetXDRFrameConstPtr txset)
CLOG_INFO(Herder, "Got TxSet {}", hexAbbrev(hash));
// Only accept if we were actually fetching this
- auto it = mPendingTxSetFetches.find(hash);
- if (it == mPendingTxSetFetches.end())
+ if (mPendingTxSetFetches.find(hash) == mPendingTxSetFetches.end())
+ {
+ if (hasTxSet(hash))
+ {
+ // E.g. built locally after requesting it from the overlay
+ CLOG_DEBUG(Herder, "TxSet {} already known - ignoring",
+ hexAbbrev(hash));
+ }
+ else
+ {
+ CLOG_WARNING(Herder, "TxSet {} not in pending fetches - rejecting",
+ hexAbbrev(hash));
+ }
+ return false;
+ }
+
+ // The overlay checks the content hash before delivering a set, but the
+ // frame recomputed it: never file a set under another hash.
+ if (txset->getContentsHash() != hash)
{
- CLOG_WARNING(Herder, "TxSet {} not in pending fetches - rejecting",
- hexAbbrev(hash));
+ CLOG_WARNING(Herder,
+ "TxSet {} delivered with content hash {} - "
+ "rejecting",
+ hexAbbrev(hash), hexAbbrev(txset->getContentsHash()));
return false;
}
- addTxSet(hash, 0, txset);
+ putTxSet(hash, 0, txset);
+ completeTxSetFetch(hash, txset, /*deliveredByOverlay=*/true);
+ return true;
+}
+
+void
+PendingEnvelopes::completeTxSetFetch(Hash const& hash,
+ TxSetXDRFrameConstPtr const& txset,
+ bool deliveredByOverlay)
+{
+ auto it = mPendingTxSetFetches.find(hash);
+ if (it == mPendingTxSetFetches.end())
+ {
+ return;
+ }
+ // Detach the waiting envelopes first: reprocessing them reenters SCP and
+ // can start or stop other fetches.
+ auto envelopes = std::move(it->second);
+ mPendingTxSetFetches.erase(it);
+ auto fetchIt = mTxSetFetches.find(hash);
+ if (fetchIt != mTxSetFetches.end())
+ {
+ // scp.fetch.txset times overlay deliveries only, not sets this node
+ // ended up building itself. (overlay.fetch.txset is the overlay's own
+ // per-request fetch time, synced from Rust.)
+ if (deliveredByOverlay)
+ {
+ mFetchTxSetTimer.Update(mApp.getClock().now() -
+ fetchIt->second.mStarted);
+ }
+ mTxSetFetches.erase(fetchIt);
+ }
// Update any ValueWrappers that were created before this tx set was
// available
mHerder.getHerderSCPDriver().onTxSetReceived(hash, txset);
- for (auto& env : it->second)
+ for (auto const& env : envelopes)
{
- CLOG_INFO(Herder, "Re-processing envelope after TxSet {} fetch",
- hexAbbrev(hash));
+ CLOG_DEBUG(Herder, "Re-processing envelope after TxSet {} fetch",
+ hexAbbrev(hash));
mApp.getHerder().recvSCPEnvelope(env);
}
- mPendingTxSetFetches.erase(hash);
- mTxSetFetchStartTimes.erase(hash);
- return true;
}
bool
@@ -706,22 +762,26 @@ PendingEnvelopes::startFetch(SCPEnvelope const& envelope)
// doesn't need fetching): nothing to do
continue;
}
+ auto const slot = envelope.statement.slotIndex;
auto it = mPendingTxSetFetches.find(h2);
if (it != mPendingTxSetFetches.end())
{
// Already fetching - just add envelope to waiting list
it->second.push_back(envelope);
+ auto& fetch = mTxSetFetches.at(h2);
+ fetch.mSlot = std::max(fetch.mSlot, slot);
}
else
{
- // Not fetching yet - start fetch
+ // Not fetching yet - start fetch. Repeated only if the overlay
+ // does not deliver it (retryStalledTxSetFetches).
auto& vec = mPendingTxSetFetches[h2];
vec.push_back(envelope);
- mTxSetFetchStartTimes.emplace(h2, mApp.getClock().now());
- mApp.getOverlayManager().requestTxSet(
- h2,
- static_cast(
- envelope.statement.slotIndex)); // Only once!
+ auto const now = mApp.getClock().now();
+ mTxSetFetches[h2] = TxSetFetchState{now, now, slot, 1};
+ mApp.getOverlayManager().requestTxSet(h2,
+ static_cast(slot));
+ scheduleTxSetFetchRetry();
}
}
@@ -750,7 +810,7 @@ PendingEnvelopes::stopFetch(SCPEnvelope const& envelope)
if (vec.empty())
{
mPendingTxSetFetches.erase(it);
- mTxSetFetchStartTimes.erase(h2);
+ mTxSetFetches.erase(h2);
}
}
}
@@ -909,8 +969,108 @@ PendingEnvelopes::stopAllOutsideRange(std::optional minSlot,
maybeRecordCost(it);
}
}
- // Clear pending fetches for old slots - no need to track individual slots
- // since Rust overlay handles timeout/retry logic
+ // Stop fetching tx sets that only purged slots were waiting for, so they
+ // are no longer re-requested.
+ for (auto it = mTxSetFetches.begin(); it != mTxSetFetches.end();)
+ {
+ auto const slot = it->second.mSlot;
+ if (slot != slotToKeep &&
+ ((minSlot && slot < *minSlot) || (maxSlot && slot > *maxSlot)))
+ {
+ mPendingTxSetFetches.erase(it->first);
+ it = mTxSetFetches.erase(it);
+ }
+ else
+ {
+ ++it;
+ }
+ }
+}
+
+void
+PendingEnvelopes::scheduleTxSetFetchRetry()
+{
+ // Only sets for undecided slots are re-requested (see
+ // retryStalledTxSetFetches).
+ auto const lcl = mApp.getLedgerManager().getLastClosedLedgerNum();
+ if (mTxSetFetchRetryScheduled ||
+ std::none_of(
+ mTxSetFetches.begin(), mTxSetFetches.end(),
+ [&](auto const& fetch) { return fetch.second.mSlot > lcl; }))
+ {
+ return;
+ }
+ mTxSetFetchRetryScheduled = true;
+ mTxSetFetchRetryTimer.expires_from_now(TX_SET_FETCH_RETRY_INTERVAL);
+ mTxSetFetchRetryTimer.async_wait(
+ [this]() {
+ mTxSetFetchRetryScheduled = false;
+ retryStalledTxSetFetches();
+ },
+ &VirtualTimer::onFailureNoop);
+}
+
+void
+PendingEnvelopes::retryStalledTxSetFetches()
+{
+ // The overlay neither times out nor retries a request: a peer can be
+ // silent or lack the set (there is no negative response), and nobody can
+ // serve a legacy (pre-protocol 20) set. Repeat requests that have not been
+ // answered; the overlay sends a repeated request to another peer.
+ auto const now = mApp.getClock().now();
+ auto const lcl = mApp.getLedgerManager().getLastClosedLedgerNum();
+ bool const legacyLcl =
+ protocolVersionIsBefore(mApp.getLedgerManager()
+ .getLastClosedLedgerHeader()
+ .header.ledgerVersion,
+ SOROBAN_PROTOCOL_VERSION);
+ for (auto& [hash, fetch] : mTxSetFetches)
+ {
+ // A set for a slot this node already closed (e.g. kept for the
+ // checkpoint) is not needed for consensus; it was requested once.
+ if (fetch.mSlot <= lcl)
+ {
+ continue;
+ }
+ // Back off (1, 2, 4, then every 8 s): a slow but live transfer of a
+ // large set should not be duplicated to another peer every second.
+ auto const backoff = TX_SET_FETCH_RETRY_INTERVAL *
+ (1u << std::min(fetch.mRequests - 1, 3));
+ if (now - fetch.mLastRequested < backoff)
+ {
+ continue;
+ }
+ fetch.mLastRequested = now;
+ ++fetch.mRequests;
+ mTxSetFetchRetries.inc();
+ auto const waited =
+ std::chrono::duration_cast(
+ now - fetch.mStarted);
+ // Warn on every doubling: a set that never arrives stays visible
+ // without flooding the log.
+ if ((fetch.mRequests & (fetch.mRequests - 1)) == 0)
+ {
+ CLOG_WARNING(Herder,
+ "TxSet {} for slot {} not received after {} ms, "
+ "requesting it again (request {}){}",
+ hexAbbrev(hash), fetch.mSlot, waited.count(),
+ fetch.mRequests,
+ legacyLcl ? ". The Rust overlay cannot fetch legacy "
+ "(pre-protocol 20) tx sets"
+ : "");
+ }
+ else
+ {
+ CLOG_DEBUG(Herder,
+ "TxSet {} for slot {} not received after {} ms, "
+ "requesting it again (request {})",
+ hexAbbrev(hash), fetch.mSlot, waited.count(),
+ fetch.mRequests);
+ }
+ mApp.getOverlayManager().requestTxSet(
+ hash, static_cast(fetch.mSlot));
+ }
+ scheduleTxSetFetchRetry();
}
void
@@ -955,13 +1115,13 @@ PendingEnvelopes::getQSet(Hash const& hash)
std::optional
PendingEnvelopes::getTxSetWaitingTime(Hash const& hash) const
{
- auto it = mTxSetFetchStartTimes.find(hash);
- if (it == mTxSetFetchStartTimes.end())
+ auto it = mTxSetFetches.find(hash);
+ if (it == mTxSetFetches.end())
{
return std::nullopt;
}
return std::chrono::duration_cast(
- mApp.getClock().now() - it->second);
+ mApp.getClock().now() - it->second.mStarted);
}
Json::Value
@@ -1004,6 +1164,7 @@ PendingEnvelopes::rebuildQuorumTrackerState()
{
// rebuild quorum information using data sources starting with the
// freshest source
+ ++mQuorumRebuildCount;
mQuorumTracker.rebuild([&](NodeID const& id) -> SCPQuorumSetPtr {
SCPQuorumSetPtr res;
if (id == mHerder.getSCP().getLocalNodeID())
@@ -1035,6 +1196,32 @@ PendingEnvelopes::rebuildQuorumTrackerState()
});
}
+bool
+PendingEnvelopes::hasPendingEnvelopeFrom(uint64 slotIndex,
+ NodeID const& node) const
+{
+ auto it = mEnvelopes.find(slotIndex);
+ if (it == mEnvelopes.end())
+ {
+ return false;
+ }
+ for (auto const& f : it->second.mFetchingEnvelopes)
+ {
+ if (f.first.statement.nodeID == node)
+ {
+ return true;
+ }
+ }
+ for (auto const& r : it->second.mReadyEnvelopes)
+ {
+ if (r->getStatement().nodeID == node)
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
QuorumTracker::QuorumMap const&
PendingEnvelopes::getCurrentlyTrackedQuorum() const
{
diff --git a/src/herder/PendingEnvelopes.h b/src/herder/PendingEnvelopes.h
index 7d9210de3e..0011b5cda1 100644
--- a/src/herder/PendingEnvelopes.h
+++ b/src/herder/PendingEnvelopes.h
@@ -9,6 +9,7 @@
#include "herder/QuorumTracker.h"
#include "lib/json/json.h"
#include "util/RandomEvictionCache.h"
+#include "util/Timer.h"
#include
#include
#include
@@ -64,9 +65,20 @@ class PendingEnvelopes
// hashes of txsets/qsets we're currently fetching
std::map> mPendingTxSetFetches;
std::map> mPendingQSetFetches;
- // when each pending tx set fetch was started, used to report how long
- // we've been waiting for a tx set (see getTxSetWaitingTime)
- std::map mTxSetFetchStartTimes;
+ // State of each pending tx set fetch: when it started (see
+ // getTxSetWaitingTime), the newest slot waiting for it, and its overlay
+ // requests. The overlay has no request timeout of its own; Core
+ // re-requests sets it has not delivered (retryStalledTxSetFetches).
+ struct TxSetFetchState
+ {
+ VirtualClock::time_point mStarted;
+ VirtualClock::time_point mLastRequested;
+ uint64 mSlot;
+ uint32_t mRequests;
+ };
+ std::map mTxSetFetches;
+ VirtualTimer mTxSetFetchRetryTimer;
+ bool mTxSetFetchRetryScheduled{false};
using TxSetFramCacheItem = std::pair;
// recent txsets
@@ -82,6 +94,7 @@ class PendingEnvelopes
RandomEvictionCache mValueSizeCache;
bool mRebuildQuorum;
+ uint64_t mQuorumRebuildCount{0};
QuorumTracker mQuorumTracker;
medida::Counter& mProcessedCount;
@@ -91,6 +104,7 @@ class PendingEnvelopes
medida::Timer& mFetchDuration;
medida::Timer& mFetchTxSetTimer;
medida::Timer& mFetchQsetTimer;
+ medida::Counter& mTxSetFetchRetries;
// Tracked cost per slot
medida::Histogram& mCostPerSlot;
@@ -98,6 +112,13 @@ class PendingEnvelopes
// as it is not sane QSet
void discardSCPEnvelopesWithQSet(Hash const& hash);
+ // Hands a newly known tx set to the envelopes waiting to fetch it.
+ void completeTxSetFetch(Hash const& hash,
+ TxSetXDRFrameConstPtr const& txset,
+ bool deliveredByOverlay);
+ void scheduleTxSetFetchRetry();
+ void retryStalledTxSetFetches();
+
void updateMetrics();
void envelopeReady(SCPEnvelope const& envelope);
@@ -131,6 +152,11 @@ class PendingEnvelopes
std::optional maxSlot, uint64 slotToKeep);
public:
+ // How long a tx set request may stay unanswered before it is first
+ // repeated; later repeats back off up to 8 times this.
+ static constexpr std::chrono::milliseconds TX_SET_FETCH_RETRY_INTERVAL{
+ 1000};
+
PendingEnvelopes(Application& app, HerderImpl& herder);
~PendingEnvelopes();
@@ -163,10 +189,10 @@ class PendingEnvelopes
bool recvSCPQuorumSet(Hash const& hash, SCPQuorumSet const& qset);
/**
- * Add @p txset identified by @p hash to local cache. Notifies
- * @see ItemFetcher about that event - it may cause calls to Herder's
- * recvSCPEnvelope which in turn may cause calls to @see recvSCPEnvelope
- * in PendingEnvelopes.
+ * Add @p txset identified by @p hash to local cache. If it was being
+ * fetched, completes the fetch: the envelopes waiting for it are passed
+ * to Herder's recvSCPEnvelope, which in turn may call @see
+ * recvSCPEnvelope in PendingEnvelopes, reenter SCP and externalize.
*/
void addTxSet(Hash const& hash, uint64 lastSeenSlotIndex,
TxSetXDRFrameConstPtr txset);
@@ -181,8 +207,8 @@ class PendingEnvelopes
/**
* Check if @p txset identified by @p hash was requested before from peers.
- * If not, ignores that @p txset. If it was requested, calls
- * @see addTxSet.
+ * If not, ignores that @p txset. If it was requested, caches it and
+ * completes the fetch like @see addTxSet.
*
* Return true if TxSet useful (was asked for).
*/
@@ -231,6 +257,17 @@ class PendingEnvelopes
bool isNodeDefinitelyInQuorum(NodeID const& node);
void rebuildQuorumTrackerState();
+ // True if an envelope of `node` for `slotIndex` is being fetched (waiting
+ // for its tx set or quorum set) or is ready but not yet processed by SCP.
+ bool hasPendingEnvelopeFrom(uint64 slotIndex, NodeID const& node) const;
+ // Incremented every time the transitive quorum is rebuilt from scratch.
+ // Together with the quorum's size (it only grows between rebuilds) this
+ // identifies its node set.
+ uint64_t
+ getQuorumRebuildCount() const
+ {
+ return mQuorumRebuildCount;
+ }
QuorumTracker::QuorumMap const& getCurrentlyTrackedQuorum() const;
// updates internal state when an envelope was successfully processed
diff --git a/src/herder/SurgePricingUtils.cpp b/src/herder/SurgePricingUtils.cpp
index fe9d0c17b8..edec65d71c 100644
--- a/src/herder/SurgePricingUtils.cpp
+++ b/src/herder/SurgePricingUtils.cpp
@@ -12,19 +12,6 @@
namespace stellar
{
-namespace
-{
-
-// Use _inclusion_ fee to order transactions
-int
-feeRate3WayCompare(TransactionFrameBase const& l, TransactionFrameBase const& r)
-{
- return stellar::feeRate3WayCompare(
- l.getInclusionFee(), l.getNumOperations(), r.getInclusionFee(),
- r.getNumOperations());
-}
-
-} // namespace
int
feeRate3WayCompare(int64_t lFeeBid, uint32_t lNbOps, int64_t rFeeBid,
@@ -82,7 +69,17 @@ bool
TxFeeComparator::operator()(TransactionFrameBasePtr const& tx1,
TransactionFrameBasePtr const& tx2) const
{
- return txLessThan(tx1, tx2);
+ // Use _inclusion_ fee to order transactions
+ return txLessThan(tx1, tx1->getInclusionFee(), tx1->getNumOperations(), tx2,
+ tx2->getInclusionFee(), tx2->getNumOperations());
+}
+
+bool
+TxFeeComparator::operator()(TransactionFrameBasePtr const& tx1, int64_t tx1Bid,
+ uint32_t tx1Ops, TransactionFrameBasePtr const& tx2,
+ int64_t tx2Bid, uint32_t tx2Ops) const
+{
+ return txLessThan(tx1, tx1Bid, tx1Ops, tx2, tx2Bid, tx2Ops);
}
bool
@@ -108,10 +105,11 @@ TxFeeComparator::isGreater() const
}
bool
-TxFeeComparator::txLessThan(TransactionFrameBasePtr const& tx1,
- TransactionFrameBasePtr const& tx2) const
+TxFeeComparator::txLessThan(TransactionFrameBasePtr const& tx1, int64_t tx1Bid,
+ uint32_t tx1Ops, TransactionFrameBasePtr const& tx2,
+ int64_t tx2Bid, uint32_t tx2Ops) const
{
- auto cmp3 = feeRate3WayCompare(*tx1, *tx2);
+ auto cmp3 = feeRate3WayCompare(tx1Bid, tx1Ops, tx2Bid, tx2Ops);
if (cmp3 != 0)
{
diff --git a/src/herder/SurgePricingUtils.h b/src/herder/SurgePricingUtils.h
index 0c353dbf07..f6e7fb3b6f 100644
--- a/src/herder/SurgePricingUtils.h
+++ b/src/herder/SurgePricingUtils.h
@@ -34,6 +34,11 @@ class TxFeeComparator
bool operator()(TransactionFrameBasePtr const& tx1,
TransactionFrameBasePtr const& tx2) const;
+ // The same comparison, given the transactions' inclusion fee bids and
+ // operation counts (e.g. computed once per transaction for a sort).
+ bool operator()(TransactionFrameBasePtr const& tx1, int64_t tx1Bid,
+ uint32_t tx1Ops, TransactionFrameBasePtr const& tx2,
+ int64_t tx2Bid, uint32_t tx2Ops) const;
bool compareFeeOnly(TransactionFrameBase const& tx1,
TransactionFrameBase const& tx2) const;
@@ -42,8 +47,9 @@ class TxFeeComparator
bool isGreater() const;
private:
- bool txLessThan(TransactionFrameBasePtr const& tx1,
- TransactionFrameBasePtr const& tx2) const;
+ bool txLessThan(TransactionFrameBasePtr const& tx1, int64_t tx1Bid,
+ uint32_t tx1Ops, TransactionFrameBasePtr const& tx2,
+ int64_t tx2Bid, uint32_t tx2Ops) const;
bool const mIsGreater;
#ifndef BUILD_TESTS
diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp
index 03b3914223..6ef86de24c 100644
--- a/src/herder/TxSetFrame.cpp
+++ b/src/herder/TxSetFrame.cpp
@@ -784,6 +784,40 @@ TxSetXDRFrame::TxSetXDRFrame(GeneralizedTransactionSet const& xdrTxSet)
{
}
+TxSetXDRFrame::TxSetXDRFrame(TransactionSet&& xdrTxSet)
+ : mXDRTxSet(std::move(xdrTxSet))
+ , mEncodedSize(xdr::xdr_argpack_size(std::get(mXDRTxSet)))
+ , mHash(computeNonGeneralizedTxSetContentsHash(
+ std::get(mXDRTxSet)))
+{
+}
+
+TxSetXDRFrame::TxSetXDRFrame(GeneralizedTransactionSet&& xdrTxSet)
+ : mXDRTxSet(std::move(xdrTxSet))
+ , mEncodedSize(
+ xdr::xdr_argpack_size(std::get(mXDRTxSet)))
+ , mHash(xdrSha256(std::get(mXDRTxSet)))
+{
+}
+
+TxSetXDRFrameConstPtr
+TxSetXDRFrame::makeFromWire(TransactionSet&& xdrTxSet)
+{
+ ZoneScoped;
+ std::shared_ptr txSet(
+ new TxSetXDRFrame(std::move(xdrTxSet)));
+ return txSet;
+}
+
+TxSetXDRFrameConstPtr
+TxSetXDRFrame::makeFromWire(GeneralizedTransactionSet&& xdrTxSet)
+{
+ ZoneScoped;
+ std::shared_ptr txSet(
+ new TxSetXDRFrame(std::move(xdrTxSet)));
+ return txSet;
+}
+
TxSetXDRFrameConstPtr
TxSetXDRFrame::makeFromWire(TransactionSet const& xdrTxSet)
{
@@ -843,11 +877,15 @@ makeTxSetFromTransactions(
txtest::ParallelSorobanOrder const& parallelSorobanOrder,
bool disableTxValidationForLegacyScenario
#endif
-)
+ ,
+ TxSetReadyCallback const& onTxSetReady,
+ std::vector const& validTxTargets)
{
releaseAssert(threadIsMain());
releaseAssert(!app.getLedgerManager().isApplying());
releaseAssert(txPhases.size() == invalidTxs.size());
+ releaseAssert(validTxTargets.empty() ||
+ validTxTargets.size() == txPhases.size());
releaseAssert(txPhases.size() <=
static_cast(TxSetPhase::PHASE_COUNT));
@@ -879,9 +917,30 @@ makeTxSetFromTransactions(
else
{
#endif
- validatedTxs = TxSetUtils::trimInvalid(
- phaseTxs, app, accountFeeMap, closeTimeOffset.seconds(),
- closeTimeOffset.seconds(), invalid);
+ auto trimTimer = app.getMetrics()
+ .NewTimer({"herder", "txset", "build-trim"})
+ .TimeScope();
+ if (validTxTargets.empty())
+ {
+ validatedTxs = TxSetUtils::trimInvalid(
+ phaseTxs, app, accountFeeMap, closeTimeOffset.seconds(),
+ closeTimeOffset.seconds(), invalid);
+ }
+ else
+ {
+ // Transactions of limited lanes (DEX) can't fill the phase,
+ // so only generic lane ones count toward the target.
+ auto laneConfig = createSurgePricingLangeConfig(
+ static_cast(i), app);
+ validatedTxs = TxSetUtils::trimInvalidPrefix(
+ phaseTxs, validTxTargets[i],
+ [&laneConfig](TransactionFrameBase const& tx) {
+ return laneConfig->getLane(tx) ==
+ SurgePricingPriorityQueue::GENERIC_LANE;
+ },
+ app, accountFeeMap, closeTimeOffset.seconds(),
+ closeTimeOffset.seconds(), invalid);
+ }
#ifdef BUILD_TESTS
// In tests we shouldn't be really trying to close ledgers with
// invalid transactions trimmed (which is unfortunately a very
@@ -901,6 +960,9 @@ makeTxSetFromTransactions(
}
#endif
auto phaseType = static_cast(i);
+ auto selectTimer = app.getMetrics()
+ .NewTimer({"herder", "txset", "build-select"})
+ .TimeScope();
auto [includedTxs, inclusionFeeMapBinding] =
applySurgePricing(phaseType, validatedTxs, app
#ifdef BUILD_TESTS
@@ -935,6 +997,8 @@ makeTxSetFromTransactions(
}
auto const& lclHeader = app.getLedgerManager().getLastClosedLedgerHeader();
+ auto xdrTimer =
+ app.getMetrics().NewTimer({"herder", "txset", "build-xdr"}).TimeScope();
// Preliminary applicable frame - we don't know the contents hash yet, but
// we also don't return this.
std::unique_ptr preliminaryApplicableTxSet(
@@ -956,8 +1020,21 @@ makeTxSetFromTransactions(
}
#endif
+ xdrTimer.Stop();
+ // The final XDR is available now. Start proposal encoding while the
+ // roundtrip and final validity checks below run; only the local builder
+ // supplies this callback. The callback must not nominate the set.
+ if (onTxSetReady)
+ {
+ onTxSetReady(outputTxSet);
+ }
+
+ auto roundtripTimer = app.getMetrics()
+ .NewTimer({"herder", "txset", "build-roundtrip"})
+ .TimeScope();
ApplicableTxSetFrameConstPtr outputApplicableTxSet =
outputTxSet->prepareForApply(app, lclHeader.header);
+ roundtripTimer.Stop();
if (!outputApplicableTxSet)
{
@@ -986,8 +1063,12 @@ makeTxSetFromTransactions(
}
// We already trimmed invalid transactions in an earlier call to
// `trimInvalid`, so skip transaction validation here
+ auto finalTimer = app.getMetrics()
+ .NewTimer({"herder", "txset", "build-final-check"})
+ .TimeScope();
auto validationResult = outputApplicableTxSet->checkValidInternalWithResult(
app, closeTimeOffset.seconds(), closeTimeOffset.seconds(), true);
+ finalTimer.Stop();
if (validationResult != TxSetValidationResult::VALID)
{
throw std::runtime_error(fmt::format(
@@ -1015,11 +1096,11 @@ TxSetXDRFrame::makeEmpty(Hash const& previousLedgerHash,
GeneralizedTransactionSet txSet;
transactionsToGeneralizedTransactionSetXDR(emptyPhases,
previousLedgerHash, txSet);
- return TxSetXDRFrame::makeFromWire(txSet);
+ return TxSetXDRFrame::makeFromWire(std::move(txSet));
}
TransactionSet txSet;
transactionsToTransactionSetXDR({}, previousLedgerHash, txSet);
- return TxSetXDRFrame::makeFromWire(txSet);
+ return TxSetXDRFrame::makeFromWire(std::move(txSet));
}
TxSetXDRFrameConstPtr
@@ -1034,7 +1115,7 @@ TxSetXDRFrame::makeFromHistoryTransactions(Hash const& previousLedgerHash,
{
TransactionSet txSet;
transactionsToTransactionSetXDR(txs, previousLedgerHash, txSet);
- return TxSetXDRFrame::makeFromWire(txSet);
+ return TxSetXDRFrame::makeFromWire(std::move(txSet));
}
#ifdef BUILD_TESTS
@@ -1367,6 +1448,68 @@ TxSetXDRFrame::createTransactionFrames(Hash const& networkID) const
return phaseTxs;
}
+void
+TxSetXDRFrame::forEachTransactionEnvelope(
+ std::function const& f) const
+{
+ if (isGeneralizedTxSet())
+ {
+ auto const& txSet =
+ std::get(mXDRTxSet).v1TxSet();
+ for (auto const& phase : txSet.phases)
+ {
+ switch (phase.v())
+ {
+ case 0:
+ for (auto const& component : phase.v0Components())
+ {
+ for (auto const& tx : component.txsMaybeDiscountedFee().txs)
+ {
+ f(tx);
+ }
+ }
+ break;
+ case 1:
+ for (auto const& stage :
+ phase.parallelTxsComponent().executionStages)
+ {
+ for (auto const& cluster : stage)
+ {
+ for (auto const& tx : cluster)
+ {
+ f(tx);
+ }
+ }
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ else
+ {
+ for (auto const& tx : std::get(mXDRTxSet).txs)
+ {
+ f(tx);
+ }
+ }
+}
+
+std::vector
+TxSetXDRFrame::getTransactionFullHashes() const
+{
+ ZoneScoped;
+ std::vector hashes;
+ hashes.reserve(sizeTxTotal());
+ // Same digest as TransactionFrame::getFullHash and
+ // FeeBumpTransactionFrame::getFullHash: SHA-256 of the envelope's XDR.
+ forEachTransactionEnvelope([&hashes](TransactionEnvelope const& tx) {
+ hashes.emplace_back(xdrSha256(tx));
+ });
+ return hashes;
+}
+
size_t
TxSetXDRFrame::encodedSize() const
{
@@ -1387,6 +1530,13 @@ TxSetXDRFrame::toXDR(GeneralizedTransactionSet& txSet) const
txSet = std::get(mXDRTxSet);
}
+GeneralizedTransactionSet const&
+TxSetXDRFrame::getGeneralizedXDR() const
+{
+ releaseAssert(isGeneralizedTxSet());
+ return std::get(mXDRTxSet);
+}
+
void
TxSetXDRFrame::storeXDR(StoredTransactionSet& txSet) const
{
@@ -2435,13 +2585,13 @@ ApplicableTxSetFrame::toWireTxSetFrame() const
{
GeneralizedTransactionSet xdrTxSet;
toXDR(xdrTxSet);
- outputTxSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
+ outputTxSet = TxSetXDRFrame::makeFromWire(std::move(xdrTxSet));
}
else
{
TransactionSet xdrTxSet;
toXDR(xdrTxSet);
- outputTxSet = TxSetXDRFrame::makeFromWire(xdrTxSet);
+ outputTxSet = TxSetXDRFrame::makeFromWire(std::move(xdrTxSet));
}
return outputTxSet;
}
diff --git a/src/herder/TxSetFrame.h b/src/herder/TxSetFrame.h
index fbf5e182c2..8bbb4c1ac4 100644
--- a/src/herder/TxSetFrame.h
+++ b/src/herder/TxSetFrame.h
@@ -26,6 +26,7 @@ class Application;
class TxSetXDRFrame;
class ApplicableTxSetFrame;
using TxSetXDRFrameConstPtr = std::shared_ptr;
+using TxSetReadyCallback = std::function;
using ApplicableTxSetFrameConstPtr =
std::unique_ptr;
@@ -117,6 +118,12 @@ using PerPhaseTransactionList = std::vector;
// close time.
// The result is guaranteed to pass `checkValid` check with the same
// arguments as in this method, so additional validation is not needed.
+// With `validTxTargets` (one entry per phase), the transactions of each phase
+// must be in priority order, and only the prefix of phase `i` that holds
+// `validTxTargets[i]` valid transactions of its generic surge pricing lane is
+// validated and offered to selection (see `TxSetUtils::trimInvalidPrefix`).
+// The transactions past that prefix are left out without being validated, so
+// they appear neither in the result nor in `invalidTxs`.
//
// **Note**: the output `ApplicableTxSetFrame` will *not* contain the input
// transaction pointers.
@@ -152,7 +159,9 @@ makeTxSetFromTransactions(
txtest::ParallelSorobanOrder const& parallelSorobanOrder = {},
bool disableTxValidationForLegacyScenario = false
#endif
-);
+ ,
+ TxSetReadyCallback const& onTxSetReady = {},
+ std::vector const& validTxTargets = {});
#ifdef BUILD_TESTS
std::pair
@@ -197,6 +206,11 @@ class TxSetXDRFrame : public NonMovableOrCopyable
static TxSetXDRFrameConstPtr makeFromWire(TransactionSet const& xdrTxSet);
static TxSetXDRFrameConstPtr
makeFromWire(GeneralizedTransactionSet const& xdrTxSet);
+ // Take ownership of the XDR instead of copying it (a large set has
+ // thousands of nested allocations).
+ static TxSetXDRFrameConstPtr makeFromWire(TransactionSet&& xdrTxSet);
+ static TxSetXDRFrameConstPtr
+ makeFromWire(GeneralizedTransactionSet&& xdrTxSet);
static TxSetXDRFrameConstPtr
makeFromStoredTxSet(StoredTransactionSet const& storedSet);
@@ -213,6 +227,8 @@ class TxSetXDRFrame : public NonMovableOrCopyable
void toXDR(TransactionSet& set) const;
void toXDR(GeneralizedTransactionSet& generalizedTxSet) const;
void storeXDR(StoredTransactionSet& txSet) const;
+ // The generalized set's XDR without copying it; the frame is immutable.
+ GeneralizedTransactionSet const& getGeneralizedXDR() const;
~TxSetXDRFrame() = default;
@@ -261,6 +277,16 @@ class TxSetXDRFrame : public NonMovableOrCopyable
PerPhaseTransactionList
createTransactionFrames(Hash const& networkID) const;
+ // Calls `f` with the envelope of every transaction in the set, in the
+ // order createTransactionFrames returns their frames, without building
+ // any frames (which copies every envelope).
+ void forEachTransactionEnvelope(
+ std::function const& f) const;
+
+ // Full hashes (`TransactionFrameBase::getFullHash`) of every transaction
+ // in the set, in the same order, hashed directly from the XDR.
+ std::vector getTransactionFullHashes() const;
+
#ifdef BUILD_TESTS
mutable ApplicableTxSetFrameConstPtr mApplicableTxSetOverride;
@@ -270,6 +296,8 @@ class TxSetXDRFrame : public NonMovableOrCopyable
private:
TxSetXDRFrame(TransactionSet const& xdrTxSet);
TxSetXDRFrame(GeneralizedTransactionSet const& xdrTxSet);
+ TxSetXDRFrame(TransactionSet&& xdrTxSet);
+ TxSetXDRFrame(GeneralizedTransactionSet&& xdrTxSet);
std::variant mXDRTxSet;
size_t mEncodedSize{};
@@ -395,7 +423,9 @@ class TxSetPhaseFrame
txtest::ParallelSorobanOrder const& parallelSorobanOrder,
bool disableTxValidationForLegacyScenario
#endif
- );
+ ,
+ TxSetReadyCallback const& onTxSetReady,
+ std::vector const& validTxTargets);
#ifdef BUILD_TESTS
friend std::pair
makeTxSetFromTransactions(
@@ -569,7 +599,9 @@ class ApplicableTxSetFrame
txtest::ParallelSorobanOrder const& parallelSorobanOrder,
bool disableTxValidationForLegacyScenario
#endif
- );
+ ,
+ TxSetReadyCallback const& onTxSetReady,
+ std::vector const& validTxTargets);
#ifdef BUILD_TESTS
friend std::pair
makeTxSetFromTransactions(
diff --git a/src/herder/TxSetUtils.cpp b/src/herder/TxSetUtils.cpp
index 80df5f4d63..60ee51e0ca 100644
--- a/src/herder/TxSetUtils.cpp
+++ b/src/herder/TxSetUtils.cpp
@@ -41,6 +41,10 @@ namespace
TxFrameList
removeTxs(TxFrameList const& txs, TxFrameList const& txsToRemove)
{
+ if (txsToRemove.empty())
+ {
+ return txs;
+ }
UnorderedSet txsToRemoveSet;
txsToRemoveSet.reserve(txsToRemove.size());
std::transform(
@@ -238,6 +242,12 @@ TxSetUtils::getInvalidTxListWithErrors(
auto diagnostics = DiagnosticEventManager::createDisabled();
for (size_t i = begin; i < end; ++i)
{
+ // The main thread needs every candidate's full hash next
+ // (removing the invalid ones, sorting the set). Compute and
+ // 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 res = txs[i]->checkValid(appConnector, view, 0,
lowerBoundCloseTimeOffset,
upperBoundCloseTimeOffset,
@@ -333,4 +343,49 @@ TxSetUtils::trimInvalid(TxFrameList const& txs, Application& app,
return removeTxs(txs, invalidTxs);
}
+TxFrameList
+TxSetUtils::trimInvalidPrefix(
+ TxFrameList const& txs, size_t targetValidTxs,
+ std::function const& countsTowardTarget,
+ Application& app, UnorderedMap& accountFeeMap,
+ uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset,
+ TxFrameList& invalidTxs)
+{
+ ZoneScoped;
+ TxFrameList validTxs;
+ invalidTxs.clear();
+ size_t counted = 0;
+ size_t validated = 0;
+ // Each pass validates the candidates right after the previous ones, so
+ // fee-source totals accumulate in the same order as when validating the
+ // whole prefix at once. The last pass validates everything left, so
+ // there are at most three passes.
+ for (int pass = 0;
+ pass < 3 && counted < targetValidTxs && validated < txs.size(); ++pass)
+ {
+ size_t const missing = targetValidTxs - counted;
+ size_t const left = txs.size() - validated;
+ size_t const count = pass == 0 ? std::min(missing, left)
+ : pass == 1 ? std::min(2 * missing, left)
+ : left;
+ TxFrameList const batch(txs.begin() + validated,
+ txs.begin() + validated + count);
+ validated += count;
+ TxFrameList batchInvalid;
+ for (auto const& tx :
+ trimInvalid(batch, app, accountFeeMap, lowerBoundCloseTimeOffset,
+ upperBoundCloseTimeOffset, batchInvalid))
+ {
+ if (!countsTowardTarget || countsTowardTarget(*tx))
+ {
+ ++counted;
+ }
+ validTxs.push_back(tx);
+ }
+ invalidTxs.insert(invalidTxs.end(), batchInvalid.begin(),
+ batchInvalid.end());
+ }
+ return validTxs;
+}
+
} // namespace stellar
diff --git a/src/herder/TxSetUtils.h b/src/herder/TxSetUtils.h
index 160ae09eb9..8ac855ab0c 100644
--- a/src/herder/TxSetUtils.h
+++ b/src/herder/TxSetUtils.h
@@ -7,6 +7,7 @@
#include "herder/TxSetFrame.h"
#include "util/UnorderedMap.h"
#include "xdr/Stellar-types.h"
+#include
#include
#include
@@ -62,5 +63,20 @@ class TxSetUtils
UnorderedMap& accountFeeMap,
uint64_t lowerBoundCloseTimeOffset,
uint64_t upperBoundCloseTimeOffset, TxFrameList& invalidTxs);
+
+ // Like `trimInvalid`, for candidates in priority order when only the
+ // first `targetValidTxs` valid ones that `countsTowardTarget` accepts are
+ // needed. Validates a prefix of `txs` in at most three passes: the first
+ // `targetValidTxs` candidates, then twice as many more as are still
+ // missing, then all the rest. Returns the valid candidates of that prefix
+ // in order, and its invalid ones in `invalidTxs`. Candidates past the
+ // prefix are neither validated nor returned.
+ static TxFrameList trimInvalidPrefix(
+ TxFrameList const& txs, size_t targetValidTxs,
+ std::function const&
+ countsTowardTarget,
+ Application& app, UnorderedMap& accountFeeMap,
+ uint64_t lowerBoundCloseTimeOffset, uint64_t upperBoundCloseTimeOffset,
+ TxFrameList& invalidTxs);
}; // class TxSetUtils
} // namespace stellar
diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp
index 4fc6d21658..f151cbe81c 100644
--- a/src/herder/test/HerderTests.cpp
+++ b/src/herder/test/HerderTests.cpp
@@ -11,11 +11,13 @@
#include "scp/LocalNode.h"
#include "scp/SCP.h"
#include "scp/Slot.h"
+#include "simulation/ApplyLoad.h"
#include "simulation/Simulation.h"
#include "simulation/Topologies.h"
#include "test/TestAccount.h"
#include "test/TestUtils.h"
#include "test/test.h"
+#include "util/BatchExecutor.h"
#include "util/JitterInjection.h"
#include "history/HistoryArchiveManager.h"
@@ -33,6 +35,7 @@
#include "lib/json/json.h"
#include "main/CommandHandler.h"
#include "main/PersistentState.h"
+#include "overlay/OverlayIPC.h"
#include "overlay/OverlayMetrics.h"
#include "overlay/RustOverlayManager.h"
#include "test/Catch2.h"
@@ -2672,23 +2675,15 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
return makeTxUpgradePair(herder, txSet, closeTime, emptyUpgradeSteps);
};
auto makeEnvelope = [&s](HerderImpl& herder, TxPair const& p, Hash qSetHash,
- uint64_t slotIndex, bool nomination) {
+ uint64_t slotIndex) {
// herder must want the TxSet before receiving it, so we are sending it
// fake envelope
auto envelope = SCPEnvelope{};
envelope.statement.slotIndex = slotIndex;
- if (nomination)
- {
- envelope.statement.pledges.type(SCP_ST_NOMINATE);
- envelope.statement.pledges.nominate().votes.push_back(p.first);
- envelope.statement.pledges.nominate().quorumSetHash = qSetHash;
- }
- else
- {
- envelope.statement.pledges.type(SCP_ST_PREPARE);
- envelope.statement.pledges.prepare().ballot.value = p.first;
- envelope.statement.pledges.prepare().quorumSetHash = qSetHash;
- }
+ envelope.statement.pledges.type(SCP_ST_PREPARE);
+ envelope.statement.pledges.prepare().ballot.counter = 1;
+ envelope.statement.pledges.prepare().ballot.value = p.first;
+ envelope.statement.pledges.prepare().quorumSetHash = qSetHash;
envelope.statement.nodeID = s.getPublicKey();
herder.signEnvelope(s, envelope);
return envelope;
@@ -2705,156 +2700,6 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
return makeTxSetFromTransactions(txs, *app, ApplyTimeOffset{});
};
- SECTION("combineCandidates")
- {
- auto& herder = static_cast(app->getHerder());
-
- ValueWrapperPtrSet candidates;
-
- auto addToCandidates = [&](TxPair const& p) {
- auto envelope = makeEnvelope(
- herder, p, {}, herder.trackingConsensusLedgerIndex() + 1, true);
- REQUIRE(herder.recvSCPEnvelope(envelope) ==
- Herder::ENVELOPE_STATUS_FETCHING);
- REQUIRE(herder.recvTxSet(p.second->getContentsHash(), p.second));
- auto v = herder.getHerderSCPDriver().wrapValue(p.first);
- candidates.emplace(v);
- };
-
- struct CandidateSpec
- {
- int const n;
- int const nbOps;
- uint32 const feeMulti;
- TimePoint const closeTime;
- std::optional const baseFeeIncrement;
- };
-
- std::vector txSetHashes;
- std::vector txSetSizes;
- std::vector txSetOpSizes;
- std::vector closeTimes;
- std::vector baseFees;
-
- auto addCandidateThenTest = [&](CandidateSpec const& spec) {
- // Create a transaction set using the given parameters, combine
- // it with the given closeTime and optionally a given base fee
- // increment, and make it into a StellarValue to add to the list
- // of candidates so far. Keep track of the hashes and sizes and
- // operation sizes of all the transaction sets, all of the close
- // times, and all of the base fee upgrades that we've seen, so that
- // we can compute the expected result of combining all the
- // candidates so far. (We're using base fees simply as one example
- // of a type of upgrade, whose expected result is the maximum of all
- // candidates'.)
- auto [txSet, applicableTxSet] =
- makeTransactions(spec.n, spec.nbOps, spec.feeMulti);
- txSetHashes.push_back(txSet->getContentsHash());
- txSetSizes.push_back(applicableTxSet->size(lcl.header));
- txSetOpSizes.push_back(applicableTxSet->sizeOpTotal());
- closeTimes.push_back(spec.closeTime);
- if (spec.baseFeeIncrement)
- {
- auto const baseFee =
- lcl.header.baseFee + *spec.baseFeeIncrement;
- baseFees.push_back(baseFee);
- LedgerUpgrade ledgerUpgrade;
- ledgerUpgrade.type(LEDGER_UPGRADE_BASE_FEE);
- ledgerUpgrade.newBaseFee() = baseFee;
- Value upgrade(xdr::xdr_to_opaque(ledgerUpgrade));
- SVUpgrades upgrades;
- upgrades.emplace_back(upgrade.begin(), upgrade.end());
- addToCandidates(
- makeTxUpgradePair(herder, txSet, spec.closeTime, upgrades));
- }
- else
- {
- addToCandidates(makeTxPair(herder, txSet, spec.closeTime));
- }
-
- // Compute the expected transaction set, close time, and upgrade
- // vector resulting from combining all the candidates so far.
- auto const bestTxSetIndex = std::distance(
- txSetSizes.begin(),
- std::max_element(txSetSizes.begin(), txSetSizes.end()));
- REQUIRE(txSetSizes.size() == closeTimes.size());
- auto const expectedHash = txSetHashes[bestTxSetIndex];
- auto const expectedCloseTime = closeTimes[bestTxSetIndex];
- SVUpgrades expectedUpgradeVector;
- if (!baseFees.empty())
- {
- LedgerUpgrade expectedLedgerUpgrade;
- expectedLedgerUpgrade.type(LEDGER_UPGRADE_BASE_FEE);
- expectedLedgerUpgrade.newBaseFee() =
- *std::max_element(baseFees.begin(), baseFees.end());
- Value const expectedUpgradeValue(
- xdr::xdr_to_opaque(expectedLedgerUpgrade));
- expectedUpgradeVector.emplace_back(expectedUpgradeValue.begin(),
- expectedUpgradeValue.end());
- }
-
- // Combine all the candidates seen so far, and extract the
- // returned StellarValue.
- ValueWrapperPtr v =
- herder.getHerderSCPDriver().combineCandidates(1, candidates);
- StellarValue sv;
- xdr::xdr_from_opaque(v->getValue(), sv);
-
- // Compare the returned StellarValue's contents with the
- // expected ones that we computed above.
- REQUIRE(isSignedStellarValue(sv));
- REQUIRE(sv.txSetHash == expectedHash);
- REQUIRE(sv.closeTime == expectedCloseTime);
- REQUIRE(sv.upgrades == expectedUpgradeVector);
- };
-
- // Test some list of candidates, comparing the output of
- // combineCandidates() and the one we compute at each step.
-
- std::vector const specs{
- {0, 1, 100, 10, std::nullopt},
- {10, 1, 100, 5, std::make_optional(1)},
- {5, 3, 100, 20, std::make_optional(2)},
- {7, 2, 5, 30, std::make_optional(3)}};
-
- std::for_each(specs.begin(), specs.end(), addCandidateThenTest);
-
- auto const bestTxSetIndex = std::distance(
- txSetSizes.begin(),
- std::max_element(txSetSizes.begin(), txSetSizes.end()));
- REQUIRE(txSetOpSizes[bestTxSetIndex] == expectedOps);
-
- auto txSetL = makeTransactions(maxTxSetSize, 1, 101).first;
- addToCandidates(makeTxPair(herder, txSetL, 20));
- auto txSetL2 = makeTransactions(maxTxSetSize, 1, 1000).first;
- addToCandidates(makeTxPair(herder, txSetL2, 20));
- auto v = herder.getHerderSCPDriver().combineCandidates(1, candidates);
- StellarValue sv;
- xdr::xdr_from_opaque(v->getValue(), sv);
- REQUIRE(isSignedStellarValue(sv));
- REQUIRE(sv.txSetHash == txSetL2->getContentsHash());
-
- if (protocolVersionStartsFrom(protocolVersion,
- MS_CLOSE_TIME_PROTOCOL_VERSION))
- {
- // Test that the winning candidate keeps its full ms close time
- // along with the redundant whole-second close time.
- auto txSetL3 = makeTransactions(maxTxSetSize, 1, 2000).first;
- StellarValue msVal = herder.makeStellarValue(
- txSetL3->getContentsHash(), makeConsensusTime(20, 7),
- emptyUpgradeSteps, root->getSecretKey());
- addToCandidates(TxPair{xdr::xdr_to_opaque(msVal), txSetL3});
- auto v2 =
- herder.getHerderSCPDriver().combineCandidates(1, candidates);
- StellarValue sv2;
- xdr::xdr_from_opaque(v2->getValue(), sv2);
- REQUIRE(sv2.ext.v() == STELLAR_VALUE_SIGNED_MS);
- REQUIRE(sv2.closeTime == 20);
- REQUIRE(getConsensusTime(sv2).milliseconds() == 20007);
- REQUIRE(sv2.txSetHash == txSetL3->getContentsHash());
- }
- }
-
SECTION("validateValue signatures")
{
auto& herder = static_cast(app->getHerder());
@@ -2866,7 +2711,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
{
// make sure that txSet0 is loaded
auto p = makeTxPair(herder, txSet0, ct);
- auto envelope = makeEnvelope(herder, p, {}, seq, true);
+ auto envelope = makeEnvelope(herder, p, {}, seq);
REQUIRE(herder.recvSCPEnvelope(envelope) ==
Herder::ENVELOPE_STATUS_FETCHING);
REQUIRE(herder.recvTxSet(txSet0->getContentsHash(), txSet0));
@@ -2884,19 +2729,19 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
}
SECTION("invalid")
{
- auto checkInvalid = [&](StellarValue const& sv, bool nomination) {
+ auto checkInvalid = [&](StellarValue const& sv, bool proposal) {
auto v = xdr::xdr_to_opaque(sv);
- REQUIRE(scp.validateValue(seq, v, nomination) ==
+ REQUIRE(scp.validateValue(seq, v, proposal) ==
SCPDriver::kInvalidValue);
};
- auto testInvalidValue = [&](bool isNomination) {
+ auto testInvalidValue = [&](bool isProposal) {
SECTION("basic value")
{
auto basicVal =
StellarValue(txSet0->getContentsHash(), ct,
emptyUpgradeSteps, STELLAR_VALUE_BASIC);
- checkInvalid(basicVal, isNomination);
+ checkInvalid(basicVal, isProposal);
}
SECTION("signed value")
{
@@ -2908,28 +2753,28 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
SECTION("missing signature")
{
getLcValueSignature(sv).signature.clear();
- checkInvalid(sv, isNomination);
+ checkInvalid(sv, isProposal);
}
SECTION("wrong signature")
{
getLcValueSignature(sv).signature[0] ^= 1;
- checkInvalid(sv, isNomination);
+ checkInvalid(sv, isProposal);
}
SECTION("wrong signature 2")
{
getLcValueSignature(sv).nodeID.ed25519()[0] ^= 1;
- checkInvalid(sv, isNomination);
+ checkInvalid(sv, isProposal);
}
}
};
- SECTION("nomination")
+ SECTION("proposal")
{
- testInvalidValue(/* isNomination */ true);
+ testInvalidValue(/* isProposal */ true);
}
SECTION("ballot")
{
- testInvalidValue(/* isNomination */ false);
+ testInvalidValue(/* isProposal */ false);
}
}
@@ -2942,10 +2787,6 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
SCPDriver::kInvalidValue);
REQUIRE(scp.validateValue(seq, v, false) ==
SCPDriver::kInvalidValue);
-
- ValueWrapperPtr extracted;
- REQUIRE_NOTHROW(extracted = scp.extractValidValue(seq, v));
- REQUIRE(extracted == nullptr);
};
SECTION("signed value with empty-tx-set hash")
@@ -2982,13 +2823,13 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
// once the protocol allows them. This is the assertion that
// catches an inverted check in deserializeAndValidateStellarValue.
REQUIRE(scp.validateValue(seq, emptyTxSetValue,
- /*nomination=*/false) ==
+ /*proposal=*/false) ==
(allowed ? SCPDriver::kFullyValidatedValue
: SCPDriver::kInvalidValue));
- // Nomination path: empty-tx-set values are rejected by design.
+ // Proposal path: empty-tx-set values are rejected by design.
REQUIRE(scp.validateValue(seq, emptyTxSetValue,
- /*nomination=*/true) ==
+ /*proposal=*/true) ==
SCPDriver::kInvalidValue);
}
}
@@ -3045,7 +2886,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
emptyUpgradeSteps, root->getSecretKey());
auto val = TxPair{xdr::xdr_to_opaque(sv), txSet};
auto const seq = herder.trackingConsensusLedgerIndex() + 1;
- auto envelope = makeEnvelope(herder, val, {}, seq, true);
+ auto envelope = makeEnvelope(herder, val, {}, seq);
REQUIRE(herder.recvSCPEnvelope(envelope) ==
Herder::ENVELOPE_STATUS_FETCHING);
REQUIRE(herder.recvTxSet(txSet->getContentsHash(), txSet));
@@ -3119,7 +2960,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
// All hits during the whole SCP round. One of them is the validity
// check that determines whether or not to replace the transaction set
// with an empty one (CAP-0083).
- uint64_t const expectedHits = 11;
+ uint64_t const expectedHits = 9;
REQUIRE(cache.getCounters().mHits == expectedHits);
// One miss from the initial makeTxSetFromTransactions
REQUIRE(cache.getCounters().mMisses == 1);
@@ -3170,13 +3011,10 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
auto p2 = makeTxPair(herder, transactions1, 10);
// use current + 1 to allow for any value (old values get filtered more)
auto lseq = herder.trackingConsensusLedgerIndex() + 1;
- auto saneEnvelopeQ1T1 =
- makeEnvelope(herder, p1, saneQSet1Hash, lseq, true);
- auto saneEnvelopeQ1T2 =
- makeEnvelope(herder, p2, saneQSet1Hash, lseq, true);
- auto saneEnvelopeQ2T1 =
- makeEnvelope(herder, p1, saneQSet2Hash, lseq, true);
- auto bigEnvelope = makeEnvelope(herder, p1, bigQSetHash, lseq, true);
+ auto saneEnvelopeQ1T1 = makeEnvelope(herder, p1, saneQSet1Hash, lseq);
+ auto saneEnvelopeQ1T2 = makeEnvelope(herder, p2, saneQSet1Hash, lseq);
+ auto saneEnvelopeQ2T1 = makeEnvelope(herder, p1, saneQSet2Hash, lseq);
+ auto bigEnvelope = makeEnvelope(herder, p1, bigQSetHash, lseq);
TxSetXDRFrameConstPtr malformedTxSet;
if (transactions1->isGeneralizedTxSet())
@@ -3201,7 +3039,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
}
auto malformedTxSetPair = makeTxPair(herder, malformedTxSet, 10);
auto malformedTxSetEnvelope =
- makeEnvelope(herder, malformedTxSetPair, saneQSet1Hash, lseq, true);
+ makeEnvelope(herder, malformedTxSetPair, saneQSet1Hash, lseq);
SECTION("return FETCHING until fetched")
{
@@ -3354,7 +3192,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
{
// make sure that txSet0 is loaded
auto p = makeTxPair(herder, txSet0, ct);
- auto envelope = makeEnvelope(herder, p, {}, seq, true);
+ auto envelope = makeEnvelope(herder, p, {}, seq);
REQUIRE(herder.recvSCPEnvelope(envelope) ==
Herder::ENVELOPE_STATUS_FETCHING);
REQUIRE(herder.recvTxSet(txSet0->getContentsHash(), txSet0));
@@ -3362,7 +3200,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
auto check = [&](StellarValue const& sv, bool expectValid) {
auto v = xdr::xdr_to_opaque(sv);
- auto res = scp.validateValue(seq, v, /*nomination=*/true);
+ auto res = scp.validateValue(seq, v, /*proposal=*/true);
if (expectValid)
{
REQUIRE(res == SCPDriver::kFullyValidatedValue);
@@ -3425,14 +3263,14 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
REQUIRE(scp.validateValue(
pastSlot,
xdr::xdr_to_opaque(makeLegacySignedValue(ct)),
- /*nomination=*/false) ==
+ /*proposal=*/false) ==
SCPDriver::kMaybeValidNotCurrentValue);
// Newer-than-externalized close time for an older slot:
// rejected
REQUIRE(scp.validateValue(
pastSlot,
xdr::xdr_to_opaque(makeLegacySignedValue(ct + 60)),
- /*nomination=*/false) == SCPDriver::kInvalidValue);
+ /*proposal=*/false) == SCPDriver::kInvalidValue);
}
SECTION("legacy signed value rejected for future slots")
{
@@ -3442,7 +3280,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
REQUIRE(scp.validateValue(
seq + 1,
xdr::xdr_to_opaque(makeLegacySignedValue(ct)),
- /*nomination=*/false) == SCPDriver::kInvalidValue);
+ /*proposal=*/false) == SCPDriver::kInvalidValue);
}
SECTION("legacy empty-tx-set value rejected for ratifiable slots")
{
@@ -3452,7 +3290,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
xdr::xdr_from_opaque(legacyEmpty, esv);
REQUIRE(esv.ext.v() == STELLAR_VALUE_EMPTY_TX_SET);
REQUIRE(scp.validateValue(seq, legacyEmpty,
- /*nomination=*/false) ==
+ /*proposal=*/false) ==
SCPDriver::kInvalidValue);
}
SECTION("closeTime inconsistent with closeTimeMs")
@@ -3573,7 +3411,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
rand_uniform(1, 999)),
emptyUpgradeSteps, s);
auto val = TxPair{xdr::xdr_to_opaque(msv), txSet};
- auto envelope = makeEnvelope(herder, val, {}, zSeq, true);
+ auto envelope = makeEnvelope(herder, val, {}, zSeq);
REQUIRE(herder.recvSCPEnvelope(envelope) ==
Herder::ENVELOPE_STATUS_FETCHING);
REQUIRE(herder.recvTxSet(txSet->getContentsHash(), txSet));
@@ -3670,7 +3508,7 @@ testSCPDriver(uint32 protocolVersion, uint32_t maxTxSetSize, size_t expectedOps)
// triggers catchup
herder.lostSync();
REQUIRE(scp.validateValue(seq + 1, xdr::xdr_to_opaque(msv),
- /*nomination=*/false) ==
+ /*proposal=*/false) ==
SCPDriver::kMaybeValidNotCurrentValue);
}
}
@@ -3688,64 +3526,6 @@ TEST_CASE("SCP Driver", "[herder][acceptance]")
}
}
-// Test combineCandidates handling of candidates where
-// previousLedgerHash != LCL.hash
-TEST_CASE("combineCandidates with mismatched previousLedgerHash candidate",
- "[herder][bug]")
-{
- Config cfg(getTestConfig());
-
- VirtualClock clock;
- auto app = createTestApplication(clock, cfg);
- auto& herder = dynamic_cast(app->getHerder());
- auto& pe = herder.getPendingEnvelopes();
- auto& driver = herder.getHerderSCPDriver();
-
- auto const& lcl = app->getLedgerManager().getLastClosedLedgerHeader();
- uint32_t const ver = lcl.header.ledgerVersion;
- TimePoint const closeTime = lcl.header.scpValue.closeTime + 1;
- uint64_t const slotIndex = lcl.header.ledgerSeq + 1;
-
- // Two structurally-valid empty tx sets that differ only in
- // previousLedgerHash.
- auto goodTxSet = TxSetXDRFrame::makeEmpty(lcl.hash, ver); // matches LCL
- auto badTxSet = TxSetXDRFrame::makeEmpty(sha256("not the LCL hash"),
- ver); // mismatched
-
- ValueWrapperPtrSet candidates;
- // Register the tx set so combineCandidates' getTxSet() returns it, then add
- // a candidate value referencing it.
- auto addCandidate = [&](TxSetXDRFrameConstPtr const& txSet) {
- pe.addTxSet(txSet->getContentsHash(), slotIndex, txSet);
- StellarValue sv = herder.makeStellarValue(
- txSet->getContentsHash(), makeConsensusTime(closeTime),
- emptyUpgradeSteps, cfg.NODE_SEED);
- candidates.emplace(driver.wrapValue(xdr::xdr_to_opaque(sv)));
- };
- auto combinedTxSetHash = [&]() {
- ValueWrapperPtr result =
- driver.combineCandidates(slotIndex, candidates);
- StellarValue sv;
- xdr::xdr_from_opaque(result->getValue(), sv);
- return sv.txSetHash;
- };
-
- SECTION("prefer applicable candidate over mismatched candidate")
- {
- addCandidate(goodTxSet);
- addCandidate(badTxSet);
- REQUIRE(combinedTxSetHash() == goodTxSet->getContentsHash());
- }
-
- SECTION("all candidates have mismatched previousLedgerHash")
- {
- // If the *only* option is a candidate with a mismatched
- // previousLedgerHash, choose it.
- addCandidate(badTxSet);
- REQUIRE(combinedTxSetHash() == badTxSet->getContentsHash());
- }
-}
-
TEST_CASE("SCP checkpoint", "[catchup][herder]")
{
auto networkID = sha256(getTestConfig().NETWORK_PASSPHRASE);
@@ -4725,11 +4505,14 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]")
auto networkID = sha256(getTestConfig().NETWORK_PASSPHRASE);
// A and B keep closing ledgers in real time while C is fed slot 2 by
// hand. Use a longer close time so that C's slot-2 EXTERNALIZE reaches
- // A and B before they close ledger 3.
+ // A and B before they close ledger 3. C is not connected, so it only has
+ // slot 2's (empty) transaction set once it built one itself, i.e. once it
+ // led a round: with three nodes that can take until round 3 (two leader
+ // timeouts, 3 s).
auto confGen = [](int i) {
auto cfg = getTestConfig(i);
cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true;
- cfg.ARTIFICIALLY_SET_CLOSE_TIME_FOR_TESTING = 5000;
+ cfg.ARTIFICIALLY_SET_CLOSE_TIME_FOR_TESTING = 10000;
return cfg;
};
auto simulation = std::make_shared(networkID, confGen);
@@ -4793,6 +4576,19 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]")
++actualTypes[env.statement.pledges.type()];
}
+ std::string actual;
+ for (auto const& [type, count] : actualTypes)
+ {
+ actual +=
+ fmt::format("type {}: {}; ", static_cast(type), count);
+ }
+ UNSCOPED_INFO("scphistory of ledger "
+ << ledgerNum << " on "
+ << node->getConfig().toShortString(
+ node->getConfig().NODE_SEED.getPublicKey())
+ << ": " << actual << "(LCL "
+ << node->getLedgerManager().getLastClosedLedgerNum()
+ << ")");
return actualTypes == expectedTypes;
};
@@ -4826,13 +4622,29 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]")
std::vector