Skip to main content

diff_roots

Function diff_roots 

Source
pub fn diff_roots(
    base_slot: u64,
    target_slot: u64,
    buffer: &[[u8; 32]],
) -> RootsDiff
Expand description

Computes the sequence of roots written during a slot range.

The returned delta contains one root for every slot in the half-open range [base_slot, target_slot).

For each slot s, the root is read from the circular buffer at:

buffer_index = s % buffer.len()

The root corresponding to target_slot is not included.

§Arguments

  • base_slot - The first slot whose root is included in the delta.
  • target_slot - The slot immediately following the final recorded root.
  • buffer - Circular root buffer belonging to the target state.

§Returns

A RootsDiff containing the target root for every slot in [base_slot, target_slot).

If base_slot == target_slot, the returned delta contains no roots.

§Panics

Panics if target_slot < base_slot.

Panics if buffer is empty, because calculating a circular-buffer index requires a non-zero capacity.

§Correctness

The buffer’s capacity is part of the implicit representation because root indices are reconstructed using modulo arithmetic.

The buffer supplied to apply_roots must therefore have the same capacity as buffer.

§Example

use eth_state_diff::recent_roots::diff_roots;

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);

assert_eq!(delta.roots.len(), 3);
assert_eq!(delta.roots[0], [1u8; 32]);
assert_eq!(delta.roots[1], [2u8; 32]);
assert_eq!(delta.roots[2], [3u8; 32]);

§Complexity

Let N = target_slot - base_slot.

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