Skip to main content

apply_roots

Function apply_roots 

Source
pub fn apply_roots(
    base_slot: u64,
    base_buffer: &mut [[u8; 32]],
    delta: &ArchivedRootsDiff,
)
Expand description

Applies a root delta to a circular root buffer in place.

Each root stored in delta is written to the destination buffer using the same slot-to-index mapping used by diff_roots:

buffer_index = slot % buffer_capacity

The first root in the delta corresponds to base_slot. Each subsequent root corresponds to the next slot.

§Arguments

  • base_slot - The slot corresponding to the first root stored in delta.
  • base_buffer - Destination circular root buffer. It is modified in place.
  • delta - Archived RootsDiff containing the roots to replay.

§Correctness

This function is the application counterpart to diff_roots.

For correct reconstruction, base_buffer must have the same capacity as the buffer that was supplied to diff_roots.

The delta does not contain explicit buffer indices. Indices are derived from base_slot and the destination buffer capacity.

§Panics

Panics if base_buffer is empty, because circular-buffer indexing requires a non-zero capacity.

§Example

use eth_state_diff::recent_roots::{apply_roots, diff_roots};
use eth_state_diff::types::ArchivedRootsDiff;

let mut target_buffer = vec![[0u8; 32]; 4];
target_buffer[0] = [1u8; 32];
target_buffer[1] = [2u8; 32];
target_buffer[2] = [3u8; 32];

let delta = diff_roots(0, 3, &target_buffer);

let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&delta).unwrap();
let archived = unsafe {
    rkyv::access_unchecked::<ArchivedRootsDiff>(&bytes)
};

let mut reconstructed = vec![[0u8; 32]; 4];
apply_roots(0, &mut reconstructed, archived);

assert_eq!(reconstructed, target_buffer);

§Complexity

If N roots are stored in delta:

  • Time: O(N)
  • Additional space: O(1)