Skip to main content

apply_queue

Function apply_queue 

Source
pub fn apply_queue(
    base: &mut Vec<u8>,
    delta: &ArchivedQueueDiff,
    item_ssz_size: usize,
) -> Result<(), Error>
Expand description

Applies a queue delta to a serialized SSZ queue in place.

For QueueDiff::Fifo, the specified number of items are removed from the front of base, after which the appended serialized items are added to the back.

For QueueDiff::FullReplacement, the existing queue is cleared and replaced with the serialized target queue stored in the delta.

§Arguments

  • base - Mutable serialized SSZ representation of the queue to update.
  • delta - Archived queue delta previously produced by diff_queue and serialized with rkyv.
  • item_ssz_size - Fixed serialized SSZ size of one queue item.

§Errors

Returns Error::MalformedDelta if:

  • the archived delta cannot be deserialized;
  • item_ssz_size is inconsistent with the serialized delta;
  • the number of consumed items exceeds the number of items in base;
  • the consumed-byte calculation overflows;
  • appended or replacement bytes are not aligned to item_ssz_size.

§Behavior

After successful execution, base represents the target queue from which the delta was originally generated.

§Complexity

For QueueDiff::Fifo, the operation is O(n) in the number of bytes removed and appended. Removing bytes from the front may require shifting the remaining contents of the Vec.

For QueueDiff::FullReplacement, the operation is O(n) in the size of the replacement queue.

§Example


const ITEM_SIZE: usize = 4;

let mut base = b"AAAABBBBCCCC".to_vec();
let target = b"CCCCDDDDEEEE";

let delta = diff_queue(&base, target, ITEM_SIZE);
let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&delta).expect("failed to serialize");
let archived = rkyv::access::<eth_state_diff::types::ArchivedQueueDiff, rkyv::rancor::Error>(&bytes)
    .expect("failed to access");

apply_queue(&mut base, archived, ITEM_SIZE).expect("failed to apply");

assert_eq!(base, target);