Skip to main content

apply_inactivity

Function apply_inactivity 

Source
pub fn apply_inactivity(
    base: &mut Vec<u64>,
    delta: &ArchivedInactivityDiff,
) -> Result<(), Error>
Expand description

Applies an inactivity-score delta to a vector in place.

After successful execution, base contains the inactivity-score vector represented by delta.

ArchivedInactivityDiff::AllZeros clears the existing vector and recreates it with the specified number of zero-valued scores.

ArchivedInactivityDiff::Sparse updates the recorded indices and then appends any newly added validator scores.

§Errors

Returns Error::InvalidDelta if:

  • an all-zero vector length cannot be represented as a usize;
  • the sparse index and value arrays have different lengths; or
  • a sparse index is outside the current base vector.

A sparse delta must therefore be applied to a compatible base state.

§Complexity

§Example

use eth_state_diff::inactivity_scores::{apply_inactivity, diff_inactivity};
use eth_state_diff::types::ArchivedInactivityDiff;

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

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

let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&delta)
    .expect("serializing a locally constructed delta should succeed");
let archived = unsafe {
    rkyv::access_unchecked::<ArchivedInactivityDiff>(&bytes)
};

apply_inactivity(&mut base, archived)
    .expect("delta generated from the same base should apply successfully");

assert_eq!(base, target);