Skip to main content

diff_slashings

Function diff_slashings 

Source
pub fn diff_slashings(
    base_slot: u64,
    target_slot: u64,
    base_buffer: &[u64],
    target_buffer: &[u64],
) -> SlashingsDiff
Expand description

Computes a sparse delta between two slashing ring buffers.

The function compares the slashing value at each ring-buffer position corresponding to an epoch boundary crossed between base_slot and target_slot.

For a base epoch B and target epoch T, the examined epochs are:

B + 1, B + 2, ..., T

For each examined epoch E, its ring-buffer position is:

index = E % buffer_capacity

If the value at that position differs between base_buffer and target_buffer, the target value is recorded in the returned SlashingsDiff.

Unchanged entries are omitted from the delta.

§Arguments

  • base_slot - Slot belonging to the base state.
  • target_slot - Slot belonging to the target state.
  • base_buffer - Slashing ring buffer belonging to the base state.
  • target_buffer - Slashing ring buffer belonging to the target state.

§Returns

A SlashingsDiff containing only the ring-buffer entries whose values changed across the epoch boundaries represented by the transition.

If base_slot and target_slot belong to the same epoch, the returned delta contains no updates.

§Panics

Panics if target_slot < base_slot.

Panics if base_buffer is empty or target_buffer is empty.

Panics if base_buffer and target_buffer have different lengths.

§Correctness

The two buffers must have the same capacity and correspond to the same logical slashing ring.

The delta stores ring indices rather than epoch numbers. Consequently, the buffer capacity is part of the implicit encoding and must remain unchanged when applying the resulting delta.

§Example

use eth_state_diff::slashings::diff_slashings;

let base_buffer = vec![0u64; 4];
let mut target_buffer = vec![0u64; 4];

// The transition crosses from epoch 0 to epoch 1.
target_buffer[1] = 100;

let delta = diff_slashings(
    0,
    eth_state_diff::types::SLOTS_PER_EPOCH,
    &base_buffer,
    &target_buffer,
);

assert_eq!(delta.updates.len(), 1);
assert_eq!(delta.updates[0], (1, 100));

§Complexity

If E epoch boundaries are crossed and U entries change:

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