Skip to main content

eth_state_diff/
pending_queue.rs

1//! Delta encoding for SSZ lists that behave as FIFO queues.
2//!
3//! This module computes compact deltas between serialized SSZ queues by
4//! identifying items that have been consumed from the front of the queue and
5//! items that have been appended to the back.
6//!
7//! The encoding supports two representations:
8//!
9//! - [`QueueDiff::Fifo`] records the number of items consumed from the front
10//!   and the serialized items appended to the back.
11//! - [`QueueDiff::FullReplacement`] stores the complete target queue when the
12//!   FIFO relationship cannot be established safely.
13//!
14//! The FIFO representation is suitable for consensus-layer queues whose
15//! logical behavior is append-at-the-back and consume-from-the-front, such as
16//! pending withdrawals and consolidations. It also supports queues such as
17//! pending deposits where reordering may occur: when the FIFO relationship
18//! cannot be proven from the serialized representation, the algorithm falls
19//! back to [`QueueDiff::FullReplacement`] rather than producing an unsafe
20//! delta.
21//!
22//! ## Candidate detection and validation
23//!
24//! To identify a candidate overlap, the encoder searches for the first item of
25//! the target queue within the base queue. Matching is performed only at valid
26//! SSZ item boundaries determined by `item_ssz_size`.
27//!
28//! Finding an item alone is not sufficient to establish a FIFO transition.
29//! After a candidate overlap is found, the encoder verifies that the remaining
30//! bytes of the base queue exactly match the corresponding prefix of the target
31//! queue. Only after this validation succeeds is a [`QueueDiff::Fifo`] emitted.
32//!
33//! If no valid overlap is found, or the remaining queue contents do not match,
34//! the encoder emits [`QueueDiff::FullReplacement`] containing the complete
35//! target queue.
36//!
37//! This conservative fallback ensures that an ambiguous or reordered queue
38//! is never represented as an incorrect FIFO delta.
39//!
40//! ## Representation
41//!
42//! For a valid FIFO transition:
43//!
44//! ```text
45//! base:   [A, B, C, D]
46//! target: [C, D, E, F]
47//!                 ^--- appended
48//!
49//! consumed_count = 2
50//! appended_items = [E, F]
51//! ```
52//!
53//! Applying the delta removes `A` and `B`, then appends `E` and `F`.
54//!
55//! ## Requirements
56//!
57//! `item_ssz_size` must be the fixed serialized SSZ size of one queue item
58//! and must be greater than zero. The input buffers must contain complete
59//! items, so their lengths must be exact multiples of `item_ssz_size`.
60//!
61//! The module operates directly on serialized SSZ bytes and does not require
62//! deserializing individual queue items during diff generation.
63//!
64//! ## Complexity
65//!
66//! [`diff_queue`] performs a linear scan of the base queue for the target head,
67//! followed by a linear validation of the candidate overlap. The resulting
68//! algorithm is O(n) in the size of the serialized queues.
69//!
70//! [`apply_queue`] performs O(n) work proportional to the bytes consumed and
71//! appended for a FIFO delta, or O(n) in the target queue size for a full
72//! replacement.
73//!
74//! # Example
75//!
76//! ```
77//! # use eth_state_diff::types::QueueDiff;
78//! # use eth_state_diff::pending_queue::diff_queue;
79//!
80//! const ITEM_SIZE: usize = 4;
81//!
82//! let base = b"AAAABBBBCCCC";
83//! let target = b"CCCCDDDDEEEE";
84//!
85//! let delta = diff_queue(base, target, ITEM_SIZE);
86//!
87//! assert_eq!(
88//!     delta,
89//!     QueueDiff::Fifo {
90//!         consumed_count: 2,
91//!         appended_items: b"DDDDEEEE".to_vec(),
92//!     }
93//! );
94//! ```
95
96use crate::{
97    types::{ArchivedQueueDiff, QueueDiff},
98    Error,
99};
100
101/// Finds the first occurrence of an SSZ-encoded queue item within `haystack`,
102/// considering only valid item boundaries.
103///
104/// The search is performed in `item_ssz_size`-byte chunks rather than with a
105/// byte-level substring search. This prevents a sequence of bytes occurring
106/// inside one SSZ item from being incorrectly interpreted as a queue-item
107/// boundary.
108///
109/// Returns the byte offset of the first matching item, or `None` if no aligned
110/// match exists.
111///
112/// # Requirements
113///
114/// `needle.len()` must equal `item_ssz_size`. If it does not, the function
115/// returns `None`.
116fn find_chunk_aligned(haystack: &[u8], needle: &[u8], item_ssz_size: usize) -> Option<usize> {
117    if needle.len() != item_ssz_size {
118        return None;
119    }
120
121    haystack
122        .chunks_exact(item_ssz_size)
123        .position(|chunk| chunk == needle)
124        .map(|idx| idx * item_ssz_size)
125}
126
127/// Computes a delta between two serialized SSZ queues.
128///
129/// The encoder first attempts to represent the transition as a FIFO operation:
130///
131/// 1. The first item of `target_ssz` is located in `base_ssz`.
132/// 2. The search is restricted to valid item boundaries using
133///    `item_ssz_size`.
134/// 3. The remaining bytes of the base queue are compared with the corresponding
135///    prefix of the target queue.
136/// 4. If they match exactly, the transition is represented as
137///    [`QueueDiff::Fifo`].
138/// 5. Otherwise, the complete target queue is stored as
139///    [`QueueDiff::FullReplacement`].
140///
141/// This validation is important for queues that may occasionally reorder
142/// items. An overlap by itself does not prove that the target is a continuation
143/// of the base queue.
144///
145/// # Arguments
146///
147/// * `base_ssz` - Serialized SSZ representation of the base queue.
148/// * `target_ssz` - Serialized SSZ representation of the target queue.
149/// * `item_ssz_size` - Fixed serialized SSZ size, in bytes, of one queue item.
150///
151/// # Returns
152///
153/// [`QueueDiff::Fifo`] when the target can be safely represented as consumed
154/// items followed by appended items. Otherwise returns
155/// [`QueueDiff::FullReplacement`] containing the complete target queue.
156///
157/// # Panics
158///
159/// Panics if `item_ssz_size` is zero or if either input contains an incomplete
160/// serialized item.
161///
162/// # Complexity
163///
164/// O(n) time, where *n* is the combined size of the queues in bytes, with
165/// O(m) additional space for the encoded appended or replacement bytes.
166///
167/// # Example
168///
169/// ```
170/// # use eth_state_diff::pending_queue::diff_queue;
171/// # use eth_state_diff::types::QueueDiff;
172///
173/// const ITEM_SIZE: usize = 4;
174///
175/// let base = b"AAAABBBBCCCC";
176/// let target = b"CCCCDDDDEEEE";
177///
178/// let delta = diff_queue(base, target, ITEM_SIZE);
179///
180/// assert_eq!(
181///     delta,
182///     QueueDiff::Fifo {
183///         consumed_count: 2,
184///         appended_items: b"DDDDEEEE".to_vec(),
185///     }
186/// );
187/// ```
188pub fn diff_queue(base_ssz: &[u8], target_ssz: &[u8], item_ssz_size: usize) -> QueueDiff {
189    assert!(item_ssz_size > 0, "item_ssz_size must be greater than 0");
190    assert!(
191        base_ssz.len().is_multiple_of(item_ssz_size),
192        "base_ssz length must be a multiple of item_ssz_size"
193    );
194    assert!(
195        target_ssz.len().is_multiple_of(item_ssz_size),
196        "target_ssz length must be a multiple of item_ssz_size"
197    );
198
199    // Edge case: target is empty, everything was consumed.
200    if target_ssz.is_empty() {
201        let consumed_count = base_ssz.len() / item_ssz_size;
202
203        return QueueDiff::Fifo {
204            consumed_count: u32::try_from(consumed_count)
205                .expect("queue item count exceeds u32::MAX"),
206            appended_items: Vec::new(),
207        };
208    }
209
210    // Edge case: base is empty, everything is an append.
211    if base_ssz.is_empty() {
212        return QueueDiff::Fifo {
213            consumed_count: 0,
214            appended_items: target_ssz.to_vec(),
215        };
216    }
217
218    let target_head = &target_ssz[..item_ssz_size];
219
220    match find_chunk_aligned(base_ssz, target_head, item_ssz_size) {
221        Some(byte_offset) => {
222            let remaining_base_bytes = &base_ssz[byte_offset..];
223            let expected_target_prefix_len = remaining_base_bytes.len();
224
225            // Validate that the overlapping portion is identical.
226            if expected_target_prefix_len <= target_ssz.len()
227                && &target_ssz[..expected_target_prefix_len] == remaining_base_bytes
228            {
229                let consumed_count = byte_offset / item_ssz_size;
230
231                let consumed_count =
232                    u32::try_from(consumed_count).expect("queue item count exceeds u32::MAX");
233
234                let appended_items = target_ssz[expected_target_prefix_len..].to_vec();
235
236                QueueDiff::Fifo {
237                    consumed_count,
238                    appended_items,
239                }
240            } else {
241                QueueDiff::FullReplacement(target_ssz.to_vec())
242            }
243        }
244        None => QueueDiff::FullReplacement(target_ssz.to_vec()),
245    }
246}
247
248/// Applies a queue delta to a serialized SSZ queue in place.
249///
250/// For [`QueueDiff::Fifo`], the specified number of items are removed from the
251/// front of `base`, after which the appended serialized items are added to the
252/// back.
253///
254/// For [`QueueDiff::FullReplacement`], the existing queue is cleared and
255/// replaced with the serialized target queue stored in the delta.
256///
257/// # Arguments
258///
259/// * `base` - Mutable serialized SSZ representation of the queue to update.
260/// * `delta` - Archived queue delta previously produced by [`diff_queue`] and
261///   serialized with `rkyv`.
262/// * `item_ssz_size` - Fixed serialized SSZ size of one queue item.
263///
264/// # Errors
265///
266/// Returns [`Error::MalformedDelta`] if:
267///
268/// - the archived delta cannot be deserialized;
269/// - `item_ssz_size` is inconsistent with the serialized delta;
270/// - the number of consumed items exceeds the number of items in `base`;
271/// - the consumed-byte calculation overflows;
272/// - appended or replacement bytes are not aligned to `item_ssz_size`.
273///
274/// # Behavior
275///
276/// After successful execution, `base` represents the target queue from which
277/// the delta was originally generated.
278///
279/// # Complexity
280///
281/// For [`QueueDiff::Fifo`], the operation is O(n) in the number of bytes
282/// removed and appended. Removing bytes from the front may require shifting
283/// the remaining contents of the `Vec`.
284///
285/// For [`QueueDiff::FullReplacement`], the operation is O(n) in the size of
286/// the replacement queue.
287///
288/// # Example
289///
290/// ```
291/// # use eth_state_diff::pending_queue::{apply_queue, diff_queue};
292///
293/// const ITEM_SIZE: usize = 4;
294///
295/// let mut base = b"AAAABBBBCCCC".to_vec();
296/// let target = b"CCCCDDDDEEEE";
297///
298/// let delta = diff_queue(&base, target, ITEM_SIZE);
299/// let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&delta).unwrap();
300/// let archived = unsafe {
301///     rkyv::access_unchecked::<eth_state_diff::types::ArchivedQueueDiff>(&bytes)
302/// };
303///
304/// apply_queue(&mut base, archived, ITEM_SIZE).unwrap();
305///
306/// assert_eq!(base, target);
307/// ```
308pub fn apply_queue(
309    base: &mut Vec<u8>,
310    delta: &ArchivedQueueDiff,
311    item_ssz_size: usize,
312) -> Result<(), Error> {
313    if item_ssz_size == 0 {
314        return Err(Error::MalformedDelta(
315            "item_ssz_size must be greater than 0".into(),
316        ));
317    }
318
319    if !base.len().is_multiple_of(item_ssz_size) {
320        return Err(Error::MalformedDelta(format!(
321            "base queue length {} is not a multiple of item size {}",
322            base.len(),
323            item_ssz_size
324        )));
325    }
326
327    let delta: QueueDiff = rkyv::deserialize::<QueueDiff, rkyv::rancor::Error>(delta)
328        .map_err(|_| Error::MalformedDelta("failed to deserialize queue delta".into()))?;
329
330    match delta {
331        QueueDiff::Fifo {
332            consumed_count,
333            appended_items,
334        } => {
335            if !appended_items.len().is_multiple_of(item_ssz_size) {
336                return Err(Error::MalformedDelta(format!(
337                    "FIFO appended payload length {} is not a multiple of item size {}",
338                    appended_items.len(),
339                    item_ssz_size
340                )));
341            }
342
343            let bytes_to_drain = (consumed_count as usize)
344                .checked_mul(item_ssz_size)
345                .ok_or_else(|| {
346                    Error::MalformedDelta("FIFO consumed byte count overflows usize".into())
347                })?;
348
349            if bytes_to_drain > base.len() {
350                return Err(Error::MalformedDelta(format!(
351                    "FIFO consumes {} bytes from a queue containing only {} bytes",
352                    bytes_to_drain,
353                    base.len()
354                )));
355            }
356
357            base.drain(..bytes_to_drain);
358            base.extend_from_slice(&appended_items);
359        }
360
361        QueueDiff::FullReplacement(replacement) => {
362            if !replacement.len().is_multiple_of(item_ssz_size) {
363                return Err(Error::MalformedDelta(format!(
364                    "replacement payload length {} is not a multiple of item size {}",
365                    replacement.len(),
366                    item_ssz_size
367                )));
368            }
369
370            base.clear();
371            base.extend_from_slice(&replacement);
372        }
373    }
374
375    Ok(())
376}