pub fn diff_queue(
base_ssz: &[u8],
target_ssz: &[u8],
item_ssz_size: usize,
) -> QueueDiffExpand description
Computes a delta between two serialized SSZ queues.
The encoder first attempts to represent the transition as a FIFO operation:
- The first item of
target_sszis located inbase_ssz. - The search is restricted to valid item boundaries using
item_ssz_size. - The remaining bytes of the base queue are compared with the corresponding prefix of the target queue.
- If they match exactly, the transition is represented as
QueueDiff::Fifo. - 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.
§Panics
Panics if item_ssz_size is zero or if either input contains an incomplete
serialized item.
§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(),
}
);