From 144de6d0e0204eb34bee6777f5e2ce676ae5fae8 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 14 Sep 2026 17:04:42 -0700 Subject: [PATCH 01/39] Batch GETDATA transaction responses on the flooding stream Port 8e926b3c1 onto the current timing and parallel-validation branch. Serve each demand in one asynchronous job, combining complete transaction frames into writes of up to 64 KiB. Preserve existing framing, priorities, retries and standalone writes for larger transactions. Retain only the next batch and current transaction while a peer is backpressured. Cover multi-batch delivery, missing hashes, oversized transactions, exact bytes and hashes, and SCP progress during a blocked TX write. Add an ordered request barrier proving blocked responses do not retain the entire demand; this regression fails before the change and passes after. --- overlay/src/libp2p_overlay.rs | 129 ++++++++++++------- overlay/src/libp2p_overlay/dispatch_tests.rs | 129 +++++++++++++++++++ overlay/src/libp2p_overlay/framed_io.rs | 20 +++ 3 files changed, 234 insertions(+), 44 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 3ec67a470..3c7a6bce2 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -1374,6 +1374,15 @@ async fn send_to_peer_stream_parts( peer_id: PeerId, stream_type: StreamType, parts: &[&[u8]], +) -> io::Result<()> { + send_peer_frames(state, peer_id, stream_type, framed_io::Frames::One(parts)).await +} + +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; @@ -1453,15 +1462,15 @@ async fn send_to_peer_stream_parts( // 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( + return Box::pin(send_peer_frames( state, peer_id, StreamType::LegacyTxSetRequest, - parts, + frames, )) .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 @@ -1867,55 +1876,87 @@ async fn handle_getdata( 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) - }; - - if let Some(tx) = tx { + let state = Arc::clone(state); + let peer = *peer_id; + tokio::spawn(async move { + // One job per demand, retaining 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. + const BATCH_BYTES: usize = 64 * 1024; + let mut batch = Vec::new(); + let mut messages = 0; + for hash in getdata.hashes { + let tx = state.tx_buffer.write().await.get(&hash); + let Some(tx) = tx else { + state + .metrics + .flood_unfulfilled_unknown + .fetch_add(1, Ordering::Relaxed); + trace!( + "TX_GETDATA_MISS: Don't have TX {:02x?}... for {}", + &hash[..4], + peer + ); + continue; + }; 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(); + if batch.len() + 4 + encoded.len() > BATCH_BYTES && !batch.is_empty() { + send_tx_response_batch(&state, peer, &batch, messages).await; + batch.clear(); + messages = 0; + } + // A transaction larger than the batch budget retains the existing + // segmented write, without allocating a second copy of its bytes. + if 4 + encoded.len() > BATCH_BYTES { + let result = send_to_peer_stream(&state, peer, StreamType::Tx, &encoded).await; + record_tx_response_write(&state, peer, result, 1, encoded.len()); + continue; + } + batch.extend_from_slice(&(encoded.len() as u32).to_be_bytes()); + batch.extend_from_slice(&encoded); + messages += 1; + } + if !batch.is_empty() { + send_tx_response_batch(&state, peer, &batch, messages).await; + } + }); +} - 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 - .metrics - .message_write - .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); - } - }); - } else { +async fn send_tx_response_batch(state: &SharedState, peer: PeerId, batch: &[u8], messages: usize) { + let result = + send_peer_frames(state, peer, StreamType::Tx, framed_io::Frames::Batch(batch)).await; + record_tx_response_write(state, peer, result, messages, batch.len() - 4 * messages); +} + +fn record_tx_response_write( + state: &SharedState, + peer: PeerId, + result: io::Result<()>, + messages: usize, + bytes: usize, +) { + 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); + } + Err(e) => { + state + .metrics + .error_write + .fetch_add(messages as u64, Ordering::Relaxed); + warn!("Failed to send {} TXs to {}: {}", messages, peer, e); } } } diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index fde3941fc..5567077fe 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -169,6 +169,135 @@ 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" + ); + 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; +} + #[tokio::test] async fn scp_state_sends_preserve_slot_order_without_blocking_other_peers() { let mut sender = TestNode::start().await; diff --git a/overlay/src/libp2p_overlay/framed_io.rs b/overlay/src/libp2p_overlay/framed_io.rs index 1332b066b..10252140d 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( From c1c653397333bebf55f7d5aab7d5e5bca1889f36 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:39:22 -0800 Subject: [PATCH 02/39] Stop serving a GETDATA demand after a failed response write A batched response job kept looking up and writing the rest of a demand after a write had already failed past send_peer_frames' retries. When a peer disconnects, every remaining batch of every in-flight demand then fails one by one, each with a warning, wasting buffer lookups and log volume at the moment the node is busiest. Move the job into serve_getdata and return at the first failed batch or standalone write. The requester re-demands unanswered hashes from another source after its response timeout, as it already must for a lost peer. Abandoned hashes are logged once at debug level. Add a test serving a multi-batch demand to a peer without streams: only the first batch is attempted. It fails before this change. --- overlay/src/libp2p_overlay.rs | 118 ++++++++++++------- overlay/src/libp2p_overlay/dispatch_tests.rs | 28 +++++ 2 files changed, 103 insertions(+), 43 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 3c7a6bce2..aea1c560e 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -1878,67 +1878,97 @@ async fn handle_getdata( let state = Arc::clone(state); let peer = *peer_id; - tokio::spawn(async move { - // One job per demand, retaining 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. - const BATCH_BYTES: usize = 64 * 1024; - let mut batch = Vec::new(); - let mut messages = 0; - for hash in getdata.hashes { - let tx = state.tx_buffer.write().await.get(&hash); - let Some(tx) = tx else { - state - .metrics - .flood_unfulfilled_unknown - .fetch_add(1, Ordering::Relaxed); - trace!( - "TX_GETDATA_MISS: Don't have TX {:02x?}... for {}", - &hash[..4], - peer - ); - continue; - }; + tokio::spawn(async move { serve_getdata(&state, peer, getdata.hashes).await }); +} + +/// 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. +/// +/// The job stops at the first write that fails after `send_peer_frames`' +/// retries: the peer has usually disconnected, and the requester re-demands +/// unanswered hashes from another source after its response timeout. +async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) { + const BATCH_BYTES: usize = 64 * 1024; + let total = hashes.len(); + let mut batch = Vec::new(); + let mut messages = 0; + for (index, hash) in hashes.into_iter().enumerate() { + let tx = state.tx_buffer.write().await.get(&hash); + let Some(tx) = tx else { state .metrics - .flood_fulfilled + .flood_unfulfilled_unknown .fetch_add(1, Ordering::Relaxed); - let encoded = tx.to_flood_frame(); - if batch.len() + 4 + encoded.len() > BATCH_BYTES && !batch.is_empty() { - send_tx_response_batch(&state, peer, &batch, messages).await; - batch.clear(); - messages = 0; - } - // A transaction larger than the batch budget retains the existing - // segmented write, without allocating a second copy of its bytes. - if 4 + encoded.len() > BATCH_BYTES { - let result = send_to_peer_stream(&state, peer, StreamType::Tx, &encoded).await; - record_tx_response_write(&state, peer, result, 1, encoded.len()); - continue; + trace!( + "TX_GETDATA_MISS: Don't have TX {:02x?}... for {}", + &hash[..4], + peer + ); + continue; + }; + state + .metrics + .flood_fulfilled + .fetch_add(1, Ordering::Relaxed); + let encoded = tx.to_flood_frame(); + if batch.len() + 4 + encoded.len() > BATCH_BYTES && !batch.is_empty() { + if !send_tx_response_batch(state, peer, &batch, messages).await { + abandon_getdata(peer, total - index); + return; } - batch.extend_from_slice(&(encoded.len() as u32).to_be_bytes()); - batch.extend_from_slice(&encoded); - messages += 1; + batch.clear(); + messages = 0; } - if !batch.is_empty() { - send_tx_response_batch(&state, peer, &batch, messages).await; + // A transaction larger than the batch budget retains the existing + // segmented write, without allocating a second copy of its bytes. + if 4 + encoded.len() > BATCH_BYTES { + let result = send_to_peer_stream(state, peer, StreamType::Tx, &encoded).await; + if !record_tx_response_write(state, peer, result, 1, encoded.len()) { + abandon_getdata(peer, total - index - 1); + return; + } + continue; } - }); + batch.extend_from_slice(&(encoded.len() as u32).to_be_bytes()); + batch.extend_from_slice(&encoded); + messages += 1; + } + if !batch.is_empty() { + send_tx_response_batch(state, peer, &batch, messages).await; + } } -async fn send_tx_response_batch(state: &SharedState, peer: PeerId, batch: &[u8], messages: usize) { +/// `remaining` counts requested hashes not yet looked up or written. +fn abandon_getdata(peer: PeerId, remaining: usize) { + if remaining > 0 { + debug!( + "TX_GETDATA_ABANDON: Not serving {} more requested hashes to {} after a failed write", + remaining, peer + ); + } +} + +/// Returns whether the batch was written. +async fn send_tx_response_batch( + state: &SharedState, + peer: PeerId, + batch: &[u8], + messages: usize, +) -> bool { let result = send_peer_frames(state, peer, StreamType::Tx, framed_io::Frames::Batch(batch)).await; - record_tx_response_write(state, peer, result, messages, batch.len() - 4 * messages); + record_tx_response_write(state, peer, result, messages, batch.len() - 4 * messages) } +/// 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 @@ -1950,6 +1980,7 @@ fn record_tx_response_write( .byte_write .fetch_add(bytes as u64, Ordering::Relaxed); debug!("TX_SEND: Sent {} TXs to {}", messages, peer); + true } Err(e) => { state @@ -1957,6 +1988,7 @@ fn record_tx_response_write( .error_write .fetch_add(messages as u64, Ordering::Relaxed); warn!("Failed to send {} TXs to {}: {}", messages, peer, e); + false } } } diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 5567077fe..b4bccc8ea 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -298,6 +298,34 @@ async fn getdata_batches_preserve_frames_and_leave_control_responsive() { 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 looked_up = state.metrics.flood_fulfilled.load(Ordering::Relaxed); + assert!(failed > 0, "the first batch must have been attempted"); + // Only the first batch was written, plus the lookup that closed it. + assert_eq!(looked_up, failed + 1); + assert!( + looked_up < 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 scp_state_sends_preserve_slot_order_without_blocking_other_peers() { let mut sender = TestNode::start().await; From 7127d3a946459c05dd8715935e5f21dab84255d2 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:41:48 -0800 Subject: [PATCH 03/39] Frame GETDATA responses straight from the shared transaction bytes Each served transaction was first framed into a new Vec by to_flood_frame (allocation plus copy), then copied again into the batch, and every hash took and released the TX buffer write lock. A node that originates load serves each transaction to most of its peers, so this runs at several times the network TPS. Copy the 4-byte Transaction discriminant and the shared envelope bytes directly into the batch (ValidatedTx::flood_frame_parts), and look up a whole batch under one buffer lock, released before every write. A transaction larger than the batch budget is written as two parts straight from the shared bytes, with no copy. Wire bytes are unchanged. Pin flood_frame_parts to the typed StellarMessage encoding, and assert in the blocked-writer test that the buffer lock is free while a response write waits (the test times out if the lock is held across the write). --- overlay/src/libp2p_overlay.rs | 32 ++++++++++++++------ overlay/src/libp2p_overlay/dispatch_tests.rs | 5 +++ overlay/src/wire.rs | 9 ++++++ overlay/src/xdr.rs | 5 +++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index aea1c560e..59160ef5d 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -1884,6 +1884,8 @@ async fn handle_getdata( /// 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 buffer lock, released before its write, +/// and frames are copied straight from the shared TX bytes into the batch. /// /// The job stops at the first write that fails after `send_peer_frames`' /// retries: the peer has usually disconnected, and the requester re-demands @@ -1893,8 +1895,13 @@ async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) let total = hashes.len(); let mut batch = Vec::new(); let mut messages = 0; + let mut buffer = None; for (index, hash) in hashes.into_iter().enumerate() { - let tx = state.tx_buffer.write().await.get(&hash); + let tx = match &mut buffer { + Some(guard) => guard, + None => buffer.insert(state.tx_buffer.write().await), + } + .get(&hash); let Some(tx) = tx else { state .metrics @@ -1911,8 +1918,10 @@ async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) .metrics .flood_fulfilled .fetch_add(1, Ordering::Relaxed); - let encoded = tx.to_flood_frame(); - if batch.len() + 4 + encoded.len() > BATCH_BYTES && !batch.is_empty() { + let parts = tx.flood_frame_parts(); + let payload = parts.iter().map(|part| part.len()).sum::(); + if batch.len() + 4 + payload > BATCH_BYTES && !batch.is_empty() { + buffer = None; if !send_tx_response_batch(state, peer, &batch, messages).await { abandon_getdata(peer, total - index); return; @@ -1921,19 +1930,24 @@ async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) messages = 0; } // A transaction larger than the batch budget retains the existing - // segmented write, without allocating a second copy of its bytes. - if 4 + encoded.len() > BATCH_BYTES { - let result = send_to_peer_stream(state, peer, StreamType::Tx, &encoded).await; - if !record_tx_response_write(state, peer, result, 1, encoded.len()) { + // segmented write, straight from the shared TX bytes. + if 4 + payload > BATCH_BYTES { + buffer = None; + let result = send_to_peer_stream_parts(state, peer, StreamType::Tx, &parts).await; + if !record_tx_response_write(state, peer, result, 1, payload) { abandon_getdata(peer, total - index - 1); return; } continue; } - batch.extend_from_slice(&(encoded.len() as u32).to_be_bytes()); - batch.extend_from_slice(&encoded); + // Fits the batch budget, so the length fits the u32 prefix. + batch.extend_from_slice(&(payload as u32).to_be_bytes()); + for part in parts { + batch.extend_from_slice(part); + } messages += 1; } + drop(buffer); if !batch.is_empty() { send_tx_response_batch(state, peer, &batch, messages).await; } diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index b4bccc8ea..99940e26a 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -255,6 +255,11 @@ async fn getdata_batches_preserve_frames_and_leave_control_responsive() { 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 { diff --git a/overlay/src/wire.rs b/overlay/src/wire.rs index 6680c7fa4..3ab51baa9 100644 --- a/overlay/src/wire.rs +++ b/overlay/src/wire.rs @@ -113,6 +113,13 @@ impl ValidatedTx { 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 +201,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 8f0ded0bb..e65607157 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) From b2349c5a0fc95c37dcbad63959c4ac513d29586e Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:43:17 -0800 Subject: [PATCH 04/39] Read inbound TX streams through a 64 KiB buffer GETDATA responses now arrive as batches of complete frames, but the reader still pulled each frame out of QUIC with two reads (the length prefix, then the body), and every quinn read takes the connection lock that the connection driver and the SCP and TxSet streams also use. INV and GETDATA frames on the same stream paid the same cost. Wrap each inbound TX stream in a 64 KiB BufReader, so one read copies whatever QUIC has received (often a whole response batch) and frames are then parsed from memory. read_framed becomes generic over AsyncRead; a frame larger than the buffer is read directly once the buffer drains. Bytes delivered to the handlers are unchanged, and the buffer never waits for more data than is available, so no latency is added. Also drop the unused stream argument from handle_tx_stream_message and handle_getdata (which no longer needs to be async). Add a test reading a batch and oversized frames through a small BufReader over a reader that returns short chunks and yields. --- overlay/src/libp2p_overlay.rs | 30 ++++++------ overlay/src/libp2p_overlay/framed_io.rs | 63 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 59160ef5d..fc16c2630 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -1557,7 +1557,7 @@ 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> { let mut len_buf = [0u8; 4]; stream.read_exact(&mut len_buf).await?; let len = u32::from_be_bytes(len_buf) as usize; @@ -1722,14 +1722,22 @@ async fn handle_inbound_scp_streams( } } +/// Inbound TX streams carry many small frames, and GETDATA responses arrive +/// as batches of them. Reading through this buffer copies whatever QUIC has +/// received in one call, instead of two reads (each taking the connection +/// lock) per frame. Larger frames bypass the buffer once it is empty. +const TX_STREAM_READ_BUFFER_BYTES: usize = 64 * 1024; + /// 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 = + futures::io::BufReader::with_capacity(TX_STREAM_READ_BUFFER_BYTES, stream); loop { match read_framed(&mut stream).await { Ok(data) => { @@ -1739,7 +1747,7 @@ async fn handle_inbound_tx_streams(mut incoming: IncomingStreams, state: Arc { state.metrics.error_read.fetch_add(1, Ordering::Relaxed); @@ -1754,18 +1762,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; @@ -1864,12 +1867,7 @@ 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, diff --git a/overlay/src/libp2p_overlay/framed_io.rs b/overlay/src/libp2p_overlay/framed_io.rs index 10252140d..21290dfa7 100644 --- a/overlay/src/libp2p_overlay/framed_io.rs +++ b/overlay/src/libp2p_overlay/framed_io.rs @@ -46,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}; @@ -100,4 +101,66 @@ 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); + } } From 9e57ea6a5f0ad66a050ad9ebf403bc37e8c95aad Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:44:42 -0800 Subject: [PATCH 05/39] Queue a relayed transaction's INVs under one batcher lock Every transaction received in a GETDATA response is announced to each connected peer that has not advertised it. The relay took and released the INV batcher's write lock once per peer, so on a 30-node mesh each received transaction cost up to 28 write-lock round trips, contending with every other peer's reader task and the housekeeping flush. Add the entry for all peers under one lock, collect the batches that filled up, and send them after releasing it. send_inv_batch only spawns the write, so the batches still go out immediately. Which peers are announced to, and the batching rules, are unchanged. Add a test covering the relay targets (not the sender, not a peer that already advertised the TX, every other peer) and the hand-off of a batch that this TX completes. Export INV_BATCH_MAX_SIZE from flood for it. --- overlay/src/flood/mod.rs | 2 +- overlay/src/libp2p_overlay.rs | 24 +++++--- overlay/src/libp2p_overlay/dispatch_tests.rs | 60 ++++++++++++++++++++ 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/overlay/src/flood/mod.rs b/overlay/src/flood/mod.rs index 9e796310e..59a6adcaa 100644 --- a/overlay/src/flood/mod.rs +++ b/overlay/src/flood/mod.rs @@ -11,7 +11,7 @@ mod tx_buffer; mod txset; 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; diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index fc16c2630..b238b9333 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -2105,15 +2105,21 @@ async fn handle_tx_response(state: &Arc, peer_id: &PeerId, tx: Arc< 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 the INV for every peer under one batcher lock, rather than + // one lock per peer, then send any batches that filled up. + let full_batches: Vec<(PeerId, InvBatch)> = { + let mut batcher = state.inv_batcher.write().await; + peers_to_announce + .iter() + .filter_map(|peer| { + batcher + .add(*peer, inv_entry.clone()) + .map(|batch| (*peer, batch)) + }) + .collect() + }; + for (peer, batch) in full_batches { + send_inv_batch(state, peer, batch).await; } } diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 99940e26a..a0a9aee0d 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -331,6 +331,66 @@ async fn getdata_job_stops_after_a_failed_write() { 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 scp_state_sends_preserve_slot_order_without_blocking_other_peers() { let mut sender = TestNode::start().await; From 106b0473ee579b98b811528b08cf361de4bcc7e0 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:02:09 -0800 Subject: [PATCH 06/39] Copy GETDATA response frames outside the TX buffer lock Review follow-up. Since df7c3213f a response job held the TX buffer write lock while it both looked up and copied up to 64 KiB of frames. broadcast_tx takes the same lock inline in the event loop, so on a node serving many peers a local submission (and any SCP command queued behind it) could wait for several jobs' copies. Serve each batch in two phases: under the lock, only look up the transactions that fit the budget (a hit that does not fit is carried to the next batch); then release it, copy the frames from the shared bytes and write. A lone transaction above the budget is still written as two borrowed parts. Also: - reserve the batch exactly, so its capacity stays within the budget (it grew by doubling to about 117 KB before); - count flood_fulfilled when a transaction is put into a write, so a failed batch no longer counts the lookup that closed it; - share one TX_RESPONSE_BATCH_BYTES constant with the inbound read buffer (buffered_tx_stream); - document lazy per-batch lookup, why a failed write ends the job, and that a retried batch can redeliver frames (dropped via tx_seen). Add a test for the batching edges: an oversized TX as the first hit and another straight after it, frames exactly at the budget, a carried oversized TX at the end and interleaved misses, checking demand order, exact bytes and metrics. It fails if the carried TX is dropped. --- overlay/src/libp2p_overlay.rs | 192 +++++++++++-------- overlay/src/libp2p_overlay/dispatch_tests.rs | 96 +++++++++- overlay/src/wire.rs | 4 +- 3 files changed, 204 insertions(+), 88 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index b238b9333..ad7f46392 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -1367,8 +1367,7 @@ 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, @@ -1378,6 +1377,15 @@ async fn send_to_peer_stream_parts( 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, @@ -1723,10 +1731,13 @@ async fn handle_inbound_scp_streams( } /// Inbound TX streams carry many small frames, and GETDATA responses arrive -/// as batches of them. Reading through this buffer copies whatever QUIC has +/// 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. Larger frames bypass the buffer once it is empty. -const TX_STREAM_READ_BUFFER_BYTES: usize = 64 * 1024; +/// 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) { @@ -1736,8 +1747,7 @@ async fn handle_inbound_tx_streams(mut incoming: IncomingStreams, state: Arc { @@ -1879,98 +1889,114 @@ fn handle_getdata(state: &Arc, peer_id: &PeerId, getdata: GetData) tokio::spawn(async move { serve_getdata(&state, peer, getdata.hashes).await }); } +/// 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::() +} + /// 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 buffer lock, released before its write, -/// and frames are copied straight from the shared TX bytes into the batch. +/// +/// 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: the peer has usually disconnected, and the requester re-demands -/// unanswered hashes from another source after its response timeout. +/// 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]>) { - const BATCH_BYTES: usize = 64 * 1024; - let total = hashes.len(); + 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(); - let mut messages = 0; - let mut buffer = None; - for (index, hash) in hashes.into_iter().enumerate() { - let tx = match &mut buffer { - Some(guard) => guard, - None => buffer.insert(state.tx_buffer.write().await), + loop { + txs.clear(); + let mut bytes = 0; + if let Some(tx) = carried.take() { + bytes += tx_frame_len(&tx); + txs.push(tx); } - .get(&hash); - let Some(tx) = tx else { - state - .metrics - .flood_unfulfilled_unknown - .fetch_add(1, Ordering::Relaxed); - trace!( - "TX_GETDATA_MISS: Don't have TX {:02x?}... for {}", - &hash[..4], - peer - ); - continue; + 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 + .flood_unfulfilled_unknown + .fetch_add(1, Ordering::Relaxed); + 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(1, Ordering::Relaxed); - let parts = tx.flood_frame_parts(); - let payload = parts.iter().map(|part| part.len()).sum::(); - if batch.len() + 4 + payload > BATCH_BYTES && !batch.is_empty() { - buffer = None; - if !send_tx_response_batch(state, peer, &batch, messages).await { - abandon_getdata(peer, total - index); - return; - } - batch.clear(); - messages = 0; - } - // A transaction larger than the batch budget retains the existing - // segmented write, straight from the shared TX bytes. - if 4 + payload > BATCH_BYTES { - buffer = None; + .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; - if !record_tx_response_write(state, peer, result, 1, payload) { - abandon_getdata(peer, total - index - 1); - return; + 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); + } } - continue; - } - // Fits the batch budget, so the length fits the u32 prefix. - 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; } - messages += 1; } - drop(buffer); - if !batch.is_empty() { - send_tx_response_batch(state, peer, &batch, messages).await; - } -} - -/// `remaining` counts requested hashes not yet looked up or written. -fn abandon_getdata(peer: PeerId, remaining: usize) { - if remaining > 0 { - debug!( - "TX_GETDATA_ABANDON: Not serving {} more requested hashes to {} after a failed write", - remaining, peer - ); - } -} - -/// Returns whether the batch was written. -async fn send_tx_response_batch( - state: &SharedState, - peer: PeerId, - batch: &[u8], - messages: usize, -) -> bool { - let result = - send_peer_frames(state, peer, StreamType::Tx, framed_io::Frames::Batch(batch)).await; - record_tx_response_write(state, peer, result, messages, batch.len() - 4 * messages) } /// Returns whether the write succeeded. diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index a0a9aee0d..448d055c1 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -303,6 +303,95 @@ async fn getdata_batches_preserve_frames_and_leave_control_responsive() { 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 getdata_job_stops_after_a_failed_write() { let (_handle, _events, _admissions, overlay) = @@ -320,12 +409,11 @@ async fn getdata_job_stops_after_a_failed_write() { serve_getdata(&state, PeerId::random(), hashes.clone()).await; let failed = state.metrics.error_write.load(Ordering::Relaxed); - let looked_up = state.metrics.flood_fulfilled.load(Ordering::Relaxed); + let attempted = state.metrics.flood_fulfilled.load(Ordering::Relaxed); assert!(failed > 0, "the first batch must have been attempted"); - // Only the first batch was written, plus the lookup that closed it. - assert_eq!(looked_up, failed + 1); + assert_eq!(attempted, failed, "only the first batch was attempted"); assert!( - looked_up < hashes.len() as u64, + 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); diff --git a/overlay/src/wire.rs b/overlay/src/wire.rs index 3ab51baa9..b5332e2ef 100644 --- a/overlay/src/wire.rs +++ b/overlay/src/wire.rs @@ -109,7 +109,9 @@ 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) } From d676ffa49804850af26a77e7521c9f106462a0c7 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:03:53 -0800 Subject: [PATCH 07/39] Test GETDATA batch retries and buffered TX stream reads Review follow-up: two behaviours had no test. - A batch whose write fails is rewritten whole on a reopened stream. Existing tests only reopened streams that were already empty. The new test finishes the sender's cached TX stream, serves a demand spanning several batches, and checks every transaction arrives once, in order, with no write errors and no duplicates at the receiver. It fails with send_peer_frames' retries disabled. - The inbound TX stream buffer (buffered_tx_stream) takes in everything available in one inner read and returns each complete frame without waiting for more data; only a read past the available data waits. This pins the "adds no latency" property of 90bc974c4. --- overlay/src/libp2p_overlay/dispatch_tests.rs | 56 ++++++++++++++++++++ overlay/src/libp2p_overlay/framed_io.rs | 48 +++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 448d055c1..c5bb87664 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -392,6 +392,62 @@ async fn getdata_batch_edges_preserve_order_and_bytes() { 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) = diff --git a/overlay/src/libp2p_overlay/framed_io.rs b/overlay/src/libp2p_overlay/framed_io.rs index 21290dfa7..b5a9e8e84 100644 --- a/overlay/src/libp2p_overlay/framed_io.rs +++ b/overlay/src/libp2p_overlay/framed_io.rs @@ -163,4 +163,52 @@ mod tests { 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()); + } } From 5dcbcebe9430217fce45947e0804a71bd6e18012 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:05:54 -0800 Subject: [PATCH 08/39] Queue locally submitted INVs under one batcher lock too Review follow-up to 39f3d5570, which batched the INV batcher lock only for transactions received from peers. broadcast_tx, which announces transactions submitted by the local core, still took the write lock once per peer, and it runs inline in the overlay event loop that also carries SCP commands, so its lock round trips cost the most. Move the logic into queue_inv and use it from both paths: add the entry for every peer under one lock, then send the batches it completed after releasing it. The test added in 39f3d5570 only checked that a completed batch left the batcher, so it would pass if that batch were then dropped. Add a test over a real connection: fill the receiver's pending batch to one short of full, complete it through the relay path and then through a local broadcast, and wait for the receiver to demand all 1000 hashes each time. The housekeeping flush needs 100 ms, so only the completed batch's own send can deliver them; the test fails if that send is removed. 30 repeated runs of the new tests: no failures. --- overlay/src/libp2p_overlay.rs | 52 +++++++++----------- overlay/src/libp2p_overlay/dispatch_tests.rs | 52 ++++++++++++++++++++ 2 files changed, 74 insertions(+), 30 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index ad7f46392..47f68c6ff 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -999,18 +999,7 @@ 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 @@ -1523,6 +1512,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; @@ -2129,24 +2138,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 }; - - // Queue the INV for every peer under one batcher lock, rather than - // one lock per peer, then send any batches that filled up. - let full_batches: Vec<(PeerId, InvBatch)> = { - let mut batcher = state.inv_batcher.write().await; - peers_to_announce - .iter() - .filter_map(|peer| { - batcher - .add(*peer, inv_entry.clone()) - .map(|batch| (*peer, batch)) - }) - .collect() - }; - for (peer, batch) in full_batches { - send_inv_batch(state, peer, batch).await; - } + queue_inv(state, &peers_to_announce, InvEntry { hash, fee_per_op }).await; } // Record recv-transaction timing diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index c5bb87664..164c987fa 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -535,6 +535,58 @@ async fn received_tx_is_announced_to_every_peer_except_known_sources() { ); } +#[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 scp_state_sends_preserve_slot_order_without_blocking_other_peers() { let mut sender = TestNode::start().await; From 21dc17ea68bb42e2eac6718843e6a05d1bd5cbf9 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 14 Sep 2026 23:20:38 -0700 Subject: [PATCH 09/39] Select loadgen source accounts in constant time Port the focused account-pool change from 75f3fbc05e onto batched-getdata. Use uniform indexed selection and swap-removal instead of walking the available-account hash set for every transaction. Preserve release only after application, including Soroban setup transitions. Cover repeated account recycling and consecutive runs using real ledger application, with more submissions than available accounts and no rejected transactions. --- src/simulation/LoadGenerator.cpp | 19 +++++++++-------- src/simulation/LoadGenerator.h | 5 ++++- src/simulation/test/LoadGeneratorTests.cpp | 24 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index b9b1191f3..ece7879fc 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -248,7 +248,7 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, { if (it->second <= lcl) { - mAccountsAvailable.insert(it->first); + mAccountsAvailable.push_back(it->first); it = mAccountsExternalized.erase(it); } else @@ -414,12 +414,13 @@ LoadGenerator::start(GeneratedLoadConfig& cfg) if (cfg.mode != LoadGenMode::PAY_PREGENERATED) { + mAccountsAvailable.reserve(cfg.nAccounts); // For upgrade modes, use root account (represented by special ID) uint32_t accounts = cfg.nAccounts; if (cfg.mode == LoadGenMode::SOROBAN_UPGRADE_SETUP || cfg.mode == LoadGenMode::SOROBAN_CREATE_UPGRADE) { - mAccountsAvailable.insert(TxGenerator::ROOT_ACCOUNT_ID); + mAccountsAvailable.push_back(TxGenerator::ROOT_ACCOUNT_ID); if (accounts) { accounts--; @@ -429,7 +430,7 @@ LoadGenerator::start(GeneratedLoadConfig& cfg) // Mark all accounts "available" as source accounts for (auto i = 0u; i < accounts; i++) { - mAccountsAvailable.insert(i + cfg.offset); + mAccountsAvailable.push_back(i + cfg.offset); } if (cfg.modeInvokes()) @@ -1026,10 +1027,9 @@ LoadGenerator::getNextAvailableAccount(uint32_t ledgerNum) auto sourceAccountIdx = rand_uniform(0, mAccountsAvailable.size() - 1); - auto it = mAccountsAvailable.begin(); - std::advance(it, sourceAccountIdx); - sourceAccountId = *it; - mAccountsAvailable.erase(it); + sourceAccountId = mAccountsAvailable[sourceAccountIdx]; + mAccountsAvailable[sourceAccountIdx] = mAccountsAvailable.back(); + mAccountsAvailable.pop_back(); releaseAssert(mAccountsInUse.insert(sourceAccountId).second); return sourceAccountId; @@ -1428,11 +1428,12 @@ LoadGenerator::waitTillComplete(GeneratedLoadConfig cfg) // The first setup phase has fully applied, so its source accounts are // safe to reuse for the instance-creation phase. - mAccountsAvailable.insert(mAccountsInUse.begin(), mAccountsInUse.end()); + mAccountsAvailable.insert(mAccountsAvailable.end(), + mAccountsInUse.begin(), mAccountsInUse.end()); mAccountsInUse.clear(); for (auto const& kv : mAccountsExternalized) { - mAccountsAvailable.insert(kv.first); + mAccountsAvailable.push_back(kv.first); } mAccountsExternalized.clear(); diff --git a/src/simulation/LoadGenerator.h b/src/simulation/LoadGenerator.h index f6051e635..0b865e737 100644 --- a/src/simulation/LoadGenerator.h +++ b/src/simulation/LoadGenerator.h @@ -281,7 +281,10 @@ class LoadGenerator // Accounts whose transaction externalized in the given ledger; released // once that ledger is applied. std::unordered_map mAccountsExternalized; - std::unordered_set mAccountsAvailable; + // Accounts enter this pool only when initialized or released from one of + // the disjoint in-use/externalized collections. A dense vector allows + // uniform random selection and removal without walking the account pool. + std::vector mAccountsAvailable; // First ledger at which load generation stalled because every account had // a pending transaction. std::optional mNoAccountsAvailableSinceLedger; diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index 7ba135721..cf562c0f8 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -328,6 +328,30 @@ TEST_CASE("generate load with unique accounts", "[loadgen]") 120 * simulation->getExpectedLedgerCloseTime(), false); REQUIRE(getSuccessfulTxCount() == nTxs); } + SECTION("recycle available accounts across ledgers and runs") + { + // More submissions than accounts requires returning applied accounts + // to the pool. Real application catches premature reuse through bad + // sequence numbers; a second run also exercises resetting the pool. + uint32_t const txsPerRun = 3 * nAccounts; + auto& completed = + app.getMetrics().NewMeter({"loadgen", "run", "complete"}, "run"); + auto& rejected = + app.getMetrics().NewMeter({"loadgen", "txn", "rejected"}, "txn"); + for (uint32_t run = 0; run < 2; ++run) + { + auto before = getSuccessfulTxCount(); + auto runsBefore = completed.count(); + loadGen.generateLoad(GeneratedLoadConfig::txLoad( + LoadGenMode::PAY, nAccounts, txsPerRun, + /* txRate */ 100, /* offset */ nAccounts)); + simulation->crankUntil( + [&]() { return completed.count() == runsBefore + 1; }, + 120 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(getSuccessfulTxCount() - before == txsPerRun); + REQUIRE(rejected.count() == 0); + } + } SECTION("invalid loadgen parameters") { uint32 numAccounts = 100; From c02730f5af1cdd2fb43b6adc980f5de8702ccc3a Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:42:39 -0800 Subject: [PATCH 10/39] Match externalized loadgen accounts in time proportional to the tx set cleanupAccounts runs on the main thread in valueExternalized for every ledger, on every node, whether or not that node generates load. It hashed and allocated a set entry for the source of every transaction in the externalized set (about 6,000 to 7,500 per ledger in perfnet's 3,000 TPS runs), then walked every in-use account with a std::map lookup. When ledgers fall behind the offered load, the in-use backlog grows by tens of accounts per ledger per generator, so the walk grew with it on the path between externalize and the next ledger. Keep in-use accounts indexed by public key instead. Accounts selected since the previous call are indexed at the start of the next one (their keys are known once TxGenerator has generated a transaction for them), and each externalized transaction's source is looked up in that index. The cost is now one allocation-free lookup per transaction in the set plus one index insert per newly selected account, independent of the backlog, with an early return when no account is waiting. Which accounts are released, and when, is unchanged. Add a test for perfnet's block_time_check load: synthetic SAC payments in overlay-only mode with more transactions than source accounts, which only completes if accounts return to the pool after their ledgers close. --- src/simulation/LoadGenerator.cpp | 58 ++++++++----- src/simulation/LoadGenerator.h | 8 ++ src/simulation/test/LoadGeneratorTests.cpp | 95 ++++++++++++++++++++++ 3 files changed, 142 insertions(+), 19 deletions(-) diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index ece7879fc..0972fa520 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -236,7 +236,6 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, PerPhaseTransactionList const& perPhaseTxs) { ZoneScoped; - auto const& accounts = mTxGenerator.getAccounts(); // An account stays in use until the transaction it submitted has been // applied, so that the next transaction generated for it sees the updated @@ -257,31 +256,47 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, } } - // Accounts whose transaction is in the set externalized for `ledgerSeq` - // are released once that ledger is applied. Accounts whose transaction is - // not in it are still pending in the mempool and remain in use. - UnorderedSet externalized; - for (auto const& txs : perPhaseTxs) + // Index the accounts selected since the last call. A transaction has been + // generated for each of them, so TxGenerator has cached the account. + if (!mAccountsInUseToIndex.empty()) { - for (auto const& tx : txs) + auto const& accounts = mTxGenerator.getAccounts(); + for (auto id : mAccountsInUseToIndex) { - externalized.insert(tx->getSourceID()); + releaseAssert(mAccountsInUse.find(id) != mAccountsInUse.end()); + auto accIt = accounts.find(id); + releaseAssert(accIt != accounts.end()); + releaseAssert( + mAccountsInUseByKey.emplace(accIt->second->getPublicKey(), id) + .second); } + mAccountsInUseToIndex.clear(); } - for (auto it = mAccountsInUse.begin(); it != mAccountsInUse.end();) + // No account is waiting for its transaction, e.g. loadgen is idle. + if (mAccountsInUseByKey.empty()) { - auto accIt = accounts.find(*it); - releaseAssert(accIt != accounts.end()); - if (externalized.find(accIt->second->getPublicKey()) != - externalized.end()) - { - mAccountsExternalized.emplace(*it, ledgerSeq); - it = mAccountsInUse.erase(it); - } - else + return; + } + + // Accounts whose transaction is in the set externalized for `ledgerSeq` + // are released once that ledger is applied. Accounts whose transaction is + // not in it are still pending in the mempool and remain in use. The set + // holds every node's transactions; looking each source up in the in-use + // index keeps this proportional to the set, however large the mempool + // backlog grows, and the lookups allocate nothing. + for (auto const& txs : perPhaseTxs) + { + for (auto const& tx : txs) { - ++it; + auto it = mAccountsInUseByKey.find(tx->getSourceID()); + if (it == mAccountsInUseByKey.end()) + { + continue; + } + mAccountsExternalized.emplace(it->second, ledgerSeq); + mAccountsInUse.erase(it->second); + mAccountsInUseByKey.erase(it); } } } @@ -293,6 +308,8 @@ LoadGenerator::reset() { mTxGenerator.reset(); mAccountsInUse.clear(); + mAccountsInUseByKey.clear(); + mAccountsInUseToIndex.clear(); mAccountsExternalized.clear(); mAccountsAvailable.clear(); mNoAccountsAvailableSinceLedger.reset(); @@ -1031,6 +1048,7 @@ LoadGenerator::getNextAvailableAccount(uint32_t ledgerNum) mAccountsAvailable[sourceAccountIdx] = mAccountsAvailable.back(); mAccountsAvailable.pop_back(); releaseAssert(mAccountsInUse.insert(sourceAccountId).second); + mAccountsInUseToIndex.push_back(sourceAccountId); return sourceAccountId; } @@ -1431,6 +1449,8 @@ LoadGenerator::waitTillComplete(GeneratedLoadConfig cfg) mAccountsAvailable.insert(mAccountsAvailable.end(), mAccountsInUse.begin(), mAccountsInUse.end()); mAccountsInUse.clear(); + mAccountsInUseByKey.clear(); + mAccountsInUseToIndex.clear(); for (auto const& kv : mAccountsExternalized) { mAccountsAvailable.push_back(kv.first); diff --git a/src/simulation/LoadGenerator.h b/src/simulation/LoadGenerator.h index 0b865e737..0664e40ae 100644 --- a/src/simulation/LoadGenerator.h +++ b/src/simulation/LoadGenerator.h @@ -11,6 +11,7 @@ #include "test/TestAccount.h" #include "test/TxTests.h" #include "util/NonCopyable.h" +#include "util/UnorderedMap.h" #include "xdr/Stellar-types.h" #include #include @@ -278,6 +279,13 @@ class LoadGenerator // Track account IDs that are currently being referenced by the transaction // queue (to avoid source account collisions during tx submission) std::unordered_set mAccountsInUse; + // In-use accounts keyed by public key, so that cleanupAccounts matches an + // externalized transaction set in time proportional to the set rather + // than to the number of accounts waiting. Accounts selected since the last + // cleanupAccounts call wait in mAccountsInUseToIndex: their keys are only + // known once TxGenerator has generated a transaction for them. + UnorderedMap mAccountsInUseByKey; + std::vector mAccountsInUseToIndex; // Accounts whose transaction externalized in the given ledger; released // once that ledger is applied. std::unordered_map mAccountsExternalized; diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index cf562c0f8..a5fd9c622 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -251,6 +251,101 @@ TEST_CASE("mixed pregen and synthetic soroban in overlay-only mode", 100 * simulation->getExpectedLedgerCloseTime(), false); } +TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", + "[loadgen]") +{ + // perfnet's block_time_check load: MIXED_PREGEN_SAC_PAYMENT with no + // classic stream, in overlay-only mode, with more transactions than + // source accounts. The soroban stream fails the run as soon as its pool + // is empty, so the run completes only if accounts return to the pool once + // their transactions' ledgers close. The pool covers twenty seconds of + // load, far more than the few one-second ledgers a transaction is in + // flight, so a stalled real-time simulation does not drain it. + uint32_t const nAccounts = 300; + uint32_t const sorobanTxRate = 15; + uint32_t const nTxs = nAccounts + 100; + + Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); + Simulation::pointer simulation = Topologies::pair(networkID, [&](int i) { + auto cfg = getTestConfig(i); + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; + cfg.ARTIFICIALLY_GENERATE_LOAD_FOR_TESTING = true; + cfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = + Config::CURRENT_LEDGER_PROTOCOL_VERSION; + cfg.GENESIS_TEST_ACCOUNT_COUNT = nAccounts; + return cfg; + }); + + simulation->startAllNodes(); + simulation->crankUntil( + [&]() { return simulation->haveAllExternalized(3, 1); }, + 10 * simulation->getExpectedLedgerCloseTime(), false); + + auto nodes = simulation->getNodes(); + auto& app = *nodes[0]; + + // Max out Soroban network limits so synthetic footprints pass checkValid + // and every submitted transaction fits in the next ledger. + upgradeSorobanNetworkConfig( + [&](SorobanNetworkConfig& cfg) { + auto mx = std::numeric_limits::max(); + cfg.mLedgerMaxTxCount = mx; + cfg.mLedgerMaxInstructions = mx; + cfg.mLedgerMaxTransactionsSizeBytes = mx; + cfg.mLedgerMaxDiskReadEntries = mx; + cfg.mLedgerMaxDiskReadBytes = mx; + cfg.mLedgerMaxWriteLedgerEntries = mx; + cfg.mLedgerMaxWriteBytes = mx; + cfg.mTxMaxInstructions = mx; + cfg.mTxMaxDiskReadEntries = mx; + cfg.mTxMaxDiskReadBytes = mx; + cfg.mTxMaxWriteLedgerEntries = mx; + cfg.mTxMaxWriteBytes = mx; + cfg.mTxMaxFootprintEntries = mx; + cfg.mTxMaxSizeBytes = mx; + cfg.mTxMaxContractEventsSizeBytes = mx; + }, + simulation); + + // MIXED_PREGEN_* modes open a pregenerated classic file even when the + // classic stream is off; this run never reads from it. + std::string fileName = + app.getConfig().LOADGEN_PREGENERATED_TRANSACTIONS_FILE; + auto cleanup = gsl::finally([&]() { std::remove(fileName.c_str()); }); + generateTransactions(app, fileName, /* numTransactions */ 1, nAccounts, + /* offset */ 0); + + for (auto& node : nodes) + { + node->setRunInOverlayOnlyMode(true); + } + + auto& completed = + app.getMetrics().NewMeter({"loadgen", "run", "complete"}, "run"); + auto& failed = + app.getMetrics().NewMeter({"loadgen", "run", "failed"}, "run"); + auto const completedBefore = completed.count(); + auto const failedBefore = failed.count(); + + GeneratedLoadConfig cfg = GeneratedLoadConfig::txLoad( + LoadGenMode::MIXED_PREGEN_SAC_PAYMENT, nAccounts, nTxs, sorobanTxRate, + /* offset */ 0); + cfg.preloadedTransactionsFile = fileName; + auto& mix = cfg.getMutMixPregenSorobanConfig(); + mix.classicTxRate = 0; + mix.sorobanTxRate = sorobanTxRate; + app.getLoadGenerator().generateLoad(cfg); + + simulation->crankUntil( + [&]() { + return completed.count() == completedBefore + 1 || + failed.count() != failedBefore; + }, + 60 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(failed.count() == failedBefore); + REQUIRE(completed.count() == completedBefore + 1); +} + TEST_CASE("generate load with unique accounts", "[loadgen]") { Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); From 7a6914db68cca6ecb5f9172503f97b5bb57e897a Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:11:13 -0800 Subject: [PATCH 11/39] Lock the seen set and INV tracker once per INV batch handle_inv_batch took the tx_seen read lock and the inv_tracker write lock once per INV entry. Every node receives about N-1 advertisements per transaction, so on a 30-node mesh at 3k TPS that is roughly 170k lock round trips per second, contending with every peer's reader task, the TX relay and the housekeeping retries. Filter the batch against tx_seen under one read lock, then record sources and pick the first-advertised hashes under one tracker write lock. The two locks are never held together. Decisions are the same: seen TXs are skipped and not recorded as sources, a hash is requested only when this is its first advertisement, repeats in a batch add the peer once, and the demand keeps batch order. Tracker calls happen in the same order, so its LRU state is the same. Also fix a comment that claimed seen TXs were recorded as sources. Add a test over a real connection: one INV batch mixing a new hash, a seen hash, a hash first advertised by another peer, a repeat, and an unsorted order. It checks the demand count, tracked sources and the order in which the demanded transactions arrive. It passes before and after this change, and fails if the seen filter or the first-advert check is removed or the demand order is reversed. 30 repeated runs: no failures. --- overlay/src/libp2p_overlay.rs | 46 ++++++------ overlay/src/libp2p_overlay/dispatch_tests.rs | 78 ++++++++++++++++++++ 2 files changed, 101 insertions(+), 23 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 47f68c6ff..0e11b7864 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -1809,29 +1809,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() { diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 164c987fa..1cb00aa20 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -587,6 +587,84 @@ async fn inv_batches_completed_by_relay_or_local_broadcast_reach_the_peer() { 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; From 33012c95e60043f3e14c5b682a91c327a7607528 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:44:18 -0800 Subject: [PATCH 12/39] Stop re-registering loadgen metrics and copying envelopes per transaction LoadGenerator::execute built a fresh TxMetrics for every submitted transaction, which resolved all ten loadgen meters through MetricsRegistry::NewMeter: a MetricName allocation, a mutex, and two searches of the registry's std::map each. It also copied the whole envelope into a new StellarMessage (twice for classic payments) only to measure its size. Use the TxMetrics member resolved once at construction, which refers to the same registry meters, and compute the message size as the 4-byte message type discriminant plus the envelope's XDR size. The recorded values are unchanged. Extend the overlay-only synthetic SAC test to check the per-transaction meters, including that loadgen.txn.bytes still equals the wire size of each submitted StellarMessage. --- src/simulation/LoadGenerator.cpp | 22 +++++++++------ src/simulation/test/LoadGeneratorTests.cpp | 32 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index 0972fa520..6091df1b6 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -1551,7 +1551,15 @@ TxSubmitStatus LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, TransactionResultCode& code) { - TxMetrics txm(mApp.getMetrics()); + // The meters live in the registry for the application's lifetime, so use + // the references resolved at construction instead of looking all ten up + // again (a locked map search each) for every transaction. + auto& txm = mTxMetrics; + // Wire size of the TRANSACTION StellarMessage that floods `txf`: the + // 4-byte message type discriminant followed by the envelope, sized without + // copying the envelope into a new message. + uint64_t const msgBytes = + sizeof(uint32_t) + xdr::xdr_size(txf->getEnvelope()); // Record tx metrics. switch (mode) @@ -1559,8 +1567,7 @@ LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, case LoadGenMode::PAY: case LoadGenMode::PAY_PREGENERATED: txm.mNativePayment.Mark(txf->getNumOperations()); - txm.mNativePaymentBytes.Mark( - xdr::xdr_argpack_size(*txf->toStellarMessage())); + txm.mNativePaymentBytes.Mark(msgBytes); break; case LoadGenMode::SOROBAN_UPLOAD: txm.mSorobanUploadTxs.Mark(); @@ -1582,8 +1589,7 @@ LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, { case LoadGenMode::PAY: txm.mNativePayment.Mark(txf->getNumOperations()); - txm.mNativePaymentBytes.Mark( - xdr::xdr_argpack_size(*txf->toStellarMessage())); + txm.mNativePaymentBytes.Mark(msgBytes); break; case LoadGenMode::SOROBAN_UPLOAD: txm.mSorobanUploadTxs.Mark(); @@ -1607,16 +1613,14 @@ LoadGenerator::execute(TransactionFrameBasePtr txf, LoadGenMode mode, else { txm.mNativePayment.Mark(txf->getNumOperations()); - txm.mNativePaymentBytes.Mark( - xdr::xdr_argpack_size(*txf->toStellarMessage())); + txm.mNativePaymentBytes.Mark(msgBytes); } break; } txm.mTxnAttempted.Mark(); - auto msg = txf->toStellarMessage(); - txm.mTxnBytes.Mark(xdr::xdr_argpack_size(*msg)); + txm.mTxnBytes.Mark(msgBytes); // Skip certain checks for pregenerated classic transactions. The synthetic // Soroban half of MIXED_PREGEN_* is generated locally, but must still go diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index a5fd9c622..2d530fc1d 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -327,6 +327,33 @@ TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", auto const completedBefore = completed.count(); auto const failedBefore = failed.count(); + // Per-transaction submission metrics. + auto& attempted = + app.getMetrics().NewMeter({"loadgen", "txn", "attempted"}, "txn"); + auto& invokes = + app.getMetrics().NewMeter({"loadgen", "soroban", "invoke"}, "txn"); + auto& payments = + app.getMetrics().NewMeter({"loadgen", "payment", "submitted"}, "op"); + auto& bytes = app.getMetrics().NewMeter({"loadgen", "txn", "bytes"}, "txn"); + auto const attemptedBefore = attempted.count(); + auto const invokesBefore = invokes.count(); + auto const paymentsBefore = payments.count(); + auto const bytesBefore = bytes.count(); + + // Every synthetic SAC payment has the same XDR shape, so one reference + // transaction gives the wire size of each message loadgen submits. + TxGenerator refGen(app); + SCAddress refDest(SC_ADDRESS_TYPE_CONTRACT); + refDest.contractId() = sha256("reference destination"); + auto refTx = refGen + .invokeSACPayment( + app.getLedgerManager().getLastClosedLedgerNum() + 1, + /* fromAccountId */ 0, refDest, + makeSyntheticContractInstance("loadgen_sac"), + /* amount */ 100, std::nullopt) + .second; + auto const refMsgBytes = xdr::xdr_argpack_size(*refTx->toStellarMessage()); + GeneratedLoadConfig cfg = GeneratedLoadConfig::txLoad( LoadGenMode::MIXED_PREGEN_SAC_PAYMENT, nAccounts, nTxs, sorobanTxRate, /* offset */ 0); @@ -344,6 +371,11 @@ TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", 60 * simulation->getExpectedLedgerCloseTime(), false); REQUIRE(failed.count() == failedBefore); REQUIRE(completed.count() == completedBefore + 1); + + REQUIRE(attempted.count() - attemptedBefore == nTxs); + REQUIRE(invokes.count() - invokesBefore == nTxs); + REQUIRE(payments.count() == paymentsBefore); + REQUIRE(bytes.count() - bytesBefore == nTxs * refMsgBytes); } TEST_CASE("generate load with unique accounts", "[loadgen]") From 1184e544477c5e4b7199643f6511c10f81f055b1 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:56:53 -0800 Subject: [PATCH 13/39] Make the loadgen account recycling test check what it claims, faster The section asserted that no transaction was rejected, but on this branch HerderImpl::recvTransaction hands every transaction to the overlay and always returns TX_STATUS_PENDING, so loadgen.txn.rejected cannot move. The checks that matter are that every transaction applies successfully and that the run completes, which requires each account's cached sequence number to match the ledger. Say so, drop the rejected assertion, and stop cranking as soon as a run fails instead of waiting for the timeout. Submit 2x the accounts at 200 tx/s instead of 3x at 100 tx/s. That still forces every account through the pool more than once and stays within the unique-account check (3 x 200 txs per one-second ledger = 600 <= 1,000 accounts), and each run takes about 10 s of submission instead of 30 s. --- src/simulation/test/LoadGeneratorTests.cpp | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index 2d530fc1d..52de6a0bd 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -457,26 +457,32 @@ TEST_CASE("generate load with unique accounts", "[loadgen]") } SECTION("recycle available accounts across ledgers and runs") { - // More submissions than accounts requires returning applied accounts - // to the pool. Real application catches premature reuse through bad - // sequence numbers; a second run also exercises resetting the pool. - uint32_t const txsPerRun = 3 * nAccounts; + // More submissions than accounts requires returning accounts to the + // pool once their transactions have applied. Every transaction must + // apply successfully, and a run completes only once each account's + // cached sequence number matches the ledger. A second run exercises + // resetting the pool. + uint32_t const txsPerRun = 2 * nAccounts; auto& completed = app.getMetrics().NewMeter({"loadgen", "run", "complete"}, "run"); - auto& rejected = - app.getMetrics().NewMeter({"loadgen", "txn", "rejected"}, "txn"); + auto& failed = + app.getMetrics().NewMeter({"loadgen", "run", "failed"}, "run"); for (uint32_t run = 0; run < 2; ++run) { auto before = getSuccessfulTxCount(); auto runsBefore = completed.count(); + auto failedBefore = failed.count(); loadGen.generateLoad(GeneratedLoadConfig::txLoad( LoadGenMode::PAY, nAccounts, txsPerRun, - /* txRate */ 100, /* offset */ nAccounts)); + /* txRate */ 200, /* offset */ nAccounts)); simulation->crankUntil( - [&]() { return completed.count() == runsBefore + 1; }, + [&]() { + return completed.count() == runsBefore + 1 || + failed.count() != failedBefore; + }, 120 * simulation->getExpectedLedgerCloseTime(), false); + REQUIRE(failed.count() == failedBefore); REQUIRE(getSuccessfulTxCount() - before == txsPerRun); - REQUIRE(rejected.count() == 0); } } SECTION("invalid loadgen parameters") From 4622ce44440120c4d04eb3528338b51e804203f5 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Tue, 15 Sep 2026 09:55:30 -0700 Subject: [PATCH 14/39] Compress and share transaction set responses across peers Prefer the negotiated zstd tx-set response protocol and cache each set's encoded payload for fanout. Encode and decode off Tokio network workers, retain existing size and send-admission bounds, and preserve legacy peer compatibility. Port 125a2487c onto the batched-getdata branch. Rust tests: 191 passed, 3 pre-existing ignored; cargo fmt passed. --- Cargo.lock | 47 ++++ overlay/Cargo.toml | 3 + overlay/src/flood/mod.rs | 2 + overlay/src/flood/txset.rs | 35 +-- overlay/src/flood/txset_encoding.rs | 195 +++++++++++++++ overlay/src/libp2p_overlay.rs | 244 +++++++++++++------ overlay/src/libp2p_overlay/dispatch_tests.rs | 148 ++++++++++- overlay/src/main.rs | 2 +- 8 files changed, 570 insertions(+), 106 deletions(-) create mode 100644 overlay/src/flood/txset_encoding.rs diff --git a/Cargo.lock b/Cargo.lock index 23b441329..b45d7b694 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/overlay/Cargo.toml b/overlay/Cargo.toml index 04b665747..1ad9ab16a 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 59a6adcaa..ecefbc36d 100644 --- a/overlay/src/flood/mod.rs +++ b/overlay/src/flood/mod.rs @@ -9,6 +9,7 @@ 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, INV_BATCH_MAX_SIZE}; @@ -17,3 +18,4 @@ 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 d8d487a52..6a800aad3 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 lazily encoded 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(vec![1, 2, 3].into()), ledger_seq: 100, }; @@ -105,12 +106,12 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: vec![].into(), + xdr: Arc::new(vec![].into()), ledger_seq: 100, }); cache.insert(CachedTxSet { hash: [2u8; 32], - xdr: vec![].into(), + xdr: Arc::new(vec![].into()), ledger_seq: 200, }); @@ -126,12 +127,12 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: vec![].into(), + xdr: Arc::new(vec![].into()), ledger_seq: 100, }); cache.insert(CachedTxSet { hash: [2u8; 32], - xdr: vec![].into(), + xdr: Arc::new(vec![].into()), 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(vec![].into()), ledger_seq: 102, }); @@ -165,14 +166,14 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: vec![1, 2, 3].into(), + xdr: Arc::new(vec![1, 2, 3].into()), 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(vec![4, 5, 6].into()), ledger_seq: 200, }); @@ -188,7 +189,7 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: vec![1, 2, 3].into(), + xdr: Arc::new(vec![1, 2, 3].into()), 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(vec![4, 5, 6].into()), 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(vec![1, 2, 3].into()), ledger_seq: 100, }); cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: vec![4, 5, 6].into(), + xdr: Arc::new(vec![4, 5, 6].into()), 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(vec![].into()), ledger_seq: 300, }); cache.insert(CachedTxSet { hash: [3u8; 32], - xdr: vec![].into(), + xdr: Arc::new(vec![].into()), ledger_seq: 400, }); diff --git a/overlay/src/flood/txset_encoding.rs b/overlay/src/flood/txset_encoding.rs new file mode 100644 index 000000000..34bc6d7e1 --- /dev/null +++ b/overlay/src/flood/txset_encoding.rs @@ -0,0 +1,195 @@ +//! Cached TX set encoding for the compressed response protocol. +//! +//! Inside the usual length-prefixed frame: a big-endian u32 XDR length, +//! followed by one zstd frame. The high bit marks uncompressed XDR when +//! compression would not save space. Both encodings retain the legacy limit. +use std::{io, sync::Arc}; +use tokio::sync::OnceCell; + +pub(crate) const MAX_TXSET_XDR_SIZE: usize = 16 * 1024 * 1024 - 4; +const RAW: u32 = 1 << 31; + +/// One immutable allocation per cached set, shared by all peer sends. The +/// compressed representation is computed on demand once for the whole fanout. +#[derive(Debug)] +pub struct TxSetData { + xdr: Vec, + compressed: OnceCell>>, +} + +impl From> for TxSetData { + fn from(xdr: Vec) -> Self { + Self { + xdr, + compressed: OnceCell::new(), + } + } +} + +impl TxSetData { + 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() + } + + pub(crate) async fn encoded(self: &Arc) -> io::Result<([u8; 4], &[u8])> { + let compressed = self + .compressed + .get_or_try_init(|| async { + let data = Arc::clone(self); + // Compression must not occupy a Tokio network worker. Admission + // already bounds the number and raw bytes of outstanding sends. + tokio::task::spawn_blocking(move || compress(&data.xdr)) + .await + .map_err(io::Error::other)? + }) + .await?; + let length = self.len() as u32; + Ok(match compressed { + Some(bytes) => (length.to_be_bytes(), bytes), + None => ((length | RAW).to_be_bytes(), &self.xdr), + }) + } +} + +fn compress(xdr: &[u8]) -> io::Result>> { + if xdr.len() > MAX_TXSET_XDR_SIZE { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "TX set exceeds wire limit", + )); + } + let mut bytes = zstd::bulk::compress(xdr, 1)?; + if bytes.len() >= xdr.len() { + return Ok(None); + } + // bulk::compress reserves the worst-case size, approximately the raw + // length. Do not retain that spare allocation throughout the cache lifetime. + bytes.shrink_to_fit(); + Ok(Some(bytes)) +} + +pub(crate) fn decode(mut frame: Vec) -> io::Result> { + let invalid = || io::Error::new(io::ErrorKind::InvalidData, "invalid TX set encoding"); + if frame.len() < 4 || frame.len() > MAX_TXSET_XDR_SIZE + 4 { + return Err(invalid()); + } + let header = u32::from_be_bytes(frame[..4].try_into().unwrap()); + let length = (header & !RAW) as usize; + // Check before any decompressor or output allocation. Never trust the + // content size embedded in the compressed frame to allocate memory. + if length > MAX_TXSET_XDR_SIZE { + return Err(invalid()); + } + if header & RAW != 0 { + if frame.len() - 4 != length { + return Err(invalid()); + } + frame.drain(..4); + return Ok(frame); + } + 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 output = decoder.decompress(compressed, length)?; + if output.len() != length { + return Err(invalid()); + } + Ok(output) +} + +#[cfg(test)] +mod tests { + use super::*; + use rand::{RngCore, SeedableRng}; + + async fn encode(xdr: Vec) -> Vec { + let data = Arc::new(TxSetData::from(xdr)); + let (header, bytes) = data.encoded().await.unwrap(); + [header.as_slice(), bytes].concat() + } + + #[tokio::test] + async fn roundtrip_and_size_bound_for_both_encodings() { + let mut random = vec![0; 64 * 1024]; + 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 frame = encode(xdr.clone()).await; + assert!(frame.len() <= xdr.len() + 4); + assert_eq!(decode(frame).unwrap(), xdr); + } + let raw = encode(vec![17]).await; + assert_ne!(raw[0] & 0x80, 0); + let compressed = encode(vec![17; 1024]).await; + assert_eq!(compressed[0] & 0x80, 0); + assert!(compressed.len() < 100); + } + + #[tokio::test] + async fn concurrent_fanout_shares_one_encoded_allocation() { + let data = Arc::new(TxSetData::from(vec![13; 4 * 1024 * 1024])); + let results = futures::future::join_all((0..29).map(|_| data.encoded())).await; + let (header, bytes) = results[0].as_ref().unwrap(); + assert_eq!(header[0] & 0x80, 0); + for result in &results { + let (other_header, other_bytes) = result.as_ref().unwrap(); + assert_eq!(other_header, header); + assert!(std::ptr::eq(bytes.as_ptr(), other_bytes.as_ptr())); + } + let (_, again) = data.encoded().await.unwrap(); + assert!(std::ptr::eq(bytes.as_ptr(), again.as_ptr())); + } + + #[tokio::test] + async fn rejects_truncation_corruption_and_length_mismatch() { + let frame = encode(vec![13; 4096]).await; + for end in 0..frame.len() { + assert!(decode(frame[..end].to_vec()).is_err()); + } + let mut corrupt = frame.clone(); + corrupt[4] ^= 0xff; // Invalid zstd magic. + assert!(decode(corrupt).is_err()); + for length in [0, 4095, 4097, MAX_TXSET_XDR_SIZE + 1] { + let mut bad = frame.clone(); + bad[..4].copy_from_slice(&(length as u32).to_be_bytes()); + assert!(decode(bad).is_err()); + } + // Exactly one zstd frame must consume the entire payload. + 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 mut raw = encode(vec![19]).await; + raw.push(0); + assert!(decode(raw).is_err()); + } + + #[tokio::test] + async fn compression_does_not_allow_oversized_raw_sets() { + let oversized = vec![0; MAX_TXSET_XDR_SIZE + 1]; + let data = Arc::new(TxSetData::from(oversized)); + assert!(data.encoded().await.is_err()); + // Even if its compressed bytes fit, an over-limit raw size is rejected. + let frame = [ + (MAX_TXSET_XDR_SIZE as u32 + 1).to_be_bytes().as_slice(), + zstd::bulk::compress(data.as_slice(), 1).unwrap().as_slice(), + ] + .concat(); + assert!(decode(frame).is_err()); + assert!(decode(vec![0; MAX_TXSET_XDR_SIZE + 5]).is_err()); + } +} diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 0e11b7864..551aa542a 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; @@ -46,10 +47,12 @@ 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"); +pub const COMPRESSED_TXSET_PROTOCOL: StreamProtocol = + StreamProtocol::new("/stellar/txset/zstd/1.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. @@ -100,7 +103,7 @@ pub enum OverlayCommand { /// Send TX set to a specific peer (response to their request) SendTxSet { hash: [u8; 32], - data: Arc>, + data: Arc, to: PeerId, permits: (OwnedSemaphorePermit, OwnedSemaphorePermit), }, @@ -225,8 +228,7 @@ impl OverlayHandle { } } - pub async fn send_txset(&self, hash: [u8; 32], data: impl Into>>, to: PeerId) { - let data = data.into(); + 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!( @@ -543,13 +545,21 @@ impl StellarOverlay { self.run_event_loop_with_control( #[cfg(test)] true, + #[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) { + // Test-only switches exercise real negotiation with legacy peers. + // Production always advertises control and compressed responses. + async fn run_event_loop_with_control( + mut self, + #[cfg(test)] accept_control: bool, + #[cfg(test)] accept_compressed: bool, + ) { + #[cfg(not(test))] + let accept_compressed = true; #[cfg(not(test))] let accept_control = true; // Accept incoming streams for each protocol @@ -596,6 +606,18 @@ impl StellarOverlay { None }; + let compressed_incoming = if accept_compressed { + match self.control.accept(COMPRESSED_TXSET_PROTOCOL) { + Ok(incoming) => Some(incoming), + Err(e) => { + error!("Failed to accept compressed TxSet protocol: {:?}", e); + return; + } + } + } else { + None + }; + // Spawn inbound stream handlers let state = self.state.clone(); tokio::spawn(handle_inbound_scp_streams( @@ -607,7 +629,15 @@ impl StellarOverlay { tokio::spawn(handle_inbound_scp_streams(incoming, state.clone(), true)); } tokio::spawn(handle_inbound_tx_streams(tx_incoming, state.clone())); - tokio::spawn(handle_inbound_txset_streams(txset_incoming, state.clone())); + tokio::spawn(handle_inbound_txset_streams( + txset_incoming, + state.clone(), + false, + )); + + if let Some(incoming) = compressed_incoming { + tokio::spawn(handle_inbound_txset_streams(incoming, state.clone(), true)); + } // Spawn INV/GETDATA housekeeping task tokio::spawn(inv_getdata_housekeeping_task(state.clone())); @@ -1150,7 +1180,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 {}", @@ -1159,16 +1189,8 @@ 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(_) => { + match send_peer_frames(&state, peer, StreamType::TxSet, PeerFrames::TxSet(&data)).await { + Ok(bytes) => { state.metrics.send_txset.fetch_add(1, Ordering::Relaxed); state.metrics.message_write.fetch_add(1, Ordering::Relaxed); state @@ -1176,7 +1198,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 @@ -1261,7 +1283,8 @@ impl StreamType { match self { StreamType::Control | StreamType::TxSetRequest => CONTROL_PROTOCOL, StreamType::Tx => TX_PROTOCOL, - StreamType::TxSet | StreamType::LegacyTxSetRequest => TXSET_PROTOCOL, + StreamType::TxSet => COMPRESSED_TXSET_PROTOCOL, + StreamType::LegacyTxSetRequest => TXSET_PROTOCOL, } } @@ -1290,6 +1313,12 @@ async fn open_peer_stream( protocol = SCP_PROTOCOL; control.open_stream(peer, protocol.clone()).await } + Err(libp2p_stream::OpenStreamError::UnsupportedProtocol(_)) + if protocol == COMPRESSED_TXSET_PROTOCOL => + { + protocol = TXSET_PROTOCOL; + control.open_stream(peer, protocol.clone()).await + } result => result, } .map_err(|e| io::Error::new(io::ErrorKind::NotConnected, e))?; @@ -1363,7 +1392,45 @@ async fn send_to_peer_stream_parts( stream_type: StreamType, parts: &[&[u8]], ) -> io::Result<()> { - send_peer_frames(state, peer_id, stream_type, framed_io::Frames::One(parts)).await + send_peer_frames( + state, + peer_id, + stream_type, + PeerFrames::Plain(framed_io::Frames::One(parts)), + ) + .await + .map(|_| ()) +} + +// Select response encoding using the protocol actually negotiated on this +// stream, including after reconnect. Other routes keep their original framing. +#[derive(Clone, Copy)] +enum PeerFrames<'a> { + Plain(framed_io::Frames<'a>), + TxSet(&'a Arc), +} + +impl PeerFrames<'_> { + async fn write(self, outbound: &mut OutboundStream) -> io::Result { + match self { + Self::Plain(frames) => { + frames.write(&mut outbound.stream).await?; + Ok(0) + } + Self::TxSet(data) => { + let (header, payload) = if outbound.protocol == COMPRESSED_TXSET_PROTOCOL { + data.encoded().await? + } else { + ( + (stellar_xdr::MessageType::GeneralizedTxSet as i32).to_be_bytes(), + data.as_slice(), + ) + }; + framed_io::write_frame_parts(&mut outbound.stream, &[&header, payload]).await?; + Ok(4 + payload.len()) + } + } + } } /// Write `frames` to the peer's stream for `stream_type`, opening it if @@ -1379,8 +1446,8 @@ async fn send_peer_frames( state: &SharedState, peer_id: PeerId, stream_type: StreamType, - frames: framed_io::Frames<'_>, -) -> io::Result<()> { + frames: PeerFrames<'_>, +) -> io::Result { // Retry up to 2 times (3 attempts total) for reliability const MAX_RETRIES: usize = 2; @@ -1467,8 +1534,8 @@ async fn send_peer_frames( )) .await; } - match frames.write(&mut stream.stream).await { - Ok(()) => return Ok(()), + match frames.write(stream).await { + Ok(bytes) => return Ok(bytes), Err(e) => { // Clear the broken stream *stream_guard = None; @@ -1991,8 +2058,10 @@ async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) batch.extend_from_slice(part); } } - let frames = framed_io::Frames::Batch(&batch); - let result = send_peer_frames(state, peer, StreamType::Tx, frames).await; + let frames = PeerFrames::Plain(framed_io::Frames::Batch(&batch)); + let result = send_peer_frames(state, peer, StreamType::Tx, frames) + .await + .map(|_| ()); record_tx_response_write(state, peer, result, txs.len(), bytes - 4 * txs.len()) }; if !written { @@ -2172,8 +2241,54 @@ fn handle_txset_request(state: &SharedState, peer_id: PeerId, hash: [u8; 32]) { } } -/// Handle inbound TxSet streams from peers -async fn handle_inbound_txset_streams(mut incoming: IncomingStreams, state: Arc) { +/// Deliver strict-decoded XDR identically for both response encodings. +async fn deliver_txset(state: &SharedState, peer_id: PeerId, txset_data: 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 + ); + } +} + +async fn handle_inbound_txset_streams( + mut incoming: IncomingStreams, + state: Arc, + compressed: bool, +) { while let Some((peer_id, mut stream)) = incoming.next().await { debug!("Accepted inbound TxSet stream from {}", peer_id); state.metrics.inbound_live.fetch_add(1, Ordering::Relaxed); @@ -2188,6 +2303,23 @@ async fn handle_inbound_txset_streams(mut incoming: IncomingStreams, state: Arc< .metrics .byte_read .fetch_add(data.len() as u64, Ordering::Relaxed); + if compressed { + // Decode and strict-parse outside the network runtime. Each + // stream awaits completion before reading another frame. + let decoded = tokio::task::spawn_blocking(move || { + use stellar_xdr::{GeneralizedTransactionSet, Limits, ReadXdr}; + let xdr = crate::flood::txset_encoding::decode(data)?; + GeneralizedTransactionSet::from_xdr(&xdr, Limits::none()) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + Ok::<_, io::Error>(xdr) + }) + .await; + match decoded { + Ok(Ok(xdr)) => deliver_txset(&state, peer_id, xdr).await, + result => warn!("TXSET_PARSE_ERR: Dropping malformed compressed TxSet from {}: {:?}", peer_id, result), + } + continue; + } let message = match crate::xdr::parse_stellar_message(&data) { Ok(message) => message, Err(e) => { @@ -2209,45 +2341,7 @@ async fn handle_inbound_txset_streams(mut incoming: IncomingStreams, state: Arc< // 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 - ); - } + deliver_txset(&state, peer_id, txset_data).await; } other => { warn!( @@ -3012,7 +3106,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(txset_data.clone().into()), from).await; } } _ = tokio::time::sleep(Duration::from_millis(10)) => {} @@ -3796,7 +3890,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(txset_data.clone().into()), from).await; responded = true; } } @@ -3913,7 +4007,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(data.into()), from).await; } } _ = tokio::time::sleep(Duration::from_millis(10)) => {} @@ -4248,7 +4342,7 @@ 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(txset_data.into()), peer2_id) .await; }); @@ -4376,7 +4470,7 @@ 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(txset_data.clone().into()), peer1_id) .await; // Verify node1 receives the TxSet response diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 1cb00aa20..8f5e707dd 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -26,7 +26,7 @@ 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(data.into()), peer).await; } "fetch" => handle.fetch_txset([1; 32], 1).await, // Awaiting a direct send must preserve ordering within an SCP-state @@ -94,6 +94,10 @@ impl TestNode { } async fn start_with_control(accept_control: bool) -> Self { + Self::start_with_protocols(accept_control, true).await + } + + async fn start_with_protocols(accept_control: bool, accept_compressed: bool) -> Self { let (handle, events, admissions, mut overlay) = create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())) .unwrap(); @@ -116,7 +120,8 @@ 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_with_control(accept_control, accept_compressed)); Self { handle, events, @@ -587,6 +592,111 @@ async fn inv_batches_completed_by_relay_or_local_broadcast_reach_the_peer() { receiver.stop().await; } +#[tokio::test] +async fn txset_encoding_negotiates_with_legacy_peers_and_after_reopening() { + for (control, compressed) in [(true, true), (true, false), (false, false)] { + let mut sender = TestNode::start().await; + let mut receiver = TestNode::start_with_protocols(control, compressed).await; + sender.connect(&receiver).await; + let streams = sender.streams_to(receiver.peer).await; + let (hash, xdr) = large_txset(); + let shared = Arc::new(TxSetData::from(xdr.clone())); + for reopen in [false, true] { + if reopen { + streams.txset.lock().await.take(); + } + let bytes = send_peer_frames( + &sender.state, + receiver.peer, + StreamType::TxSet, + PeerFrames::TxSet(&shared), + ) + .await + .unwrap(); + if compressed { + assert!( + bytes < xdr.len() / 2, + "large responses must use compressed bytes" + ); + } else { + assert_eq!(bytes, xdr.len() + 4); + } + { + let guard = streams.txset.lock().await; + let outbound = guard.as_ref().unwrap(); + assert_eq!( + outbound.protocol, + if compressed { + COMPRESSED_TXSET_PROTOCOL + } else { + 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, xdr); + break; + } + } + }) + .await + .expect("negotiated response was not delivered intact"); + } + sender.stop().await; + receiver.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(vec![0xff; 1000])); + send_peer_frames( + &sender.state, + receiver.peer, + StreamType::TxSet, + PeerFrames::TxSet(&malformed), + ) + .await + .unwrap(); + let (hash, xdr) = test_txset_xdr(19); + // The uncompressed representation on the new protocol is also accepted. + let raw = [ + ((xdr.len() as u32) | (1 << 31)).to_be_bytes().as_slice(), + &xdr, + ] + .concat(); + send_to_peer_stream(&sender.state, receiver.peer, StreamType::TxSet, &raw) + .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, 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}; @@ -776,7 +886,7 @@ 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(data.clone().into()), slow.peer) .await; // Also exercise reopening: this write can complete only if the dispatcher @@ -785,12 +895,16 @@ 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(data.clone().into()), 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(second_data.clone().into()), + healthy.peer, + ) .await; let scp = test_scp_envelope_xdr(1234); sender.handle.broadcast_scp(scp.clone()).await; @@ -861,7 +975,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(response_data.clone().into()), + 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. @@ -934,11 +1052,11 @@ async fn quic_transport_priorities_are_applied_to_each_route_and_reopening() { streams.txset.lock().await.take(); let (hash, data) = test_txset_xdr(1); // Await the actual write, rather than command enqueue, to observe reopening. - send_to_peer_stream( + send_peer_frames( &sender.state, receiver.peer, StreamType::TxSet, - &crate::xdr::frame_tx_set(&data), + PeerFrames::TxSet(&Arc::new(data.clone().into())), ) .await .unwrap(); @@ -1025,7 +1143,7 @@ async fn legacy_peer_receives_scp_and_fetches_on_its_supported_routes() { send_to_peer_stream( &legacy.state, sender.peer, - StreamType::TxSet, + StreamType::LegacyTxSetRequest, &crate::xdr::frame_get_tx_set(hash), ) .await @@ -1066,8 +1184,10 @@ 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(data.clone().into()), peer) + .await; + let waiting = handle.send_txset(hash, Arc::new(data.into()), peer); tokio::pin!(waiting); assert!(futures::poll!(&mut waiting).is_pending()); tokio::time::timeout(Duration::from_secs(1), handle.ping()) @@ -1106,7 +1226,9 @@ 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(data.into()), 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) @@ -1179,7 +1301,7 @@ 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(data)); let retained = Arc::downgrade(&data); for _ in 0..29 { handle diff --git a/overlay/src/main.rs b/overlay/src/main.rs index 752ea0d62..572a3a218 100644 --- a/overlay/src/main.rs +++ b/overlay/src/main.rs @@ -383,7 +383,7 @@ fn cache_tx_set_xdr( ) { tx_set_cache.insert(CachedTxSet { hash, - xdr: Arc::new(xdr), + xdr: Arc::new(xdr.into()), ledger_seq: current_ledger_seq, }); } From a1060af74678e652d366bb280cfe1597f60eb4d8 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Thu, 17 Sep 2026 14:14:42 -0700 Subject: [PATCH 15/39] Prepare mandatory compressed transaction sets before validation --- docs/rust-overlay/transport.md | 16 +- docs/rust-overlay/txset-fetching.md | 38 +- overlay/src/flood/txset.rs | 30 +- overlay/src/flood/txset_encoding.rs | 205 +++++------ overlay/src/libp2p_overlay.rs | 361 +++++-------------- overlay/src/libp2p_overlay/dispatch_tests.rs | 323 ++++++++--------- overlay/src/main.rs | 121 ++++++- src/herder/HerderImpl.cpp | 41 ++- src/herder/TxSetFrame.cpp | 11 +- src/herder/TxSetFrame.h | 10 +- src/herder/test/TxSetTests.cpp | 38 ++ 11 files changed, 577 insertions(+), 617 deletions(-) diff --git a/docs/rust-overlay/transport.md b/docs/rust-overlay/transport.md index 55274af13..95dbefc95 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 193c26d48..2900f9529 100644 --- a/docs/rust-overlay/txset-fetching.md +++ b/docs/rust-overlay/txset-fetching.md @@ -6,15 +6,37 @@ 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 + +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 @@ -55,7 +77,7 @@ provide a timeout or automatic retry for a silent peer. ## 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. diff --git a/overlay/src/flood/txset.rs b/overlay/src/flood/txset.rs index 6a800aad3..95beb3c44 100644 --- a/overlay/src/flood/txset.rs +++ b/overlay/src/flood/txset.rs @@ -12,7 +12,7 @@ pub type Hash256 = [u8; 32]; pub struct CachedTxSet { /// The TX set hash (SHA256 of XDR) pub hash: Hash256, - /// Immutable XDR and lazily encoded response shared by the cache and + /// 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 @@ -86,7 +86,7 @@ mod tests { let tx_set = CachedTxSet { hash: [1u8; 32], - xdr: Arc::new(vec![1, 2, 3].into()), + xdr: Arc::new(TxSetData::from_local(vec![1, 2, 3]).unwrap()), ledger_seq: 100, }; @@ -106,12 +106,12 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: Arc::new(vec![].into()), + xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()), ledger_seq: 100, }); cache.insert(CachedTxSet { hash: [2u8; 32], - xdr: Arc::new(vec![].into()), + xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()), ledger_seq: 200, }); @@ -127,12 +127,12 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: Arc::new(vec![].into()), + xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()), ledger_seq: 100, }); cache.insert(CachedTxSet { hash: [2u8; 32], - xdr: Arc::new(vec![].into()), + xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()), ledger_seq: 101, }); @@ -141,7 +141,7 @@ mod tests { // Insert 3rd - should evict one cache.insert(CachedTxSet { hash: [3u8; 32], - xdr: Arc::new(vec![].into()), + xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()), ledger_seq: 102, }); @@ -166,14 +166,14 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: Arc::new(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: Arc::new(vec![4, 5, 6].into()), + xdr: Arc::new(TxSetData::from_local(vec![4, 5, 6]).unwrap()), ledger_seq: 200, }); @@ -189,7 +189,7 @@ mod tests { cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: Arc::new(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"); @@ -198,7 +198,7 @@ mod tests { // not evict the entry or shrink the cache below capacity. cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: Arc::new(vec![4, 5, 6].into()), + xdr: Arc::new(TxSetData::from_local(vec![4, 5, 6]).unwrap()), ledger_seq: 102, }); @@ -217,12 +217,12 @@ mod tests { // Insert, then overwrite the same hash with a newer ledger_seq. cache.insert(CachedTxSet { hash: [1u8; 32], - xdr: Arc::new(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: Arc::new(vec![4, 5, 6].into()), + xdr: Arc::new(TxSetData::from_local(vec![4, 5, 6]).unwrap()), ledger_seq: 200, }); @@ -240,12 +240,12 @@ mod tests { // cache exactly at capacity (not one over). cache.insert(CachedTxSet { hash: [2u8; 32], - xdr: Arc::new(vec![].into()), + xdr: Arc::new(TxSetData::from_local(vec![]).unwrap()), ledger_seq: 300, }); cache.insert(CachedTxSet { hash: [3u8; 32], - xdr: Arc::new(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 index 34bc6d7e1..a9bab83f9 100644 --- a/overlay/src/flood/txset_encoding.rs +++ b/overlay/src/flood/txset_encoding.rs @@ -1,32 +1,39 @@ -//! Cached TX set encoding for the compressed response protocol. +//! Immutable TX set bytes and their mandatory zstd response encoding. //! -//! Inside the usual length-prefixed frame: a big-endian u32 XDR length, -//! followed by one zstd frame. The high bit marks uncompressed XDR when -//! compression would not save space. Both encodings retain the legacy limit. -use std::{io, sync::Arc}; -use tokio::sync::OnceCell; +//! 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; -const RAW: u32 = 1 << 31; -/// One immutable allocation per cached set, shared by all peer sends. The -/// compressed representation is computed on demand once for the whole fanout. +/// 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, - compressed: OnceCell>>, + encoded: Vec, } -impl From> for TxSetData { - fn from(xdr: Vec) -> Self { - Self { - xdr, - compressed: OnceCell::new(), +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 }) } -} -impl TxSetData { pub fn as_slice(&self) -> &[u8] { &self.xdr } @@ -39,62 +46,24 @@ impl TxSetData { self.xdr.is_empty() } - pub(crate) async fn encoded(self: &Arc) -> io::Result<([u8; 4], &[u8])> { - let compressed = self - .compressed - .get_or_try_init(|| async { - let data = Arc::clone(self); - // Compression must not occupy a Tokio network worker. Admission - // already bounds the number and raw bytes of outstanding sends. - tokio::task::spawn_blocking(move || compress(&data.xdr)) - .await - .map_err(io::Error::other)? - }) - .await?; - let length = self.len() as u32; - Ok(match compressed { - Some(bytes) => (length.to_be_bytes(), bytes), - None => ((length | RAW).to_be_bytes(), &self.xdr), - }) - } -} - -fn compress(xdr: &[u8]) -> io::Result>> { - if xdr.len() > MAX_TXSET_XDR_SIZE { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - "TX set exceeds wire limit", - )); - } - let mut bytes = zstd::bulk::compress(xdr, 1)?; - if bytes.len() >= xdr.len() { - return Ok(None); + /// Borrow the exact cached response; sending never invokes a compressor. + pub fn encoded(&self) -> &[u8] { + &self.encoded } - // bulk::compress reserves the worst-case size, approximately the raw - // length. Do not retain that spare allocation throughout the cache lifetime. - bytes.shrink_to_fit(); - Ok(Some(bytes)) } -pub(crate) fn decode(mut frame: Vec) -> io::Result> { +/// 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_TXSET_XDR_SIZE + 4 { + if frame.len() < 4 || frame.len() > max_encoded_size() { return Err(invalid()); } - let header = u32::from_be_bytes(frame[..4].try_into().unwrap()); - let length = (header & !RAW) as usize; - // Check before any decompressor or output allocation. Never trust the - // content size embedded in the compressed frame to allocate memory. + 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()); } - if header & RAW != 0 { - if frame.len() - 4 != length { - return Err(invalid()); - } - frame.drain(..4); - return Ok(frame); - } let compressed = &frame[4..]; if zstd::zstd_safe::find_frame_compressed_size(compressed).map_err(|_| invalid())? != compressed.len() @@ -103,93 +72,105 @@ pub(crate) fn decode(mut frame: Vec) -> io::Result> { } let mut decoder = zstd::bulk::Decompressor::new()?; decoder.window_log_max(24)?; - let output = decoder.decompress(compressed, length)?; - if output.len() != length { + let xdr = decoder.decompress(compressed, length)?; + if xdr.len() != length { return Err(invalid()); } - Ok(output) + Ok(TxSetData { + xdr, + encoded: frame, + }) } #[cfg(test)] mod tests { use super::*; use rand::{RngCore, SeedableRng}; + use std::sync::Arc; - async fn encode(xdr: Vec) -> Vec { - let data = Arc::new(TxSetData::from(xdr)); - let (header, bytes) = data.encoded().await.unwrap(); - [header.as_slice(), bytes].concat() - } - - #[tokio::test] - async fn roundtrip_and_size_bound_for_both_encodings() { - let mut random = vec![0; 64 * 1024]; + #[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 frame = encode(xdr.clone()).await; - assert!(frame.len() <= xdr.len() + 4); - assert_eq!(decode(frame).unwrap(), xdr); + 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); } - let raw = encode(vec![17]).await; - assert_ne!(raw[0] & 0x80, 0); - let compressed = encode(vec![17; 1024]).await; - assert_eq!(compressed[0] & 0x80, 0); - assert!(compressed.len() < 100); } - #[tokio::test] - async fn concurrent_fanout_shares_one_encoded_allocation() { - let data = Arc::new(TxSetData::from(vec![13; 4 * 1024 * 1024])); - let results = futures::future::join_all((0..29).map(|_| data.encoded())).await; - let (header, bytes) = results[0].as_ref().unwrap(); - assert_eq!(header[0] & 0x80, 0); - for result in &results { - let (other_header, other_bytes) = result.as_ref().unwrap(); - assert_eq!(other_header, header); - assert!(std::ptr::eq(bytes.as_ptr(), other_bytes.as_ptr())); + #[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() + )); } - let (_, again) = data.encoded().await.unwrap(); - assert!(std::ptr::eq(bytes.as_ptr(), again.as_ptr())); } - #[tokio::test] - async fn rejects_truncation_corruption_and_length_mismatch() { - let frame = encode(vec![13; 4096]).await; + #[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; // Invalid zstd magic. + corrupt[4] ^= 0xff; assert!(decode(corrupt).is_err()); - for length in [0, 4095, 4097, MAX_TXSET_XDR_SIZE + 1] { + 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()); } - // Exactly one zstd frame must consume the entire payload. 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 mut raw = encode(vec![19]).await; - raw.push(0); + let raw = [19u32.to_be_bytes().as_slice(), &[19; 19]].concat(); assert!(decode(raw).is_err()); } - #[tokio::test] - async fn compression_does_not_allow_oversized_raw_sets() { + #[test] + fn compression_does_not_allow_oversized_raw_sets() { let oversized = vec![0; MAX_TXSET_XDR_SIZE + 1]; - let data = Arc::new(TxSetData::from(oversized)); - assert!(data.encoded().await.is_err()); - // Even if its compressed bytes fit, an over-limit raw size is rejected. + assert!(TxSetData::from_local(oversized.clone()).is_err()); let frame = [ - (MAX_TXSET_XDR_SIZE as u32 + 1).to_be_bytes().as_slice(), - zstd::bulk::compress(data.as_slice(), 1).unwrap().as_slice(), + (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_TXSET_XDR_SIZE + 5]).is_err()); + assert!(decode(vec![0; max_encoded_size() + 1]).is_err()); } } diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 551aa542a..1150902a7 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -43,12 +43,10 @@ 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"); -pub const COMPRESSED_TXSET_PROTOCOL: StreamProtocol = - StreamProtocol::new("/stellar/txset/zstd/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) @@ -75,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). @@ -135,15 +133,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, } @@ -153,7 +147,6 @@ impl PeerOutboundStreams { scp: Mutex::new(None), tx: Mutex::new(None), txset: Mutex::new(None), - legacy_txset_request: Mutex::new(None), } } } @@ -541,103 +534,23 @@ impl StellarOverlay { self.run_event_loop().await; } - pub async fn run_event_loop(self) { - self.run_event_loop_with_control( - #[cfg(test)] - true, - #[cfg(test)] - true, - ) - .await; - } - - // Test-only switches exercise real negotiation with legacy peers. - // Production always advertises control and compressed responses. - async fn run_event_loop_with_control( - mut self, - #[cfg(test)] accept_control: bool, - #[cfg(test)] accept_compressed: bool, - ) { - #[cfg(not(test))] - let accept_compressed = true; - #[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 - }; - - let compressed_incoming = if accept_compressed { - match self.control.accept(COMPRESSED_TXSET_PROTOCOL) { - Ok(incoming) => Some(incoming), - Err(e) => { - error!("Failed to accept compressed TxSet 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(), - false, - )); - - if let Some(incoming) = compressed_incoming { - tokio::spawn(handle_inbound_txset_streams(incoming, state.clone(), true)); - } + tokio::spawn(handle_inbound_txset_streams(txset_incoming, state.clone())); // Spawn INV/GETDATA housekeeping task tokio::spawn(inv_getdata_housekeeping_task(state.clone())); @@ -1189,8 +1102,9 @@ async fn send_txset_response( peer ); - match send_peer_frames(&state, peer, StreamType::TxSet, PeerFrames::TxSet(&data)).await { - Ok(bytes) => { + 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 @@ -1275,7 +1189,6 @@ enum StreamType { Tx, TxSet, TxSetRequest, - LegacyTxSetRequest, } impl StreamType { @@ -1283,48 +1196,37 @@ impl StreamType { match self { StreamType::Control | StreamType::TxSetRequest => CONTROL_PROTOCOL, StreamType::Tx => TX_PROTOCOL, - StreamType::TxSet => COMPRESSED_TXSET_PROTOCOL, - 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 - } - Err(libp2p_stream::OpenStreamError::UnsupportedProtocol(_)) - if protocol == COMPRESSED_TXSET_PROTOCOL => - { - protocol = TXSET_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) @@ -1347,7 +1249,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; @@ -1392,45 +1293,7 @@ async fn send_to_peer_stream_parts( stream_type: StreamType, parts: &[&[u8]], ) -> io::Result<()> { - send_peer_frames( - state, - peer_id, - stream_type, - PeerFrames::Plain(framed_io::Frames::One(parts)), - ) - .await - .map(|_| ()) -} - -// Select response encoding using the protocol actually negotiated on this -// stream, including after reconnect. Other routes keep their original framing. -#[derive(Clone, Copy)] -enum PeerFrames<'a> { - Plain(framed_io::Frames<'a>), - TxSet(&'a Arc), -} - -impl PeerFrames<'_> { - async fn write(self, outbound: &mut OutboundStream) -> io::Result { - match self { - Self::Plain(frames) => { - frames.write(&mut outbound.stream).await?; - Ok(0) - } - Self::TxSet(data) => { - let (header, payload) = if outbound.protocol == COMPRESSED_TXSET_PROTOCOL { - data.encoded().await? - } else { - ( - (stellar_xdr::MessageType::GeneralizedTxSet as i32).to_be_bytes(), - data.as_slice(), - ) - }; - framed_io::write_frame_parts(&mut outbound.stream, &[&header, payload]).await?; - Ok(4 + payload.len()) - } - } - } + 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 @@ -1446,8 +1309,8 @@ async fn send_peer_frames( state: &SharedState, peer_id: PeerId, stream_type: StreamType, - frames: PeerFrames<'_>, -) -> io::Result { + frames: framed_io::Frames<'_>, +) -> io::Result<()> { // Retry up to 2 times (3 attempts total) for reliability const MAX_RETRIES: usize = 2; @@ -1469,7 +1332,6 @@ async fn send_peer_frames( 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; @@ -1522,20 +1384,8 @@ async fn send_peer_frames( } 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_peer_frames( - state, - peer_id, - StreamType::LegacyTxSetRequest, - frames, - )) - .await; - } - match frames.write(stream).await { - Ok(bytes) => return Ok(bytes), + match frames.write(&mut stream.stream).await { + Ok(()) => return Ok(()), Err(e) => { // Clear the broken stream *stream_guard = None; @@ -1642,14 +1492,21 @@ async fn send_inv_batch(state: &Arc, peer: PeerId, batch: InvBatch) /// Read length-prefixed frame from stream 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), )); } @@ -1659,11 +1516,7 @@ async fn read_framed(stream: &mut R) -> io::Resul } /// 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 @@ -1692,9 +1545,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; } @@ -2058,10 +1909,8 @@ async fn serve_getdata(state: &SharedState, peer: PeerId, hashes: Vec<[u8; 32]>) batch.extend_from_slice(part); } } - let frames = PeerFrames::Plain(framed_io::Frames::Batch(&batch)); - let result = send_peer_frames(state, peer, StreamType::Tx, frames) - .await - .map(|_| ()); + 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 { @@ -2223,7 +2072,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 {}", @@ -2242,8 +2091,8 @@ fn handle_txset_request(state: &SharedState, peer_id: PeerId, hash: [u8; 32]) { } /// Deliver strict-decoded XDR identically for both response encodings. -async fn deliver_txset(state: &SharedState, peer_id: PeerId, txset_data: Vec) { - let hash = crate::xdr::sha256_hash(&txset_data); +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 = { @@ -2284,11 +2133,7 @@ async fn deliver_txset(state: &SharedState, peer_id: PeerId, txset_data: Vec } } -async fn handle_inbound_txset_streams( - mut incoming: IncomingStreams, - state: Arc, - compressed: bool, -) { +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); state.metrics.inbound_live.fetch_add(1, Ordering::Relaxed); @@ -2296,61 +2141,31 @@ async fn handle_inbound_txset_streams( 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); - if compressed { - // Decode and strict-parse outside the network runtime. Each - // stream awaits completion before reading another frame. - let decoded = tokio::task::spawn_blocking(move || { - use stellar_xdr::{GeneralizedTransactionSet, Limits, ReadXdr}; - let xdr = crate::flood::txset_encoding::decode(data)?; - GeneralizedTransactionSet::from_xdr(&xdr, Limits::none()) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - Ok::<_, io::Error>(xdr) - }) - .await; - match decoded { - Ok(Ok(xdr)) => deliver_txset(&state, peer_id, xdr).await, - result => warn!("TXSET_PARSE_ERR: Dropping malformed compressed TxSet from {}: {:?}", peer_id, result), - } - continue; - } - 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(); - deliver_txset(&state, peer_id, txset_data).await; - } - 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); @@ -3106,7 +2921,7 @@ mod tests { request_received = true; // Node1 responds with TxSet data - handle1.send_txset(hash, Arc::new(txset_data.clone().into()), from).await; + handle1.send_txset(hash, Arc::new(TxSetData::from_local(txset_data.clone()).unwrap()), from).await; } } _ = tokio::time::sleep(Duration::from_millis(10)) => {} @@ -3126,7 +2941,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; } } @@ -3890,7 +3705,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, Arc::new(txset_data.clone().into()), from).await; + handle1.send_txset(hash, Arc::new(TxSetData::from_local(txset_data.clone()).unwrap()), from).await; responded = true; } } @@ -3911,7 +3726,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; } } @@ -4007,7 +3822,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, Arc::new(data.into()), from).await; + handle1.send_txset(hash, Arc::new(TxSetData::from_local(data).unwrap()), from).await; } } _ = tokio::time::sleep(Duration::from_millis(10)) => {} @@ -4342,7 +4157,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, Arc::new(txset_data.into()), peer2_id) + .send_txset( + txset_hash, + Arc::new(TxSetData::from_local(txset_data).unwrap()), + peer2_id, + ) .await; }); @@ -4470,7 +4289,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, Arc::new(txset_data.clone().into()), peer1_id) + .send_txset( + txset_hash, + Arc::new(TxSetData::from_local(txset_data.clone()).unwrap()), + peer1_id, + ) .await; // Verify node1 receives the TxSet response @@ -4480,7 +4303,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 8f5e707dd..02f75f6b2 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, Arc::new(data.into()), 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,14 +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 { - Self::start_with_protocols(accept_control, true).await - } - - async fn start_with_protocols(accept_control: bool, accept_compressed: bool) -> Self { let (handle, events, admissions, mut overlay) = create_test_overlay(Keypair::generate_ed25519(), Arc::new(OverlayMetrics::new())) .unwrap(); @@ -120,8 +114,7 @@ impl TestNode { }) .await .expect("listener did not start"); - let task = - tokio::spawn(overlay.run_event_loop_with_control(accept_control, accept_compressed)); + let task = tokio::spawn(overlay.run_event_loop()); Self { handle, events, @@ -593,65 +586,116 @@ async fn inv_batches_completed_by_relay_or_local_broadcast_reach_the_peer() { } #[tokio::test] -async fn txset_encoding_negotiates_with_legacy_peers_and_after_reopening() { - for (control, compressed) in [(true, true), (true, false), (false, false)] { - let mut sender = TestNode::start().await; - let mut receiver = TestNode::start_with_protocols(control, compressed).await; - sender.connect(&receiver).await; - let streams = sender.streams_to(receiver.peer).await; - let (hash, xdr) = large_txset(); - let shared = Arc::new(TxSetData::from(xdr.clone())); - for reopen in [false, true] { - if reopen { - streams.txset.lock().await.take(); +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; + } } - let bytes = send_peer_frames( - &sender.state, - receiver.peer, - StreamType::TxSet, - PeerFrames::TxSet(&shared), - ) - .await - .unwrap(); - if compressed { - assert!( - bytes < xdr.len() / 2, - "large responses must use compressed bytes" - ); - } else { - assert_eq!(bytes, xdr.len() + 4); + }) + .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() { - let guard = streams.txset.lock().await; - let outbound = guard.as_ref().unwrap(); - assert_eq!( - outbound.protocol, - if compressed { - COMPRESSED_TXSET_PROTOCOL - } else { - TXSET_PROTOCOL - } - ); - assert_eq!(outbound.stream.priority().unwrap(), 1); + assert_eq!(got, hash); + assert_eq!(data.as_slice(), xdr); + assert_eq!(data.encoded(), encoded); + break; } - 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, xdr); - break; - } - } - }) - .await - .expect("negotiated response was not delivered intact"); } - sender.stop().await; - receiver.stop().await; - } + }) + .await + .unwrap(); + leader.stop().await; + relay.stop().await; + follower.stop().await; } #[tokio::test] @@ -660,25 +704,36 @@ async fn malformed_compressed_txsets_are_dropped_without_losing_next_frame() { 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(vec![0xff; 1000])); - send_peer_frames( + let malformed = Arc::new(TxSetData::from_local(vec![0xff; 1000]).unwrap()); + send_to_peer_stream( &sender.state, receiver.peer, StreamType::TxSet, - PeerFrames::TxSet(&malformed), + malformed.encoded(), ) .await .unwrap(); let (hash, xdr) = test_txset_xdr(19); - // The uncompressed representation on the new protocol is also accepted. + 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(); - send_to_peer_stream(&sender.state, receiver.peer, StreamType::TxSet, &raw) - .await - .unwrap(); + 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 { @@ -686,7 +741,7 @@ async fn malformed_compressed_txsets_are_dropped_without_losing_next_frame() { } = receiver.events.recv().await.unwrap() { assert_eq!(got, hash, "malformed XDR was forwarded"); - assert_eq!(data, xdr); + assert_eq!(data.as_slice(), xdr); break; } } @@ -886,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, Arc::new(data.clone().into()), 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 @@ -895,14 +954,18 @@ async fn blocked_peer_does_not_delay_other_txsets_or_scp_delivery() { healthy_streams.txset.lock().await.take(); sender .handle - .send_txset(hash, Arc::new(data.clone().into()), 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, - Arc::new(second_data.clone().into()), + Arc::new(TxSetData::from_local(second_data.clone()).unwrap()), healthy.peer, ) .await; @@ -924,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, @@ -949,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; } } @@ -977,7 +1040,7 @@ async fn txset_fetch_reaches_peer_while_response_to_same_peer_is_blocked() { .handle .send_txset( response_hash, - Arc::new(response_data.clone().into()), + Arc::new(TxSetData::from_local(response_data.clone()).unwrap()), receiver.peer, ) .await; @@ -1025,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; } } @@ -1048,15 +1111,14 @@ 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. - send_peer_frames( + send_to_peer_stream( &sender.state, receiver.peer, StreamType::TxSet, - PeerFrames::TxSet(&Arc::new(data.clone().into())), + TxSetData::from_local(data.clone()).unwrap().encoded(), ) .await .unwrap(); @@ -1088,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::LegacyTxSetRequest, - &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(); @@ -1185,9 +1170,13 @@ async fn bulk_send_admission_is_bounded(byte_limit: bool) { 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, Arc::new(data.clone().into()), peer) + .send_txset( + hash, + Arc::new(TxSetData::from_local(data.clone()).unwrap()), + peer, + ) .await; - let waiting = handle.send_txset(hash, Arc::new(data.into()), peer); + 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()) @@ -1227,7 +1216,11 @@ async fn failed_txset_send_releases_admission() { 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, Arc::new(data.into()), PeerId::random()) + .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) @@ -1301,7 +1294,7 @@ 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(TxSetData::from(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/main.rs b/overlay/src/main.rs index 572a3a218..ac6fbc522 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.into()), + 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, @@ -1054,13 +1054,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 +1076,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); } @@ -2054,13 +2082,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 +2208,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 +2248,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 +2259,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 +2299,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 +2315,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 +2350,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 +2395,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 +2414,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 +2496,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/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 9b3dfe58e..b7a686e0a 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1790,7 +1790,23 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) invalidTxPhases.resize(txPhases.size()); std::tie(proposedSet, applicableProposedSet) = makeTxSetFromTransactions( - txPhases, mApp, closeTimeOffset, invalidTxPhases); + txPhases, mApp, closeTimeOffset, invalidTxPhases +#ifdef BUILD_TESTS + , + false, {}, false +#endif + , + [&](TxSetXDRFrameConstPtr const& txSet) { + // Hand the final XDR to the overlay before the builder's remaining + // validation. Its eager compression runs concurrently in Rust. + if (txSet->isGeneralizedTxSet()) + { + GeneralizedTransactionSet xdrTxSet; + txSet->toXDR(xdrTxSet); + overlayMgr.cacheTxSet(txSet->getContentsHash(), + xdr::xdr_to_opaque(xdrTxSet), ledgerSeq); + } + }); CLOG_INFO(Herder, "Proposed TX set has {} transactions", proposedSet->sizeTxTotal()); @@ -1994,6 +2010,15 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, 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()) + { + GeneralizedTransactionSet xdrTxSet; + proposedSet->toXDR(xdrTxSet); + mApp.getOverlayManager().cacheTxSet( + proposedSet->getContentsHash(), xdr::xdr_to_opaque(xdrTxSet), + lcl.header.ledgerSeq + 1); + } } #endif @@ -2008,20 +2033,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 diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index c8b514eb9..bad5528cc 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -843,7 +843,8 @@ makeTxSetFromTransactions( txtest::ParallelSorobanOrder const& parallelSorobanOrder, bool disableTxValidationForLegacyScenario #endif -) + , + TxSetReadyCallback const& onTxSetReady) { releaseAssert(threadIsMain()); releaseAssert(!app.getLedgerManager().isApplying()); @@ -956,6 +957,14 @@ makeTxSetFromTransactions( } #endif + // 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); + } + ApplicableTxSetFrameConstPtr outputApplicableTxSet = outputTxSet->prepareForApply(app, lclHeader.header); diff --git a/src/herder/TxSetFrame.h b/src/herder/TxSetFrame.h index 66f926f70..b2ac0e9be 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; @@ -152,7 +153,8 @@ makeTxSetFromTransactions( txtest::ParallelSorobanOrder const& parallelSorobanOrder = {}, bool disableTxValidationForLegacyScenario = false #endif -); + , + TxSetReadyCallback const& onTxSetReady = {}); #ifdef BUILD_TESTS std::pair @@ -405,7 +407,8 @@ class TxSetPhaseFrame txtest::ParallelSorobanOrder const& parallelSorobanOrder, bool disableTxValidationForLegacyScenario #endif - ); + , + TxSetReadyCallback const& onTxSetReady); #ifdef BUILD_TESTS friend std::pair makeTxSetFromTransactions( @@ -579,7 +582,8 @@ class ApplicableTxSetFrame txtest::ParallelSorobanOrder const& parallelSorobanOrder, bool disableTxValidationForLegacyScenario #endif - ); + , + TxSetReadyCallback const& onTxSetReady); #ifdef BUILD_TESTS friend std::pair makeTxSetFromTransactions( diff --git a/src/herder/test/TxSetTests.cpp b/src/herder/test/TxSetTests.cpp index 133a57ce5..3e497f48f 100644 --- a/src/herder/test/TxSetTests.cpp +++ b/src/herder/test/TxSetTests.cpp @@ -33,6 +33,44 @@ namespace { using namespace txtest; +TEST_CASE("built tx set is available before final validation returns", + "[txset][eager-compression]") +{ + auto const protocol = GENERATE(27, 28); + auto cfg = getTestConfig(); + cfg.LEDGER_PROTOCOL_VERSION = protocol; + cfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION = protocol; + cfg.GENESIS_TEST_ACCOUNT_COUNT = 3; + VirtualClock clock; + auto app = createTestApplication(clock, cfg); + auto source = getGenesisAccount(*app, 0); + auto destination = getGenesisAccount(*app, 1); + auto tx = transactionFromOperations( + *app, source.getSecretKey(), source.nextSequenceNumber(), + {payment(destination.getPublicKey(), 1)}, 100); + PerPhaseTransactionList phases{{tx}, {}}; + PerPhaseTransactionList invalid(2); + TxSetXDRFrameConstPtr ready; + size_t callbacks = 0; + auto [wire, applicable] = makeTxSetFromTransactions( + phases, *app, ApplyTimeOffset{}, invalid, false, {}, false, + [&](TxSetXDRFrameConstPtr const& set) { + ++callbacks; + ready = set; + REQUIRE(set->isGeneralizedTxSet()); + REQUIRE(set->sizeTxTotal() == 1); + GeneralizedTransactionSet xdrSet; + set->toXDR(xdrSet); + REQUIRE(xdrSha256(xdrSet) == set->getContentsHash()); + }); + REQUIRE(callbacks == 1); + REQUIRE(ready == wire); + REQUIRE(invalid[0].empty()); + REQUIRE(invalid[1].empty()); + REQUIRE(applicable); + REQUIRE(applicable->checkValid(*app, 0, 0)); +} + TEST_CASE("generalized tx set XDR validation", "[txset]") { Config cfg(getTestConfig()); From 0fd7d0edf1ba9f896745f6df5d03b45f9647576e Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:03:55 -0800 Subject: [PATCH 16/39] Hash externalized transactions without rebuilding their frames HerderImpl::processExternalized needs two things from every transaction in the externalized set on the main thread: its full hash, so the Rust overlay can drop it from the mempool, and (in BUILD_TESTS) its source account, so loadgen can release it. It got both by calling createTransactionFrames, which copies every envelope into a new TransactionFrame (with an OperationFrame per operation) only to hash it and read its source, then frees them all. That is thousands of frames per ledger at perfnet's load. Add TxSetXDRFrame::forEachTransactionEnvelope, which visits the set's envelopes in createTransactionFrames' order, and getTransactionFullHashes, which hashes each envelope's XDR in place: the same SHA-256 that TransactionFrame::getFullHash and FeeBumpTransactionFrame::getFullHash compute. LoadGenerator::cleanupAccounts takes the tx set and reads each source from the envelope with getEnvelopeSourceID, which mirrors getSourceID: the unmuxed source, or a fee bump's inner source. The overlay receives the same hashes in the same order, and loadgen releases the same accounts. Add a test that builds legacy and generalized sets (sequential and parallel phases; v0, v1, muxed and fee-bump envelopes) from XDR and checks the visited envelopes, full hashes and sources against the frames createTransactionFrames builds from the same set. --- src/herder/HerderImpl.cpp | 16 +-- src/herder/TxSetFrame.cpp | 62 ++++++++++++ src/herder/TxSetFrame.h | 10 ++ src/simulation/LoadGenerator.cpp | 46 ++++++--- src/simulation/LoadGenerator.h | 8 +- src/simulation/test/LoadGeneratorTests.cpp | 111 +++++++++++++++++++++ 6 files changed, 226 insertions(+), 27 deletions(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 9d25ad692..9b3dfe58e 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -349,22 +349,16 @@ HerderImpl::processExternalized(uint64 slotIndex, StellarValue const& value, releaseAssert(externalizedSet != nullptr); // Notify overlay to clear TXs from mempool (for RustOverlayManager) - // Extract TX hashes from the externalized set so Rust can remove them + // Extract TX hashes from the externalized set so Rust can remove them. + // Hash the envelopes in place: building a frame per transaction would + // copy every envelope on the main thread only to hash it. std::vector txHashes; if (externalizedSet) { - auto txFramesList = - externalizedSet->createTransactionFrames(mApp.getNetworkID()); - for (auto const& txPhase : txFramesList) - { - for (auto const& txFrame : txPhase) - { - txHashes.push_back(txFrame->getFullHash()); - } - } + txHashes = externalizedSet->getTransactionFullHashes(); #ifdef BUILD_TESTS mApp.getLoadGenerator().cleanupAccounts( - static_cast(slotIndex), txFramesList); + static_cast(slotIndex), *externalizedSet); #endif } mApp.getOverlayManager().notifyTxSetExternalized(value.txSetHash, txHashes); diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index 03b391422..c8b514eb9 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -1367,6 +1367,68 @@ TxSetXDRFrame::createTransactionFrames(Hash const& networkID) const return phaseTxs; } +void +TxSetXDRFrame::forEachTransactionEnvelope( + std::function const& f) const +{ + if (isGeneralizedTxSet()) + { + auto const& txSet = + std::get(mXDRTxSet).v1TxSet(); + for (auto const& phase : txSet.phases) + { + switch (phase.v()) + { + case 0: + for (auto const& component : phase.v0Components()) + { + for (auto const& tx : component.txsMaybeDiscountedFee().txs) + { + f(tx); + } + } + break; + case 1: + for (auto const& stage : + phase.parallelTxsComponent().executionStages) + { + for (auto const& cluster : stage) + { + for (auto const& tx : cluster) + { + f(tx); + } + } + } + break; + default: + break; + } + } + } + else + { + for (auto const& tx : std::get(mXDRTxSet).txs) + { + f(tx); + } + } +} + +std::vector +TxSetXDRFrame::getTransactionFullHashes() const +{ + ZoneScoped; + std::vector hashes; + hashes.reserve(sizeTxTotal()); + // Same digest as TransactionFrame::getFullHash and + // FeeBumpTransactionFrame::getFullHash: SHA-256 of the envelope's XDR. + forEachTransactionEnvelope([&hashes](TransactionEnvelope const& tx) { + hashes.emplace_back(xdrSha256(tx)); + }); + return hashes; +} + size_t TxSetXDRFrame::encodedSize() const { diff --git a/src/herder/TxSetFrame.h b/src/herder/TxSetFrame.h index fbf5e182c..66f926f70 100644 --- a/src/herder/TxSetFrame.h +++ b/src/herder/TxSetFrame.h @@ -261,6 +261,16 @@ class TxSetXDRFrame : public NonMovableOrCopyable PerPhaseTransactionList createTransactionFrames(Hash const& networkID) const; + // Calls `f` with the envelope of every transaction in the set, in the + // order createTransactionFrames returns their frames, without building + // any frames (which copies every envelope). + void forEachTransactionEnvelope( + std::function const& f) const; + + // Full hashes (`TransactionFrameBase::getFullHash`) of every transaction + // in the set, in the same order, hashed directly from the XDR. + std::vector getTransactionFullHashes() const; + #ifdef BUILD_TESTS mutable ApplicableTxSetFrameConstPtr mApplicableTxSetOverride; diff --git a/src/simulation/LoadGenerator.cpp b/src/simulation/LoadGenerator.cpp index 6091df1b6..c763d764f 100644 --- a/src/simulation/LoadGenerator.cpp +++ b/src/simulation/LoadGenerator.cpp @@ -231,9 +231,31 @@ LoadGenerator::getTxPerStep(uint32_t txRate, std::chrono::seconds spikeInterval, return txs - static_cast(submittedSoFar); } +AccountID +LoadGenerator::getEnvelopeSourceID(TransactionEnvelope const& env) +{ + // Mirrors TransactionFrame::getSourceID and + // FeeBumpTransactionFrame::getSourceID (the inner transaction's source). + switch (env.type()) + { + case ENVELOPE_TYPE_TX_V0: + { + AccountID res; + res.ed25519() = env.v0().tx.sourceAccountEd25519; + return res; + } + case ENVELOPE_TYPE_TX: + return toAccountID(env.v1().tx.sourceAccount); + case ENVELOPE_TYPE_TX_FEE_BUMP: + return toAccountID(env.feeBump().tx.innerTx.v1().tx.sourceAccount); + default: + // TransactionFrameBase::makeTransactionFromWire aborts here too. + abort(); + } +} + void -LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, - PerPhaseTransactionList const& perPhaseTxs) +LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, TxSetXDRFrame const& txSet) { ZoneScoped; @@ -285,20 +307,16 @@ LoadGenerator::cleanupAccounts(uint32_t ledgerSeq, // holds every node's transactions; looking each source up in the in-use // index keeps this proportional to the set, however large the mempool // backlog grows, and the lookups allocate nothing. - for (auto const& txs : perPhaseTxs) - { - for (auto const& tx : txs) + txSet.forEachTransactionEnvelope([&](TransactionEnvelope const& env) { + auto it = mAccountsInUseByKey.find(getEnvelopeSourceID(env)); + if (it == mAccountsInUseByKey.end()) { - auto it = mAccountsInUseByKey.find(tx->getSourceID()); - if (it == mAccountsInUseByKey.end()) - { - continue; - } - mAccountsExternalized.emplace(it->second, ledgerSeq); - mAccountsInUse.erase(it->second); - mAccountsInUseByKey.erase(it); + return; } - } + mAccountsExternalized.emplace(it->second, ledgerSeq); + mAccountsInUse.erase(it->second); + mAccountsInUseByKey.erase(it); + }); } // Reset everything except Soroban persistent state diff --git a/src/simulation/LoadGenerator.h b/src/simulation/LoadGenerator.h index 0664e40ae..97ca72ecd 100644 --- a/src/simulation/LoadGenerator.h +++ b/src/simulation/LoadGenerator.h @@ -227,8 +227,12 @@ class LoadGenerator void stop(); - void cleanupAccounts(uint32_t ledgerSeq, - PerPhaseTransactionList const& perPhaseTxs); + void cleanupAccounts(uint32_t ledgerSeq, TxSetXDRFrame const& txSet); + + // The source account of the transaction in `env`, as + // TransactionFrameBase::getSourceID reports it for a frame built from + // `env`: the unmuxed source, or the inner transaction's for a fee bump. + static AccountID getEnvelopeSourceID(TransactionEnvelope const& env); private: struct TxMetrics diff --git a/src/simulation/test/LoadGeneratorTests.cpp b/src/simulation/test/LoadGeneratorTests.cpp index 52de6a0bd..7e482fb35 100644 --- a/src/simulation/test/LoadGeneratorTests.cpp +++ b/src/simulation/test/LoadGeneratorTests.cpp @@ -378,6 +378,117 @@ TEST_CASE("synthetic SAC load recycles source accounts in overlay-only mode", REQUIRE(bytes.count() - bytesBefore == nTxs * refMsgBytes); } +TEST_CASE("externalized tx set hashes and sources match transaction frames", + "[loadgen][herder][txset]") +{ + // Herder hands the overlay getTransactionFullHashes() and loadgen the + // envelopes from forEachTransactionEnvelope() instead of building a frame + // per transaction. Pin both to what frames built from the same set give: + // same order, same full hashes, same source accounts. + Hash const networkID = sha256("externalized tx set test network"); + auto key = [](std::string const& name) { + return txtest::getAccount(name).getPublicKey(); + }; + uint32_t nextSeq = 1; + auto v1Tx = [&](PublicKey const& source, + std::optional muxId = std::nullopt) { + TransactionEnvelope env(ENVELOPE_TYPE_TX); + env.v1().tx.sourceAccount = toMuxedAccount(source, muxId); + env.v1().tx.fee = 100; + env.v1().tx.seqNum = nextSeq++; + env.v1().tx.operations.emplace_back(txtest::payment(key("dest"), 1)); + return env; + }; + auto v0Tx = [&](PublicKey const& source) { + TransactionEnvelope env(ENVELOPE_TYPE_TX_V0); + env.v0().tx.sourceAccountEd25519 = source.ed25519(); + env.v0().tx.fee = 100; + env.v0().tx.seqNum = nextSeq++; + env.v0().tx.operations.emplace_back(txtest::payment(key("dest"), 1)); + return env; + }; + auto feeBumpTx = [&](PublicKey const& feeSource, PublicKey const& source) { + TransactionEnvelope env(ENVELOPE_TYPE_TX_FEE_BUMP); + env.feeBump().tx.feeSource = toMuxedAccount(feeSource, 7); + env.feeBump().tx.fee = 400; + env.feeBump().tx.innerTx.type(ENVELOPE_TYPE_TX); + env.feeBump().tx.innerTx.v1() = v1Tx(source).v1(); + return env; + }; + + auto checkMatchesFrames = [&](TxSetXDRFrameConstPtr const& txSet, + size_t expectedTxs) { + std::vector frames; + for (auto const& phase : txSet->createTransactionFrames(networkID)) + { + frames.insert(frames.end(), phase.begin(), phase.end()); + } + std::vector envelopes; + txSet->forEachTransactionEnvelope( + [&](TransactionEnvelope const& env) { envelopes.push_back(env); }); + auto hashes = txSet->getTransactionFullHashes(); + + REQUIRE(frames.size() == expectedTxs); + REQUIRE(envelopes.size() == expectedTxs); + REQUIRE(hashes.size() == expectedTxs); + for (size_t i = 0; i < expectedTxs; ++i) + { + REQUIRE(envelopes[i] == frames[i]->getEnvelope()); + REQUIRE(hashes[i] == frames[i]->getFullHash()); + REQUIRE(LoadGenerator::getEnvelopeSourceID(envelopes[i]) == + frames[i]->getSourceID()); + } + }; + + SECTION("source account of each envelope type") + { + REQUIRE(LoadGenerator::getEnvelopeSourceID(v0Tx(key("a"))) == key("a")); + REQUIRE(LoadGenerator::getEnvelopeSourceID(v1Tx(key("b"))) == key("b")); + // Muxed sources resolve to the underlying account. + REQUIRE(LoadGenerator::getEnvelopeSourceID(v1Tx(key("c"), 42)) == + key("c")); + // A fee bump's source is its inner transaction's, not the fee source. + REQUIRE(LoadGenerator::getEnvelopeSourceID( + feeBumpTx(key("fee"), key("inner"))) == key("inner")); + } + SECTION("legacy tx set") + { + TransactionSet xdrSet; + xdrSet.previousLedgerHash = sha256("previous ledger"); + xdrSet.txs = {v0Tx(key("a")), v1Tx(key("b")), v1Tx(key("c"), 42), + feeBumpTx(key("fee"), key("inner"))}; + checkMatchesFrames(TxSetXDRFrame::makeFromWire(xdrSet), 4); + } + SECTION("generalized tx set with sequential and parallel phases") + { + GeneralizedTransactionSet xdrSet(1); + auto& v1Set = xdrSet.v1TxSet(); + v1Set.previousLedgerHash = sha256("previous ledger"); + + auto& classic = v1Set.phases.emplace_back(0); + for (auto const& txs : + {std::vector{v1Tx(key("a")), v0Tx(key("b"))}, + std::vector{ + v1Tx(key("c"), 42), feeBumpTx(key("fee"), key("d"))}}) + { + auto& component = classic.v0Components().emplace_back( + TXSET_COMP_TXS_MAYBE_DISCOUNTED_FEE); + component.txsMaybeDiscountedFee().txs.assign(txs.begin(), + txs.end()); + } + + auto& parallel = v1Set.phases.emplace_back(1); + auto& stages = parallel.parallelTxsComponent().executionStages; + auto& stage0 = stages.emplace_back(); + stage0.push_back({v1Tx(key("e")), v1Tx(key("f"))}); + stage0.push_back({feeBumpTx(key("fee"), key("g"))}); + auto& stage1 = stages.emplace_back(); + stage1.push_back({v1Tx(key("h"), 9)}); + + checkMatchesFrames(TxSetXDRFrame::makeFromWire(xdrSet), 8); + } +} + TEST_CASE("generate load with unique accounts", "[loadgen]") { Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); From e545aca8b386fe04f25be19529a781b6568dbe6d Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Thu, 17 Sep 2026 14:17:47 -0700 Subject: [PATCH 17/39] Clarify compressed transaction set delivery ownership --- overlay/src/libp2p_overlay.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 1150902a7..38040657a 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -2090,7 +2090,7 @@ fn handle_txset_request(state: &SharedState, peer_id: PeerId, hash: [u8; 32]) { } } -/// Deliver strict-decoded XDR identically for both response encodings. +/// 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()); From 223f00ebc7b6fa823025886671b86798e4d98264 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Thu, 17 Sep 2026 15:45:32 -0700 Subject: [PATCH 18/39] Build local transaction sets only for nomination leaders --- docs/rust-overlay/txset-fetching.md | 9 +++ src/herder/HerderImpl.cpp | 86 ++++++++++++++++++------ src/herder/HerderImpl.h | 2 + src/herder/HerderSCPDriver.cpp | 42 ++++++++---- src/herder/HerderSCPDriver.h | 3 +- src/herder/test/HerderTests.cpp | 86 ++++++++++++++++++++++-- src/scp/NominationProtocol.cpp | 39 +++++++---- src/scp/NominationProtocol.h | 4 +- src/scp/SCP.cpp | 4 +- src/scp/SCP.h | 4 +- src/scp/SCPDriver.h | 5 ++ src/scp/Slot.cpp | 5 +- src/scp/Slot.h | 4 +- src/scp/test/SCPTests.cpp | 3 +- src/scp/test/SCPUnitTests.cpp | 100 +++++++++++++++++++++++++++- src/simulation/ApplyLoad.cpp | 9 ++- 16 files changed, 339 insertions(+), 66 deletions(-) diff --git a/docs/rust-overlay/txset-fetching.md b/docs/rust-overlay/txset-fetching.md index 2900f9529..826bf17cf 100644 --- a/docs/rust-overlay/txset-fetching.md +++ b/docs/rust-overlay/txset-fetching.md @@ -24,6 +24,15 @@ 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 pulling the +mempool, building a private set, or compressing it. 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. + 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 diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index b7a686e0a..36c3e4c95 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1507,7 +1507,7 @@ HerderImpl::setupTriggerNextLedger() if (getSCP().isValidator()) { auto leaders = getSCP().predictNominationLeaders( - nextIndex, xdr::xdr_to_opaque(lcl.header.scpValue), 2); + nextIndex, xdr::xdr_to_opaque(lcl.header.scpValue), 1); if (leaders.count(getSCP().getLocalNodeID())) { // Validate against the close time we expect at the trigger, @@ -1680,6 +1680,15 @@ HerderImpl::prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) { return; } + auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); + if (!getSCP().isValidator() || + !getSCP() + .predictNominationLeaders( + ledgerSeq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) + .count(getSCP().getLocalNodeID())) + { + return; + } mPreparedTxSet = buildTxSet(ledgerSeq, closeTime); CLOG_INFO(Herder, "Prepared TX set for ledger {} before trigger: {} transactions, " @@ -1695,6 +1704,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)); + bool const activeLeader = getSCP().getNominationLeaders(ledgerSeq).count( + getSCP().getLocalNodeID()); + releaseAssert(activeLeader || + getSCP() + .predictNominationLeaders( + ledgerSeq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) + .count(getSCP().getLocalNodeID())); + CLOG_INFO(Herder, "Building TX set as {} nomination leader for ledger {}", + activeLeader ? "active" : "first-round", ledgerSeq); // Apply-time offset: see the comment in triggerNextLedger auto const closeTimeOffset = closeTime.toApplyTime() - getApplyTime(lcl.header.scpValue); @@ -1948,10 +1966,50 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, } // 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. + // nominate. A skipped trigger must not credit the time spent waiting for + // peers to the drift allowance. mHerderSCPDriver.recordNominationTrigger(ledgerSeqToTrigger); + if (!getSCP().isValidator()) + { + CLOG_DEBUG(Herder, "Non-validating node, skipping nomination (SCP)."); + return; + } + + getHerderSCPDriver().recordSCPEvent(ledgerSeqToTrigger, true); + mHerderSCPDriver.nominate( + ledgerSeqToTrigger, + [this, ledgerSeqToTrigger, checkTrackingSCP]() { + return makeNominationValue(ledgerSeqToTrigger, checkTrackingSCP); + }, + lcl.header.scpValue); +} + +ValueWrapperPtr +HerderImpl::makeNominationValue(uint32_t ledgerSeqToTrigger, + bool checkTrackingSCP) +{ + // 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. + if ((!isTracking() && checkTrackingSCP) || !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 // and the cached validation result. @@ -1995,7 +2053,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 @@ -2015,9 +2073,9 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, { GeneralizedTransactionSet xdrTxSet; proposedSet->toXDR(xdrTxSet); - mApp.getOverlayManager().cacheTxSet( - proposedSet->getContentsHash(), xdr::xdr_to_opaque(xdrTxSet), - lcl.header.ledgerSeq + 1); + mApp.getOverlayManager().cacheTxSet(proposedSet->getContentsHash(), + xdr::xdr_to_opaque(xdrTxSet), + lcl.header.ledgerSeq + 1); } } #endif @@ -2044,7 +2102,7 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, // applying if (ledgerSeqToTrigger != slotIndex || mLedgerManager.isApplying()) { - return; + return nullptr; } auto newUpgrades = emptyUpgradeSteps; @@ -2074,15 +2132,6 @@ 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 && getHerderSCPDriver().protocolAllowsEmptyTxSetValues()) @@ -2097,8 +2146,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 diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index bebbce06d..52b28ed82 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -269,6 +269,8 @@ class HerderImpl : public Herder PreparedTxSet buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); void prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); void discardPreparedTxSet(); + ValueWrapperPtr makeNominationValue(uint32_t ledgerSeq, + bool checkTrackingSCP); // Compute the trigger-timer anchor point using the local node's // prepare-start timestamp for the previous slot. Returns a pessimistic diff --git a/src/herder/HerderSCPDriver.cpp b/src/herder/HerderSCPDriver.cpp index d487c7d0b..550e5084a 100644 --- a/src/herder/HerderSCPDriver.cpp +++ b/src/herder/HerderSCPDriver.cpp @@ -1253,7 +1253,7 @@ HerderSCPDriver::valueExternalized(uint64_t slotIndex, Value const& value) logQuorumInformationAndUpdateMetrics(slotIndex - 2); } - if (mCurrentValue) + if (mLedgerSeqNominating != 0) { // stop nomination // this may or may not be the ledger that is currently externalizing @@ -1262,6 +1262,7 @@ HerderSCPDriver::valueExternalized(uint64_t slotIndex, Value const& value) // or we're going to trigger catchup from history mSCP.stopNomination(mLedgerSeqNominating); mCurrentValue.reset(); + mLedgerSeqNominating = 0; } if (!mHerder.isTracking()) @@ -1337,24 +1338,37 @@ HerderSCPDriver::logQuorumInformationAndUpdateMetrics(uint64_t index) } void -HerderSCPDriver::nominate(uint64_t slotIndex, StellarValue const& value, - TxSetXDRFrameConstPtr proposedSet, +HerderSCPDriver::nominate(uint64_t slotIndex, NominationValueSupplier makeValue, StellarValue const& previousValue) { ZoneScoped; - mCurrentValue = wrapStellarValue(value); + if (mLedgerSeqNominating != slotIndex) + { + mCurrentValue.reset(); + } mLedgerSeqNominating = static_cast(slotIndex); - - auto valueHash = xdrSha256(mCurrentValue->getValue()); - CLOG_DEBUG(Herder, - "HerderSCPDriver::triggerNextLedger txSet.size: {} " - "previousLedgerHash: {} value: {} slot: {}", - proposedSet->sizeTxTotal(), - hexAbbrev(proposedSet->previousLedgerHash()), - hexAbbrev(valueHash), slotIndex); - auto prevValue = xdr::xdr_to_opaque(previousValue); - mSCP.nominate(slotIndex, mCurrentValue, prevValue); + mSCP.nominate( + slotIndex, + [this, slotIndex, + makeValue = std::move(makeValue)]() -> ValueWrapperPtr { + if (mLedgerSeqNominating != slotIndex) + { + return nullptr; + } + if (!mCurrentValue) + { + auto value = makeValue(); + // Publishing the tx set may externalize a pending value. + if (mLedgerSeqNominating != slotIndex) + { + return nullptr; + } + mCurrentValue = std::move(value); + } + return mCurrentValue; + }, + prevValue); } SCPQuorumSetPtr diff --git a/src/herder/HerderSCPDriver.h b/src/herder/HerderSCPDriver.h index ec3b1bc9a..06354c82e 100644 --- a/src/herder/HerderSCPDriver.h +++ b/src/herder/HerderSCPDriver.h @@ -131,8 +131,7 @@ class HerderSCPDriver : public SCPDriver // Submit a value to consider for slotIndex // previousValue is the value from slotIndex-1 - void nominate(uint64_t slotIndex, StellarValue const& value, - TxSetXDRFrameConstPtr proposedSet, + void nominate(uint64_t slotIndex, NominationValueSupplier makeValue, StellarValue const& previousValue); SCPQuorumSetPtr getQSet(Hash const& qSetHash) override; diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 4fc6d2165..01ab1ebb6 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -6768,6 +6768,37 @@ class EarlyNominationTestAccess { herder.setupTriggerNextLedger(); } + + // These fixtures inspect a local proposal while requiring an absent peer + // to prevent externalization. Pick that peer so the local node is the + // first leader, independently of the test RNG seed and previous ledger. + static void + selectLocalFirstLeader(HerderImpl& herder) + { + auto& scp = herder.getSCP(); + auto const& lcl = + herder.mApp.getLedgerManager().getLastClosedLedgerHeader(); + auto qset = scp.getLocalQuorumSet(); + REQUIRE(qset.validators.size() == 2); + for (int i = 0; i < 100; ++i) + { + qset.validators = { + scp.getLocalNodeID(), + SecretKey::fromSeed(sha256(fmt::format("absent-peer-{}", i))) + .getPublicKey()}; + scp.updateLocalQuorumSet(qset); + if (scp.predictNominationLeaders( + lcl.header.ledgerSeq + 1, + xdr::xdr_to_opaque(lcl.header.scpValue), 1) + .count(scp.getLocalNodeID())) + { + herder.mPendingEnvelopes.addSCPQuorumSet( + scp.getLocalNode()->getQuorumSetHash(), qset); + return; + } + } + FAIL("Could not select a first-round local leader"); + } }; } @@ -6859,6 +6890,7 @@ TEST_CASE("trigger work is recorded by the proposal path", SecretKey::pseudoRandomForTesting().getPublicKey()}; auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); + EarlyNominationTestAccess::selectLocalFirstLeader(herder); auto& driver = herder.getHerderSCPDriver(); auto const slot = app->getLedgerManager().getLastClosedLedgerNum() + 1; herder.mGetTopTransactionsForTesting = [&](size_t) { @@ -7201,6 +7233,7 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") config.mLedgerMaxTxCount = 1; }); } + EarlyNominationTestAccess::selectLocalFirstLeader(herder); auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); // The Soroban upgrade helper closes ledgers at its fixed test date. The // sub-second offset makes the prepared close time carry milliseconds once @@ -7342,7 +7375,7 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") herder.mGetTopTransactionsForTesting = nullptr; } -TEST_CASE("only the first two nomination leaders prepare early", +TEST_CASE("only nomination leaders construct local proposals", "[herder][early-nomination]") { // A fixed, unanimous quorum gives all three nodes the same weights and @@ -7407,7 +7440,7 @@ TEST_CASE("only the first two nomination leaders prepare early", REQUIRE(checkedRoles.insert(role).second); CAPTURE(role); EarlyNominationTestAccess::schedule(herder); - REQUIRE(EarlyNominationTestAccess::scheduled(herder) == (role < 2)); + REQUIRE(EarlyNominationTestAccess::scheduled(herder) == (role == 0)); bool done = false; VirtualTimer stop(clock); stop.expires_from_now(std::chrono::milliseconds(1)); @@ -7416,11 +7449,53 @@ TEST_CASE("only the first two nomination leaders prepare early", app, [&]() { return done; }, std::chrono::seconds(1)); REQUIRE(done); REQUIRE(clock.now() < herder.getTriggerTimer().expiry_time()); - REQUIRE(pulls == (role < 2 ? 1 : 0)); + REQUIRE(pulls == (role == 0 ? 1 : 0)); REQUIRE(bool(EarlyNominationTestAccess::prepared(herder)) == - (role < 2)); + (role == 0)); REQUIRE(scp.getNominationLeaders(seq).empty()); REQUIRE(scp.getLatestMessagesSend(seq).empty()); + auto const trigger = herder.getTriggerTimer().expiry_time(); + clock.setCurrentVirtualTime(trigger); + auto const triggerSystemTime = clock.system_now(); + testutil::crankUntil( + app, [&]() { return !scp.getNominationLeaders(seq).empty(); }, + std::chrono::seconds(1)); + REQUIRE(scp.getNominationLeaders(seq) == first); + // The first leader refreshes its empty early snapshot. Followers do + // not even pull the mempool when starting nomination. + REQUIRE(pulls == (role == 0 ? 2 : 0)); + REQUIRE(scp.getLatestMessagesSend(seq).empty() == (role != 0)); + + // With the first leader silent, real nomination timers eventually + // promote each follower. It builds exactly then, using a current close + // time, and its prepared/validated tx set is available for retrieval. + testutil::crankUntil( + app, [&]() { return !scp.getLatestMessagesSend(seq).empty(); }, + std::chrono::seconds(30)); + REQUIRE(scp.getNominationLeaders(seq).count(scp.getLocalNodeID())); + REQUIRE(pulls == (role == 0 ? 2 : 1)); + auto messages = scp.getLatestMessagesSend(seq); + auto const& votes = messages.front().statement.pledges.nominate().votes; + REQUIRE(votes.size() == 1); + StellarValue nominated; + xdr::xdr_from_opaque(votes.front(), nominated); + auto txSet = std::get( + herder.getTxSet(nominated.txSetHash)); + REQUIRE(txSet); + REQUIRE(txSet->previousLedgerHash() == lcl.hash); + REQUIRE(herder.getHerderSCPDriver().validateValue(seq, votes.front(), + true) == + SCPDriver::kFullyValidatedValue); + if (role != 0) + { + REQUIRE(getConsensusTime(nominated) > + ConsensusTime::fromSystemTime(triggerSystemTime, + lcl.header.ledgerVersion)); + } + auto const pullsAfterVote = pulls; + // Reentering the trigger for this slot cannot build a second proposal. + herder.triggerNextLedger(seq, true); + REQUIRE(pulls == pullsAfterVote); herder.mGetTopTransactionsForTesting = nullptr; } REQUIRE(checkedRoles == std::set{0, 1, 2}); @@ -7444,6 +7519,7 @@ TEST_CASE("early underfilled proposals refresh at the trigger", SecretKey::pseudoRandomForTesting().getPublicKey()}; auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); + EarlyNominationTestAccess::selectLocalFirstLeader(herder); auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto const seq = lcl.header.ledgerSeq + 1; herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); @@ -7511,6 +7587,7 @@ TEST_CASE("early preparation respects manual and immediately due triggers", SecretKey::pseudoRandomForTesting().getPublicKey()}; auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); + EarlyNominationTestAccess::selectLocalFirstLeader(herder); auto const seq = app->getLedgerManager().getLastClosedLedgerNum() + 1; size_t pulls = 0; herder.mGetTopTransactionsForTesting = [&](size_t) { @@ -7525,6 +7602,7 @@ TEST_CASE("early preparation respects manual and immediately due triggers", } else { + EarlyNominationTestAccess::schedule(herder); // No previous prepare timestamp: the normal fallback triggers now. REQUIRE(herder.getTriggerTimer().expiry_time() == clock.now()); testutil::crankUntil( diff --git a/src/scp/NominationProtocol.cpp b/src/scp/NominationProtocol.cpp index fed728c4f..015bd8f81 100644 --- a/src/scp/NominationProtocol.cpp +++ b/src/scp/NominationProtocol.cpp @@ -593,8 +593,8 @@ NominationProtocol::stripUpgrades(ValueWrapperPtr& value) const // attempts to nominate a value for consensus bool -NominationProtocol::nominate(ValueWrapperPtr value, Value const& previousValue, - bool timedout) +NominationProtocol::nominate(NominationValueSupplier const& makeValue, + Value const& previousValue, bool timedout) { ZoneScoped; @@ -608,8 +608,7 @@ NominationProtocol::nominate(ValueWrapperPtr value, Value const& previousValue, return false; } - CLOG_DEBUG(SCP, "NominationProtocol::nominate ({}) {}", mRoundNumber, - mSlot.getSCP().getValueString(value->getValue())); + CLOG_DEBUG(SCP, "NominationProtocol::nominate ({})", mRoundNumber); bool updated = false; @@ -662,6 +661,7 @@ NominationProtocol::nominate(ValueWrapperPtr value, Value const& previousValue, mSlot.getSCPDriver().getUpgradeNominationTimeoutLimit(); bool shouldVoteForValue = false; + bool shouldStripUpgrades = false; // Add our value if we haven't added any votes yet. if (mVotes.empty()) { @@ -692,19 +692,34 @@ NominationProtocol::nominate(ValueWrapperPtr value, Value const& previousValue, { // All votes have upgrades, so strip upgrades from `value` and // vote for it. - stripUpgrades(value); + shouldStripUpgrades = true; shouldVoteForValue = true; } } if (shouldVoteForValue) { - auto ins = mVotes.insert(value); - if (ins.second) + auto value = makeValue(); + // Making the value can make pending envelopes ready and reenter + // SCP, potentially externalizing this slot or creating a candidate. + // Do not vote or rearm its timer after nomination has stopped. + if (!mNominationStarted || !mCandidates.empty()) { - updated = true; - mSlot.getSCPDriver().nominatingValue(mSlot.getSlotIndex(), - value->getValue()); + return false; + } + if (value) + { + if (shouldStripUpgrades) + { + stripUpgrades(value); + } + auto ins = mVotes.insert(value); + if (ins.second) + { + updated = true; + mSlot.getSCPDriver().nominatingValue(mSlot.getSlotIndex(), + value->getValue()); + } } } } @@ -712,8 +727,8 @@ NominationProtocol::nominate(ValueWrapperPtr value, Value const& previousValue, std::shared_ptr slot = mSlot.shared_from_this(); mSlot.getSCPDriver().setupTimer( mSlot.getSlotIndex(), Slot::NOMINATION_TIMER, timeout, - [slot, value, previousValue]() { - slot->nominate(value, previousValue, true); + [slot, makeValue, previousValue]() { + slot->nominate(makeValue, previousValue, true); }); #ifdef BUILD_TESTS diff --git a/src/scp/NominationProtocol.h b/src/scp/NominationProtocol.h index 9eb32dfcc..cf0e3656e 100644 --- a/src/scp/NominationProtocol.h +++ b/src/scp/NominationProtocol.h @@ -105,8 +105,8 @@ class NominationProtocol static std::vector getStatementValues(SCPStatement const& st); // attempts to nominate a value for consensus - bool nominate(ValueWrapperPtr value, Value const& previousValue, - bool timedout); + bool nominate(NominationValueSupplier const& makeValue, + Value const& previousValue, bool timedout); // stops the nomination protocol void stopNomination(); diff --git a/src/scp/SCP.cpp b/src/scp/SCP.cpp index 46e9778f3..8afbc3a6b 100644 --- a/src/scp/SCP.cpp +++ b/src/scp/SCP.cpp @@ -34,11 +34,11 @@ SCP::receiveEnvelope(SCPEnvelopeWrapperPtr envelope) } bool -SCP::nominate(uint64 slotIndex, ValueWrapperPtr value, +SCP::nominate(uint64 slotIndex, NominationValueSupplier const& makeValue, Value const& previousValue) { dbgAssert(isValidator()); - return getSlot(slotIndex, true)->nominate(value, previousValue, false); + return getSlot(slotIndex, true)->nominate(makeValue, previousValue, false); } std::set diff --git a/src/scp/SCP.h b/src/scp/SCP.h index f55f4a9f4..e49e3535d 100644 --- a/src/scp/SCP.h +++ b/src/scp/SCP.h @@ -51,9 +51,9 @@ class SCP // invokes the appropriate methods EnvelopeState receiveEnvelope(SCPEnvelopeWrapperPtr envelope); - // Submit a value to consider for slotIndex + // Start nomination for slotIndex, constructing a local value only if needed // previousValue is the value from slotIndex-1 - bool nominate(uint64 slotIndex, ValueWrapperPtr value, + bool nominate(uint64 slotIndex, NominationValueSupplier const& makeValue, Value const& previousValue); // stops nomination for a slot diff --git a/src/scp/SCPDriver.h b/src/scp/SCPDriver.h index 0a1a4e53a..19fcbfc5e 100644 --- a/src/scp/SCPDriver.h +++ b/src/scp/SCPDriver.h @@ -44,6 +44,11 @@ class ValueWrapper : public NonMovableOrCopyable typedef std::shared_ptr SCPQuorumSetPtr; typedef std::shared_ptr ValueWrapperPtr; +// Called only when the local node is a nomination leader and needs its own +// value. May return nullptr if the application can no longer propose for the +// slot. The callback must remain valid across nomination timeouts. +using NominationValueSupplier = std::function; + class WrappedValuePtrComparator { public: diff --git a/src/scp/Slot.cpp b/src/scp/Slot.cpp index bff055757..4971fc2e2 100644 --- a/src/scp/Slot.cpp +++ b/src/scp/Slot.cpp @@ -225,9 +225,10 @@ Slot::bumpState(Value const& value, bool force) } bool -Slot::nominate(ValueWrapperPtr value, Value const& previousValue, bool timedout) +Slot::nominate(NominationValueSupplier const& makeValue, + Value const& previousValue, bool timedout) { - return mNominationProtocol.nominate(value, previousValue, timedout); + return mNominationProtocol.nominate(makeValue, previousValue, timedout); } void diff --git a/src/scp/Slot.h b/src/scp/Slot.h index ecf1e8fee..3b556a904 100644 --- a/src/scp/Slot.h +++ b/src/scp/Slot.h @@ -125,8 +125,8 @@ class Slot : public std::enable_shared_from_this bool bumpState(Value const& value, bool force); // attempts to nominate a value for consensus - bool nominate(ValueWrapperPtr value, Value const& previousValue, - bool timedout); + bool nominate(NominationValueSupplier const& makeValue, + Value const& previousValue, bool timedout); void stopNomination(); diff --git a/src/scp/test/SCPTests.cpp b/src/scp/test/SCPTests.cpp index 24674fe98..8f57dda9b 100644 --- a/src/scp/test/SCPTests.cpp +++ b/src/scp/test/SCPTests.cpp @@ -189,7 +189,8 @@ class TestSCP : public SCPDriver nominate(uint64 slotIndex, Value const& value, bool timedout) { auto wv = wrapValue(value); - return mSCP.getSlot(slotIndex, true)->nominate(wv, value, timedout); + return mSCP.getSlot(slotIndex, true) + ->nominate([wv]() { return wv; }, value, timedout); } // only used by nomination protocol diff --git a/src/scp/test/SCPUnitTests.cpp b/src/scp/test/SCPUnitTests.cpp index dff33931e..50dbd484a 100644 --- a/src/scp/test/SCPUnitTests.cpp +++ b/src/scp/test/SCPUnitTests.cpp @@ -229,7 +229,7 @@ class LeaderPreviewTestDriver : public TestNominationSCP { return 0; } - auto winner = round < 3 ? nodes[0] : nodes[1]; + auto winner = round < 3 ? nodes[0] : round == 3 ? nodes[1] : nodes[2]; return id == winner || (tie && round < 3 && id == nodes[2]) ? 100 : 1; } @@ -275,7 +275,14 @@ TEST_CASE("nomination leader preview has no protocol side effects", REQUIRE(driver.emitted == 0); REQUIRE(driver.timers == 0); - slot->nominate(std::make_shared(value), previous, false); + size_t builds = 0; + slot->nominate( + [&]() { + ++builds; + return std::make_shared(value); + }, + previous, false); + REQUIRE(builds == 0); REQUIRE(slot->getNominationLeaders() == first); REQUIRE(driver.emitted == 0); REQUIRE(driver.timers == 1); @@ -289,6 +296,95 @@ TEST_CASE("nomination leader preview has no protocol side effects", REQUIRE(slot->getNominationLeaders() == firstTwo); REQUIRE(driver.emitted == 1); REQUIRE(driver.timers == 2); + REQUIRE(builds == 1); + timeout = std::move(driver.nominationTimeout); + REQUIRE(timeout); + timeout(); + REQUIRE(builds == 1); +} + +TEST_CASE("lazy nomination follows leaders and handles unavailable values", + "[scp][early-nomination]") +{ + SIMULATION_CREATE_NODE(0); + SIMULATION_CREATE_NODE(1); + SIMULATION_CREATE_NODE(2); + SCPQuorumSet qset; + qset.threshold = 3; + qset.validators = {v0NodeID, v1NodeID, v2NodeID}; + LeaderPreviewTestDriver driver(v1NodeID, qset); + auto& scp = driver.mSCP; + Value previous{42}, value{43}; + size_t builds = 0; + bool available = true; + bool stopWhileBuilding = false; + NominationValueSupplier makeValue = [&]() -> ValueWrapperPtr { + ++builds; + if (stopWhileBuilding) + { + scp.stopNomination(7); + } + return available ? std::make_shared(value) : nullptr; + }; + + SECTION("a follower votes for the leader without constructing a value") + { + SCPEnvelope envelope; + envelope.statement.nodeID = v0NodeID; + envelope.statement.slotIndex = 7; + envelope.statement.pledges.type(SCP_ST_NOMINATE); + auto& nom = envelope.statement.pledges.nominate(); + nom.quorumSetHash = sha256(xdr::xdr_to_opaque(qset)); + nom.votes.push_back(value); + REQUIRE(scp.receiveEnvelope(driver.wrapEnvelope(envelope)) == + SCP::VALID); + REQUIRE(scp.nominate(7, makeValue, previous)); + REQUIRE(builds == 0); + REQUIRE(driver.emitted == 1); + auto timeout = std::move(driver.nominationTimeout); + timeout(); + REQUIRE(scp.getNominationLeaders(7).count(v1NodeID)); + // Even a promoted leader needs no private value if it already follows + // a usable value from the first leader. + REQUIRE(builds == 0); + } + SECTION("an unavailable proposal is retried on the next timeout") + { + available = false; + REQUIRE(!scp.nominate(7, makeValue, previous)); + REQUIRE(builds == 0); + auto timeout = std::move(driver.nominationTimeout); + timeout(); + REQUIRE(builds == 1); + REQUIRE(driver.emitted == 0); + available = true; + timeout = std::move(driver.nominationTimeout); + REQUIRE(timeout); + timeout(); + REQUIRE(builds == 2); + REQUIRE(driver.emitted == 1); + } + SECTION("stopping nomination during construction cannot emit or rearm") + { + stopWhileBuilding = true; + scp.nominate(7, makeValue, previous); + REQUIRE(builds == 0); + auto timeout = std::move(driver.nominationTimeout); + timeout(); + REQUIRE(builds == 1); + REQUIRE(driver.emitted == 0); + REQUIRE(driver.timers == 1); + } + SECTION("a stopped follower never constructs a proposal") + { + scp.nominate(7, makeValue, previous); + scp.stopNomination(7); + auto timeout = std::move(driver.nominationTimeout); + timeout(); + REQUIRE(builds == 0); + REQUIRE(driver.emitted == 0); + REQUIRE(driver.timers == 1); + } } TEST_CASE("nomination leader preview preserves ties and zero weights", diff --git a/src/simulation/ApplyLoad.cpp b/src/simulation/ApplyLoad.cpp index b47e57265..0e98d9fe1 100644 --- a/src/simulation/ApplyLoad.cpp +++ b/src/simulation/ApplyLoad.cpp @@ -1036,8 +1036,13 @@ ApplyLoad::closeBenchmarkLedger(std::vector const& txs, // Nomination through externalization and apply use production SCP paths. herder.getPendingEnvelopes().putTxSet(receivedTxSet->getContentsHash(), ledgerSeq, receivedTxSet); - herder.getHerderSCPDriver().nominate(ledgerSeq, nominatedValue, - receivedTxSet, lcl.header.scpValue); + auto& driver = herder.getHerderSCPDriver(); + driver.nominate( + ledgerSeq, + [&driver, nominatedValue]() { + return driver.wrapStellarValue(nominatedValue); + }, + lcl.header.scpValue); auto const deadline = std::chrono::steady_clock::now() + std::chrono::seconds(60); From 2b05be10d78e447889899da9f9c58739cf74bcea Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:56:43 -0800 Subject: [PATCH 19/39] Build legacy tx sets on every validator under leader-only nomination Leader-only construction left followers without a local tx set, so they had to fetch the leader's. The Rust overlay only carries generalized tx sets, so before protocol 20 (legacy TransactionSet) a follower could not obtain the leader's set at all: it waited until nomination timeouts promoted it to leader and it built the set itself. A network created at the genesis protocol (perfnet) therefore stalled before reaching its upgrade: 22 of 30 nodes reached ledger 2, 8 stayed at ledger 1. For ledgers whose LCL is before protocol 20, every validator constructs its nomination value at the trigger again, as before leader-only construction; it is voted only if the node becomes a round leader. With an empty mempool (genesis and upgrade-only ledgers) the legacy set depends only on the previous ledger, so every validator holds the leader's set. Leader-only construction is unchanged from protocol 20 on. Add a Rust overlay simulation that starts at the genesis protocol, closes legacy-set ledgers, upgrades straight to the latest protocol and keeps closing ledgers. It fails without this change (a node stays at ledger 2 while the others close). --- docs/rust-overlay/txset-fetching.md | 6 + src/herder/HerderImpl.cpp | 26 ++++- src/herder/HerderImpl.h | 3 + src/herder/HerderSCPDriver.cpp | 16 ++- src/herder/HerderSCPDriver.h | 8 +- src/overlay/test/OverlayIPCTests.cpp | 159 +++++++++++++++++++++++++++ 6 files changed, 211 insertions(+), 7 deletions(-) diff --git a/docs/rust-overlay/txset-fetching.md b/docs/rust-overlay/txset-fetching.md index 826bf17cf..e6e8fb5d5 100644 --- a/docs/rust-overlay/txset-fetching.md +++ b/docs/rust-overlay/txset-fetching.md @@ -33,6 +33,12 @@ 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 diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 36c3e4c95..aa25a17fb 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1706,13 +1706,17 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) releaseAssert(closeTime > getConsensusTime(lcl.header.scpValue)); bool const activeLeader = getSCP().getNominationLeaders(ledgerSeq).count( getSCP().getLocalNodeID()); - releaseAssert(activeLeader || + bool const legacyTxSet = buildsLegacyTxSet(lcl.header.ledgerVersion); + releaseAssert(activeLeader || legacyTxSet || getSCP() .predictNominationLeaders( ledgerSeq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) .count(getSCP().getLocalNodeID())); - CLOG_INFO(Herder, "Building TX set as {} nomination leader for ledger {}", - activeLeader ? "active" : "first-round", ledgerSeq); + CLOG_INFO(Herder, "Building TX set as {} for ledger {}", + activeLeader ? "active nomination leader" + : legacyTxSet ? "validator (legacy tx set)" + : "first-round nomination leader", + ledgerSeq); // Apply-time offset: see the comment in triggerNextLedger auto const closeTimeOffset = closeTime.toApplyTime() - getApplyTime(lcl.header.scpValue); @@ -1982,7 +1986,21 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, [this, ledgerSeqToTrigger, checkTrackingSCP]() { return makeNominationValue(ledgerSeqToTrigger, checkTrackingSCP); }, - lcl.header.scpValue); + lcl.header.scpValue, + /*constructValueNow=*/buildsLegacyTxSet(lcl.header.ledgerVersion)); +} + +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 diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index 52b28ed82..ac14c4d2a 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -271,6 +271,9 @@ class HerderImpl : public Herder void discardPreparedTxSet(); ValueWrapperPtr makeNominationValue(uint32_t ledgerSeq, bool checkTrackingSCP); + // 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); // Compute the trigger-timer anchor point using the local node's // prepare-start timestamp for the previous slot. Returns a pessimistic diff --git a/src/herder/HerderSCPDriver.cpp b/src/herder/HerderSCPDriver.cpp index 550e5084a..6d38fe72e 100644 --- a/src/herder/HerderSCPDriver.cpp +++ b/src/herder/HerderSCPDriver.cpp @@ -1339,7 +1339,8 @@ HerderSCPDriver::logQuorumInformationAndUpdateMetrics(uint64_t index) void HerderSCPDriver::nominate(uint64_t slotIndex, NominationValueSupplier makeValue, - StellarValue const& previousValue) + StellarValue const& previousValue, + bool constructValueNow) { ZoneScoped; if (mLedgerSeqNominating != slotIndex) @@ -1347,6 +1348,19 @@ HerderSCPDriver::nominate(uint64_t slotIndex, NominationValueSupplier makeValue, mCurrentValue.reset(); } mLedgerSeqNominating = static_cast(slotIndex); + if (constructValueNow && !mCurrentValue) + { + auto value = makeValue(); + // Publishing the tx set may externalize this slot, which stops its + // nomination. Do not restart it. + if (mLedgerSeqNominating != slotIndex) + { + return; + } + // A null value is retried by the supplier below if this node becomes + // a leader. + mCurrentValue = std::move(value); + } auto prevValue = xdr::xdr_to_opaque(previousValue); mSCP.nominate( slotIndex, diff --git a/src/herder/HerderSCPDriver.h b/src/herder/HerderSCPDriver.h index 06354c82e..047412be3 100644 --- a/src/herder/HerderSCPDriver.h +++ b/src/herder/HerderSCPDriver.h @@ -129,10 +129,14 @@ class HerderSCPDriver : public SCPDriver // value for slotIndex. void noteEmptyTxSetValueReplaced(uint64_t slotIndex) override; - // Submit a value to consider for slotIndex + // Start nomination for slotIndex. SCP calls makeValue only when the local + // node is a round leader that needs its own value, unless + // constructValueNow is set: then the value is constructed before + // nomination starts (and reused if this node becomes a leader). // previousValue is the value from slotIndex-1 void nominate(uint64_t slotIndex, NominationValueSupplier makeValue, - StellarValue const& previousValue); + StellarValue const& previousValue, + bool constructValueNow = false); SCPQuorumSetPtr getQSet(Hash const& qSetHash) override; diff --git a/src/overlay/test/OverlayIPCTests.cpp b/src/overlay/test/OverlayIPCTests.cpp index cfa0b5b7a..196e6e2b5 100644 --- a/src/overlay/test/OverlayIPCTests.cpp +++ b/src/overlay/test/OverlayIPCTests.cpp @@ -3,6 +3,9 @@ // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 #include "herder/TxSetFrame.h" +#include "herder/Upgrades.h" +#include "ledger/LedgerHeaderUtils.h" +#include "ledger/LedgerManager.h" #include "lib/catch.hpp" #include "overlay/OverlayIPC.h" #include "rust/RustBridge.h" @@ -14,6 +17,8 @@ #include #include +#include +#include #include using namespace stellar; @@ -1600,3 +1605,157 @@ TEST_CASE("Rust overlay applies Soroban TX count upgrade", "[overlay-ipc]") LOG_INFO(DEFAULT_LOG, "✓ Soroban TX count upgrade works with Rust overlay"); } + +/** + * A network created at the genesis protocol must reach the latest protocol + * over the Rust overlay. Before generalized tx sets (protocol 20), proposals + * carry legacy `TransactionSet`s, which the Rust overlay cannot fetch or serve. + * Every validator therefore has to obtain the leader's legacy set locally: + * with an empty mempool, it builds the identical deterministic set itself. + * + * This reproduces the stall seen on perfnet with leader-only tx-set + * construction: followers that never built their own value could not obtain + * the leader's legacy set, so only nodes promoted to leader by nomination + * timeouts could externalize, one slow round at a time. + */ +TEST_CASE("Rust overlay upgrades from genesis through legacy tx sets", + "[overlay-ipc][upgrades]") +{ + requireOverlayBinary(); + + Hash networkID = sha256("Test network passphrase for genesis upgrades"); + auto simulation = std::make_shared(networkID); + + constexpr size_t NUM_NODES = 4; + std::vector keys; + SCPQuorumSet qSet; + qSet.threshold = 3; + for (size_t i = 0; i < NUM_NODES; ++i) + { + keys.push_back( + SecretKey::fromSeed(sha256(fmt::format("GENESIS_UPGRADE_{}", i)))); + qSet.validators.push_back(keys.back().getPublicKey()); + } + + auto cfgs = makeFullMeshConfigs(*simulation, NUM_NODES); + for (size_t i = 0; i < NUM_NODES; ++i) + { + auto& cfg = cfgs[i]; + // Start at the genesis protocol, like a fresh perfnet network. + cfg.USE_CONFIG_FOR_GENESIS = false; + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; + simulation->addNode(keys[i], qSet, &cfg); + } + simulation->startAllNodes(); + + auto nodes = simulation->getNodes(); + auto const expectedClose = simulation->getExpectedLedgerCloseTime(); + auto lclHeader = [](Application::pointer const& node) { + return node->getLedgerManager().getLastClosedLedgerHeader(); + }; + auto logLedgers = [&]() { + for (auto const& node : nodes) + { + auto const lcl = lclHeader(node); + UNSCOPED_INFO(fmt::format( + "node {}: ledger {} protocol {}", node->getConfig().PEER_PORT, + lcl.header.ledgerSeq, lcl.header.ledgerVersion)); + } + }; + // Simulation::crankUntil throws on timeout; report per-node state instead. + auto closesWithin = [&](std::function const& done, + std::chrono::milliseconds budget) { + try + { + simulation->crankUntil(done, budget, false); + return true; + } + catch (std::runtime_error const&) + { + logLedgers(); + return false; + } + }; + // Every node must have externalized a real, signed value (not an + // empty-tx-set replacement) whose tx set it holds, in the expected format. + auto requireRealTxSets = [&](std::optional generalized) { + for (auto const& node : nodes) + { + auto const lcl = lclHeader(node); + CAPTURE(node->getConfig().PEER_PORT, lcl.header.ledgerSeq); + REQUIRE(isSignedStellarValue(lcl.header.scpValue)); + auto txSet = + node->getHerder().getTxSet(lcl.header.scpValue.txSetHash); + REQUIRE(std::holds_alternative(txSet)); + auto const& frame = std::get(txSet); + REQUIRE(frame); + if (generalized) + { + REQUIRE(frame->isGeneralizedTxSet() == *generalized); + } + } + }; + + // Let the overlay connect and the network close its first ledger. + REQUIRE( + closesWithin([&]() { return simulation->haveAllExternalized(2, 1); }, + 30 * expectedClose)); + for (auto const& node : nodes) + { + REQUIRE(lclHeader(node).header.ledgerVersion == + LedgerManager::GENESIS_LEDGER_VERSION); + } + + // Legacy tx-set ledgers must close at the normal cadence. Without a + // locally available leader set, each one waits for nomination timeouts + // (1 s, 2 s, ...) to promote enough followers to leaders. + auto const firstLegacy = lclHeader(nodes[0]).header.ledgerSeq; + constexpr uint32_t LEGACY_LEDGERS = 4; + REQUIRE(closesWithin( + [&]() { + return simulation->haveAllExternalized(firstLegacy + LEGACY_LEDGERS, + 1); + }, + LEGACY_LEDGERS * 5 * expectedClose / 2)); + requireRealTxSets(false); + + // Upgrade straight to the latest protocol. The upgrade ledger itself still + // uses a legacy tx set; the next one is the first generalized set. + Upgrades::UpgradeParameters scheduledUpgrades; + scheduledUpgrades.mUpgradeTime = VirtualClock::from_time_t( + lclHeader(nodes[0]).header.scpValue.closeTime); + scheduledUpgrades.mProtocolVersion = + Config::CURRENT_LEDGER_PROTOCOL_VERSION; + for (auto const& node : nodes) + { + node->getHerder().setUpgrades(scheduledUpgrades); + } + REQUIRE(closesWithin( + [&]() { + return std::all_of( + nodes.begin(), nodes.end(), [&](auto const& node) { + return lclHeader(node).header.ledgerVersion == + Config::CURRENT_LEDGER_PROTOCOL_VERSION; + }); + }, + 10 * expectedClose)); + // A node may already have closed the first generalized-set ledger. + requireRealTxSets(std::nullopt); + + // Leader-only construction resumes at the latest protocol, where + // followers fetch the leader's generalized set through the overlay. + auto const firstLatest = lclHeader(nodes[0]).header.ledgerSeq; + REQUIRE(closesWithin( + [&]() { return simulation->haveAllExternalized(firstLatest + 3, 1); }, + 10 * expectedClose)); + requireRealTxSets(true); + + // Nodes that closed the same ledger agree on it. + std::map hashBySeq; + for (auto const& node : nodes) + { + auto const lcl = lclHeader(node); + auto [it, inserted] = hashBySeq.emplace(lcl.header.ledgerSeq, lcl.hash); + REQUIRE((inserted || it->second == lcl.hash)); + } +} From 8fcfe5d4d7182a9a9e905db8596baea1c481c71e Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 10:59:40 -0800 Subject: [PATCH 20/39] Complete pending tx-set fetches with locally built sets PendingEnvelopes::addTxSet only cached the set. Envelopes waiting to fetch that hash stayed in the fetching state until the overlay delivered the set again or the sender re-sent the envelope. Upstream's ItemFetcher released them on addTxSet; this branch's IPC fetch path lost that. That matters for sets the overlay cannot deliver: a leader's legacy (pre-protocol 20) set that a validator builds itself after the leader's nomination arrived, or a promoted leader that builds the set it was fetching. For generalized sets it saves the IPC round trip back from Rust. addTxSet now completes a pending fetch the same way recvTxSet does: it updates waiting value wrappers and reprocesses the waiting envelopes, which can reenter SCP (the nomination supplier already handles that). The waiting envelopes are detached before reprocessing, so reentrant fetch changes cannot invalidate the iteration. Completing a fetch now records the overlay.fetch.txset timer, which was never updated. A redundant overlay delivery of a known set is logged at debug level. --- src/herder/PendingEnvelopes.cpp | 55 ++++++++++++++++++----- src/herder/PendingEnvelopes.h | 16 ++++--- src/herder/test/PendingEnvelopesTests.cpp | 25 +++++++++++ 3 files changed, 79 insertions(+), 17 deletions(-) diff --git a/src/herder/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp index 204fa4986..4b86534f4 100644 --- a/src/herder/PendingEnvelopes.cpp +++ b/src/herder/PendingEnvelopes.cpp @@ -265,6 +265,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); } bool @@ -274,29 +279,57 @@ 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()) { - CLOG_WARNING(Herder, "TxSet {} not in pending fetches - rejecting", - hexAbbrev(hash)); + 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; } - addTxSet(hash, 0, txset); + putTxSet(hash, 0, txset); + completeTxSetFetch(hash, txset); + return true; +} + +void +PendingEnvelopes::completeTxSetFetch(Hash const& hash, + TxSetXDRFrameConstPtr const& txset) +{ + 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 startIt = mTxSetFetchStartTimes.find(hash); + if (startIt != mTxSetFetchStartTimes.end()) + { + mFetchTxSetTimer.Update(mApp.getClock().now() - startIt->second); + mTxSetFetchStartTimes.erase(startIt); + } // 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 diff --git a/src/herder/PendingEnvelopes.h b/src/herder/PendingEnvelopes.h index 7d9210de3..68bd31960 100644 --- a/src/herder/PendingEnvelopes.h +++ b/src/herder/PendingEnvelopes.h @@ -98,6 +98,10 @@ 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); + void updateMetrics(); void envelopeReady(SCPEnvelope const& envelope); @@ -163,10 +167,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 +185,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). */ diff --git a/src/herder/test/PendingEnvelopesTests.cpp b/src/herder/test/PendingEnvelopesTests.cpp index b50c9a39b..478ee74ad 100644 --- a/src/herder/test/PendingEnvelopesTests.cpp +++ b/src/herder/test/PendingEnvelopesTests.cpp @@ -201,6 +201,31 @@ TEST_CASE_VERSIONS("PendingEnvelopes recvSCPEnvelope", "[herder]") Herder::ENVELOPE_STATUS_PROCESSED); } + SECTION("process when the tx set being fetched is built locally") + { + REQUIRE(pendingEnvelopes.recvSCPQuorumSet(saneQSetHash, saneQSet)); + REQUIRE(herder.getSCP().getLatestMessage(pk) == nullptr); + auto& fetchTimer = + app->getMetrics().NewTimer({"overlay", "fetch", "txset"}); + auto const fetches = fetchTimer.count(); + + // A promoted leader or a legacy-tx-set validator can build the + // set it is fetching. No overlay delivery may ever follow, so + // adding it completes the fetch and processes the envelope. + pendingEnvelopes.addTxSet(p.second->getContentsHash(), + lcl.header.ledgerSeq + 1, p.second); + auto m = herder.getSCP().getLatestMessage(pk); + REQUIRE(m); + REQUIRE(*m == saneEnvelope); + REQUIRE(fetchTimer.count() == fetches + 1); + + // A later delivery from the overlay is redundant. + REQUIRE(!pendingEnvelopes.recvTxSet(p.second->getContentsHash(), + p.second)); + REQUIRE(pendingEnvelopes.recvSCPEnvelope(saneEnvelope) == + Herder::ENVELOPE_STATUS_PROCESSED); + } + SECTION("process when all data came (tx set first)") { REQUIRE(pendingEnvelopes.recvTxSet(p.second->getContentsHash(), From e5d8948daa7d88acefcfcdcf164f7e3b97b055da Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:02:41 -0800 Subject: [PATCH 21/39] Let promoted nomination leaders propose after losing sync With leader-only construction a follower builds its value only when a nomination timeout promotes it to leader, possibly long after the trigger. makeNominationValue rechecked herder tracking at that point, so a node that lost sync while the slot was stalled (35 s without externalizing) could never propose for that slot, even once promoted. On perfnet that turned the legacy-tx-set stall into a permanent one: nodes waiting for a set they could not fetch lost sync, and then could not build it either. Tracking is checked when the trigger starts nomination, as before; a value built at the trigger kept being nominated after losing sync. The supplier now only rechecks what makes building unsafe or stale (ledger manager synced and not applying, LCL still the slot's predecessor, close time valid). Add a herder test: a follower in a stalled unanimous quorum loses sync and must still propose a valid value once nomination timeouts promote it. It fails without this change (the follower never builds). --- src/herder/HerderImpl.cpp | 14 +++--- src/herder/HerderImpl.h | 3 +- src/herder/test/HerderTests.cpp | 78 +++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 8 deletions(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index aa25a17fb..3fd34453a 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1983,8 +1983,8 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, getHerderSCPDriver().recordSCPEvent(ledgerSeqToTrigger, true); mHerderSCPDriver.nominate( ledgerSeqToTrigger, - [this, ledgerSeqToTrigger, checkTrackingSCP]() { - return makeNominationValue(ledgerSeqToTrigger, checkTrackingSCP); + [this, ledgerSeqToTrigger]() { + return makeNominationValue(ledgerSeqToTrigger); }, lcl.header.scpValue, /*constructValueNow=*/buildsLegacyTxSet(lcl.header.ledgerVersion)); @@ -2004,13 +2004,15 @@ HerderImpl::buildsLegacyTxSet(uint32_t lclLedgerVersion) } ValueWrapperPtr -HerderImpl::makeNominationValue(uint32_t ledgerSeqToTrigger, - bool checkTrackingSCP) +HerderImpl::makeNominationValue(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. - if ((!isTracking() && checkTrackingSCP) || !mLedgerManager.isSynced() || - mLedgerManager.isApplying()) + // 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; } diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index ac14c4d2a..7e608a9cc 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -269,8 +269,7 @@ class HerderImpl : public Herder PreparedTxSet buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); void prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); void discardPreparedTxSet(); - ValueWrapperPtr makeNominationValue(uint32_t ledgerSeq, - bool checkTrackingSCP); + ValueWrapperPtr makeNominationValue(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); diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 01ab1ebb6..a5f19bf7d 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -7501,6 +7501,84 @@ TEST_CASE("only nomination leaders construct local proposals", REQUIRE(checkedRoles == std::set{0, 1, 2}); } +TEST_CASE("a follower promoted after losing sync still proposes", + "[herder][early-nomination]") +{ + // Unanimous three-node quorum with silent peers: the slot cannot + // externalize, so the local follower eventually loses sync. Nomination + // timeouts must still let it propose a value of its own once promoted. + std::vector keys; + SCPQuorumSet qset; + qset.threshold = 3; + for (int i = 0; i < 3; ++i) + { + keys.push_back(SecretKey::fromSeed( + sha256(fmt::format("early-nomination-node-{}", i)))); + qset.validators.push_back(keys.back().getPublicKey()); + } + + bool checkedFollower = false; + for (int i = 0; i < 3 && !checkedFollower; ++i) + { + VirtualClock clock; + clock.setCurrentVirtualTime(VirtualClock::from_time_t(1000)); + auto cfg = getTestConfig(i); + cfg.HTTP_PORT = 0; + cfg.MANUAL_CLOSE = false; + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = false; + cfg.FORCE_OLD_STYLE_PREPARE_START_TRIGGER_TIMER = true; + cfg.NODE_SEED = keys[i]; + cfg.QUORUM_SET = qset; + auto app = createTestApplication(clock, cfg); + auto& herder = static_cast(app->getHerder()); + auto& scp = herder.getSCP(); + auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); + auto const seq = lcl.header.ledgerSeq + 1; + if (scp.predictNominationLeaders( + seq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) + .count(scp.getLocalNodeID())) + { + // The first leader proposes at the trigger; test a follower. + continue; + } + checkedFollower = true; + herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + size_t pulls = 0; + herder.mGetTopTransactionsForTesting = [&](size_t) { + ++pulls; + return std::vector{}; + }; + + EarlyNominationTestAccess::schedule(herder); + clock.setCurrentVirtualTime(herder.getTriggerTimer().expiry_time()); + testutil::crankUntil( + app, [&]() { return !scp.getNominationLeaders(seq).empty(); }, + std::chrono::seconds(1)); + REQUIRE(!scp.getNominationLeaders(seq).empty()); + REQUIRE(scp.getLatestMessagesSend(seq).empty()); + REQUIRE(pulls == 0); + + // The stalled slot costs the node its sync before it is promoted. + herder.lostSync(); + REQUIRE(!herder.isTracking()); + + testutil::crankUntil( + app, [&]() { return !scp.getLatestMessagesSend(seq).empty(); }, + std::chrono::seconds(30)); + REQUIRE(scp.getNominationLeaders(seq).count(scp.getLocalNodeID())); + REQUIRE(pulls == 1); + auto messages = scp.getLatestMessagesSend(seq); + REQUIRE(!messages.empty()); + auto const& votes = messages.front().statement.pledges.nominate().votes; + REQUIRE(votes.size() == 1); + REQUIRE(herder.getHerderSCPDriver().validateValue(seq, votes.front(), + true) == + SCPDriver::kFullyValidatedValue); + herder.mGetTopTransactionsForTesting = nullptr; + } + REQUIRE(checkedFollower); +} + TEST_CASE("early underfilled proposals refresh at the trigger", "[herder][early-nomination]") { From 9b6ef061ee7ecd9c145a205482c4987b0b5cfc56 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:15:02 -0800 Subject: [PATCH 22/39] Retry stalled tx-set fetches and report sets that never arrive Core requested each missing tx set from the Rust overlay once, assuming the overlay handled timeouts and retries (stopAllOutsideRange said so), while the overlay explicitly has neither: a request to a peer that is silent or lacks the set (there is no negative response) stays reserved until that peer disconnects. A follower then waited forever, silently. Pending fetches and their envelopes were also never dropped when their slots were purged. Core: PendingEnvelopes keeps per-fetch state (start, last request, newest waiting slot, request count). A timer repeats requests that have not been answered after one second, every second, counts them in overlay.fetch.txset-retry and logs a warning on every doubling of the request count, naming the slot and how long it waited (and that legacy pre-protocol 20 sets cannot be fetched over the Rust overlay, when the LCL is legacy). Fetches whose waiting slots are purged are dropped. Rust: a RequestTxSet for a hash Core is still waiting for is a retry. It replaces the outstanding reservation and goes to another connected peer (the SCP source only if it was not the previous target, otherwise a random one), instead of being deduplicated against the stuck request. LedgerClosed prunes reservations below the retained-slot window, which also drops the prefetches of legacy sets that no peer can serve. Tests: a herder test for retry timing, completion and slot purging, and a dispatcher test for retry peer selection and pruning. --- docs/rust-overlay/txset-fetching.md | 18 ++- overlay/src/libp2p_overlay.rs | 158 ++++++++++++------- overlay/src/libp2p_overlay/dispatch_tests.rs | 71 ++++++++- overlay/src/main.rs | 22 ++- src/herder/PendingEnvelopes.cpp | 124 +++++++++++++-- src/herder/PendingEnvelopes.h | 25 ++- src/herder/test/PendingEnvelopesTests.cpp | 79 ++++++++++ 7 files changed, 414 insertions(+), 83 deletions(-) diff --git a/docs/rust-overlay/txset-fetching.md b/docs/rust-overlay/txset-fetching.md index e6e8fb5d5..a170754a9 100644 --- a/docs/rust-overlay/txset-fetching.md +++ b/docs/rust-overlay/txset-fetching.md @@ -86,8 +86,16 @@ 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 `overlay.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 @@ -109,6 +117,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/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 38040657a..0d0eb6bed 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -96,8 +96,15 @@ 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], @@ -209,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!( @@ -221,6 +239,22 @@ impl OverlayHandle { } } + /// 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 { @@ -574,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); @@ -945,67 +982,63 @@ impl StellarOverlay { 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 + /// prefers a different connected peer: the previous one 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 + let peer = if let Some(source_peer) = known_source.filter(|p| 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 } @@ -1018,6 +1051,7 @@ impl StellarOverlay { } } }; + drop(streams); // Keep selection and reservation in the dispatcher, before spawning // the write, so repeated FetchTxSet commands cannot race past dedup. @@ -1055,6 +1089,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; diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 02f75f6b2..3eddf0bb1 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -1252,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, @@ -1289,6 +1289,73 @@ 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))); + // With no other peer left, a retry asks the remaining one again. + overlay.state.peer_streams.write().await.remove(&peers[0]); + overlay.fetch_txset(hash, 21, true).await; + assert_eq!(overlay.sends.len(), 3); + 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) = diff --git a/overlay/src/main.rs b/overlay/src/main.rs index ac6fbc522..5b2dfa3f6 100644 --- a/overlay/src/main.rs +++ b/overlay/src/main.rs @@ -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; + } }); } } @@ -1177,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; + }); } } diff --git a/src/herder/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp index 4b86534f4..cebee5f40 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( @@ -45,6 +47,8 @@ PendingEnvelopes::PendingEnvelopes(Application& app, HerderImpl& herder) , mFetchDuration(app.getMetrics().NewTimer({"scp", "fetch", "envelope"})) , mFetchTxSetTimer(app.getMetrics().NewTimer({"overlay", "fetch", "txset"})) , mFetchQsetTimer(app.getMetrics().NewTimer({"overlay", "fetch", "qset"})) + , mTxSetFetchRetries( + app.getMetrics().NewCounter({"overlay", "fetch", "txset-retry"})) , mCostPerSlot(app.getMetrics().NewHistogram({"scp", "cost", "per-slot"})) { } @@ -313,11 +317,12 @@ PendingEnvelopes::completeTxSetFetch(Hash const& hash, // can start or stop other fetches. auto envelopes = std::move(it->second); mPendingTxSetFetches.erase(it); - auto startIt = mTxSetFetchStartTimes.find(hash); - if (startIt != mTxSetFetchStartTimes.end()) + auto fetchIt = mTxSetFetches.find(hash); + if (fetchIt != mTxSetFetches.end()) { - mFetchTxSetTimer.Update(mApp.getClock().now() - startIt->second); - mTxSetFetchStartTimes.erase(startIt); + mFetchTxSetTimer.Update(mApp.getClock().now() - + fetchIt->second.mStarted); + mTxSetFetches.erase(fetchIt); } // Update any ValueWrappers that were created before this tx set was @@ -739,22 +744,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(); } } @@ -783,7 +792,7 @@ PendingEnvelopes::stopFetch(SCPEnvelope const& envelope) if (vec.empty()) { mPendingTxSetFetches.erase(it); - mTxSetFetchStartTimes.erase(h2); + mTxSetFetches.erase(h2); } } } @@ -942,8 +951,91 @@ 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() +{ + if (mTxSetFetchRetryScheduled || mTxSetFetches.empty()) + { + 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(); + bool const legacyLcl = + protocolVersionIsBefore(mApp.getLedgerManager() + .getLastClosedLedgerHeader() + .header.ledgerVersion, + SOROBAN_PROTOCOL_VERSION); + for (auto& [hash, fetch] : mTxSetFetches) + { + if (now - fetch.mLastRequested < TX_SET_FETCH_RETRY_INTERVAL) + { + 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 @@ -988,13 +1080,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 diff --git a/src/herder/PendingEnvelopes.h b/src/herder/PendingEnvelopes.h index 68bd31960..4f9ad6e60 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 @@ -91,6 +103,7 @@ class PendingEnvelopes medida::Timer& mFetchDuration; medida::Timer& mFetchTxSetTimer; medida::Timer& mFetchQsetTimer; + medida::Counter& mTxSetFetchRetries; // Tracked cost per slot medida::Histogram& mCostPerSlot; @@ -101,6 +114,8 @@ class PendingEnvelopes // Hands a newly known tx set to the envelopes waiting to fetch it. void completeTxSetFetch(Hash const& hash, TxSetXDRFrameConstPtr const& txset); + void scheduleTxSetFetchRetry(); + void retryStalledTxSetFetches(); void updateMetrics(); @@ -135,6 +150,10 @@ class PendingEnvelopes std::optional maxSlot, uint64 slotToKeep); public: + // How long a tx set request may stay unanswered before it is repeated + static constexpr std::chrono::milliseconds TX_SET_FETCH_RETRY_INTERVAL{ + 1000}; + PendingEnvelopes(Application& app, HerderImpl& herder); ~PendingEnvelopes(); diff --git a/src/herder/test/PendingEnvelopesTests.cpp b/src/herder/test/PendingEnvelopesTests.cpp index 478ee74ad..28ef3d9d8 100644 --- a/src/herder/test/PendingEnvelopesTests.cpp +++ b/src/herder/test/PendingEnvelopesTests.cpp @@ -800,3 +800,82 @@ TEST_CASE("PendingEnvelopes recvSCPEnvelope without parallel tx set download", REQUIRE(herder.getSCP().getLatestMessage(pk) != nullptr); } } + +TEST_CASE("PendingEnvelopes re-requests tx sets the overlay does not deliver", + "[herder]") +{ + Config cfg(getTestConfig()); + VirtualClock clock; + auto s = SecretKey::pseudoRandomForTesting(); + auto& pk = s.getPublicKey(); + cfg.QUORUM_SET.validators.emplace_back(pk); + Application::pointer app = createTestApplication(clock, cfg); + auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); + auto& herder = static_cast(app->getHerder()); + auto& pendingEnvelopes = herder.getPendingEnvelopes(); + + auto root = app->getRoot(); + std::vector accs; + for (size_t i = 0; i < 2; i++) + { + accs.push_back(TestAccount{*app, getAccount("A" + std::to_string(i))}); + } + auto qSet = SCPQuorumSet{}; + qSet.threshold = 1; + qSet.validators.push_back(pk); + auto qSetHash = sha256(xdr::xdr_to_opaque(qSet)); + auto p = makeTxPair(herder, s, makeTransactions(*app, accs, *root, 2), 10); + auto const hash = p.second->getContentsHash(); + auto const slot = lcl.header.ledgerSeq + 1; + auto envelope = makeEnvelope(herder, s, p, qSetHash, slot); + pendingEnvelopes.addSCPQuorumSet(qSetHash, qSet); + + auto& retries = + app->getMetrics().NewCounter({"overlay", "fetch", "txset-retry"}); + auto const initialRetries = retries.count(); + auto const interval = PendingEnvelopes::TX_SET_FETCH_RETRY_INTERVAL; + // Advance virtual time by exactly `d` (crankFor can jump past it to the + // next timer). + auto crankBy = [&](VirtualClock::duration d) { + bool reached = false; + VirtualTimer sentinel(clock); + sentinel.expires_from_now(d); + sentinel.async_wait([&]() { reached = true; }, + &VirtualTimer::onFailureNoop); + while (!reached && clock.crank(false) > 0) + { + } + REQUIRE(reached); + }; + + REQUIRE(pendingEnvelopes.recvSCPEnvelope(envelope) == + Herder::ENVELOPE_STATUS_FETCHING); + REQUIRE(pendingEnvelopes.getTxSetWaitingTime(hash)); + + // Nothing is repeated before the retry interval. + crankBy(interval / 2); + REQUIRE(retries.count() == initialRetries); + + SECTION("an undelivered set is requested again until it arrives") + { + crankBy(2 * interval); + auto const retried = retries.count() - initialRetries; + REQUIRE(retried >= 1); + REQUIRE(retried <= 2); + + REQUIRE(pendingEnvelopes.recvTxSet(hash, p.second)); + REQUIRE(!pendingEnvelopes.getTxSetWaitingTime(hash)); + crankBy(3 * interval); + REQUIRE(retries.count() == initialRetries + retried); + } + + SECTION("purging the waiting slot stops the fetch") + { + pendingEnvelopes.eraseOutsideRange(slot + 1, std::nullopt, 0); + REQUIRE(!pendingEnvelopes.getTxSetWaitingTime(hash)); + crankBy(3 * interval); + REQUIRE(retries.count() == initialRetries); + // The envelope is gone with its slot, so a late delivery is unused. + REQUIRE(!pendingEnvelopes.recvTxSet(hash, p.second)); + } +} From 3630d31fd741869931e67a2d4e209188bae99f13 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:37:08 -0800 Subject: [PATCH 23/39] Avoid copying tx-set XDR on the overlay IPC paths The leader copied its set's whole GeneralizedTransactionSet (toXDR) to serialize it into a temporary vector, which CacheTxSet then copied into the IPC message. Followers copied the received payload into a vector to decode it, and makeFromWire copied the decoded set again into the frame. Several frame constructors also copied freshly built local XDR. - CacheTxSet serializes the frame's XDR directly into the IPC payload (TxSetXDRFrame::getGeneralizedXDR gives const access to it). - TX_SET_AVAILABLE decodes in place from the payload and moves the set into the frame (new rvalue makeFromWire overloads), also used by toWireTxSetFrame, makeEmpty and makeFromHistoryTransactions. - PendingEnvelopes::recvTxSet rejects a set whose recomputed content hash differs from the hash it was delivered for; Core keyed sets by the overlay's hash without checking. Add a hidden benchmark ("received tx set processing costs") for the dissemination path. For 6,000 classic payments (1.2 MB) on the laptop: leader encode 0.71 -> 0.35 ms, follower frame 1.16 -> 1.04 ms (n=5 medians). The savings are small for classic sets and grow with Soroban transactions' nested XDR. The benchmark also shows received-set validation at 67 ms with a cold signature cache vs 10.5 ms warm (4 tasks). --- src/herder/HerderImpl.cpp | 14 +-- src/herder/PendingEnvelopes.cpp | 11 ++ src/herder/TxSetFrame.cpp | 51 +++++++- src/herder/TxSetFrame.h | 9 ++ src/herder/test/PendingEnvelopesTests.cpp | 16 +++ src/herder/test/TxSetTests.cpp | 135 ++++++++++++++++++++++ src/overlay/OverlayIPC.cpp | 29 +++-- src/overlay/OverlayIPC.h | 7 +- src/overlay/RustOverlayManager.cpp | 22 ++-- src/overlay/RustOverlayManager.h | 4 +- 10 files changed, 258 insertions(+), 40 deletions(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 3fd34453a..4282315f6 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1823,10 +1823,10 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) // validation. Its eager compression runs concurrently in Rust. if (txSet->isGeneralizedTxSet()) { - GeneralizedTransactionSet xdrTxSet; - txSet->toXDR(xdrTxSet); + // Serialized straight into the IPC message, without copying + // the set's XDR first. overlayMgr.cacheTxSet(txSet->getContentsHash(), - xdr::xdr_to_opaque(xdrTxSet), ledgerSeq); + txSet->getGeneralizedXDR(), ledgerSeq); } }); CLOG_INFO(Herder, "Proposed TX set has {} transactions", @@ -2091,11 +2091,9 @@ HerderImpl::makeNominationValue(uint32_t ledgerSeqToTrigger) // The overlay only cached the set built above; peers fetch this one. if (proposedSet->isGeneralizedTxSet()) { - GeneralizedTransactionSet xdrTxSet; - proposedSet->toXDR(xdrTxSet); - mApp.getOverlayManager().cacheTxSet(proposedSet->getContentsHash(), - xdr::xdr_to_opaque(xdrTxSet), - lcl.header.ledgerSeq + 1); + mApp.getOverlayManager().cacheTxSet( + proposedSet->getContentsHash(), + proposedSet->getGeneralizedXDR(), lcl.header.ledgerSeq + 1); } } #endif diff --git a/src/herder/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp index cebee5f40..e5015cada 100644 --- a/src/herder/PendingEnvelopes.cpp +++ b/src/herder/PendingEnvelopes.cpp @@ -299,6 +299,17 @@ PendingEnvelopes::recvTxSet(Hash const& hash, TxSetXDRFrameConstPtr txset) 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 {} delivered with content hash {} - " + "rejecting", + hexAbbrev(hash), hexAbbrev(txset->getContentsHash())); + return false; + } + putTxSet(hash, 0, txset); completeTxSetFetch(hash, txset); return true; diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index bad5528cc..43e6362d5 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) { @@ -1024,11 +1058,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 @@ -1043,7 +1077,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 @@ -1458,6 +1492,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 { @@ -2506,13 +2547,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 b2ac0e9be..78ce87a3e 100644 --- a/src/herder/TxSetFrame.h +++ b/src/herder/TxSetFrame.h @@ -199,6 +199,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); @@ -215,6 +220,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; @@ -282,6 +289,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{}; diff --git a/src/herder/test/PendingEnvelopesTests.cpp b/src/herder/test/PendingEnvelopesTests.cpp index 28ef3d9d8..6b7395830 100644 --- a/src/herder/test/PendingEnvelopesTests.cpp +++ b/src/herder/test/PendingEnvelopesTests.cpp @@ -201,6 +201,22 @@ TEST_CASE_VERSIONS("PendingEnvelopes recvSCPEnvelope", "[herder]") Herder::ENVELOPE_STATUS_PROCESSED); } + SECTION("reject a delivered set whose content hash differs") + { + REQUIRE(pendingEnvelopes.recvSCPQuorumSet(saneQSetHash, saneQSet)); + auto other = TxSetXDRFrame::makeEmpty( + sha256("another previous ledger"), lcl.header.ledgerVersion); + REQUIRE(other->getContentsHash() != p.second->getContentsHash()); + REQUIRE(!pendingEnvelopes.recvTxSet(p.second->getContentsHash(), + other)); + REQUIRE(herder.getSCP().getLatestMessage(pk) == nullptr); + REQUIRE(pendingEnvelopes.getTxSetWaitingTime( + p.second->getContentsHash())); + REQUIRE(pendingEnvelopes.recvTxSet(p.second->getContentsHash(), + p.second)); + REQUIRE(herder.getSCP().getLatestMessage(pk)); + } + SECTION("process when the tx set being fetched is built locally") { REQUIRE(pendingEnvelopes.recvSCPQuorumSet(saneQSetHash, saneQSet)); diff --git a/src/herder/test/TxSetTests.cpp b/src/herder/test/TxSetTests.cpp index 3e497f48f..f23037071 100644 --- a/src/herder/test/TxSetTests.cpp +++ b/src/herder/test/TxSetTests.cpp @@ -3,6 +3,7 @@ // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 #include "crypto/SHA.h" +#include "crypto/SecretKey.h" #include "herder/ParallelTxSetBuilder.h" #include "herder/TxSetFrame.h" #include "herder/TxSetUtils.h" @@ -3693,5 +3694,139 @@ TEST_CASE("parallel tx set validation matches sequential", "[txset]") } } +// Costs on the tx-set dissemination path for a large set: the leader's +// encoding for the overlay, the follower's frame construction from the +// overlay's bytes, and the follower's validation with a cold vs warm +// signature-verification cache. Classic payments (one signature each). +TEST_CASE("received tx set processing costs", "[txset][.][benchmark]") +{ + size_t const txCount = 6000; + int const reps = 5; + VirtualClock clock; + auto cfg = getTestConfig(); + cfg.GENESIS_TEST_ACCOUNT_COUNT = txCount + 1; + cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = 2 * txCount; + Application::pointer app = createTestApplication(clock, cfg); + auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); + auto dest = getGenesisAccount(*app, static_cast(txCount)); + PerPhaseTransactionList phases(2); + for (size_t i = 0; i < txCount; ++i) + { + auto source = getGenesisAccount(*app, static_cast(i)); + phases[0].push_back(transactionFromOperations( + *app, source.getSecretKey(), source.getLastSequenceNumber() + 1, + {payment(dest.getPublicKey(), 1)}, 100)); + } + auto [txSet, applicable] = + makeTxSetFromTransactions(phases, *app, ApplyTimeOffset{}); + REQUIRE(txSet->isGeneralizedTxSet()); + REQUIRE(txSet->sizeTxTotal() == txCount); + auto const bytes = xdr::xdr_to_opaque(txSet->getGeneralizedXDR()); + + using Clock = std::chrono::steady_clock; + auto medianMs = [](std::vector v) { + std::sort(v.begin(), v.end()); + return v[v.size() / 2]; + }; + auto timeMs = [](auto&& f) { + auto start = Clock::now(); + f(); + return std::chrono::duration(Clock::now() - start) + .count(); + }; + auto report = [&](std::string const& what, std::vector const& v) { + auto sorted = v; + std::sort(sorted.begin(), sorted.end()); + std::cout << what << ": median " << medianMs(v) << " ms (min " + << sorted.front() << ", max " << sorted.back() + << ", n=" << v.size() << ")" << std::endl; + }; + + std::cout << "=== " << txCount << " txs, " << bytes.size() + << " XDR bytes ===" << std::endl; + // Leader: hand the set to the overlay. + std::vector encodeCopy, encodeDirect; + for (int i = 0; i < reps; ++i) + { + encodeCopy.push_back(timeMs([&]() { + GeneralizedTransactionSet copy; + txSet->toXDR(copy); + auto out = xdr::xdr_to_opaque(copy); + REQUIRE(out.size() == bytes.size()); + })); + encodeDirect.push_back(timeMs([&]() { + auto const& xdrSet = txSet->getGeneralizedXDR(); + std::vector out(36 + xdr::xdr_argpack_size(xdrSet)); + xdr::xdr_put put(out.data() + 36, out.data() + out.size()); + xdr::xdr_argpack_archive(put, xdrSet); + REQUIRE(out.size() == 36 + bytes.size()); + })); + } + report("leader encode: toXDR copy + xdr_to_opaque", encodeCopy); + report("leader encode: in place from frame", encodeDirect); + + // Follower: decode the overlay's bytes into a frame. + std::vector decode, frameCopy, frameMove; + for (int i = 0; i < reps; ++i) + { + GeneralizedTransactionSet parsed; + decode.push_back( + timeMs([&]() { xdr::xdr_from_opaque(bytes, parsed); })); + frameCopy.push_back(timeMs([&]() { + auto frame = TxSetXDRFrame::makeFromWire(parsed); + REQUIRE(frame->getContentsHash() == txSet->getContentsHash()); + })); + frameMove.push_back(timeMs([&]() { + auto frame = TxSetXDRFrame::makeFromWire(std::move(parsed)); + REQUIRE(frame->getContentsHash() == txSet->getContentsHash()); + })); + } + report("follower decode bytes", decode); + report("follower frame: copy (makeFromWire const&)", frameCopy); + report("follower frame: move (makeFromWire &&)", frameMove); + + // Follower: validate the received set (what herder.txset.validate times). + for (size_t tasks : + {app->getBatchExecutor().preferredTaskCount(), size_t(4), size_t(1)}) + { + app->getBatchExecutor().setPreferredTaskCountForTesting(tasks); + std::vector cold, warm; + for (int i = 0; i < reps; ++i) + { + // A fresh frame per run, as for each received set; built outside + // the timed region. + auto validate = [&](TxSetXDRFrameConstPtr const& received) { + return timeMs([&]() { + auto prepared = received->prepareForApply(*app, lcl.header); + REQUIRE(prepared); + REQUIRE(prepared->checkValid(*app, 0, 0)); + }); + }; + auto first = TxSetXDRFrame::makeFromWire( + GeneralizedTransactionSet(txSet->getGeneralizedXDR())); + auto second = TxSetXDRFrame::makeFromWire( + GeneralizedTransactionSet(txSet->getGeneralizedXDR())); + PubKeyUtils::clearVerifySigCache(); + uint64_t hits = 0, misses = 0; + PubKeyUtils::flushVerifySigCacheCounts(hits, misses); + cold.push_back(validate(first)); + PubKeyUtils::flushVerifySigCacheCounts(hits, misses); + if (i == 0) + { + std::cout << "cold run: " << misses + << " signature verifications, " << hits + << " cache hits" << std::endl; + } + warm.push_back(validate(second)); + } + report("validate, " + std::to_string(tasks) + + " tasks, cold signature cache", + cold); + report("validate, " + std::to_string(tasks) + + " tasks, warm signature cache", + warm); + } +} + } // namespace } // namespace stellar diff --git a/src/overlay/OverlayIPC.cpp b/src/overlay/OverlayIPC.cpp index a26be0d39..39aa870f1 100644 --- a/src/overlay/OverlayIPC.cpp +++ b/src/overlay/OverlayIPC.cpp @@ -4,6 +4,7 @@ #include "overlay/OverlayIPC.h" #include "crypto/Hex.h" +#include "util/GlobalChecks.h" #include "util/Logging.h" #include "xdr/Stellar-ledger.h" #include @@ -414,13 +415,16 @@ OverlayIPC::handleMessage(IPCMessage const& msg) { try { + // Decode in place after the hash (offset 32 keeps the 4-byte + // alignment xdrpp requires) and hand over the decoded set. GeneralizedTransactionSet txSet; - std::vector xdrData(msg.payload.begin() + 32, - msg.payload.end()); - xdr::xdr_from_opaque(xdrData, txSet); + xdr::xdr_get get(msg.payload.data() + 32, + msg.payload.data() + msg.payload.size()); + xdr::xdr_argpack_archive(get, txSet); + get.done(); CLOG_INFO(Overlay, "Received TX set {} ({} bytes) from overlay", - hexAbbrev(hash), xdrData.size()); - mOnTxSetReceived(hash, txSet); + hexAbbrev(hash), msg.payload.size() - 32); + mOnTxSetReceived(hash, std::move(txSet)); } catch (std::exception const& e) { @@ -700,7 +704,7 @@ OverlayIPC::requestTxSet(Hash const& hash, uint32_t slotIndex) } void -OverlayIPC::cacheTxSet(Hash const& hash, std::vector const& xdr, +OverlayIPC::cacheTxSet(Hash const& hash, GeneralizedTransactionSet const& txSet, uint32_t slotIndex) { if (!mChannel || !mChannel->isConnected()) @@ -712,14 +716,19 @@ OverlayIPC::cacheTxSet(Hash const& hash, std::vector const& xdr, msg.type = IPCMessageType::CACHE_TX_SET; // Payload: [hash:32][slotSeq:4][txSetXDR...]. The slot the set is for; // used by the Rust overlay to stamp the cache entry for exact age-based - // eviction. - msg.payload.resize(36 + xdr.size()); + // eviction. The set is serialized in place (offset 36 keeps the 4-byte + // alignment xdrpp requires). + auto const xdrSize = xdr::xdr_argpack_size(txSet); + msg.payload.resize(36 + xdrSize); std::memcpy(msg.payload.data(), hash.data(), 32); std::memcpy(msg.payload.data() + 32, &slotIndex, 4); - std::memcpy(msg.payload.data() + 36, xdr.data(), xdr.size()); + xdr::xdr_put put(msg.payload.data() + 36, + msg.payload.data() + msg.payload.size()); + xdr::xdr_argpack_archive(put, txSet); + releaseAssert(put.p_ == put.e_); CLOG_DEBUG(Overlay, "Caching TX set {} for slot {} ({} bytes)", - hexAbbrev(hash), slotIndex, xdr.size()); + hexAbbrev(hash), slotIndex, xdrSize); std::lock_guard lock(mSendMutex); mChannel->send(msg); } diff --git a/src/overlay/OverlayIPC.h b/src/overlay/OverlayIPC.h index 2bd2e9a2c..1963b6541 100644 --- a/src/overlay/OverlayIPC.h +++ b/src/overlay/OverlayIPC.h @@ -46,8 +46,9 @@ class OverlayIPC std::function(uint32_t ledgerSeq)>; /// Callback when TX set received from peers (async fetch response) + /// (the callee may take ownership of the decoded set) using TxSetReceivedCallback = std::function; + Hash const& hash, GeneralizedTransactionSet&& txSet)>; /** * Create an OverlayIPC instance. @@ -171,10 +172,10 @@ class OverlayIPC * who request it via TX set fetching. * * @param hash The TX set hash - * @param xdr The serialized TX set XDR + * @param txSet The TX set, serialized directly into the IPC message * @param slotIndex The slot the set is for (stamps the cache entry) */ - void cacheTxSet(Hash const& hash, std::vector const& xdr, + void cacheTxSet(Hash const& hash, GeneralizedTransactionSet const& txSet, uint32_t slotIndex); /// Set callback for received SCP envelopes diff --git a/src/overlay/RustOverlayManager.cpp b/src/overlay/RustOverlayManager.cpp index 4e531a89e..95cc41818 100644 --- a/src/overlay/RustOverlayManager.cpp +++ b/src/overlay/RustOverlayManager.cpp @@ -63,16 +63,14 @@ RustOverlayManager::start() return mApp.getHerder().getSCPStateForPeer(ledgerSeq); }); - mOverlayIPC->setOnTxSetReceived( - [this](Hash const& hash, GeneralizedTransactionSet const& txSet) { - // Called from IPC reader thread - post to main thread - auto frame = TxSetXDRFrame::makeFromWire(txSet); - mApp.postOnMainThread( - [this, hash, frame]() { - mApp.getHerder().recvTxSet(hash, frame); - }, - "RustOverlayManager: TxSetReceived"); - }); + mOverlayIPC->setOnTxSetReceived([this](Hash const& hash, + GeneralizedTransactionSet&& txSet) { + // Called from IPC reader thread - post to main thread + auto frame = TxSetXDRFrame::makeFromWire(std::move(txSet)); + mApp.postOnMainThread( + [this, hash, frame]() { mApp.getHerder().recvTxSet(hash, frame); }, + "RustOverlayManager: TxSetReceived"); + }); if (!mOverlayIPC->start()) { @@ -221,12 +219,12 @@ RustOverlayManager::requestTxSet(Hash const& txSetHash, uint32_t slotIndex) void RustOverlayManager::cacheTxSet(Hash const& txSetHash, - std::vector const& xdr, + GeneralizedTransactionSet const& txSet, uint32_t slotIndex) { if (mOverlayIPC && !mShuttingDown) { - mOverlayIPC->cacheTxSet(txSetHash, xdr, slotIndex); + mOverlayIPC->cacheTxSet(txSetHash, txSet, slotIndex); } } diff --git a/src/overlay/RustOverlayManager.h b/src/overlay/RustOverlayManager.h index d4709862e..fc6d95ede 100644 --- a/src/overlay/RustOverlayManager.h +++ b/src/overlay/RustOverlayManager.h @@ -71,8 +71,8 @@ class RustOverlayManager // Cache a locally-built TX set in Rust overlay. slotIndex is the slot the // set is for, used to stamp the Rust-side cache entry. - void cacheTxSet(Hash const& txSetHash, std::vector const& xdr, - uint32_t slotIndex); + void cacheTxSet(Hash const& txSetHash, + GeneralizedTransactionSet const& txSet, uint32_t slotIndex); // Get top transactions from Rust overlay's mempool for TX set building. // Blocks until the overlay responds, shuts down, or disconnects. From 260c4a7df06b38198a68c119c6eca31da0a8ba40 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 11:43:12 -0800 Subject: [PATCH 24/39] Pre-verify mempool signatures on followers at the trigger Before leader-only construction every validator built its own set at the trigger, validating its mempool and so populating the signature verification cache. When the leader's set arrived, its signatures were mostly cache hits. Followers now validate the leader's set cold: the PR's perfnet run saw the received-set validation timer rise from about 158 ms to 205 ms. The "received tx set processing costs" benchmark reproduces that: validating 6,000 transactions takes 67 ms with a cold signature cache and 10.5 ms warm (4 tasks, the physical cores of a perfnet node). At the trigger, a validator that is not a round-1 leader (and not on a legacy tx-set protocol, where everyone builds) pulls its mempool's top transactions once (up to a full set) and verifies their source, operation source and fee source master-key signatures on background tasks, as many as the batch executor would use. The verification goes through SignatureUtils::verify, so it fills exactly the cache entries tx-set validation looks up. Signatures by other signers need ledger state and are left to validation. The prewarm stops as soon as validation of a received set (a validity-cache miss) or a local build begins, which then verify the rest themselves on all cores. The follower's main thread does the pull while it waits for the leader's nomination; it still builds and compresses nothing. In-process simulations share the global signature cache across nodes, so they cannot show the effect; it needs a perfnet comparison. --- docs/rust-overlay/txset-fetching.md | 8 +- src/herder/HerderImpl.cpp | 172 +++++++++++++++++++++++++--- src/herder/HerderImpl.h | 13 +++ src/herder/HerderSCPDriver.cpp | 3 + src/herder/test/HerderTests.cpp | 139 +++++++++++++++++++++- 5 files changed, 313 insertions(+), 22 deletions(-) diff --git a/docs/rust-overlay/txset-fetching.md b/docs/rust-overlay/txset-fetching.md index a170754a9..a4008ca17 100644 --- a/docs/rust-overlay/txset-fetching.md +++ b/docs/rust-overlay/txset-fetching.md @@ -25,8 +25,12 @@ 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 pulling the -mempool, building a private set, or compressing it. A node promoted by 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 diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 4282315f6..03409ab83 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -35,8 +35,10 @@ #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" @@ -62,6 +64,71 @@ 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. +void +verifyMasterKeySignatures(Hash const& networkID, TransactionEnvelope const& env) +{ + auto verify = [](Hash const& contentsHash, auto const& signatures, + std::vector const& keys) { + for (auto const& sig : signatures) + { + for (auto const& key : keys) + { + if (SignatureUtils::verify(sig, key, contentsHash).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(); + verifyMasterKeySignatures(networkID, inner); + break; + } + default: + break; + } +} +} // namespace + // Roughly ~10 minutes of consensus constexpr uint32 const CLOSE_TIME_DRIFT_LEDGER_WINDOW_SIZE = 120; // 10 seconds of drift threshold @@ -1729,20 +1796,9 @@ 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 txEnvelopes = pullTopTransactions(maxTxSetCandidates() * 2); CLOG_INFO(Herder, "Got {} transactions from Rust overlay mempool", txEnvelopes.size()); @@ -1988,6 +2044,94 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, }, lcl.header.scpValue, /*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. Verify their signatures + // meanwhile, as building our own set used to before leader-only + // construction, so that validation finds them in the cache. + if (!buildsLegacyTxSet(lcl.header.ledgerVersion) && + !getSCP() + .getNominationLeaders(ledgerSeqToTrigger) + .count(getSCP().getLocalNodeID())) + { + prewarmSignatureCache(); + } +} + +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); +} + +void +HerderImpl::prewarmSignatureCache() +{ + ZoneScoped; + stopSignaturePrewarm(); + // The leader's set has at most this many transactions, drawn from the + // top of a mempool much like ours. + auto envelopes = std::make_shared const>( + pullTopTransactions(maxTxSetCandidates())); + if (envelopes->empty()) + { + return; + } + auto stop = std::make_shared>(false); + mSignaturePrewarmStop = stop; + // The batch executor (validation) is idle until the leader's set + // arrives; use as many background tasks as it would. + size_t const tasks = std::max( + 1, std::min(mApp.getBatchExecutor().preferredTaskCount(), + mApp.getConfig().WORKER_THREADS)); + CLOG_DEBUG(Herder, + "Pre-verifying signatures of {} transactions in {} tasks", + envelopes->size(), tasks); + for (size_t task = 0; task < tasks; ++task) + { + mApp.postOnBackgroundThread( + [envelopes, stop, networkID = mApp.getNetworkID(), task, tasks]() { + ZoneScopedN("signature prewarm"); + for (size_t i = task; i < envelopes->size(); i += tasks) + { + if (stop->load(std::memory_order_relaxed)) + { + return; + } + verifyMasterKeySignatures(networkID, (*envelopes)[i]); + } + }, + "Herder: signature prewarm"); + } +} + +void +HerderImpl::stopSignaturePrewarm() +{ + if (mSignaturePrewarmStop) + { + mSignaturePrewarmStop->store(true, std::memory_order_relaxed); + mSignaturePrewarmStop.reset(); + } } bool diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index 7e608a9cc..57f1eb0d4 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -273,7 +273,20 @@ class HerderImpl : public Herder // 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; + std::vector pullTopTransactions(size_t count); + // Verify the signatures of the mempool's top transactions on background + // threads, warming the signature cache for validating the leader's set. + void prewarmSignatureCache(); + std::shared_ptr> mSignaturePrewarmStop; + public: + // Stops a running signature prewarm: 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 6d38fe72e..6fcc27624 100644 --- a/src/herder/HerderSCPDriver.cpp +++ b/src/herder/HerderSCPDriver.cpp @@ -1959,6 +1959,9 @@ 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(); auto validationTime = mSCPMetrics.mTxSetValidation.TimeScope(); // The invariant here is that we only validate tx sets nominated diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index a5f19bf7d..756a0ff96 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -7461,9 +7461,10 @@ TEST_CASE("only nomination leaders construct local proposals", app, [&]() { return !scp.getNominationLeaders(seq).empty(); }, std::chrono::seconds(1)); REQUIRE(scp.getNominationLeaders(seq) == first); - // The first leader refreshes its empty early snapshot. Followers do - // not even pull the mempool when starting nomination. - REQUIRE(pulls == (role == 0 ? 2 : 0)); + // The first leader refreshes its empty early snapshot. Followers + // pull the mempool once, only to pre-verify signatures; they do not + // build or publish a set. + REQUIRE(pulls == (role == 0 ? 2 : 1)); REQUIRE(scp.getLatestMessagesSend(seq).empty() == (role != 0)); // With the first leader silent, real nomination timers eventually @@ -7473,7 +7474,7 @@ TEST_CASE("only nomination leaders construct local proposals", app, [&]() { return !scp.getLatestMessagesSend(seq).empty(); }, std::chrono::seconds(30)); REQUIRE(scp.getNominationLeaders(seq).count(scp.getLocalNodeID())); - REQUIRE(pulls == (role == 0 ? 2 : 1)); + REQUIRE(pulls == 2); auto messages = scp.getLatestMessagesSend(seq); auto const& votes = messages.front().statement.pledges.nominate().votes; REQUIRE(votes.size() == 1); @@ -7556,7 +7557,8 @@ TEST_CASE("a follower promoted after losing sync still proposes", std::chrono::seconds(1)); REQUIRE(!scp.getNominationLeaders(seq).empty()); REQUIRE(scp.getLatestMessagesSend(seq).empty()); - REQUIRE(pulls == 0); + // Only the signature prewarm pull; no build yet. + REQUIRE(pulls == 1); // The stalled slot costs the node its sync before it is promoted. herder.lostSync(); @@ -7566,7 +7568,7 @@ TEST_CASE("a follower promoted after losing sync still proposes", app, [&]() { return !scp.getLatestMessagesSend(seq).empty(); }, std::chrono::seconds(30)); REQUIRE(scp.getNominationLeaders(seq).count(scp.getLocalNodeID())); - REQUIRE(pulls == 1); + REQUIRE(pulls == 2); auto messages = scp.getLatestMessagesSend(seq); REQUIRE(!messages.empty()); auto const& votes = messages.front().statement.pledges.nominate().votes; @@ -7579,6 +7581,131 @@ TEST_CASE("a follower promoted after losing sync still proposes", REQUIRE(checkedFollower); } +TEST_CASE("followers pre-verify mempool signatures at the trigger", + "[herder][early-nomination]") +{ + // Followers no longer validate their own mempool by building a set, so + // the leader's set would be validated with a cold signature cache. At the + // trigger they pre-verify their top transactions' signatures instead. + std::vector keys; + SCPQuorumSet qset; + qset.threshold = 3; + for (int i = 0; i < 3; ++i) + { + keys.push_back(SecretKey::fromSeed( + sha256(fmt::format("early-nomination-node-{}", i)))); + qset.validators.push_back(keys.back().getPublicKey()); + } + + std::set checkedRoles; + for (int i = 0; i < 3 && checkedRoles.size() < 2; ++i) + { + VirtualClock clock; + clock.setCurrentVirtualTime(VirtualClock::from_time_t(1000)); + auto cfg = getTestConfig(i); + cfg.HTTP_PORT = 0; + cfg.MANUAL_CLOSE = false; + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = false; + cfg.FORCE_OLD_STYLE_PREPARE_START_TRIGGER_TIMER = true; + cfg.NODE_SEED = keys[i]; + cfg.QUORUM_SET = qset; + cfg.GENESIS_TEST_ACCOUNT_COUNT = 41; + auto app = createTestApplication(clock, cfg); + auto& herder = static_cast(app->getHerder()); + auto& scp = herder.getSCP(); + auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); + auto const seq = lcl.header.ledgerSeq + 1; + bool const leader = + scp.predictNominationLeaders( + seq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) + .count(scp.getLocalNodeID()) != 0; + if (!checkedRoles.insert(leader).second) + { + continue; + } + CAPTURE(leader); + + // Payments signed by their sources' master keys, one by an operation + // source too, and a fee bump. + auto dest = getGenesisAccount(*app, 40); + std::vector txs; + for (uint32_t a = 0; a < 30; ++a) + { + auto source = getGenesisAccount(*app, a); + txs.push_back(source.tx({payment(dest, 1)})); + } + auto opSource = getGenesisAccount(*app, 30); + auto withOpSource = + getGenesisAccount(*app, 31).tx({opSource.op(payment(dest, 1))}); + withOpSource->addSignature(opSource.getSecretKey()); + txs.push_back(withOpSource); + auto feeSource = getGenesisAccount(*app, 32); + auto inner = getGenesisAccount(*app, 33); + txs.push_back( + feeBump(*app, feeSource, inner.tx({payment(dest, 1)}), 1000)); + std::vector envelopes; + for (auto const& tx : txs) + { + envelopes.push_back(tx->getEnvelope()); + } + herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + herder.mGetTopTransactionsForTesting = [&](size_t) { + return envelopes; + }; + + PubKeyUtils::clearVerifySigCache(); + uint64_t hits = 0, misses = 0; + PubKeyUtils::flushVerifySigCacheCounts(hits, misses); + size_t const signatures = + txs.size() + 1 /* op source */ + 1 /* fee bump's inner tx */; + EarlyNominationTestAccess::schedule(herder); + clock.setCurrentVirtualTime(herder.getTriggerTimer().expiry_time()); + testutil::crankUntil( + app, [&]() { return !scp.getNominationLeaders(seq).empty(); }, + std::chrono::seconds(1)); + REQUIRE(!scp.getNominationLeaders(seq).empty()); + + if (leader) + { + // The leader validates its own candidates while building, so it + // does not pre-verify. Its build verified every signature. + PubKeyUtils::flushVerifySigCacheCounts(hits, misses); + REQUIRE(misses >= signatures); + herder.mGetTopTransactionsForTesting = nullptr; + continue; + } + + // Background tasks verify every signature once. Wait in real time + // without cranking: virtual nomination timeouts would promote this + // follower, whose build verifies signatures too. + uint64_t verified = 0; + auto const deadline = + std::chrono::steady_clock::now() + std::chrono::seconds(10); + while (verified < signatures && + std::chrono::steady_clock::now() < deadline) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + PubKeyUtils::flushVerifySigCacheCounts(hits, misses); + verified += misses; + } + REQUIRE(verified == signatures); + REQUIRE(scp.getLatestMessagesSend(seq).empty()); + + // Validating these transactions now finds every signature cached. + for (auto const& tx : txs) + { + auto ledgerView = app->getLedgerManager().getLCLView(); + REQUIRE(tx->checkValid(app->getAppConnector(), *ledgerView, 0, 0, 0) + ->isSuccess()); + } + PubKeyUtils::flushVerifySigCacheCounts(hits, misses); + REQUIRE(misses == 0); + REQUIRE(hits >= signatures); + herder.mGetTopTransactionsForTesting = nullptr; + } + REQUIRE(checkedRoles == std::set{false, true}); +} + TEST_CASE("early underfilled proposals refresh at the trigger", "[herder][early-nomination]") { From 18fa41bbb826141874ab7ca3826638c33d9d3591 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 12:08:25 -0800 Subject: [PATCH 25/39] Back off tx-set fetch retries and retry only undecided slots Follow-up to the fetch retries, from review: - Retries back off (1, 2, 4, then every 8 s). A slow but live transfer of a multi-MB set, under exactly the load where links are slow, was duplicated to another peer every second. - Only sets for slots after the LCL are re-requested. A fetch kept for the checkpoint slot (never purged) was re-requested every second until the checkpoint moved, and the overlay had evicted that set anyway. - overlay.fetch.txset again only times sets delivered by the overlay, not fetches a local build completed. - A Rust retry goes to a random connected peer other than the previous target. Preferring the SCP source again sent every other retry back to a source that may not answer. --- overlay/src/libp2p_overlay.rs | 10 +++-- overlay/src/libp2p_overlay/dispatch_tests.rs | 15 +++++++- src/herder/PendingEnvelopes.cpp | 37 ++++++++++++++---- src/herder/PendingEnvelopes.h | 6 ++- src/herder/test/PendingEnvelopesTests.cpp | 40 ++++++++++++++++---- 5 files changed, 86 insertions(+), 22 deletions(-) diff --git a/overlay/src/libp2p_overlay.rs b/overlay/src/libp2p_overlay.rs index 0d0eb6bed..a46b6e56b 100644 --- a/overlay/src/libp2p_overlay.rs +++ b/overlay/src/libp2p_overlay.rs @@ -984,8 +984,8 @@ impl StellarOverlay { /// 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 - /// prefers a different connected peer: the previous one may have been - /// silent or not had the set, and there is no negative response. + /// 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 = { @@ -1012,8 +1012,10 @@ impl StellarOverlay { 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 - let peer = if let Some(source_peer) = known_source.filter(|p| eligible(p)) { + // 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], diff --git a/overlay/src/libp2p_overlay/dispatch_tests.rs b/overlay/src/libp2p_overlay/dispatch_tests.rs index 3eddf0bb1..361001c85 100644 --- a/overlay/src/libp2p_overlay/dispatch_tests.rs +++ b/overlay/src/libp2p_overlay/dispatch_tests.rs @@ -1332,10 +1332,21 @@ async fn retried_fetch_moves_to_another_peer_and_stale_requests_are_pruned() { overlay.fetch_txset(hash, 21, true).await; assert_eq!(overlay.sends.len(), 2); assert_eq!(requested_peer(&overlay).await, Some((peers[1], 21))); - // With no other peer left, a retry asks the remaining one again. + // 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!(overlay.sends.len(), 3); + 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. diff --git a/src/herder/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp index e5015cada..c8183a569 100644 --- a/src/herder/PendingEnvelopes.cpp +++ b/src/herder/PendingEnvelopes.cpp @@ -273,7 +273,7 @@ PendingEnvelopes::addTxSet(Hash const& hash, uint64 lastSeenSlotIndex, // 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); + completeTxSetFetch(hash, txset, /*deliveredByOverlay=*/false); } bool @@ -311,13 +311,14 @@ PendingEnvelopes::recvTxSet(Hash const& hash, TxSetXDRFrameConstPtr txset) } putTxSet(hash, 0, txset); - completeTxSetFetch(hash, txset); + completeTxSetFetch(hash, txset, /*deliveredByOverlay=*/true); return true; } void PendingEnvelopes::completeTxSetFetch(Hash const& hash, - TxSetXDRFrameConstPtr const& txset) + TxSetXDRFrameConstPtr const& txset, + bool deliveredByOverlay) { auto it = mPendingTxSetFetches.find(hash); if (it == mPendingTxSetFetches.end()) @@ -331,8 +332,13 @@ PendingEnvelopes::completeTxSetFetch(Hash const& hash, auto fetchIt = mTxSetFetches.find(hash); if (fetchIt != mTxSetFetches.end()) { - mFetchTxSetTimer.Update(mApp.getClock().now() - - fetchIt->second.mStarted); + // overlay.fetch.txset times overlay fetches only, not sets this node + // ended up building itself. + if (deliveredByOverlay) + { + mFetchTxSetTimer.Update(mApp.getClock().now() - + fetchIt->second.mStarted); + } mTxSetFetches.erase(fetchIt); } @@ -983,7 +989,13 @@ PendingEnvelopes::stopAllOutsideRange(std::optional minSlot, void PendingEnvelopes::scheduleTxSetFetchRetry() { - if (mTxSetFetchRetryScheduled || mTxSetFetches.empty()) + // 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; } @@ -1005,6 +1017,7 @@ PendingEnvelopes::retryStalledTxSetFetches() // 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() @@ -1012,7 +1025,17 @@ PendingEnvelopes::retryStalledTxSetFetches() SOROBAN_PROTOCOL_VERSION); for (auto& [hash, fetch] : mTxSetFetches) { - if (now - fetch.mLastRequested < TX_SET_FETCH_RETRY_INTERVAL) + // 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; } diff --git a/src/herder/PendingEnvelopes.h b/src/herder/PendingEnvelopes.h index 4f9ad6e60..75f597308 100644 --- a/src/herder/PendingEnvelopes.h +++ b/src/herder/PendingEnvelopes.h @@ -113,7 +113,8 @@ class PendingEnvelopes // Hands a newly known tx set to the envelopes waiting to fetch it. void completeTxSetFetch(Hash const& hash, - TxSetXDRFrameConstPtr const& txset); + TxSetXDRFrameConstPtr const& txset, + bool deliveredByOverlay); void scheduleTxSetFetchRetry(); void retryStalledTxSetFetches(); @@ -150,7 +151,8 @@ class PendingEnvelopes std::optional maxSlot, uint64 slotToKeep); public: - // How long a tx set request may stay unanswered before it is repeated + // 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}; diff --git a/src/herder/test/PendingEnvelopesTests.cpp b/src/herder/test/PendingEnvelopesTests.cpp index 6b7395830..08e925ab9 100644 --- a/src/herder/test/PendingEnvelopesTests.cpp +++ b/src/herder/test/PendingEnvelopesTests.cpp @@ -233,7 +233,8 @@ TEST_CASE_VERSIONS("PendingEnvelopes recvSCPEnvelope", "[herder]") auto m = herder.getSCP().getLatestMessage(pk); REQUIRE(m); REQUIRE(*m == saneEnvelope); - REQUIRE(fetchTimer.count() == fetches + 1); + // Not an overlay fetch: overlay.fetch.txset is not updated. + REQUIRE(fetchTimer.count() == fetches); // A later delivery from the overlay is redundant. REQUIRE(!pendingEnvelopes.recvTxSet(p.second->getContentsHash(), @@ -874,15 +875,40 @@ TEST_CASE("PendingEnvelopes re-requests tx sets the overlay does not deliver", SECTION("an undelivered set is requested again until it arrives") { - crankBy(2 * interval); - auto const retried = retries.count() - initialRetries; - REQUIRE(retried >= 1); - REQUIRE(retried <= 2); + auto& fetchTimer = + app->getMetrics().NewTimer({"overlay", "fetch", "txset"}); + auto const fetches = fetchTimer.count(); + // Requests repeat after 1 s, then back off: 2 s, 4 s, ... + crankBy(interval); // t = 1.5 s + REQUIRE(retries.count() == initialRetries + 1); + crankBy(interval); // t = 2.5 s + REQUIRE(retries.count() == initialRetries + 1); + crankBy(interval); // t = 3.5 s + REQUIRE(retries.count() == initialRetries + 2); + crankBy(3 * interval); // t = 6.5 s + REQUIRE(retries.count() == initialRetries + 2); + crankBy(interval); // t = 7.5 s + REQUIRE(retries.count() == initialRetries + 3); REQUIRE(pendingEnvelopes.recvTxSet(hash, p.second)); REQUIRE(!pendingEnvelopes.getTxSetWaitingTime(hash)); - crankBy(3 * interval); - REQUIRE(retries.count() == initialRetries + retried); + REQUIRE(fetchTimer.count() == fetches + 1); + crankBy(10 * interval); + REQUIRE(retries.count() == initialRetries + 3); + } + + SECTION("a set for a closed slot is not requested again") + { + auto oldEnvelope = + makeEnvelope(herder, s, p, qSetHash, lcl.header.ledgerSeq); + // Purge the undecided slot's fetch; only the old slot's remains. + pendingEnvelopes.eraseOutsideRange(std::nullopt, slot - 1, 0); + REQUIRE(!pendingEnvelopes.getTxSetWaitingTime(hash)); + REQUIRE(pendingEnvelopes.recvSCPEnvelope(oldEnvelope) == + Herder::ENVELOPE_STATUS_FETCHING); + REQUIRE(pendingEnvelopes.getTxSetWaitingTime(hash)); + crankBy(10 * interval); + REQUIRE(retries.count() == initialRetries); } SECTION("purging the waiting slot stops the fetch") From 2798f92840cafbaa5b13d7aa0e22f21f5522afdd Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 12:08:25 -0800 Subject: [PATCH 26/39] Bound the follower signature prewarm Follow-up to the signature prewarm, from review: - Skip it when this node already voted in the slot (it validated the leader's value cold, so the prewarm would only re-check cached signatures while balloting runs). - Stop it when a slot externalizes and on herder shutdown, not only when validation or a local build starts. - Leave two worker threads free for long background jobs (bucket merges): tasks = min(batch executor tasks, WORKER_THREADS - 2). - Cap verifications per transaction at 20 and only try keys whose hint matches, so a crafted transaction with colliding hints cannot make the prewarm expensive. - The test no longer asserts exact process-global verification counts (other code flushes them); it asserts that validation finds every signature cached. --- src/herder/HerderImpl.cpp | 32 ++++++++++++++++++++++++++------ src/herder/test/HerderTests.cpp | 4 +++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 03409ab83..69ab03749 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -74,12 +74,25 @@ namespace void verifyMasterKeySignatures(Hash const& networkID, TransactionEnvelope const& env) { - auto verify = [](Hash const& contentsHash, auto const& signatures, - std::vector const& keys) { + // 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; + auto verify = [&budget](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; if (SignatureUtils::verify(sig, key, contentsHash).valid) { break; @@ -373,6 +386,7 @@ void HerderImpl::shutdown() { discardPreparedTxSet(); + stopSignaturePrewarm(); mTrackingTimer.cancel(); mOutOfSyncTimer.cancel(); mTriggerTimer.cancel(); @@ -566,6 +580,8 @@ HerderImpl::valueExternalized(uint64 slotIndex, StellarValue const& value, { ZoneScoped; int const DUMP_SCP_TIMEOUT_SECONDS = 20; + // The prewarm only serves validating this slot's values. + stopSignaturePrewarm(); recordExternalizeAndCheckCloseTimeDrift(slotIndex, value, mDriftCTSlidingWindow); @@ -2048,11 +2064,13 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, // A follower validates the leader's set when it arrives, and most of its // transactions are in this node's mempool too. Verify their signatures // meanwhile, as building our own set used to before leader-only - // construction, so that validation finds them in the cache. + // construction, 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) && !getSCP() .getNominationLeaders(ledgerSeqToTrigger) - .count(getSCP().getLocalNodeID())) + .count(getSCP().getLocalNodeID()) && + getSCP().getLatestMessagesSend(ledgerSeqToTrigger).empty()) { prewarmSignatureCache(); } @@ -2099,10 +2117,12 @@ HerderImpl::prewarmSignatureCache() auto stop = std::make_shared>(false); mSignaturePrewarmStop = stop; // The batch executor (validation) is idle until the leader's set - // arrives; use as many background tasks as it would. + // arrives; 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(), - mApp.getConfig().WORKER_THREADS)); + workers > 2 ? workers - 2 : 1)); CLOG_DEBUG(Herder, "Pre-verifying signatures of {} transactions in {} tasks", envelopes->size(), tasks); diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 756a0ff96..44d040190 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -7688,7 +7688,9 @@ TEST_CASE("followers pre-verify mempool signatures at the trigger", PubKeyUtils::flushVerifySigCacheCounts(hits, misses); verified += misses; } - REQUIRE(verified == signatures); + // The counters are process-global and other code flushes them too, so + // they only bound the wait; the cache check below is the assertion. + INFO("verified " << verified << " of " << signatures); REQUIRE(scp.getLatestMessagesSend(seq).empty()); // Validating these transactions now finds every signature cached. From ba7ef4d9aa153780eadd567f73c9074998819201 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 12:11:19 -0800 Subject: [PATCH 27/39] Test externalizing inside a leader's own tx-set build Since locally built sets complete pending fetches, a leader that builds the very set a peer's ballot is waiting for reprocesses that envelope inside SCP's nomination value supplier. With the peer v-blocking, the slot externalizes before the supplier returns. Cover that path: the peer's CONFIRM waits for the (empty, hence identical) set, the local leader's trigger builds it, the slot closes with the peer's value, and nomination stops without voting or emitting a nomination. --- src/herder/test/HerderTests.cpp | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 44d040190..4d6d00c51 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -7581,6 +7581,99 @@ TEST_CASE("a follower promoted after losing sync still proposes", REQUIRE(checkedFollower); } +TEST_CASE("a leader build that completes a fetch can externalize", + "[herder][early-nomination]") +{ + // The local leader builds the very set a peer's ballot is waiting for (an + // empty set is the same on every node). Publishing it completes the fetch + // inside SCP's nomination value supplier: the peer's CONFIRM is + // reprocessed there and, the peer being v-blocking, externalizes the slot + // before the supplier returns. Nomination must then stop cleanly without + // voting or emitting. + VirtualClock clock; + clock.setCurrentVirtualTime(VirtualClock::from_time_t(1000)); + auto cfg = getTestConfig(); + cfg.HTTP_PORT = 0; + // Herder discards network envelopes under MANUAL_CLOSE. + cfg.MANUAL_CLOSE = false; + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = false; + cfg.FORCE_OLD_STYLE_PREPARE_START_TRIGGER_TIMER = true; + cfg.QUORUM_SET.threshold = 2; + cfg.QUORUM_SET.validators = { + cfg.NODE_SEED.getPublicKey(), + SecretKey::pseudoRandomForTesting().getPublicKey()}; + auto app = createTestApplication(clock, cfg); + auto& herder = static_cast(app->getHerder()); + auto& scp = herder.getSCP(); + EarlyNominationTestAccess::selectLocalFirstLeader(herder); + auto const qset = scp.getLocalQuorumSet(); + auto const qsetHash = scp.getLocalNode()->getQuorumSetHash(); + std::optional peer; + for (int i = 0; i < 100 && !peer; ++i) + { + auto key = + SecretKey::fromSeed(sha256(fmt::format("absent-peer-{}", i))); + if (std::find(qset.validators.begin(), qset.validators.end(), + key.getPublicKey()) != qset.validators.end()) + { + peer = key; + } + } + REQUIRE(peer); + + auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); + auto const seq = lcl.header.ledgerSeq + 1; + herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + herder.mGetTopTransactionsForTesting = [](size_t) { + return std::vector{}; + }; + // The set the local leader is about to build. + PerPhaseTransactionList phases(2); + auto const expected = + makeTxSetFromTransactions(phases, *app, ApplyTimeOffset{}).first; + REQUIRE( + !herder.getPendingEnvelopes().hasTxSet(expected->getContentsHash())); + + auto const peerValue = herder.makeStellarValue( + expected->getContentsHash(), + makeConsensusTime(lcl.header.scpValue.closeTime + 1), emptyUpgradeSteps, + *peer); + SCPEnvelope confirm; + confirm.statement.nodeID = peer->getPublicKey(); + confirm.statement.slotIndex = seq; + confirm.statement.pledges.type(SCP_ST_CONFIRM); + auto& conf = confirm.statement.pledges.confirm(); + conf.ballot.counter = 1; + conf.ballot.value = xdr::xdr_to_opaque(peerValue); + conf.nPrepared = 1; + conf.nCommit = 1; + conf.nH = 1; + conf.quorumSetHash = qsetHash; + herder.signEnvelope(*peer, confirm); + REQUIRE(herder.recvSCPEnvelope(confirm) == + Herder::ENVELOPE_STATUS_FETCHING); + REQUIRE(scp.getLatestMessage(peer->getPublicKey()) == nullptr); + + herder.triggerNextLedger(seq, true); + + // Processed and externalized during the leader's own build. + REQUIRE(scp.getLatestMessage(peer->getPublicKey())); + testutil::crankUntil( + app, + [&]() { + return app->getLedgerManager().getLastClosedLedgerNum() == seq; + }, + std::chrono::seconds(10)); + auto const closed = app->getLedgerManager().getLastClosedLedgerHeader(); + REQUIRE(closed.header.ledgerSeq == seq); + REQUIRE(closed.header.scpValue == peerValue); + for (auto const& env : scp.getLatestMessagesSend(seq)) + { + REQUIRE(env.statement.pledges.type() != SCP_ST_NOMINATE); + } + herder.mGetTopTransactionsForTesting = nullptr; +} + TEST_CASE("followers pre-verify mempool signatures at the trigger", "[herder][early-nomination]") { From 3c5d8851f0237b5b95a3f0c7a232bbc9ae132a36 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 14:15:59 -0800 Subject: [PATCH 28/39] Skip early tx-set preparation when the trigger is already due setupTriggerNextLedger arms the early-preparation timer for a predicted first-round leader at "now", next to the trigger timer. When the trigger is already due, both fire at the same instant, and VirtualClock orders equal-time events from different timers arbitrarily (event sequence numbers are per timer). If preparation ran first, the leader built its set once to prepare and again at the trigger (an underfilled prepared set is refreshed), costing a full build on exactly the late ledgers where the trigger is due immediately. "early preparation respects manual and immediately due triggers" caught it: with leader-only construction the test reschedules with both timers due, and it failed in partitioned make check (base instance 400, seed 20766: two pulls instead of one) while passing with other instances. Arm early preparation only when the trigger is still ahead; a due trigger builds itself. The test now also checks that nothing was scheduled to prepare, instead of depending on timer order. --- src/herder/HerderImpl.cpp | 7 ++++++- src/herder/test/HerderTests.cpp | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 69ab03749..7c3929008 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1587,7 +1587,12 @@ 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), 1); diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 4d6d00c51..a7350e2bd 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -7905,6 +7905,9 @@ TEST_CASE("early preparation respects manual and immediately due triggers", EarlyNominationTestAccess::schedule(herder); // No previous prepare timestamp: the normal fallback triggers now. REQUIRE(herder.getTriggerTimer().expiry_time() == clock.now()); + // A due trigger builds itself. Preparing too could build twice: the + // two timers would fire at the same instant in either order. + REQUIRE(!EarlyNominationTestAccess::scheduled(herder)); testutil::crankUntil( app, [&]() { From 11d416db83a0502749eedd70feb7931b9ae94abf Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 15:26:58 -0800 Subject: [PATCH 29/39] Time tx-set build phases and add a hidden leader build benchmark Phase timers herder.txset.build-{pull,frames,trim,select,xdr,handoff, roundtrip,final-check} around HerderImpl::buildTxSet and makeTxSetFromTransactions, and a hidden benchmark ("leader tx set build costs", tags [herder][.][benchmark]) that hands the herder 15,000 SAC payments (ApplyLoad's generator) through an exact replay of the overlay IPC decode and times each phase with a cold and a warm signature cache, plus cold received-set validation for calibration against perfnet. BENCH_REPS, BENCH_TASKS, BENCH_WARM and BENCH_PULL control it; under callgrind with --instr-atstart=no only the cold builds are instrumented. --- src/herder/HerderImpl.cpp | 11 +- src/herder/TxSetFrame.cpp | 18 +++ src/herder/test/HerderTests.cpp | 197 ++++++++++++++++++++++++++++++++ src/simulation/ApplyLoad.cpp | 17 +++ src/simulation/ApplyLoad.h | 7 ++ 5 files changed, 249 insertions(+), 1 deletion(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 7c3929008..882d59568 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1819,7 +1819,13 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) auto& overlayMgr = mApp.getOverlayManager(); // This build validates every candidate itself. stopSignaturePrewarm(); - auto txEnvelopes = pullTopTransactions(maxTxSetCandidates() * 2); + 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()); @@ -1887,6 +1893,7 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) PerPhaseTransactionList invalidTxPhases; invalidTxPhases.resize(txPhases.size()); + framesTimer.Stop(); std::tie(proposedSet, applicableProposedSet) = makeTxSetFromTransactions( txPhases, mApp, closeTimeOffset, invalidTxPhases @@ -1896,6 +1903,8 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) #endif , [&](TxSetXDRFrameConstPtr const& txSet) { + auto t = metrics.NewTimer({"herder", "txset", "build-handoff"}) + .TimeScope(); // Hand the final XDR to the overlay before the builder's remaining // validation. Its eager compression runs concurrently in Rust. if (txSet->isGeneralizedTxSet()) diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index 43e6362d5..3968b1478 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -914,6 +914,10 @@ makeTxSetFromTransactions( else { #endif + auto trimTimer = + app.getMetrics() + .NewTimer({"herder", "txset", "build-trim"}) + .TimeScope(); validatedTxs = TxSetUtils::trimInvalid( phaseTxs, app, accountFeeMap, closeTimeOffset.seconds(), closeTimeOffset.seconds(), invalid); @@ -936,6 +940,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 @@ -970,6 +977,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( @@ -991,6 +1000,7 @@ 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. @@ -999,8 +1009,12 @@ makeTxSetFromTransactions( onTxSetReady(outputTxSet); } + auto roundtripTimer = app.getMetrics() + .NewTimer({"herder", "txset", "build-roundtrip"}) + .TimeScope(); ApplicableTxSetFrameConstPtr outputApplicableTxSet = outputTxSet->prepareForApply(app, lclHeader.header); + roundtripTimer.Stop(); if (!outputApplicableTxSet) { @@ -1029,8 +1043,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( diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index a7350e2bd..82cf71970 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" @@ -6769,6 +6771,13 @@ class EarlyNominationTestAccess herder.setupTriggerNextLedger(); } + // The leader's full build (mempool pull through final checks). + static TxSetXDRFrameConstPtr + build(HerderImpl& herder, uint32_t ledgerSeq, ConsensusTime closeTime) + { + return herder.buildTxSet(ledgerSeq, closeTime).txSet; + } + // These fixtures inspect a local proposal while requiring an absent peer // to prevent externalization. Pick that peer so the local node is the // first leader, independently of the test RNG seed and previous ledger. @@ -8301,3 +8310,191 @@ TEST_CASE("upgrade scheduling under sub-second ledgers", "[upgrades]") .createUpgradesFor(header, *ledgerView, app->getConfig()); REQUIRE(notDue.empty()); } + +#if __has_include() +#include +#else +#define CALLGRIND_START_INSTRUMENTATION +#define CALLGRIND_STOP_INSTRUMENTATION +#endif + +// A leader's tx-set build at perfnet's 3k SAC TPS scale: the mempool hands +// over twice the cap of SAC payments (15,000 here vs perfnet's 15,200), all +// valid, never validated by Core before (the Rust overlay admitted them), and +// the builder keeps 7,500 in a parallel Soroban phase. Times each phase of +// HerderImpl::buildTxSet with a cold and with a warm signature cache. +// Env: BENCH_REPS (default 5), BENCH_TASKS (validation tasks, default 4 = +// c5d.2xlarge physical cores), BENCH_WARM=0 to skip the warm runs. Under +// valgrind --tool=callgrind --instr-atstart=no only the cold builds are +// instrumented. +TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") +{ + auto envInt = [](char const* name, uint32_t dflt) { + auto v = std::getenv(name); + return v ? static_cast(std::stoul(v)) : dflt; + }; + uint32_t const sorobanCap = 7500; + uint32_t const candidates = 2 * sorobanCap; + uint32_t const reps = envInt("BENCH_REPS", 5); + uint32_t const tasks = envInt("BENCH_TASKS", 4); + bool const warmRuns = envInt("BENCH_WARM", 1) != 0; + + auto cfg = getTestConfig(); + cfg.APPLY_LOAD_MODE = ApplyLoadMode::BENCHMARK_MODEL_TX; + cfg.APPLY_LOAD_MODEL_TX = ApplyLoadModelTx::SAC; + cfg.USE_CONFIG_FOR_GENESIS = true; + cfg.LEDGER_PROTOCOL_VERSION = Config::CURRENT_LEDGER_PROTOCOL_VERSION; + cfg.MANUAL_CLOSE = true; + cfg.IGNORE_MESSAGE_LIMITS_FOR_TESTING = true; + cfg.APPLY_LOAD_MAX_SOROBAN_TX_COUNT = sorobanCap; + cfg.APPLY_LOAD_BATCH_SAC_COUNT = 1; + cfg.APPLY_LOAD_LEDGER_MAX_DEPENDENT_TX_CLUSTERS = 1; + cfg.APPLY_LOAD_CLASSIC_TXS_PER_LEDGER = 0; + cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = 100; + cfg.GENESIS_TEST_ACCOUNT_COUNT = candidates + 10; + VirtualClock clock(VirtualClock::REAL_TIME); + auto app = createTestApplication(clock, cfg); + auto& herder = static_cast(app->getHerder()); + + std::vector envelopes; + { + ApplyLoad al(*app); + envelopes = al.generateSacPaymentEnvelopesForTesting(candidates); + } + REQUIRE(envelopes.size() == candidates); + app->getBatchExecutor().setPreferredTaskCountForTesting(tasks); + + // The Rust overlay's GetTopTxs response, decoded exactly as + // OverlayIPC::getTopTransactions does: [count:4]([len:4][xdr])* + std::vector payload(4); + uint32_t const count = static_cast(envelopes.size()); + std::memcpy(payload.data(), &count, 4); + for (auto const& env : envelopes) + { + auto bytes = xdr::xdr_to_opaque(env); + uint32_t const len = static_cast(bytes.size()); + auto const at = payload.size(); + payload.resize(at + 4 + len); + std::memcpy(payload.data() + at, &len, 4); + std::memcpy(payload.data() + at + 4, bytes.data(), len); + } + // BENCH_PULL: hand over only the first N candidates (a smaller pull). + uint32_t const pull = std::min(envInt("BENCH_PULL", count), count); + herder.mGetTopTransactionsForTesting = [&](size_t) { + std::vector result; + size_t offset = 4; + for (uint32_t i = 0; i < pull; ++i) + { + uint32_t len; + std::memcpy(&len, payload.data() + offset, 4); + offset += 4; + TransactionEnvelope tx; + std::vector txData(payload.begin() + offset, + payload.begin() + offset + len); + xdr::xdr_from_opaque(txData, tx); + result.push_back(std::move(tx)); + offset += len; + } + return result; + }; + + auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); + auto const seq = lcl.header.ledgerSeq + 1; + auto const closeTime = + getConsensusTime(lcl.header.scpValue).next(lcl.header.ledgerVersion); + std::vector const phases = { + "build-pull", "build-frames", "build-trim", + "build-select", "build-xdr", "build-handoff", + "build-roundtrip", "build-final-check", "build"}; + auto& metrics = app->getMetrics(); + + std::cout << "=== leader tx set build: " << pull << " of " << candidates + << " SAC candidates, cap " << sorobanCap << ", " << tasks + << " validation tasks, IPC payload " << payload.size() + << " bytes ===" << std::endl; + for (bool cold : {true, false}) + { + if (!cold && !warmRuns) + { + continue; + } + std::map> samples; + size_t included = 0; + uint64_t misses = 0; + for (uint32_t rep = 0; rep < reps; ++rep) + { + if (cold) + { + PubKeyUtils::clearVerifySigCache(); + } + uint64_t h = 0, m = 0; + PubKeyUtils::flushVerifySigCacheCounts(h, m); + for (auto const& phase : phases) + { + metrics.NewTimer({"herder", "txset", phase}).Clear(); + } + if (cold) + { + CALLGRIND_START_INSTRUMENTATION; + } + auto txSet = + EarlyNominationTestAccess::build(herder, seq, closeTime); + if (cold) + { + CALLGRIND_STOP_INSTRUMENTATION; + } + PubKeyUtils::flushVerifySigCacheCounts(h, m); + misses = m; + included = txSet->sizeTxTotal(); + for (auto const& phase : phases) + { + samples[phase].push_back( + metrics.NewTimer({"herder", "txset", phase}).sum()); + } + // The overlay handoff's serialization into the IPC message (the + // test app has no overlay process, so time it here). + auto const start = std::chrono::steady_clock::now(); + auto const& xdrSet = txSet->getGeneralizedXDR(); + std::vector msg(36 + xdr::xdr_argpack_size(xdrSet)); + xdr::xdr_put put(msg.data() + 36, msg.data() + msg.size()); + xdr::xdr_argpack_archive(put, xdrSet); + samples["handoff-serialize"].push_back( + std::chrono::duration( + std::chrono::steady_clock::now() - start) + .count()); + } + REQUIRE(included == std::min(sorobanCap, pull)); + if (cold) + { + // Calibration against perfnet's herder.txset.validate: a + // follower validating the received set with a cold cache. + auto const& lastSet = + EarlyNominationTestAccess::build(herder, seq, closeTime); + for (uint32_t rep = 0; rep < reps; ++rep) + { + auto received = TxSetXDRFrame::makeFromWire( + GeneralizedTransactionSet(lastSet->getGeneralizedXDR())); + PubKeyUtils::clearVerifySigCache(); + auto const start = std::chrono::steady_clock::now(); + auto applicable = received->prepareForApply(*app, lcl.header); + REQUIRE(applicable); + REQUIRE(applicable->checkValid(*app, 0, 0)); + samples["received-set-validate"].push_back( + std::chrono::duration( + std::chrono::steady_clock::now() - start) + .count()); + } + } + std::cout << (cold ? "cold" : "warm") + << " signature cache (last build: " << misses + << " verifications, " << included << " txs)" << std::endl; + for (auto& [phase, v] : samples) + { + std::sort(v.begin(), v.end()); + std::cout << " " << phase << ": median " << v[v.size() / 2] + << " ms (min " << v.front() << ", max " << v.back() + << ", n=" << v.size() << ")" << std::endl; + } + } + herder.mGetTopTransactionsForTesting = nullptr; +} diff --git a/src/simulation/ApplyLoad.cpp b/src/simulation/ApplyLoad.cpp index 0e98d9fe1..720f030d8 100644 --- a/src/simulation/ApplyLoad.cpp +++ b/src/simulation/ApplyLoad.cpp @@ -959,6 +959,23 @@ ApplyLoad::closeLedger(std::vector const& txs, stellar::txtest::closeLedger(mApp, txs, /* strictOrder */ false, upgrades); } +#ifdef BUILD_TESTS +std::vector +ApplyLoad::generateSacPaymentEnvelopesForTesting(uint32_t count) +{ + std::vector txs; + txs.reserve(count); + generateSacPayments(txs, count); + std::vector envelopes; + envelopes.reserve(txs.size()); + for (auto const& tx : txs) + { + envelopes.emplace_back(tx->getEnvelope()); + } + return envelopes; +} +#endif + void ApplyLoad::closeBenchmarkLedger(std::vector const& txs, bool recordUtilization) diff --git a/src/simulation/ApplyLoad.h b/src/simulation/ApplyLoad.h index d0fdda704..555817047 100644 --- a/src/simulation/ApplyLoad.h +++ b/src/simulation/ApplyLoad.h @@ -22,6 +22,13 @@ class ApplyLoad // of values is [0,1.0]. double successRate(); +#ifdef BUILD_TESTS + // Signed SAC payments from distinct sources, as envelopes the way a + // leader's mempool hands them to the herder (tx-set build benchmarks). + std::vector + generateSacPaymentEnvelopesForTesting(uint32_t count); +#endif + // Closes a setup ledger through direct externalization. Setup and upgrades // are excluded from benchmark phase timings. void closeLedger(std::vector const& txs, From 925dca9f92bc6fa990c1bc8df00157873c9e7d03 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 15:49:17 -0800 Subject: [PATCH 30/39] Validate only as many tx-set candidates as selection can use The leader pulls twice the tx-set capacity from the mempool and validated every candidate, although selection keeps at most the capacity. Signature verification of the surplus was most of the build's cost (15,000 SAC candidates for 7,500 slots on perfnet). Keep the same pull, but validate each phase in mempool order only until it holds 5/4 of its capacity in valid transactions of its generic surge pricing lane (TxSetUtils::trimInvalidPrefix): the first 5/4 of the capacity, then twice the shortfall if some were invalid, then everything left, so at most three passes and each candidate validated at most once. DEX-lane transactions don't count toward the target, since their lane can't fill the phase. Candidates past the validated prefix are neither selected nor reported invalid; they stay in the mempool. The selected set is the same as when validating everything unless selection would have reached past the prefix: a candidate that doesn't fit the remaining resources leaving room that only later candidates fit, a mempool order (total fee per operation) that differs from the inclusion fee order of Soroban transactions, equal fees at the cutoff (broken randomly either way), or a fee source paying for several candidates (its fees accumulate over fewer of them). Laptop benchmark (15,000 SAC candidates, 4 tasks, n=5 medians): cold signature cache 261.0 -> 183.6 ms, warm 124.3 -> 98.7 ms. --- src/herder/HerderImpl.cpp | 30 +++++- src/herder/TxSetFrame.cpp | 29 +++++- src/herder/TxSetFrame.h | 15 ++- src/herder/TxSetUtils.cpp | 45 +++++++++ src/herder/TxSetUtils.h | 16 +++ src/herder/test/HerderTests.cpp | 75 +++++++++++++++ src/herder/test/TxSetTests.cpp | 166 ++++++++++++++++++++++++++++++++ 7 files changed, 368 insertions(+), 8 deletions(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 882d59568..56b3684d6 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1891,6 +1891,28 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) txPhases.emplace_back(std::move(sorobanTxs)); } + // 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. Validate 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 validTxTargets{ + validationTarget(mLedgerManager.getLastMaxTxSetSizeOps())}; + if (supportsSoroban) + { + validTxTargets.push_back(validationTarget( + mLedgerManager.hasLastClosedSorobanNetworkConfig() + ? mLedgerManager.getLastClosedSorobanNetworkConfig() + .ledgerMaxTxCount() + : 0)); + } + PerPhaseTransactionList invalidTxPhases; invalidTxPhases.resize(txPhases.size()); framesTimer.Stop(); @@ -1914,7 +1936,8 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) overlayMgr.cacheTxSet(txSet->getContentsHash(), txSet->getGeneralizedXDR(), ledgerSeq); } - }); + }, + validTxTargets); CLOG_INFO(Herder, "Proposed TX set has {} transactions", proposedSet->sizeTxTotal()); @@ -1952,6 +1975,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) { diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index 3968b1478..8c96fa2a4 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -878,11 +878,14 @@ makeTxSetFromTransactions( bool disableTxValidationForLegacyScenario #endif , - TxSetReadyCallback const& onTxSetReady) + 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)); @@ -918,9 +921,27 @@ makeTxSetFromTransactions( app.getMetrics() .NewTimer({"herder", "txset", "build-trim"}) .TimeScope(); - validatedTxs = TxSetUtils::trimInvalid( - phaseTxs, app, accountFeeMap, closeTimeOffset.seconds(), - closeTimeOffset.seconds(), invalid); + 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 diff --git a/src/herder/TxSetFrame.h b/src/herder/TxSetFrame.h index 78ce87a3e..8bbb4c1ac 100644 --- a/src/herder/TxSetFrame.h +++ b/src/herder/TxSetFrame.h @@ -118,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. @@ -154,7 +160,8 @@ makeTxSetFromTransactions( bool disableTxValidationForLegacyScenario = false #endif , - TxSetReadyCallback const& onTxSetReady = {}); + TxSetReadyCallback const& onTxSetReady = {}, + std::vector const& validTxTargets = {}); #ifdef BUILD_TESTS std::pair @@ -417,7 +424,8 @@ class TxSetPhaseFrame bool disableTxValidationForLegacyScenario #endif , - TxSetReadyCallback const& onTxSetReady); + TxSetReadyCallback const& onTxSetReady, + std::vector const& validTxTargets); #ifdef BUILD_TESTS friend std::pair makeTxSetFromTransactions( @@ -592,7 +600,8 @@ class ApplicableTxSetFrame bool disableTxValidationForLegacyScenario #endif , - TxSetReadyCallback const& onTxSetReady); + 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 80df5f4d6..f2c8564f2 100644 --- a/src/herder/TxSetUtils.cpp +++ b/src/herder/TxSetUtils.cpp @@ -333,4 +333,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 160ae09eb..8ac855ab0 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 82cf71970..980929f8c 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -6778,6 +6778,15 @@ class EarlyNominationTestAccess return herder.buildTxSet(ledgerSeq, closeTime).txSet; } + // The same, with the hashes of the invalid candidates it found. + static std::pair> + buildWithInvalid(HerderImpl& herder, uint32_t ledgerSeq, + ConsensusTime closeTime) + { + auto prepared = herder.buildTxSet(ledgerSeq, closeTime); + return {prepared.txSet, prepared.invalidTxHashes}; + } + // These fixtures inspect a local proposal while requiring an absent peer // to prevent externalization. Pick that peer so the local node is the // first leader, independently of the test RNG seed and previous ledger. @@ -8311,6 +8320,72 @@ TEST_CASE("upgrade scheduling under sub-second ledgers", "[upgrades]") REQUIRE(notDue.empty()); } +TEST_CASE("leader builds validate only the top of the mempool pull", + "[herder][txset]") +{ + // Classic capacity 10: the herder validates classic candidates until 13 + // are valid. The mempool holds 60 payments bidding 1000 - i, so its order + // is i. The top 5 and #40 have a stale sequence number (the account's + // current one), so the herder drops them from the mempool once it finds + // them invalid. + uint32_t const candidates = 60; + auto cfg = getTestConfig(); + cfg.MANUAL_CLOSE = true; + cfg.GENESIS_TEST_ACCOUNT_COUNT = candidates + 1; + cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = 10; + VirtualClock clock; + auto app = createTestApplication(clock, cfg); + auto& herder = static_cast(app->getHerder()); + auto& lm = app->getLedgerManager(); + REQUIRE(lm.getLastMaxTxSetSizeOps() == 10); + auto dest = getGenesisAccount(*app, candidates); + std::vector mempool; + std::vector hashes; + for (uint32_t i = 0; i < candidates; ++i) + { + auto source = getGenesisAccount(*app, i); + bool const badSeq = i < 5 || i == 40; + auto tx = transactionFromOperations( + *app, source.getSecretKey(), + source.getLastSequenceNumber() + (badSeq ? 0 : 1), + {payment(dest.getPublicKey(), 1)}, 1000 - i); + mempool.push_back(tx->getEnvelope()); + hashes.push_back(tx->getFullHash()); + } + size_t requested = 0; + herder.mGetTopTransactionsForTesting = [&](size_t count) { + requested = count; + return std::vector( + mempool.begin(), mempool.begin() + std::min(count, mempool.size())); + }; + + auto const lcl = lm.getLastClosedLedgerHeader(); + auto [txSet, invalid] = EarlyNominationTestAccess::buildWithInvalid( + herder, lcl.header.ledgerSeq + 1, + getConsensusTime(lcl.header.scpValue).next(lcl.header.ledgerVersion)); + herder.mGetTopTransactionsForTesting = nullptr; + + // The pull is unchanged: twice the capacity of both phases. + REQUIRE( + requested == + 2 * (10 + lm.getLastClosedSorobanNetworkConfig().ledgerMaxTxCount())); + // The invalid top candidates were replaced: the set is full, with the + // best valid ones. + auto applicable = txSet->prepareForApply(*app, lcl.header); + REQUIRE(applicable); + std::set included; + for (auto const& tx : applicable->getPhase(TxSetPhase::CLASSIC)) + { + included.insert(tx->getFullHash()); + } + REQUIRE(included == + std::set(hashes.begin() + 5, hashes.begin() + 15)); + // #40 is past the validated candidates (0-22): it stays in the mempool + // until it reaches them. + REQUIRE(std::set(invalid.begin(), invalid.end()) == + std::set(hashes.begin(), hashes.begin() + 5)); +} + #if __has_include() #include #else diff --git a/src/herder/test/TxSetTests.cpp b/src/herder/test/TxSetTests.cpp index f23037071..d963e0420 100644 --- a/src/herder/test/TxSetTests.cpp +++ b/src/herder/test/TxSetTests.cpp @@ -72,6 +72,172 @@ TEST_CASE("built tx set is available before final validation returns", REQUIRE(applicable->checkValid(*app, 0, 0)); } +TEST_CASE("tx set building validates only as many candidates as it needs", + "[txset]") +{ + // A classic phase of 10 operations, of which DEX transactions may use 2. + // The herder's target for it is 5/4 of that: 13 valid transactions. + uint32_t const candidates = 40; + std::vector const targets{13, 13}; + VirtualClock clock; + auto cfg = getTestConfig(); + cfg.GENESIS_TEST_ACCOUNT_COUNT = candidates + 1; + cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = 10; + cfg.MAX_DEX_TX_OPERATIONS_IN_TX_SET = 2; + auto app = createTestApplication(clock, cfg); + REQUIRE(app->getLedgerManager().getLastMaxTxSetSizeOps() == 10); + auto dest = getGenesisAccount(*app, candidates); + auto const usd = makeAsset(dest.getSecretKey(), "USD"); + + enum class Kind + { + PAYMENT, + BAD_SEQ, + DEX, + SIX_OPS + }; + // Candidate i bids 1000 - i per operation, so the input order is the + // priority order. Each one has its own source account. + auto makeCandidates = [&](std::function const& kindOf) { + TxFrameList txs; + for (uint32_t i = 0; i < candidates; ++i) + { + auto source = getGenesisAccount(*app, i); + auto const kind = kindOf(i); + std::vector ops; + if (kind == Kind::DEX) + { + ops.push_back( + manageOffer(0, makeNativeAsset(), usd, Price{1, 1}, 1)); + } + else + { + ops.resize(kind == Kind::SIX_OPS ? 6 : 1, + payment(dest.getPublicKey(), 1)); + } + auto const seq = source.getLastSequenceNumber() + + (kind == Kind::BAD_SEQ ? 0 : 1); + txs.push_back(transactionFromOperations( + *app, source.getSecretKey(), seq, ops, + static_cast((1000 - i) * ops.size()))); + } + return txs; + }; + struct Built + { + Hash contentsHash; + std::set included; + std::vector invalid; + }; + // Builds with the targets, or validating every candidate (`targets` {}). + auto build = [&](TxFrameList const& txs, + std::vector const& validTxTargets) { + std::map indexOf; + for (uint32_t i = 0; i < txs.size(); ++i) + { + indexOf.emplace(txs[i]->getFullHash(), i); + } + PerPhaseTransactionList phases{txs, {}}; + PerPhaseTransactionList invalid(2); + auto [txSet, applicable] = + makeTxSetFromTransactions(phases, *app, ApplyTimeOffset{}, invalid, + false, {}, false, {}, validTxTargets); + REQUIRE(applicable->checkValid(*app, 0, 0)); + REQUIRE(invalid[1].empty()); + Built built{txSet->getContentsHash(), {}, {}}; + for (auto const& tx : applicable->getPhase(TxSetPhase::CLASSIC)) + { + built.included.insert(indexOf.at(tx->getFullHash())); + } + for (auto const& tx : invalid[0]) + { + built.invalid.push_back(indexOf.at(tx->getFullHash())); + } + return built; + }; + auto range = [](uint32_t begin, uint32_t end) { + std::set result; + for (uint32_t i = begin; i < end; ++i) + { + result.insert(i); + } + return result; + }; + + SECTION("nothing invalid near the top: same set as validating everything") + { + // Candidate 30 is past the 13 validated ones, so the windowed build + // never finds out that it is invalid. + auto txs = makeCandidates( + [](uint32_t i) { return i == 30 ? Kind::BAD_SEQ : Kind::PAYMENT; }); + auto windowed = build(txs, targets); + auto full = build(txs, {}); + REQUIRE(windowed.included == range(0, 10)); + REQUIRE(windowed.invalid.empty()); + REQUIRE(full.invalid == std::vector{30}); + REQUIRE(windowed.contentsHash == full.contentsHash); + } + SECTION("invalid top candidates are replaced from further down") + { + // The first pass validates 0-12 and finds 8 valid, the second + // validates the next 2 * 5 (13-22). + auto txs = makeCandidates([](uint32_t i) { + return i < 5 || i == 30 ? Kind::BAD_SEQ : Kind::PAYMENT; + }); + auto windowed = build(txs, targets); + auto full = build(txs, {}); + REQUIRE(windowed.included == range(5, 15)); + REQUIRE(windowed.invalid == std::vector{0, 1, 2, 3, 4}); + REQUIRE(full.invalid == std::vector{0, 1, 2, 3, 4, 30}); + REQUIRE(windowed.contentsHash == full.contentsHash); + } + SECTION("mostly invalid: each candidate is validated once") + { + // Three passes: 0-12, 13-38, 39. Every invalid candidate is reported + // exactly once, and the set holds the two valid ones. + auto txs = makeCandidates([](uint32_t i) { + return i == 25 || i == 35 ? Kind::PAYMENT : Kind::BAD_SEQ; + }); + auto windowed = build(txs, targets); + auto full = build(txs, {}); + REQUIRE(windowed.included == std::set{25, 35}); + auto expectedInvalid = range(0, candidates); + expectedInvalid.erase(25); + expectedInvalid.erase(35); + REQUIRE(windowed.invalid == + std::vector(expectedInvalid.begin(), + expectedInvalid.end())); + REQUIRE(windowed.contentsHash == full.contentsHash); + } + SECTION("DEX transactions don't count toward the target") + { + // The DEX lane takes only 2 of the 15 DEX candidates. Had they + // counted, the 13 validated ones would have filled the target and the + // set would hold just those 2. + auto txs = makeCandidates( + [](uint32_t i) { return i < 15 ? Kind::DEX : Kind::PAYMENT; }); + auto windowed = build(txs, targets); + auto full = build(txs, {}); + auto expected = range(15, 23); + expected.insert({0, 1}); + REQUIRE(windowed.included == expected); + REQUIRE(windowed.contentsHash == full.contentsHash); + } + SECTION("where it differs: only candidates past the window fit the rest") + { + // The first six-operation transaction leaves room for four + // operations, which none of the other validated (six-operation) + // candidates fit. Validating everything fills that room with the + // one-operation candidates 13-16. + auto txs = makeCandidates( + [](uint32_t i) { return i < 13 ? Kind::SIX_OPS : Kind::PAYMENT; }); + auto windowed = build(txs, targets); + auto full = build(txs, {}); + REQUIRE(windowed.included == std::set{0}); + REQUIRE(full.included == std::set{0, 13, 14, 15, 16}); + } +} + TEST_CASE("generalized tx set XDR validation", "[txset]") { Config cfg(getTestConfig()); From 1e16ae04b430d9a01a6c0dad5431c86fc1a511f2 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 15:51:41 -0800 Subject: [PATCH 31/39] Hash tx-set candidates on the validation workers Building a tx set hashed every candidate's full envelope on the main thread (removing the invalid ones, then sorting the set by hash): SHA-256 of every candidate, 18% of a cold build's instructions on hosts without SHA extensions such as c5d. Compute and cache each candidate's full hash in the parallel validation task that already validates it (which also computes its contents hash for the signature checks), and skip removeTxs' lookups when nothing is invalid. Same hashes, same order: only where they are computed changes. Each frame is in exactly one task range and the batch completes before the main thread reads the cached hash. Laptop benchmark (has SHA-NI): warm build-trim 22.1 -> 18.0 ms. --- src/herder/TxSetUtils.cpp | 10 +++++++ src/herder/test/TxSetTests.cpp | 55 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/herder/TxSetUtils.cpp b/src/herder/TxSetUtils.cpp index f2c8564f2..60ee51e0c 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, diff --git a/src/herder/test/TxSetTests.cpp b/src/herder/test/TxSetTests.cpp index d963e0420..781c905b7 100644 --- a/src/herder/test/TxSetTests.cpp +++ b/src/herder/test/TxSetTests.cpp @@ -238,6 +238,61 @@ TEST_CASE("tx set building validates only as many candidates as it needs", } } +TEST_CASE("tx set validation caches candidate hashes", "[txset]") +{ + uint32_t const count = 64; + VirtualClock clock; + auto cfg = getTestConfig(); + cfg.GENESIS_TEST_ACCOUNT_COUNT = count + 1; + cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = count; + auto app = createTestApplication(clock, cfg); + // Several tasks, so most hashes are computed off the main thread. + app->getBatchExecutor().setPreferredTaskCountForTesting(4); + auto dest = getGenesisAccount(*app, count); + // Fresh frames from the envelopes, with no hash computed yet. Every + // eighth candidate has a stale sequence number. + TxFrameList txs; + std::set expectedIncluded; + for (uint32_t i = 0; i < count; ++i) + { + auto source = getGenesisAccount(*app, i); + auto signedTx = transactionFromOperations( + *app, source.getSecretKey(), + source.getLastSequenceNumber() + (i % 8 == 0 ? 0 : 1), + {payment(dest.getPublicKey(), 1)}, 100 + i); + txs.push_back(TransactionFrameBase::makeTransactionFromWire( + app->getNetworkID(), signedTx->getEnvelope())); + if (i % 8 != 0) + { + expectedIncluded.insert(xdrSha256(signedTx->getEnvelope())); + } + } + + PerPhaseTransactionList phases{txs, {}}; + PerPhaseTransactionList invalid(2); + auto [txSet, applicable] = + makeTxSetFromTransactions(phases, *app, ApplyTimeOffset{}, invalid); + REQUIRE(invalid[0].size() == count / 8); + // The cached hashes are the envelopes' hashes. + for (auto const& tx : txs) + { + REQUIRE(tx->getFullHash() == xdrSha256(tx->getEnvelope())); + } + // The set holds the valid candidates, in full hash order. + auto const& classic = + txSet->getGeneralizedXDR().v1TxSet().phases[0].v0Components(); + REQUIRE(classic.size() == 1); + std::vector setHashes; + for (auto const& env : classic[0].txsMaybeDiscountedFee().txs) + { + setHashes.push_back(xdrSha256(env)); + } + REQUIRE(std::is_sorted(setHashes.begin(), setHashes.end())); + REQUIRE(std::set(setHashes.begin(), setHashes.end()) == + expectedIncluded); + REQUIRE(applicable->checkValid(*app, 0, 0)); +} + TEST_CASE("generalized tx set XDR validation", "[txset]") { Config cfg(getTestConfig()); From f19953942481c82621b2f23f1932c10e747126d0 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 15:58:29 -0800 Subject: [PATCH 32/39] Precompute the parallel tx-set builder's sort keys The parallel Soroban phase builder sorts all candidates by inclusion fee rate, and each comparison looked up both transactions' inclusion fee and operation count through virtual calls (~1,450 instructions per comparison on the main thread). Look them up once per transaction and compare the cached values with the same comparator logic: TxFeeComparator gets an overload that takes the fee bids and operation counts, and the plain overload forwards to it, so both order identically (same tie-break). Laptop benchmark (15,000 SAC candidates, 9,375 validated): build-select 17.1 -> 14.0 ms cold, 17.2 -> 13.6 ms warm. --- src/herder/ParallelTxSetBuilder.cpp | 15 +++++-- src/herder/SurgePricingUtils.cpp | 32 +++++++------- src/herder/SurgePricingUtils.h | 10 ++++- src/herder/test/TxSetTests.cpp | 65 +++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 22 deletions(-) diff --git a/src/herder/ParallelTxSetBuilder.cpp b/src/herder/ParallelTxSetBuilder.cpp index dacb0ebdf..bbb9d552e 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/SurgePricingUtils.cpp b/src/herder/SurgePricingUtils.cpp index fe9d0c17b..edec65d71 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 0c353dbf0..f6e7fb3b6 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/test/TxSetTests.cpp b/src/herder/test/TxSetTests.cpp index 781c905b7..ce9871a1b 100644 --- a/src/herder/test/TxSetTests.cpp +++ b/src/herder/test/TxSetTests.cpp @@ -5,6 +5,7 @@ #include "crypto/SHA.h" #include "crypto/SecretKey.h" #include "herder/ParallelTxSetBuilder.h" +#include "herder/SurgePricingUtils.h" #include "herder/TxSetFrame.h" #include "herder/TxSetUtils.h" #include "herder/test/TestTxSetUtils.h" @@ -28,6 +29,7 @@ #include #include #include +#include namespace stellar { namespace @@ -293,6 +295,69 @@ TEST_CASE("tx set validation caches candidate hashes", "[txset]") REQUIRE(applicable->checkValid(*app, 0, 0)); } +TEST_CASE("fee comparator with precomputed bids orders like the plain one", + "[txset]") +{ + VirtualClock clock; + auto app = createTestApplication(clock, getTestConfig()); + overrideSorobanNetworkConfigForTest(*app); + auto root = app->getRoot(); + // Classic transactions with 1-3 operations and Soroban transactions with + // varying resource fees (so their full fees don't order like their + // inclusion fees), over few fee rates, so that many compare equal. + TxFrameList txs; + for (int i = 0; i < 200; ++i) + { + uint32_t const rate = 100 * rand_uniform(1, 4); + if (i % 2 == 0) + { + uint32_t const ops = rand_uniform(1, 3); + std::vector payments(ops, + payment(root->getPublicKey(), 1)); + txs.push_back(transactionFromOperations( + *app, SecretKey::pseudoRandomForTesting(), 1, payments, + ops * rate)); + } + else + { + SorobanResources resources; + resources.instructions = 800'000; + resources.diskReadBytes = 1000; + resources.writeBytes = 1000; + auto const resourceFee = + sorobanResourceFee(*app, resources, 5000, 100) + + rand_uniform(0, 10'000); + txs.push_back(createUploadWasmTx(*app, *root, rate, resourceFee, + resources, std::nullopt, 0, + std::nullopt, i)); + } + } + for (bool isGreater : {true, false}) + { + TxFeeComparator comparator( + isGreater, + rand_uniform(0, std::numeric_limits::max())); + auto plain = txs; + std::sort(plain.begin(), plain.end(), comparator); + + std::vector> bids; + for (auto const& tx : txs) + { + bids.emplace_back(tx->getInclusionFee(), tx->getNumOperations()); + } + std::vector order(txs.size()); + std::iota(order.begin(), order.end(), 0); + std::sort(order.begin(), order.end(), [&](size_t a, size_t b) { + return comparator(txs[a], bids[a].first, bids[a].second, txs[b], + bids[b].first, bids[b].second); + }); + for (size_t i = 0; i < txs.size(); ++i) + { + REQUIRE(txs[order[i]] == plain[i]); + } + } +} + TEST_CASE("generalized tx set XDR validation", "[txset]") { Config cfg(getTestConfig()); From d03d56f2949bc7d5a419ae43b0bde5c675ce5d5a Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 16:04:23 -0800 Subject: [PATCH 33/39] Decode mempool pulls in place from the overlay IPC payload OverlayIPC::getTopTransactions copied every transaction's XDR into a temporary vector before decoding it. Decode well-formed entries straight from the response payload (OverlayIPC::parseTopTransactions); entries that are not 4-byte aligned, which a well-formed response never has, are still decoded from an aligned copy, and entries that fail to decode are still skipped. The leader build benchmark now decodes its replayed response with the same function. Laptop benchmark (15,000 SAC candidates): build-pull 19.4 -> 10.7 ms. --- src/herder/test/HerderTests.cpp | 31 ++++---------- src/overlay/OverlayIPC.cpp | 39 ++++++++++++----- src/overlay/OverlayIPC.h | 9 ++++ src/overlay/test/OverlayIPCTests.cpp | 62 ++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 33 deletions(-) diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 980929f8c..f2cd9ac13 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -35,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" @@ -8439,38 +8440,24 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") REQUIRE(envelopes.size() == candidates); app->getBatchExecutor().setPreferredTaskCountForTesting(tasks); - // The Rust overlay's GetTopTxs response, decoded exactly as + // BENCH_PULL: hand over only the first N candidates (a smaller pull). + uint32_t const count = static_cast(envelopes.size()); + uint32_t const pull = std::min(envInt("BENCH_PULL", count), count); + // The Rust overlay's GetTopTxs response for the pull, decoded as // OverlayIPC::getTopTransactions does: [count:4]([len:4][xdr])* std::vector payload(4); - uint32_t const count = static_cast(envelopes.size()); - std::memcpy(payload.data(), &count, 4); - for (auto const& env : envelopes) + std::memcpy(payload.data(), &pull, 4); + for (uint32_t i = 0; i < pull; ++i) { - auto bytes = xdr::xdr_to_opaque(env); + auto bytes = xdr::xdr_to_opaque(envelopes[i]); uint32_t const len = static_cast(bytes.size()); auto const at = payload.size(); payload.resize(at + 4 + len); std::memcpy(payload.data() + at, &len, 4); std::memcpy(payload.data() + at + 4, bytes.data(), len); } - // BENCH_PULL: hand over only the first N candidates (a smaller pull). - uint32_t const pull = std::min(envInt("BENCH_PULL", count), count); herder.mGetTopTransactionsForTesting = [&](size_t) { - std::vector result; - size_t offset = 4; - for (uint32_t i = 0; i < pull; ++i) - { - uint32_t len; - std::memcpy(&len, payload.data() + offset, 4); - offset += 4; - TransactionEnvelope tx; - std::vector txData(payload.begin() + offset, - payload.begin() + offset + len); - xdr::xdr_from_opaque(txData, tx); - result.push_back(std::move(tx)); - offset += len; - } - return result; + return OverlayIPC::parseTopTransactions(payload); }; auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); diff --git a/src/overlay/OverlayIPC.cpp b/src/overlay/OverlayIPC.cpp index 39aa870f1..51c702fa7 100644 --- a/src/overlay/OverlayIPC.cpp +++ b/src/overlay/OverlayIPC.cpp @@ -608,25 +608,31 @@ OverlayIPC::getTopTransactions(size_t count) return result; } - // Parse response: list of XDR-encoded TransactionEnvelopes + return parseTopTransactions(response.payload); +} + +std::vector +OverlayIPC::parseTopTransactions(std::vector const& payload) +{ + std::vector result; // Format: [count:4][len1:4][tx1:len1][len2:4][tx2:len2]... - if (response.payload.size() < 4) + if (payload.size() < 4) { return result; } uint32_t txCount; - std::memcpy(&txCount, response.payload.data(), 4); + std::memcpy(&txCount, payload.data(), 4); + result.reserve(std::min(txCount, payload.size() / 4)); size_t offset = 4; - for (uint32_t i = 0; i < txCount && offset + 4 <= response.payload.size(); - ++i) + for (uint32_t i = 0; i < txCount && offset + 4 <= payload.size(); ++i) { uint32_t txLen; - std::memcpy(&txLen, response.payload.data() + offset, 4); + std::memcpy(&txLen, payload.data() + offset, 4); offset += 4; - if (offset + txLen > response.payload.size()) + if (txLen > payload.size() - offset) { break; } @@ -634,10 +640,21 @@ OverlayIPC::getTopTransactions(size_t count) try { TransactionEnvelope tx; - std::vector txData(response.payload.begin() + offset, - response.payload.begin() + offset + - txLen); - xdr::xdr_from_opaque(txData, tx); + uint8_t const* const start = payload.data() + offset; + // XDR is read in 4-byte words from 4-byte aligned memory. Well + // formed entries are (XDR lengths are multiples of 4), so decode + // them in place; copy anything else to aligned memory first. + if ((reinterpret_cast(start) & 3) == 0) + { + xdr::xdr_get g(start, start + txLen); + xdr::xdr_argpack_archive(g, tx); + g.done(); + } + else + { + std::vector const txData(start, start + txLen); + xdr::xdr_from_opaque(txData, tx); + } result.push_back(std::move(tx)); } catch (std::exception const& e) diff --git a/src/overlay/OverlayIPC.h b/src/overlay/OverlayIPC.h index 1963b6541..685821ed9 100644 --- a/src/overlay/OverlayIPC.h +++ b/src/overlay/OverlayIPC.h @@ -124,6 +124,15 @@ class OverlayIPC */ std::vector getTopTransactions(size_t count); + /** + * Decode a GET_TOP_TXS response payload: + * [count:4]([len:4][TransactionEnvelope XDR:len])*. Envelopes are + * decoded in place from the payload. Entries that fail to decode are + * skipped (with a warning); a truncated payload ends the list. + */ + static std::vector + parseTopTransactions(std::vector const& payload); + /** * Submit a transaction to the overlay for flooding. * diff --git a/src/overlay/test/OverlayIPCTests.cpp b/src/overlay/test/OverlayIPCTests.cpp index 196e6e2b5..255756b35 100644 --- a/src/overlay/test/OverlayIPCTests.cpp +++ b/src/overlay/test/OverlayIPCTests.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -73,6 +74,67 @@ makeMockSCPEnvelope(uint64_t slotIndex, uint32_t nodeId) } // anonymous namespace +TEST_CASE("OverlayIPC decodes top transactions in place", "[overlay-ipc]") +{ + // Envelopes of different lengths (memo text is padded to 4 bytes). + std::vector envs; + for (uint32_t i = 0; i < 3; ++i) + { + TransactionEnvelope env(ENVELOPE_TYPE_TX); + env.v1().tx.fee = 100 + i; + env.v1().tx.seqNum = i + 1; + env.v1().tx.memo.type(MEMO_TEXT); + env.v1().tx.memo.text() = std::string(3 * i + 1, 'x'); + envs.push_back(env); + } + auto payloadOf = [](uint32_t count, + std::vector> const& entries) { + std::vector payload(4); + std::memcpy(payload.data(), &count, 4); + for (auto const& entry : entries) + { + uint32_t const len = static_cast(entry.size()); + auto const at = payload.size(); + payload.resize(at + 4); + std::memcpy(payload.data() + at, &len, 4); + payload.insert(payload.end(), entry.begin(), entry.end()); + } + return payload; + }; + std::vector> xdrs; + for (auto const& env : envs) + { + xdrs.push_back(xdr::xdr_to_opaque(env)); + } + + SECTION("well formed") + { + REQUIRE(OverlayIPC::parseTopTransactions(payloadOf(3, xdrs)) == envs); + } + SECTION("malformed entries are skipped") + { + // A 5-byte entry fails to decode and leaves the next ones unaligned, + // decoded from a copy, until a 3-byte one (which fails too) aligns + // them again. An aligned entry with an invalid envelope type fails + // to decode in place. + std::vector const badType(xdrs[0].size(), 0xff); + auto payload = payloadOf( + 6, + {xdrs[0], {1, 2, 3, 4, 5}, xdrs[1], {1, 2, 3}, badType, xdrs[2]}); + REQUIRE(OverlayIPC::parseTopTransactions(payload) == envs); + } + SECTION("truncated payload") + { + auto payload = payloadOf(3, xdrs); + payload.resize(payload.size() - 1); + REQUIRE(OverlayIPC::parseTopTransactions(payload) == + std::vector{envs[0], envs[1]}); + // More entries announced than present. + REQUIRE(OverlayIPC::parseTopTransactions(payloadOf(5, xdrs)) == envs); + REQUIRE(OverlayIPC::parseTopTransactions({1, 0}).empty()); + } +} + TEST_CASE("OverlayIPC connects to Rust overlay", "[overlay-ipc]") { auto overlayBinary = requireOverlayBinary(); From 547e29f55dd06665dc9212206bbe636271c7e00b Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 16:20:06 -0800 Subject: [PATCH 34/39] Give Core's tx-set fetch metrics their own names PendingEnvelopes timed its tx-set fetches (request to overlay delivery) into overlay.fetch.txset, which RustOverlayManager also feeds with the Rust overlay's own fetch times, synced as per-batch averages: one timer mixing two measurements, with about two samples per ledger. Name Core's timer scp.fetch.txset, next to scp.fetch.envelope from the same class, and its retry counter scp.fetch.txset-retry; overlay.fetch.txset is left to the overlay. docs/metrics.md lists both. --- docs/metrics.md | 4 +++- docs/rust-overlay/txset-fetching.md | 2 +- src/herder/PendingEnvelopes.cpp | 9 +++++---- src/herder/test/PendingEnvelopesTests.cpp | 14 ++++++++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/docs/metrics.md b/docs/metrics.md index b37a5c24b..4f9bb35de 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -130,7 +130,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 +181,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/txset-fetching.md b/docs/rust-overlay/txset-fetching.md index a4008ca17..1e4b89902 100644 --- a/docs/rust-overlay/txset-fetching.md +++ b/docs/rust-overlay/txset-fetching.md @@ -95,7 +95,7 @@ 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 `overlay.fetch.txset-retry`). +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 diff --git a/src/herder/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp index c8183a569..6c3a3b5af 100644 --- a/src/herder/PendingEnvelopes.cpp +++ b/src/herder/PendingEnvelopes.cpp @@ -45,10 +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({"overlay", "fetch", "txset-retry"})) + app.getMetrics().NewCounter({"scp", "fetch", "txset-retry"})) , mCostPerSlot(app.getMetrics().NewHistogram({"scp", "cost", "per-slot"})) { } @@ -332,8 +332,9 @@ PendingEnvelopes::completeTxSetFetch(Hash const& hash, auto fetchIt = mTxSetFetches.find(hash); if (fetchIt != mTxSetFetches.end()) { - // overlay.fetch.txset times overlay fetches only, not sets this node - // ended up building itself. + // 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() - diff --git a/src/herder/test/PendingEnvelopesTests.cpp b/src/herder/test/PendingEnvelopesTests.cpp index 08e925ab9..0eee4f71c 100644 --- a/src/herder/test/PendingEnvelopesTests.cpp +++ b/src/herder/test/PendingEnvelopesTests.cpp @@ -222,7 +222,7 @@ TEST_CASE_VERSIONS("PendingEnvelopes recvSCPEnvelope", "[herder]") REQUIRE(pendingEnvelopes.recvSCPQuorumSet(saneQSetHash, saneQSet)); REQUIRE(herder.getSCP().getLatestMessage(pk) == nullptr); auto& fetchTimer = - app->getMetrics().NewTimer({"overlay", "fetch", "txset"}); + app->getMetrics().NewTimer({"scp", "fetch", "txset"}); auto const fetches = fetchTimer.count(); // A promoted leader or a legacy-tx-set validator can build the @@ -233,7 +233,7 @@ TEST_CASE_VERSIONS("PendingEnvelopes recvSCPEnvelope", "[herder]") auto m = herder.getSCP().getLatestMessage(pk); REQUIRE(m); REQUIRE(*m == saneEnvelope); - // Not an overlay fetch: overlay.fetch.txset is not updated. + // Not an overlay fetch: scp.fetch.txset is not updated. REQUIRE(fetchTimer.count() == fetches); // A later delivery from the overlay is redundant. @@ -848,7 +848,7 @@ TEST_CASE("PendingEnvelopes re-requests tx sets the overlay does not deliver", pendingEnvelopes.addSCPQuorumSet(qSetHash, qSet); auto& retries = - app->getMetrics().NewCounter({"overlay", "fetch", "txset-retry"}); + app->getMetrics().NewCounter({"scp", "fetch", "txset-retry"}); auto const initialRetries = retries.count(); auto const interval = PendingEnvelopes::TX_SET_FETCH_RETRY_INTERVAL; // Advance virtual time by exactly `d` (crankFor can jump past it to the @@ -876,8 +876,13 @@ TEST_CASE("PendingEnvelopes re-requests tx sets the overlay does not deliver", SECTION("an undelivered set is requested again until it arrives") { auto& fetchTimer = - app->getMetrics().NewTimer({"overlay", "fetch", "txset"}); + app->getMetrics().NewTimer({"scp", "fetch", "txset"}); auto const fetches = fetchTimer.count(); + // The overlay's own fetch time (synced from Rust) is a separate + // metric that Core's fetch tracking doesn't feed. + auto& overlayFetchTimer = + app->getMetrics().NewTimer({"overlay", "fetch", "txset"}); + auto const overlayFetches = overlayFetchTimer.count(); // Requests repeat after 1 s, then back off: 2 s, 4 s, ... crankBy(interval); // t = 1.5 s REQUIRE(retries.count() == initialRetries + 1); @@ -893,6 +898,7 @@ TEST_CASE("PendingEnvelopes re-requests tx sets the overlay does not deliver", REQUIRE(pendingEnvelopes.recvTxSet(hash, p.second)); REQUIRE(!pendingEnvelopes.getTxSetWaitingTime(hash)); REQUIRE(fetchTimer.count() == fetches + 1); + REQUIRE(overlayFetchTimer.count() == overlayFetches); crankBy(10 * interval); REQUIRE(retries.count() == initialRetries + 3); } From 18f84acb0df1bc9a5c671320bf3e8f90bc87bb61 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Mon, 9 Nov 2026 16:49:23 -0800 Subject: [PATCH 35/39] Prewarm the next ledger's signatures from the ballot on every validator On perfnet (3k SAC TPS) followers still validated the leader's set with a mostly cold signature cache, and the leader built with a cold one. The follower prewarm verified each follower's own top 7,600 mempool txs at the trigger, but with equal fees the mempool orders by hash while selection breaks ties randomly, so the leader's 7,500 came from its top 15,200 and only about half were covered; the leader had no prewarm. When slot N starts balloting, every tracking validator now starts a prewarm for ledger N + 1 over exactly the candidates its build validates first: per phase, the first validTxTargets() (5/4 of the capacity) in mempool order, past the transactions of N's value, which stay in the mempool until N externalizes. The leader's set comes from the same window, so a finished prewarm covers its build and the followers' validation. It runs while N finishes balloting, externalizes and closes: externalizing N keeps it; the build or a received-set validation stops it, as before. The follower's trigger-time prewarm stays as a top-up. The main thread only does the overlay round trip (posted after the SCP step that started the ballot; OverlayIPC::getTopTransactionsPayload returns the response undecoded). Decoding, windowing and verification run in at most min(batch executor tasks, WORKER_THREADS - 2) background tasks, which also free what they allocated. Metrics, so a perfnet run shows coverage: herder.prewarm.{pull,duration} timers, herder.prewarm.{txs,verified,incomplete} meters, and signature cache lookups during a build and during received-set validation, herder.txset.{build,validate}-sig-cache-{hit,miss} (from the new non-resetting PubKeyUtils::getVerifySigCacheCounts). docs/metrics.md lists them, and the build phase timers. The leader build benchmark gets a prewarmed mode. Laptop, 15,000 SAC candidates, 4 tasks: build cold 169.2 / warm 84.1 / after a prewarm 83.0 ms; received-set validation 102.2 / 34.2 / 37.7 ms; both with no signature verified after the prewarm, which itself takes 110.8 ms in the background. --- docs/metrics.md | 17 ++ src/crypto/SecretKey.cpp | 13 ++ src/crypto/SecretKey.h | 2 + src/herder/HerderImpl.cpp | 345 ++++++++++++++++++++++++----- src/herder/HerderImpl.h | 47 +++- src/herder/HerderSCPDriver.cpp | 16 ++ src/herder/HerderUtils.cpp | 24 ++ src/herder/HerderUtils.h | 8 + src/herder/test/HerderTests.cpp | 272 ++++++++++++++++++++++- src/overlay/OverlayIPC.cpp | 10 +- src/overlay/OverlayIPC.h | 7 + src/overlay/RustOverlayManager.cpp | 10 + src/overlay/RustOverlayManager.h | 2 + 13 files changed, 705 insertions(+), 68 deletions(-) diff --git a/docs/metrics.md b/docs/metrics.md index 4f9bb35de..098823e7b 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 diff --git a/src/crypto/SecretKey.cpp b/src/crypto/SecretKey.cpp index ba36b1bb6..b73994c61 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 c30286e4d..08e55ce9d 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/HerderImpl.cpp b/src/herder/HerderImpl.cpp index 56b3684d6..d1abc8073 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -29,6 +29,7 @@ #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" @@ -58,6 +59,7 @@ #include #include #include +#include #include using namespace std; @@ -70,16 +72,18 @@ namespace // 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. -void +// 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; - auto verify = [&budget](Hash const& contentsHash, auto const& signatures, - std::vector const& keys) { + 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) @@ -93,7 +97,13 @@ verifyMasterKeySignatures(Hash const& networkID, TransactionEnvelope const& env) return; } --budget; - if (SignatureUtils::verify(sig, key, contentsHash).valid) + auto const res = SignatureUtils::verify(sig, key, contentsHash); + if (res.cacheResult == + PubKeyUtils::VerifySigCacheLookupResult::MISS) + { + ++verified; + } + if (res.valid) { break; } @@ -133,13 +143,96 @@ verifyMasterKeySignatures(Hash const& networkID, TransactionEnvelope const& env) {frame->getFeeSourceID()}); TransactionEnvelope inner(ENVELOPE_TYPE_TX); inner.v1() = env.feeBump().tx.innerTx.v1(); - verifyMasterKeySignatures(networkID, inner); + 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 @@ -580,8 +673,12 @@ HerderImpl::valueExternalized(uint64 slotIndex, StellarValue const& value, { ZoneScoped; int const DUMP_SCP_TIMEOUT_SECONDS = 20; - // The prewarm only serves validating this slot's values. - stopSignaturePrewarm(); + // 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); @@ -1891,32 +1988,17 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) txPhases.emplace_back(std::move(sorobanTxs)); } - // 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. Validate 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 validTxTargets{ - validationTarget(mLedgerManager.getLastMaxTxSetSizeOps())}; - if (supportsSoroban) - { - validTxTargets.push_back(validationTarget( - mLedgerManager.hasLastClosedSorobanNetworkConfig() - ? mLedgerManager.getLastClosedSorobanNetworkConfig() - .ledgerMaxTxCount() - : 0)); - } + // 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 #ifdef BUILD_TESTS @@ -1937,7 +2019,8 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) txSet->getGeneralizedXDR(), ledgerSeq); } }, - validTxTargets); + targets); + markSignatureCacheLookups(mApp, "build", sigCacheHits, sigCacheMisses); CLOG_INFO(Herder, "Proposed TX set has {} transactions", proposedSet->sizeTxTotal()); @@ -2104,17 +2187,18 @@ HerderImpl::triggerNextLedger(uint32_t 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. Verify their signatures - // meanwhile, as building our own set used to before leader-only - // construction, so that validation finds them in the cache. Skip it if - // this node already voted, i.e. validated the leader's value. + // 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) && !getSCP() .getNominationLeaders(ledgerSeqToTrigger) .count(getSCP().getLocalNodeID()) && getSCP().getLatestMessagesSend(ledgerSeqToTrigger).empty()) { - prewarmSignatureCache(); + prewarmSignatureCache(ledgerSeqToTrigger, {}); } } @@ -2143,43 +2227,173 @@ HerderImpl::pullTopTransactions(size_t count) 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::prewarmSignatureCache() +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 leader's set has at most this many transactions, drawn from the - // top of a mempool much like ours. - auto envelopes = std::make_shared const>( - pullTopTransactions(maxTxSetCandidates())); - if (envelopes->empty()) + // 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 stop = std::make_shared>(false); - mSignaturePrewarmStop = stop; - // The batch executor (validation) is idle until the leader's set - // arrives; use as many background tasks as it would, but leave worker - // threads for long background jobs such as bucket merges. + 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 of {} transactions in {} tasks", - envelopes->size(), tasks); + "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( - [envelopes, stop, networkID = mApp.getNetworkID(), task, tasks]() { + [prewarm, payload, windows, networkID = mApp.getNetworkID(), task, + tasks]() { ZoneScopedN("signature prewarm"); - for (size_t i = task; i < envelopes->size(); i += tasks) + if (!prewarm->stop.load(std::memory_order_relaxed)) { - if (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) { - return; + if (prewarm->stop.load(std::memory_order_relaxed)) + { + break; + } + prewarm->verifiedSignatures += + verifyMasterKeySignatures(networkID, candidates[i]); + ++prewarm->checkedTxs; } - verifyMasterKeySignatures(networkID, (*envelopes)[i]); + } + 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"); @@ -2189,10 +2403,29 @@ HerderImpl::prewarmSignatureCache() void HerderImpl::stopSignaturePrewarm() { - if (mSignaturePrewarmStop) + if (!mSignaturePrewarm) { - mSignaturePrewarmStop->store(true, std::memory_order_relaxed); - mSignaturePrewarmStop.reset(); + 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(); } } diff --git a/src/herder/HerderImpl.h b/src/herder/HerderImpl.h index 57f1eb0d4..fb29ba924 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 @@ -275,15 +278,47 @@ class HerderImpl : public Herder 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); - // Verify the signatures of the mempool's top transactions on background - // threads, warming the signature cache for validating the leader's set. - void prewarmSignatureCache(); - std::shared_ptr> mSignaturePrewarmStop; + // 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: - // Stops a running signature prewarm: called when validation or a local - // build is about to verify the signatures itself. + // 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: diff --git a/src/herder/HerderSCPDriver.cpp b/src/herder/HerderSCPDriver.cpp index 6fcc27624..84a2c5882 100644 --- a/src/herder/HerderSCPDriver.cpp +++ b/src/herder/HerderSCPDriver.cpp @@ -1448,6 +1448,17 @@ HerderSCPDriver::startedBallotProtocol(uint64_t slotIndex, SCPBallot const& ballot) { recordSCPEvent(slotIndex, false); + // 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, @@ -1962,6 +1973,9 @@ HerderSCPDriver::checkAndCacheTxSetValid(TxSetXDRFrame const& txSet, // 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 @@ -1988,6 +2002,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; diff --git a/src/herder/HerderUtils.cpp b/src/herder/HerderUtils.cpp index 62322fe36..47d73de61 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 4274e5a78..465acffe3 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/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index f2cd9ac13..0fc00e6b4 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -6788,6 +6788,43 @@ class EarlyNominationTestAccess return {prepared.txSet, prepared.invalidTxHashes}; } + static size_t + maxTxSetCandidates(HerderImpl& herder) + { + return herder.maxTxSetCandidates(); + } + + // Starts a signature prewarm for `ledgerSeq`'s candidates. + static void + prewarm(HerderImpl& herder, uint32_t ledgerSeq) + { + herder.prewarmSignatureCache(ledgerSeq, {}); + } + + // The ledger the running signature prewarm is for (0 if none). + static uint32_t + prewarmLedger(HerderImpl& herder) + { + return herder.mSignaturePrewarm ? herder.mSignaturePrewarm->ledgerSeq + : 0; + } + + // Waits in real time, without cranking, until the running prewarm's + // tasks are done. Returns false if there is none or it takes too long. + static bool + waitForPrewarm(HerderImpl& herder) + { + auto prewarm = herder.mSignaturePrewarm; + auto const deadline = + std::chrono::steady_clock::now() + std::chrono::seconds(60); + while (prewarm && prewarm->finishedAfterNs.load() < 0 && + std::chrono::steady_clock::now() < deadline) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + return prewarm && prewarm->finishedAfterNs.load() >= 0; + } + // These fixtures inspect a local proposal while requiring an absent peer // to prevent externalization. Pick that peer so the local node is the // first leader, independently of the test RNG seed and previous ledger. @@ -7820,6 +7857,161 @@ TEST_CASE("followers pre-verify mempool signatures at the trigger", REQUIRE(checkedRoles == std::set{false, true}); } +TEST_CASE("signature prewarm at the ballot covers the next ledger", + "[herder][prewarm]") +{ + // When slot N starts balloting, a validator pre-verifies the signatures + // of ledger N + 1's candidates: the top of its mempool past the + // transactions of N's value, which stay there until N externalizes. The + // next build validates 20 (5/4 of the classic capacity of 16). + uint32_t const valueTxs = 16; + uint32_t const nextTxs = 32; + uint32_t const target = 20; + auto cfg = getTestConfig(); + cfg.MANUAL_CLOSE = true; + cfg.WORKER_THREADS = 4; + cfg.GENESIS_TEST_ACCOUNT_COUNT = valueTxs + nextTxs + 1; + cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = 16; + VirtualClock clock; + auto app = createTestApplication(clock, cfg); + auto& herder = static_cast(app->getHerder()); + auto& lm = app->getLedgerManager(); + auto& metrics = app->getMetrics(); + REQUIRE(lm.getLastMaxTxSetSizeOps() == 16); + REQUIRE(herder.isTracking()); + + auto dest = getGenesisAccount(*app, valueTxs + nextTxs); + TxFrameList valueTxFrames; + TxFrameList nextTxFrames; + std::vector mempool; + for (uint32_t i = 0; i < valueTxs + nextTxs; ++i) + { + auto tx = getGenesisAccount(*app, i).tx({payment(dest, 1)}); + (i < valueTxs ? valueTxFrames : nextTxFrames).push_back(tx); + mempool.push_back(tx->getEnvelope()); + } + size_t requested = 0; + herder.mGetTopTransactionsForTesting = [&](size_t count) { + requested = count; + return std::vector( + mempool.begin(), mempool.begin() + std::min(count, mempool.size())); + }; + + // Slot N's value holds the top 16. + auto const lcl = lm.getLastClosedLedgerHeader(); + auto const slot = lcl.header.ledgerSeq + 1; + auto const closeTime = makeConsensusTime(app->timeNow() + 1); + auto valueSet = + makeTxSetFromTransactions(PerPhaseTransactionList{valueTxFrames, {}}, + *app, ApplyTimeOffset{}) + .first; + REQUIRE(valueSet->sizeTxTotal() == valueTxs); + herder.getPendingEnvelopes().putTxSet(valueSet->getContentsHash(), slot, + valueSet); + auto const value = xdr::xdr_to_opaque( + herder.makeStellarValue(valueSet->getContentsHash(), closeTime, + emptyUpgradeSteps, cfg.NODE_SEED)); + + PubKeyUtils::clearVerifySigCache(); + auto& prewarmTxs = + metrics.NewMeter({"herder", "prewarm", "txs"}, "transaction"); + auto& prewarmVerified = + metrics.NewMeter({"herder", "prewarm", "verified"}, "signature"); + auto& prewarmIncomplete = + metrics.NewMeter({"herder", "prewarm", "incomplete"}, "prewarm"); + auto const txs0 = prewarmTxs.count(); + auto const verified0 = prewarmVerified.count(); + auto const incomplete0 = prewarmIncomplete.count(); + + // The prewarm starts once SCP is done with the step that started the + // ballot, pulling past N's value, and verifies in the background. + SCPBallot ballot; + ballot.counter = 1; + ballot.value = value; + herder.getHerderSCPDriver().startedBallotProtocol(slot, ballot); + REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == 0); + testutil::crankUntil( + app, + [&]() { + return EarlyNominationTestAccess::prewarmLedger(herder) == slot + 1; + }, + std::chrono::seconds(1)); + REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == slot + 1); + REQUIRE(requested == + 2 * EarlyNominationTestAccess::maxTxSetCandidates(herder) + + valueTxs); + REQUIRE(EarlyNominationTestAccess::waitForPrewarm(herder)); + + // N externalizes and closes; the prewarm for N + 1 is kept. The overlay + // drops N's transactions from the mempool. + herder.externalizeValue(valueSet, slot, closeTime, emptyUpgradeSteps, + cfg.NODE_SEED); + REQUIRE(lm.getLastClosedLedgerNum() == slot); + REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == slot + 1); + mempool.erase(mempool.begin(), mempool.begin() + valueTxs); + auto const nextLcl = lm.getLastClosedLedgerHeader(); + auto const nextCloseTime = makeConsensusTime(app->timeNow() + 2); + + // No signature verified, and at least one lookup per transaction (a + // payment's operation checks its source's signature again). + auto checkLookups = [&](std::string const& prefix, uint64_t hits0, + uint64_t misses0, uint64_t txs) { + auto& hits = metrics.NewMeter( + {"herder", "txset", prefix + "-sig-cache-hit"}, "lookup"); + auto& misses = metrics.NewMeter( + {"herder", "txset", prefix + "-sig-cache-miss"}, "lookup"); + REQUIRE(misses.count() == misses0); + REQUIRE(hits.count() >= hits0 + txs); + }; + auto lookups = [&](std::string const& prefix) { + return std::make_pair( + metrics + .NewMeter({"herder", "txset", prefix + "-sig-cache-hit"}, + "lookup") + .count(), + metrics + .NewMeter({"herder", "txset", prefix + "-sig-cache-miss"}, + "lookup") + .count()); + }; + + SECTION("the next leader's build finds every signature cached") + { + auto [hits0, misses0] = lookups("build"); + auto txSet = + EarlyNominationTestAccess::build(herder, slot + 1, nextCloseTime); + REQUIRE(txSet->sizeTxTotal() == 16); + checkLookups("build", hits0, misses0, target); + } + SECTION("a follower's validation of the leader's set finds them cached") + { + // The leader's set, built elsewhere (without verifying anything + // here). + TxFrameList leaderTxs(nextTxFrames.begin(), nextTxFrames.begin() + 16); + auto leaderSet = testtxset::makeNonValidatedGeneralizedTxSet( + {{std::make_pair(std::nullopt, leaderTxs)}, {}}, + *app, nextLcl.hash) + .first; + herder.getPendingEnvelopes().putTxSet(leaderSet->getContentsHash(), + slot + 1, leaderSet); + auto leaderValue = xdr::xdr_to_opaque( + herder.makeStellarValue(leaderSet->getContentsHash(), nextCloseTime, + emptyUpgradeSteps, cfg.NODE_SEED)); + auto [hits0, misses0] = lookups("validate"); + REQUIRE(herder.getHerderSCPDriver().validateValue(slot + 1, leaderValue, + false) == + SCPDriver::kFullyValidatedValue); + checkLookups("validate", hits0, misses0, 16); + } + // Validation and the build stop the prewarm and record what it did: the + // 16 transactions of N's value and the next 20, each verified once. + REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == 0); + REQUIRE(prewarmTxs.count() == txs0 + valueTxs + target); + REQUIRE(prewarmVerified.count() == verified0 + valueTxs + target); + REQUIRE(prewarmIncomplete.count() == incomplete0); + herder.mGetTopTransactionsForTesting = nullptr; +} + TEST_CASE("early underfilled proposals refresh at the trigger", "[herder][early-nomination]") { @@ -8428,6 +8620,8 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") cfg.APPLY_LOAD_CLASSIC_TXS_PER_LEDGER = 0; cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = 100; cfg.GENESIS_TEST_ACCOUNT_COUNT = candidates + 10; + // The signature prewarm leaves 2 worker threads for other jobs. + cfg.WORKER_THREADS = static_cast(tasks) + 2; VirtualClock clock(VirtualClock::REAL_TIME); auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); @@ -8526,17 +8720,19 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") .count()); } REQUIRE(included == std::min(sorobanCap, pull)); - if (cold) { - // Calibration against perfnet's herder.txset.validate: a - // follower validating the received set with a cold cache. + // A follower validating the received set (cold: calibration + // against perfnet's herder.txset.validate). auto const& lastSet = EarlyNominationTestAccess::build(herder, seq, closeTime); for (uint32_t rep = 0; rep < reps; ++rep) { auto received = TxSetXDRFrame::makeFromWire( GeneralizedTransactionSet(lastSet->getGeneralizedXDR())); - PubKeyUtils::clearVerifySigCache(); + if (cold) + { + PubKeyUtils::clearVerifySigCache(); + } auto const start = std::chrono::steady_clock::now(); auto applicable = received->prepareForApply(*app, lcl.header); REQUIRE(applicable); @@ -8558,5 +8754,73 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") << ", n=" << v.size() << ")" << std::endl; } } + + // A signature prewarm (as from the previous slot's ballot) that finished + // before the leader's build, and before a follower validates its set. + { + std::map> samples; + auto& prewarmDuration = + metrics.NewTimer({"herder", "prewarm", "duration"}); + auto& buildMisses = metrics.NewMeter( + {"herder", "txset", "build-sig-cache-miss"}, "lookup"); + auto const lastSet = + EarlyNominationTestAccess::build(herder, seq, closeTime); + uint64_t misses = 0; + uint64_t validateMisses = 0; + for (uint32_t rep = 0; rep < reps; ++rep) + { + PubKeyUtils::clearVerifySigCache(); + auto const durationBefore = prewarmDuration.sum(); + EarlyNominationTestAccess::prewarm(herder, seq); + REQUIRE(EarlyNominationTestAccess::waitForPrewarm(herder)); + for (auto const& phase : phases) + { + metrics.NewTimer({"herder", "txset", phase}).Clear(); + } + auto const missesBefore = buildMisses.count(); + // The build stops the (finished) prewarm and records it. + auto txSet = + EarlyNominationTestAccess::build(herder, seq, closeTime); + REQUIRE(txSet->sizeTxTotal() == std::min(sorobanCap, pull)); + misses = buildMisses.count() - missesBefore; + samples["prewarm (background, before the build)"].push_back( + prewarmDuration.sum() - durationBefore); + for (auto const& phase : phases) + { + samples[phase].push_back( + metrics.NewTimer({"herder", "txset", phase}).sum()); + } + + auto received = TxSetXDRFrame::makeFromWire( + GeneralizedTransactionSet(lastSet->getGeneralizedXDR())); + PubKeyUtils::clearVerifySigCache(); + EarlyNominationTestAccess::prewarm(herder, seq); + REQUIRE(EarlyNominationTestAccess::waitForPrewarm(herder)); + uint64_t hits0 = 0, misses0 = 0, hits1 = 0, misses1 = 0; + PubKeyUtils::getVerifySigCacheCounts(hits0, misses0); + auto const start = std::chrono::steady_clock::now(); + auto applicable = received->prepareForApply(*app, lcl.header); + REQUIRE(applicable); + REQUIRE(applicable->checkValid(*app, 0, 0)); + samples["received-set-validate"].push_back( + std::chrono::duration( + std::chrono::steady_clock::now() - start) + .count()); + PubKeyUtils::getVerifySigCacheCounts(hits1, misses1); + validateMisses = misses1 - misses0; + herder.stopSignaturePrewarm(); + } + std::cout << "prewarmed signature cache (last build: " << misses + << " signature cache misses; last received-set " + "validation: " + << validateMisses << ")" << std::endl; + for (auto& [phase, v] : samples) + { + std::sort(v.begin(), v.end()); + std::cout << " " << phase << ": median " << v[v.size() / 2] + << " ms (min " << v.front() << ", max " << v.back() + << ", n=" << v.size() << ")" << std::endl; + } + } herder.mGetTopTransactionsForTesting = nullptr; } diff --git a/src/overlay/OverlayIPC.cpp b/src/overlay/OverlayIPC.cpp index 51c702fa7..284dd3db4 100644 --- a/src/overlay/OverlayIPC.cpp +++ b/src/overlay/OverlayIPC.cpp @@ -555,7 +555,13 @@ OverlayIPC::removeTransactions(std::vector const& txHashes) std::vector OverlayIPC::getTopTransactions(size_t count) { - std::vector result; + return parseTopTransactions(getTopTransactionsPayload(count)); +} + +std::vector +OverlayIPC::getTopTransactionsPayload(size_t count) +{ + std::vector result; if (!mChannel || !mChannel->isConnected()) { @@ -608,7 +614,7 @@ OverlayIPC::getTopTransactions(size_t count) return result; } - return parseTopTransactions(response.payload); + return std::move(response.payload); } std::vector diff --git a/src/overlay/OverlayIPC.h b/src/overlay/OverlayIPC.h index 685821ed9..4fd231ba2 100644 --- a/src/overlay/OverlayIPC.h +++ b/src/overlay/OverlayIPC.h @@ -124,6 +124,13 @@ class OverlayIPC */ std::vector getTopTransactions(size_t count); + /** + * The same request, returning the response payload undecoded (empty if + * there is no response), for callers that decode it elsewhere with + * parseTopTransactions. + */ + std::vector getTopTransactionsPayload(size_t count); + /** * Decode a GET_TOP_TXS response payload: * [count:4]([len:4][TransactionEnvelope XDR:len])*. Envelopes are diff --git a/src/overlay/RustOverlayManager.cpp b/src/overlay/RustOverlayManager.cpp index 95cc41818..9326e4e08 100644 --- a/src/overlay/RustOverlayManager.cpp +++ b/src/overlay/RustOverlayManager.cpp @@ -238,6 +238,16 @@ RustOverlayManager::getTopTransactions(size_t count) return {}; } +std::vector +RustOverlayManager::getTopTransactionsPayload(size_t count) +{ + if (mOverlayIPC && !mShuttingDown) + { + return mOverlayIPC->getTopTransactionsPayload(count); + } + return {}; +} + OverlayMetrics& RustOverlayManager::getOverlayMetrics() { diff --git a/src/overlay/RustOverlayManager.h b/src/overlay/RustOverlayManager.h index fc6d95ede..31506106e 100644 --- a/src/overlay/RustOverlayManager.h +++ b/src/overlay/RustOverlayManager.h @@ -77,6 +77,8 @@ class RustOverlayManager // Get top transactions from Rust overlay's mempool for TX set building. // Blocks until the overlay responds, shuts down, or disconnects. std::vector getTopTransactions(size_t count); + // The same, undecoded (see OverlayIPC::getTopTransactionsPayload). + std::vector getTopTransactionsPayload(size_t count); // Metrics and managers OverlayMetrics& getOverlayMetrics(); From ab58eae61348b546c2e842b363974230138aa359 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Tue, 10 Nov 2026 04:06:50 +0000 Subject: [PATCH 36/39] Raise the Core-overlay IPC payload limit to 64 MiB and never exceed it Both ends of the IPC rejected payloads over 16 MiB, and Core's reader treats a rejected length as a corrupt stream: it stops reading and never recovers, so the node can no longer send or receive SCP messages. The ballot-start signature prewarm pulls 2 x the tx-set capacity plus the balloting value's transactions: 22,700 SAC payments of ~752 bytes, about 17.2 MB, once the mempool holds that many. On perfnet (3k SAC TPS, 30 validators) every validator starts that pull at the same ballot, so 80 s into the load 16 of the 30 nodes lost their overlay within 0.35 s, the rest lost quorum, and the network stalled at one ledger. - Raise the limit to 64 MiB on both sides, named once per side (ipc::MAX_PAYLOAD_BYTES in IPC.h, MAX_PAYLOAD_SIZE in messages.rs) with a note that they must match. - The overlay sends at most the longest prefix of the top transactions that fits in the limit (the highest-priority ones), with the count matching, and warns when it truncates, so a large mempool can't produce an oversized response whatever the transaction sizes. - Neither side sends a payload over the limit: CoreSender refuses it (IpcError::PayloadTooLarge), and Core's sendMessage refuses it before writing anything, keeping the connection. - Core logs an error with the message type and length when it rejects a payload, instead of only "connection closed". - Tests: a [overlay-ipc] test with the real Rust overlay where a 20 MiB top transactions response arrives whole (it fails with the old limit: the pull returns nothing) and an 80 MiB pull is truncated to the 63 highest-fee transactions while the IPC keeps working; IPC tests for a 20 MB message, an over-limit length (rejected, with a receive timeout so a regression fails instead of hanging) and an over-limit send (nothing written); Rust tests for the truncation and the send guard. - docs/rust-overlay/ipc.md: the 64 MiB limit and how each side enforces it. --- docs/rust-overlay/ipc.md | 10 ++- overlay/src/ipc/messages.rs | 11 ++- overlay/src/ipc/transport.rs | 109 +++++++++++++++++++++++---- src/overlay/IPC.cpp | 27 ++++++- src/overlay/IPC.h | 8 ++ src/overlay/test/IPCTests.cpp | 83 ++++++++++++++++++++ src/overlay/test/OverlayIPCTests.cpp | 89 ++++++++++++++++++++++ 7 files changed, 316 insertions(+), 21 deletions(-) diff --git a/docs/rust-overlay/ipc.md b/docs/rust-overlay/ipc.md index dfac36fa3..a96778308 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/overlay/src/ipc/messages.rs b/overlay/src/ipc/messages.rs index 131318682..9930f9613 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 7c96aa613..c62e013b9 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/src/overlay/IPC.cpp b/src/overlay/IPC.cpp index 32827ea2a..07f568162 100644 --- a/src/overlay/IPC.cpp +++ b/src/overlay/IPC.cpp @@ -3,6 +3,7 @@ // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 #include "overlay/IPC.h" +#include "util/Logging.h" #include #include #include @@ -83,6 +84,17 @@ sendMessage(int socket, IPCMessage const& msg) { IgnoreSIGPIPE guard; + // The receiver would reject a larger payload and stop reading the stream. + if (msg.payload.size() > MAX_PAYLOAD_BYTES) + { + CLOG_ERROR(Overlay, + "Not sending IPC message type {}: its {} byte payload is " + "over the {} byte limit", + static_cast(msg.type), msg.payload.size(), + MAX_PAYLOAD_BYTES); + return false; + } + // Message format: [type:4 bytes][length:4 bytes][payload] uint32_t type = static_cast(msg.type); uint32_t payloadLen = static_cast(msg.payload.size()); @@ -124,9 +136,14 @@ receiveMessage(int socket) std::memcpy(&type, &header[0], 4); std::memcpy(&payloadLen, &header[4], 4); - // Sanity check payload length (16MB max) - if (payloadLen > 16 * 1024 * 1024) + // Sanity check payload length + if (payloadLen > MAX_PAYLOAD_BYTES) { + CLOG_ERROR(Overlay, + "IPC message type {} has a {} byte payload, over the {} " + "byte limit; treating the stream as corrupt and stopping " + "reading it", + type, payloadLen, MAX_PAYLOAD_BYTES); return std::nullopt; } @@ -201,6 +218,12 @@ IPCChannel::send(IPCMessage const& msg) { return false; } + // Refused before anything is written (sendMessage logs it); the + // connection stays usable. + if (msg.payload.size() > ipc::MAX_PAYLOAD_BYTES) + { + return ipc::sendMessage(mSocket, msg); + } bool result = ipc::sendMessage(mSocket, msg); if (!result) diff --git a/src/overlay/IPC.h b/src/overlay/IPC.h index a8a2c13df..cfe81b300 100644 --- a/src/overlay/IPC.h +++ b/src/overlay/IPC.h @@ -114,6 +114,14 @@ struct IPCMessage namespace ipc { +// Largest payload either side sends or accepts in one message; a reader +// treats a larger length as a corrupt stream and stops reading it. Must equal +// MAX_PAYLOAD_SIZE in overlay/src/ipc/messages.rs. A mempool pull (up to +// 2 x the tx-set capacity plus a pending set's transactions, e.g. 22,700 SAC +// payments of ~750 bytes) must fit, and the overlay truncates its top +// transactions response to this size. +constexpr uint32_t MAX_PAYLOAD_BYTES = 64 * 1024 * 1024; + /** * Send an IPC message over a socket. * diff --git a/src/overlay/test/IPCTests.cpp b/src/overlay/test/IPCTests.cpp index dbf72da7c..fde1d04a8 100644 --- a/src/overlay/test/IPCTests.cpp +++ b/src/overlay/test/IPCTests.cpp @@ -7,6 +7,7 @@ #include "util/TmpDir.h" #include #include +#include #include #include #include @@ -191,6 +192,88 @@ TEST_CASE("IPC large message", "[overlay][ipc]") close(sockets[0]); close(sockets[1]); } + + SECTION("send and receive a 20MB top transactions response") + { + // Over the old 16 MiB limit: a signature prewarm's mempool pull + // (2 x 7,600 + 7,500 SAC payments of ~752 bytes) is about 17.2 MB. + int sockets[2]; + REQUIRE(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0); + + IPCMessage msg; + msg.type = IPCMessageType::TOP_TXS_RESPONSE; + msg.payload.resize(20 * 1024 * 1024); + for (size_t i = 0; i < msg.payload.size(); ++i) + { + msg.payload[i] = static_cast(i % 251); + } + + std::optional recvMsg; + std::thread receiver( + [&]() { recvMsg = ipc::receiveMessage(sockets[1]); }); + bool sent = ipc::sendMessage(sockets[0], msg); + receiver.join(); + + REQUIRE(sent); + REQUIRE(recvMsg.has_value()); + REQUIRE(recvMsg->type == IPCMessageType::TOP_TXS_RESPONSE); + REQUIRE(recvMsg->payload == msg.payload); + + close(sockets[0]); + close(sockets[1]); + } + + SECTION("reject a payload length over the limit") + { + int sockets[2]; + REQUIRE(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0); + + // Only the header: the length alone must be rejected, without + // waiting for (or allocating) the payload. + uint32_t type = static_cast(IPCMessageType::TOP_TXS_RESPONSE); + uint32_t len = ipc::MAX_PAYLOAD_BYTES + 1; + uint8_t header[8]; + std::memcpy(&header[0], &type, 4); + std::memcpy(&header[4], &len, 4); + REQUIRE(write(sockets[0], header, sizeof(header)) == + static_cast(sizeof(header))); + + // If the length were accepted, the read of the missing payload would + // block: time out so that fails instead of hanging. + struct timeval timeout{}; + timeout.tv_sec = 5; + REQUIRE(setsockopt(sockets[1], SOL_SOCKET, SO_RCVTIMEO, &timeout, + sizeof(timeout)) == 0); + + REQUIRE_FALSE(ipc::receiveMessage(sockets[1]).has_value()); + + close(sockets[0]); + close(sockets[1]); + } + + SECTION("refuse to send a payload over the limit") + { + int sockets[2]; + REQUIRE(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0); + + IPCMessage msg; + msg.type = IPCMessageType::CACHE_TX_SET; + msg.payload.resize(ipc::MAX_PAYLOAD_BYTES + 1); + REQUIRE_FALSE(ipc::sendMessage(sockets[0], msg)); + + // Nothing was written: the stream still carries the next message. + IPCMessage small; + small.type = IPCMessageType::LEDGER_CLOSED; + small.payload = {1, 2, 3}; + REQUIRE(ipc::sendMessage(sockets[0], small)); + auto received = ipc::receiveMessage(sockets[1]); + REQUIRE(received.has_value()); + REQUIRE(received->type == IPCMessageType::LEDGER_CLOSED); + REQUIRE(received->payload == small.payload); + + close(sockets[0]); + close(sockets[1]); + } } TEST_CASE("IPCChannel", "[overlay][ipc]") diff --git a/src/overlay/test/OverlayIPCTests.cpp b/src/overlay/test/OverlayIPCTests.cpp index 255756b35..540e2c7c7 100644 --- a/src/overlay/test/OverlayIPCTests.cpp +++ b/src/overlay/test/OverlayIPCTests.cpp @@ -516,6 +516,95 @@ makeTxEnvelope(int64_t fee, int64_t seqNum, uint8_t accountByte, return txEnv; } +/** + * A transaction envelope of roughly `wasmBytes` bytes: one + * uploadContractWasm operation carrying a filler "wasm". The overlay takes + * Core's submissions without decoding them, so this only needs to be valid + * XDR. + */ +static TransactionEnvelope +makeLargeTxEnvelope(int64_t fee, int64_t seqNum, uint8_t accountByte, + size_t wasmBytes) +{ + TransactionEnvelope txEnv; + txEnv.type(ENVELOPE_TYPE_TX); + auto& tx = txEnv.v1().tx; + tx.sourceAccount.type(KEY_TYPE_ED25519); + std::fill(tx.sourceAccount.ed25519().begin(), + tx.sourceAccount.ed25519().end(), accountByte); + tx.fee = static_cast(fee); + tx.seqNum = seqNum; + tx.cond.type(PRECOND_NONE); + tx.operations.resize(1); + tx.operations[0].body.type(INVOKE_HOST_FUNCTION); + auto& hostFn = tx.operations[0].body.invokeHostFunctionOp().hostFunction; + hostFn.type(HOST_FUNCTION_TYPE_UPLOAD_CONTRACT_WASM); + hostFn.wasm().resize(wasmBytes); + std::fill(hostFn.wasm().begin(), hostFn.wasm().end(), accountByte); + return txEnv; +} + +/** + * Top transactions responses larger than the old 16 MiB IPC limit reach Core + * (a signature prewarm's pull on perfnet was about 17.2 MB and ended Core's + * IPC reader), and a pull larger than the limit is truncated to the + * highest-priority transactions that fit instead of breaking the connection. + */ +TEST_CASE("Rust overlay returns top transactions over 16 MiB", + "[overlay-ipc]") +{ + std::string overlayBinary = requireOverlayBinary(); + TmpDir tmpDir("overlay_ipc_large_top_txs_test"); + std::string socketPath = tmpDir.getName() + "/overlay.sock"; + uint16_t peerPort = getTestConfig(7).PEER_PORT; + + auto ipc = + std::make_unique(socketPath, overlayBinary, peerPort); + REQUIRE(ipc->start()); + + size_t const txBytes = 1024 * 1024; + auto submit = [&](size_t count, uint8_t firstAccount) { + for (size_t i = 0; i < count; ++i) + { + int64_t fee = 1000 + 100 * static_cast(i); + auto account = static_cast(firstAccount + i); + ipc->submitTransaction( + makeLargeTxEnvelope(fee, 1, account, txBytes), fee, 1); + } + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + }; + + SECTION("a 20 MiB response arrives whole") + { + submit(20, 0); + auto txs = ipc->getTopTransactions(20); + REQUIRE(txs.size() == 20); + for (auto const& tx : txs) + { + REQUIRE(tx.v1().tx.operations[0] + .body.invokeHostFunctionOp() + .hostFunction.wasm() + .size() == txBytes); + } + // Highest fee first. + REQUIRE(txs.front().v1().tx.fee == 1000 + 100 * 19); + } + + SECTION("a response over the limit is truncated and the IPC survives") + { + submit(80, 0); + auto txs = ipc->getTopTransactions(80); + // 64 MiB holds 63 of these (each about 1 MiB plus framing). + REQUIRE(txs.size() > 16); + REQUIRE(txs.size() < 80); + REQUIRE(txs.front().v1().tx.fee == 1000 + 100 * 79); + // The connection still works. + REQUIRE(ipc->getTopTransactions(3).size() == 3); + } + + ipc->shutdown(); +} + /** * Test TX inclusion in TX set. * From 683b3f322a3277ef422922671efe804b2cce7385 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Tue, 10 Nov 2026 10:41:05 -0800 Subject: [PATCH 37/39] Replace SCP nomination with a deterministic leader schedule (prototype) Experimental. Not protocol-gated and not compatible with nominating nodes. Nomination is removed. For slot N every node orders the transitive quorum by SHA256(hash of ledger header N-1 || node id); the leader of round r (a round is a ballot counter, starting at 1) is entry (r - 1) mod n. The round leader builds a value and proposes it straight into the ballot protocol as PREPARE at counter r. Other nodes vote for the leader's ballot at that counter if it passes proposal validation (the former nomination-time checks, e.g. only upgrades the node armed). A leader timer (while the node has no ballot for its round) or the ballot timer moves a node to the next round. Accept, confirm and commit are unchanged, and every new vote still goes through bumpState (value override, updateCurrentValue). Details: - Followers vote for the round 1 leader as soon as they see its ballot, without waiting for their own trigger. - A node that has not voted yet votes for an earlier round's leader ballot at that ballot's counter when it arrives late, and keeps waiting for its current round's leader. - The leader timer gets one extension when the Herder already holds the leader's statement (e.g. it is fetching its transaction set). - A leader's empty-tx-set ballot is followed under ballot validation. - No leader and no leader timer at the UINT32_MAX counter. - State restore sets the value override and the round. - Herder: the trigger starts the slot; the early tx-set build and the follower signature prewarm key off the round leader; NOMINATE envelopes are discarded. New metrics scp.timeout.leader and scp.leader.round; scp.timing.nominated, scp.timeout.nominate and scp.nomination.combinecandidates are gone. Removed: NominationProtocol, the nomination API of SCP/Slot/SCPDriver, combineCandidates, node weights, upgrade stripping, the nomination emit delay, and the nomination-only tests (SCPUnitTests.cpp, nomination cases of SCPTests.cpp, weight / win-rate / timeout statistics in HerderTests.cpp). FORCE_OLD_STYLE_LEADER_ELECTION, VALIDATOR_WEIGHT_CONFIG and the nomination timeout settings still parse but are unused. Tests: leader round unit cases and a randomized multi-node model in SCPTests.cpp, LeaderScheduleTests.cpp, LeaderRoundsTests.cpp (network simulations, including failover with a validator down); Herder, quorum tracker, PendingEnvelopes and overlay IPC tests ported to leader rounds. Known limitations: symmetric, static quorums are assumed (the schedule's node set is each node's view of the transitive quorum); uniform leader weights; the header hash can be ground by a leader; no upgrade stripping or merging; a leader re-proposes a value that is only structurally valid. --- src/herder/Herder.h | 4 +- src/herder/HerderImpl.cpp | 72 +- src/herder/HerderImpl.h | 10 +- src/herder/HerderSCPDriver.cpp | 664 ++---- src/herder/HerderSCPDriver.h | 120 +- src/herder/LeaderSchedule.cpp | 38 + src/herder/LeaderSchedule.h | 26 + src/herder/PendingEnvelopes.cpp | 27 + src/herder/PendingEnvelopes.h | 12 + src/herder/test/HerderTests.cpp | 2009 +++--------------- src/herder/test/LeaderRoundsTests.cpp | 221 ++ src/herder/test/LeaderScheduleTests.cpp | 123 ++ src/herder/test/PendingEnvelopesTests.cpp | 62 +- src/herder/test/QuorumTrackerTests.cpp | 36 +- src/main/Config.cpp | 14 +- src/main/Config.h | 6 +- src/overlay/test/OverlayIPCTests.cpp | 50 +- src/scp/BallotProtocol.cpp | 378 +++- src/scp/BallotProtocol.h | 56 +- src/scp/NominationProtocol.cpp | 926 --------- src/scp/NominationProtocol.h | 152 -- src/scp/SCP.cpp | 40 +- src/scp/SCP.h | 27 +- src/scp/SCPDriver.cpp | 108 - src/scp/SCPDriver.h | 120 +- src/scp/Slot.cpp | 109 +- src/scp/Slot.h | 32 +- src/scp/test/SCPTests.cpp | 2256 +++++++++++---------- src/scp/test/SCPUnitTests.cpp | 840 -------- src/simulation/ApplyLoad.cpp | 11 +- src/test/TxTests.cpp | 2 +- 31 files changed, 2849 insertions(+), 5702 deletions(-) create mode 100644 src/herder/LeaderSchedule.cpp create mode 100644 src/herder/LeaderSchedule.h create mode 100644 src/herder/test/LeaderRoundsTests.cpp create mode 100644 src/herder/test/LeaderScheduleTests.cpp delete mode 100644 src/scp/NominationProtocol.cpp delete mode 100644 src/scp/NominationProtocol.h delete mode 100644 src/scp/test/SCPUnitTests.cpp diff --git a/src/herder/Herder.h b/src/herder/Herder.h index 66207170e..449225496 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 e26125643..1a66412bb 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -999,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 @@ -1691,9 +1697,7 @@ HerderImpl::setupTriggerNextLedger() // trigger. if (getSCP().isValidator() && !triggerDue) { - auto leaders = getSCP().predictNominationLeaders( - nextIndex, xdr::xdr_to_opaque(lcl.header.scpValue), 1); - 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. @@ -1865,12 +1869,8 @@ HerderImpl::prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) { return; } - auto const& lcl = mLedgerManager.getLastClosedLedgerHeader(); if (!getSCP().isValidator() || - !getSCP() - .predictNominationLeaders( - ledgerSeq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) - .count(getSCP().getLocalNodeID())) + !mHerderSCPDriver.isLocalNodeRoundLeader(ledgerSeq, 1)) { return; } @@ -1889,18 +1889,14 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) auto const lcl = mLedgerManager.getLastClosedLedgerHeader(); releaseAssert(ledgerSeq == lcl.header.ledgerSeq + 1); releaseAssert(closeTime > getConsensusTime(lcl.header.scpValue)); - bool const activeLeader = getSCP().getNominationLeaders(ledgerSeq).count( - getSCP().getLocalNodeID()); + uint32_t const round = getSCP().getLeaderRound(ledgerSeq); + bool const activeLeader = + round != 0 && mHerderSCPDriver.isLocalNodeRoundLeader(ledgerSeq, round); bool const legacyTxSet = buildsLegacyTxSet(lcl.header.ledgerVersion); - releaseAssert(activeLeader || legacyTxSet || - getSCP() - .predictNominationLeaders( - ledgerSeq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) - .count(getSCP().getLocalNodeID())); CLOG_INFO(Herder, "Building TX set as {} for ledger {}", - activeLeader ? "active nomination leader" - : legacyTxSet ? "validator (legacy tx set)" - : "first-round nomination leader", + 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 = @@ -2162,29 +2158,30 @@ 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 - // nominate. A skipped trigger must not credit the time spent waiting for + // take part. A skipped trigger must not credit the time spent waiting for // peers to the drift allowance. - mHerderSCPDriver.recordNominationTrigger(ledgerSeqToTrigger); + mHerderSCPDriver.recordTrigger(ledgerSeqToTrigger); if (!getSCP().isValidator()) { - CLOG_DEBUG(Herder, "Non-validating node, skipping nomination (SCP)."); + CLOG_DEBUG(Herder, "Non-validating node, not starting the slot (SCP)."); return; } - getHerderSCPDriver().recordSCPEvent(ledgerSeqToTrigger, true); - mHerderSCPDriver.nominate( + // 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 makeNominationValue(ledgerSeqToTrigger); + return makeProposalValue(ledgerSeqToTrigger); }, - lcl.header.scpValue, /*constructValueNow=*/buildsLegacyTxSet(lcl.header.ledgerVersion)); // A follower validates the leader's set when it arrives, and most of its @@ -2194,9 +2191,10 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger, // 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) && - !getSCP() - .getNominationLeaders(ledgerSeqToTrigger) - .count(getSCP().getLocalNodeID()) && + !mHerderSCPDriver.isLocalNodeRoundLeader( + ledgerSeqToTrigger, + std::max(1, + getSCP().getLeaderRound(ledgerSeqToTrigger))) && getSCP().getLatestMessagesSend(ledgerSeqToTrigger).empty()) { prewarmSignatureCache(ledgerSeqToTrigger, {}); @@ -2444,7 +2442,7 @@ HerderImpl::buildsLegacyTxSet(uint32_t lclLedgerVersion) } ValueWrapperPtr -HerderImpl::makeNominationValue(uint32_t ledgerSeqToTrigger) +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. @@ -2522,9 +2520,9 @@ HerderImpl::makeNominationValue(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); @@ -2591,12 +2589,12 @@ HerderImpl::makeNominationValue(uint32_t ledgerSeqToTrigger) } #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)); } @@ -3623,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 fb29ba924..f46ad8bf7 100644 --- a/src/herder/HerderImpl.h +++ b/src/herder/HerderImpl.h @@ -138,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; @@ -162,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; @@ -195,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; @@ -272,7 +272,7 @@ class HerderImpl : public Herder PreparedTxSet buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); void prepareTxSet(uint32_t ledgerSeq, ConsensusTime closeTime); void discardPreparedTxSet(); - ValueWrapperPtr makeNominationValue(uint32_t ledgerSeq); + 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); diff --git a/src/herder/HerderSCPDriver.cpp b/src/herder/HerderSCPDriver.cpp index 84a2c5882..4a04b8217 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,17 +927,11 @@ HerderSCPDriver::valueExternalized(uint64_t slotIndex, Value const& value) logQuorumInformationAndUpdateMetrics(slotIndex - 2); } - if (mLedgerSeqNominating != 0) - { - // 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(); - mLedgerSeqNominating = 0; - } + // 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()) { @@ -1338,22 +1006,20 @@ HerderSCPDriver::logQuorumInformationAndUpdateMetrics(uint64_t index) } void -HerderSCPDriver::nominate(uint64_t slotIndex, NominationValueSupplier makeValue, - StellarValue const& previousValue, - bool constructValueNow) +HerderSCPDriver::startSlot(uint64_t slotIndex, ProposalValueSupplier makeValue, + bool constructValueNow) { ZoneScoped; - if (mLedgerSeqNominating != slotIndex) + if (mLedgerSeqProposing != slotIndex) { mCurrentValue.reset(); } - mLedgerSeqNominating = static_cast(slotIndex); + mLedgerSeqProposing = static_cast(slotIndex); if (constructValueNow && !mCurrentValue) { auto value = makeValue(); - // Publishing the tx set may externalize this slot, which stops its - // nomination. Do not restart it. - if (mLedgerSeqNominating != slotIndex) + // Publishing the tx set may externalize this slot. Do not restart it. + if (mLedgerSeqProposing != slotIndex) { return; } @@ -1361,28 +1027,85 @@ HerderSCPDriver::nominate(uint64_t slotIndex, NominationValueSupplier makeValue, // a leader. mCurrentValue = std::move(value); } - auto prevValue = xdr::xdr_to_opaque(previousValue); - mSCP.nominate( - slotIndex, - [this, slotIndex, - makeValue = std::move(makeValue)]() -> ValueWrapperPtr { - if (mLedgerSeqNominating != slotIndex) - { - return nullptr; - } - if (!mCurrentValue) - { - auto value = makeValue(); - // Publishing the tx set may externalize a pending value. - if (mLedgerSeqNominating != slotIndex) - { - return nullptr; - } - mCurrentValue = std::move(value); - } - return mCurrentValue; - }, - prevValue); + 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; +} + +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); +} + +bool +HerderSCPDriver::isLocalNodeRoundLeader(uint64_t slotIndex, uint32_t round) +{ + auto leader = getRoundLeader(slotIndex, round); + return leader && *leader == mSCP.getLocalNodeID(); } SCPQuorumSetPtr @@ -1432,22 +1155,44 @@ 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; @@ -1560,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 @@ -1686,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 @@ -2026,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 047412be3..1067c0575 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,45 +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; - // Start nomination for slotIndex. SCP calls makeValue only when the local - // node is a round leader that needs its own value, unless - // constructValueNow is set: then the value is constructed before - // nomination starts (and reused if this node becomes a leader). - // previousValue is the value from slotIndex-1 - void nominate(uint64_t slotIndex, NominationValueSupplier makeValue, - StellarValue const& previousValue, - bool constructValueNow = false); + // 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, @@ -199,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; @@ -219,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; @@ -267,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 @@ -300,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 @@ -313,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}; @@ -331,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; @@ -354,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, @@ -379,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/LeaderSchedule.cpp b/src/herder/LeaderSchedule.cpp new file mode 100644 index 000000000..cc9599a71 --- /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 000000000..9042d90ea --- /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/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp index 6c3a3b5af..82b35f300 100644 --- a/src/herder/PendingEnvelopes.cpp +++ b/src/herder/PendingEnvelopes.cpp @@ -1164,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()) @@ -1195,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 75f597308..0011b5cda 100644 --- a/src/herder/PendingEnvelopes.h +++ b/src/herder/PendingEnvelopes.h @@ -94,6 +94,7 @@ class PendingEnvelopes RandomEvictionCache mValueSizeCache; bool mRebuildQuorum; + uint64_t mQuorumRebuildCount{0}; QuorumTracker mQuorumTracker; medida::Counter& mProcessedCount; @@ -256,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/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 0fc00e6b4..f151cbe81 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -2675,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; @@ -2708,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()); @@ -2869,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)); @@ -2887,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") { @@ -2911,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); } } @@ -2945,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") @@ -2985,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); } } @@ -3048,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)); @@ -3122,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); @@ -3173,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()) @@ -3204,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") { @@ -3357,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)); @@ -3365,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); @@ -3428,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") { @@ -3445,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") { @@ -3455,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") @@ -3576,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)); @@ -3673,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); } } @@ -3691,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); @@ -4728,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); @@ -4796,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; }; @@ -4829,13 +4622,29 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]") std::vector BEnvs = herderB.getSCP().getLatestMessagesSend(2); // Pass A and B's messages to C + auto const tFeedC = A->getClock().now(); + UNSCOPED_INFO( + "feeding C: A LCL " + << A->getLedgerManager().getLastClosedLedgerNum() << ", C round " + << dynamic_cast(C->getHerder()).getSCP().getLeaderRound(2) + << ", C sent " + << dynamic_cast(C->getHerder()) + .getSCP() + .getLatestMessagesSend(2) + .size()); for (auto const& env : AEnvs) { - C->getHerder().recvSCPEnvelope(env); + auto st = C->getHerder().recvSCPEnvelope(env); + UNSCOPED_INFO("C status for A's message type " + << static_cast(env.statement.pledges.type()) << ": " + << static_cast(st)); } for (auto const& env : BEnvs) { - C->getHerder().recvSCPEnvelope(env); + auto st = C->getHerder().recvSCPEnvelope(env); + UNSCOPED_INFO("C status for B's message type " + << static_cast(env.statement.pledges.type()) << ": " + << static_cast(st)); } // Crank C until it is on ledger 2 @@ -4843,15 +4652,31 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]") [&]() { return C->getLedgerManager().getLastClosedLedgerNum() == 2; }, 4 * simulation->getExpectedLedgerCloseTime(), false); + UNSCOPED_INFO("C reached ledger 2 after " + << std::chrono::duration_cast( + A->getClock().now() - tFeedC) + .count() + << " ms"); + // Get messages from C HerderImpl& herderC = dynamic_cast(C->getHerder()); std::vector CEnvs = herderC.getSCP().getLatestMessagesSend(2); // Pass C's messages to A and B + INFO("C sent " << CEnvs.size() << " message(s) for ledger 2; A at LCL " + << A->getLedgerManager().getLastClosedLedgerNum() + << ", B at LCL " + << B->getLedgerManager().getLastClosedLedgerNum() + << " when they are delivered"); for (auto const& env : CEnvs) { - A->getHerder().recvSCPEnvelope(env); - B->getHerder().recvSCPEnvelope(env); + INFO("C message type " + << static_cast(env.statement.pledges.type())); + auto const statusA = A->getHerder().recvSCPEnvelope(env); + auto const statusB = B->getHerder().recvSCPEnvelope(env); + UNSCOPED_INFO("delivery status A " << static_cast(statusA) + << ", B " + << static_cast(statusB)); } // Crank A and B until they're on at least ledger 3 @@ -4882,1088 +4707,6 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]") REQUIRE(checkSCPHistoryEntries(C, 2, expectedTypes)); } -using Topology = std::pair, std::vector>; - -// Generate a Topology with a single org containing 3 validators of HIGH quality -static Topology -simpleThreeNode() -{ - // Generate validators - std::vector sks; - std::vector validators; - int constexpr numValidators = 3; - for (int i = 0; i < numValidators; ++i) - { - SecretKey const& key = - sks.emplace_back(SecretKey::pseudoRandomForTesting()); - ValidatorEntry& entry = validators.emplace_back(); - entry.mName = fmt::format("validator-{}", i); - entry.mHomeDomain = "A"; - entry.mQuality = ValidatorQuality::VALIDATOR_HIGH_QUALITY; - entry.mKey = key.getPublicKey(); - entry.mHasHistory = false; - } - return {sks, validators}; -} - -// Generate a topology with 3 orgs of HIGH quality. Two orgs have 3 validators -// and one org has 5 validators. -static Topology -unbalancedOrgs() -{ - // Generate validators - std::vector sks; - std::vector validators; - int constexpr numValidators = 11; - for (int i = 0; i < numValidators; ++i) - { - // Orgs A and B have 3 validators each. Org C has 5 validators. - std::string org = "C"; - if (i < 3) - { - org = "A"; - } - else if (i < 6) - { - org = "B"; - } - - SecretKey const& key = - sks.emplace_back(SecretKey::pseudoRandomForTesting()); - ValidatorEntry& entry = validators.emplace_back(); - entry.mName = fmt::format("validator-{}", i); - entry.mHomeDomain = org; - entry.mQuality = ValidatorQuality::VALIDATOR_HIGH_QUALITY; - entry.mKey = key.getPublicKey(); - entry.mHasHistory = false; - } - return {sks, validators}; -} - -// Generate a tier 1-like topology. This topology has 7 HIGH quality orgs, each -// with 3 validators. -static Topology -tier1Like() -{ - std::vector sks; - std::vector validators; - int constexpr numOrgs = 7; - int constexpr validatorsPerOrg = 3; - - for (int i = 0; i < numOrgs; ++i) - { - std::string const org = fmt::format("org-{}", i); - for (int j = 0; j < validatorsPerOrg; ++j) - { - SecretKey const& key = - sks.emplace_back(SecretKey::pseudoRandomForTesting()); - ValidatorEntry& entry = validators.emplace_back(); - entry.mName = fmt::format("validator-{}-{}", i, j); - entry.mHomeDomain = org; - entry.mQuality = ValidatorQuality::VALIDATOR_HIGH_QUALITY; - entry.mKey = key.getPublicKey(); - entry.mHasHistory = false; - } - } - - return {sks, validators}; -} - -// Generate a slightly unbalanced topology. This topology has 7 HIGH quality -// orgs, 6 of which have 3 validators and 1 has 5 validators. -static Topology -slightlyUnbalancedOrgs() -{ - std::vector sks; - std::vector validators; - int constexpr numOrgs = 7; - - for (int i = 0; i < numOrgs; ++i) - { - std::string const org = fmt::format("org-{}", i); - int const numValidators = i == 0 ? 5 : 3; - for (int j = 0; j < numValidators; ++j) - { - SecretKey const& key = - sks.emplace_back(SecretKey::pseudoRandomForTesting()); - ValidatorEntry& entry = validators.emplace_back(); - entry.mName = fmt::format("validator-{}-{}", i, j); - entry.mHomeDomain = org; - entry.mQuality = ValidatorQuality::VALIDATOR_HIGH_QUALITY; - entry.mKey = key.getPublicKey(); - entry.mHasHistory = false; - } - } - - return {sks, validators}; -} - -// Returns a random quality up to `maxQuality` -static ValidatorQuality -randomQuality(ValidatorQuality maxQuality) -{ - return static_cast(rand_uniform( - static_cast(ValidatorQuality::VALIDATOR_LOW_QUALITY), - static_cast(maxQuality))); -} - -// Returns the minimum size an org of quality `q` can have -static int constexpr minOrgSize(ValidatorQuality q) -{ - switch (q) - { - case ValidatorQuality::VALIDATOR_LOW_QUALITY: - case ValidatorQuality::VALIDATOR_MED_QUALITY: - return 1; - case ValidatorQuality::VALIDATOR_HIGH_QUALITY: - case ValidatorQuality::VALIDATOR_CRITICAL_QUALITY: - return 3; - } -} - -// Generate a random topology with up to `maxValidators` validators. Ensures at -// least one org is HIGH quality. -static Topology -randomTopology(int maxValidators) -{ - int const numValidators = rand_uniform(3, maxValidators); - int constexpr minCritOrgSize = - minOrgSize(ValidatorQuality::VALIDATOR_CRITICAL_QUALITY); - - // Generate validators - int curOrg = 0; - int curOrgSize = 0; - ValidatorQuality curQuality = ValidatorQuality::VALIDATOR_HIGH_QUALITY; - std::vector sks(numValidators); - std::vector validators(numValidators); - for (int i = 0; i < numValidators; ++i) - { - if (curOrgSize >= minOrgSize(curQuality) && rand_flip()) - { - // Start new org - ++curOrg; - curOrgSize = 0; - curQuality = - randomQuality(numValidators - i >= minCritOrgSize - ? ValidatorQuality::VALIDATOR_CRITICAL_QUALITY - : ValidatorQuality::VALIDATOR_MED_QUALITY); - } - - std::string const org = fmt::format("org-{}", curOrg); - SecretKey const& key = sks.at(i) = SecretKey::pseudoRandomForTesting(); - - ValidatorEntry& entry = validators.at(i); - entry.mName = fmt::format("validator-{}", i); - entry.mHomeDomain = org; - entry.mQuality = curQuality; - entry.mKey = key.getPublicKey(); - entry.mHasHistory = false; - - ++curOrgSize; - } - - return {sks, validators}; -} - -// Expected weight of an org with quality `orgQuality` in a topology with a max -// quality of `maxQuality` and or quality counts of `orgQualityCounts`. This -// function normalizes the weight so that the highest quality has a weight of -// `1`. -static double -expectedOrgNormalizedWeight( - std::unordered_map const& orgQualityCounts, - ValidatorQuality maxQuality, ValidatorQuality orgQuality) -{ - if (orgQuality == ValidatorQuality::VALIDATOR_LOW_QUALITY) - { - return 0.0; - } - - double normalizedWeight = 1.0; - - // For each quality level higher than `orgQuality`, divide the weight by 10 - // times the number of orgs at that quality level - for (int q = static_cast(maxQuality); q > static_cast(orgQuality); - --q) - { - normalizedWeight /= - 10 * orgQualityCounts.at(static_cast(q)); - } - return normalizedWeight; -} - -// Expected weight of a validator in an org of size `orgSize` with quality -// `orgQuality`. `maxQuality` is the maximum quality present in the -// configuration. This function normalizes the weight so that the highest -// organization-level quality has a weight of `1`. -static double -expectedNormalizedWeight( - std::unordered_map const& orgQualityCounts, - ValidatorQuality maxQuality, ValidatorQuality orgQuality, int orgSize) -{ - return expectedOrgNormalizedWeight(orgQualityCounts, maxQuality, - orgQuality) / - orgSize; -} - -// Collect information about the qualities and sizes of organizations in -// `validators` and store them in `maxQuality`, `orgQualities`, `orgSizes`, and -// `orgQualityCounts`. -static void -collectOrgInfo(ValidatorQuality& maxQuality, - std::unordered_map& orgQualities, - std::unordered_map& orgSizes, - std::unordered_map& orgQualityCounts, - std::vector const& validators) -{ - maxQuality = ValidatorQuality::VALIDATOR_LOW_QUALITY; - ValidatorQuality minQuality = ValidatorQuality::VALIDATOR_CRITICAL_QUALITY; - std::unordered_map> - orgsByQuality; - for (ValidatorEntry const& validator : validators) - { - maxQuality = std::max(maxQuality, validator.mQuality); - minQuality = std::min(minQuality, validator.mQuality); - orgQualities[validator.mHomeDomain] = validator.mQuality; - ++orgSizes[validator.mHomeDomain]; - orgsByQuality[validator.mQuality].insert(validator.mHomeDomain); - } - - // Count orgs at each quality level - for (int q = static_cast(minQuality); - q <= static_cast(maxQuality); ++q) - { - orgQualityCounts[static_cast(q)] = - orgsByQuality[static_cast(q)].size(); - if (q != static_cast(minQuality)) - { - // Add virtual org covering next lower quality level - ++orgQualityCounts[static_cast(q)]; - } - } -} - -// Given a list of validators, test that the weights of the validators herder -// reports are correct -static void -testWeights(std::vector const& validators) -{ - Config cfg = getTestConfig(0); - - cfg.generateQuorumSetForTesting(validators); - - VirtualClock clock; - Application::pointer app = createTestApplication(clock, cfg); - - // Collect info about orgs - ValidatorQuality maxQuality; - std::unordered_map orgQualities; - std::unordered_map orgSizes; - std::unordered_map orgQualityCounts; - collectOrgInfo(maxQuality, orgQualities, orgSizes, orgQualityCounts, - validators); - - // Check per-validator weights - HerderImpl& herder = dynamic_cast(app->getHerder()); - std::unordered_map normalizedOrgWeights; - for (ValidatorEntry const& validator : validators) - { - uint64_t weight = herder.getHerderSCPDriver().getNodeWeight( - validator.mKey, cfg.QUORUM_SET, false); - double normalizedWeight = - static_cast(weight) / static_cast(UINT64_MAX); - normalizedOrgWeights[validator.mHomeDomain] += normalizedWeight; - - std::string const& org = validator.mHomeDomain; - REQUIRE_THAT(normalizedWeight, - Catch::Matchers::WithinAbs( - expectedNormalizedWeight(orgQualityCounts, maxQuality, - orgQualities.at(org), - orgSizes.at(org)), - 0.0001)); - } - - // Check per-org weights - for (auto const& [org, weight] : normalizedOrgWeights) - { - REQUIRE_THAT( - weight, Catch::Matchers::WithinAbs( - expectedOrgNormalizedWeight( - orgQualityCounts, maxQuality, orgQualities.at(org)), - 0.0001)); - } -} - -// Test that HerderSCPDriver::getNodeWeight produces weights that result in a -// fair distribution of nomination wins. -TEST_CASE("getNodeWeight", "[herder]") -{ - SECTION("3 tier 1 validators, 1 org") - { - testWeights(simpleThreeNode().second); - } - - SECTION("11 tier 1 validators, 3 unbalanced orgs") - { - testWeights(unbalancedOrgs().second); - } - - SECTION("Tier1-like topology") - { - testWeights(tier1Like().second); - } - - SECTION("Tier1-like topology with a single unbalanced org") - { - testWeights(slightlyUnbalancedOrgs().second); - } - - SECTION("Random topology") - { - // Test weights for 1000 random topologies of up to 200 validators - for (int i = 0; i < 1000; ++i) - { - testWeights(randomTopology(200).second); - } - } -} - -static Value -getRandomValue() -{ - auto h = sha256(fmt::format("value {}", getGlobalRandomEngine()())); - return xdr::xdr_to_opaque(h); -} - -// A test version of NominationProtocol that exposes `updateRoundLeaders` -class TestNominationProtocol : public NominationProtocol -{ - public: - TestNominationProtocol(Slot& slot) : NominationProtocol(slot) - { - } - - std::set const& - updateRoundLeadersForTesting( - std::optional const& previousValue = std::nullopt) - { - mPreviousValue = previousValue.value_or(getRandomValue()); - updateRoundLeaders(); - return getLeaders(); - } - - // Detect fast timeouts by examining the final round number - bool - fastTimedOut() const - { - return mRoundNumber > 0; - } -}; - -// Test nomination over `numLedgers` slots. After running, check that the win -// percentages of each node and org are within 5% of the expected win -// percentages. -static void -testWinProbabilities(std::vector const& sks, - std::vector const& validators, - int const numLedgers) -{ - REQUIRE(sks.size() == validators.size()); - - // Collect info about orgs - ValidatorQuality maxQuality; - std::unordered_map orgQualities; - std::unordered_map orgSizes; - std::unordered_map orgQualityCounts; - collectOrgInfo(maxQuality, orgQualities, orgSizes, orgQualityCounts, - validators); - - // Generate a config - Config cfg = getTestConfig(); - cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; - cfg.generateQuorumSetForTesting(validators); - cfg.NODE_SEED = sks.front(); - - // Create an application - VirtualClock clock; - Application::pointer app = createTestApplication(clock, cfg); - - // Run for `numLedgers` slots, recording the number of times each - // node wins nomination - UnorderedMap publishCounts; - HerderImpl& herder = dynamic_cast(app->getHerder()); - SCP& scp = herder.getSCP(); - int fastTimeouts = 0; - for (int i = 0; i < numLedgers; ++i) - { - auto s = std::make_shared(i, scp); - TestNominationProtocol np(*s); - - std::set const& leaders = np.updateRoundLeadersForTesting(); - REQUIRE(leaders.size() == 1); - for (NodeID const& leader : leaders) - { - ++publishCounts[leader]; - } - - if (np.fastTimedOut()) - { - ++fastTimeouts; - } - } - - CLOG_INFO(Herder, "Fast Timeouts: {} ({}%)", fastTimeouts, - fastTimeouts * 100.0 / numLedgers); - - // Compute total expected normalized weight across all nodes - double totalNormalizedWeight = 0.0; - for (ValidatorEntry const& validator : validators) - { - totalNormalizedWeight += - expectedNormalizedWeight(orgQualityCounts, maxQuality, - orgQualities.at(validator.mHomeDomain), - orgSizes.at(validator.mHomeDomain)); - } - - // Check validator win rates - std::map orgPublishCounts; - for (ValidatorEntry const& validator : validators) - { - NodeID const& nodeID = validator.mKey; - int publishCount = publishCounts[nodeID]; - - // Compute and report node's win rate - double winRate = static_cast(publishCount) / numLedgers; - CLOG_INFO(Herder, "Node {} win rate: {} (published {} ledgers)", - cfg.toShortString(nodeID), winRate, publishCount); - - // Expected win rate is `weight / total weight` - double expectedWinRate = - expectedNormalizedWeight(orgQualityCounts, maxQuality, - orgQualities.at(validator.mHomeDomain), - orgSizes.at(validator.mHomeDomain)) / - totalNormalizedWeight; - - // Check that actual win rate is within .05 of expected win - // rate. - REQUIRE_THAT(winRate, - Catch::Matchers::WithinAbs(expectedWinRate, 0.05)); - - // Record org publish counts for the next set of checks - orgPublishCounts[validator.mHomeDomain] += publishCount; - } - - // Check org win rates - for (auto const& [org, count] : orgPublishCounts) - { - // Compute and report org's win rate - double winRate = static_cast(count) / numLedgers; - CLOG_INFO(Herder, "Org {} win rate: {} (published {} ledgers)", org, - winRate, count); - - // Expected win rate is `weight / total weight` - double expectedWinRate = - expectedOrgNormalizedWeight(orgQualityCounts, maxQuality, - orgQualities.at(org)) / - totalNormalizedWeight; - - // Check that actual win rate is within .05 of expected win - // rate. - REQUIRE_THAT(winRate, - Catch::Matchers::WithinAbs(expectedWinRate, 0.05)); - } -} - -// Test that the nomination algorithm produces a fair distribution of ledger -// publishers. -TEST_CASE("Fair nomination win rates", "[herder]") -{ - SECTION("3 tier 1 validators, 1 org") - { - auto [sks, validators] = simpleThreeNode(); - testWinProbabilities(sks, validators, 10000); - } - - SECTION("11 tier 1 validators, 3 unbalanced orgs") - { - auto [sks, validators] = unbalancedOrgs(); - testWinProbabilities(sks, validators, 10000); - } - - SECTION("Tier 1-like topology") - { - auto [sks, validators] = tier1Like(); - testWinProbabilities(sks, validators, 10000); - } - - SECTION("Tier 1-like topology with a single unbalanced org") - { - auto [sks, validators] = slightlyUnbalancedOrgs(); - testWinProbabilities(sks, validators, 10000); - } - - SECTION("Random topology") - { - for (int i = 0; i < 10; ++i) - { - auto [sks, validators] = randomTopology(50); - testWinProbabilities(sks, validators, 10000); - } - } -} - -namespace -{ -// Returns a new `Topology` with the last org in `t` replaced with a new org -// with 3 validators. Requires that the last org in `t` have 3 validators and be -// contiguous at the back of the validators vecto. -Topology -replaceOneOrg(Topology const& t) -{ - Topology t2(t); // Copy the topology - auto& [sks, validators] = t2; - REQUIRE(sks.size() == validators.size()); - - // Give the org a unique name - std::string const orgName = "org-replaced"; - - // Double check that the new org name is unique - for (ValidatorEntry const& v : validators) - { - REQUIRE(v.mHomeDomain != orgName); - } - - // Remove the last org - constexpr int validatorsPerOrg = 3; - sks.resize(sks.size() - validatorsPerOrg); - validators.resize(validators.size() - validatorsPerOrg); - - // Add new org with 3 validators - int constexpr numValidators = 3; - for (int j = 0; j < numValidators; ++j) - { - SecretKey const& key = - sks.emplace_back(SecretKey::pseudoRandomForTesting()); - ValidatorEntry& entry = validators.emplace_back(); - entry.mName = fmt::format("validator-replaced-{}", j); - entry.mHomeDomain = orgName; - entry.mQuality = ValidatorQuality::VALIDATOR_HIGH_QUALITY; - entry.mKey = key.getPublicKey(); - entry.mHasHistory = false; - } - - return {sks, validators}; -} - -// Add `orgsToAdd` new orgs to the topology `t`. Each org will have 3 -// validators. -Topology -addOrgs(int orgsToAdd, Topology const& t) -{ - Topology t2(t); // Copy the topology - auto& [sks, validators] = t2; - REQUIRE(sks.size() == validators.size()); - - // Generate new orgs - for (int i = 0; i < orgsToAdd; ++i) - { - std::string const org = fmt::format("new-org-{}", i); - int constexpr numValidators = 3; - for (int j = 0; j < numValidators; ++j) - { - SecretKey const& key = - sks.emplace_back(SecretKey::pseudoRandomForTesting()); - ValidatorEntry& entry = validators.emplace_back(); - entry.mName = fmt::format("new-validator-{}-{}", i, j); - entry.mHomeDomain = org; - entry.mQuality = ValidatorQuality::VALIDATOR_HIGH_QUALITY; - entry.mKey = key.getPublicKey(); - entry.mHasHistory = false; - } - } - return t2; -} - -// Returns `true` if the set intersection of `leaders1` and `leaders2` is not -// empty. -bool -leadersIntersect(std::set const& leaders1, - std::set const& leaders2) -{ - std::vector intersection; - std::set_intersection(leaders1.begin(), leaders1.end(), leaders2.begin(), - leaders2.end(), std::back_inserter(intersection)); - return !intersection.empty(); -} - -// Given two quorum sets consisting of validators in `validators1` and -// `validators2`, this function returns the probability that the two quorum sets -// will agree on a leader in the first round of nomination. -double -computeExpectedFirstRoundAgreementProbability( - std::vector const& validators1, - std::vector const& validators2) -{ - // Gather orgs - std::set orgs1; - std::transform(validators1.begin(), validators1.end(), - std::inserter(orgs1, orgs1.end()), - [](ValidatorEntry const& v) { return v.mHomeDomain; }); - std::set orgs2; - std::transform(validators2.begin(), validators2.end(), - std::inserter(orgs2, orgs2.end()), - [](ValidatorEntry const& v) { return v.mHomeDomain; }); - - // Compute overlap - std::vector sharedOrgs; - std::set_intersection(orgs1.begin(), orgs1.end(), orgs2.begin(), - orgs2.end(), std::back_inserter(sharedOrgs)); - - // Probability of agreement in first round is (orgs overlapping / orgs1) * - // (orgs overlapping / orgs2). That's the probability that the two sides - // will pick any overlapping org. The algorithm guarantees that if they pick - // overlapping validator, they'll pick the same validator. - double overlap = static_cast(sharedOrgs.size()); - return overlap / orgs1.size() * overlap / orgs2.size(); -} - -// Test that the nomination algorithm behaves as expected when the two quorum -// sets `qs1` and `qs2` are not equivalent. This function requires that both -// quorum sets overlap, and contain only a single quality level of validators. -// Runs simulation for `numLedgers` slots. -// NOTE: This test counts any failure to agree on a leader as a timeout. In -// practice, it's possible that one side of the split is large enough to proceed -// without the other side. In this case, the larger side might not experience a -// timeout and "drag" the other side through consensus with it. However, this -// test aims to analyze the worst case scenario where the two sides are fairly -// balanced and real-world networking conditions are in place (some nodes -// lagging, etc), such that disagreement always results in a timeout. -void -testAsymmetricTimeouts(Topology const& qs1, Topology const& qs2, - int const numLedgers) -{ - auto const& [sks1, validators1] = qs1; - auto const& [sks2, validators2] = qs2; - - REQUIRE(sks1.size() == validators1.size()); - REQUIRE(sks2.size() == validators2.size()); - - // Generate configs and nodes representing one validator with each quorum - // set - std::vector clocks(2); - std::vector apps; - for (int i = 0; i < 2; ++i) - { - Config cfg = getTestConfig(i); - cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; - cfg.generateQuorumSetForTesting(i == 0 ? validators1 : validators2); - cfg.NODE_SEED = i == 0 ? sks1.back() : sks2.back(); - - auto app = apps.emplace_back(createTestApplication(clocks.at(i), cfg)); - } - - // Run the nomination algorithm for `numLedgers` slots. Simulate timeouts by - // re-running slots that don't agree on a leader until their leader - // elections overlap. Record the number of timeouts it takes for the two - // quorum sets to agree on a leader in `timeouts`, which is effectively a - // mapping from number of timeouts to the number of ledgers that experienced - // that many timeouts. - std::vector timeouts(std::max(validators1.size(), validators2.size())); - for (int i = 0; i < numLedgers; ++i) - { - Value const v = getRandomValue(); - SCP& scp1 = dynamic_cast(apps.at(0)->getHerder()).getSCP(); - SCP& scp2 = dynamic_cast(apps.at(1)->getHerder()).getSCP(); - auto s1 = std::make_shared(i, scp1); - auto s2 = std::make_shared(i, scp2); - - TestNominationProtocol np1(*s1); - TestNominationProtocol np2(*s2); - - for (int j = 0; j < timeouts.size(); ++j) - { - std::set const& leaders1 = - np1.updateRoundLeadersForTesting(v); - std::set const& leaders2 = - np2.updateRoundLeadersForTesting(v); - REQUIRE(leaders1.size() == j + 1); - REQUIRE(leaders2.size() == j + 1); - - if (leadersIntersect(leaders1, leaders2)) - { - // Agreed on a leader! Record the number of timeouts resulted. - ++timeouts.at(j); - break; - } - } - - // If leaders don't intersect after running through the loop then the - // two quorum sets have no overlap and the test is broken. - REQUIRE(leadersIntersect(np1.getLeaders(), np2.getLeaders())); - } - - // For the first round, we can easily compute the expected agreement - // probability. For subsequent rounds, we check only that the success rate - // increases over time (modulo some small epsilon). - double expectedSuccessRate = - computeExpectedFirstRoundAgreementProbability(validators1, validators2); - - // Allow for some small decrease in success rate from the theoretical value. - // We're working with probabilistic simulation here so we can't be too - // strict or the test will be flaky. - double constexpr epsilon = 0.1; - - // There's not enough data in the tail of the distribution to allow us to - // assert that the success rate is what's expected. To avoid sporadic test - // failures, we cut off `tailCutoffPoint` of the tail of the distribution - // for the purposes of asserting test values. However, the test will still - // log those success rates for manual examination. - double constexpr tailCutoffPoint = 0.05; - - int numLedgersRemaining = numLedgers; - for (int i = 0; i < timeouts.size(); ++i) - { - int const numTimeouts = timeouts.at(i); - if (numTimeouts == 0) - { - // Avoid cluttering output - continue; - } - - CLOG_INFO(Herder, "Ledgers with {} timeouts: {} ({}%)", i, numTimeouts, - static_cast(numTimeouts) * 100 / numLedgers); - - if (numLedgersRemaining > numLedgers * tailCutoffPoint) - { - // Check that success rate increases over time. Allow some epsilon - // decrease because this is a probabilistic simulation. Also stop - // checking when we're at the last `tailCutoffPoint` timeouts as the - // data is too sparse to be useful. - double successRate = - static_cast(timeouts.at(i)) / numLedgersRemaining; - REQUIRE(successRate > expectedSuccessRate - epsilon); - - // Take max of success rate and previous success rate to avoid - // accidentally accepting a declining success rate due to episilon. - expectedSuccessRate = std::max(successRate, expectedSuccessRate); - numLedgersRemaining -= numTimeouts; - } - } -} -} // namespace - -// Test timeouts with asymmetric quorums. This test serves two purposes: -// 1. It contains assertions checking for moderate (10%) deviations from the -// expected behavior of the nomination algorithm. These should detect any -// major issues/regressions with the algorithm. -// 2. It logs the distributions of timeouts for manual inspection. This is -// useful for understanding the behavior of the algorithm and for testing -// specific scenarios one might be interested in (e.g., if tier 1 disagrees -// on one org's presence in tier 1, what is the impact on nomination -// timeouts?). -// NOTE: This provides a worst-case analysis of timeouts. See the NOTE on -// `testAsymmetricTimeouts` for more details. -TEST_CASE("Asymmetric quorum timeouts", "[herder]") -{ - // Number of slots to run for - int constexpr numLedgers = 20000; - - SECTION("Tier 1-like topology with replaced org") - { - auto t = tier1Like(); - testAsymmetricTimeouts(t, replaceOneOrg(t), numLedgers); - } - - SECTION("Tier 1-like topology with 1 added org") - { - auto t = tier1Like(); - testAsymmetricTimeouts(t, addOrgs(1, t), numLedgers); - } - - SECTION("Tier 1-like topology with 3 added orgs") - { - auto t = tier1Like(); - testAsymmetricTimeouts(t, addOrgs(3, t), numLedgers); - } -} - -// Test that the nomination algorithm behaves as expected when a random -// `numUnresponsive` set of nodes in `qs` are unresponsive. Runs simulation for -// `numLedgers` slots. -static void -testUnresponsiveTimeouts(Topology const& qs, int numUnresponsive, - int const numLedgers) -{ - auto const& [sks, validators] = qs; - REQUIRE(sks.size() == validators.size()); - REQUIRE(numUnresponsive < validators.size()); - - // extract and shuffle node ids. Choose `numUnresponsive` nodes to be the - // unresponsive nodes. - std::vector nodeIDs; - std::transform(validators.begin(), validators.end(), - std::back_inserter(nodeIDs), - [](ValidatorEntry const& v) { return v.mKey; }); - stellar::shuffle(nodeIDs.begin(), nodeIDs.end(), getGlobalRandomEngine()); - std::set unresponsive(nodeIDs.begin(), - nodeIDs.begin() + numUnresponsive); - - // Collect info about orgs - ValidatorQuality maxQuality; - std::unordered_map orgQualities; - std::unordered_map orgSizes; - std::unordered_map orgQualityCounts; - collectOrgInfo(maxQuality, orgQualities, orgSizes, orgQualityCounts, - validators); - - // Compute total weight of all validators, as well as the total weight of - // unresponsive validators - double totalWeight = 0.0; - double unresponsiveWeight = 0.0; - for (ValidatorEntry const& validator : validators) - { - double normalizedWeight = - expectedNormalizedWeight(orgQualityCounts, maxQuality, - orgQualities.at(validator.mHomeDomain), - orgSizes.at(validator.mHomeDomain)); - totalWeight += normalizedWeight; - if (unresponsive.count(validator.mKey)) - { - unresponsiveWeight += normalizedWeight; - } - } - - // Compute the average weight of an unresponsive node - double avgUnresponsiveWeight = unresponsiveWeight / numUnresponsive; - - // Compute expected number of ledgers experiencing `n` timeouts where `n` is - // the index of the `timeouts` vector. This vector is a mapping from number - // of timeouts to expected number of ledgers experiencing that number of - // timeouts. - std::vector expectedTimeouts(numUnresponsive + 1); - double remainingWeight = totalWeight; - int remainingUnresponsive = numUnresponsive; - int remainingLedgers = numLedgers; - for (int i = 0; i < expectedTimeouts.size(); ++i) - { - double timeoutProb = - (avgUnresponsiveWeight * remainingUnresponsive) / remainingWeight; - // To get expected number of ledgers experiencing `i` timeouts, we take - // the probability a timeout does not occur and multiply it by the - // number of remaining ledgers. - int expectedLedgers = (1 - timeoutProb) * remainingLedgers; - expectedTimeouts.at(i) = expectedLedgers; - - // Remaining ledgers decreases by expected number of ledgers - // experiencing `i` timeouts - remainingLedgers -= expectedLedgers; - - // For `i+1` timeouts to occur, an unresponsive node must be chosen. - // Therefore, deduct the average weight of an unresponsive node from the - // total weight left in the network. - remainingWeight -= avgUnresponsiveWeight; - --remainingUnresponsive; - } - - // Generate a config - Config cfg = getTestConfig(); - cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; - cfg.generateQuorumSetForTesting(validators); - cfg.NODE_SEED = sks.front(); - - // Create an application - VirtualClock clock; - Application::pointer app = createTestApplication(clock, cfg); - - // Run for `numLedgers` slots, recording the number of times each slot timed - // out due to unresponsive nodes before successfully electing a responsive - // leader. - SCP& scp = dynamic_cast(app->getHerder()).getSCP(); - std::vector timeouts(numUnresponsive + 1); - for (int i = 0; i < numLedgers; ++i) - { - Value const v = getRandomValue(); - auto s = std::make_shared(i, scp); - - TestNominationProtocol np(*s); - for (int i = 0; i < timeouts.size(); ++i) - { - std::set const& leaders = - np.updateRoundLeadersForTesting(v); - // If leaders is a subset of unresponsive, then a timeout occurs. - if (!std::includes(unresponsive.begin(), unresponsive.end(), - leaders.begin(), leaders.end())) - { - ++timeouts.at(i); - break; - } - } - } - - // Allow for some small multiplicative increase in timeouts from the - // theoretical value. We're working with probabilistic simulation here so - // we can't be too strict or the test will be flaky. - double constexpr epsilon = 1.1; - - // There's not enough data in the tail of the distribution to allow us to - // assert that the timeout values are what's expected. To avoid sporadic - // test failures, we cut off `tailCutoffPoint` of the tail of the - // distribution for the purposes of asserting test values. However, the test - // will still log those values for manual examination. - double constexpr tailCutoffPoint = 0.05; - - // Analyze timeouts - int numLedgersRemaining = numLedgers; - for (int i = 0; i < timeouts.size(); ++i) - { - int const numTimeouts = timeouts.at(i); - int const expectedNumTimeouts = expectedTimeouts.at(i); - - if (numLedgersRemaining > numLedgers * tailCutoffPoint) - { - // Check that timeouts are less than epsilon times the expected - // value. Also stop checking when we're at the last - // `tailCutoffPoint` timeouts as the data is too sparse to be - // useful. - REQUIRE(numTimeouts < expectedNumTimeouts * epsilon); - } - CLOG_INFO(Herder, "Ledgers with {} timeouts: {} ({}%)", i, numTimeouts, - numTimeouts * 100.0 / numLedgers); - numLedgersRemaining -= numTimeouts; - } -} - -// Test timeouts for a tier 1-like topology with 1-5 unresponsive nodes. This -// test serves two purposes: -// 1. It contains assertions checking for moderate (10%) deviations from the -// expected behavior of the nomination algorithm. These should detect any -// major issues/regressions with the algorithm. -// 2. It logs the distributions of timeouts for manual inspection. This is -// useful for understanding the behavior of the algorithm and for testing -// specific scenarios one might be interested in (e.g., if 3 tier 1 nodes -// are heavily lagging, what is the impact on nomination timeouts?). -TEST_CASE("Unresponsive quorum timeouts", "[herder]") -{ - // Number of slots to run for - int constexpr numLedgers = 20000; - - auto t = tier1Like(); - for (int i = 1; i <= 5; ++i) - { - CLOG_INFO(Herder, "Simulating nomination with {} unresponsive nodes", - i); - testUnresponsiveTimeouts(t, i, numLedgers); - } -} - -TEST_CASE("nomination timeouts with partial upgrade arming", - "[herder][acceptance]") -{ - // Configure simulation to use automatic quorum set configuration so that it - // runs with the application-specific leader election algorithm, which does - // not introduce its own timeouts. - Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); - Simulation::pointer simulation = - Topologies::separateAllHighQuality(16, networkID, [&](int i) { - return getTestConfig(i, Config::TESTDB_DEFAULT); - }); - simulation->fullyConnectAllPending(); - simulation->startAllNodes(); - auto nodes = simulation->getNodes(); - REQUIRE(nodes.size() == 16); - - // Let the network run for a few ledgers normally first - auto const expectedLedgerCloseTime = - simulation->getExpectedLedgerCloseTime(); - simulation->crankUntil( - [&]() { return simulation->haveAllExternalized(5, 1); }, - 10 * expectedLedgerCloseTime, false); - - // Create an upgrade to arm on a subset of nodes. - Upgrades::UpgradeParameters scheduledUpgrades; - auto lclCloseTime = - VirtualClock::from_time_t(nodes[0] - ->getLedgerManager() - .getLastClosedLedgerHeader() - .header.scpValue.closeTime); - - // Set upgrade time to now so it's active immediately - scheduledUpgrades.mUpgradeTime = lclCloseTime; - - // Upgrade the base fee by a small amount - auto const currentFee = nodes[0]->getLedgerManager().getLastTxFee(); - scheduledUpgrades.mBaseFee = currentFee + 100; - - // Limit max timeouts per slot to 1 - constexpr uint32_t maxTimeouts = 1; - scheduledUpgrades.mNominationTimeoutLimit = maxTimeouts; - - // Reduce upgrade window to 4 minutes - constexpr std::chrono::minutes upgradeWindow(4); - scheduledUpgrades.mExpirationMinutes = upgradeWindow; - - // Number of ledgers to check timeouts during - constexpr int ledgersToRun = 20; - - // Maximum total timeout duration for the test. Worst case is that each slot - // experiences 1 timeout, which adds 1 second each. - constexpr auto maxTotalTimeoutDuration = std::chrono::seconds(ledgersToRun); - - // Ensure upgrade window is set properly so that the upgrade doesn't expire - // during the `ledgersToRun` time period - REQUIRE(upgradeWindow > - expectedLedgerCloseTime * ledgersToRun + maxTotalTimeoutDuration); - - // Arm upgrades on 10 nodes (just 1 shy of a quorum) - for (size_t i = 0; i < 10; ++i) - { - nodes[i]->getHerder().setUpgrades(scheduledUpgrades); - } - - // Track initial ledger number - auto const startLedger = - nodes[0]->getLedgerManager().getLastClosedLedgerNum(); - - // Run for `ledgersToRun` more ledgers with mixed upgrade state - auto& herder = dynamic_cast(nodes[0]->getHerder()); - HerderSCPDriver const& driver = herder.getHerderSCPDriver(); - for (int i = 1; i <= ledgersToRun; ++i) - { - uint32_t const ledger = startLedger + i; - simulation->crankUntil( - [&]() { return simulation->haveAllExternalized(ledger, 1); }, - expectedLedgerCloseTime * 2, false); - - // Should see at most `maxTimeouts` per slot, depending on the round - // leaders. - std::optional timeouts = driver.getNominationTimeouts(ledger); - REQUIRE(timeouts.has_value()); - REQUIRE(timeouts.value() <= maxTimeouts); - } - - // Helper to check whether upgrade is still active by comparing with - // `scheduledUpgrades` - std::string const upgradeJson = scheduledUpgrades.toJson(); - auto const upgradeIsActive = [&]() { - return herder.getUpgrades().getParameters().toJson() == upgradeJson; - }; - - // Verify upgrade is still active - REQUIRE(upgradeIsActive()); - - // Verify that upgrade expires properly after the window - simulation->crankUntil(std::not_fn(upgradeIsActive), upgradeWindow, false); - - // Ensure the changed fields are all reset - auto const& upgradeParams = herder.getUpgrades().getParameters(); - REQUIRE(!upgradeParams.mBaseFee.has_value()); - REQUIRE(!upgradeParams.mNominationTimeoutLimit.has_value()); - REQUIRE(!upgradeParams.mExpirationMinutes.has_value()); - - // Verify the upgrade did not go through - REQUIRE(nodes[0]->getLedgerManager().getLastTxFee() == currentFee); -} - TEST_CASE("late joining node reaches consensus", "[herder]") { auto networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); @@ -6100,7 +4843,7 @@ TEST_CASE_VERSIONS("Herder properly validates when tx set is missing", }); } -// One of four validators nominates a non-canonical empty tx set (a tx set +// One of four validators proposes a non-canonical empty tx set (a tx set // without transactions that isn't encoded as `TxSetXDRFrame::makeEmpty`, // which history can't replay) on every ledger. Before the protocol gate the // network externalizes it, after the gate it's rejected as invalid. @@ -6132,8 +4875,7 @@ TEST_CASE("network does not externalize non-canonical empty tx set", auto simulation = Topologies::core( 4, 0.75, sha256(getTestConfig().NETWORK_PASSPHRASE), makeConfig); auto nodes = simulation->getNodes(); - static_cast(nodes[0]->getHerder()) - .mNominatedTxSetOverride = + static_cast(nodes[0]->getHerder()).mProposedTxSetOverride = [&](LedgerHeaderHistoryEntry const& lcl) { return makeNonCanonicalEmptyTxSet(lcl.hash); }; @@ -6169,8 +4911,11 @@ TEST_CASE("network does not externalize non-canonical empty tx set", } else { + // Honest nodes do not vote for the bad proposal. There is no + // nomination to carry it into their ballots (where it used to be + // replaced with an empty-tx-set value): the round times out and + // the next leader's value closes the ledger. REQUIRE(externalized == 0); - REQUIRE(replaced > 0); } }; @@ -6197,7 +4942,7 @@ TEST_CASE("network externalizes empty-tx-set on missing value", "[herder][tx]") auto cfg = getTestConfig(i, Config::TESTDB_DEFAULT); cfg.EXPERIMENTAL_PARALLEL_TX_SET_DOWNLOAD = true; cfg.TX_SET_DOWNLOAD_TIMEOUT = std::chrono::milliseconds{0}; - cfg.TESTING_NOMINATE_RANDOM_VALUES = true; + cfg.TESTING_PROPOSE_RANDOM_VALUES = true; return cfg; }); @@ -6301,7 +5046,7 @@ TEST_CASE("SCP state restore with missing tx set", "[herder]") // The restored value's tx set is missing and nothing is fetching it, but // the value is still structurally valid - REQUIRE(driver.validateValue(slot, value, /*nomination*/ false) == + REQUIRE(driver.validateValue(slot, value, /*proposal*/ false) == SCPDriver::kStructurallyValidValue); // The peer's view of the slot: it timed out waiting for the missing tx @@ -6372,11 +5117,10 @@ triggerTimerProtocolSupported() } // Four top-tier validators over TCP on the real clock, with a 1s artificial -// apply delay and a configurable nomination-emit delay so we can actually see -// the impact of the two different timers. +// apply delay. static Simulation::pointer makeTriggerTimerSimulation( - bool forcePrepareStartTimer, std::chrono::milliseconds nominationEmitDelay, + bool forcePrepareStartTimer, std::chrono::milliseconds driftClockOffset = std::chrono::milliseconds::zero(), std::optional startingProtocol = std::nullopt) @@ -6391,8 +5135,6 @@ makeTriggerTimerSimulation( forcePrepareStartTimer; cfg.ARTIFICIALLY_DELAY_LEDGER_CLOSE_FOR_TESTING = std::chrono::milliseconds(1000); - cfg.ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING = - nominationEmitDelay; // Remember enough SCP slots that the tests can attribute every // externalized value in their measurement windows to its // proposer. @@ -6469,7 +5211,6 @@ TEST_CASE("consensus close time trigger timer", "[herder][!hide]") int64_t driftedNodeFallbacks{0}; int64_t maxOtherNodeFallbacks{0}; int64_t driftedLedSlots{0}; - bool sawNominationTimeout{false}; }; auto fallbackCount = [](Application::pointer const& app) { @@ -6487,11 +5228,10 @@ TEST_CASE("consensus close time trigger timer", "[herder][!hide]") auto runSimulation = [&](bool forcePrepareStartTimer, - std::chrono::milliseconds nominationEmitDelay, std::chrono::milliseconds triggerClockOffset = std::chrono::milliseconds::zero()) -> RunResult { - auto simulation = makeTriggerTimerSimulation( - forcePrepareStartTimer, nominationEmitDelay, triggerClockOffset); + auto simulation = makeTriggerTimerSimulation(forcePrepareStartTimer, + triggerClockOffset); auto nodes = simulation->getNodes(); std::vector fallbackCounts; @@ -6524,18 +5264,6 @@ TEST_CASE("consensus close time trigger timer", "[herder][!hide]") result.maxOtherNodeFallbacks = std::max(result.maxOtherNodeFallbacks, delta); } - - auto const& driver = - dynamic_cast(nodes[i]->getHerder()) - .getHerderSCPDriver(); - for (uint32_t ledger = startLedger + 1; ledger <= targetLedger; - ++ledger) - { - auto timeouts = driver.getNominationTimeouts(ledger); - result.sawNominationTimeout = - result.sawNominationTimeout || - (timeouts.has_value() && timeouts.value() > 0); - } } // Count the externalized values the drifted @@ -6580,13 +5308,10 @@ TEST_CASE("consensus close time trigger timer", "[herder][!hide]") return result; }; - // New timer is faster without drift. + // Without drift the new timer never falls back. (There is no nomination + // phase whose duration would separate the two anchors any more.) { - auto const nominationDelay = std::chrono::milliseconds(1000); - auto const oldTimer = runSimulation(true, nominationDelay); - auto const newTimer = runSimulation(false, nominationDelay); - - REQUIRE(newTimer.elapsed < oldTimer.elapsed); + auto const newTimer = runSimulation(false); REQUIRE(newTimer.totalFallbacks == 0); } @@ -6594,8 +5319,7 @@ TEST_CASE("consensus close time trigger timer", "[herder][!hide]") // One node drifting ahead falls back. { - auto const nodeAhead = runSimulation( - false, std::chrono::milliseconds::zero(), driftOffset); + auto const nodeAhead = runSimulation(false, driftOffset); REQUIRE(nodeAhead.driftedNodeFallbacks >= LEDGERS_TO_RUN - nodeAhead.driftedLedSlots - FALLBACK_SLACK); @@ -6607,8 +5331,7 @@ TEST_CASE("consensus close time trigger timer", "[herder][!hide]") // One node drifting behind falls back. { - auto const nodeBehind = runSimulation( - false, std::chrono::milliseconds::zero(), -driftOffset); + auto const nodeBehind = runSimulation(false, -driftOffset); REQUIRE(nodeBehind.driftedNodeFallbacks >= LEDGERS_TO_RUN - nodeBehind.driftedLedSlots - FALLBACK_SLACK); @@ -6617,122 +5340,11 @@ TEST_CASE("consensus close time trigger timer", "[herder][!hide]") REQUIRE(nodeBehind.maxOtherNodeFallbacks <= nodeBehind.driftedLedSlots + FALLBACK_SLACK); } - - // Long nomination does not cause timer fallback - { - auto const nominationDelay = std::chrono::milliseconds(5000); - auto const slowNomination = runSimulation(false, nominationDelay); - REQUIRE(slowNomination.sawNominationTimeout); - REQUIRE(slowNomination.totalFallbacks == 0); - } -} - -TEST_CASE("trigger timer switches anchor at protocol 28 upgrade", - "[herder][upgrades][!hide]") -{ - if (!triggerTimerProtocolSupported()) - { - return; - } - - auto const upgradeVersion = - static_cast(CONSENSUS_CLOSE_TIME_TRIGGER_PROTOCOL_VERSION); - - // With a delayed nomination emit, the prepare-start timer paces ledgers - // at roughly expectedClose + nominationEmitDelay (nomination happens - // before the anchor point), while the consensus-close-time timer absorbs - // the nomination delay and paces at expectedClose. This delta helps us - // measure the timer change after the upgrade. - constexpr uint32_t LEDGERS_TO_MEASURE = 8; - auto const nominationEmitDelay = std::chrono::milliseconds(1000); - - struct RunResult - { - std::chrono::milliseconds preUpgrade; - std::chrono::milliseconds postUpgrade; - }; - - auto runSimulation = [&](bool forcePrepareStartTimer) -> RunResult { - // Start the network one protocol before the trigger-timer switch. - auto simulation = makeTriggerTimerSimulation( - forcePrepareStartTimer, nominationEmitDelay, - std::chrono::milliseconds::zero(), upgradeVersion - 1); - auto nodes = simulation->getNodes(); - auto const expectedClose = simulation->getExpectedLedgerCloseTime(); - - auto lclVersion = [](Application::pointer const& node) { - return node->getLedgerManager() - .getLastClosedLedgerHeader() - .header.ledgerVersion; - }; - - // Measure the cadence on the pre-28 protocol. - simulation->crankUntil( - [&]() { return simulation->haveAllExternalized(3, 1); }, - 10 * expectedClose, false); - auto const preUpgrade = closeLedgers(simulation, LEDGERS_TO_MEASURE); - for (auto const& node : nodes) - { - REQUIRE(lclVersion(node) == upgradeVersion - 1); - } - - // Upgrade to protocol 28 - Upgrades::UpgradeParameters scheduledUpgrades; - scheduledUpgrades.mUpgradeTime = - VirtualClock::from_time_t(nodes[0] - ->getLedgerManager() - .getLastClosedLedgerHeader() - .header.scpValue.closeTime); - scheduledUpgrades.mProtocolVersion = upgradeVersion; - for (auto const& node : nodes) - { - node->getHerder().setUpgrades(scheduledUpgrades); - } - - // Crank until every node has closed the upgrade ledger, then one - // more ledger so the measured window is fully post-upgrade. - simulation->crankUntil( - [&]() { - return std::all_of(nodes.begin(), nodes.end(), - [&](Application::pointer const& node) { - return lclVersion(node) == - upgradeVersion; - }); - }, - 10 * expectedClose, false); - closeLedgers(simulation, 1); - - auto const postUpgrade = closeLedgers(simulation, LEDGERS_TO_MEASURE); - for (auto const& node : nodes) - { - REQUIRE(lclVersion(node) == upgradeVersion); - } - - return {preUpgrade, postUpgrade}; - }; - - // Expected cadence saving is nominationEmitDelay per ledger; splitting - // pass/fail at half of it tolerates scheduling noise in both directions. - auto const cadenceMargin = LEDGERS_TO_MEASURE * nominationEmitDelay / 2; - - // Crossing the boundary switches to the consensus-close-time anchor: - // post-upgrade ledgers close significantly faster. - { - auto const result = runSimulation(false); - REQUIRE(result.postUpgrade + cadenceMargin < result.preUpgrade); - } - - // The override flag keeps the prepare-start anchor after the upgrade, so - // the cadence does not improve. - { - auto const result = runSimulation(true); - REQUIRE(result.postUpgrade + cadenceMargin > result.preUpgrade); - } } namespace stellar { -class EarlyNominationTestAccess +class LeaderBuildTestAccess { public: static VirtualClock::time_point @@ -6843,13 +5455,12 @@ class EarlyNominationTestAccess SecretKey::fromSeed(sha256(fmt::format("absent-peer-{}", i))) .getPublicKey()}; scp.updateLocalQuorumSet(qset); - if (scp.predictNominationLeaders( - lcl.header.ledgerSeq + 1, - xdr::xdr_to_opaque(lcl.header.scpValue), 1) - .count(scp.getLocalNodeID())) + herder.mPendingEnvelopes.addSCPQuorumSet( + scp.getLocalNode()->getQuorumSetHash(), qset); + herder.mPendingEnvelopes.forceRebuildQuorum(); + if (herder.getHerderSCPDriver().isLocalNodeRoundLeader( + lcl.header.ledgerSeq + 1, 1)) { - herder.mPendingEnvelopes.addSCPQuorumSet( - scp.getLocalNode()->getQuorumSetHash(), qset); return; } } @@ -6858,7 +5469,7 @@ class EarlyNominationTestAccess }; } -TEST_CASE("trigger fallback credits elapsed time through nomination", +TEST_CASE("trigger fallback credits elapsed time up to the first ballot", "[herder][trigger-construction]") { using namespace std::chrono; @@ -6872,23 +5483,23 @@ TEST_CASE("trigger fallback credits elapsed time through nomination", int expectedWaitMs; int expectedFallbacks; }; - auto scenario = - GENERATE(Scenario{350, 1900, 2100, 0, true, 0, 0}, - Scenario{350, 1900, 2100, 4000, true, 1800, 1}, - Scenario{350, 1900, 2100, -4000, true, 1800, 1}, - Scenario{350, 1900, 4100, 0, true, 0, 1}, - Scenario{350, 1900, 2100, 0, false, 1800, 1}, - Scenario{350, 1500, 1700, 0, true, 300, 0}, - // A slow first round need not execute a timeout callback - // before entering ballot. Its elapsed time is still real. - Scenario{100, 2000, 2250, 0, true, 0, 0}, - // Followers need no construction, but still wait on peers. - Scenario{0, 2250, 2450, 0, true, 0, 0}, - // Delayed timer callbacks must not hide elapsed time. - Scenario{100, 6500, 6700, 0, true, 0, 0}, - // Long nomination does not excuse arbitrary clock drift. - Scenario{100, 6500, 6700, 10000, true, 1800, 1}, - Scenario{100, 6500, 6700, -10000, true, 1800, 1}); + auto scenario = GENERATE( + Scenario{350, 1900, 2100, 0, true, 0, 0}, + Scenario{350, 1900, 2100, 4000, true, 1800, 1}, + Scenario{350, 1900, 2100, -4000, true, 1800, 1}, + Scenario{350, 1900, 4100, 0, true, 0, 1}, + Scenario{350, 1900, 2100, 0, false, 1800, 1}, + Scenario{350, 1500, 1700, 0, true, 300, 0}, + // A slow first round need not execute a timeout callback + // before entering ballot. Its elapsed time is still real. + Scenario{100, 2000, 2250, 0, true, 0, 0}, + // Followers need no construction, but still wait on peers. + Scenario{0, 2250, 2450, 0, true, 0, 0}, + // Delayed timer callbacks must not hide elapsed time. + Scenario{100, 6500, 6700, 0, true, 0, 0}, + // A long wait for a proposal does not excuse arbitrary clock drift. + Scenario{100, 6500, 6700, 10000, true, 1800, 1}, + Scenario{100, 6500, 6700, -10000, true, 1800, 1}); CAPTURE(scenario.buildMs, scenario.closeMs, scenario.clockOffsetMs, scenario.recordTrigger); VirtualClock clock; @@ -6907,15 +5518,14 @@ TEST_CASE("trigger fallback credits elapsed time through nomination", herder.setTrackingSCPState(slot, value, true); if (scenario.recordTrigger) { - driver.recordNominationTrigger(slot); + driver.recordTrigger(slot); } clock.setCurrentVirtualTime(origin + milliseconds(scenario.buildMs)); - driver.recordSCPEvent(slot, true); clock.setCurrentVirtualTime(origin + milliseconds(scenario.ballotMs)); - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); clock.setCurrentVirtualTime(origin + milliseconds(scenario.closeMs)); clock.setSystemTimeOffset(milliseconds(scenario.clockOffsetMs)); - REQUIRE(driver.getNominationTimeouts(slot).value() == 0); + REQUIRE(driver.getLeaderTimeouts(slot).value() == 0); REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(scenario.recordTrigger ? scenario.ballotMs : 0)); REQUIRE(driver.getTriggerToBallotDuration(slot + 1) == @@ -6926,7 +5536,7 @@ TEST_CASE("trigger fallback credits elapsed time through nomination", auto const before = fallback.count(); auto deadline = std::max( clock.now(), - EarlyNominationTestAccess::triggerAnchor(herder, slot) + seconds(2)); + LeaderBuildTestAccess::triggerAnchor(herder, slot) + seconds(2)); REQUIRE(duration_cast(deadline - clock.now()).count() == scenario.expectedWaitMs); REQUIRE(fallback.count() - before == scenario.expectedFallbacks); @@ -6946,7 +5556,7 @@ TEST_CASE("trigger work is recorded by the proposal path", SecretKey::pseudoRandomForTesting().getPublicKey()}; auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); - EarlyNominationTestAccess::selectLocalFirstLeader(herder); + LeaderBuildTestAccess::selectLocalFirstLeader(herder); auto& driver = herder.getHerderSCPDriver(); auto const slot = app->getLedgerManager().getLastClosedLedgerNum() + 1; herder.mGetTopTransactionsForTesting = [&](size_t) { @@ -6957,19 +5567,16 @@ TEST_CASE("trigger work is recorded by the proposal path", return std::vector{}; }; herder.triggerNextLedger(slot, true); - // Until ballot starts there is no completed interval to credit. - REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds::zero()); - clock.setCurrentVirtualTime(clock.now() + milliseconds(100)); - driver.recordSCPEvent(slot, false); - REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(500)); + // The round leader proposes straight into the ballot protocol: the + // interval ends when its own build does. + REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(400)); clock.setCurrentVirtualTime(clock.now() + seconds(1)); - driver.recordNominationTrigger(slot); - driver.recordSCPEvent(slot, true); - REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(500)); + driver.recordTrigger(slot); + REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(400)); herder.mGetTopTransactionsForTesting = nullptr; } -TEST_CASE("skipped nomination does not start the trigger interval", +TEST_CASE("skipped trigger does not start the trigger interval", "[herder][trigger-construction]") { using namespace std::chrono; @@ -6977,7 +5584,7 @@ TEST_CASE("skipped nomination does not start the trigger interval", auto cfg = getTestConfig(); cfg.HTTP_PORT = 0; cfg.MANUAL_CLOSE = true; - // A second validator keeps our own nomination from externalizing. + // A second validator keeps our own proposal from externalizing. cfg.QUORUM_SET.threshold = 2; cfg.QUORUM_SET.validators = { cfg.NODE_SEED.getPublicKey(), @@ -6996,7 +5603,7 @@ TEST_CASE("skipped nomination does not start the trigger interval", clock.setSystemTimeOffset(-(Herder::MAX_TIME_SLIP_SECONDS + seconds(10))); // Counts proposal constructions, which happen only for triggers that go - // on to nominate. + // on to start the slot. int builds = 0; herder.mGetTopTransactionsForTesting = [&](size_t) { ++builds; @@ -7009,7 +5616,7 @@ TEST_CASE("skipped nomination does not start the trigger interval", // Peers move the slot into ballot while we wait. None of that waiting is // trigger-to-ballot work. clock.setCurrentVirtualTime(clock.now() + milliseconds(700)); - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds::zero()); // With the clock back in sync, the same path builds a proposal and its @@ -7020,7 +5627,7 @@ TEST_CASE("skipped nomination does not start the trigger interval", herder.triggerNextLedger(nextSlot, true); REQUIRE(builds == 1); clock.setCurrentVirtualTime(clock.now() + milliseconds(300)); - driver.recordSCPEvent(nextSlot, false); + driver.recordBallotStart(nextSlot); REQUIRE(driver.getTriggerToBallotDuration(nextSlot) == milliseconds(300)); herder.mGetTopTransactionsForTesting = nullptr; } @@ -7037,16 +5644,14 @@ TEST_CASE("trigger work excludes time after entering ballot", auto& driver = static_cast(app->getHerder()).getHerderSCPDriver(); uint64_t const slot = 100; - driver.recordNominationTrigger(slot); + driver.recordTrigger(slot); clock.setCurrentVirtualTime(clock.now() + milliseconds(100)); - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); clock.setCurrentVirtualTime(clock.now() + milliseconds(300)); - driver.recordSCPEvent(slot, true); REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(100)); - driver.recordSCPEvent(slot + 1, false); + driver.recordBallotStart(slot + 1); clock.setCurrentVirtualTime(clock.now() + milliseconds(100)); - driver.recordNominationTrigger(slot + 1); - driver.recordSCPEvent(slot + 1, true); + driver.recordTrigger(slot + 1); REQUIRE(driver.getTriggerToBallotDuration(slot + 1) == milliseconds::zero()); } @@ -7067,19 +5672,17 @@ TEST_CASE("trigger allowance is independent of timeout callback ordering", auto const ct = VirtualClock::to_time_t(clock.system_now()) + 1; auto const origin = VirtualClock::from_time_t(ct); clock.setCurrentVirtualTime(origin); - driver.recordNominationTrigger(slot); + driver.recordTrigger(slot); clock.setCurrentVirtualTime(origin + milliseconds(100)); - driver.recordSCPEvent(slot, true); bool timerFired = false; - driver.setupTimer(slot, Slot::NOMINATION_TIMER, seconds(2), + driver.setupTimer(slot, Slot::LEADER_TIMER, seconds(2), [&] { timerFired = true; }); - // Both events become runnable while the main thread is occupied. Entering - // ballot before dispatching the expired timer suppresses its timeout count. + // Both events become runnable while the main thread is occupied. clock.setCurrentVirtualTime(origin + milliseconds(2500)); if (ballotBeforeTimeout) { - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); } for (int i = 0; i < 100 && !timerFired; ++i) { @@ -7089,18 +5692,16 @@ TEST_CASE("trigger allowance is independent of timeout callback ordering", REQUIRE(clock.system_now() == origin + milliseconds(2500)); if (!ballotBeforeTimeout) { - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); } - REQUIRE(driver.getNominationTimeouts(slot) == - (ballotBeforeTimeout ? 0 : 1)); + REQUIRE(driver.getLeaderTimeouts(slot) == 1); REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(2500)); StellarValue value; value.closeTime = ct; herder.setTrackingSCPState(slot, value, true); clock.setCurrentVirtualTime(origin + milliseconds(2700)); - REQUIRE(EarlyNominationTestAccess::triggerAnchor(herder, slot) + - seconds(2) <= + REQUIRE(LeaderBuildTestAccess::triggerAnchor(herder, slot) + seconds(2) <= clock.now()); } @@ -7133,10 +5734,9 @@ TEST_CASE("trigger anchor keeps millisecond close times", // 600 ms later, so the allowance is exactly 600 ms. auto const origin = getConsensusTime(value).toSystemTime(); clock.setCurrentVirtualTime(origin); - driver.recordNominationTrigger(slot); - driver.recordSCPEvent(slot, true); + driver.recordTrigger(slot); clock.setCurrentVirtualTime(origin + milliseconds(600)); - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); REQUIRE(driver.getTriggerToBallotDuration(slot) == milliseconds(600)); auto metrics = app->getMetrics().GetAllMetrics(); @@ -7145,10 +5745,9 @@ TEST_CASE("trigger anchor keeps millisecond close times", auto const before = fallback.count(); auto waitAt = [&](milliseconds sinceClose) { clock.setCurrentVirtualTime(origin + sinceClose); - auto deadline = - std::max(clock.now(), - EarlyNominationTestAccess::triggerAnchor(herder, slot) + - seconds(2)); + auto deadline = std::max( + clock.now(), + LeaderBuildTestAccess::triggerAnchor(herder, slot) + seconds(2)); return duration_cast(deadline - clock.now()); }; @@ -7158,7 +5757,8 @@ TEST_CASE("trigger anchor keeps millisecond close times", REQUIRE(fallback.count() == before); // Elapsed time up to the target plus the allowance is still explained by - // nomination; one millisecond more is attributed to clock drift. + // the wait for a proposal; one millisecond more is attributed to clock + // drift. REQUIRE(waitAt(milliseconds(2600)) == milliseconds::zero()); REQUIRE(fallback.count() == before); REQUIRE(waitAt(milliseconds(2601)) == milliseconds::zero()); @@ -7180,27 +5780,31 @@ TEST_CASE("trigger-to-ballot time is recorded at first ballot entry", app->getMetrics().NewTimer({"scp", "timing", "trigger-to-ballot"}); REQUIRE(timer.count() == 0); - // Construction and nomination both count, up to the first ballot entry. + // Construction and the wait for the leader both count, up to the first + // ballot entry. uint64_t const slot = 100; - driver.recordNominationTrigger(slot); + driver.recordTrigger(slot); clock.setCurrentVirtualTime(clock.now() + milliseconds(400)); - driver.recordSCPEvent(slot, true); clock.setCurrentVirtualTime(clock.now() + milliseconds(100)); - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); REQUIRE(timer.count() == 1); REQUIRE(timer.max() == Approx(500.0)); // Later ballot events of the same slot are not new intervals. clock.setCurrentVirtualTime(clock.now() + milliseconds(50)); - driver.recordSCPEvent(slot, false); + driver.recordBallotStart(slot); REQUIRE(timer.count() == 1); - // No interval without a local trigger, or when ballot started first. - driver.recordSCPEvent(slot + 1, false); - driver.recordSCPEvent(slot + 2, false); - driver.recordNominationTrigger(slot + 2); - driver.recordSCPEvent(slot + 2, true); + // No interval without a local trigger. + driver.recordBallotStart(slot + 1); REQUIRE(timer.count() == 1); + + // A trigger that fires after the node already voted for the round + // leader's proposal records a zero interval. + driver.recordBallotStart(slot + 2); + driver.recordTrigger(slot + 2); + REQUIRE(timer.count() == 2); + REQUIRE(timer.min() == Approx(0.0)); } TEST_CASE("trigger lateness and immediate triggers are recorded", @@ -7255,7 +5859,7 @@ TEST_CASE("trigger lateness and immediate triggers are recorded", clock.setCurrentVirtualTime(std::max(overdue, clock.now()) + milliseconds(500)); auto const immediateCount = immediate.count(); - EarlyNominationTestAccess::setupTrigger(herder); + LeaderBuildTestAccess::setupTrigger(herder); REQUIRE(immediate.count() == immediateCount + 1); REQUIRE(herder.getTriggerTimer().expiry_time() == clock.now()); testutil::crankUntil( @@ -7263,7 +5867,7 @@ TEST_CASE("trigger lateness and immediate triggers are recorded", REQUIRE(lm.getLastClosedLedgerNum() > lcl); } -TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") +TEST_CASE("prepare proposal before trigger", "[herder][early-proposal]") { auto const soroban = GENERATE(false, true); VirtualClock clock; @@ -7276,7 +5880,7 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") cfg.GENESIS_TEST_ACCOUNT_COUNT = 10; cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE = 1; // Require another node to externalize, so this test can inspect the value - // we nominate without immediately closing it and scheduling another slot. + // we propose without immediately closing it and scheduling another slot. cfg.QUORUM_SET.threshold = 2; cfg.QUORUM_SET.validators = { cfg.NODE_SEED.getPublicKey(), @@ -7289,7 +5893,7 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") config.mLedgerMaxTxCount = 1; }); } - EarlyNominationTestAccess::selectLocalFirstLeader(herder); + LeaderBuildTestAccess::selectLocalFirstLeader(herder); auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); // The Soroban upgrade helper closes ledgers at its fixed test date. The // sub-second offset makes the prepared close time carry milliseconds once @@ -7298,7 +5902,7 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") VirtualClock::from_time_t(lcl.header.scpValue.closeTime + 1000) + std::chrono::milliseconds(375)); auto const seq = lcl.header.ledgerSeq + 1; - herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + herder.getHerderSCPDriver().recordBallotStart(lcl.header.ledgerSeq); std::vector mempool; auto alice = txtest::getGenesisAccount(*app, 0); @@ -7339,18 +5943,16 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") REQUIRE(trigger > clock.now()); testutil::crankUntil( app, - [&]() { - return EarlyNominationTestAccess::prepared(herder) != nullptr; - }, + [&]() { return LeaderBuildTestAccess::prepared(herder) != nullptr; }, std::chrono::seconds(1)); REQUIRE(clock.now() < trigger); REQUIRE(pulls == 1); - auto prepared = EarlyNominationTestAccess::prepared(herder); - auto const preparedCloseTime = EarlyNominationTestAccess::closeTime(herder); + auto prepared = LeaderBuildTestAccess::prepared(herder); + auto const preparedCloseTime = LeaderBuildTestAccess::closeTime(herder); REQUIRE(prepared); REQUIRE(prepared->sizeTxTotal() == 1); REQUIRE(preparedCloseTime == expectedConsensusTime); - REQUIRE(herder.getSCP().getNominationLeaders(seq).empty()); + REQUIRE(herder.getSCP().getLeaderRound(seq) == 0); REQUIRE(herder.getSCP().getLatestMessagesSend(seq).empty()); auto known = herder.getTxSet(prepared->getContentsHash()); REQUIRE(std::get(known) == nullptr); @@ -7361,13 +5963,10 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") // used to validate time bounds, not acquire the new wall-clock time. clock.setCurrentVirtualTime(trigger + std::chrono::seconds(1)); testutil::crankUntil( - app, - [&]() { - return !herder.getSCP().getNominationLeaders(seq).empty(); - }, + app, [&]() { return herder.getSCP().getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); REQUIRE(pulls == 1); - REQUIRE(!EarlyNominationTestAccess::prepared(herder)); + REQUIRE(!LeaderBuildTestAccess::prepared(herder)); REQUIRE(std::get( herder.getTxSet(prepared->getContentsHash())) == prepared); // The value can be checked independently of the production cache. @@ -7387,10 +5986,12 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") std::chrono::seconds(1)); auto messages = herder.getSCP().getLatestMessagesSend(seq); REQUIRE(!messages.empty()); - auto const& votes = messages.front().statement.pledges.nominate().votes; - REQUIRE(votes.size() == 1); + REQUIRE(messages.front().statement.pledges.type() == SCP_ST_PREPARE); + auto const& ballot = + messages.front().statement.pledges.prepare().ballot; + REQUIRE(ballot.counter == 1); StellarValue value; - xdr::xdr_from_opaque(votes.front(), value); + xdr::xdr_from_opaque(ballot.value, value); REQUIRE(value.txSetHash == prepared->getContentsHash()); REQUIRE(getConsensusTime(value) == preparedCloseTime); REQUIRE(pulls == 1); @@ -7402,7 +6003,7 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") // claim that our time-bound transaction is valid for that time. herder.triggerNextLedger(seq, true); REQUIRE(pulls == 2); - REQUIRE(!EarlyNominationTestAccess::prepared(herder)); + REQUIRE(!LeaderBuildTestAccess::prepared(herder)); REQUIRE(std::get( herder.getTxSet(prepared->getContentsHash())) == nullptr); } @@ -7410,32 +6011,32 @@ TEST_CASE("prepare nomination before trigger", "[herder][early-nomination]") { herder.externalizeValue(TxSetXDRFrame::makeEmpty(lcl), seq, preparedCloseTime, {}, cfg.NODE_SEED); - REQUIRE(EarlyNominationTestAccess::prepared(herder) != prepared); + REQUIRE(LeaderBuildTestAccess::prepared(herder) != prepared); auto const pullsAfterClose = pulls; - // A stale timer/caller cannot nominate the old slot or pull again. + // A stale timer/caller cannot propose for the old slot or pull again. herder.triggerNextLedger(seq, true); REQUIRE(pulls == pullsAfterClose); } SECTION("loss of sync discards preparation") { herder.lostSync(); - REQUIRE(!EarlyNominationTestAccess::prepared(herder)); + REQUIRE(!LeaderBuildTestAccess::prepared(herder)); herder.triggerNextLedger(seq, true); REQUIRE(pulls == 1); } SECTION("shutdown cancels preparation") { herder.shutdown(); - REQUIRE(!EarlyNominationTestAccess::prepared(herder)); + REQUIRE(!LeaderBuildTestAccess::prepared(herder)); } herder.mGetTopTransactionsForTesting = nullptr; } -TEST_CASE("only nomination leaders construct local proposals", - "[herder][early-nomination]") +TEST_CASE("only round leaders construct local proposals", + "[herder][early-proposal]") { - // A fixed, unanimous quorum gives all three nodes the same weights and - // leader ordering. Exercise each member as the local node, rather than + // A fixed, unanimous quorum gives all three nodes the same leader + // schedule. Exercise each member as the local node, rather than // hoping random peers cover every role for one fixed local priority. std::vector keys; SCPQuorumSet qset; @@ -7443,14 +6044,14 @@ TEST_CASE("only nomination leaders construct local proposals", for (int i = 0; i < 3; ++i) { keys.push_back(SecretKey::fromSeed( - sha256(fmt::format("early-nomination-node-{}", i)))); + sha256(fmt::format("early-proposal-node-{}", i)))); qset.validators.push_back(keys.back().getPublicKey()); } std::set checkedRoles; - std::optional previous; - std::optional> expectedFirst; - std::optional> expectedFirstTwo; + std::optional previous; + std::optional expectedFirst; + std::optional expectedSecond; for (int i = 0; i < 3; ++i) { VirtualClock clock; @@ -7467,36 +6068,38 @@ TEST_CASE("only nomination leaders construct local proposals", auto& scp = herder.getSCP(); auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto const seq = lcl.header.ledgerSeq + 1; - auto const value = xdr::xdr_to_opaque(lcl.header.scpValue); + // The schedule is seeded by the hash of the last closed ledger. if (!previous) { - previous = value; + previous = lcl.hash; } - REQUIRE(value == *previous); - herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + REQUIRE(lcl.hash == *previous); + auto& driver = herder.getHerderSCPDriver(); + herder.getHerderSCPDriver().recordBallotStart(lcl.header.ledgerSeq); size_t pulls = 0; herder.mGetTopTransactionsForTesting = [&](size_t) { ++pulls; return std::vector{}; }; - auto first = scp.predictNominationLeaders(seq, value, 1); - auto firstTwo = scp.predictNominationLeaders(seq, value, 2); - REQUIRE(first.size() == 1); - REQUIRE(firstTwo.size() == 2); + auto first = driver.getRoundLeader(seq, 1); + auto second = driver.getRoundLeader(seq, 2); + REQUIRE(first); + REQUIRE(second); + REQUIRE(*first != *second); if (!expectedFirst) { expectedFirst = first; - expectedFirstTwo = firstTwo; + expectedSecond = second; } - REQUIRE(first == *expectedFirst); - REQUIRE(firstTwo == *expectedFirstTwo); - int role = first.count(scp.getLocalNodeID()) ? 0 - : firstTwo.count(scp.getLocalNodeID()) ? 1 - : 2; + REQUIRE(*first == *expectedFirst); + REQUIRE(*second == *expectedSecond); + int role = *first == scp.getLocalNodeID() ? 0 + : *second == scp.getLocalNodeID() ? 1 + : 2; REQUIRE(checkedRoles.insert(role).second); CAPTURE(role); - EarlyNominationTestAccess::schedule(herder); - REQUIRE(EarlyNominationTestAccess::scheduled(herder) == (role == 0)); + LeaderBuildTestAccess::schedule(herder); + REQUIRE(LeaderBuildTestAccess::scheduled(herder) == (role == 0)); bool done = false; VirtualTimer stop(clock); stop.expires_from_now(std::chrono::milliseconds(1)); @@ -7506,42 +6109,42 @@ TEST_CASE("only nomination leaders construct local proposals", REQUIRE(done); REQUIRE(clock.now() < herder.getTriggerTimer().expiry_time()); REQUIRE(pulls == (role == 0 ? 1 : 0)); - REQUIRE(bool(EarlyNominationTestAccess::prepared(herder)) == - (role == 0)); - REQUIRE(scp.getNominationLeaders(seq).empty()); + REQUIRE(bool(LeaderBuildTestAccess::prepared(herder)) == (role == 0)); + REQUIRE(scp.getLeaderRound(seq) == 0); REQUIRE(scp.getLatestMessagesSend(seq).empty()); auto const trigger = herder.getTriggerTimer().expiry_time(); clock.setCurrentVirtualTime(trigger); auto const triggerSystemTime = clock.system_now(); testutil::crankUntil( - app, [&]() { return !scp.getNominationLeaders(seq).empty(); }, + app, [&]() { return scp.getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); - REQUIRE(scp.getNominationLeaders(seq) == first); + REQUIRE(scp.getLeaderRound(seq) == 1); // The first leader refreshes its empty early snapshot. Followers // pull the mempool once, only to pre-verify signatures; they do not // build or publish a set. REQUIRE(pulls == (role == 0 ? 2 : 1)); REQUIRE(scp.getLatestMessagesSend(seq).empty() == (role != 0)); - // With the first leader silent, real nomination timers eventually + // With the first leader silent, real leader timers eventually // promote each follower. It builds exactly then, using a current close // time, and its prepared/validated tx set is available for retrieval. testutil::crankUntil( app, [&]() { return !scp.getLatestMessagesSend(seq).empty(); }, std::chrono::seconds(30)); - REQUIRE(scp.getNominationLeaders(seq).count(scp.getLocalNodeID())); + REQUIRE(scp.getLeaderRound(seq) == static_cast(role + 1)); + REQUIRE(driver.isLocalNodeRoundLeader(seq, scp.getLeaderRound(seq))); REQUIRE(pulls == 2); auto messages = scp.getLatestMessagesSend(seq); - auto const& votes = messages.front().statement.pledges.nominate().votes; - REQUIRE(votes.size() == 1); + REQUIRE(messages.front().statement.pledges.type() == SCP_ST_PREPARE); + auto const& proposed = + messages.front().statement.pledges.prepare().ballot.value; StellarValue nominated; - xdr::xdr_from_opaque(votes.front(), nominated); + xdr::xdr_from_opaque(proposed, nominated); auto txSet = std::get( herder.getTxSet(nominated.txSetHash)); REQUIRE(txSet); REQUIRE(txSet->previousLedgerHash() == lcl.hash); - REQUIRE(herder.getHerderSCPDriver().validateValue(seq, votes.front(), - true) == + REQUIRE(driver.validateValue(seq, proposed, true) == SCPDriver::kFullyValidatedValue); if (role != 0) { @@ -7559,10 +6162,10 @@ TEST_CASE("only nomination leaders construct local proposals", } TEST_CASE("a follower promoted after losing sync still proposes", - "[herder][early-nomination]") + "[herder][early-proposal]") { // Unanimous three-node quorum with silent peers: the slot cannot - // externalize, so the local follower eventually loses sync. Nomination + // externalize, so the local follower eventually loses sync. Leader // timeouts must still let it propose a value of its own once promoted. std::vector keys; SCPQuorumSet qset; @@ -7570,7 +6173,7 @@ TEST_CASE("a follower promoted after losing sync still proposes", for (int i = 0; i < 3; ++i) { keys.push_back(SecretKey::fromSeed( - sha256(fmt::format("early-nomination-node-{}", i)))); + sha256(fmt::format("early-proposal-node-{}", i)))); qset.validators.push_back(keys.back().getPublicKey()); } @@ -7591,27 +6194,26 @@ TEST_CASE("a follower promoted after losing sync still proposes", auto& scp = herder.getSCP(); auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto const seq = lcl.header.ledgerSeq + 1; - if (scp.predictNominationLeaders( - seq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) - .count(scp.getLocalNodeID())) + auto& driver = herder.getHerderSCPDriver(); + if (driver.isLocalNodeRoundLeader(seq, 1)) { // The first leader proposes at the trigger; test a follower. continue; } checkedFollower = true; - herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + herder.getHerderSCPDriver().recordBallotStart(lcl.header.ledgerSeq); size_t pulls = 0; herder.mGetTopTransactionsForTesting = [&](size_t) { ++pulls; return std::vector{}; }; - EarlyNominationTestAccess::schedule(herder); + LeaderBuildTestAccess::schedule(herder); clock.setCurrentVirtualTime(herder.getTriggerTimer().expiry_time()); testutil::crankUntil( - app, [&]() { return !scp.getNominationLeaders(seq).empty(); }, + app, [&]() { return scp.getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); - REQUIRE(!scp.getNominationLeaders(seq).empty()); + REQUIRE(scp.getLeaderRound(seq) != 0); REQUIRE(scp.getLatestMessagesSend(seq).empty()); // Only the signature prewarm pull; no build yet. REQUIRE(pulls == 1); @@ -7623,29 +6225,29 @@ TEST_CASE("a follower promoted after losing sync still proposes", testutil::crankUntil( app, [&]() { return !scp.getLatestMessagesSend(seq).empty(); }, std::chrono::seconds(30)); - REQUIRE(scp.getNominationLeaders(seq).count(scp.getLocalNodeID())); + REQUIRE(driver.isLocalNodeRoundLeader(seq, scp.getLeaderRound(seq))); REQUIRE(pulls == 2); auto messages = scp.getLatestMessagesSend(seq); REQUIRE(!messages.empty()); - auto const& votes = messages.front().statement.pledges.nominate().votes; - REQUIRE(votes.size() == 1); - REQUIRE(herder.getHerderSCPDriver().validateValue(seq, votes.front(), - true) == - SCPDriver::kFullyValidatedValue); + REQUIRE(messages.front().statement.pledges.type() == SCP_ST_PREPARE); + REQUIRE(driver.validateValue( + seq, + messages.front().statement.pledges.prepare().ballot.value, + true) == SCPDriver::kFullyValidatedValue); herder.mGetTopTransactionsForTesting = nullptr; } REQUIRE(checkedFollower); } TEST_CASE("a leader build that completes a fetch can externalize", - "[herder][early-nomination]") + "[herder][early-proposal]") { // The local leader builds the very set a peer's ballot is waiting for (an // empty set is the same on every node). Publishing it completes the fetch - // inside SCP's nomination value supplier: the peer's CONFIRM is + // inside SCP's proposal value supplier: the peer's CONFIRM is // reprocessed there and, the peer being v-blocking, externalizes the slot - // before the supplier returns. Nomination must then stop cleanly without - // voting or emitting. + // before the supplier returns. The leader must then stop cleanly without + // proposing its own value. VirtualClock clock; clock.setCurrentVirtualTime(VirtualClock::from_time_t(1000)); auto cfg = getTestConfig(); @@ -7661,7 +6263,7 @@ TEST_CASE("a leader build that completes a fetch can externalize", auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); auto& scp = herder.getSCP(); - EarlyNominationTestAccess::selectLocalFirstLeader(herder); + LeaderBuildTestAccess::selectLocalFirstLeader(herder); auto const qset = scp.getLocalQuorumSet(); auto const qsetHash = scp.getLocalNode()->getQuorumSetHash(); std::optional peer; @@ -7679,7 +6281,7 @@ TEST_CASE("a leader build that completes a fetch can externalize", auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto const seq = lcl.header.ledgerSeq + 1; - herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + herder.getHerderSCPDriver().recordBallotStart(lcl.header.ledgerSeq); herder.mGetTopTransactionsForTesting = [](size_t) { return std::vector{}; }; @@ -7725,13 +6327,13 @@ TEST_CASE("a leader build that completes a fetch can externalize", REQUIRE(closed.header.scpValue == peerValue); for (auto const& env : scp.getLatestMessagesSend(seq)) { - REQUIRE(env.statement.pledges.type() != SCP_ST_NOMINATE); + REQUIRE(env.statement.pledges.type() == SCP_ST_EXTERNALIZE); } herder.mGetTopTransactionsForTesting = nullptr; } TEST_CASE("followers pre-verify mempool signatures at the trigger", - "[herder][early-nomination]") + "[herder][early-proposal]") { // Followers no longer validate their own mempool by building a set, so // the leader's set would be validated with a cold signature cache. At the @@ -7742,7 +6344,7 @@ TEST_CASE("followers pre-verify mempool signatures at the trigger", for (int i = 0; i < 3; ++i) { keys.push_back(SecretKey::fromSeed( - sha256(fmt::format("early-nomination-node-{}", i)))); + sha256(fmt::format("early-proposal-node-{}", i)))); qset.validators.push_back(keys.back().getPublicKey()); } @@ -7765,9 +6367,7 @@ TEST_CASE("followers pre-verify mempool signatures at the trigger", auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto const seq = lcl.header.ledgerSeq + 1; bool const leader = - scp.predictNominationLeaders( - seq, xdr::xdr_to_opaque(lcl.header.scpValue), 1) - .count(scp.getLocalNodeID()) != 0; + herder.getHerderSCPDriver().isLocalNodeRoundLeader(seq, 1); if (!checkedRoles.insert(leader).second) { continue; @@ -7797,7 +6397,7 @@ TEST_CASE("followers pre-verify mempool signatures at the trigger", { envelopes.push_back(tx->getEnvelope()); } - herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + herder.getHerderSCPDriver().recordBallotStart(lcl.header.ledgerSeq); herder.mGetTopTransactionsForTesting = [&](size_t) { return envelopes; }; @@ -7807,12 +6407,12 @@ TEST_CASE("followers pre-verify mempool signatures at the trigger", PubKeyUtils::flushVerifySigCacheCounts(hits, misses); size_t const signatures = txs.size() + 1 /* op source */ + 1 /* fee bump's inner tx */; - EarlyNominationTestAccess::schedule(herder); + LeaderBuildTestAccess::schedule(herder); clock.setCurrentVirtualTime(herder.getTriggerTimer().expiry_time()); testutil::crankUntil( - app, [&]() { return !scp.getNominationLeaders(seq).empty(); }, + app, [&]() { return scp.getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); - REQUIRE(!scp.getNominationLeaders(seq).empty()); + REQUIRE(scp.getLeaderRound(seq) != 0); if (leader) { @@ -7825,7 +6425,7 @@ TEST_CASE("followers pre-verify mempool signatures at the trigger", } // Background tasks verify every signature once. Wait in real time - // without cranking: virtual nomination timeouts would promote this + // without cranking: virtual leader timeouts would promote this // follower, whose build verifies signatures too. uint64_t verified = 0; auto const deadline = @@ -7929,25 +6529,24 @@ TEST_CASE("signature prewarm at the ballot covers the next ledger", ballot.counter = 1; ballot.value = value; herder.getHerderSCPDriver().startedBallotProtocol(slot, ballot); - REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == 0); + REQUIRE(LeaderBuildTestAccess::prewarmLedger(herder) == 0); testutil::crankUntil( app, [&]() { - return EarlyNominationTestAccess::prewarmLedger(herder) == slot + 1; + return LeaderBuildTestAccess::prewarmLedger(herder) == slot + 1; }, std::chrono::seconds(1)); - REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == slot + 1); + REQUIRE(LeaderBuildTestAccess::prewarmLedger(herder) == slot + 1); REQUIRE(requested == - 2 * EarlyNominationTestAccess::maxTxSetCandidates(herder) + - valueTxs); - REQUIRE(EarlyNominationTestAccess::waitForPrewarm(herder)); + 2 * LeaderBuildTestAccess::maxTxSetCandidates(herder) + valueTxs); + REQUIRE(LeaderBuildTestAccess::waitForPrewarm(herder)); // N externalizes and closes; the prewarm for N + 1 is kept. The overlay // drops N's transactions from the mempool. herder.externalizeValue(valueSet, slot, closeTime, emptyUpgradeSteps, cfg.NODE_SEED); REQUIRE(lm.getLastClosedLedgerNum() == slot); - REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == slot + 1); + REQUIRE(LeaderBuildTestAccess::prewarmLedger(herder) == slot + 1); mempool.erase(mempool.begin(), mempool.begin() + valueTxs); auto const nextLcl = lm.getLastClosedLedgerHeader(); auto const nextCloseTime = makeConsensusTime(app->timeNow() + 2); @@ -7979,7 +6578,7 @@ TEST_CASE("signature prewarm at the ballot covers the next ledger", { auto [hits0, misses0] = lookups("build"); auto txSet = - EarlyNominationTestAccess::build(herder, slot + 1, nextCloseTime); + LeaderBuildTestAccess::build(herder, slot + 1, nextCloseTime); REQUIRE(txSet->sizeTxTotal() == 16); checkLookups("build", hits0, misses0, target); } @@ -8005,7 +6604,7 @@ TEST_CASE("signature prewarm at the ballot covers the next ledger", } // Validation and the build stop the prewarm and record what it did: the // 16 transactions of N's value and the next 20, each verified once. - REQUIRE(EarlyNominationTestAccess::prewarmLedger(herder) == 0); + REQUIRE(LeaderBuildTestAccess::prewarmLedger(herder) == 0); REQUIRE(prewarmTxs.count() == txs0 + valueTxs + target); REQUIRE(prewarmVerified.count() == verified0 + valueTxs + target); REQUIRE(prewarmIncomplete.count() == incomplete0); @@ -8013,7 +6612,7 @@ TEST_CASE("signature prewarm at the ballot covers the next ledger", } TEST_CASE("early underfilled proposals refresh at the trigger", - "[herder][early-nomination]") + "[herder][early-proposal]") { auto const invalidFirst = GENERATE(false, true); VirtualClock clock; @@ -8030,10 +6629,10 @@ TEST_CASE("early underfilled proposals refresh at the trigger", SecretKey::pseudoRandomForTesting().getPublicKey()}; auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); - EarlyNominationTestAccess::selectLocalFirstLeader(herder); + LeaderBuildTestAccess::selectLocalFirstLeader(herder); auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto const seq = lcl.header.ledgerSeq + 1; - herder.getHerderSCPDriver().recordSCPEvent(lcl.header.ledgerSeq, false); + herder.getHerderSCPDriver().recordBallotStart(lcl.header.ledgerSeq); auto alice = txtest::getGenesisAccount(*app, 0); auto bob = txtest::getGenesisAccount(*app, 1); auto tx = alice.tx({payment(bob, 100)}); @@ -8051,24 +6650,21 @@ TEST_CASE("early underfilled proposals refresh at the trigger", ++pulls; return mempool; }; - EarlyNominationTestAccess::schedule(herder); + LeaderBuildTestAccess::schedule(herder); testutil::crankUntil( app, - [&]() { - return EarlyNominationTestAccess::prepared(herder) != nullptr; - }, + [&]() { return LeaderBuildTestAccess::prepared(herder) != nullptr; }, std::chrono::seconds(1)); REQUIRE(pulls == 1); - REQUIRE(EarlyNominationTestAccess::prepared(herder)->sizeTxTotal() == 0); + REQUIRE(LeaderBuildTestAccess::prepared(herder)->sizeTxTotal() == 0); // Transactions arrive while we are waiting for the normal trigger. mempool = {tx->getEnvelope(), extra->getEnvelope()}; clock.setCurrentVirtualTime(herder.getTriggerTimer().expiry_time()); testutil::crankUntil( - app, - [&]() { return !herder.getSCP().getNominationLeaders(seq).empty(); }, + app, [&]() { return herder.getSCP().getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); REQUIRE(pulls == 2); - // Check the actual set cached for nomination, independently of benchmark + // Check the actual set cached for the proposal, independently of benchmark // instrumentation. Both newly arrived transactions must be present. auto expected = makeTxSetFromTransactions(TxFrameList{tx, extra}, *app, ApplyTimeOffset{}) @@ -8078,12 +6674,12 @@ TEST_CASE("early underfilled proposals refresh at the trigger", herder.getTxSet(expected->getContentsHash())); REQUIRE(nominated); REQUIRE(nominated->sizeTxTotal() == 2); - REQUIRE(!EarlyNominationTestAccess::prepared(herder)); + REQUIRE(!LeaderBuildTestAccess::prepared(herder)); herder.mGetTopTransactionsForTesting = nullptr; } TEST_CASE("early preparation respects manual and immediately due triggers", - "[herder][early-nomination]") + "[herder][early-proposal]") { auto const manual = GENERATE(false, true); VirtualClock clock; @@ -8098,7 +6694,7 @@ TEST_CASE("early preparation respects manual and immediately due triggers", SecretKey::pseudoRandomForTesting().getPublicKey()}; auto app = createTestApplication(clock, cfg); auto& herder = static_cast(app->getHerder()); - EarlyNominationTestAccess::selectLocalFirstLeader(herder); + LeaderBuildTestAccess::selectLocalFirstLeader(herder); auto const seq = app->getLedgerManager().getLastClosedLedgerNum() + 1; size_t pulls = 0; herder.mGetTopTransactionsForTesting = [&](size_t) { @@ -8107,29 +6703,26 @@ TEST_CASE("early preparation respects manual and immediately due triggers", }; if (manual) { - REQUIRE(!EarlyNominationTestAccess::scheduled(herder)); + REQUIRE(!LeaderBuildTestAccess::scheduled(herder)); REQUIRE(herder.getTriggerTimer().seq() == 0); herder.triggerNextLedger(seq, true); } else { - EarlyNominationTestAccess::schedule(herder); + LeaderBuildTestAccess::schedule(herder); // No previous prepare timestamp: the normal fallback triggers now. REQUIRE(herder.getTriggerTimer().expiry_time() == clock.now()); // A due trigger builds itself. Preparing too could build twice: the // two timers would fire at the same instant in either order. - REQUIRE(!EarlyNominationTestAccess::scheduled(herder)); + REQUIRE(!LeaderBuildTestAccess::scheduled(herder)); testutil::crankUntil( - app, - [&]() { - return !herder.getSCP().getNominationLeaders(seq).empty(); - }, + app, [&]() { return herder.getSCP().getLeaderRound(seq) != 0; }, std::chrono::seconds(1)); } REQUIRE(pulls == 1); - REQUIRE(!EarlyNominationTestAccess::prepared(herder)); - REQUIRE(!EarlyNominationTestAccess::scheduled(herder)); - REQUIRE(!herder.getSCP().getNominationLeaders(seq).empty()); + REQUIRE(!LeaderBuildTestAccess::prepared(herder)); + REQUIRE(!LeaderBuildTestAccess::scheduled(herder)); + REQUIRE(herder.getSCP().getLeaderRound(seq) != 0); herder.mGetTopTransactionsForTesting = nullptr; } @@ -8553,7 +7146,7 @@ TEST_CASE("leader builds validate only the top of the mempool pull", }; auto const lcl = lm.getLastClosedLedgerHeader(); - auto [txSet, invalid] = EarlyNominationTestAccess::buildWithInvalid( + auto [txSet, invalid] = LeaderBuildTestAccess::buildWithInvalid( herder, lcl.header.ledgerSeq + 1, getConsensusTime(lcl.header.scpValue).next(lcl.header.ledgerVersion)); herder.mGetTopTransactionsForTesting = nullptr; @@ -8693,8 +7286,7 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") { CALLGRIND_START_INSTRUMENTATION; } - auto txSet = - EarlyNominationTestAccess::build(herder, seq, closeTime); + auto txSet = LeaderBuildTestAccess::build(herder, seq, closeTime); if (cold) { CALLGRIND_STOP_INSTRUMENTATION; @@ -8724,7 +7316,7 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") // A follower validating the received set (cold: calibration // against perfnet's herder.txset.validate). auto const& lastSet = - EarlyNominationTestAccess::build(herder, seq, closeTime); + LeaderBuildTestAccess::build(herder, seq, closeTime); for (uint32_t rep = 0; rep < reps; ++rep) { auto received = TxSetXDRFrame::makeFromWire( @@ -8764,23 +7356,22 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") auto& buildMisses = metrics.NewMeter( {"herder", "txset", "build-sig-cache-miss"}, "lookup"); auto const lastSet = - EarlyNominationTestAccess::build(herder, seq, closeTime); + LeaderBuildTestAccess::build(herder, seq, closeTime); uint64_t misses = 0; uint64_t validateMisses = 0; for (uint32_t rep = 0; rep < reps; ++rep) { PubKeyUtils::clearVerifySigCache(); auto const durationBefore = prewarmDuration.sum(); - EarlyNominationTestAccess::prewarm(herder, seq); - REQUIRE(EarlyNominationTestAccess::waitForPrewarm(herder)); + LeaderBuildTestAccess::prewarm(herder, seq); + REQUIRE(LeaderBuildTestAccess::waitForPrewarm(herder)); for (auto const& phase : phases) { metrics.NewTimer({"herder", "txset", phase}).Clear(); } auto const missesBefore = buildMisses.count(); // The build stops the (finished) prewarm and records it. - auto txSet = - EarlyNominationTestAccess::build(herder, seq, closeTime); + auto txSet = LeaderBuildTestAccess::build(herder, seq, closeTime); REQUIRE(txSet->sizeTxTotal() == std::min(sorobanCap, pull)); misses = buildMisses.count() - missesBefore; samples["prewarm (background, before the build)"].push_back( @@ -8794,8 +7385,8 @@ TEST_CASE("leader tx set build costs", "[herder][.][benchmark]") auto received = TxSetXDRFrame::makeFromWire( GeneralizedTransactionSet(lastSet->getGeneralizedXDR())); PubKeyUtils::clearVerifySigCache(); - EarlyNominationTestAccess::prewarm(herder, seq); - REQUIRE(EarlyNominationTestAccess::waitForPrewarm(herder)); + LeaderBuildTestAccess::prewarm(herder, seq); + REQUIRE(LeaderBuildTestAccess::waitForPrewarm(herder)); uint64_t hits0 = 0, misses0 = 0, hits1 = 0, misses1 = 0; PubKeyUtils::getVerifySigCacheCounts(hits0, misses0); auto const start = std::chrono::steady_clock::now(); diff --git a/src/herder/test/LeaderRoundsTests.cpp b/src/herder/test/LeaderRoundsTests.cpp new file mode 100644 index 000000000..ad1db120e --- /dev/null +++ b/src/herder/test/LeaderRoundsTests.cpp @@ -0,0 +1,221 @@ +// 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 + +// Network-level tests of consensus without nomination: the leader of each +// round comes from a deterministic schedule seeded by the hash of the last +// closed ledger header (see herder/LeaderSchedule.h). + +#include "crypto/SHA.h" +#include "herder/Herder.h" +#include "herder/HerderImpl.h" +#include "herder/LeaderSchedule.h" +#include "ledger/LedgerHeaderUtils.h" +#include "ledger/LedgerManager.h" +#include "main/Application.h" +#include "simulation/Simulation.h" +#include "simulation/Topologies.h" +#include "test/Catch2.h" +#include "test/test.h" +#include "util/Logging.h" +#include "util/MetricsRegistry.h" +#include "util/XDROperators.h" +#include +#include +#include + +using namespace stellar; + +namespace +{ +Config +acceleratedConfig(int configNum) +{ + auto cfg = getTestConfig(configNum); + cfg.ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING = true; + return cfg; +} + +struct ClosedLedger +{ + Hash mPreviousHash; + NodeID mProposer; +}; + +// Cranks until `app` closed `lastLedger`, recording for every ledger it +// closes on the way the hash of the ledger before it and the node that signed +// the externalized value (its proposer). +std::map +crankAndRecord(Simulation::pointer const& simulation, + Application::pointer const& app, uint32_t lastLedger, + std::chrono::seconds timeout) +{ + std::map res; + auto& lm = app->getLedgerManager(); + auto prev = lm.getLastClosedLedgerHeader(); + auto const deadline = std::chrono::steady_clock::now() + timeout; + while (lm.getLastClosedLedgerNum() < lastLedger) + { + REQUIRE(std::chrono::steady_clock::now() < deadline); + simulation->crankForAtMost(std::chrono::milliseconds(20), false); + auto cur = lm.getLastClosedLedgerHeader(); + if (cur.header.ledgerSeq == prev.header.ledgerSeq + 1) + { + REQUIRE(cur.header.previousLedgerHash == prev.hash); + res[cur.header.ledgerSeq] = ClosedLedger{ + prev.hash, getLcValueSignature(cur.header.scpValue).nodeID}; + } + prev = cur; + } + return res; +} + +int64_t +leaderTimeouts(Application::pointer const& app) +{ + return static_cast( + app->getMetrics().NewHistogram({"scp", "timeout", "leader"}).sum()); +} + +int64_t +ballotTimeouts(Application::pointer const& app) +{ + return static_cast( + app->getMetrics().NewHistogram({"scp", "timeout", "prepare"}).sum()); +} +} + +TEST_CASE("leader schedule drives consensus on a healthy network", + "[herder][leader-schedule][simulation]") +{ + Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); + auto simulation = Topologies::core(4, 0.75, networkID, acceleratedConfig); + simulation->startAllNodes(); + auto nodes = simulation->getNodes(); + auto nodeIDs = simulation->getNodeIDs(); + + uint32_t const lastLedger = 16; + auto closed = crankAndRecord(simulation, nodes[0], lastLedger, + std::chrono::seconds(300)); + simulation->crankUntil( + [&]() { return simulation->haveAllExternalized(lastLedger, 3); }, + std::chrono::seconds(60), false); + + int64_t timeouts = 0; + for (auto const& n : nodes) + { + timeouts += leaderTimeouts(n) + ballotTimeouts(n); + } + + // Every node computes the same schedule from the previous ledger's hash, + // and the value that externalizes is the one its round 1 leader proposed + // (unless a round timed out somewhere, e.g. on a busy test machine). + std::set proposers; + int64_t notFirstLeader = 0; + REQUIRE(closed.size() >= 10); + for (auto const& c : closed) + { + auto schedule = computeLeaderSchedule(c.second.mPreviousHash, nodeIDs); + REQUIRE(schedule.size() == 4); + REQUIRE(std::find(schedule.begin(), schedule.end(), + c.second.mProposer) != schedule.end()); + if (c.second.mProposer != schedule[0]) + { + ++notFirstLeader; + } + proposers.insert(c.second.mProposer); + } + INFO(fmt::format("ledgers {} not-first-leader {} timeouts {}", + closed.size(), notFirstLeader, timeouts)); + REQUIRE(notFirstLeader <= timeouts); + // most ledgers close in round 1 + REQUIRE(notFirstLeader * 2 < static_cast(closed.size())); + // leadership rotates + REQUIRE(proposers.size() >= 2); + + // the nodes agree on the schedule of the next ledger + auto const next = nodes[0]->getLedgerManager().getLastClosedLedgerNum() + 1; + std::optional> expected; + for (auto const& n : nodes) + { + if (n->getLedgerManager().getLastClosedLedgerNum() + 1 != next) + { + continue; + } + auto& driver = + static_cast(n->getHerder()).getHerderSCPDriver(); + auto schedule = driver.getLeaderSchedule(next); + REQUIRE(schedule.size() == 4); + if (!expected) + { + expected = schedule; + } + REQUIRE(*expected == schedule); + REQUIRE(driver.getLeaderSchedule(next + 1).empty()); + REQUIRE(driver.getLeaderSchedule(next - 1).empty()); + } +} + +TEST_CASE("leader schedule fails over when a validator is down", + "[herder][leader-schedule][simulation]") +{ + // 3 validators, threshold 2, one never started. Whenever the dead node + // leads round 1 the others time out and the round 2 leader takes over. + Hash networkID = sha256(getTestConfig().NETWORK_PASSPHRASE); + auto simulation = + std::make_shared(networkID, acceleratedConfig); + + std::vector keys; + for (int i = 0; i < 3; i++) + { + keys.push_back( + SecretKey::fromSeed(sha256("NODE_SEED_" + std::to_string(i)))); + } + SCPQuorumSet qSet; + qSet.threshold = 2; + std::vector allNodes; + for (auto const& k : keys) + { + qSet.validators.push_back(k.getPublicKey()); + allNodes.push_back(k.getPublicKey()); + } + NodeID const dead = keys[2].getPublicKey(); + + simulation->addNode(keys[0], qSet); + simulation->addNode(keys[1], qSet); + simulation->addPendingConnection(keys[0].getPublicKey(), + keys[1].getPublicKey()); + simulation->startAllNodes(); + auto nodes = simulation->getNodes(); + + uint32_t const lastLedger = 25; + auto closed = crankAndRecord(simulation, nodes[0], lastLedger, + std::chrono::seconds(600)); + simulation->crankUntil( + [&]() { return simulation->haveAllExternalized(lastLedger, 3); }, + std::chrono::seconds(60), false); + + int64_t deadLedRound1 = 0; + REQUIRE(closed.size() >= 20); + for (auto const& c : closed) + { + // the schedule covers the whole quorum set, including the dead node + auto schedule = computeLeaderSchedule(c.second.mPreviousHash, allNodes); + REQUIRE(schedule.size() == 3); + // every ledger closed anyway, with a live node's proposal + REQUIRE(c.second.mProposer != dead); + if (schedule[0] == dead) + { + ++deadLedRound1; + } + } + INFO(fmt::format("ledgers {} led by the dead node {}", closed.size(), + deadLedRound1)); + // 1/3 of the ledgers on average: (2/3)^20 that it never happens + REQUIRE(deadLedRound1 > 0); + // and each of those cost the live nodes a leader timeout + for (auto const& n : nodes) + { + REQUIRE(leaderTimeouts(n) >= deadLedRound1); + } +} diff --git a/src/herder/test/LeaderScheduleTests.cpp b/src/herder/test/LeaderScheduleTests.cpp new file mode 100644 index 000000000..c5bc3d149 --- /dev/null +++ b/src/herder/test/LeaderScheduleTests.cpp @@ -0,0 +1,123 @@ +// 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 "crypto/SHA.h" +#include "crypto/SecretKey.h" +#include "herder/LeaderSchedule.h" +#include "test/Catch2.h" +#include "util/Math.h" +#include "util/XDROperators.h" +#include +#include +#include +#include + +using namespace stellar; + +namespace +{ +std::vector +makeNodes(size_t n) +{ + std::vector nodes; + for (size_t i = 0; i < n; i++) + { + nodes.emplace_back( + SecretKey::fromSeed(sha256(fmt::format("SCHEDULE_NODE_{}", i))) + .getPublicKey()); + } + return nodes; +} +} + +TEST_CASE("leader schedule is a deterministic permutation", + "[herder][leader-schedule]") +{ + auto nodes = makeNodes(30); + Hash seed = sha256("ledger header 41"); + + auto schedule = computeLeaderSchedule(seed, nodes); + + SECTION("it is a permutation of the node set") + { + REQUIRE(schedule.size() == nodes.size()); + REQUIRE(std::set(schedule.begin(), schedule.end()) == + std::set(nodes.begin(), nodes.end())); + } + SECTION("it does not depend on the order nodes are listed in") + { + // every node enumerates its quorum in its own order + for (int i = 0; i < 20; i++) + { + auto shuffled = nodes; + stellar::shuffle(shuffled.begin(), shuffled.end(), + getGlobalRandomEngine()); + REQUIRE(computeLeaderSchedule(seed, shuffled) == schedule); + } + } + SECTION("duplicates are ignored") + { + auto dup = nodes; + dup.insert(dup.end(), nodes.begin(), nodes.begin() + 7); + REQUIRE(computeLeaderSchedule(seed, dup) == schedule); + } + SECTION("the seed changes the order") + { + auto other = computeLeaderSchedule(sha256("ledger header 42"), nodes); + REQUIRE(std::set(other.begin(), other.end()) == + std::set(nodes.begin(), nodes.end())); + REQUIRE(other != schedule); + } + SECTION("the node set changes the order only by the nodes involved") + { + // removing a node leaves the relative order of the others alone + auto fewer = nodes; + NodeID removed = fewer.back(); + fewer.pop_back(); + auto expected = schedule; + expected.erase(std::find(expected.begin(), expected.end(), removed)); + REQUIRE(computeLeaderSchedule(seed, fewer) == expected); + } + SECTION("degenerate sets") + { + REQUIRE(computeLeaderSchedule(seed, {}).empty()); + REQUIRE(computeLeaderSchedule(seed, {nodes[3]}) == + std::vector{nodes[3]}); + } +} + +TEST_CASE("leader schedule is fair across seeds", "[herder][leader-schedule]") +{ + // Each node should lead round 1 about 1/n of the time, and the node + // leading round 2 should be independent of the one leading round 1. + size_t const n = 10; + size_t const slots = 20000; + auto nodes = makeNodes(n); + std::map first; + std::map, size_t> firstTwo; + for (size_t i = 0; i < slots; i++) + { + auto schedule = + computeLeaderSchedule(sha256(fmt::format("header {}", i)), nodes); + first[schedule[0]]++; + firstTwo[{schedule[0], schedule[1]}]++; + } + REQUIRE(first.size() == n); + double const expected = static_cast(slots) / n; + for (auto const& f : first) + { + // 2000 expected, standard deviation ~42: 10% is over 4 sigma + REQUIRE(f.second > expected * 0.9); + REQUIRE(f.second < expected * 1.1); + } + // all ordered pairs of distinct nodes show up + REQUIRE(firstTwo.size() == n * (n - 1)); + double const expectedPair = static_cast(slots) / (n * (n - 1)); + for (auto const& f : firstTwo) + { + // 222 expected, standard deviation ~15 + REQUIRE(f.second > expectedPair * 0.6); + REQUIRE(f.second < expectedPair * 1.4); + } +} diff --git a/src/herder/test/PendingEnvelopesTests.cpp b/src/herder/test/PendingEnvelopesTests.cpp index 0eee4f71c..6b8085ade 100644 --- a/src/herder/test/PendingEnvelopesTests.cpp +++ b/src/herder/test/PendingEnvelopesTests.cpp @@ -100,6 +100,44 @@ makePublicKey(int i) auto secretKey = SecretKey::fromSeed(hash); return secretKey.getPublicKey(); } + +// The local node votes for the round 1 leader's ballot as soon as SCP sees +// it, before its own trigger. These tests feed the node a peer's statements +// and then inspect PendingEnvelopes (reference counts, fetch status), which +// such a vote would change. So they use a peer that does not lead round 1 of +// the next slot: the local node then leads it (or a node that never speaks +// does, once the peer's quorum set is known) and does not vote. +struct FollowerPeerApp +{ + // destroyed last: the application uses the clock + std::unique_ptr mClock; + SecretKey mPeer; + Application::pointer mApp; +}; + +FollowerPeerApp +createAppWithFollowerPeer(Config const& baseCfg) +{ + for (int i = 0; i < 100; ++i) + { + FollowerPeerApp res; + res.mPeer = SecretKey::pseudoRandomForTesting(); + Config cfg(baseCfg); + cfg.QUORUM_SET.validators.emplace_back(res.mPeer.getPublicKey()); + res.mClock = std::make_unique(); + res.mApp = createTestApplication(*res.mClock, cfg); + auto& herder = static_cast(res.mApp->getHerder()); + auto const seq = + res.mApp->getLedgerManager().getLastClosedLedgerNum() + 1; + auto leader = herder.getHerderSCPDriver().getRoundLeader(seq, 1); + if (leader && !(*leader == res.mPeer.getPublicKey())) + { + return res; + } + } + throw std::runtime_error( + "could not pick a peer that does not lead round 1"); +} } // namespace TEST_CASE_VERSIONS("PendingEnvelopes recvSCPEnvelope", "[herder]") @@ -108,12 +146,10 @@ TEST_CASE_VERSIONS("PendingEnvelopes recvSCPEnvelope", "[herder]") cfg.MANUAL_CLOSE = false; cfg.EXPERIMENTAL_PARALLEL_TX_SET_DOWNLOAD = true; - VirtualClock clock; - - auto s = SecretKey::pseudoRandomForTesting(); + auto fixture = createAppWithFollowerPeer(cfg); + auto s = fixture.mPeer; auto& pk = s.getPublicKey(); - cfg.QUORUM_SET.validators.emplace_back(s.getPublicKey()); - Application::pointer app = createTestApplication(clock, cfg); + Application::pointer app = fixture.mApp; auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); @@ -766,12 +802,10 @@ TEST_CASE("PendingEnvelopes recvSCPEnvelope without parallel tx set download", cfg.MANUAL_CLOSE = false; cfg.EXPERIMENTAL_PARALLEL_TX_SET_DOWNLOAD = false; - VirtualClock clock; - - auto s = SecretKey::pseudoRandomForTesting(); + auto fixture = createAppWithFollowerPeer(cfg); + auto s = fixture.mPeer; auto& pk = s.getPublicKey(); - cfg.QUORUM_SET.validators.emplace_back(s.getPublicKey()); - Application::pointer app = createTestApplication(clock, cfg); + Application::pointer app = fixture.mApp; auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); @@ -822,11 +856,11 @@ TEST_CASE("PendingEnvelopes re-requests tx sets the overlay does not deliver", "[herder]") { Config cfg(getTestConfig()); - VirtualClock clock; - auto s = SecretKey::pseudoRandomForTesting(); + auto fixture = createAppWithFollowerPeer(cfg); + auto& clock = *fixture.mClock; + auto s = fixture.mPeer; auto& pk = s.getPublicKey(); - cfg.QUORUM_SET.validators.emplace_back(pk); - Application::pointer app = createTestApplication(clock, cfg); + Application::pointer app = fixture.mApp; auto const lcl = app->getLedgerManager().getLastClosedLedgerHeader(); auto& herder = static_cast(app->getHerder()); auto& pendingEnvelopes = herder.getPendingEnvelopes(); diff --git a/src/herder/test/QuorumTrackerTests.cpp b/src/herder/test/QuorumTrackerTests.cpp index 030f80082..04abe0056 100644 --- a/src/herder/test/QuorumTrackerTests.cpp +++ b/src/herder/test/QuorumTrackerTests.cpp @@ -78,21 +78,25 @@ testQuorumTracker() herder->recvTxSet(p.mTxSet->getContentsHash(), p.mTxSet); } }; - auto recvNom = [&](uint64 slotID, SecretKey const& k, - SCPQuorumSet const& qSet, - std::vector const& pp) { + auto recvPrepare = [&](uint64 slotID, SecretKey const& k, + SCPQuorumSet const& qSet, + std::vector const& pp) { + // A PREPARE that references every value of `pp`: the last one is the + // ballot's value, an earlier one (if any) is the prepared value. More + // values make a newer statement. + releaseAssert(!pp.empty() && pp.size() <= 2); SCPEnvelope envelope; - envelope.statement.pledges.type(SCP_ST_NOMINATE); - auto& nom = envelope.statement.pledges.nominate(); - - std::set values; - for (auto& p : pp) + envelope.statement.pledges.type(SCP_ST_PREPARE); + auto& prep = envelope.statement.pledges.prepare(); + prep.ballot.counter = static_cast(pp.size()); + prep.ballot.value = pp.back().mSignedV; + if (pp.size() > 1) { - values.insert(p.mSignedV); + prep.prepared.activate().counter = 1; + prep.prepared->value = pp.front().mSignedV; } - nom.votes.insert(nom.votes.begin(), values.begin(), values.end()); auto qSetH = sha256(xdr::xdr_to_opaque(qSet)); - nom.quorumSetHash = qSetH; + prep.quorumSetHash = qSetH; recvEnvelope(envelope, slotID, k, qSet, pp); }; auto recvExternalize = [&](uint64 slotID, SecretKey const& k, @@ -135,17 +139,17 @@ testQuorumTracker() SECTION("Receive self") { checkInQuorum({0, 1}); - recvNom(3, cfg.NODE_SEED, cfg.QUORUM_SET, {vv}); + recvPrepare(3, cfg.NODE_SEED, cfg.QUORUM_SET, {vv}); checkInQuorum({0, 1}); } SECTION("Expand 0") { checkInQuorum({0, 1}); - recvNom(3, otherKeys[0], qSet0, {vv}); + recvPrepare(3, otherKeys[0], qSet0, {vv}); checkInQuorum({0, 1, 2, 3}); SECTION("Expand 2") { - recvNom(3, otherKeys[2], qSet2, {vv}); + recvPrepare(3, otherKeys[2], qSet2, {vv}); checkInQuorum({0, 1, 2, 3, 5, 6}); SECTION("node restart") { @@ -168,13 +172,13 @@ testQuorumTracker() SECTION("Update 0's qSet") { auto vv2 = makeValue(2); - recvNom(3, otherKeys[0], qSet0b, {vv, vv2}); + recvPrepare(3, otherKeys[0], qSet0b, {vv, vv2}); checkInQuorum({0, 1, 4, 5}); } SECTION("Update 0's qSet in an old slot") { auto vv2 = makeValue(2); - recvNom(2, otherKeys[0], qSet0b, {vv, vv2}); + recvPrepare(2, otherKeys[0], qSet0b, {vv, vv2}); // nothing changes (slot 3 has precedence) checkInQuorum({0, 1, 2, 3}); } diff --git a/src/main/Config.cpp b/src/main/Config.cpp index 457e23ab9..33e580961 100644 --- a/src/main/Config.cpp +++ b/src/main/Config.cpp @@ -68,7 +68,6 @@ static std::unordered_set const TESTING_ONLY_OPTIONS = { "ARTIFICIALLY_SET_SURVEY_PHASE_DURATION_FOR_TESTING", "ARTIFICIALLY_DELAY_BUCKET_APPLICATION_FOR_TESTING", "ARTIFICIALLY_SLEEP_MAIN_THREAD_FOR_TESTING", - "ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING", "ARTIFICIALLY_SKIP_CONNECTION_ADJUSTMENT_FOR_TESTING", "ARTIFICIALLY_DELAY_LEDGER_CLOSE_FOR_TESTING", "SKIP_HIGH_CRITICAL_VALIDATOR_CHECKS_FOR_TESTING", @@ -155,7 +154,7 @@ Config::Config() : NODE_SEED(SecretKey::random()) TESTING_MAX_CLASSIC_BYTE_ALLOWANCE = 0; IGNORE_MESSAGE_LIMITS_FOR_TESTING = false; TESTING_IGNORE_LEDGER_TIME_UPGRADE_BOUNDS = false; - TESTING_NOMINATE_RANDOM_VALUES = false; + TESTING_PROPOSE_RANDOM_VALUES = false; ALLOW_PRIVATE_ADDRESSES_FOR_TESTING = false; #endif @@ -376,8 +375,6 @@ Config::Config() : NODE_SEED(SecretKey::random()) SKIP_HIGH_CRITICAL_VALIDATOR_CHECKS_FOR_TESTING = false; ARTIFICIALLY_SET_SYSTEM_CLOCK_OFFSET_FOR_TESTING = std::chrono::milliseconds::zero(); - ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING = - std::chrono::milliseconds::zero(); #endif #ifdef BEST_OFFER_DEBUGGING @@ -1179,8 +1176,8 @@ Config::processConfig(std::shared_ptr t) "upgrade bounds for testing"); } }}, - {"TESTING_NOMINATE_RANDOM_VALUES", - [&]() { TESTING_NOMINATE_RANDOM_VALUES = readBool(item); }}, + {"TESTING_PROPOSE_RANDOM_VALUES", + [&]() { TESTING_PROPOSE_RANDOM_VALUES = readBool(item); }}, #endif {"PEER_PORT", [&]() { PEER_PORT = readInt(item, 1); }}, @@ -1985,11 +1982,6 @@ Config::processConfig(std::shared_ptr t) ARTIFICIALLY_SET_SYSTEM_CLOCK_OFFSET_FOR_TESTING = std::chrono::milliseconds(readInt(item)); }}, - {"ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING", - [&]() { - ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING = - std::chrono::milliseconds(readInt(item)); - }}, #endif {"MAX_DEX_TX_OPERATIONS_IN_TX_SET", [&]() { diff --git a/src/main/Config.h b/src/main/Config.h index da3a79720..f3238b9a4 100644 --- a/src/main/Config.h +++ b/src/main/Config.h @@ -952,16 +952,12 @@ class Config : public std::enable_shared_from_this // When set, this node will nominate random values when it is the round // leader. This is useful for testing CAP-0083 behavior. This is a testing // only flag. - bool TESTING_NOMINATE_RANDOM_VALUES; + bool TESTING_PROPOSE_RANDOM_VALUES; // Injects a signed wall-clock offset into the node's system clock for // testing. Expressed in milliseconds. std::chrono::milliseconds ARTIFICIALLY_SET_SYSTEM_CLOCK_OFFSET_FOR_TESTING; - // Delay emission of updated nomination messages for testing nomination - // timeout behavior. Expressed in milliseconds. - std::chrono::milliseconds ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING; - // Set QUORUM_SET using automatic quorum set configuration based on // `validators`. void diff --git a/src/overlay/test/OverlayIPCTests.cpp b/src/overlay/test/OverlayIPCTests.cpp index 12a3f5c60..e79cd21b0 100644 --- a/src/overlay/test/OverlayIPCTests.cpp +++ b/src/overlay/test/OverlayIPCTests.cpp @@ -56,18 +56,19 @@ makeMockSCPEnvelope(uint64_t slotIndex, uint32_t nodeId) { SCPEnvelope env; env.statement.slotIndex = slotIndex; - env.statement.pledges.type(SCP_ST_NOMINATE); + env.statement.pledges.type(SCP_ST_PREPARE); // Set some mock data - auto& nom = env.statement.pledges.nominate(); - nom.quorumSetHash.fill(static_cast(nodeId)); + auto& prep = env.statement.pledges.prepare(); + prep.quorumSetHash.fill(static_cast(nodeId)); + prep.ballot.counter = 1; // Value is opaque<> (xvector), not Hash Value mockValue; mockValue.resize(32); std::fill(mockValue.begin(), mockValue.end(), static_cast(slotIndex & 0xFF)); - nom.votes.push_back(mockValue); + prep.ballot.value = mockValue; return env; } @@ -404,11 +405,11 @@ TEST_CASE("Rust overlay SCP consensus", "[overlay-ipc][.]") } /** - * Test TX set building and nomination hash request. + * Test TX set building and proposal hash request. * * This test: * 1. Creates an OverlayIPC connection to a Rust overlay - * 2. Requests a nomination hash (which builds a TX set from empty mempool) + * 2. Requests a proposal hash (which builds a TX set from empty mempool) * 3. Verifies a hash is returned */ TEST_CASE("Rust overlay get top transactions", "[overlay-ipc][.]") @@ -1008,7 +1009,7 @@ TEST_CASE("Rust overlay TX included in ledger", "[overlay-ipc][.]") * - 200 tx/ledger (heavy load) * * For each rate, runs for several ledgers and measures: - * - scp.timing.nominated (time from nomination to prepare) + * - scp.timing.trigger-to-ballot (time from the trigger to the first ballot) * - scp.timing.externalized (time from prepare to externalize) * - ledger.ledger.close (total ledger close time) */ @@ -1034,8 +1035,8 @@ TEST_CASE("Rust overlay SCP latency under TX load", std::string label; int txSubmitted; int txIncluded; - double scpNominatedMean; - double scpNominatedMax; + double scpTriggerToBallotMean; + double scpTriggerToBallotMax; double scpExternalizedMean; double scpExternalizedMax; double ledgerCloseMean; @@ -1095,8 +1096,8 @@ TEST_CASE("Rust overlay SCP latency under TX load", // Get metrics (they accumulate across ledgers) auto& metrics = node0->getMetrics(); - auto& scpNominated = - metrics.NewTimer({"scp", "timing", "nominated"}); + auto& scpTriggerToBallot = + metrics.NewTimer({"scp", "timing", "trigger-to-ballot"}); auto& scpExternalized = metrics.NewTimer({"scp", "timing", "externalized"}); auto& ledgerClose = metrics.NewTimer({"ledger", "ledger", "close"}); @@ -1214,8 +1215,8 @@ TEST_CASE("Rust overlay SCP latency under TX load", res.label = run.label; res.txSubmitted = txSubmitted; res.txIncluded = static_cast(txIncluded); - res.scpNominatedMean = scpNominated.mean(); - res.scpNominatedMax = scpNominated.max(); + res.scpTriggerToBallotMean = scpTriggerToBallot.mean(); + res.scpTriggerToBallotMax = scpTriggerToBallot.max(); res.scpExternalizedMean = scpExternalized.mean(); res.scpExternalizedMax = scpExternalized.max(); res.ledgerCloseMean = ledgerClose.mean(); @@ -1250,8 +1251,8 @@ TEST_CASE("Rust overlay SCP latency under TX load", { LOG_INFO(DEFAULT_LOG, "{:<20} {:>10} {:>10} {:>12.2f} {:>12.2f} {:>12.2f} {:>12.2f}", - r.label, r.txSubmitted, r.txIncluded, r.scpNominatedMean, - r.scpNominatedMax, r.scpExternalizedMean, + r.label, r.txSubmitted, r.txIncluded, r.scpTriggerToBallotMean, + r.scpTriggerToBallotMax, r.scpExternalizedMean, r.scpExternalizedMax); } @@ -1272,11 +1273,11 @@ TEST_CASE("Rust overlay SCP latency under TX load", // Allow 3x degradation from light to heavy load if (allResults.size() >= 2) { - double lightMean = allResults[0].scpNominatedMean; - double heavyMean = allResults.back().scpNominatedMean; + double lightMean = allResults[0].scpTriggerToBallotMean; + double heavyMean = allResults.back().scpTriggerToBallotMean; LOG_INFO(DEFAULT_LOG, - "SCP nominated latency ratio (heavy/light): {:.2f}x", + "SCP trigger-to-ballot latency ratio (heavy/light): {:.2f}x", heavyMean / lightMean); // Warn but don't fail if degradation is significant @@ -1400,7 +1401,8 @@ TEST_CASE("Rust overlay 15-node 2000 TPS stress test", // Get metrics from node 0 auto& metrics = nodes[0]->getMetrics(); - auto& scpNominated = metrics.NewTimer({"scp", "timing", "nominated"}); + auto& scpTriggerToBallot = + metrics.NewTimer({"scp", "timing", "trigger-to-ballot"}); auto& scpExternalized = metrics.NewTimer({"scp", "timing", "externalized"}); auto& ledgerClose = metrics.NewTimer({"ledger", "ledger", "close"}); @@ -1470,8 +1472,8 @@ TEST_CASE("Rust overlay 15-node 2000 TPS stress test", LOG_INFO(DEFAULT_LOG, " Effective TPS: {:.0f}", effectiveTps); LOG_INFO(DEFAULT_LOG, ""); LOG_INFO(DEFAULT_LOG, "SCP Timing (ms):"); - LOG_INFO(DEFAULT_LOG, " Nominated: mean={:.2f}, max={:.2f}", - scpNominated.mean(), scpNominated.max()); + LOG_INFO(DEFAULT_LOG, " TriggerToBallot: mean={:.2f}, max={:.2f}", + scpTriggerToBallot.mean(), scpTriggerToBallot.max()); LOG_INFO(DEFAULT_LOG, " Externalized: mean={:.2f}, max={:.2f}", scpExternalized.mean(), scpExternalized.max()); LOG_INFO(DEFAULT_LOG, ""); @@ -1484,10 +1486,10 @@ TEST_CASE("Rust overlay 15-node 2000 TPS stress test", // Warn if SCP latency is concerning (but don't fail - this is // informational) - if (scpNominated.max() > 5000) + if (scpTriggerToBallot.max() > 5000) { - WARN("SCP nomination max latency exceeded 5s: " << scpNominated.max() - << "ms"); + WARN("SCP trigger-to-ballot max latency exceeded 5s: " + << scpTriggerToBallot.max() << "ms"); } LOG_INFO(DEFAULT_LOG, diff --git a/src/scp/BallotProtocol.cpp b/src/scp/BallotProtocol.cpp index b69f5c2a8..01bc51c56 100644 --- a/src/scp/BallotProtocol.cpp +++ b/src/scp/BallotProtocol.cpp @@ -32,6 +32,11 @@ BallotProtocol::BallotProtocol(Slot& slot) , mPhase(SCP_PHASE_PREPARE) , mCurrentMessageLevel(0) , mTimerExpCount(0) + , mLeaderRound(0) + , mLeaderRoundsStarted(false) + , mLeaderTimerRound(0) + , mLeaderTimerExtended(false) + , mInProgressRound(false) { } @@ -247,7 +252,18 @@ BallotProtocol::processEnvelope(SCPEnvelopeWrapperPtr envelope, bool self) } recordEnvelope(envelope); + if (!self) + { + // the statement may be the round leader's proposal + progressRound(); + } advanceSlot(statement); + if (!self) + { + // processing the statement may have moved the local node to a + // new round, or given it a value it has to stick to + progressRound(); + } return SCP::EnvelopeState::VALID; } @@ -349,28 +365,331 @@ BallotProtocol::abandonBallot(uint32 n) { ZoneScoped; CLOG_TRACE(SCP, "BallotProtocol::abandonBallot"); + if (mPhase != SCP_PHASE_PREPARE && mPhase != SCP_PHASE_CONFIRM) + { + return false; + } + if (n == 0 && mLeaderRound == UINT32_MAX) + { + // already at the "infinite" counter of a committed ballot + return false; + } + uint32 newRound = (n == 0) ? (mLeaderRound + 1) : n; + if (newRound > mLeaderRound) + { + mLeaderRound = newRound; + if (mLeaderRound != UINT32_MAX) + { + mSlot.getSCPDriver().enteredRound(mSlot.getSlotIndex(), + mLeaderRound, n == 0); + } + } + return progressRound(); +} + +bool +BallotProtocol::startLeaderRounds(ProposalValueSupplier const& makeValue) +{ + ZoneScoped; + if (mPhase != SCP_PHASE_PREPARE && mPhase != SCP_PHASE_CONFIRM) + { + return false; + } + mMakeValue = makeValue; + mLeaderRoundsStarted = true; + if (mLeaderRound == 0) + { + mLeaderRound = 1; + mSlot.getSCPDriver().enteredRound(mSlot.getSlotIndex(), mLeaderRound, + false); + } + return progressRound(); +} + +std::optional +BallotProtocol::getLeaderBallot(NodeID const& leader, uint32 round) const +{ + auto it = mLatestEnvelopes.find(leader); + if (it == mLatestEnvelopes.end()) + { + return std::nullopt; + } + auto const& pl = it->second->getStatement().pledges; + switch (pl.type()) + { + case SCP_ST_PREPARE: + if (pl.prepare().ballot.counter == round) + { + return pl.prepare().ballot; + } + break; + case SCP_ST_CONFIRM: + if (pl.confirm().ballot.counter == round) + { + return pl.confirm().ballot; + } + break; + default: + // an EXTERNALIZE statement is not tied to a round: the regular + // accept/confirm steps deal with it + break; + } + return std::nullopt; +} + +bool +BallotProtocol::progressRound() +{ + ZoneScoped; + if (mInProgressRound) + { + // the outer call loops until there is nothing left to do + return false; + } + mInProgressRound = true; bool res = false; - auto v = mSlot.getLatestCompositeCandidate(); + try + { + // A step that changes the ballot may enable another one (e.g. the + // node moved to a new round while processing its own statement). + // The ballot only ever increases, and each productive step raises + // its counter to the current round, so this ends. + while (progressRoundStep()) + { + res = true; + } + updateLeaderTimer(); + } + catch (...) + { + mInProgressRound = false; + throw; + } + mInProgressRound = false; + return res; +} - if (!v || v->getValue().empty()) +bool +BallotProtocol::progressRoundStep() +{ + if (mPhase != SCP_PHASE_PREPARE && mPhase != SCP_PHASE_CONFIRM) { - if (mCurrentBallot) + return false; + } + if (!getLocalNode()->isValidator()) + { + // only validators vote + return false; + } + + // Before the application starts the slot, the local node may still adopt + // what the round 1 leader put forward: followers do not wait for their + // own clock. + uint32 const round = std::max(mLeaderRound, 1); + uint32 const counter = + mCurrentBallot ? mCurrentBallot->getBallot().counter : 0; + if (counter >= round) + { + return false; + } + + auto& driver = mSlot.getSCPDriver(); + uint64 const slotIndex = mSlot.getSlotIndex(); + + if (mValueOverride) + { + // The node saw this value confirmed prepared, or voted to commit it: + // it sticks to it whoever leads the round (bumpState enforces this + // for any value passed in). + if (mLeaderRound == 0) { - v = mCurrentBallot->getWValue(); + return false; } + return bumpState(mValueOverride->getValue(), round); } - if (v && !v->getValue().empty()) + + if (mPhase != SCP_PHASE_PREPARE) { - if (n == 0) + // CONFIRM has a value override + return false; + } + + if (round == UINT32_MAX) + { + // The "infinite" counter of a committed ballot (a v-blocking set + // externalized) is not a round and has no leader: just keep up with + // the value the node already votes for, if any. + if (mCurrentBallot && mLeaderRound != 0) { - res = bumpState(v->getValue(), true); + Value v = mCurrentBallot->getBallot().value; + return bumpState(v, round); } - else + return false; + } + + auto leader = driver.getRoundLeader(slotIndex, round); + if (!leader) + { + return false; + } + + if (*leader == getLocalNode()->getNodeID()) + { + if (mLeaderRound == 0) + { + // not started: the application is not ready to propose + return false; + } + // Keep the value the node already votes for unless it is not + // acceptable as a proposal (e.g. an empty-tx-set value): the nodes + // that voted for it already know (or are fetching) its transaction + // set. bumpState gives up on a transaction set that never arrived. + if (mCurrentBallot && + driver.validateValue(slotIndex, mCurrentBallot->getBallot().value, + /*proposal=*/true) != SCPDriver::kInvalidValue) { - res = bumpState(v->getValue(), n); + Value v = mCurrentBallot->getBallot().value; + driver.proposedValue(slotIndex, round, v); + return bumpState(v, round); } + if (!mLeaderRoundsStarted || !mMakeValue || mCurrentMessageLevel != 0) + { + // Building a value re-enters the slot, so it is only done from + // the top level; processEnvelope calls back once it is there. + return false; + } + auto value = mMakeValue(); + // Building the value can make pending envelopes ready and re-enter + // SCP: the slot may have moved on. + uint32 const newCounter = + mCurrentBallot ? mCurrentBallot->getBallot().counter : 0; + if (!value || mPhase != SCP_PHASE_PREPARE || mValueOverride || + mLeaderRound != round || newCounter >= round) + { + // re-evaluate from the new state, if it changed + return newCounter != counter || mLeaderRound != round || + mValueOverride != nullptr; + } + driver.proposedValue(slotIndex, round, value->getValue()); + return bumpState(value->getValue(), round); } - return res; + + uint32 adoptRound = round; + auto leaderBallot = getLeaderBallot(*leader, round); + if (!leaderBallot && !mCurrentBallot) + { + // The node gave up on earlier rounds without ever voting. If one of + // their leaders' proposals shows up late (e.g. its transaction set + // took longer than the leader timeout to arrive), vote for it at its + // own counter rather than discard it: the nodes that were in time + // voted for it too. The node stays in its round, so the current + // leader's ballot (higher counter) still takes over when it comes. + static uint32 const MAX_LATE_ROUNDS = 8; + for (uint32 r = round - 1; + r >= 1 && round - r <= MAX_LATE_ROUNDS && !leaderBallot; --r) + { + auto pastLeader = driver.getRoundLeader(slotIndex, r); + if (pastLeader && !(*pastLeader == getLocalNode()->getNodeID())) + { + leaderBallot = getLeaderBallot(*pastLeader, r); + adoptRound = r; + } + } + } + if (!leaderBallot) + { + return false; + } + // Voting for a leader's value is the local node's endorsement of it as a + // proposal, so the stricter validation applies (as it did for nomination + // votes): e.g. it must only carry upgrades the local node armed. + // An empty-tx-set value is never built as a proposal: a leader only votes + // for one after the ballot protocol replaced a value whose transaction + // set did not arrive. Following it is a ballot-level decision, so the + // ballot validation applies. + bool const proposal = !driver.isEmptyTxSetValue(leaderBallot->value); + if (driver.validateValue(slotIndex, leaderBallot->value, proposal) < + SCPDriver::kStructurallyValidValue) + { + return false; + } + return bumpState(leaderBallot->value, adoptRound); +} + +void +BallotProtocol::updateLeaderTimer() +{ + uint32 const counter = + mCurrentBallot ? mCurrentBallot->getBallot().counter : 0; + bool const waiting = mPhase == SCP_PHASE_PREPARE && mLeaderRound != 0 && + mLeaderRound != UINT32_MAX && counter < mLeaderRound && + getLocalNode()->isValidator(); + if (waiting) + { + if (mLeaderTimerRound != mLeaderRound) + { + // The ballot (if any) is for a round the node gave up on. + stopBallotProtocolTimer(); + mLeaderTimerRound = mLeaderRound; + mLeaderTimerExtended = false; + armLeaderTimer(mLeaderRound); + } + } + else if (mLeaderTimerRound != 0) + { + mLeaderTimerRound = 0; + mSlot.getSCPDriver().setupTimer(mSlot.getSlotIndex(), + Slot::LEADER_TIMER, + std::chrono::seconds::zero(), nullptr); + } +} + +void +BallotProtocol::armLeaderTimer(uint32 round) +{ + std::shared_ptr slot = mSlot.shared_from_this(); + mSlot.getSCPDriver().setupTimer( + mSlot.getSlotIndex(), Slot::LEADER_TIMER, + mSlot.getSCPDriver().computeTimeout(round), [slot, round]() { + slot->getBallotProtocol().leaderTimerExpired(round); + }); +} + +void +BallotProtocol::leaderTimerExpired(uint32 round) +{ + if (mLeaderTimerRound != round) + { + // stale callback + return; + } + uint32 const counter = + mCurrentBallot ? mCurrentBallot->getBallot().counter : 0; + if (mPhase != SCP_PHASE_PREPARE || mLeaderRound != round || + counter >= round) + { + mLeaderTimerRound = 0; + return; + } + if (!mLeaderTimerExtended) + { + // The leader is not silent if its statement already reached the + // application and is only waiting for its transaction set: give it + // one more timeout before moving on, rather than have the next leader + // build a competing value. + auto leader = + mSlot.getSCPDriver().getRoundLeader(mSlot.getSlotIndex(), round); + if (leader && mSlot.getSCPDriver().isProposalPending( + mSlot.getSlotIndex(), *leader)) + { + mLeaderTimerExtended = true; + armLeaderTimer(round); + return; + } + } + mLeaderTimerRound = 0; + mTimerExpCount++; + abandonBallot(0); } bool @@ -580,13 +899,27 @@ BallotProtocol::bumpToBallot(SCPBallot const& ballot, bool check) { mHeardFromQuorum = false; } + + // The ballot can also move through the regular accept/confirm steps + // (e.g. a quorum confirmed a higher ballot prepared). + if (ballot.counter > mLeaderRound) + { + mLeaderRound = ballot.counter; + // UINT32_MAX is the "infinite" counter of a committed ballot, not a + // round + if (mLeaderRound != UINT32_MAX) + { + mSlot.getSCPDriver().enteredRound(mSlot.getSlotIndex(), + mLeaderRound, false); + } + } } void BallotProtocol::startBallotProtocolTimer() { std::chrono::milliseconds timeout = mSlot.getSCPDriver().computeTimeout( - mCurrentBallot->getBallot().counter, /*isNomination=*/false); + mCurrentBallot->getBallot().counter); std::shared_ptr slot = mSlot.shared_from_this(); mSlot.getSCPDriver().setupTimer( @@ -607,6 +940,8 @@ void BallotProtocol::ballotProtocolTimerExpired() { mTimerExpCount++; + // The timer only runs for a ballot at the current round (see + // checkHeardFromQuorum), so this moves to the round after it. abandonBallot(0); } @@ -1438,7 +1773,7 @@ void BallotProtocol::throwIfValueInvalidForConfirmCommit(Value const& value) { auto validationLevel = mSlot.getSCPDriver().validateValue( - mSlot.getSlotIndex(), value, /*nomination=*/false); + mSlot.getSlotIndex(), value, /*proposal=*/false); if (validationLevel == SCPDriver::kFullyValidatedValue || validationLevel == SCPDriver::kMaybeValidNotCurrentValue) @@ -1685,7 +2020,7 @@ BallotProtocol::setConfirmCommit(SCPBallot const& c, SCPBallot const& h) emitCurrentStateStatement(); - mSlot.stopNomination(); + updateLeaderTimer(); mSlot.getSCPDriver().valueExternalized(mSlot.getSlotIndex(), mCommit->getBallot().value); @@ -1931,6 +2266,11 @@ BallotProtocol::setStateFromEnvelope(SCPEnvelopeWrapperPtr e) { mCommit = makeBallot(prep.nC, b.value); } + if (prep.nH) + { + // as when the node confirmed the ballot prepared + mValueOverride = mSlot.getSCPDriver().wrapValue(b.value); + } mPhase = SCP_PHASE_PREPARE; } break; @@ -1942,6 +2282,7 @@ BallotProtocol::setStateFromEnvelope(SCPEnvelopeWrapperPtr e) mPrepared = makeBallot(c.nPrepared, v); mHighBallot = makeBallot(c.nH, v); mCommit = makeBallot(c.nCommit, v); + mValueOverride = mSlot.getSCPDriver().wrapValue(v); mPhase = SCP_PHASE_CONFIRM; } break; @@ -2174,6 +2515,12 @@ BallotProtocol::getJsonInfo() ret["heard"] = mHeardFromQuorum; ret["ballot"] = ballotToStr(mCurrentBallot); ret["phase"] = phaseNames[mPhase]; + ret["round"] = mLeaderRound; + if (auto leader = mSlot.getSCPDriver().getRoundLeader( + mSlot.getSlotIndex(), std::max(mLeaderRound, 1))) + { + ret["leader"] = mSlot.getSCPDriver().toShortString(*leader); + } ret["state"] = getLocalState(); return ret; @@ -2387,7 +2734,10 @@ BallotProtocol::checkHeardFromQuorum() // if we transition from not heard -> heard, we start the timer mSlot.getSCPDriver().ballotDidHearFromQuorum( mSlot.getSlotIndex(), mCurrentBallot->getBallot()); - if (mPhase != SCP_PHASE_EXTERNALIZE) + // A ballot behind the current round is one the node already + // gave up on: the leader timer runs instead. + if (mPhase != SCP_PHASE_EXTERNALIZE && + mCurrentBallot->getBallot().counter >= mLeaderRound) { startBallotProtocolTimer(); } diff --git a/src/scp/BallotProtocol.h b/src/scp/BallotProtocol.h index 2ff2cb120..e7126d144 100644 --- a/src/scp/BallotProtocol.h +++ b/src/scp/BallotProtocol.h @@ -9,6 +9,7 @@ #include "util/GlobalChecks.h" #include #include +#include #include #include #include @@ -95,6 +96,31 @@ class BallotProtocol SCPEnvelopeWrapperPtr mLastEnvelopeEmit; // last envelope emitted by this node + // ** Leader rounds + // There is no nomination phase. A round is a ballot counter, and each + // round has one leader (SCPDriver::getRoundLeader). At counter r a node + // votes for the ballot `leader(r)` votes for at counter r, unless it + // already holds a value it must stick to (mValueOverride). + + // The round the local node is in: the highest counter it is willing to + // vote at. 0 until the node starts the slot or adopts a round 1 ballot. + // Invariant: mLeaderRound >= mCurrentBallot's counter (so it is + // UINT32_MAX once the node moved to the "infinite" counter of a committed + // ballot). + uint32 mLeaderRound; + // True once the application started the slot (startLeaderRounds). + bool mLeaderRoundsStarted; + // Builds the local node's own value when it leads a round. + ProposalValueSupplier mMakeValue; + // Round the leader timer is armed for (0 = not armed). + uint32 mLeaderTimerRound; + // True once the leader timer of mLeaderTimerRound got its one extension + // (the leader's proposal was pending in the application when it fired). + bool mLeaderTimerExtended; + // Guards against re-entering progressRound (the value supplier and + // bumpState both re-enter the slot). + bool mInProgressRound; + public: BallotProtocol(Slot& slot); @@ -106,10 +132,21 @@ class BallotProtocol bool self); void ballotProtocolTimerExpired(); - // abandon's current ballot, move to a new ballot - // at counter `n` (or, if n == 0, increment current counter) + void leaderTimerExpired(uint32 round); + // abandon's current ballot, move to round + // `n` (or, if n == 0, to the next round) bool abandonBallot(uint32 n); + // Called when the local node is ready to take part in consensus on this + // slot: enters round 1 (if not already further along). + bool startLeaderRounds(ProposalValueSupplier const& makeValue); + + uint32 + getLeaderRound() const + { + return mLeaderRound; + } + // bumps the ballot based on the local state and the value passed in: // in prepare phase, attempts to take value // otherwise, no-ops @@ -328,6 +365,21 @@ class BallotProtocol void stopBallotProtocolTimer(); void checkHeardFromQuorum(); + // Tries to bring the current ballot up to the current round: with the + // value the node must stick to, else as the round's leader (own value), + // else by adopting what the round's leader voted for. Arms the leader + // timer while the ballot is still behind the round. + // Returns true if the current ballot changed. + bool progressRound(); + // One attempt of the above; returns true if the current ballot changed. + bool progressRoundStep(); + // Returns the ballot `leader` voted for at exactly counter `round`, if + // its latest statement is at that counter. + std::optional getLeaderBallot(NodeID const& leader, + uint32 round) const; + void updateLeaderTimer(); + void armLeaderTimer(uint32 round); + SCPBallotWrapperUPtr makeBallot(SCPBallot const& b) const; SCPBallotWrapperUPtr makeBallot(uint32 c, Value const& v) const; diff --git a/src/scp/NominationProtocol.cpp b/src/scp/NominationProtocol.cpp deleted file mode 100644 index 015bd8f81..000000000 --- a/src/scp/NominationProtocol.cpp +++ /dev/null @@ -1,926 +0,0 @@ -// Copyright 2014 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 "NominationProtocol.h" - -#include "Slot.h" -#include "crypto/Hex.h" -#include "lib/json/json.h" -#include "scp/LocalNode.h" -#include "scp/QuorumSetUtils.h" -#include "util/GlobalChecks.h" -#include "util/Logging.h" -#include "util/XDROperators.h" -#include "xdrpp/marshal.h" -#include -#include -#include - -namespace stellar -{ -using namespace std::placeholders; - -NominationProtocol::NominationProtocol(Slot& slot) - : mSlot(slot), mRoundNumber(0), mNominationStarted(false), mTimerExpCount(0) -{ -} - -std::set -NominationProtocol::predictLeaders(Slot& slot, Value const& previousValue, - uint32_t rounds) -{ - NominationProtocol preview(slot); - preview.mPreviousValue = previousValue; - for (uint32_t i = 0; i < rounds; ++i) - { - ++preview.mRoundNumber; - preview.updateRoundLeaders(); - } - return preview.mRoundLeaders; -} - -bool -NominationProtocol::isNewerStatement(NodeID const& nodeID, - SCPNomination const& st) -{ - auto oldp = mLatestNominations.find(nodeID); - bool res = false; - - if (oldp == mLatestNominations.end()) - { - res = true; - } - else - { - res = isNewerStatement(oldp->second->getStatement().pledges.nominate(), - st); - } - return res; -} - -bool -NominationProtocol::isSubsetHelper(xdr::xvector const& p, - xdr::xvector const& v, bool& notEqual) -{ - bool res; - if (p.size() <= v.size()) - { - res = std::includes(v.begin(), v.end(), p.begin(), p.end()); - if (res) - { - notEqual = p.size() != v.size(); - } - else - { - notEqual = true; - } - } - else - { - notEqual = true; - res = false; - } - return res; -} - -SCPDriver::ValidationLevel -NominationProtocol::validateValue(Value const& v) -{ - ZoneScoped; - return mSlot.getSCPDriver().validateValue(mSlot.getSlotIndex(), v, true); -} - -ValueWrapperPtr -NominationProtocol::extractValidValue(Value const& value) -{ - ZoneScoped; - return mSlot.getSCPDriver().extractValidValue(mSlot.getSlotIndex(), value); -} - -bool -NominationProtocol::isNewerStatement(SCPNomination const& oldst, - SCPNomination const& st) -{ - bool res = false; - bool grows; - bool g = false; - - if (isSubsetHelper(oldst.votes, st.votes, g)) - { - grows = g; - if (isSubsetHelper(oldst.accepted, st.accepted, g)) - { - grows = grows || g; - res = grows; // true only if one of the sets grew - } - } - - return res; -} - -bool -NominationProtocol::isSane(SCPStatement const& st) -{ - auto const& nom = st.pledges.nominate(); - bool res = (nom.votes.size() + nom.accepted.size()) != 0; - - res = res && (std::adjacent_find( - nom.votes.begin(), nom.votes.end(), - [](stellar::Value const& l, stellar::Value const& r) { - return !(l < r); - }) == nom.votes.end()); - res = res && (std::adjacent_find( - nom.accepted.begin(), nom.accepted.end(), - [](stellar::Value const& l, stellar::Value const& r) { - return !(l < r); - }) == nom.accepted.end()); - - return res; -} - -// only called after a call to isNewerStatement so safe to replace the -// mLatestNomination -void -NominationProtocol::recordEnvelope(SCPEnvelopeWrapperPtr env) -{ - auto const& st = env->getStatement(); - auto oldp = mLatestNominations.find(st.nodeID); - if (oldp == mLatestNominations.end()) - { - mLatestNominations.insert(std::make_pair(st.nodeID, env)); - } - else - { - oldp->second = env; - } - mSlot.recordStatement(env->getStatement()); -} - -void -NominationProtocol::emitNomination() -{ - ZoneScoped; - SCPStatement st; - st.nodeID = mSlot.getLocalNode()->getNodeID(); - st.pledges.type(SCP_ST_NOMINATE); - auto& nom = st.pledges.nominate(); - - nom.quorumSetHash = mSlot.getLocalNode()->getQuorumSetHash(); - - for (auto const& v : mVotes) - { - nom.votes.emplace_back(v->getValue()); - } - for (auto const& a : mAccepted) - { - nom.accepted.emplace_back(a->getValue()); - } - - SCPEnvelope envelope = mSlot.createEnvelope(st); - - auto envW = mSlot.getSCPDriver().wrapEnvelope(envelope); - - if (mSlot.processEnvelope(envW, true) == SCP::EnvelopeState::VALID) - { - if (!mLastEnvelope || - isNewerStatement(mLastEnvelope->getStatement().pledges.nominate(), - st.pledges.nominate())) - { - mLastEnvelope = envW; - if (mSlot.isFullyValidated()) - { - mSlot.getSCPDriver().emitEnvelope(envelope); - } - } - } - else - { - // there is a bug in the application if it queued up - // a statement for itself that it considers invalid - throw std::runtime_error("moved to a bad state (nomination)"); - } -} - -#ifdef BUILD_TESTS -void -NominationProtocol::emitDelayedNomination() -{ - // While the deferred emit was pending, processEnvelope may already have - // emitted (and self-processed) a statement carrying the same votes and - // accepted values. Emitting it again would be rejected as not newer by - // our own processEnvelope, which emitNomination treats as a bad state. - SCPNomination nom; - for (auto const& v : mVotes) - { - nom.votes.emplace_back(v->getValue()); - } - for (auto const& a : mAccepted) - { - nom.accepted.emplace_back(a->getValue()); - } - if (!isNewerStatement(mSlot.getLocalNode()->getNodeID(), nom)) - { - CLOG_DEBUG(SCP, "NominationProtocol: delayed emit already sent"); - return; - } - emitNomination(); -} -#endif - -bool -NominationProtocol::acceptPredicate(Value const& v, SCPStatement const& st) -{ - auto const& nom = st.pledges.nominate(); - bool res; - res = (std::find(nom.accepted.begin(), nom.accepted.end(), v) != - nom.accepted.end()); - return res; -} - -void -NominationProtocol::applyAll(SCPNomination const& nom, - std::function processor) -{ - for (auto const& v : nom.votes) - { - processor(v); - } - // NB: "accepted" should be a subset of "votes", so this should no-op - for (auto const& a : nom.accepted) - { - if (std::find(nom.votes.begin(), nom.votes.end(), a) == nom.votes.end()) - { - processor(a); - } - } -} - -void -NominationProtocol::updateRoundLeaders() -{ - ZoneScoped; - SCPQuorumSet myQSet = mSlot.getLocalNode()->getQuorumSet(); - - auto localID = mSlot.getLocalNode()->getNodeID(); - normalizeQSet(myQSet, &localID); // excludes self - - // Count potential leaders: only include nodes with non-zero weight, - // as nodes with weight 0 can never be elected as round leaders. - size_t maxLeaderCount = 0; - if (mSlot.getSCPDriver().getNodeWeight(localID, myQSet, true) > 0) - { - ++maxLeaderCount; - } - LocalNode::forAllNodes(myQSet, [&](NodeID const& cur) { - if (mSlot.getSCPDriver().getNodeWeight(cur, myQSet, false) > 0) - { - ++maxLeaderCount; - } - return true; - }); - - // Cap the number of iterations to prevent infinite loops if something has - // gone wrong - int iterationsRemaining = 1000; - while (mRoundLeaders.size() < maxLeaderCount) - { - // initialize priority with value derived from self - std::set newRoundLeaders; - - newRoundLeaders.insert(localID); - uint64 topPriority = getNodePriority(localID, myQSet); - - LocalNode::forAllNodes(myQSet, [&](NodeID const& cur) { - uint64 w = getNodePriority(cur, myQSet); - if (w > topPriority) - { - topPriority = w; - newRoundLeaders.clear(); - } - if (w == topPriority && w > 0) - { - newRoundLeaders.insert(cur); - } - return true; - }); - - if (topPriority == 0) - { - // No one had priority, so all nodes would choose themselves - // resulting in a timeout. Clear newRoundLeaders, allowing the - // algorithm to fast timeout and try again. - newRoundLeaders.clear(); - } - - // expand mRoundLeaders with the newly computed leaders - auto oldSize = mRoundLeaders.size(); - mRoundLeaders.insert(newRoundLeaders.begin(), newRoundLeaders.end()); - if (oldSize != mRoundLeaders.size()) - { - if (Logging::logDebug("SCP")) - { - CLOG_DEBUG(SCP, "updateRoundLeaders: {} -> {}", oldSize, - mRoundLeaders.size()); - for (auto const& rl : mRoundLeaders) - { - CLOG_DEBUG(SCP, " leader {}", - mSlot.getSCPDriver().toShortString(rl)); - } - } - return; - } - else - { - mRoundNumber++; - CLOG_DEBUG(SCP, - "updateRoundLeaders: fast timeout (would no op) -> {}", - mRoundNumber); - } - - --iterationsRemaining; - if (iterationsRemaining <= 0) - { - throw std::runtime_error( - "updateRoundLeaders appears to be stuck in an infinite loop"); - } - } - CLOG_DEBUG(SCP, "updateRoundLeaders: nothing to do"); -} - -uint64 -NominationProtocol::hashNode(bool isPriority, NodeID const& nodeID) -{ - ZoneScoped; - dbgAssert(!mPreviousValue.empty()); - return mSlot.getSCPDriver().computeHashNode( - mSlot.getSlotIndex(), mPreviousValue, isPriority, mRoundNumber, nodeID); -} - -uint64 -NominationProtocol::hashValue(Value const& value) -{ - ZoneScoped; - dbgAssert(!mPreviousValue.empty()); - return mSlot.getSCPDriver().computeValueHash( - mSlot.getSlotIndex(), mPreviousValue, mRoundNumber, value); -} - -uint64 -NominationProtocol::getNodePriority(NodeID const& nodeID, - SCPQuorumSet const& qset) -{ - ZoneScoped; - uint64 res; - uint64 w = mSlot.getSCPDriver().getNodeWeight( - nodeID, qset, nodeID == mSlot.getLocalNode()->getNodeID()); - - // if w > 0; w is inclusive here as - // 0 <= hashNode <= UINT64_MAX - if (w > 0 && hashNode(false, nodeID) <= w) - { - res = hashNode(true, nodeID); - } - else - { - res = 0; - } - return res; -} - -ValueWrapperPtr -NominationProtocol::getNewValueFromNomination(SCPNomination const& nom) -{ - ZoneScoped; - // pick the highest value we don't have from the leader - // sorted using hashValue. - ValueWrapperPtr newVote; - uint64 newHash = 0; - bool foundValidValue = false; - - auto pickValue = [&](Value const& value) { - ValueWrapperPtr valueToNominate; - auto vl = validateValue(value); - if (vl >= SCPDriver::kStructurallyValidValue) - { - valueToNominate = mSlot.getSCPDriver().wrapValue(value); - } - else - { - valueToNominate = extractValidValue(value); - } - if (valueToNominate) - { - foundValidValue = true; - if (mVotes.find(valueToNominate) == mVotes.end()) - { - uint64 curHash = hashValue(valueToNominate->getValue()); - if (curHash >= newHash) - { - newHash = curHash; - newVote = valueToNominate; - } - } - } - }; - - for (auto const& val : nom.accepted) - { - pickValue(val); - } - - // Move on to votes if we have not found a valid accepted value - if (!foundValidValue) - { - for (auto const& val : nom.votes) - { - pickValue(val); - } - } - - return newVote; -} - -SCP::EnvelopeState -NominationProtocol::processEnvelope(SCPEnvelopeWrapperPtr envelope) -{ - ZoneScoped; - auto const& st = envelope->getStatement(); - auto const& nom = st.pledges.nominate(); - - if (!isNewerStatement(st.nodeID, nom)) - return SCP::EnvelopeState::INVALID; - - if (!isSane(st)) - { - CLOG_TRACE(SCP, "NominationProtocol: message didn't pass sanity check"); - return SCP::EnvelopeState::INVALID; - } - - recordEnvelope(envelope); - - // Ensure node doesn't vote on future slots - if (mNominationStarted) - { - // tracks if we should emit a new nomination message - bool modified = false; - bool newCandidates = false; - - // attempts to promote some of the votes to accepted - for (auto const& v : nom.votes) - { - auto vw = mSlot.getSCPDriver().wrapValue(v); - if (mAccepted.find(vw) != mAccepted.end()) - { // v is already accepted - continue; - } - if (mSlot.federatedAccept( - [&v](SCPStatement const& st) -> bool { - auto const& nom = st.pledges.nominate(); - bool res; - res = (std::find(nom.votes.begin(), nom.votes.end(), - v) != nom.votes.end()); - return res; - }, - std::bind(&NominationProtocol::acceptPredicate, v, _1), - mLatestNominations)) - { - auto vl = validateValue(v); - if (vl >= SCPDriver::kStructurallyValidValue) - { - mAccepted.emplace(vw); - mVotes.emplace(vw); - modified = true; - } - else - { - // the value made it pretty far: - // see if we can vote for a variation that - // we consider valid - auto toVote = extractValidValue(v); - if (toVote) - { - if (mVotes.emplace(toVote).second) - { - modified = true; - } - } - } - } - } - // attempts to promote accepted values to candidates - for (auto const& a : mAccepted) - { - if (mCandidates.find(a) != mCandidates.end()) - { - continue; - } - if (mSlot.federatedRatify( - std::bind(&NominationProtocol::acceptPredicate, - a->getValue(), _1), - mLatestNominations)) - { - mCandidates.emplace(a); - newCandidates = true; - // Stop the timer, as there's no need to continue nominating, - // per the whitepaper: - // "As soon as `v` has a candidate value, however, it must cease - // voting to nominate `x` for any new values `x`" - mSlot.getSCPDriver().stopTimer(mSlot.getSlotIndex(), - Slot::NOMINATION_TIMER); - } - } - - // only take round leader votes if we're still looking for - // candidates - if (mCandidates.empty() && - mRoundLeaders.find(st.nodeID) != mRoundLeaders.end()) - { - auto newVote = getNewValueFromNomination(nom); - if (newVote) - { - mVotes.emplace(newVote); - modified = true; - mSlot.getSCPDriver().nominatingValue(mSlot.getSlotIndex(), - newVote->getValue()); - } - } - - if (modified) - { - emitNomination(); - } - - if (newCandidates) - { - mLatestCompositeCandidate = mSlot.getSCPDriver().combineCandidates( - mSlot.getSlotIndex(), mCandidates); - - mSlot.getSCPDriver().updatedCandidateValue( - mSlot.getSlotIndex(), mLatestCompositeCandidate->getValue()); - - mSlot.bumpState(mLatestCompositeCandidate->getValue(), false); - } - } - return SCP::EnvelopeState::VALID; -} - -std::vector -NominationProtocol::getStatementValues(SCPStatement const& st) -{ - std::vector res; - applyAll(st.pledges.nominate(), - [&](Value const& v) { res.emplace_back(v); }); - return res; -} - -void -NominationProtocol::stripUpgrades(ValueWrapperPtr& value) const -{ - ZoneScoped; - ValueWrapperPtr const newValue = - mSlot.getSCPDriver().stripAllUpgrades(value->getValue()); - if (newValue != nullptr && newValue->getValue() != value->getValue()) - { - CLOG_WARNING(SCP, - "Due to high timeouts, stripping upgrades from " - "nominated value: {} -> {}", - mSlot.getSCP().getValueString(value->getValue()), - mSlot.getSCP().getValueString(newValue->getValue())); - value = newValue; - } -} - -// attempts to nominate a value for consensus -bool -NominationProtocol::nominate(NominationValueSupplier const& makeValue, - Value const& previousValue, bool timedout) -{ - ZoneScoped; - - // No need to continue nominating, as per the whitepaper: - // "As soon as `v` has a candidate value, however, it must cease - // voting to nominate `x` for any new values `x`" - if (!mCandidates.empty()) - { - CLOG_DEBUG(SCP, "Skip nomination round {}, already have a candidate", - mRoundNumber); - return false; - } - - CLOG_DEBUG(SCP, "NominationProtocol::nominate ({})", mRoundNumber); - - bool updated = false; - - if (timedout) - { - mTimerExpCount++; - } - - if (timedout && !mNominationStarted) - { - CLOG_DEBUG(SCP, "NominationProtocol::nominate (TIMED OUT)"); - return false; - } - - mNominationStarted = true; - - mPreviousValue = previousValue; - - mRoundNumber++; - updateRoundLeaders(); - - std::chrono::milliseconds timeout = mSlot.getSCPDriver().computeTimeout( - mRoundNumber, /*isNomination=*/true); - - // add a few more values from other leaders - for (auto const& leader : mRoundLeaders) - { - auto it = mLatestNominations.find(leader); - if (it != mLatestNominations.end()) - { - auto lnmV = getNewValueFromNomination( - it->second->getStatement().pledges.nominate()); - if (lnmV) - { - mVotes.insert(lnmV); - updated = true; - mSlot.getSCPDriver().nominatingValue(mSlot.getSlotIndex(), - lnmV->getValue()); - } - } - } - - // Check if we are a leader for this round - if (mRoundLeaders.find(mSlot.getLocalNode()->getNodeID()) != - mRoundLeaders.end()) - { - // Check whether we've exceeded the upgrade timeout limit for this slot - bool const overUpgradeTimeoutLimit = - mTimerExpCount >= - mSlot.getSCPDriver().getUpgradeNominationTimeoutLimit(); - - bool shouldVoteForValue = false; - bool shouldStripUpgrades = false; - // Add our value if we haven't added any votes yet. - if (mVotes.empty()) - { - shouldVoteForValue = true; - } - - if (overUpgradeTimeoutLimit) - { - // We've exceeded the upgrade timeout limit. First, check whether - // all of our votes have upgrades. We only want to add a new value - // with stripped upgrades if there's a chance that it will help - // alleviate timeouts due to contentious upgrades. If we're already - // voting for a value without upgrades, then adding another is - // unlikely to help, as we are likely in one of the two following - // scenarios: - // 1. The current timeout situation is unrelated to upgrades - // 2. We just happened to add a value without upgrades earlier in - // this function - // In either case, adding a new value without upgrades will not - // help, and may make things worse by putting more strain on the - // network. - bool const allVotesHaveUpgrades = std::all_of( - mVotes.begin(), mVotes.end(), [&](ValueWrapperPtr const& v) { - return mSlot.getSCPDriver().hasUpgrades(v->getValue()); - }); - - if (allVotesHaveUpgrades) - { - // All votes have upgrades, so strip upgrades from `value` and - // vote for it. - shouldStripUpgrades = true; - shouldVoteForValue = true; - } - } - - if (shouldVoteForValue) - { - auto value = makeValue(); - // Making the value can make pending envelopes ready and reenter - // SCP, potentially externalizing this slot or creating a candidate. - // Do not vote or rearm its timer after nomination has stopped. - if (!mNominationStarted || !mCandidates.empty()) - { - return false; - } - if (value) - { - if (shouldStripUpgrades) - { - stripUpgrades(value); - } - auto ins = mVotes.insert(value); - if (ins.second) - { - updated = true; - mSlot.getSCPDriver().nominatingValue(mSlot.getSlotIndex(), - value->getValue()); - } - } - } - } - - std::shared_ptr slot = mSlot.shared_from_this(); - mSlot.getSCPDriver().setupTimer( - mSlot.getSlotIndex(), Slot::NOMINATION_TIMER, timeout, - [slot, makeValue, previousValue]() { - slot->nominate(makeValue, previousValue, true); - }); - -#ifdef BUILD_TESTS - // If a nomination-emit delay is configured, defer the emit by - // arming NOMINATION_EMIT_TIMER instead of broadcasting now. A subsequent - // nominate() will replace the timer so the eventual emit reflects the - // latest state. Skip the deferral once the SCP nomination timeout has - // fired (`timedout`) so retries don't compound latency, and cancel any - // still-armed timer in that case. - if (updated) - { - auto delay = mSlot.getSCPDriver().getNominationEmitDelayForTesting(); - if (delay > std::chrono::milliseconds::zero() && !timedout) - { - mSlot.getSCPDriver().setupTimer( - mSlot.getSlotIndex(), Slot::NOMINATION_EMIT_TIMER, delay, - [slot, this]() { emitDelayedNomination(); }); - return updated; - } - if (delay > std::chrono::milliseconds::zero()) - { - mSlot.getSCPDriver().stopTimer(mSlot.getSlotIndex(), - Slot::NOMINATION_EMIT_TIMER); - } - } -#endif - - if (updated) - { - emitNomination(); - } - else - { - CLOG_DEBUG(SCP, "NominationProtocol::nominate (SKIPPED)"); - } - - return updated; -} - -void -NominationProtocol::stopNomination() -{ - mNominationStarted = false; -} - -std::set const& -NominationProtocol::getLeaders() const -{ - return mRoundLeaders; -} - -Json::Value -NominationProtocol::getJsonInfo() -{ - Json::Value ret; - ret["roundnumber"] = mRoundNumber; - ret["started"] = mNominationStarted; - - int counter = 0; - for (auto const& v : mVotes) - { - ret["X"][counter] = mSlot.getSCP().getValueString(v->getValue()); - counter++; - } - - counter = 0; - for (auto const& v : mAccepted) - { - ret["Y"][counter] = mSlot.getSCP().getValueString(v->getValue()); - counter++; - } - - counter = 0; - for (auto const& v : mCandidates) - { - ret["Z"][counter] = mSlot.getSCP().getValueString(v->getValue()); - counter++; - } - - return ret; -} - -SCP::QuorumInfoNodeState -NominationProtocol::getState(NodeID const& n, bool selfAlreadyMovedOn) -{ - auto state = SCP::QuorumInfoNodeState::AGREE; - if (n == mSlot.getLocalNode()->getNodeID()) - { - // always mark myself as AGREE. - return state; - } - - bool enoughTimeHasPassed = - mTimerExpCount >= Slot::NUM_TIMEOUTS_THRESHOLD_FOR_REPORTING || - - selfAlreadyMovedOn; - - auto it = mLatestNominations.find(n); - if (it == mLatestNominations.end()) - { - if (enoughTimeHasPassed) - { - state = SCP::QuorumInfoNodeState::MISSING; - } - else - { - // Not enough time has passed to start calling this node missing. - state = SCP::QuorumInfoNodeState::NO_INFO; - } - } - else - { - if (enoughTimeHasPassed && mLastEnvelope) - { - // Enough time has passed & we have some information on this node. - // We'll examine the information to determine the state of this - // node. - auto const& other = - it->second->getStatement().pledges.nominate().accepted; - auto const& mine = - mLastEnvelope->getStatement().pledges.nominate().accepted; - - bool notEq, isSubset = isSubsetHelper(other, mine, notEq); - if (isSubset) - { - if (notEq) - { - // If there's some values that I've accepted that - // they haven't, they are slow. - state = SCP::QuorumInfoNodeState::DELAYED; - } - } - else - { - // If they have accepted a value that I haven't, - // it's likely that I'll never accept it. - state = SCP::QuorumInfoNodeState::DISAGREE; - } - } - } - return state; -} - -void -NominationProtocol::setStateFromEnvelope(SCPEnvelopeWrapperPtr e) -{ - if (mNominationStarted) - { - throw std::runtime_error( - "Cannot set state after nomination is started"); - } - recordEnvelope(e); - auto const& nom = e->getStatement().pledges.nominate(); - for (auto const& a : nom.accepted) - { - mAccepted.emplace(mSlot.getSCPDriver().wrapValue(a)); - } - for (auto const& v : nom.votes) - { - mVotes.emplace(mSlot.getSCPDriver().wrapValue(v)); - } - - mLastEnvelope = e; -} - -bool -NominationProtocol::processCurrentState( - std::function const& f, bool forceSelf) const -{ - for (auto const& n : mLatestNominations) - { - // only return messages for self if the slot is fully validated - if (forceSelf || !(n.first == mSlot.getSCP().getLocalNodeID()) || - mSlot.isFullyValidated()) - { - if (!f(n.second->getEnvelope())) - { - return false; - } - } - } - return true; -} - -SCPEnvelope const* -NominationProtocol::getLatestMessage(NodeID const& id) const -{ - auto it = mLatestNominations.find(id); - if (it != mLatestNominations.end()) - { - return &it->second->getEnvelope(); - } - return nullptr; -} -} diff --git a/src/scp/NominationProtocol.h b/src/scp/NominationProtocol.h deleted file mode 100644 index cf0e3656e..000000000 --- a/src/scp/NominationProtocol.h +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright 2014 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 "lib/json/json-forwards.h" -#include "scp/SCP.h" -#include -#include -#include -#include -#include - -namespace stellar -{ -class NominationProtocol -{ - protected: - Slot& mSlot; - - int32 mRoundNumber; - - ValueWrapperPtrSet mVotes; // X - ValueWrapperPtrSet mAccepted; // Y - ValueWrapperPtrSet mCandidates; // Z - std::map mLatestNominations; // N - - SCPEnvelopeWrapperPtr mLastEnvelope; // last envelope emitted by this node - - // nodes from quorum set that have the highest priority this round - std::set mRoundLeaders; - - // true if 'nominate' was called - bool mNominationStarted; - - // the latest (if any) candidate value - ValueWrapperPtr mLatestCompositeCandidate; - - // the value from the previous slot - Value mPreviousValue; - - bool isNewerStatement(NodeID const& nodeID, SCPNomination const& st); - - // returns true if 'p' is a subset of 'v' - // also sets 'notEqual' if p and v differ - // note: p and v must be sorted - static bool isSubsetHelper(xdr::xvector const& p, - xdr::xvector const& v, bool& notEqual); - - SCPDriver::ValidationLevel validateValue(Value const& v); - ValueWrapperPtr extractValidValue(Value const& value); - - bool isSane(SCPStatement const& st); - - void recordEnvelope(SCPEnvelopeWrapperPtr env); - - void emitNomination(); - -#ifdef BUILD_TESTS - // Fires the deferred emit armed by nominate() when a nomination-emit - // delay is configured. Skips the emit if this node already sent a - // statement at least as recent in the meantime. - void emitDelayedNomination(); -#endif - - // returns true if v is in the accepted list from the statement - static bool acceptPredicate(Value const& v, SCPStatement const& st); - - // applies 'processor' to all values from the passed in nomination - static void applyAll(SCPNomination const& nom, - std::function processor); - - // updates the set of nodes that have priority over the others - void updateRoundLeaders(); - - // computes Gi(isPriority?P:N, prevValue, mRoundNumber, nodeID) - // from the paper - uint64 hashNode(bool isPriority, NodeID const& nodeID); - - // computes Gi(K, prevValue, mRoundNumber, value) - uint64 hashValue(Value const& value); - - uint64 getNodePriority(NodeID const& nodeID, SCPQuorumSet const& qset); - - // returns the highest value that we don't have yet, that we should - // vote for, extracted from a nomination. - // returns nullptr if no new value was found - ValueWrapperPtr getNewValueFromNomination(SCPNomination const& nom); - - public: - static bool isNewerStatement(SCPNomination const& oldst, - SCPNomination const& st); - - NominationProtocol(Slot& slot); - - // Preview the cumulative leaders for the first `rounds` nomination calls. - // Uses the same fast-forward/tie handling as live nomination, without - // starting nomination, arming timers, or emitting statements. - static std::set - predictLeaders(Slot& slot, Value const& previousValue, uint32_t rounds); - - SCP::EnvelopeState processEnvelope(SCPEnvelopeWrapperPtr envelope); - - static std::vector getStatementValues(SCPStatement const& st); - - // attempts to nominate a value for consensus - bool nominate(NominationValueSupplier const& makeValue, - Value const& previousValue, bool timedout); - - // stops the nomination protocol - void stopNomination(); - - // return the current leaders - std::set const& getLeaders() const; - - ValueWrapperPtr const& - getLatestCompositeCandidate() const - { - return mLatestCompositeCandidate; - } - - Json::Value getJsonInfo(); - - SCP::QuorumInfoNodeState getState(NodeID const& node, - bool selfAlreadyMovedOn); - - SCPEnvelope const* - getLastMessageSend() const - { - return mLastEnvelope ? &mLastEnvelope->getEnvelope() : nullptr; - } - - void setStateFromEnvelope(SCPEnvelopeWrapperPtr e); - - bool processCurrentState(std::function const& f, - bool forceSelf) const; - - // returns the latest message from a node - // or nullptr if not found - SCPEnvelope const* getLatestMessage(NodeID const& id) const; - - private: - // The number of times the timer has expired - // Used for the quorum endpoint. - uint32_t mTimerExpCount; - - // Strip any upgrades that `value` may have, modifying it in place. Does - // nothing if `value` has no upgrades. - void stripUpgrades(ValueWrapperPtr& value) const; -}; -} diff --git a/src/scp/SCP.cpp b/src/scp/SCP.cpp index 8afbc3a6b..f8f5641f7 100644 --- a/src/scp/SCP.cpp +++ b/src/scp/SCP.cpp @@ -34,29 +34,17 @@ SCP::receiveEnvelope(SCPEnvelopeWrapperPtr envelope) } bool -SCP::nominate(uint64 slotIndex, NominationValueSupplier const& makeValue, - Value const& previousValue) +SCP::startSlot(uint64 slotIndex, ProposalValueSupplier const& makeValue) { dbgAssert(isValidator()); - return getSlot(slotIndex, true)->nominate(makeValue, previousValue, false); + return getSlot(slotIndex, true)->startLeaderRounds(makeValue); } -std::set -SCP::predictNominationLeaders(uint64 slotIndex, Value const& previousValue, - uint32_t rounds) -{ - Slot preview(slotIndex, *this); - return NominationProtocol::predictLeaders(preview, previousValue, rounds); -} - -void -SCP::stopNomination(uint64 slotIndex) +uint32 +SCP::getLeaderRound(uint64 slotIndex) { - auto s = getSlot(slotIndex, false); - if (s) - { - s->stopNomination(); - } + auto slot = getSlot(slotIndex, false); + return slot ? slot->getLeaderRound() : 0; } void @@ -287,17 +275,6 @@ SCP::getMissingNodes(NodeID const& id, uint64 index) return ret; } -std::set -SCP::getNominationLeaders(uint64 slotIndex) -{ - auto slot = getSlot(slotIndex, false); - if (slot) - { - return slot->getNominationLeaders(); - } - return {}; -} - bool SCP::isValidator() { @@ -433,8 +410,7 @@ SCP::getLatestMessage(NodeID const& id) } bool -SCP::isNewerNominationOrBallotSt(SCPStatement const& oldSt, - SCPStatement const& newSt) +SCP::isNewerStatement(SCPStatement const& oldSt, SCPStatement const& newSt) { if (oldSt.slotIndex != newSt.slotIndex || !(oldSt.nodeID == newSt.nodeID)) { @@ -444,7 +420,7 @@ SCP::isNewerNominationOrBallotSt(SCPStatement const& oldSt, auto slot = getSlot(oldSt.slotIndex, false); if (slot) { - return slot->isNewerNominationOrBallotSt(oldSt, newSt); + return slot->isNewerStatement(oldSt, newSt); } return false; diff --git a/src/scp/SCP.h b/src/scp/SCP.h index e49e3535d..2fe4fbd7b 100644 --- a/src/scp/SCP.h +++ b/src/scp/SCP.h @@ -51,13 +51,16 @@ class SCP // invokes the appropriate methods EnvelopeState receiveEnvelope(SCPEnvelopeWrapperPtr envelope); - // Start nomination for slotIndex, constructing a local value only if needed - // previousValue is the value from slotIndex-1 - bool nominate(uint64 slotIndex, NominationValueSupplier const& makeValue, - Value const& previousValue); + // Starts consensus on slotIndex for the local node (its trigger fired). + // There is no nomination phase: each round (ballot counter) has one leader + // given by SCPDriver::getRoundLeader, and nodes vote for the leader's + // ballot. `makeValue` is invoked only if the local node becomes the leader + // of a round and has no value to put forward. + bool startSlot(uint64 slotIndex, ProposalValueSupplier const& makeValue); - // stops nomination for a slot - void stopNomination(uint64 slotIndex); + // The round the local node is in for slotIndex (0 if it has not started + // the slot or does not know it). + uint32 getLeaderRound(uint64 slotIndex); // Local QuorumSet interface (can be dynamically updated) void updateLocalQuorumSet(SCPQuorumSet const& qSet); @@ -90,14 +93,6 @@ class SCP // Get a list of nodes from id's quorum set that are missing in consensus std::set getMissingNodes(NodeID const& id, uint64 index = 0); - // returns the current nomination leaders for the given slot - std::set getNominationLeaders(uint64 slotIndex); - - // A read-only preview; does not create or modify a live SCP slot. - std::set predictNominationLeaders(uint64 slotIndex, - Value const& previousValue, - uint32_t rounds); - // Purges all data relative to slots that fall outside the range // [minSlotIndex, maxSlotIndex]. Either bound may be nullopt to skip // that direction. The slotToKeep slot is never purged. @@ -148,8 +143,8 @@ class SCP // or nullptr if not found SCPEnvelope const* getLatestMessage(NodeID const& id); - bool isNewerNominationOrBallotSt(SCPStatement const& oldSt, - SCPStatement const& newSt); + // returns true if newSt is newer than oldSt (same node, same slot) + bool isNewerStatement(SCPStatement const& oldSt, SCPStatement const& newSt); // returns messages that contributed to externalizing the slot // (or empty if the slot didn't externalize) diff --git a/src/scp/SCPDriver.cpp b/src/scp/SCPDriver.cpp index 9e54c0777..4f4137e51 100644 --- a/src/scp/SCPDriver.cpp +++ b/src/scp/SCPDriver.cpp @@ -10,28 +10,11 @@ #include "crypto/KeyUtils.h" #include "crypto/SecretKey.h" #include "util/GlobalChecks.h" -#include "util/numeric.h" #include "xdrpp/marshal.h" namespace stellar { -namespace -{ -uint64 -computeWeight(uint64 m, uint64 total, uint64 threshold) -{ - uint64 res; - releaseAssert(threshold <= total); - // Since threshold <= total, calculating res=m*threshold/total will always - // produce res <= m, and we do not need to handle the possibility of this - // call returning false (indicating overflow). - bool noOverflow = bigDivideUnsigned(res, m, threshold, total, ROUND_UP); - releaseAssert(noOverflow); - return res; -} -} // namespace - bool WrappedValuePtrComparator::operator()(ValueWrapperPtr const& l, ValueWrapperPtr const& r) const @@ -90,95 +73,4 @@ SCPDriver::toShortString(NodeID const& pk) const return KeyUtils::toShortString(pk); } -// values used to switch hash function between priority and neighborhood checks -static uint32 const hash_N = 1; -static uint32 const hash_P = 2; -static uint32 const hash_K = 3; - -uint64 -SCPDriver::hashHelper( - uint64 slotIndex, Value const& prev, - std::function>&)> extra) -{ - std::vector> vals; - vals.emplace_back(xdr::xdr_to_opaque(slotIndex)); - vals.emplace_back(xdr::xdr_to_opaque(prev)); - extra(vals); - Hash t = getHashOf(vals); - uint64 res = 0; - for (size_t i = 0; i < sizeof(res); i++) - { - res = (res << 8) | t[i]; - } - return res; -} - -uint64 -SCPDriver::computeHashNode(uint64 slotIndex, Value const& prev, bool isPriority, - int32_t roundNumber, NodeID const& nodeID) -{ -#ifdef BUILD_TESTS - if (mPriorityLookupForTesting) - { - return isPriority ? mPriorityLookupForTesting(nodeID) : 0; - } -#endif - return hashHelper( - slotIndex, prev, [&](std::vector>& vals) { - vals.emplace_back(xdr::xdr_to_opaque(isPriority ? hash_P : hash_N)); - vals.emplace_back(xdr::xdr_to_opaque(roundNumber)); - vals.emplace_back(xdr::xdr_to_opaque(nodeID)); - }); -} - -uint64 -SCPDriver::computeValueHash(uint64 slotIndex, Value const& prev, - int32_t roundNumber, Value const& value) -{ - return hashHelper(slotIndex, prev, - [&](std::vector>& vals) { - vals.emplace_back(xdr::xdr_to_opaque(hash_K)); - vals.emplace_back(xdr::xdr_to_opaque(roundNumber)); - vals.emplace_back(xdr::xdr_to_opaque(value)); - }); -} - -// if a validator is repeated multiple times its weight is only the -// weight of the first occurrence -uint64 -SCPDriver::getNodeWeight(NodeID const& nodeID, SCPQuorumSet const& qset, - bool isLocalNode) const -{ - if (isLocalNode) - { - // local node is in all quorum sets - return UINT64_MAX; - } - - uint64 n = qset.threshold; - uint64 d = qset.innerSets.size() + qset.validators.size(); - uint64 res; - - for (auto const& qsetNode : qset.validators) - { - if (qsetNode == nodeID) - { - res = computeWeight(UINT64_MAX, d, n); - return res; - } - } - - for (auto const& q : qset.innerSets) - { - uint64 leafW = SCPDriver::getNodeWeight(nodeID, q, isLocalNode); - if (leafW) - { - res = computeWeight(leafW, d, n); - return res; - } - } - - return 0; -} - } diff --git a/src/scp/SCPDriver.h b/src/scp/SCPDriver.h index 19fcbfc5e..a41af7d78 100644 --- a/src/scp/SCPDriver.h +++ b/src/scp/SCPDriver.h @@ -44,10 +44,10 @@ class ValueWrapper : public NonMovableOrCopyable typedef std::shared_ptr SCPQuorumSetPtr; typedef std::shared_ptr ValueWrapperPtr; -// Called only when the local node is a nomination leader and needs its own -// value. May return nullptr if the application can no longer propose for the -// slot. The callback must remain valid across nomination timeouts. -using NominationValueSupplier = std::function; +// Called only when the local node is the leader of a round and needs to build +// its own value. May return nullptr if the application can no longer propose +// for the slot. The callback must remain valid for the lifetime of the slot. +using ProposalValueSupplier = std::function; class WrappedValuePtrComparator { @@ -155,7 +155,10 @@ class SCPDriver // be validated due to parallel downloading (e.g. it's for LCL+1 but the // node is still downloading the tx set), then kStructurallyValidValue // should be used. - // validation can be *more* restrictive during nomination as needed + // validation is *more* restrictive when `proposal` is set: that mode is + // used before the local node adopts (votes for) a round leader's value, + // and for the leader's own value. It is where the application enforces + // what it is willing to propose (e.g. only upgrades it has armed). // NB: validation levels are ordered enum ValidationLevel { @@ -172,22 +175,11 @@ class SCPDriver kFullyValidatedValue = 3 }; virtual ValidationLevel - validateValue(uint64 slotIndex, Value const& value, bool nomination) const + validateValue(uint64 slotIndex, Value const& value, bool proposal) const { return kMaybeValidNotCurrentValue; } - // `extractValidValue` transforms the value, if possible to a different - // value that the local node would agree to (fully validated). - // This is used during nomination when encountering an invalid value (ie - // validateValue did not return `kFullyValidatedValue` for this value). - // returning nullptr means no valid value could be extracted - virtual ValueWrapperPtr - extractValidValue(uint64 slotIndex, Value const& value) - { - return nullptr; - } - // Helper function to craft an empty-tx-set value from a Value. virtual Value makeEmptyTxSetValueFromValue(Value const& v) const = 0; @@ -215,33 +207,23 @@ class SCPDriver virtual Hash getHashOf(std::vector> const& vals) const = 0; - // `computeHashNode` is used by the nomination protocol to - // randomize the order of messages between nodes. - virtual uint64 computeHashNode(uint64 slotIndex, Value const& prev, - bool isPriority, int32_t roundNumber, - NodeID const& nodeID); - - // `computeValueHash` is used by the nomination protocol to - // randomize the relative order between values. - virtual uint64 computeValueHash(uint64 slotIndex, Value const& prev, - int32_t roundNumber, Value const& value); - - // `combineCandidates` computes the composite value based off a list - // of candidate values. - virtual ValueWrapperPtr - combineCandidates(uint64 slotIndex, - ValueWrapperPtrSet const& candidates) = 0; - - // Checks whether `v` contains upgrades - virtual bool hasUpgrades(Value const& v) = 0; - - // `stripAllUpgrades` returns a new value with all upgrades removed - virtual ValueWrapperPtr stripAllUpgrades(Value const& v) = 0; - - // Returns the maximum number of nomination timeouts permitted per slot for - // the currently set upgrade. Defaults to the maximum uint32_t value - // (effectively unlimited) if the upgrade does not specify a limit. - virtual uint32_t getUpgradeNominationTimeoutLimit() const = 0; + // `getRoundLeader` returns the leader of round `round` (1-based, a round + // is a ballot counter) for `slotIndex`. Every node that takes part in + // consensus on the slot must compute the same answer. Returns nullopt when + // the schedule is not known for that slot (e.g. the slot is not the next + // one to close), in which case the local node neither proposes nor adopts. + virtual std::optional getRoundLeader(uint64 slotIndex, + uint32 round) = 0; + + // `isProposalPending` returns true if the application holds a statement + // of `leader` for `slotIndex` that it has not handed to SCP yet (e.g. it + // is still fetching the transaction set of its value). SCP then gives a + // round leader one more timeout before moving to the next round. + virtual bool + isProposalPending(uint64 slotIndex, NodeID const& leader) const + { + return false; + } // `setupTimer`: requests to trigger 'cb' after timeout // if cb is nullptr, the timer is cancelled @@ -253,22 +235,7 @@ class SCPDriver // `computeTimeout` computes a timeout given a round number // it should be sufficiently large such that nodes in a // quorum can exchange 4 messages - virtual std::chrono::milliseconds computeTimeout(uint32 roundNumber, - bool isNomination) = 0; - -#ifdef BUILD_TESTS - virtual std::chrono::milliseconds - getNominationEmitDelayForTesting() const - { - return std::chrono::milliseconds::zero(); - } -#endif - - // returns the weight of the node within the qset normalized between - // 0-UINT64_MAX. If `nodeID` is the local node, then set `isLocalNode` to - // `true`. - virtual uint64 getNodeWeight(NodeID const& nodeID, SCPQuorumSet const& qset, - bool isLocalNode) const; + virtual std::chrono::milliseconds computeTimeout(uint32 roundNumber) = 0; // Inform about events happening within the consensus algorithm. @@ -284,21 +251,20 @@ class SCPDriver { } - // ``nominatingValue`` is called every time the local instance nominates - // a new value. + // the following methods are used for monitoring of the SCP subsystem + // most implementation don't really need to do anything with these + + // `proposedValue` is called when the local node, as the leader of + // `round`, puts `value` forward. virtual void - nominatingValue(uint64 slotIndex, Value const& value) + proposedValue(uint64 slotIndex, uint32 round, Value const& value) { } - // the following methods are used for monitoring of the SCP subsystem - // most implementation don't really need to do anything with these - - // `updatedCandidateValue` is called every time a new candidate value - // is included in the candidate set, the value passed in is - // a composite value + // `enteredRound` is called when the local node moves to `round` for + // `slotIndex`. `timedOut` is true when a timer expiry caused the move. virtual void - updatedCandidateValue(uint64 slotIndex, Value const& value) + enteredRound(uint64 slotIndex, uint32 round, bool timedOut) { } @@ -347,19 +313,5 @@ class SCPDriver measureAndRecordBallotBlockedOnTxSet(uint64 slotIndex, Value const& value) { } - -#ifdef BUILD_TESTS - std::function mPriorityLookupForTesting; - void - setPriorityLookup(std::function const& f) - { - mPriorityLookupForTesting = f; - } -#endif - - private: - uint64 - hashHelper(uint64 slotIndex, Value const& prev, - std::function>&)> extra); }; } diff --git a/src/scp/Slot.cpp b/src/scp/Slot.cpp index 4971fc2e2..37ce296ef 100644 --- a/src/scp/Slot.cpp +++ b/src/scp/Slot.cpp @@ -24,31 +24,18 @@ Slot::Slot(uint64 slotIndex, SCP& scp) : mSlotIndex(slotIndex) , mSCP(scp) , mBallotProtocol(*this) - , mNominationProtocol(*this) , mFullyValidated(scp.getLocalNode()->isValidator()) , mGotVBlocking(false) { } -ValueWrapperPtr const& -Slot::getLatestCompositeCandidate() -{ - return mNominationProtocol.getLatestCompositeCandidate(); -} - std::vector Slot::getLatestMessagesSend() const { std::vector res; if (mFullyValidated) { - SCPEnvelope const* e; - e = mNominationProtocol.getLastMessageSend(); - if (e) - { - res.emplace_back(*e); - } - e = mBallotProtocol.getLastMessageSend(); + SCPEnvelope const* e = mBallotProtocol.getLastMessageSend(); if (e) { res.emplace_back(*e); @@ -68,12 +55,11 @@ Slot::setStateFromEnvelope(SCPEnvelopeWrapperPtr env) if (e.statement.pledges.type() == SCPStatementType::SCP_ST_NOMINATE) { - mNominationProtocol.setStateFromEnvelope(env); - } - else - { - mBallotProtocol.setStateFromEnvelope(env); + // Nomination no longer exists: ignore state saved by a build that + // still had it. + return; } + mBallotProtocol.setStateFromEnvelope(env); if (!prev) { @@ -91,53 +77,24 @@ void Slot::processCurrentState(std::function const& f, bool forceSelf) const { - mNominationProtocol.processCurrentState(f, forceSelf) && - mBallotProtocol.processCurrentState(f, forceSelf); + mBallotProtocol.processCurrentState(f, forceSelf); } SCPEnvelope const* Slot::getLatestMessage(NodeID const& id) const { - auto m = mBallotProtocol.getLatestMessage(id); - if (m == nullptr) - { - m = mNominationProtocol.getLatestMessage(id); - } - return m; + return mBallotProtocol.getLatestMessage(id); } bool -Slot::isNewerNominationOrBallotSt(SCPStatement const& oldSt, - SCPStatement const& newSt) +Slot::isNewerStatement(SCPStatement const& oldSt, SCPStatement const& newSt) { - bool oldNomination = - oldSt.pledges.type() == SCPStatementType::SCP_ST_NOMINATE; - bool newNomination = - newSt.pledges.type() == SCPStatementType::SCP_ST_NOMINATE; - - if (oldNomination != newNomination) + if (oldSt.pledges.type() == SCPStatementType::SCP_ST_NOMINATE || + newSt.pledges.type() == SCPStatementType::SCP_ST_NOMINATE) { return false; } - - bool replace = false; - if (oldNomination) - { - if (NominationProtocol::isNewerStatement(oldSt.pledges.nominate(), - newSt.pledges.nominate())) - { - replace = true; - } - } - else - { - if (BallotProtocol::isNewerStatement(oldSt, newSt)) - { - replace = true; - } - } - - return replace; + return BallotProtocol::isNewerStatement(oldSt, newSt); } std::vector @@ -173,12 +130,11 @@ Slot::processEnvelope(SCPEnvelopeWrapperPtr envelope, bool self) if (st.pledges.type() == SCPStatementType::SCP_ST_NOMINATE) { - res = mNominationProtocol.processEnvelope(envelope); - } - else - { - res = mBallotProtocol.processEnvelope(envelope, self); + // There is no nomination phase: the round leader's ballot is the + // proposal. Nomination statements are dropped. + return SCP::EnvelopeState::INVALID; } + res = mBallotProtocol.processEnvelope(envelope, self); if (!prev && res == SCP::VALID) { @@ -211,12 +167,6 @@ Slot::processEnvelope(SCPEnvelopeWrapperPtr envelope, bool self) return res; } -bool -Slot::abandonBallot() -{ - return mBallotProtocol.abandonBallot(0); -} - bool Slot::bumpState(Value const& value, bool force) { @@ -225,22 +175,15 @@ Slot::bumpState(Value const& value, bool force) } bool -Slot::nominate(NominationValueSupplier const& makeValue, - Value const& previousValue, bool timedout) +Slot::startLeaderRounds(ProposalValueSupplier const& makeValue) { - return mNominationProtocol.nominate(makeValue, previousValue, timedout); + return mBallotProtocol.startLeaderRounds(makeValue); } -void -Slot::stopNomination() +uint32 +Slot::getLeaderRound() const { - mNominationProtocol.stopNomination(); -} - -std::set -Slot::getNominationLeaders() const -{ - return mNominationProtocol.getLeaders(); + return mBallotProtocol.getLeaderRound(); } bool @@ -300,7 +243,9 @@ Slot::getStatementValues(SCPStatement const& st) std::vector res; if (st.pledges.type() == SCP_ST_NOMINATE) { - res = NominationProtocol::getStatementValues(st); + auto const& nom = st.pledges.nominate(); + res.insert(res.end(), nom.votes.begin(), nom.votes.end()); + res.insert(res.end(), nom.accepted.begin(), nom.accepted.end()); } else { @@ -378,7 +323,6 @@ Slot::getJsonInfo(bool fullKeys) } ret["validated"] = mFullyValidated; - ret["nomination"] = mNominationProtocol.getJsonInfo(); ret["ballotProtocol"] = mBallotProtocol.getJsonInfo(); return ret; @@ -387,12 +331,7 @@ Slot::getJsonInfo(bool fullKeys) SCP::QuorumInfoNodeState Slot::getState(NodeID const& node, bool selfAlreadyMovedOn) { - auto b = mBallotProtocol.getState(node, selfAlreadyMovedOn); - if (b != SCP::QuorumInfoNodeState::NO_INFO) - { - return b; - } - return mNominationProtocol.getState(node, selfAlreadyMovedOn); + return mBallotProtocol.getState(node, selfAlreadyMovedOn); } Json::Value diff --git a/src/scp/Slot.h b/src/scp/Slot.h index 3b556a904..06c6dbfb9 100644 --- a/src/scp/Slot.h +++ b/src/scp/Slot.h @@ -6,7 +6,6 @@ #include "BallotProtocol.h" #include "LocalNode.h" -#include "NominationProtocol.h" #include "lib/json/json-forwards.h" #include "scp/SCP.h" #include @@ -29,7 +28,6 @@ class Slot : public std::enable_shared_from_this SCP& mSCP; BallotProtocol mBallotProtocol; - NominationProtocol mNominationProtocol; // keeps track of all statements seen so far for this slot. // it is used for debugging purpose @@ -81,8 +79,6 @@ class Slot : public std::enable_shared_from_this return mBallotProtocol; } - ValueWrapperPtr const& getLatestCompositeCandidate(); - // returns the latest messages the slot emitted std::vector getLatestMessagesSend() const; @@ -99,8 +95,7 @@ class Slot : public std::enable_shared_from_this SCPEnvelope const* getLatestMessage(NodeID const& id) const; // Return true if the statement is latest for a node that sent it - bool isNewerNominationOrBallotSt(SCPStatement const& oldSt, - SCPStatement const& newSt); + bool isNewerStatement(SCPStatement const& oldSt, SCPStatement const& newSt); // returns messages that helped this slot externalize std::vector getExternalizingState() const; @@ -115,8 +110,6 @@ class Slot : public std::enable_shared_from_this SCP::EnvelopeState processEnvelope(SCPEnvelopeWrapperPtr envelope, bool self); - bool abandonBallot(); - // bumps the ballot based on the local state and the value passed in: // in prepare phase, attempts to take value // otherwise, no-ops @@ -124,14 +117,13 @@ class Slot : public std::enable_shared_from_this // the state if no value was prepared bool bumpState(Value const& value, bool force); - // attempts to nominate a value for consensus - bool nominate(NominationValueSupplier const& makeValue, - Value const& previousValue, bool timedout); - - void stopNomination(); + // Starts leader rounds for this slot (the local node is ready to take + // part in consensus on it). `makeValue` is invoked only if the local node + // becomes the leader of a round and has no value to put forward. + bool startLeaderRounds(ProposalValueSupplier const& makeValue); - // returns the current nomination leaders - std::set getNominationLeaders() const; + // The round the local node is in (0 if it has not started the slot). + uint32 getLeaderRound() const; bool isFullyValidated() const; void setFullyValidated(bool fullyValidated); @@ -196,14 +188,10 @@ class Slot : public std::enable_shared_from_this enum timerIDs { - NOMINATION_TIMER = 0, + // Runs while the local node waits for the leader of its current round + // to put a value forward. + LEADER_TIMER = 0, BALLOT_PROTOCOL_TIMER = 1, -#ifdef BUILD_TESTS - // Test-only: defers broadcast of a nomination vote by - // ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING. Used to simulate - // slow nominators without breaking the protocol. - NOMINATION_EMIT_TIMER = 2, -#endif }; // The number of times the timer has to expire before we consider the node diff --git a/src/scp/test/SCPTests.cpp b/src/scp/test/SCPTests.cpp index 8f57dda9b..6feb5d763 100644 --- a/src/scp/test/SCPTests.cpp +++ b/src/scp/test/SCPTests.cpp @@ -17,6 +17,10 @@ #include "xdrpp/marshal.h" #include "xdrpp/printer.h" #include +#include +#include +#include +#include // General convention in this file is that numbers in parenthesis // refer to the rule number in the related protocol from the white paper @@ -41,18 +45,15 @@ class TestSCP : public SCPDriver SCP mSCP; uint32_t mInitialBallotTimeoutMS = 1000; uint32_t mIncrementBallotTimeoutMS = 1000; - uint32_t mInitialNominationTimeoutMS = 1000; - uint32_t mIncrementNominationTimeoutMS = 1000; TestSCP(NodeID const& nodeID, SCPQuorumSet const& qSetLocal, bool isValidator = true) : mSCP(*this, nodeID, isValidator, qSetLocal) { - mPriorityLookup = [&](NodeID const& n) { - return (n == mSCP.getLocalNodeID()) ? 1000 : 1; - }; - - mHashValueCalculator = [&](Value const& v) { return 0; }; + // By default the local node leads every round: a ballot timeout then + // bumps the counter with the value the node already votes for, like + // the plain ballot protocol. + mRoundLeader = [&](uint64, uint32) { return mSCP.getLocalNodeID(); }; auto localQSet = std::make_shared(mSCP.getLocalQuorumSet()); @@ -73,11 +74,11 @@ class TestSCP : public SCPDriver SCPDriver::ValidationLevel validateValue(uint64 slotIndex, Value const& value, - bool nomination) const override + bool proposal) const override { if (mValidateValueOverride) { - return mValidateValueOverride(slotIndex, value, nomination); + return mValidateValueOverride(slotIndex, value, proposal); } // If we're tracking download wait time for this value, it's awaiting // download @@ -178,62 +179,55 @@ class TestSCP : public SCPDriver mEnvs.push_back(envelope); } - // used to test BallotProtocol and bypass nomination + // used to test BallotProtocol directly, without a round leader bool bumpState(uint64 slotIndex, Value const& v) { return mSCP.getSlot(slotIndex, true)->bumpState(v, true); } + // starts the slot the way the application does at its trigger; `value` + // is what the local node proposes if it leads a round bool - nominate(uint64 slotIndex, Value const& value, bool timedout) + startSlot(uint64 slotIndex, Value const& value) { auto wv = wrapValue(value); - return mSCP.getSlot(slotIndex, true) - ->nominate([wv]() { return wv; }, value, timedout); + return mSCP.startSlot(slotIndex, [this, wv]() { + ++mValuesBuilt; + return mMakeValueOverride ? mMakeValueOverride() : wv; + }); } - // only used by nomination protocol - ValueWrapperPtr - combineCandidates(uint64 slotIndex, - ValueWrapperPtrSet const& candidates) override + std::optional + getRoundLeader(uint64 slotIndex, uint32 round) override { - REQUIRE(candidates.size() == mExpectedCandidates.size()); - auto it1 = candidates.begin(); - auto it2 = mExpectedCandidates.end(); - for (; it1 != candidates.end() && it2 != mExpectedCandidates.end(); - it1++, it2++) - { - REQUIRE((*it1)->getValue() == *it2); - } - - REQUIRE(!mCompositeValue.empty()); - - return wrapValue(mCompositeValue); + return mRoundLeader(slotIndex, round); } - bool - hasUpgrades(Value const& v) override + void + proposedValue(uint64 slotIndex, uint32 round, Value const& value) override { - // Not implemented - releaseAssert(false); + mProposedValues.emplace_back(round, value); } - ValueWrapperPtr - stripAllUpgrades(Value const& v) override + void + enteredRound(uint64 slotIndex, uint32 round, bool timedOut) override { - // Not implemented - releaseAssert(false); + mEnteredRounds.emplace_back(round, timedOut); } - uint32_t - getUpgradeNominationTimeoutLimit() const override + bool + isProposalPending(uint64, NodeID const&) const override { - return std::numeric_limits::max(); + return mProposalPending; } - std::set mExpectedCandidates; - Value mCompositeValue; + std::function(uint64, uint32)> mRoundLeader; + bool mProposalPending{false}; + std::function mMakeValueOverride; + int mValuesBuilt{0}; + std::vector> mProposedValues; + std::vector> mEnteredRounds; Hash getHashOf(std::vector> const& vals) const override @@ -246,34 +240,6 @@ class TestSCP : public SCPDriver return hasher.finish(); } - // override the internal hashing scheme in order to make tests - // more predictable. - uint64 - computeHashNode(uint64 slotIndex, Value const& prev, bool isPriority, - int32_t roundNumber, NodeID const& nodeID) override - { - uint64 res; - if (isPriority) - { - res = mPriorityLookup(nodeID); - } - else - { - res = 0; - } - return res; - } - - // override the value hashing, to make tests more predictable. - uint64 - computeValueHash(uint64 slotIndex, Value const& prev, int32_t roundNumber, - Value const& value) override - { - return mHashValueCalculator(value); - } - - std::function mPriorityLookup; - std::function mHashValueCalculator; std::function mValidateValueOverride; @@ -293,15 +259,6 @@ class TestSCP : public SCPDriver std::map mTimers; std::chrono::milliseconds mCurrentTimerOffset{0}; - // Defers this node's nomination emits, like - // ARTIFICIALLY_DELAY_NOMINATION_EMIT_FOR_TESTING does for Herder. - std::chrono::milliseconds mNominationEmitDelay{0}; - std::chrono::milliseconds - getNominationEmitDelayForTesting() const override - { - return mNominationEmitDelay; - } - void setupTimer(uint64 slotIndex, int timerID, std::chrono::milliseconds timeout, std::function cb) override @@ -333,6 +290,34 @@ class TestSCP : public SCPDriver return mCurrentTimerOffset; } + TimerData + getLeaderTimer() + { + return mTimers[Slot::LEADER_TIMER]; + } + + bool + hasLeaderTimer() + { + return !!getLeaderTimer().mCallback; + } + + // fires a timer the way a real (one-shot) timer does + void + fireTimer(int timerID) + { + auto cb = mTimers[timerID].mCallback; + REQUIRE(cb); + mTimers[timerID].mCallback = nullptr; + cb(); + } + + uint32 + getLeaderRound(uint64 slotIndex) + { + return mSCP.getLeaderRound(slotIndex); + } + // returns true if a ballot protocol timer exists (in the past or future) bool hasBallotTimer() @@ -351,14 +336,6 @@ class TestSCP : public SCPDriver return mCurrentTimerOffset < getBallotProtocolTimer().mAbsoluteTimeout; } - Value const& - getLatestCompositeCandidate(uint64 slotIndex) - { - return mSCP.getSlot(slotIndex, true) - ->getLatestCompositeCandidate() - ->getValue(); - } - SCP::EnvelopeState receiveEnvelope(SCPEnvelope const& envelope) { @@ -393,12 +370,6 @@ class TestSCP : public SCPDriver throw std::runtime_error("not found"); } - std::set - getNominationLeaders(uint64 slotIndex) - { - return mSCP.getSlot(slotIndex, false)->getNominationLeaders(); - } - // Helper methods for empty-tx-set value testing void startDownload(Value const& v, std::chrono::milliseconds waitTime) @@ -416,21 +387,10 @@ class TestSCP : public SCPDriver static uint32_t const MAX_TIMEOUT_MS = (30 * 60) * 1000; std::chrono::milliseconds - computeTimeout(uint32 roundNumber, bool isNomination) override + computeTimeout(uint32 roundNumber) override { - int initialTimeoutMS; - int incrementMS; - - if (isNomination) - { - initialTimeoutMS = mInitialNominationTimeoutMS; - incrementMS = mIncrementNominationTimeoutMS; - } - else - { - initialTimeoutMS = mInitialBallotTimeoutMS; - incrementMS = mIncrementBallotTimeoutMS; - } + int initialTimeoutMS = mInitialBallotTimeoutMS; + int incrementMS = mIncrementBallotTimeoutMS; int timeoutMS = initialTimeoutMS + (roundNumber - 1) * incrementMS; if (timeoutMS > MAX_TIMEOUT_MS) @@ -599,15 +559,6 @@ verifyExternalize(SCPEnvelope const& actual, SecretKey const& secretKey, REQUIRE(exp.statement == actual.statement); } -void -verifyNominate(SCPEnvelope const& actual, SecretKey const& secretKey, - Hash const& qSetHash, uint64 slotIndex, std::vector votes, - std::vector accepted) -{ - auto exp = makeNominate(secretKey, qSetHash, slotIndex, votes, accepted); - REQUIRE(exp.statement == actual.statement); -} - // Simulate xValue being only structurally valid (e.g., due to an invalid tx // set) SCPDriver::ValidationLevel @@ -803,12 +754,8 @@ testTimeouts(TestSCP& scp, std::function f) { SECTION("minimum values") { - scp.mInitialNominationTimeoutMS = MinimumSorobanNetworkConfig:: - NOMINATION_TIMEOUT_INITIAL_MILLISECONDS; scp.mInitialBallotTimeoutMS = MinimumSorobanNetworkConfig::BALLOT_TIMEOUT_INITIAL_MILLISECONDS; - scp.mIncrementNominationTimeoutMS = MinimumSorobanNetworkConfig:: - NOMINATION_TIMEOUT_INCREMENT_MILLISECONDS; scp.mIncrementBallotTimeoutMS = MinimumSorobanNetworkConfig::BALLOT_TIMEOUT_INCREMENT_MILLISECONDS; f(scp); @@ -816,12 +763,8 @@ testTimeouts(TestSCP& scp, std::function f) SECTION("initial values") { - scp.mInitialNominationTimeoutMS = InitialSorobanNetworkConfig:: - NOMINATION_TIMEOUT_INITIAL_MILLISECONDS; scp.mInitialBallotTimeoutMS = InitialSorobanNetworkConfig::BALLOT_TIMEOUT_INITIAL_MILLISECONDS; - scp.mIncrementNominationTimeoutMS = InitialSorobanNetworkConfig:: - NOMINATION_TIMEOUT_INCREMENT_MILLISECONDS; scp.mIncrementBallotTimeoutMS = InitialSorobanNetworkConfig::BALLOT_TIMEOUT_INCREMENT_MILLISECONDS; f(scp); @@ -829,12 +772,8 @@ testTimeouts(TestSCP& scp, std::function f) SECTION("maximum values") { - scp.mInitialNominationTimeoutMS = MaximumSorobanNetworkConfig:: - NOMINATION_TIMEOUT_INITIAL_MILLISECONDS; scp.mInitialBallotTimeoutMS = MaximumSorobanNetworkConfig::BALLOT_TIMEOUT_INITIAL_MILLISECONDS; - scp.mIncrementNominationTimeoutMS = MaximumSorobanNetworkConfig:: - NOMINATION_TIMEOUT_INCREMENT_MILLISECONDS; scp.mIncrementBallotTimeoutMS = MaximumSorobanNetworkConfig::BALLOT_TIMEOUT_INCREMENT_MILLISECONDS; f(scp); @@ -2900,663 +2839,218 @@ TEST_CASE("ballot protocol core3", "[scp][ballotprotocol]") testTimeouts(scp, test); } -TEST_CASE("delayed nomination emit after an immediate emit", - "[scp][nominationprotocol]") +TEST_CASE("ballot protocol self-emits CONFIRM after federated accept-commit on " + "structurally-valid value", + "[scp][ballotprotocol]") { setupValues(); SIMULATION_CREATE_NODE(0); SIMULATION_CREATE_NODE(1); SIMULATION_CREATE_NODE(2); - SIMULATION_CREATE_NODE(3); - SIMULATION_CREATE_NODE(4); - // v-blocking set size 2, quorum threshold 4 SCPQuorumSet qSet; - qSet.threshold = 4; + qSet.threshold = 2; qSet.validators.push_back(v0NodeID); qSet.validators.push_back(v1NodeID); qSet.validators.push_back(v2NodeID); - qSet.validators.push_back(v3NodeID); - qSet.validators.push_back(v4NodeID); + uint256 qSetHash = sha256(xdr::xdr_to_opaque(qSet)); TestSCP scp(v0SecretKey.getPublicKey(), qSet); scp.storeQuorumSet(std::make_shared(qSet)); - uint256 qSetHash0 = scp.mSCP.getLocalNode()->getQuorumSetHash(); - scp.mNominationEmitDelay = std::chrono::milliseconds(1000); - // v0 leads the round and votes for x, but its emit is deferred - REQUIRE(scp.nominate(0, xValue, false)); - REQUIRE(scp.mEnvs.empty()); - REQUIRE(scp.mTimers.count(Slot::NOMINATION_EMIT_TIMER) == 1); - auto delayedEmit = scp.mTimers[Slot::NOMINATION_EMIT_TIMER].mCallback; - REQUIRE(delayedEmit); + // xValue stays kStructurallyValidValue throughout, with a wait time + // below the download timeout so maybeReplaceValueWithEmptyTxSet does not + // replace it with an empty-tx-set value. + scp.startDownload(xValue, UNDER_TX_SET_TIMEOUT); + scp.mValidateValueOverride = xValueStructurallyValidValidationOverride; - std::vector votes{xValue}; + SCPBallot xB1(1, xValue); - SECTION("nothing emitted meanwhile: the delayed emit sends the vote") - { - delayedEmit(); - REQUIRE(scp.mEnvs.size() == 1); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, votes, {}); - } + // v0 enters ballot protocol with xValue. + REQUIRE(scp.bumpState(0, xValue)); + REQUIRE(scp.mEnvs.size() == 1); - SECTION("an immediate emit meanwhile: the delayed emit is not repeated") - { - // A v-blocking set accepting x makes v0 accept it, and processEnvelope - // emits the new state at once while the delayed emit is still armed. - std::vector accepted{xValue}; - scp.receiveEnvelope( - makeNominate(v1SecretKey, qSetHash, 0, votes, accepted)); - REQUIRE(scp.mEnvs.empty()); - scp.receiveEnvelope( - makeNominate(v2SecretKey, qSetHash, 0, votes, accepted)); - REQUIRE(scp.mEnvs.size() == 1); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, votes, - accepted); + // v1, v2 vote-prepare for (1, xValue) → v0 accept-prepared (1, xValue). + REQUIRE(scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, xB1)) == + SCP::EnvelopeState::VALID); + REQUIRE(scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, xB1)) == + SCP::EnvelopeState::VALID); - // Nothing changed since: re-emitting the same statement used to be - // rejected by our own processEnvelope ("moved to a bad state"). - REQUIRE_NOTHROW(delayedEmit()); - REQUIRE(scp.mEnvs.size() == 1); - } + // v1, v2 signal accept-prepared (1, xValue). v0 would normally + // confirm-prepared here, but setConfirmPrepared stalls on + // kStructurallyValidValue so c/h stay unset on v0's side. + REQUIRE( + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, xB1, &xB1)) == + SCP::EnvelopeState::VALID); + REQUIRE( + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, xB1, &xB1)) == + SCP::EnvelopeState::VALID); + + // v1, v2 vote-to-commit (1, xValue) via nC/nH on their PREPAREs. + // federatedAccept fires via the "quorum voted-or-accepted" path v0 + // accept-commits, transitions mPhase to CONFIRM, and self-emits a CONFIRM + // with xValue. + REQUIRE(scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, xB1, &xB1, 1, 1)) == + SCP::EnvelopeState::VALID); + REQUIRE(scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, xB1, &xB1, 1, 1)) == + SCP::EnvelopeState::VALID); + + // Last emitted envelope should be a CONFIRM carrying xValue — proves that + // processEnvelope correctly accepts self-emitted CONFIRMs with + // kStructurallyValidValue. + auto const& lastEnv = scp.mEnvs.back(); + REQUIRE(lastEnv.statement.pledges.type() == SCP_ST_CONFIRM); + auto const& cBallot = lastEnv.statement.pledges.confirm().ballot; + REQUIRE(cBallot.value == xValue); + REQUIRE(!scp.isEmptyTxSetValue(cBallot.value)); + + // A peer CONFIRM whose value v0 considers kStructurallyValidValue is + // rejected by processEnvelope + auto const res = scp.receiveEnvelope( + makeConfirm(v1SecretKey, qSetHash, 0, 1, xB1, 1, 1)); + REQUIRE(res == SCP::EnvelopeState::INVALID); } -TEST_CASE("nomination tests core5", "[scp][nominationprotocol]") +TEST_CASE("drop tx set on download timeout", "[scp][ballotprotocol]") { setupValues(); SIMULATION_CREATE_NODE(0); SIMULATION_CREATE_NODE(1); SIMULATION_CREATE_NODE(2); - SIMULATION_CREATE_NODE(3); - SIMULATION_CREATE_NODE(4); - // we need 5 nodes to avoid sharing various thresholds: - // v-blocking set size: 2 - // threshold: 4 = 3 + self or 4 others + // 3 node network with threshold=2 (need any 2 nodes to form quorum) SCPQuorumSet qSet; - qSet.threshold = 4; + qSet.threshold = 2; qSet.validators.push_back(v0NodeID); qSet.validators.push_back(v1NodeID); qSet.validators.push_back(v2NodeID); - qSet.validators.push_back(v3NodeID); - qSet.validators.push_back(v4NodeID); uint256 qSetHash = sha256(xdr::xdr_to_opaque(qSet)); - REQUIRE(xValue < yValue); - REQUIRE(yValue < zValue); - - auto checkLeaders = [&](TestSCP& scp, std::set expectedLeaders) { - auto l = scp.getNominationLeaders(0); - REQUIRE(std::equal(l.begin(), l.end(), expectedLeaders.begin(), - expectedLeaders.end())); - }; + TestSCP scp(v0SecretKey.getPublicKey(), qSet); + scp.storeQuorumSet(std::make_shared(qSet)); + uint256 qSetHash0 = scp.mSCP.getLocalNode()->getQuorumSetHash(); - SECTION("nomination - v0 is top") + SECTION("timeout during prepare phase") { - TestSCP scp(v0SecretKey.getPublicKey(), qSet); + // Node v0 starts ballot protocol with xValue + // Simulate that xValue is awaiting download with timeout exceeded + scp.startDownload(xValue, OVER_TX_SET_TIMEOUT); - auto test = [&](TestSCP& scp) { - uint256 qSetHash0 = scp.mSCP.getLocalNode()->getQuorumSetHash(); - scp.storeQuorumSet(std::make_shared(qSet)); + // Now call bumpState which should trigger + // maybeReplaceValueWithEmptyTxSet + REQUIRE(scp.bumpState(0, xValue)); + REQUIRE(scp.mEnvs.size() == 1); - SECTION("v0 starts to nominates xValue") - { - REQUIRE(scp.nominate(0, xValue, false)); + // The ballot should have an empty-tx-set value, not the original + // xValue + auto const& emittedBallot = + scp.mEnvs[0].statement.pledges.prepare().ballot; - checkLeaders(scp, {v0SecretKey.getPublicKey()}); + REQUIRE(emittedBallot.counter == 1); + REQUIRE(scp.isEmptyTxSetValue(emittedBallot.value)); - SECTION("others nominate what v0 says (x) -> prepare x") - { - std::vector votes, accepted; - votes.emplace_back(xValue); + // Verify it's the empty-tx-set value derived from xValue + Value expectedEmptyTxSetValue = + scp.makeEmptyTxSetValueFromValue(xValue); + REQUIRE(emittedBallot.value == expectedEmptyTxSetValue); - REQUIRE(scp.mEnvs.size() == 1); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, - votes, accepted); - - SCPEnvelope nom1 = - makeNominate(v1SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope nom2 = - makeNominate(v2SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope nom3 = - makeNominate(v3SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope nom4 = - makeNominate(v4SecretKey, qSetHash, 0, votes, accepted); - - // nothing happens yet - scp.receiveEnvelope(nom1); - scp.receiveEnvelope(nom2); - REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, + SCPBallot(1, expectedEmptyTxSetValue)); + } - // this causes 'x' to be accepted (quorum) - scp.receiveEnvelope(nom3); - REQUIRE(scp.mEnvs.size() == 2); + SECTION("no timeout when wait time under threshold") + { + // Node v0 starts ballot protocol with xValue + REQUIRE(scp.bumpState(0, xValue)); + REQUIRE(scp.mEnvs.size() == 1); - scp.mExpectedCandidates.emplace(xValue); - scp.mCompositeValue = xValue; + SCPBallot b1(1, xValue); - accepted.emplace_back(xValue); - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, - votes, accepted); + // Simulate that xValue is awaiting download but wait time is still low + scp.startDownload(xValue, UNDER_TX_SET_TIMEOUT); - // extra message doesn't do anything - scp.receiveEnvelope(nom4); - REQUIRE(scp.mEnvs.size() == 2); + // Try to bump state - should NOT replace with an empty-tx-set value + REQUIRE(scp.bumpState(0, xValue)); + REQUIRE(scp.mEnvs.size() == 2); - SCPEnvelope acc1 = - makeNominate(v1SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope acc2 = - makeNominate(v2SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope acc3 = - makeNominate(v3SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope acc4 = - makeNominate(v4SecretKey, qSetHash, 0, votes, accepted); - - // nothing happens yet - scp.receiveEnvelope(acc1); - scp.receiveEnvelope(acc2); - REQUIRE(scp.mEnvs.size() == 2); + // Verify ballot still has original xValue, not an empty-tx-set value + auto const& emittedBallot = + scp.mEnvs[1].statement.pledges.prepare().ballot; + REQUIRE(emittedBallot.counter == 2); + REQUIRE(!scp.isEmptyTxSetValue(emittedBallot.value)); + REQUIRE(emittedBallot.value == xValue); + } - scp.mCompositeValue = xValue; - // this causes the node to send a prepare message (quorum) - scp.receiveEnvelope(acc3); - REQUIRE(scp.mEnvs.size() == 3); + SECTION("empty-tx-set value can be prepared and confirmed") + { + // Start with xValue and timeout to an empty-tx-set value + scp.startDownload(xValue, OVER_TX_SET_TIMEOUT); - verifyPrepare(scp.mEnvs[2], v0SecretKey, qSetHash0, 0, - SCPBallot(1, xValue)); + REQUIRE(scp.bumpState(0, xValue)); + REQUIRE(scp.mEnvs.size() == 1); - scp.receiveEnvelope(acc4); - REQUIRE(scp.mEnvs.size() == 3); + Value emptyTxSetValue = scp.makeEmptyTxSetValueFromValue(xValue); + SCPBallot emptyTxSetB1(1, emptyTxSetValue); - std::vector votes2 = votes; - votes2.emplace_back(yValue); + // Verify we emitted an empty-tx-set value + REQUIRE(scp.isEmptyTxSetValue( + scp.mEnvs[0].statement.pledges.prepare().ballot.value)); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, emptyTxSetB1); - SECTION("nominate x -> accept x -> prepare (x) ; others " - "accepted y " - "-> update latest to (z=x+y)") - { - SCPEnvelope acc1_2 = makeNominate(v1SecretKey, qSetHash, - 0, votes2, votes2); - SCPEnvelope acc2_2 = makeNominate(v2SecretKey, qSetHash, - 0, votes2, votes2); - SCPEnvelope acc3_2 = makeNominate(v3SecretKey, qSetHash, - 0, votes2, votes2); - SCPEnvelope acc4_2 = makeNominate(v4SecretKey, qSetHash, - 0, votes2, votes2); - - scp.receiveEnvelope(acc1_2); - REQUIRE(scp.mEnvs.size() == 3); - - // v-blocking - scp.receiveEnvelope(acc2_2); - REQUIRE(scp.mEnvs.size() == 4); - verifyNominate(scp.mEnvs[3], v0SecretKey, qSetHash0, 0, - votes2, votes2); - - scp.mExpectedCandidates.insert(yValue); - scp.mCompositeValue = kValue; - // this updates the composite value to use next time - // but does not prepare it - scp.receiveEnvelope(acc3_2); - REQUIRE(scp.mEnvs.size() == 4); - - REQUIRE(scp.getLatestCompositeCandidate(0) == kValue); - - scp.receiveEnvelope(acc4_2); - REQUIRE(scp.mEnvs.size() == 4); - } - SECTION("nomination - restored state") - { - TestSCP scp2(v0SecretKey.getPublicKey(), qSet); - scp2.storeQuorumSet( - std::make_shared(qSet)); - - // at this point - // votes = { x } - // accepted = { x } - - // tests if nomination proceeds like normal - // nominates x - auto nominationRestore = [&]() { - // restores from the previous state - scp2.mSCP.setStateFromEnvelope( - 0, scp2.wrapEnvelope( - makeNominate(v0SecretKey, qSetHash0, 0, - votes, accepted))); - // tries to start nomination with yValue, but picks - // xValue since it was already in the votes - REQUIRE(!scp2.nominate(0, yValue, false)); - - checkLeaders(scp2, {v0SecretKey.getPublicKey()}); - - REQUIRE(scp2.mEnvs.size() == 0); - - // other nodes vote for 'x' - scp2.receiveEnvelope(nom1); - scp2.receiveEnvelope(nom2); - REQUIRE(scp2.mEnvs.size() == 0); - // 'x' is accepted (quorum) - // but because the restored state already included - // 'x' in the accepted set, no new message is - // emitted - scp2.receiveEnvelope(nom3); - - scp2.mExpectedCandidates.emplace(xValue); - scp2.mCompositeValue = xValue; - - // other nodes not emit 'x' as accepted - scp2.receiveEnvelope(acc1); - scp2.receiveEnvelope(acc2); - REQUIRE(scp2.mEnvs.size() == 0); - - scp2.mCompositeValue = xValue; - // this causes the node to update its composite - // value to - // x - scp2.receiveEnvelope(acc3); - }; - - SECTION("ballot protocol not started") - { - nominationRestore(); - // nomination ended up starting the ballot protocol - REQUIRE(scp2.mEnvs.size() == 1); + // Other nodes also move to the empty-tx-set value + scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, emptyTxSetB1)); + scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, emptyTxSetB1)); - verifyPrepare(scp2.mEnvs[0], v0SecretKey, qSetHash0, - 0, SCPBallot(1, xValue)); - } - SECTION("ballot protocol started (on value k)") - { - scp2.mSCP.setStateFromEnvelope( - 0, scp2.wrapEnvelope( - makePrepare(v0SecretKey, qSetHash0, 0, - SCPBallot(1, kValue)))); - nominationRestore(); - // nomination didn't do anything (already working on - // k) - REQUIRE(scp2.mEnvs.size() == 0); - } - } - } - SECTION("receive more messages, then v0 switches to a " - "different leader") - { - SCPEnvelope nom1 = - makeNominate(v1SecretKey, qSetHash, 0, {kValue}, {}); - SCPEnvelope nom2 = - makeNominate(v2SecretKey, qSetHash, 0, {yValue}, {}); - - // nothing more happens - scp.receiveEnvelope(nom1); - scp.receiveEnvelope(nom2); - REQUIRE(scp.mEnvs.size() == 1); + // Should prepare the empty-tx-set value (quorum reached) + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, emptyTxSetB1, + &emptyTxSetB1); - // switch leader to v1 - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v1NodeID) ? 1000 : 1; - }; - REQUIRE(scp.nominate(0, xValue, true)); - REQUIRE(scp.mEnvs.size() == 2); + // Quorum confirms prepared empty-tx-set value + scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, emptyTxSetB1, &emptyTxSetB1)); + scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, emptyTxSetB1, &emptyTxSetB1)); - std::vector votesXK; - votesXK.emplace_back(xValue); - votesXK.emplace_back(kValue); - std::sort(votesXK.begin(), votesXK.end()); + REQUIRE(scp.mEnvs.size() == 3); + verifyPrepare(scp.mEnvs[2], v0SecretKey, qSetHash0, 0, emptyTxSetB1, + &emptyTxSetB1, 1, 1); - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, - votesXK, {}); - } - SECTION("select accepted value from leader") - { - REQUIRE(xValue < yValue); - REQUIRE(yValue < zValue); - REQUIRE(scp.mEnvs.size() == 1); + // Accept commit + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, emptyTxSetB1, + &emptyTxSetB1, 1, 1)); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, emptyTxSetB1, + &emptyTxSetB1, 1, 1)); - // Update round leader to v1 - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v1NodeID) ? 1000 : 1; - }; + REQUIRE(scp.mEnvs.size() == 4); + verifyConfirm(scp.mEnvs[3], v0SecretKey, qSetHash0, 0, 1, emptyTxSetB1, + 1, 1); - SCPEnvelope nom1 = makeNominate(v1SecretKey, qSetHash, 0, - {yValue, zValue}, {yValue}); + // Externalize the empty-tx-set value + scp.receiveEnvelope( + makeConfirm(v1SecretKey, qSetHash, 0, 1, emptyTxSetB1, 1, 1)); + scp.receiveEnvelope( + makeConfirm(v2SecretKey, qSetHash, 0, 1, emptyTxSetB1, 1, 1)); - SECTION("receive accepted before timeout") - { - // nothing more happens, v0 is leader - scp.receiveEnvelope(nom1); - REQUIRE(scp.mEnvs.size() == 1); - - // Update round leaders, vote for accepted value (y) - REQUIRE(scp.nominate(0, xValue, true)); - REQUIRE(scp.mEnvs.size() == 2); - } - SECTION("receive accepted after timeout") - { - REQUIRE(!scp.nominate(0, xValue, true)); - REQUIRE(scp.mEnvs.size() == 1); - - // Vote for accepted value (y) - scp.receiveEnvelope(nom1); - REQUIRE(scp.mEnvs.size() == 2); - } - - std::vector votesXY; - votesXY.emplace_back(xValue); - votesXY.emplace_back(yValue); - - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, - votesXY, {}); - - SCPEnvelope nom2 = - makeNominate(v1SecretKey, qSetHash, 0, - {yValue, zValue, zzValue}, {yValue}); - scp.receiveEnvelope(nom2); - // Nothing happens, as v0 already voted for the accepted - // value (y) - REQUIRE(scp.mEnvs.size() == 2); - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, - votesXY, {}); - } - } - SECTION("self nominates 'x', others nominate y -> prepare y") - { - std::vector myVotes, accepted; - myVotes.emplace_back(xValue); - - scp.mExpectedCandidates.emplace(xValue); - scp.mCompositeValue = xValue; - REQUIRE(scp.nominate(0, xValue, false)); - - REQUIRE(scp.mEnvs.size() == 1); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, myVotes, - accepted); - - std::vector votes; - votes.emplace_back(yValue); - - std::vector acceptedY = accepted; - - acceptedY.emplace_back(yValue); - - SECTION("others only vote for y") - { - SCPEnvelope nom1 = - makeNominate(v1SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope nom2 = - makeNominate(v2SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope nom3 = - makeNominate(v3SecretKey, qSetHash, 0, votes, accepted); - SCPEnvelope nom4 = - makeNominate(v4SecretKey, qSetHash, 0, votes, accepted); - - // nothing happens yet - scp.receiveEnvelope(nom1); - scp.receiveEnvelope(nom2); - scp.receiveEnvelope(nom3); - REQUIRE(scp.mEnvs.size() == 1); - - // 'y' is accepted (quorum) - scp.receiveEnvelope(nom4); - REQUIRE(scp.mEnvs.size() == 2); - myVotes.emplace_back(yValue); - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, - myVotes, acceptedY); - } - SECTION("others accepted y") - { - SCPEnvelope acc1 = makeNominate(v1SecretKey, qSetHash, 0, - votes, acceptedY); - SCPEnvelope acc2 = makeNominate(v2SecretKey, qSetHash, 0, - votes, acceptedY); - SCPEnvelope acc3 = makeNominate(v3SecretKey, qSetHash, 0, - votes, acceptedY); - SCPEnvelope acc4 = makeNominate(v4SecretKey, qSetHash, 0, - votes, acceptedY); - - scp.receiveEnvelope(acc1); - REQUIRE(scp.mEnvs.size() == 1); - - // this causes 'y' to be accepted (v-blocking) - scp.receiveEnvelope(acc2); - REQUIRE(scp.mEnvs.size() == 2); - - myVotes.emplace_back(yValue); - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, - myVotes, acceptedY); - - scp.mExpectedCandidates.clear(); - scp.mExpectedCandidates.insert(yValue); - scp.mCompositeValue = yValue; - // this causes the node to send a prepare message (quorum) - scp.receiveEnvelope(acc3); - REQUIRE(scp.mEnvs.size() == 3); - - verifyPrepare(scp.mEnvs[2], v0SecretKey, qSetHash0, 0, - SCPBallot(1, yValue)); - - scp.receiveEnvelope(acc4); - REQUIRE(scp.mEnvs.size() == 3); - } - } - }; - - testTimeouts(scp, test); - } - SECTION("v1 is top node") - { - TestSCP scp(v0SecretKey.getPublicKey(), qSet); - - auto test = [&](TestSCP& scp) { - uint256 qSetHash0 = scp.mSCP.getLocalNode()->getQuorumSetHash(); - scp.storeQuorumSet(std::make_shared(qSet)); - - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v1NodeID) ? 1000 : 1; - }; - - std::vector votesX, votesY, votesK, votesXY, votesYK, - votesXK, emptyV; - votesX.emplace_back(xValue); - votesY.emplace_back(yValue); - votesK.emplace_back(kValue); - - votesXY.emplace_back(xValue); - votesXY.emplace_back(yValue); - - votesYK.emplace_back(yValue); - votesYK.emplace_back(kValue); - std::sort(votesYK.begin(), votesYK.end()); - - votesXK.emplace_back(xValue); - votesXK.emplace_back(kValue); - std::sort(votesXK.begin(), votesXK.end()); - - std::vector valuesHash; - valuesHash.emplace_back(xValue); - valuesHash.emplace_back(yValue); - valuesHash.emplace_back(kValue); - std::sort(valuesHash.begin(), valuesHash.end()); - - scp.mHashValueCalculator = [&](Value const& v) { - auto pos = std::find(valuesHash.begin(), valuesHash.end(), v); - if (pos == valuesHash.end()) - { - abort(); - } - return 1 + std::distance(valuesHash.begin(), pos); - }; - - SCPEnvelope nom1 = - makeNominate(v1SecretKey, qSetHash, 0, votesXY, emptyV); - SCPEnvelope nom2 = - makeNominate(v2SecretKey, qSetHash, 0, votesXK, emptyV); - - SECTION( - "value from v1 is a candidate, self should not introduce new " - "value on timeout") - { - REQUIRE(!scp.nominate(0, xValue, false)); - checkLeaders(scp, {v1SecretKey.getPublicKey()}); - - REQUIRE(scp.mEnvs.size() == 0); - nom1 = makeNominate(v1SecretKey, qSetHash, 0, votesX, emptyV); - nom2 = makeNominate(v2SecretKey, qSetHash, 0, votesX, emptyV); - SCPEnvelope nom3 = - makeNominate(v3SecretKey, qSetHash, 0, votesX, emptyV); - - // Receive `x` from v1, vote for it - scp.receiveEnvelope(nom1); - REQUIRE(scp.mEnvs.size() == 1); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, votesX, - emptyV); - - scp.receiveEnvelope(nom2); - scp.receiveEnvelope(nom3); - REQUIRE(scp.mEnvs.size() == 2); - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, votesX, - votesX); - - SCPEnvelope acc1 = - makeNominate(v1SecretKey, qSetHash, 0, votesX, votesX); - SCPEnvelope acc2 = - makeNominate(v2SecretKey, qSetHash, 0, votesX, votesX); - SCPEnvelope acc3 = - makeNominate(v3SecretKey, qSetHash, 0, votesX, votesX); - - scp.receiveEnvelope(acc1); - scp.receiveEnvelope(acc2); - REQUIRE(scp.mEnvs.size() == 2); - - // Receive accept from quorum, ratify and generate a candidate - // value - REQUIRE(scp.mTimers.find(Slot::NOMINATION_TIMER) != - scp.mTimers.end()); - scp.mCompositeValue = xValue; - scp.mExpectedCandidates.emplace(xValue); - scp.receiveEnvelope(acc3); - REQUIRE(scp.mEnvs.size() == 3); - // Timer is cancelled - REQUIRE(scp.mTimers.find(Slot::NOMINATION_TIMER) == - scp.mTimers.end()); - - // v0 is the new leader, but we already have a candidate - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v0NodeID) ? 1000 : 1; - }; - REQUIRE(!scp.nominate(0, kValue, true)); - } - SECTION("nomination waits for v1") - { - REQUIRE(!scp.nominate(0, xValue, false)); - - checkLeaders(scp, {v1SecretKey.getPublicKey()}); - - REQUIRE(scp.mEnvs.size() == 0); - - SCPEnvelope nom4 = - makeNominate(v4SecretKey, qSetHash, 0, votesXK, emptyV); - - // nothing happens with non top nodes - scp.receiveEnvelope(nom2); - // (note: don't receive anything from node3 - we want to pick - // another dead node) - REQUIRE(scp.mEnvs.size() == 0); - - // v1 is leader -> nominate the first value from its message - // that's "y" - scp.receiveEnvelope(nom1); - REQUIRE(scp.mEnvs.size() == 1); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, votesY, - emptyV); - - scp.receiveEnvelope(nom4); - REQUIRE(scp.mEnvs.size() == 1); - - // "timeout -> pick another value from v1" - scp.mExpectedCandidates.emplace(xValue); - scp.mCompositeValue = xValue; - - // allows to pick another leader, - // pick another dead node v3 as to force picking up - // a new value from v1 - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v3NodeID) ? 1000 : 1; - }; - - // note: value passed in here should be ignored - REQUIRE(scp.nominate(0, kValue, true)); - // picks up 'x' from v1 (as we already have 'y') - // which also happens to causes 'x' to be accepted - REQUIRE(scp.mEnvs.size() == 2); - verifyNominate(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, votesXY, - votesX); - } - SECTION("v1 dead, timeout") - { - REQUIRE(!scp.nominate(0, xValue, false)); - - REQUIRE(scp.mEnvs.size() == 0); - - scp.receiveEnvelope(nom2); - REQUIRE(scp.mEnvs.size() == 0); - - checkLeaders(scp, {v1SecretKey.getPublicKey()}); - - SECTION("v0 is new top node") - { - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v0NodeID) ? 1000 : 1; - }; - - REQUIRE(scp.nominate(0, xValue, true)); - checkLeaders(scp, {v0SecretKey.getPublicKey(), - v1SecretKey.getPublicKey()}); - - REQUIRE(scp.mEnvs.size() == 1); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, - votesX, emptyV); - } - SECTION("v2 is new top node") - { - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v2NodeID) ? 1000 : 1; - }; - - REQUIRE(scp.nominate(0, xValue, true)); - checkLeaders(scp, {v1SecretKey.getPublicKey(), - v2SecretKey.getPublicKey()}); - - REQUIRE(scp.mEnvs.size() == 1); - // v2 votes for XK, but nomination only picks the highest - // value - std::vector v2Top; - v2Top.emplace_back(std::max(xValue, kValue)); - verifyNominate(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, - v2Top, emptyV); - } - SECTION("v3 is new top node") - { - scp.mPriorityLookup = [&](NodeID const& n) { - return (n == v3NodeID) ? 1000 : 1; - }; - // nothing happens, we don't have any message for v3 - REQUIRE(!scp.nominate(0, xValue, true)); - checkLeaders(scp, {v1SecretKey.getPublicKey(), - v3SecretKey.getPublicKey()}); - - REQUIRE(scp.mEnvs.size() == 0); - } - } - }; + REQUIRE(scp.mEnvs.size() == 5); + verifyExternalize(scp.mEnvs[4], v0SecretKey, qSetHash0, 0, emptyTxSetB1, + 1); - testTimeouts(scp, test); + // Verify the externalized value is the empty-tx-set value + REQUIRE(scp.mExternalizedValues.size() == 1); + REQUIRE(scp.isEmptyTxSetValue(scp.mExternalizedValues[0])); + REQUIRE(scp.mExternalizedValues[0] == emptyTxSetValue); } } -TEST_CASE("nomination times out structurally-valid value into empty tx set", - "[scp][nomination]") +TEST_CASE("Proper handling of non-current ledger value", + "[scp][ballotprotocol]") { setupValues(); SIMULATION_CREATE_NODE(0); @@ -3569,48 +3063,19 @@ TEST_CASE("nomination times out structurally-valid value into empty tx set", qSet.validators.push_back(v1NodeID); qSet.validators.push_back(v2NodeID); - auto const qSetHash = sha256(xdr::xdr_to_opaque(qSet)); - TestSCP scp(v0SecretKey.getPublicKey(), qSet); scp.storeQuorumSet(std::make_shared(qSet)); - // xValue is structurally-valid throughout — its tx set is either still in - // flight or has been downloaded-and-found-invalid. Seed a wait time past - // the download timeout so maybeReplaceValueWithEmptyTxSet triggers - // empty-tx-set replacement at bumpState time. - scp.startDownload(xValue, OVER_TX_SET_TIMEOUT); - scp.mValidateValueOverride = xValueStructurallyValidValidationOverride; - - REQUIRE(scp.nominate(0, xValue, false)); - - auto const followerVoteNomination = - makeNominate(v1SecretKey, qSetHash, 0, {xValue}, {}); - REQUIRE(scp.receiveEnvelope(followerVoteNomination) == - SCP::EnvelopeState::VALID); - - scp.mExpectedCandidates.emplace(xValue); - scp.mCompositeValue = xValue; + // validateValue reports xValue is not for the current ledger. + scp.mValidateValueOverride = xValueNotCurrentLedgerOverride; - auto const followerAcceptedNomination = - makeNominate(v2SecretKey, qSetHash, 0, {xValue}, {xValue}); - // Quorum accept-nominated xValue → composite value flows to - // bumpState → maybeReplaceValueWithEmptyTxSet sees - // kStructurallyValidValue with the wait time past the timeout and - // substitutes an empty-tx-set value. - REQUIRE(scp.receiveEnvelope(followerAcceptedNomination) == - SCP::EnvelopeState::VALID); + REQUIRE(scp.bumpState(0, xValue)); - // The emitted ballot should carry the empty-tx-set value derived from - // xValue, not xValue itself. - auto const& lastEnv = scp.mEnvs.back(); - REQUIRE(lastEnv.statement.pledges.type() == SCP_ST_PREPARE); - auto const& ballot = lastEnv.statement.pledges.prepare().ballot; - REQUIRE(scp.isEmptyTxSetValue(ballot.value)); - REQUIRE(ballot.value == scp.makeEmptyTxSetValueFromValue(xValue)); + // Do not emit a ballot with a kMaybeValid value + REQUIRE(scp.mEnvs.size() == 0); } -TEST_CASE("ballot protocol self-emits CONFIRM after federated accept-commit on " - "structurally-valid value", +TEST_CASE("setConfirmPrepared stalls on kStructurallyValidValue value", "[scp][ballotprotocol]") { setupValues(); @@ -3629,27 +3094,15 @@ TEST_CASE("ballot protocol self-emits CONFIRM after federated accept-commit on " TestSCP scp(v0SecretKey.getPublicKey(), qSet); scp.storeQuorumSet(std::make_shared(qSet)); - // xValue stays kStructurallyValidValue throughout, with a wait time - // below the download timeout so maybeReplaceValueWithEmptyTxSet does not - // replace it with an empty-tx-set value. + // Simulate parallel downloading scp.startDownload(xValue, UNDER_TX_SET_TIMEOUT); - scp.mValidateValueOverride = xValueStructurallyValidValidationOverride; - - SCPBallot xB1(1, xValue); - // v0 enters ballot protocol with xValue. + // v0 enters ballot protocol REQUIRE(scp.bumpState(0, xValue)); REQUIRE(scp.mEnvs.size() == 1); + SCPBallot xB1(1, xValue); - // v1, v2 vote-prepare for (1, xValue) → v0 accept-prepared (1, xValue). - REQUIRE(scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, xB1)) == - SCP::EnvelopeState::VALID); - REQUIRE(scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, xB1)) == - SCP::EnvelopeState::VALID); - - // v1, v2 signal accept-prepared (1, xValue). v0 would normally - // confirm-prepared here, but setConfirmPrepared stalls on - // kStructurallyValidValue so c/h stay unset on v0's side. + // v1 and v2 send PREPAREs with prepared — quorum confirms prepared REQUIRE( scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, xB1, &xB1)) == SCP::EnvelopeState::VALID); @@ -3657,41 +3110,72 @@ TEST_CASE("ballot protocol self-emits CONFIRM after federated accept-commit on " scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, xB1, &xB1)) == SCP::EnvelopeState::VALID); - // v1, v2 vote-to-commit (1, xValue) via nC/nH on their PREPAREs. - // federatedAccept fires via the "quorum voted-or-accepted" path v0 - // accept-commits, transitions mPhase to CONFIRM, and self-emits a CONFIRM - // with xValue. - REQUIRE(scp.receiveEnvelope( - makePrepare(v1SecretKey, qSetHash, 0, xB1, &xB1, 1, 1)) == - SCP::EnvelopeState::VALID); - REQUIRE(scp.receiveEnvelope( - makePrepare(v2SecretKey, qSetHash, 0, xB1, &xB1, 1, 1)) == - SCP::EnvelopeState::VALID); + SECTION("commit gate stalls mCommit but mHighBallot is set") + { - // Last emitted envelope should be a CONFIRM carrying xValue — proves that - // processEnvelope correctly accepts self-emitted CONFIRMs with - // kStructurallyValidValue. - auto const& lastEnv = scp.mEnvs.back(); - REQUIRE(lastEnv.statement.pledges.type() == SCP_ST_CONFIRM); - auto const& cBallot = lastEnv.statement.pledges.confirm().ballot; - REQUIRE(cBallot.value == xValue); - REQUIRE(!scp.isEmptyTxSetValue(cBallot.value)); + // setConfirmPrepared sets mHighBallot (nH > 0) but the commit gate + // stalls mCommit (nC == 0) because xValue is kStructurallyValidValue. + // Check the latest emitted PREPARE. + REQUIRE(scp.mEnvs.size() >= 2); + auto const& lastPrep = scp.mEnvs.back().statement.pledges.prepare(); + REQUIRE(lastPrep.nH == 1); + REQUIRE(lastPrep.nC == 0); + } - // A peer CONFIRM whose value v0 considers kStructurallyValidValue is - // rejected by processEnvelope - auto const res = scp.receiveEnvelope( - makeConfirm(v1SecretKey, qSetHash, 0, 1, xB1, 1, 1)); - REQUIRE(res == SCP::EnvelopeState::INVALID); + SECTION("proceeds after value becomes validated") + { + auto envsBeforeClear = scp.mEnvs.size(); + + // Simulate tx set arrival — value becomes fully validated + scp.clearDownload(xValue); + + // Trigger advanceSlot with envelopes that confirm-prepare at a + // higher counter, so attemptConfirmPrepared finds newH > mHighBallot. + // This causes setConfirmPrepared to be called with the now-validated + // value, setting mCommit. + SCPBallot xB2(2, xValue); + REQUIRE(scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, xB2, &xB2)) == + SCP::EnvelopeState::VALID); + REQUIRE(scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, xB2, &xB2)) == + SCP::EnvelopeState::VALID); + + // setConfirmPrepared should now succeed — mCommit set, node + // progresses. Expect at least one new envelope with nC > 0 or a + // CONFIRM/EXTERNALIZE. + REQUIRE(scp.mEnvs.size() > envsBeforeClear); + bool foundC = false; + for (size_t i = envsBeforeClear; i < scp.mEnvs.size(); i++) + { + auto const& st = scp.mEnvs[i].statement; + if (st.pledges.type() == SCP_ST_PREPARE) + { + if (st.pledges.prepare().nC > 0) + { + foundC = true; + break; + } + } + else + { + // CONFIRM or EXTERNALIZE also proves we got past the stall + foundC = true; + break; + } + } + REQUIRE(foundC); + } } -TEST_CASE("drop tx set on download timeout", "[scp][ballotprotocol]") +TEST_CASE("incoming PREPARE with structurally valid prepared value is accepted", + "[scp][ballotprotocol]") { setupValues(); SIMULATION_CREATE_NODE(0); SIMULATION_CREATE_NODE(1); SIMULATION_CREATE_NODE(2); - // 3 node network with threshold=2 (need any 2 nodes to form quorum) SCPQuorumSet qSet; qSet.threshold = 2; qSet.validators.push_back(v0NodeID); @@ -3702,124 +3186,24 @@ TEST_CASE("drop tx set on download timeout", "[scp][ballotprotocol]") TestSCP scp(v0SecretKey.getPublicKey(), qSet); scp.storeQuorumSet(std::make_shared(qSet)); - uint256 qSetHash0 = scp.mSCP.getLocalNode()->getQuorumSetHash(); - SECTION("timeout during prepare phase") - { - // Node v0 starts ballot protocol with xValue - // Simulate that xValue is awaiting download with timeout exceeded - scp.startDownload(xValue, OVER_TX_SET_TIMEOUT); + // v0 enters ballot protocol with yValue + REQUIRE(scp.bumpState(0, yValue)); + REQUIRE(scp.mEnvs.size() == 1); - // Now call bumpState which should trigger - // maybeReplaceValueWithEmptyTxSet - REQUIRE(scp.bumpState(0, xValue)); - REQUIRE(scp.mEnvs.size() == 1); + // Set xValue to kStructurallyValidValue + scp.mValidateValueOverride = xValueStructurallyValidValidationOverride; - // The ballot should have an empty-tx-set value, not the original - // xValue - auto const& emittedBallot = - scp.mEnvs[0].statement.pledges.prepare().ballot; - - REQUIRE(emittedBallot.counter == 1); - REQUIRE(scp.isEmptyTxSetValue(emittedBallot.value)); - - // Verify it's the empty-tx-set value derived from xValue - Value expectedEmptyTxSetValue = - scp.makeEmptyTxSetValueFromValue(xValue); - REQUIRE(emittedBallot.value == expectedEmptyTxSetValue); - - verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, - SCPBallot(1, expectedEmptyTxSetValue)); - } - - SECTION("no timeout when wait time under threshold") - { - // Node v0 starts ballot protocol with xValue - REQUIRE(scp.bumpState(0, xValue)); - REQUIRE(scp.mEnvs.size() == 1); - - SCPBallot b1(1, xValue); - - // Simulate that xValue is awaiting download but wait time is still low - scp.startDownload(xValue, UNDER_TX_SET_TIMEOUT); - - // Try to bump state - should NOT replace with an empty-tx-set value - REQUIRE(scp.bumpState(0, xValue)); - REQUIRE(scp.mEnvs.size() == 2); - - // Verify ballot still has original xValue, not an empty-tx-set value - auto const& emittedBallot = - scp.mEnvs[1].statement.pledges.prepare().ballot; - REQUIRE(emittedBallot.counter == 2); - REQUIRE(!scp.isEmptyTxSetValue(emittedBallot.value)); - REQUIRE(emittedBallot.value == xValue); - } - - SECTION("empty-tx-set value can be prepared and confirmed") - { - // Start with xValue and timeout to an empty-tx-set value - scp.startDownload(xValue, OVER_TX_SET_TIMEOUT); - - REQUIRE(scp.bumpState(0, xValue)); - REQUIRE(scp.mEnvs.size() == 1); - - Value emptyTxSetValue = scp.makeEmptyTxSetValueFromValue(xValue); - SCPBallot emptyTxSetB1(1, emptyTxSetValue); - - // Verify we emitted an empty-tx-set value - REQUIRE(scp.isEmptyTxSetValue( - scp.mEnvs[0].statement.pledges.prepare().ballot.value)); - verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, emptyTxSetB1); - - // Other nodes also move to the empty-tx-set value - scp.receiveEnvelope( - makePrepare(v1SecretKey, qSetHash, 0, emptyTxSetB1)); - scp.receiveEnvelope( - makePrepare(v2SecretKey, qSetHash, 0, emptyTxSetB1)); - - // Should prepare the empty-tx-set value (quorum reached) - REQUIRE(scp.mEnvs.size() == 2); - verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, emptyTxSetB1, - &emptyTxSetB1); - - // Quorum confirms prepared empty-tx-set value - scp.receiveEnvelope( - makePrepare(v1SecretKey, qSetHash, 0, emptyTxSetB1, &emptyTxSetB1)); - scp.receiveEnvelope( - makePrepare(v2SecretKey, qSetHash, 0, emptyTxSetB1, &emptyTxSetB1)); - - REQUIRE(scp.mEnvs.size() == 3); - verifyPrepare(scp.mEnvs[2], v0SecretKey, qSetHash0, 0, emptyTxSetB1, - &emptyTxSetB1, 1, 1); - - // Accept commit - scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, emptyTxSetB1, - &emptyTxSetB1, 1, 1)); - scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, emptyTxSetB1, - &emptyTxSetB1, 1, 1)); - - REQUIRE(scp.mEnvs.size() == 4); - verifyConfirm(scp.mEnvs[3], v0SecretKey, qSetHash0, 0, 1, emptyTxSetB1, - 1, 1); - - // Externalize the empty-tx-set value - scp.receiveEnvelope( - makeConfirm(v1SecretKey, qSetHash, 0, 1, emptyTxSetB1, 1, 1)); - scp.receiveEnvelope( - makeConfirm(v2SecretKey, qSetHash, 0, 1, emptyTxSetB1, 1, 1)); - - REQUIRE(scp.mEnvs.size() == 5); - verifyExternalize(scp.mEnvs[4], v0SecretKey, qSetHash0, 0, emptyTxSetB1, - 1); - - // Verify the externalized value is the empty-tx-set value - REQUIRE(scp.mExternalizedValues.size() == 1); - REQUIRE(scp.isEmptyTxSetValue(scp.mExternalizedValues[0])); - REQUIRE(scp.mExternalizedValues[0] == emptyTxSetValue); - } + // v1 sends PREPARE with valid ballot value but only structurally valid + // prepared value. This should be accepted. + SCPBallot yB1(1, yValue); + SCPBallot xB1(1, xValue); + REQUIRE( + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, yB1, &xB1)) == + SCP::EnvelopeState::VALID); } -TEST_CASE("Proper handling of non-current ledger value", +TEST_CASE("incoming PREPARE with non-tx-set-invalid value is dropped", "[scp][ballotprotocol]") { setupValues(); @@ -3833,180 +3217,994 @@ TEST_CASE("Proper handling of non-current ledger value", qSet.validators.push_back(v1NodeID); qSet.validators.push_back(v2NodeID); + uint256 qSetHash = sha256(xdr::xdr_to_opaque(qSet)); + TestSCP scp(v0SecretKey.getPublicKey(), qSet); scp.storeQuorumSet(std::make_shared(qSet)); - // validateValue reports xValue is not for the current ledger. - scp.mValidateValueOverride = xValueNotCurrentLedgerOverride; + // xValue is invalid for some non-tx-set reason (close time, signature, + // ...). This should NOT be accepted as kStructurallyValidValue; the + // statement should be dropped. + scp.mValidateValueOverride = xValueNonTxSetInvalidValidationOverride; - REQUIRE(scp.bumpState(0, xValue)); + SCPBallot xB1(1, xValue); - // Do not emit a ballot with a kMaybeValid value - REQUIRE(scp.mEnvs.size() == 0); + // Envelope should be rejected as invalid + REQUIRE(scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, xB1)) == + SCP::EnvelopeState::INVALID); + + // Envelope was not recorded — recordEnvelope only runs for non-kInvalid. + REQUIRE(scp.mSCP.getLatestMessage(v1NodeID) == nullptr); + // No local emit triggered. + REQUIRE(scp.mEnvs.empty()); } -TEST_CASE("setConfirmPrepared stalls on kStructurallyValidValue value", - "[scp][ballotprotocol]") +// --------------------------------------------------------------------------- +// Leader rounds +// +// There is no nomination phase. A round is a ballot counter; each round has +// one leader (SCPDriver::getRoundLeader). At counter r a node votes for the +// ballot `leader(r)` votes for at counter r, unless it already has a value it +// must stick to. +// --------------------------------------------------------------------------- + +namespace +{ +SCPBallot const& +latestBallot(TestSCP& scp) +{ + REQUIRE(!scp.mEnvs.empty()); + auto const& pl = scp.mEnvs.back().statement.pledges; + REQUIRE(pl.type() == SCP_ST_PREPARE); + return pl.prepare().ballot; +} +} + +TEST_CASE("leader rounds core5", "[scp][leaderschedule]") { setupValues(); SIMULATION_CREATE_NODE(0); SIMULATION_CREATE_NODE(1); SIMULATION_CREATE_NODE(2); + SIMULATION_CREATE_NODE(3); + SIMULATION_CREATE_NODE(4); + // we need 5 nodes to avoid sharing various thresholds: + // v-blocking set size: 2 + // threshold: 4 = 3 + self or 4 others SCPQuorumSet qSet; - qSet.threshold = 2; + qSet.threshold = 4; qSet.validators.push_back(v0NodeID); qSet.validators.push_back(v1NodeID); qSet.validators.push_back(v2NodeID); + qSet.validators.push_back(v3NodeID); + qSet.validators.push_back(v4NodeID); uint256 qSetHash = sha256(xdr::xdr_to_opaque(qSet)); TestSCP scp(v0SecretKey.getPublicKey(), qSet); scp.storeQuorumSet(std::make_shared(qSet)); + uint256 qSetHash0 = scp.mSCP.getLocalNode()->getQuorumSetHash(); - // Simulate parallel downloading - scp.startDownload(xValue, UNDER_TX_SET_TIMEOUT); + // round r is led by schedule[(r - 1) % size] + std::vector schedule; + scp.mRoundLeader = [&](uint64, uint32 round) -> std::optional { + if (schedule.empty()) + { + return std::nullopt; + } + return schedule[(round - 1) % schedule.size()]; + }; - // v0 enters ballot protocol - REQUIRE(scp.bumpState(0, xValue)); - REQUIRE(scp.mEnvs.size() == 1); - SCPBallot xB1(1, xValue); + SCPBallot x1(1, xValue), y1(1, yValue), x2(2, xValue), y2(2, yValue), + z2(2, zValue), z3(3, zValue); - // v1 and v2 send PREPAREs with prepared — quorum confirms prepared - REQUIRE( - scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, xB1, &xB1)) == - SCP::EnvelopeState::VALID); - REQUIRE( - scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, xB1, &xB1)) == - SCP::EnvelopeState::VALID); + SECTION("the round 1 leader proposes when the slot starts") + { + schedule = {v0NodeID, v1NodeID, v2NodeID, v3NodeID, v4NodeID}; + REQUIRE(scp.startSlot(0, xValue)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, x1); + REQUIRE(scp.mValuesBuilt == 1); + REQUIRE(scp.getLeaderRound(0) == 1); + REQUIRE(scp.mProposedValues.size() == 1); + REQUIRE(scp.mProposedValues[0] == std::make_pair(uint32(1), xValue)); + // it has a ballot at its round: the ballot timer rules apply, not the + // leader timer + REQUIRE(!scp.hasLeaderTimer()); + + SECTION("starting again changes nothing") + { + REQUIRE(!scp.startSlot(0, yValue)); + REQUIRE(scp.mEnvs.size() == 1); + REQUIRE(scp.mValuesBuilt == 1); + } + SECTION("other nodes' ballots do not move the leader") + { + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, y1)); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, y1)); + REQUIRE(scp.mEnvs.size() == 1); + } + } - SECTION("commit gate stalls mCommit but mHighBallot is set") + SECTION("a follower waits for the leader and adopts its ballot") { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(!scp.startSlot(0, yValue)); + REQUIRE(scp.mEnvs.empty()); + // followers do not build a value + REQUIRE(scp.mValuesBuilt == 0); + REQUIRE(scp.getLeaderRound(0) == 1); + REQUIRE(scp.hasLeaderTimer()); + + // a ballot from a node that does not lead the round is recorded but + // not adopted + REQUIRE( + scp.receiveEnvelope(makePrepare(v4SecretKey, qSetHash, 0, y1)) == + SCP::EnvelopeState::VALID); + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.hasLeaderTimer()); - // setConfirmPrepared sets mHighBallot (nH > 0) but the commit gate - // stalls mCommit (nC == 0) because xValue is kStructurallyValidValue. - // Check the latest emitted PREPARE. - REQUIRE(scp.mEnvs.size() >= 2); - auto const& lastPrep = scp.mEnvs.back().statement.pledges.prepare(); - REQUIRE(lastPrep.nH == 1); - REQUIRE(lastPrep.nC == 0); + // the leader's ballot is adopted + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, x1); + REQUIRE(!scp.hasLeaderTimer()); + REQUIRE(scp.mValuesBuilt == 0); + + SECTION("and externalizes with a quorum") + { + // quorum votes prepare x1 -> accept prepared + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, x1)); + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, x1, &x1); + // quorum accepted prepared -> confirm prepared, vote commit + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, x1, &x1)); + REQUIRE(scp.mEnvs.size() == 3); + verifyPrepare(scp.mEnvs[2], v0SecretKey, qSetHash0, 0, x1, &x1, 1, + 1); + // quorum votes commit -> accept commit + scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, x1, &x1, 1, 1)); + scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, x1, &x1, 1, 1)); + scp.receiveEnvelope( + makePrepare(v3SecretKey, qSetHash, 0, x1, &x1, 1, 1)); + REQUIRE(scp.mEnvs.size() == 4); + verifyConfirm(scp.mEnvs[3], v0SecretKey, qSetHash0, 0, 1, x1, 1, 1); + // quorum accepted commit -> externalize + scp.receiveEnvelope( + makeConfirm(v1SecretKey, qSetHash, 0, 1, x1, 1, 1)); + scp.receiveEnvelope( + makeConfirm(v2SecretKey, qSetHash, 0, 1, x1, 1, 1)); + scp.receiveEnvelope( + makeConfirm(v3SecretKey, qSetHash, 0, 1, x1, 1, 1)); + REQUIRE(scp.mEnvs.size() == 5); + verifyExternalize(scp.mEnvs[4], v0SecretKey, qSetHash0, 0, x1, 1); + REQUIRE(scp.mExternalizedValues[0] == xValue); + REQUIRE(scp.mValuesBuilt == 0); + REQUIRE(!scp.hasLeaderTimer()); + } + SECTION("a later ballot of the same leader at the same counter is " + "not adopted") + { + // equivocating leader: the node already voted for x1 + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, y1)); + REQUIRE(scp.mEnvs.size() == 1); + } } - SECTION("proceeds after value becomes validated") + SECTION("a follower adopts the round 1 leader before its own trigger") { - auto envsBeforeClear = scp.mEnvs.size(); + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(scp.getLeaderRound(0) == 0); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, x1); + REQUIRE(scp.getLeaderRound(0) == 1); - // Simulate tx set arrival — value becomes fully validated - scp.clearDownload(xValue); + // its own trigger then has nothing left to do + REQUIRE(!scp.startSlot(0, yValue)); + REQUIRE(scp.mEnvs.size() == 1); + REQUIRE(scp.mValuesBuilt == 0); + } - // Trigger advanceSlot with envelopes that confirm-prepare at a - // higher counter, so attemptConfirmPrepared finds newH > mHighBallot. - // This causes setConfirmPrepared to be called with the now-validated - // value, setting mCommit. - SCPBallot xB2(2, xValue); - REQUIRE(scp.receiveEnvelope( - makePrepare(v1SecretKey, qSetHash, 0, xB2, &xB2)) == - SCP::EnvelopeState::VALID); - REQUIRE(scp.receiveEnvelope( - makePrepare(v2SecretKey, qSetHash, 0, xB2, &xB2)) == - SCP::EnvelopeState::VALID); + SECTION("before its trigger a node does not adopt later rounds' leaders") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.getLeaderRound(0) == 0); + REQUIRE(!scp.hasLeaderTimer()); + } - // setConfirmPrepared should now succeed — mCommit set, node - // progresses. Expect at least one new envelope with nC > 0 or a - // CONFIRM/EXTERNALIZE. - REQUIRE(scp.mEnvs.size() > envsBeforeClear); - bool foundC = false; - for (size_t i = envsBeforeClear; i < scp.mEnvs.size(); i++) + SECTION("a leader that is not started does not propose") + { + schedule = {v0NodeID, v1NodeID, v2NodeID, v3NodeID, v4NodeID}; + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, y1)); + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.mValuesBuilt == 0); + } + + SECTION("silent leader: the leader timer hands over to the next leader") + { + SECTION("local node leads round 2") { - auto const& st = scp.mEnvs[i].statement; - if (st.pledges.type() == SCP_ST_PREPARE) + schedule = {v1NodeID, v0NodeID, v2NodeID, v3NodeID, v4NodeID}; + REQUIRE(!scp.startSlot(0, yValue)); + REQUIRE(scp.hasLeaderTimer()); + auto timer = scp.getLeaderTimer(); + REQUIRE(timer.mAbsoluteTimeout == + std::chrono::milliseconds(scp.mInitialBallotTimeoutMS)); + + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnteredRounds.back() == + std::make_pair(uint32(2), true)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, y2); + REQUIRE(scp.mValuesBuilt == 1); + REQUIRE(!scp.hasLeaderTimer()); + + SECTION("the round 1 leader showing up late changes nothing") { - if (st.pledges.prepare().nC > 0) - { - foundC = true; - break; - } + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); } - else + SECTION("a stale leader timer callback is ignored") { - // CONFIRM or EXTERNALIZE also proves we got past the stall - foundC = true; - break; + timer.mCallback(); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.size() == 1); } } - REQUIRE(foundC); - } -} + SECTION("another node leads round 2") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(!scp.startSlot(0, zValue)); -TEST_CASE("incoming PREPARE with structurally valid prepared value is accepted", - "[scp][ballotprotocol]") -{ - setupValues(); - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); + SECTION("its ballot arrives after the timeout") + { + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.empty()); + // waiting for the round 2 leader now, with the round 2 + // timeout + REQUIRE(scp.hasLeaderTimer()); + REQUIRE( + scp.getLeaderTimer().mAbsoluteTimeout == + std::chrono::milliseconds(scp.mInitialBallotTimeoutMS + + scp.mIncrementBallotTimeoutMS)); + + SECTION("the round 2 leader's ballot is adopted") + { + // a round 2 ballot from a node that does not lead round 2 + scp.receiveEnvelope( + makePrepare(v3SecretKey, qSetHash, 0, x2)); + REQUIRE(scp.mEnvs.empty()); - SCPQuorumSet qSet; - qSet.threshold = 2; - qSet.validators.push_back(v0NodeID); - qSet.validators.push_back(v1NodeID); - qSet.validators.push_back(v2NodeID); + scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, y2); + REQUIRE(!scp.hasLeaderTimer()); + } + SECTION("a late round 1 proposal still gets the node's vote") + { + // The node never voted: it votes for the round 1 leader's + // late proposal at the round 1 counter, and keeps waiting + // for the round 2 leader. + scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, x1); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.hasLeaderTimer()); + REQUIRE(!scp.hasBallotTimer()); - uint256 qSetHash = sha256(xdr::xdr_to_opaque(qSet)); + SECTION("and externalizes with a quorum at round 1") + { + scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope( + makePrepare(v3SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope( + makePrepare(v4SecretKey, qSetHash, 0, x1, &x1)); + // accepted then confirmed prepared: the node votes to + // commit x at counter 1 + REQUIRE(scp.mEnvs.size() >= 2); + auto const& last = + scp.mEnvs.back().statement.pledges.prepare(); + REQUIRE(last.ballot.value == xValue); + REQUIRE(last.nC == 1); + REQUIRE(last.nH == 1); + } + SECTION("the round 2 leader's ballot takes over") + { + scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, + y2); + REQUIRE(!scp.hasLeaderTimer()); + } + SECTION("a second late proposal is ignored") + { + // only a node without a ballot takes a late proposal + schedule = {v1NodeID, v2NodeID, v3NodeID, v0NodeID, + v4NodeID}; + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 3); + scp.receiveEnvelope( + makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.size() == 1); + } + } + SECTION("a proposal that is not valid is not taken late either") + { + scp.mValidateValueOverride = [&](uint64, Value const& v, + bool proposal) { + return (proposal && v == xValue) + ? SCPDriver::kInvalidValue + : SCPDriver::kFullyValidatedValue; + }; + scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.empty()); + } + } + SECTION("the round 1 leader's proposal is pending: one extension") + { + // The application holds the leader's statement (e.g. it is + // fetching its transaction set): the node waits one more + // timeout, once. + scp.mProposalPending = true; + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 1); + REQUIRE(scp.hasLeaderTimer()); + REQUIRE(scp.getLeaderTimer().mAbsoluteTimeout == + std::chrono::milliseconds(scp.mInitialBallotTimeoutMS)); + REQUIRE(scp.mEnvs.empty()); + + SECTION("the proposal arrives during the extension") + { + scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, x1); + REQUIRE(scp.getLeaderRound(0) == 1); + REQUIRE(!scp.hasLeaderTimer()); + } + SECTION("it does not: the node moves on") + { + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.hasLeaderTimer()); + REQUIRE(scp.mEnvs.empty()); + } + } + SECTION("its ballot arrived early: kept until the round starts") + { + // a single node cannot make others skip a round + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.getLeaderRound(0) == 1); - TestSCP scp(v0SecretKey.getPublicKey(), qSet); - scp.storeQuorumSet(std::make_shared(qSet)); + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, y2); + } + SECTION("then local node leads round 3 with its own value") + { + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.mEnvs.empty()); + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 3); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, z3); + REQUIRE(scp.mValuesBuilt == 1); + } + } + } - // v0 enters ballot protocol with yValue - REQUIRE(scp.bumpState(0, yValue)); - REQUIRE(scp.mEnvs.size() == 1); + SECTION("ballot timeout: the next leader takes over") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(!scp.startSlot(0, zValue)); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); - // Set xValue to kStructurallyValidValue - scp.mValidateValueOverride = xValueStructurallyValidValidationOverride; + // a quorum is at counter 1: the ballot timer runs + REQUIRE(!scp.hasBallotTimer()); + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, y1)); + scp.receiveEnvelope(makePrepare(v4SecretKey, qSetHash, 0, y1)); + REQUIRE(scp.hasBallotTimer()); + REQUIRE(!scp.hasLeaderTimer()); - // v1 sends PREPARE with valid ballot value but only structurally valid - // prepared value. This should be accepted. - SCPBallot yB1(1, yValue); - SCPBallot xB1(1, xValue); - REQUIRE( - scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, yB1, &xB1)) == - SCP::EnvelopeState::VALID); + SECTION("to a leader whose ballot is known") + { + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.size() == 1); + scp.fireTimer(Slot::BALLOT_PROTOCOL_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.size() == 2); + // v3 and v4 voted for y at counter 1: with the leader and the + // local node that is a quorum, so (1, y) is accepted prepared + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, y2, &y1); + } + SECTION("to a leader that has not spoken yet") + { + scp.fireTimer(Slot::BALLOT_PROTOCOL_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + // keeps its round 1 ballot, votes for nothing new + REQUIRE(scp.mEnvs.size() == 1); + REQUIRE(scp.hasLeaderTimer()); + // the ballot timer of the round it gave up on is stopped + REQUIRE(!scp.hasBallotTimer()); + + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, y2, &y1); + REQUIRE(!scp.hasLeaderTimer()); + } + SECTION("to the local node, which keeps the value it voted for") + { + schedule = {v1NodeID, v0NodeID, v2NodeID, v3NodeID, v4NodeID}; + scp.fireTimer(Slot::BALLOT_PROTOCOL_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.size() == 2); + // x, not its own z: the transaction set of x is already out + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, x2); + REQUIRE(scp.mValuesBuilt == 0); + } + SECTION("to the local node, whose current value is not proposable") + { + schedule = {v1NodeID, v0NodeID, v2NodeID, v3NodeID, v4NodeID}; + scp.mValidateValueOverride = [&](uint64, Value const& v, + bool proposal) { + return (proposal && v == xValue) + ? SCPDriver::kInvalidValue + : SCPDriver::kFullyValidatedValue; + }; + scp.fireTimer(Slot::BALLOT_PROTOCOL_TIMER); + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, z2); + REQUIRE(scp.mValuesBuilt == 1); + } + } + + SECTION("a confirmed prepared value wins over the leader's") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(!scp.startSlot(0, zValue)); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, x1)); + scp.receiveEnvelope(makePrepare(v4SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 2); + verifyPrepare(scp.mEnvs[1], v0SecretKey, qSetHash0, 0, x1, &x1); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope(makePrepare(v4SecretKey, qSetHash, 0, x1, &x1)); + // confirmed prepared: x is now the value to stick to + REQUIRE(scp.mEnvs.size() == 3); + verifyPrepare(scp.mEnvs[2], v0SecretKey, qSetHash0, 0, x1, &x1, 1, 1); + + // the round 2 leader votes for something else + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.size() == 3); + scp.fireTimer(Slot::BALLOT_PROTOCOL_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + REQUIRE(scp.mEnvs.size() == 4); + verifyPrepare(scp.mEnvs[3], v0SecretKey, qSetHash0, 0, x2, &x1, 1, 1); + REQUIRE(!scp.hasLeaderTimer()); + } + + SECTION("a v-blocking set ahead moves a node to that round") + { + schedule = {v1NodeID, v2NodeID, v3NodeID, v0NodeID, v4NodeID}; + // v1 and v2 (v-blocking) are at counter 3, on a value they must + // stick to; local node has no ballot at all + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, z3)); + REQUIRE(scp.getLeaderRound(0) == 0); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, z3)); + REQUIRE(scp.getLeaderRound(0) == 3); + // it does not vote for what they vote for: only the round's leader + // decides + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.hasLeaderTimer()); + + SECTION("where it adopts the leader") + { + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, z3)); + REQUIRE(scp.mEnvs.size() == 1); + // with v1, v2 and the leader that is a quorum voting for z3 + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, z3, &z3); + REQUIRE(!scp.hasLeaderTimer()); + } + SECTION("where it leads once it is started") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(scp.startSlot(0, xValue)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, + SCPBallot(3, xValue)); + } + } + + SECTION("a started leader pushed to its round by a v-blocking set " + "proposes") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(!scp.startSlot(0, xValue)); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, z3)); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, z3)); + REQUIRE(scp.getLeaderRound(0) == 3); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, + SCPBallot(3, xValue)); + REQUIRE(scp.mValuesBuilt == 1); + } + + SECTION("proposal validation gates adoption") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(!scp.startSlot(0, zValue)); + + SECTION("a value the node would not propose is not adopted") + { + // valid for the ballot protocol, but not as a proposal (e.g. it + // carries an upgrade the local node did not arm) + scp.mValidateValueOverride = [&](uint64, Value const& v, + bool proposal) { + return (proposal && v == xValue) + ? SCPDriver::kInvalidValue + : SCPDriver::kFullyValidatedValue; + }; + REQUIRE(scp.receiveEnvelope( + makePrepare(v1SecretKey, qSetHash, 0, x1)) == + SCP::EnvelopeState::VALID); + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.hasLeaderTimer()); + + // the regular protocol still applies: a quorum that prepared the + // value brings the node along + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, x1, &x1)); + // v-blocking accepted prepared + REQUIRE(scp.mEnvs.empty()); + scp.receiveEnvelope(makePrepare(v3SecretKey, qSetHash, 0, x1, &x1)); + scp.receiveEnvelope(makePrepare(v4SecretKey, qSetHash, 0, x1, &x1)); + // quorum accepted prepared -> confirmed prepared + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, x1, &x1, 1, + 1); + } + SECTION("a value whose transaction set is still downloading is " + "adopted") + { + scp.startDownload(xValue, UNDER_TX_SET_TIMEOUT); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, x1); + } + } + + SECTION("a leader that cannot build a value lets the round time out") + { + schedule = {v0NodeID, v1NodeID, v2NodeID, v3NodeID, v4NodeID}; + scp.mMakeValueOverride = []() { return ValueWrapperPtr(); }; + REQUIRE(!scp.startSlot(0, xValue)); + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.mValuesBuilt == 1); + REQUIRE(scp.hasLeaderTimer()); + scp.fireTimer(Slot::LEADER_TIMER); + REQUIRE(scp.getLeaderRound(0) == 2); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, y2)); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, y2); + } + + SECTION("building the value re-enters SCP") + { + schedule = {v0NodeID, v1NodeID, v2NodeID, v3NodeID, v4NodeID}; + auto xW = scp.wrapValue(xValue); + + SECTION("the slot moved to a later round meanwhile") + { + scp.mMakeValueOverride = [&]() { + // envelopes that became ready while the value was built + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, z3)); + scp.receiveEnvelope(makePrepare(v2SecretKey, qSetHash, 0, z3)); + return xW; + }; + REQUIRE(scp.startSlot(0, xValue)); + // the value was built for round 1; the node is in round 3 now, + // which v2 leads: it votes for v2's ballot, not for its own value + REQUIRE(scp.getLeaderRound(0) == 3); + REQUIRE(scp.mEnvs.size() == 1); + verifyPrepare(scp.mEnvs[0], v0SecretKey, qSetHash0, 0, z3); + REQUIRE(!scp.hasLeaderTimer()); + } + SECTION("the slot externalized meanwhile") + { + scp.mMakeValueOverride = [&]() { + for (auto const& k : + {v1SecretKey, v2SecretKey, v3SecretKey, v4SecretKey}) + { + scp.receiveEnvelope(makeExternalize(k, qSetHash, 0, y1, 1)); + } + return xW; + }; + scp.startSlot(0, xValue); + REQUIRE(scp.mExternalizedValues[0] == yValue); + for (auto const& e : scp.mEnvs) + { + // never voted for its own value + auto vals = Slot::getStatementValues(e.statement); + REQUIRE(std::find(vals.begin(), vals.end(), xValue) == + vals.end()); + } + REQUIRE(!scp.hasLeaderTimer()); + } + } + + SECTION("no schedule known: neither proposes nor adopts") + { + // e.g. the slot is not the next ledger to close + REQUIRE(!scp.startSlot(0, xValue)); + scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(scp.mEnvs.empty()); + REQUIRE(scp.mValuesBuilt == 0); + } + + SECTION("non-validators never vote for a leader's ballot") + { + TestSCP watcher(v0SecretKey.getPublicKey(), qSet, false); + watcher.storeQuorumSet(std::make_shared(qSet)); + watcher.mRoundLeader = [&](uint64, uint32) { return v1NodeID; }; + watcher.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, x1)); + REQUIRE(watcher.mEnvs.empty()); + REQUIRE(watcher.getLeaderRound(0) == 0); + REQUIRE(!watcher.hasLeaderTimer()); + } + + SECTION("nomination statements are dropped") + { + schedule = {v1NodeID, v2NodeID, v0NodeID, v3NodeID, v4NodeID}; + REQUIRE(scp.receiveEnvelope( + makeNominate(v1SecretKey, qSetHash, 0, {xValue}, {})) == + SCP::EnvelopeState::INVALID); + REQUIRE(scp.mSCP.getLatestMessage(v1NodeID) == nullptr); + REQUIRE(scp.mEnvs.empty()); + } } -TEST_CASE("incoming PREPARE with non-tx-set-invalid value is dropped", - "[scp][ballotprotocol]") +namespace { - setupValues(); - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); +// A small network of TestSCP nodes on one slot, driven by a virtual clock: +// messages are delayed, reordered and (for a while) lost; some nodes are dead; +// nodes start the slot at different times. +struct LeaderRoundsModel +{ + struct Node + { + SecretKey mKey; + std::unique_ptr mSCP; + bool mAlive{true}; + bool mStarted{false}; + std::chrono::milliseconds mStartTime{0}; + size_t mEnvsSeen{0}; + Value mValue; + }; - SCPQuorumSet qSet; - qSet.threshold = 2; - qSet.validators.push_back(v0NodeID); - qSet.validators.push_back(v1NodeID); - qSet.validators.push_back(v2NodeID); + struct Delivery + { + std::chrono::milliseconds mWhen; + uint64_t mSeq; + size_t mTo; + SCPEnvelope mEnv; + bool + operator>(Delivery const& o) const + { + return std::tie(mWhen, mSeq) > std::tie(o.mWhen, o.mSeq); + } + }; - uint256 qSetHash = sha256(xdr::xdr_to_opaque(qSet)); + stellar_default_random_engine mRng; + std::vector mNodes; + std::vector mSchedule; + std::priority_queue, std::greater<>> mQueue; + uint64_t mSeq{0}; + std::chrono::milliseconds mNow{0}; + // until then, messages are lost with probability mLossPercent and can be + // delayed up to mMaxChaosDelay + std::chrono::milliseconds mChaosEnd{0}; + uint32_t mLossPercent{0}; + std::chrono::milliseconds mMaxChaosDelay{0}; + std::chrono::milliseconds mNextRebroadcast{0}; + size_t mThreshold{0}; + + LeaderRoundsModel(uint32_t seed, size_t numNodes, size_t numDead) + : mRng(seed) + { + mThreshold = numNodes - (numNodes - 1) / 3; + releaseAssert(numNodes - numDead >= mThreshold); + SCPQuorumSet qSet; + qSet.threshold = static_cast(mThreshold); + for (size_t i = 0; i < numNodes; i++) + { + Node n; + n.mKey = SecretKey::fromSeed( + sha256(fmt::format("LEADER_MODEL_NODE_{}", i))); + qSet.validators.push_back(n.mKey.getPublicKey()); + // distinct proposals + n.mValue = xdr::xdr_to_opaque( + sha256(fmt::format("LEADER_MODEL_VALUE_{}_{}", seed, i))); + mNodes.emplace_back(std::move(n)); + } + for (auto const& n : mNodes) + { + mSchedule.emplace_back(n.mKey.getPublicKey()); + } + stellar::shuffle(mSchedule.begin(), mSchedule.end(), mRng); - TestSCP scp(v0SecretKey.getPublicKey(), qSet); - scp.storeQuorumSet(std::make_shared(qSet)); + // dead nodes are picked from the front of the schedule half of the + // time (so the first leaders are dead), at random otherwise + std::vector order(numNodes); + std::iota(order.begin(), order.end(), 0); + if (rand_uniform(0, 1, mRng) == 0) + { + std::sort(order.begin(), order.end(), [&](size_t a, size_t b) { + auto pos = [&](size_t i) { + return std::find(mSchedule.begin(), mSchedule.end(), + mNodes[i].mKey.getPublicKey()) - + mSchedule.begin(); + }; + return pos(a) < pos(b); + }); + } + else + { + stellar::shuffle(order.begin(), order.end(), mRng); + } + for (size_t i = 0; i < numDead; i++) + { + mNodes[order[i]].mAlive = false; + } - // xValue is invalid for some non-tx-set reason (close time, signature, - // ...). This should NOT be accepted as kStructurallyValidValue; the - // statement should be dropped. - scp.mValidateValueOverride = xValueNonTxSetInvalidValidationOverride; + mChaosEnd = + std::chrono::milliseconds(rand_uniform(0, 30000, mRng)); + mLossPercent = rand_uniform(0, 40, mRng); + mMaxChaosDelay = + std::chrono::milliseconds(rand_uniform(10, 4000, mRng)); + mNextRebroadcast = mChaosEnd; - SCPBallot xB1(1, xValue); + for (auto& n : mNodes) + { + n.mSCP = std::make_unique(n.mKey.getPublicKey(), qSet); + n.mSCP->mRoundLeader = [this](uint64, uint32 round) { + return mSchedule[(round - 1) % mSchedule.size()]; + }; + // most nodes start close together; some are late + n.mStartTime = std::chrono::milliseconds( + rand_uniform(0, 3, mRng) == 0 + ? rand_uniform(0, 8000, mRng) + : rand_uniform(0, 300, mRng)); + } + } - // Envelope should be rejected as invalid - REQUIRE(scp.receiveEnvelope(makePrepare(v1SecretKey, qSetHash, 0, xB1)) == - SCP::EnvelopeState::INVALID); + void + send(size_t from, SCPEnvelope const& env) + { + for (size_t to = 0; to < mNodes.size(); to++) + { + if (to == from || !mNodes[to].mAlive) + { + continue; + } + bool chaos = mNow < mChaosEnd; + if (chaos && rand_uniform(0, 99, mRng) < mLossPercent) + { + continue; + } + auto maxDelay = + chaos ? mMaxChaosDelay : std::chrono::milliseconds(150); + auto delay = std::chrono::milliseconds( + rand_uniform(1, static_cast(maxDelay.count()), mRng)); + mQueue.push(Delivery{mNow + delay, mSeq++, to, env}); + } + } - // Envelope was not recorded — recordEnvelope only runs for non-kInvalid. - REQUIRE(scp.mSCP.getLatestMessage(v1NodeID) == nullptr); - // No local emit triggered. - REQUIRE(scp.mEnvs.empty()); + // forwards whatever the nodes emitted since the last call + void + flush() + { + for (size_t i = 0; i < mNodes.size(); i++) + { + auto& n = mNodes[i]; + while (n.mEnvsSeen < n.mSCP->mEnvs.size()) + { + auto env = n.mSCP->mEnvs[n.mEnvsSeen++]; + send(i, env); + } + } + } + + bool + allExternalized() const + { + for (auto const& n : mNodes) + { + if (n.mAlive && n.mSCP->mExternalizedValues.empty()) + { + return false; + } + } + return true; + } + + // runs until every live node externalized or `limit` (virtual time); + // returns true in the first case + bool + run(std::chrono::milliseconds limit) + { + while (!allExternalized()) + { + // next event: a start, a delivery, a timer or a rebroadcast + enum class Kind + { + NONE, + START, + DELIVERY, + TIMER, + REBROADCAST + }; + Kind kind = Kind::REBROADCAST; + auto when = mNextRebroadcast; + size_t node = 0; + int timerID = 0; + for (size_t i = 0; i < mNodes.size(); i++) + { + auto& n = mNodes[i]; + if (!n.mAlive) + { + continue; + } + if (!n.mStarted && n.mStartTime < when) + { + kind = Kind::START; + when = n.mStartTime; + node = i; + } + for (auto const& t : n.mSCP->mTimers) + { + if (t.second.mCallback && t.second.mAbsoluteTimeout < when) + { + kind = Kind::TIMER; + when = t.second.mAbsoluteTimeout; + node = i; + timerID = t.first; + } + } + } + if (!mQueue.empty() && mQueue.top().mWhen < when) + { + kind = Kind::DELIVERY; + when = mQueue.top().mWhen; + } + if (when > limit) + { + return false; + } + mNow = std::max(mNow, when); + for (auto& n : mNodes) + { + n.mSCP->mCurrentTimerOffset = mNow; + } + switch (kind) + { + case Kind::START: + { + auto& n = mNodes[node]; + n.mStarted = true; + n.mSCP->startSlot(0, n.mValue); + } + break; + case Kind::TIMER: + { + auto& td = mNodes[node].mSCP->mTimers[timerID]; + auto cb = td.mCallback; + td.mCallback = nullptr; + cb(); + } + break; + case Kind::DELIVERY: + { + auto d = mQueue.top(); + mQueue.pop(); + mNodes[d.mTo].mSCP->receiveEnvelope(d.mEnv); + } + break; + case Kind::REBROADCAST: + { + // like Herder, nodes periodically re-send their latest + // statement + mNextRebroadcast = mNow + std::chrono::milliseconds(2000); + for (size_t i = 0; i < mNodes.size(); i++) + { + auto& n = mNodes[i]; + if (n.mAlive && !n.mSCP->mEnvs.empty()) + { + send(i, n.mSCP->mEnvs.back()); + } + } + } + break; + default: + releaseAssert(false); + } + flush(); + } + return true; + } + + void + checkAgreement() const + { + std::optional externalized; + for (auto const& n : mNodes) + { + auto const& ext = n.mSCP->mExternalizedValues; + if (ext.empty()) + { + continue; + } + auto const& v = ext.begin()->second; + if (!externalized) + { + externalized = v; + // it is one of the proposals + REQUIRE( + std::any_of(mNodes.begin(), mNodes.end(), + [&](Node const& o) { return o.mValue == v; })); + } + REQUIRE(*externalized == v); + } + } +}; + +void +runLeaderRoundsModel(uint32_t firstSeed, uint32_t numSeeds) +{ + for (uint32_t seed = firstSeed; seed < firstSeed + numSeeds; seed++) + { + size_t const numNodes = 4 + seed % 4; // 4..7 + size_t const maxDead = (numNodes - 1) / 3; // 1, 1, 1, 2 + size_t const numDead = (seed / 4) % (maxDead + 1); + INFO(fmt::format("seed {} nodes {} dead {}", seed, numNodes, numDead)); + LeaderRoundsModel model(seed, numNodes, numDead); + // Once messages flow again, the slot must close: the bound leaves + // room for a full turn of the schedule with growing timeouts. + bool done = model.run(model.mChaosEnd + std::chrono::minutes(10)); + // safety holds no matter what + model.checkAgreement(); + REQUIRE(done); + } +} +} + +TEST_CASE("leader rounds network model", "[scp][leaderschedule]") +{ + runLeaderRoundsModel(1, 400); +} + +TEST_CASE("leader rounds network model long", "[scp][leaderschedule][!hide]") +{ + runLeaderRoundsModel(100000, 20000); } } diff --git a/src/scp/test/SCPUnitTests.cpp b/src/scp/test/SCPUnitTests.cpp deleted file mode 100644 index 50dbd484a..000000000 --- a/src/scp/test/SCPUnitTests.cpp +++ /dev/null @@ -1,840 +0,0 @@ -#include "scp/LocalNode.h" -#include "scp/SCP.h" -#include "scp/Slot.h" -#include "simulation/Simulation.h" -#include "test/Catch2.h" -#include "util/Logging.h" -#include "xdrpp/marshal.h" - -namespace stellar -{ -static bool -isNear(uint64 r, double target) -{ - double v = (double)r / (double)UINT64_MAX; - return (std::abs(v - target) < .01); -} - -class TestNominationSCP : public SCPDriver -{ - public: - SCP mSCP; - uint32_t mInitialNominationTimeoutMS = 1000; - uint32_t mIncrementNominationTimeoutMS = 1000; - uint32_t mInitialBallotTimeoutMS = 1000; - uint32_t mIncrementBallotTimeoutMS = 1000; - - TestNominationSCP(NodeID const& nodeID, SCPQuorumSet const& qSetLocal) - : mSCP(*this, nodeID, true, qSetLocal) - { - auto localQSet = - std::make_shared(mSCP.getLocalQuorumSet()); - storeQuorumSet(localQSet); - } - - void - signEnvelope(SCPEnvelope&) override - { - } - - void - storeQuorumSet(SCPQuorumSetPtr qSet) - { - Hash qSetHash = sha256(xdr::xdr_to_opaque(*qSet.get())); - mQuorumSets[qSetHash] = qSet; - } - - SCPDriver::ValidationLevel - validateValue(uint64 slotIndex, Value const& value, - bool nomination) const override - { - return SCPDriver::kFullyValidatedValue; - } - - SCPQuorumSetPtr - getQSet(Hash const& qSetHash) override - { - if (mQuorumSets.find(qSetHash) != mQuorumSets.end()) - { - - return mQuorumSets[qSetHash]; - } - return SCPQuorumSetPtr(); - } - - void - emitEnvelope(SCPEnvelope const& envelope) override - { - } - - ValueWrapperPtr - combineCandidates(uint64 slotIndex, - ValueWrapperPtrSet const& candidates) override - { - return nullptr; - } - - bool - hasUpgrades(Value const& v) override - { - // Not implemented - releaseAssert(false); - } - - ValueWrapperPtr - stripAllUpgrades(Value const& v) override - { - // Not implemented - releaseAssert(false); - } - - uint32_t - getUpgradeNominationTimeoutLimit() const override - { - return std::numeric_limits::max(); - } - - void - setupTimer(uint64 slotIndex, int timerID, std::chrono::milliseconds timeout, - std::function cb) override - { - } - - void - stopTimer(uint64 slotIndex, int timerID) override - { - } - - std::optional - getTxSetDownloadWaitTime(Value const& v) const override - { - return std::nullopt; - } - - std::chrono::milliseconds - getTxSetDownloadTimeout() const override - { - return std::chrono::milliseconds(100); - } - - Value - makeEmptyTxSetValueFromValue(Value const& value) const override - { - releaseAssert(false); - } - - bool - isEmptyTxSetValue(Value const& v) const override - { - releaseAssert(false); - } - - bool - isParallelTxSetDownloadEnabled() const override - { - return true; - } - - bool - protocolAllowsEmptyTxSetValues() const override - { - return true; - } - - std::map mQuorumSets; - - Value const& - getLatestCompositeCandidate(uint64 slotIndex) - { - static Value const emptyValue{}; - return emptyValue; - } - - Hash - getHashOf(std::vector> const& vals) const override - { - SHA256 hasher; - for (auto const& v : vals) - { - hasher.add(v); - } - return hasher.finish(); - } - - // Copied from HerderSCPDriver.cpp - static uint32_t const MAX_TIMEOUT_MS = (30 * 60) * 1000; - - std::chrono::milliseconds - computeTimeout(uint32 roundNumber, bool isNomination) override - { - int initialTimeoutMS; - int incrementMS; - - if (isNomination) - { - initialTimeoutMS = mInitialNominationTimeoutMS; - incrementMS = mIncrementNominationTimeoutMS; - } - else - { - initialTimeoutMS = mInitialBallotTimeoutMS; - incrementMS = mIncrementBallotTimeoutMS; - } - - int timeoutMS = initialTimeoutMS + (roundNumber - 1) * incrementMS; - if (timeoutMS > MAX_TIMEOUT_MS) - { - timeoutMS = MAX_TIMEOUT_MS; - } - return std::chrono::milliseconds(timeoutMS); - } - - bool - isEnvelopeReady(SCPEnvelope const& envelope) const override - { - // Not implemented. These tests do not use PendingEnvelopes, and so do - // not require this method. - releaseAssert(false); - } -}; - -// Deliberately repeat the first winner in round 2, so the second call must -// fast-forward to round 3. This catches predicting round 2's hash in isolation. -class LeaderPreviewTestDriver : public TestNominationSCP -{ - public: - std::vector nodes; - bool tie = false; - std::set zeroWeight; - size_t emitted = 0; - size_t timers = 0; - std::function nominationTimeout; - - LeaderPreviewTestDriver(NodeID const& local, SCPQuorumSet const& qset) - : TestNominationSCP(local, qset), nodes(qset.validators) - { - } - - uint64 - getNodeWeight(NodeID const& id, SCPQuorumSet const&, bool) const override - { - return zeroWeight.count(id) ? 0 : UINT64_MAX; - } - - uint64 - computeHashNode(uint64, Value const&, bool priority, int32 round, - NodeID const& id) override - { - if (!priority) - { - return 0; - } - auto winner = round < 3 ? nodes[0] : round == 3 ? nodes[1] : nodes[2]; - return id == winner || (tie && round < 3 && id == nodes[2]) ? 100 : 1; - } - - void - emitEnvelope(SCPEnvelope const&) override - { - ++emitted; - } - - void - setupTimer(uint64, int timerID, std::chrono::milliseconds, - std::function cb) override - { - ++timers; - if (timerID == Slot::NOMINATION_TIMER) - { - nominationTimeout = std::move(cb); - } - } -}; - -TEST_CASE("nomination leader preview has no protocol side effects", - "[scp][early-nomination]") -{ - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); - SCPQuorumSet qset; - qset.threshold = 2; - qset.validators = {v0NodeID, v1NodeID, v2NodeID}; - // The local node is the *second* leader. Merely preparing its value must - // not cause it to vote before the actual nomination timeout. - LeaderPreviewTestDriver driver(v1NodeID, qset); - auto& scp = driver.mSCP; - Value previous{42}, value{43}; - auto slot = std::make_shared(7, scp); - auto const first = scp.predictNominationLeaders(7, previous, 1); - auto const firstTwo = scp.predictNominationLeaders(7, previous, 2); - REQUIRE(first == std::set{v0NodeID}); - REQUIRE(firstTwo == std::set{v0NodeID, v1NodeID}); - REQUIRE(scp.getKnownSlotsCount() == 0); - REQUIRE(slot->getNominationLeaders().empty()); - REQUIRE(driver.emitted == 0); - REQUIRE(driver.timers == 0); - - size_t builds = 0; - slot->nominate( - [&]() { - ++builds; - return std::make_shared(value); - }, - previous, false); - REQUIRE(builds == 0); - REQUIRE(slot->getNominationLeaders() == first); - REQUIRE(driver.emitted == 0); - REQUIRE(driver.timers == 1); - // Preview again after nomination has started: it still changes no state. - REQUIRE(scp.predictNominationLeaders(7, previous, 2) == firstTwo); - REQUIRE(slot->getNominationLeaders() == first); - REQUIRE(driver.timers == 1); - auto timeout = std::move(driver.nominationTimeout); - REQUIRE(timeout); - timeout(); - REQUIRE(slot->getNominationLeaders() == firstTwo); - REQUIRE(driver.emitted == 1); - REQUIRE(driver.timers == 2); - REQUIRE(builds == 1); - timeout = std::move(driver.nominationTimeout); - REQUIRE(timeout); - timeout(); - REQUIRE(builds == 1); -} - -TEST_CASE("lazy nomination follows leaders and handles unavailable values", - "[scp][early-nomination]") -{ - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); - SCPQuorumSet qset; - qset.threshold = 3; - qset.validators = {v0NodeID, v1NodeID, v2NodeID}; - LeaderPreviewTestDriver driver(v1NodeID, qset); - auto& scp = driver.mSCP; - Value previous{42}, value{43}; - size_t builds = 0; - bool available = true; - bool stopWhileBuilding = false; - NominationValueSupplier makeValue = [&]() -> ValueWrapperPtr { - ++builds; - if (stopWhileBuilding) - { - scp.stopNomination(7); - } - return available ? std::make_shared(value) : nullptr; - }; - - SECTION("a follower votes for the leader without constructing a value") - { - SCPEnvelope envelope; - envelope.statement.nodeID = v0NodeID; - envelope.statement.slotIndex = 7; - envelope.statement.pledges.type(SCP_ST_NOMINATE); - auto& nom = envelope.statement.pledges.nominate(); - nom.quorumSetHash = sha256(xdr::xdr_to_opaque(qset)); - nom.votes.push_back(value); - REQUIRE(scp.receiveEnvelope(driver.wrapEnvelope(envelope)) == - SCP::VALID); - REQUIRE(scp.nominate(7, makeValue, previous)); - REQUIRE(builds == 0); - REQUIRE(driver.emitted == 1); - auto timeout = std::move(driver.nominationTimeout); - timeout(); - REQUIRE(scp.getNominationLeaders(7).count(v1NodeID)); - // Even a promoted leader needs no private value if it already follows - // a usable value from the first leader. - REQUIRE(builds == 0); - } - SECTION("an unavailable proposal is retried on the next timeout") - { - available = false; - REQUIRE(!scp.nominate(7, makeValue, previous)); - REQUIRE(builds == 0); - auto timeout = std::move(driver.nominationTimeout); - timeout(); - REQUIRE(builds == 1); - REQUIRE(driver.emitted == 0); - available = true; - timeout = std::move(driver.nominationTimeout); - REQUIRE(timeout); - timeout(); - REQUIRE(builds == 2); - REQUIRE(driver.emitted == 1); - } - SECTION("stopping nomination during construction cannot emit or rearm") - { - stopWhileBuilding = true; - scp.nominate(7, makeValue, previous); - REQUIRE(builds == 0); - auto timeout = std::move(driver.nominationTimeout); - timeout(); - REQUIRE(builds == 1); - REQUIRE(driver.emitted == 0); - REQUIRE(driver.timers == 1); - } - SECTION("a stopped follower never constructs a proposal") - { - scp.nominate(7, makeValue, previous); - scp.stopNomination(7); - auto timeout = std::move(driver.nominationTimeout); - timeout(); - REQUIRE(builds == 0); - REQUIRE(driver.emitted == 0); - REQUIRE(driver.timers == 1); - } -} - -TEST_CASE("nomination leader preview preserves ties and zero weights", - "[scp][early-nomination]") -{ - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); - SCPQuorumSet qset; - qset.threshold = 2; - qset.validators = {v0NodeID, v1NodeID, v2NodeID}; - LeaderPreviewTestDriver driver(v1NodeID, qset); - Value previous{42}; - SECTION("ties may elect more than two nodes in the first two calls") - { - driver.tie = true; - REQUIRE(driver.mSCP.predictNominationLeaders(7, previous, 1) == - std::set{v0NodeID, v2NodeID}); - REQUIRE(driver.mSCP.predictNominationLeaders(7, previous, 2) == - std::set{v0NodeID, v1NodeID, v2NodeID}); - } - SECTION("zero weight winners are excluded, including self") - { - driver.zeroWeight = {v0NodeID, v1NodeID}; - REQUIRE(driver.mSCP.predictNominationLeaders(7, previous, 2) == - std::set{v2NodeID}); - } - SECTION("no eligible nodes") - { - driver.zeroWeight = {v0NodeID, v1NodeID, v2NodeID}; - REQUIRE(driver.mSCP.predictNominationLeaders(7, previous, 2).empty()); - } - REQUIRE(driver.emitted == 0); - REQUIRE(driver.timers == 0); - REQUIRE(driver.mSCP.getKnownSlotsCount() == 0); -} - -TEST_CASE("nomination weight", "[scp]") -{ - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); - SIMULATION_CREATE_NODE(3); - SIMULATION_CREATE_NODE(4); - SIMULATION_CREATE_NODE(5); - - SCPQuorumSet qSet; - qSet.threshold = 3; - qSet.validators.push_back(v0NodeID); - qSet.validators.push_back(v1NodeID); - qSet.validators.push_back(v2NodeID); - qSet.validators.push_back(v3NodeID); - - TestNominationSCP const nomSCP(v0NodeID, qSet); - - uint64 result = nomSCP.getNodeWeight(v2NodeID, qSet, false); - - REQUIRE(isNear(result, .75)); - - result = nomSCP.getNodeWeight(v4NodeID, qSet, false); - REQUIRE(result == 0); - - SCPQuorumSet iQSet; - iQSet.threshold = 1; - iQSet.validators.push_back(v4NodeID); - iQSet.validators.push_back(v5NodeID); - qSet.innerSets.push_back(iQSet); - - result = nomSCP.getNodeWeight(v4NodeID, qSet, false); - - REQUIRE(isNear(result, .6 * .5)); -} - -class NominationTestHandler : public NominationProtocol -{ - public: - NominationTestHandler(Slot& s) : NominationProtocol(s) - { - } - - void - setPreviousValue(Value const& v) - { - mPreviousValue = v; - } - - void - setRoundNumber(int32 n) - { - mRoundNumber = n; - } - - void - updateRoundLeaders() - { - NominationProtocol::updateRoundLeaders(); - } - - std::set& - getRoundLeaders() - { - return mRoundLeaders; - } - - uint64 - getNodePriority(NodeID const& nodeID, SCPQuorumSet const& qset) - { - return NominationProtocol::getNodePriority(nodeID, qset); - } -}; - -// A test SCPDriver that allows specification of nodes with 0 weight. -class ZeroWeightTestNominationSCP : public TestNominationSCP -{ - public: - std::set mZeroWeightNodes; - - ZeroWeightTestNominationSCP(NodeID const& nodeID, - SCPQuorumSet const& qSetLocal, - std::set const& zeroWeightNodes) - : TestNominationSCP(nodeID, qSetLocal) - , mZeroWeightNodes(zeroWeightNodes) - { - } - - uint64 - getNodeWeight(NodeID const& nodeID, SCPQuorumSet const& qset, - bool isLocalNode) const override - { - if (mZeroWeightNodes.count(nodeID)) - { - return 0; - } - return TestNominationSCP::getNodeWeight(nodeID, qset, isLocalNode); - } -}; - -static SCPQuorumSet -makeQSet(std::vector const& nodeIDs, int threshold, int total, - int offset) -{ - SCPQuorumSet qSet; - qSet.threshold = threshold; - for (int i = 0; i < total; i++) - { - qSet.validators.push_back(nodeIDs[i + offset]); - } - return qSet; -} - -TEST_CASE("updateRoundLeaders handles zero weight nodes", "[scp]") -{ - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); - - // 3 nodes total: v0 (local), v1 (normal weight), v2 (zero weight). - SCPQuorumSet qSet; - qSet.threshold = 2; - qSet.validators.push_back(v0NodeID); - qSet.validators.push_back(v1NodeID); - qSet.validators.push_back(v2NodeID); - - auto runScenario = [&](std::set const& zeroWeightNodes) { - ZeroWeightTestNominationSCP nomSCP(v0NodeID, qSet, zeroWeightNodes); - - Slot slot(0, nomSCP.mSCP); - NominationTestHandler nom(slot); - - Value v; - v.emplace_back(uint8_t(42)); - nom.setPreviousValue(v); - - // Ensure that even with many more rounds than validators, - // `updateRoundLeaders` always terminates and never picks a zero-weight - // node as leader. - int const maxRounds = 20; - for (int i = 0; i < maxRounds; i++) - { - nom.setRoundNumber(i); - nom.updateRoundLeaders(); - } - - return nom.getRoundLeaders(); - }; - - SECTION("non-local zero-weight validator is excluded from round leaders") - { - // v2 simulates a LOW-quality validator with zero weight, mimicking - // HerderSCPDriver::getNodeWeight behavior for LOW-quality nodes. - auto const& leaders = runScenario({v2NodeID}); - REQUIRE(leaders.count(v0NodeID) == 1); - REQUIRE(leaders.count(v1NodeID) == 1); - REQUIRE(leaders.count(v2NodeID) == 0); - REQUIRE(leaders.size() == 2); - } - - SECTION("local zero-weight validator is excluded from round leaders") - { - auto const& leaders = runScenario({v0NodeID}); - REQUIRE(leaders.count(v0NodeID) == 0); - REQUIRE(leaders.count(v1NodeID) == 1); - REQUIRE(leaders.count(v2NodeID) == 1); - REQUIRE(leaders.size() == 2); - } -} - -// this test case display statistical information on the priority function used -// by nomination -TEST_CASE("nomination weight stats", "[scp][!hide]") -{ - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); - - SIMULATION_CREATE_NODE(3); - SIMULATION_CREATE_NODE(4); - SIMULATION_CREATE_NODE(5); - SIMULATION_CREATE_NODE(6); - - std::vector nodeIDs = {v0NodeID, v1NodeID, v2NodeID, v3NodeID, - v4NodeID, v5NodeID, v6NodeID}; - - int const totalSlots = 1000; - int const maxRoundPerSlot = 5; // 5 -> 15 seconds - int const totalRounds = totalSlots * maxRoundPerSlot; - - auto runTests = [&](SCPQuorumSet qSet) { - std::map wins; - - TestNominationSCP nomSCP(v0NodeID, qSet); - for (int s = 0; s < totalSlots; s++) - { - Slot slot(s, nomSCP.mSCP); - - NominationTestHandler nom(slot); - - Value v; - v.emplace_back(uint8_t(s)); // anything will do as a value - - nom.setPreviousValue(v); - - for (int i = 0; i < maxRoundPerSlot; i++) - { - nom.setRoundNumber(i); - nom.updateRoundLeaders(); - auto& l = nom.getRoundLeaders(); - REQUIRE(!l.empty()); - for (auto& w : l) - { - wins[w]++; - } - } - } - return wins; - }; - - SECTION("flat quorum") - { - auto flatTest = [&](int threshold, int total) { - auto qSet = makeQSet(nodeIDs, threshold, total, 0); - - auto wins = runTests(qSet); - - for (auto& w : wins) - { - double stats = double(w.second * 100) / double(totalRounds); - CLOG_INFO(SCP, "Got {}{}", stats, - ((v0NodeID == w.first) ? " LOCAL" : "")); - } - }; - - SECTION("3 out of 5") - { - flatTest(3, 5); - } - SECTION("2 out of 3") - { - flatTest(2, 3); - } - } - SECTION("hierarchy") - { - auto qSet = makeQSet(nodeIDs, 3, 4, 0); - - auto qSetInner = makeQSet(nodeIDs, 2, 3, 4); - qSet.innerSets.emplace_back(qSetInner); - - auto wins = runTests(qSet); - - for (auto& w : wins) - { - double stats = double(w.second * 100) / double(totalRounds); - bool outer = - std::any_of(qSet.validators.begin(), qSet.validators.end(), - [&](auto const& k) { return k == w.first; }); - CLOG_INFO(SCP, "Got {} {}", stats, - ((v0NodeID == w.first) ? "LOCAL" - : (outer ? "OUTER" : "INNER"))); - } - } -} - -TEST_CASE("nomination two nodes win stats", "[scp][!hide]") -{ - int const nbRoundsForStats = 9; - SIMULATION_CREATE_NODE(0); - SIMULATION_CREATE_NODE(1); - SIMULATION_CREATE_NODE(2); - - SIMULATION_CREATE_NODE(3); - SIMULATION_CREATE_NODE(4); - SIMULATION_CREATE_NODE(5); - SIMULATION_CREATE_NODE(6); - - std::vector nodeIDs = {v0NodeID, v1NodeID, v2NodeID, v3NodeID, - v4NodeID, v5NodeID, v6NodeID}; - - int const totalIter = 10000; - - // maxRounds is the number of rounds to evaluate in a row - // the iteration is considered successful if validators could - // agree on what to nominate before maxRounds is reached - auto nominationLeaders = [&](int maxRounds, SCPQuorumSet qSetNode0, - SCPQuorumSet qSetNode1) { - TestNominationSCP nomSCP0(v0NodeID, qSetNode0); - TestNominationSCP nomSCP1(v1NodeID, qSetNode1); - - int tot = 0; - for (int g = 0; g < totalIter; g++) - { - Slot slot0(0, nomSCP0.mSCP); - NominationTestHandler nom0(slot0); - - Slot slot1(0, nomSCP1.mSCP); - NominationTestHandler nom1(slot1); - - Value v; - v.emplace_back(uint8_t(g)); - nom0.setPreviousValue(v); - nom1.setPreviousValue(v); - - bool res = true; - - bool v0Voted = false; - bool v1Voted = false; - - int r = 0; - do - { - nom0.setRoundNumber(r); - nom1.setRoundNumber(r); - nom0.updateRoundLeaders(); - nom1.updateRoundLeaders(); - - auto& l0 = nom0.getRoundLeaders(); - REQUIRE(!l0.empty()); - auto& l1 = nom1.getRoundLeaders(); - REQUIRE(!l1.empty()); - - auto updateVoted = [&](auto const& id, auto const& leaders, - bool& voted) { - if (!voted) - { - voted = std::find(leaders.begin(), leaders.end(), id) != - leaders.end(); - } - }; - - // checks if id voted (any past round, including this one) - // AND id is a leader this round - auto findNode = [](auto const& id, bool idVoted, - auto const& otherLeaders) { - bool r = (idVoted && std::find(otherLeaders.begin(), - otherLeaders.end(), - id) != otherLeaders.end()); - return r; - }; - - updateVoted(v0NodeID, l0, v0Voted); - updateVoted(v1NodeID, l1, v1Voted); - - // either both vote for v0 or both vote for v1 - res = findNode(v0NodeID, v0Voted, l1); - res = res || findNode(v1NodeID, v1Voted, l0); - } while (!res && ++r < maxRounds); - - tot += res ? 1 : 0; - } - return tot; - }; - - SECTION("flat quorum") - { - // test using the same quorum on all nodes - auto flatTest = [&](int threshold, int total) { - auto qSet = makeQSet(nodeIDs, threshold, total, 0); - - for (int maxRounds = 1; maxRounds <= nbRoundsForStats; maxRounds++) - { - int tot = nominationLeaders(maxRounds, qSet, qSet); - double stats = double(tot * 100) / double(totalIter); - CLOG_INFO(SCP, "Win rate for {} : {}", maxRounds, stats); - } - }; - - SECTION("3 out of 5") - { - flatTest(3, 5); - } - SECTION("2 out of 3") - { - flatTest(2, 3); - } - } - - SECTION("hierarchy") - { - SECTION("same qSet") - { - auto qSet = makeQSet(nodeIDs, 3, 4, 0); - - auto qSetInner = makeQSet(nodeIDs, 2, 3, 4); - qSet.innerSets.emplace_back(qSetInner); - - for (int maxRounds = 1; maxRounds <= nbRoundsForStats; maxRounds++) - { - int tot = nominationLeaders(maxRounds, qSet, qSet); - double stats = double(tot * 100) / double(totalIter); - CLOG_INFO(SCP, "Win rate for {} : {}", maxRounds, stats); - } - } - SECTION("v0 is inner node for v1") - { - auto qSet0 = makeQSet(nodeIDs, 3, 4, 0); - auto qSetInner0 = makeQSet(nodeIDs, 2, 3, 4); - qSet0.innerSets.emplace_back(qSetInner0); - - // v1's qset: we move v0 into the inner set - auto qSet1 = qSet0; - REQUIRE(qSet1.validators[0] == v0NodeID); - std::swap(qSet1.validators[0], qSet1.innerSets[0].validators[0]); - - for (int maxRounds = 1; maxRounds <= nbRoundsForStats; maxRounds++) - { - int tot = nominationLeaders(maxRounds, qSet0, qSet1); - double stats = double(tot * 100) / double(totalIter); - CLOG_INFO(SCP, "Win rate for {} : {}", maxRounds, stats); - } - } - } -} -} diff --git a/src/simulation/ApplyLoad.cpp b/src/simulation/ApplyLoad.cpp index 720f030d8..f96126f9b 100644 --- a/src/simulation/ApplyLoad.cpp +++ b/src/simulation/ApplyLoad.cpp @@ -1050,16 +1050,13 @@ ApplyLoad::closeBenchmarkLedger(std::vector const& txs, xdr::xdr_from_opaque(wireBytes, receivedXdr); auto receivedTxSet = TxSetXDRFrame::makeFromWire(receivedXdr); - // Nomination through externalization and apply use production SCP paths. + // Proposal through externalization and apply use production SCP paths. herder.getPendingEnvelopes().putTxSet(receivedTxSet->getContentsHash(), ledgerSeq, receivedTxSet); auto& driver = herder.getHerderSCPDriver(); - driver.nominate( - ledgerSeq, - [&driver, nominatedValue]() { - return driver.wrapStellarValue(nominatedValue); - }, - lcl.header.scpValue); + driver.startSlot(ledgerSeq, [&driver, nominatedValue]() { + return driver.wrapStellarValue(nominatedValue); + }); auto const deadline = std::chrono::steady_clock::now() + std::chrono::seconds(60); diff --git a/src/test/TxTests.cpp b/src/test/TxTests.cpp index 66b40cc8c..d4e4a68c1 100644 --- a/src/test/TxTests.cpp +++ b/src/test/TxTests.cpp @@ -80,7 +80,7 @@ captureLastClosedLedgerLcm(Application& app) // Configs whose ledger content depends on thread scheduling or on // randomized nomination cannot produce reproducible golden data. if (app.getConfig().EXPERIMENTAL_PARALLEL_TX_SET_DOWNLOAD || - app.getConfig().TESTING_NOMINATE_RANDOM_VALUES) + app.getConfig().TESTING_PROPOSE_RANDOM_VALUES) { disableLcmCapture(); } From 1a52bf6af94ccacab0c263720bfb2842d3fee0b0 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Tue, 10 Nov 2026 13:04:33 -0800 Subject: [PATCH 38/39] Run make format --- src/herder/HerderImpl.cpp | 3 ++- src/herder/TxSetFrame.cpp | 7 +++---- src/overlay/test/OverlayIPCTests.cpp | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index d1abc8073..e26125643 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -1918,7 +1918,8 @@ HerderImpl::buildTxSet(uint32_t ledgerSeq, ConsensusTime closeTime) stopSignaturePrewarm(); auto& metrics = mApp.getMetrics(); auto txEnvelopes = [&]() { - auto t = metrics.NewTimer({"herder", "txset", "build-pull"}).TimeScope(); + auto t = + metrics.NewTimer({"herder", "txset", "build-pull"}).TimeScope(); return pullTopTransactions(maxTxSetCandidates() * 2); }(); auto framesTimer = diff --git a/src/herder/TxSetFrame.cpp b/src/herder/TxSetFrame.cpp index 8c96fa2a4..6ef86de24 100644 --- a/src/herder/TxSetFrame.cpp +++ b/src/herder/TxSetFrame.cpp @@ -917,10 +917,9 @@ makeTxSetFromTransactions( else { #endif - auto trimTimer = - app.getMetrics() - .NewTimer({"herder", "txset", "build-trim"}) - .TimeScope(); + auto trimTimer = app.getMetrics() + .NewTimer({"herder", "txset", "build-trim"}) + .TimeScope(); if (validTxTargets.empty()) { validatedTxs = TxSetUtils::trimInvalid( diff --git a/src/overlay/test/OverlayIPCTests.cpp b/src/overlay/test/OverlayIPCTests.cpp index 540e2c7c7..12a3f5c60 100644 --- a/src/overlay/test/OverlayIPCTests.cpp +++ b/src/overlay/test/OverlayIPCTests.cpp @@ -550,8 +550,7 @@ makeLargeTxEnvelope(int64_t fee, int64_t seqNum, uint8_t accountByte, * IPC reader), and a pull larger than the limit is truncated to the * highest-priority transactions that fit instead of breaking the connection. */ -TEST_CASE("Rust overlay returns top transactions over 16 MiB", - "[overlay-ipc]") +TEST_CASE("Rust overlay returns top transactions over 16 MiB", "[overlay-ipc]") { std::string overlayBinary = requireOverlayBinary(); TmpDir tmpDir("overlay_ipc_large_top_txs_test"); @@ -581,7 +580,8 @@ TEST_CASE("Rust overlay returns top transactions over 16 MiB", REQUIRE(txs.size() == 20); for (auto const& tx : txs) { - REQUIRE(tx.v1().tx.operations[0] + REQUIRE(tx.v1() + .tx.operations[0] .body.invokeHostFunctionOp() .hostFunction.wasm() .size() == txBytes); From b524ef9624f9212153adb290aec20542cac94119 Mon Sep 17 00:00:00 2001 From: Garand Tyson Date: Tue, 10 Nov 2026 20:51:59 +0000 Subject: [PATCH 39/39] Describe the leader-schedule prototype and its perfnet results in the README Replace the stock README on this experimental branch with a description of the change, the 2 s (pass) and 1 s (fail) perfnet results measured on daa965f4c, and how they were run: topology, hardware, node configuration, network settings, load and pass criteria. --- README.md | 262 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 238 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 0126b7350..f6d812cfd 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,248 @@ -
-Stellar -
-Creating equitable access to the global financial system -

Stellar Core

-
-

-Build Status -

+# 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. -[![Ask DeepWiki](https://deepwiki.com/badge.svg)](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`.