Skip to main content

diff_inactivity

Function diff_inactivity 

Source
pub fn diff_inactivity(base: &[u64], target: &[u64]) -> InactivityDiff
Expand description

Computes a compact delta between two validator inactivity-score vectors.

The returned delta contains sufficient information to reconstruct target from base.

If the target contains only zero-valued scores and the base contains at least one non-zero score, InactivityDiff::AllZeros is emitted. This avoids storing individual updates when the entire target vector has been cleared.

Otherwise, InactivityDiff::Sparse stores only the indices whose values changed and their corresponding target values. Scores belonging to newly appended validators are stored in the extensions field.

If base and target have different lengths, only their common prefix is compared. Any remaining target scores are treated as newly appended entries.

§Arguments

  • base - Inactivity scores from the source state.
  • target - Inactivity scores from the target state.

§Returns

A compact InactivityDiff representing the transition from base to target.

§Complexity

O(n) time, where n is the length of the larger input vector.

Additional space is proportional to the number of changed scores plus the number of newly appended scores.

§Example

use eth_state_diff::inactivity_scores::diff_inactivity;
use eth_state_diff::types::InactivityDiff;

let base = vec![10, 20, 30, 40];
let target = vec![10, 25, 30, 50];

let delta = diff_inactivity(&base, &target);

assert_eq!(
    delta,
    InactivityDiff::Sparse {
        indices: vec![1, 3],
        new_values: vec![25, 50],
        extensions: vec![],
    }
);

A completely cleared vector can be represented without storing individual zero values:

use eth_state_diff::inactivity_scores::diff_inactivity;
use eth_state_diff::types::InactivityDiff;

let base = vec![10, 20, 30];
let target = vec![0, 0, 0];

assert_eq!(
    diff_inactivity(&base, &target),
    InactivityDiff::AllZeros(3)
);