Skip to main content

miden_protocol/
protocol.rs

1use alloc::sync::Arc;
2
3use crate::assembly::Library;
4use crate::assembly::mast::MastForest;
5use crate::utils::sync::LazyLock;
6use crate::vm::Package;
7
8// CONSTANTS
9// ================================================================================================
10
11const PROTOCOL_PACKAGE_BYTES: &[u8] =
12    include_bytes!(concat!(env!("OUT_DIR"), "/assets/miden-protocol.masp"));
13
14static PROTOCOL_PACKAGE: LazyLock<Arc<Package>> = LazyLock::new(|| {
15    Arc::new(
16        // These bytes are produced by this crate's build script and embedded in the binary.
17        Package::read_from_bytes_trusted(PROTOCOL_PACKAGE_BYTES)
18            .expect("protocol lib masp should be well-formed"),
19    )
20});
21
22// PROTOCOL LIBRARY
23// ================================================================================================
24
25#[derive(Clone)]
26pub struct ProtocolLib(Arc<Package>);
27
28impl ProtocolLib {
29    /// Returns a reference to the [`MastForest`] of the inner [`Library`].
30    pub fn mast_forest(&self) -> &Arc<MastForest> {
31        self.0.mast_forest()
32    }
33}
34
35impl AsRef<Library> for ProtocolLib {
36    fn as_ref(&self) -> &Library {
37        self.0.as_ref()
38    }
39}
40
41impl From<ProtocolLib> for Library {
42    fn from(value: ProtocolLib) -> Self {
43        Arc::unwrap_or_clone(value.0)
44    }
45}
46
47impl Default for ProtocolLib {
48    fn default() -> Self {
49        ProtocolLib(PROTOCOL_PACKAGE.clone())
50    }
51}
52
53// TESTS
54// ================================================================================================
55
56// NOTE: Most protocol-related tests can be found in miden-testing.
57#[cfg(all(test, feature = "std"))]
58mod tests {
59    use super::ProtocolLib;
60    use crate::assembly::Path;
61
62    #[test]
63    fn test_compile() {
64        let path = Path::new("::miden::protocol::active_account::get_id");
65        let miden = ProtocolLib::default();
66        let exists = miden.0.module_infos().any(|module| {
67            module
68                .procedures()
69                .any(|(_, proc)| module.path().join(&proc.name).as_path() == path)
70        });
71
72        assert!(exists);
73    }
74}