Skip to main content

aztec_core/abi/
storage_layout.rs

1use serde::{Deserialize, Serialize};
2
3use crate::types::Fr;
4
5/// Describes the storage slot layout for a single contract storage field.
6#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
7pub struct FieldLayout {
8    /// Slot number in the contract's storage tree.
9    pub slot: Fr,
10}
11
12/// Maps storage field names to their slot layout descriptors.
13///
14/// Used for off-chain reads and storage proofs.
15pub type ContractStorageLayout = std::collections::BTreeMap<String, FieldLayout>;
16
17#[cfg(test)]
18#[allow(clippy::unwrap_used)]
19mod tests {
20    use super::*;
21
22    #[test]
23    fn field_layout_roundtrip() {
24        let layout = FieldLayout {
25            slot: Fr::from(42u64),
26        };
27        let json = serde_json::to_string(&layout).unwrap();
28        let decoded: FieldLayout = serde_json::from_str(&json).unwrap();
29        assert_eq!(decoded, layout);
30    }
31
32    #[test]
33    fn contract_storage_layout_insert_and_lookup() {
34        let mut storage = ContractStorageLayout::new();
35        storage.insert(
36            "balances".to_owned(),
37            FieldLayout {
38                slot: Fr::from(1u64),
39            },
40        );
41        storage.insert(
42            "total_supply".to_owned(),
43            FieldLayout {
44                slot: Fr::from(2u64),
45            },
46        );
47
48        assert_eq!(storage.len(), 2);
49        assert_eq!(storage["balances"].slot, Fr::from(1u64));
50        assert_eq!(storage["total_supply"].slot, Fr::from(2u64));
51    }
52}