pub fn apply_roots(
base_slot: u64,
base_buffer: &mut [[u8; 32]],
delta: &ArchivedRootsDiff,
) -> Result<(), Error>Expand description
Applies a root delta to a circular root buffer in place.
Each root stored in delta is written to the destination buffer using the
same slot-to-index mapping used by diff_roots:
buffer_index = slot % buffer_capacityThe first root in the delta corresponds to base_slot. Each subsequent
root corresponds to the next slot.
§Arguments
base_slot- The slot corresponding to the first root stored indelta. This must be the same starting slot used to generate the delta.base_buffer- Destination circular root buffer. It is modified in place and must have the same capacity as the buffer used to generate the delta.delta- ArchivedRootsDiffcontaining the roots to replay.
§Correctness
This function is the application counterpart to diff_roots.
For correct reconstruction, base_buffer must have the same capacity as
the buffer supplied to diff_roots, and base_slot must be the same
starting slot used when generating the delta.
The delta does not contain explicit buffer indices. Indices are derived
from base_slot and the destination buffer capacity. Contents at positions
outside the recorded slot range are preserved.
§Errors
Returns Error::InvalidDelta if base_buffer is empty.
§Example
use eth_state_diff::recent_roots::{apply_roots, diff_roots};
use eth_state_diff::types::ArchivedRootsDiff;
let mut target_buffer = vec![[0u8; 32]; 4];
target_buffer[0] = [1u8; 32];
target_buffer[1] = [2u8; 32];
target_buffer[2] = [3u8; 32];
let delta = diff_roots(0, 3, &target_buffer);
let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&delta).expect("valid delta");
let archived = rkyv::access::<ArchivedRootsDiff, rkyv::rancor::Error>(&bytes)
.expect("test setup: failed to access archived delta");
let mut reconstructed = vec![[0u8; 32]; 4];
apply_roots(0, &mut reconstructed, archived).expect("valid delta");
assert_eq!(reconstructed, target_buffer);§Complexity
If N roots are stored in delta:
- Time: O(N)
- Additional space: O(1)