Skip to main content

eth_state_diff/
sync_committee.rs

1//! Delta encoding for Ethereum sync committees.
2//!
3//! Sync committees are static for the duration of a sync committee period
4//! (256 epochs on Mainnet). Because a standard 32-epoch diff window almost never
5//! crosses a period boundary, this module simply checks for equality and emits
6//! a full replacement only when the committee actually changes.
7
8use crate::types::{ArchivedSyncCommitteeDiff, SyncCommitteeDiff};
9
10/// Computes the delta between two sync committees.
11///
12/// Expects the raw SSZ bytes of the `SyncCommittee` containers (including the
13/// 4-byte SSZ length prefix if serialized as a list, though `SyncCommittee` is
14/// typically serialized as a fixed-size struct).
15pub fn diff_sync_committee(base_ssz: &[u8], target_ssz: &[u8]) -> SyncCommitteeDiff {
16    if base_ssz == target_ssz {
17        SyncCommitteeDiff::Unchanged
18    } else {
19        SyncCommitteeDiff::FullReplacement(target_ssz.to_vec())
20    }
21}
22
23/// Applies a sync committee delta in place.
24///
25/// # Complexity
26///
27/// - `Unchanged`: O(1)
28/// - `FullReplacement`: O(target committee size)
29pub fn apply_sync_committee(base: &mut Vec<u8>, delta: &ArchivedSyncCommitteeDiff) {
30    match delta {
31        ArchivedSyncCommitteeDiff::Unchanged => {
32            // Do nothing
33        }
34        ArchivedSyncCommitteeDiff::FullReplacement(replacement) => {
35            base.clear();
36            base.extend_from_slice(replacement.as_slice());
37        }
38    }
39}