Skip to main content

diff_queue

Function diff_queue 

Source
pub fn diff_queue(
    base_ssz: &[u8],
    target_ssz: &[u8],
    item_ssz_size: usize,
) -> QueueDiff
Expand description

Computes a delta between two serialized SSZ queues.

The encoder first attempts to represent the transition as a FIFO operation:

  1. The first item of target_ssz is located in base_ssz.
  2. The search is restricted to valid item boundaries using item_ssz_size.
  3. The remaining bytes of the base queue are compared with the corresponding prefix of the target queue.
  4. If they match exactly, the transition is represented as QueueDiff::Fifo.
  5. Otherwise, the complete target queue is stored as QueueDiff::FullReplacement.

This validation is important for queues that may occasionally reorder items. An overlap by itself does not prove that the target is a continuation of the base queue.

§Arguments

  • base_ssz - Serialized SSZ representation of the base queue.
  • target_ssz - Serialized SSZ representation of the target queue.
  • item_ssz_size - Fixed serialized SSZ size, in bytes, of one queue item.

§Returns

QueueDiff::Fifo when the target can be safely represented as consumed items followed by appended items. Otherwise returns QueueDiff::FullReplacement containing the complete target queue.

§Edge cases

  • If target_ssz is empty, all items in the base queue are considered consumed and no items are appended.
  • If base_ssz is empty, the target queue is represented as a pure append.
  • If the target head cannot be found at an item boundary, the encoder falls back to full replacement.
  • If the target head is found but the remaining base queue does not exactly match the target prefix, the encoder falls back to full replacement.

§Panics

Panics if item_ssz_size is zero.

§Complexity

O(n) time, where n is the combined size of the queues in bytes, with O(m) additional space for the encoded appended or replacement bytes.

§Example


const ITEM_SIZE: usize = 4;

let base = b"AAAABBBBCCCC";
let target = b"CCCCDDDDEEEE";

let delta = diff_queue(base, target, ITEM_SIZE);

assert_eq!(
    delta,
    QueueDiff::Fifo {
        consumed_count: 2,
        appended_items: b"DDDDEEEE".to_vec(),
    }
);