Skip to main content

pessimistic_proof/
local_state.rs

1use agglayer_primitives::Digest;
2use agglayer_tries::roots::{LocalBalanceRoot, LocalExitRoot, LocalNullifierRoot};
3use serde::{Deserialize, Serialize};
4use unified_bridge::LocalExitTree;
5
6use crate::{local_balance_tree::LocalBalanceTree, nullifier_tree::NullifierTree};
7
8/// State representation of one network without the leaves, taken as input by
9/// the prover.
10#[derive(Clone, Debug, Serialize, Deserialize, Default)]
11pub struct LocalNetworkState {
12    /// Commitment to the [`BridgeExit`](struct@crate::bridge_exit::BridgeExit).
13    pub exit_tree: LocalExitTree,
14    /// Commitment to the balance for each token.
15    pub balance_tree: LocalBalanceTree,
16    /// Commitment to the Nullifier tree for the local network, tracks claimed
17    /// assets on foreign networks
18    pub nullifier_tree: NullifierTree,
19}
20
21impl From<LocalNetworkState> for pessimistic_proof_core::NetworkState {
22    fn from(state: LocalNetworkState) -> Self {
23        pessimistic_proof_core::NetworkState {
24            exit_tree: state.exit_tree,
25            balance_tree: state.balance_tree.into(),
26            nullifier_tree: state.nullifier_tree.into(),
27        }
28    }
29}
30
31/// The roots of one [`LocalNetworkState`].
32#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
33pub struct StateCommitment {
34    pub exit_root: Digest,
35    pub ler_leaf_count: u32,
36    pub balance_root: Digest,
37    pub nullifier_root: Digest,
38}
39
40impl StateCommitment {
41    pub fn display_to_hex(&self) -> String {
42        format!(
43            "exit_root: {}, ler_leaf_count: {}, balance_root: {}, nullifier_root: {}",
44            self.exit_root, self.ler_leaf_count, self.balance_root, self.nullifier_root,
45        )
46    }
47}
48
49impl From<StateCommitment> for pessimistic_proof_core::local_state::commitment::StateCommitment {
50    fn from(commitment: StateCommitment) -> Self {
51        Self {
52            exit_root: LocalExitRoot::new(commitment.exit_root),
53            ler_leaf_count: commitment.ler_leaf_count,
54            balance_root: LocalBalanceRoot::new(commitment.balance_root),
55            nullifier_root: LocalNullifierRoot::new(commitment.nullifier_root),
56        }
57    }
58}