Skip to main content

pessimistic_proof/local_exit_tree/
mod.rs

1use agglayer_primitives::Digest;
2use unified_bridge::LocalExitTreeError;
3
4use crate::local_exit_tree::data::LocalExitTreeData;
5pub mod data;
6
7pub use unified_bridge::LocalExitTree;
8
9#[cfg(test)]
10mod tests;
11
12impl<const TREE_DEPTH: usize> TryFrom<&LocalExitTreeData<TREE_DEPTH>>
13    for LocalExitTree<TREE_DEPTH>
14{
15    type Error = LocalExitTreeError;
16
17    fn try_from(data: &LocalExitTreeData<TREE_DEPTH>) -> Result<Self, Self::Error> {
18        let leaf_count = data.layers[0].len();
19        let mut frontier = [Digest::default(); TREE_DEPTH];
20        let mut index = leaf_count;
21        let mut height = 0;
22        while index != 0 {
23            if height >= TREE_DEPTH {
24                return Err(LocalExitTreeError::FrontierIndexOutOfBounds);
25            }
26            if index & 1 == 1 {
27                frontier[height] = data.get(height, index ^ 1)?;
28            }
29            height += 1;
30            index >>= 1;
31        }
32
33        Ok(LocalExitTree::from_parts(
34            leaf_count
35                .try_into()
36                .map_err(|_| LocalExitTreeError::LeafIndexOverflow)?,
37            frontier,
38        ))
39    }
40}