Skip to main content

miden_protocol/
protocol.rs

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