Skip to main content

diff_randao

Function diff_randao 

Source
pub fn diff_randao(
    base_slot: u64,
    target_slot: u64,
    target_buffer: &[[u8; 32]],
) -> RandaoDiff
Expand description

Computes a RANDAO delta between two consensus slots.

The returned delta contains one RANDAO mix for every epoch in the inclusive range from the epoch containing base_slot through the epoch containing target_slot.

Each mix is read from target_buffer using the circular-buffer indexing rule:

buffer_index = epoch % target_buffer.len()

Only the mixes corresponding to the covered epochs are stored. The complete target buffer is not copied.

§Arguments

  • base_slot - Starting consensus slot. The epoch containing this slot is the first epoch represented in the delta.
  • target_slot - Ending consensus slot. The epoch containing this slot is the final epoch represented in the delta.
  • target_buffer - RANDAO mix buffer belonging to the target state.

§Returns

A RandaoDiff containing one mix for each epoch from the base epoch through the target epoch, inclusive.

§Panics

Panics if target_slot < base_slot.

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

§Correctness

The delta stores mixes in chronological epoch order rather than storing their circular-buffer indices. During application, indices are reconstructed using modulo arithmetic and the capacity of the destination buffer.

The buffer used with apply_randao must therefore have the same capacity as target_buffer, and apply_randao must be given the same base_slot.

§Example

use eth_state_diff::randao_mixes::diff_randao;

let base_slot = 0;
let target_slot = 64;

let target_buffer = vec![[0u8; 32]; 4];

let delta = diff_randao(base_slot, target_slot, &target_buffer);

// With 32 slots per epoch, slots 0 and 64 belong to epochs 0 and 2.
// The inclusive epoch range is therefore 0..=2.
assert_eq!(delta.mixes.len(), 3);

§Complexity

If E is the number of epochs from the base epoch through the target epoch, inclusive:

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