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 the underlying [`Arc<Package>`]
30    pub fn package(&self) -> Arc<Package> {
31        self.0.clone()
32    }
33
34    /// Returns a reference to the [`MastForest`] of the inner [`Library`].
35    pub fn mast_forest(&self) -> &Arc<MastForest> {
36        self.0.mast_forest()
37    }
38}
39
40impl AsRef<Library> for ProtocolLib {
41    fn as_ref(&self) -> &Library {
42        self.0.as_ref()
43    }
44}
45
46impl From<ProtocolLib> for Library {
47    fn from(value: ProtocolLib) -> Self {
48        Arc::unwrap_or_clone(value.0)
49    }
50}
51
52impl Default for ProtocolLib {
53    fn default() -> Self {
54        ProtocolLib(PROTOCOL_PACKAGE.clone())
55    }
56}
57
58// TESTS
59// ================================================================================================
60
61// NOTE: Most protocol-related tests can be found in miden-testing.
62#[cfg(all(test, feature = "std"))]
63mod tests {
64    use super::ProtocolLib;
65    use crate::assembly::Path;
66
67    #[test]
68    fn test_compile() {
69        let path = Path::new("::miden::protocol::active_account::get_id");
70        let miden = ProtocolLib::default();
71        let exists = miden.0.module_infos().any(|module| {
72            module
73                .procedures()
74                .any(|(_, proc)| module.path().join(&proc.name).as_path() == path)
75        });
76
77        assert!(exists);
78    }
79}