Skip to main content

miden_testing/mock_chain/
note.rs

1use miden_processor::serde::DeserializationError;
2use miden_protocol::note::{Note, NoteId, NoteInclusionProof, NoteMetadata};
3use miden_protocol::transaction::InputNote;
4use miden_tx::utils::serde::{ByteReader, ByteWriter, Deserializable, Serializable};
5
6// MOCK CHAIN NOTE
7// ================================================================================================
8
9/// Represents a note that is stored in the mock chain.
10#[allow(clippy::large_enum_variant)]
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum MockChainNote {
13    /// Details for a private note only include its [`NoteMetadata`] and [`NoteInclusionProof`].
14    /// Other details needed to consume the note are expected to be stored locally, off-chain.
15    Private(NoteId, NoteMetadata, NoteInclusionProof),
16    /// Contains the full [`Note`] object alongside its [`NoteInclusionProof`].
17    Public(Note, NoteInclusionProof),
18}
19
20impl MockChainNote {
21    /// Returns the note's inclusion details.
22    pub fn inclusion_proof(&self) -> &NoteInclusionProof {
23        match self {
24            MockChainNote::Private(_, _, inclusion_proof)
25            | MockChainNote::Public(_, inclusion_proof) => inclusion_proof,
26        }
27    }
28
29    /// Returns the note's metadata.
30    pub fn metadata(&self) -> &NoteMetadata {
31        match self {
32            MockChainNote::Private(_, metadata, _) => metadata,
33            MockChainNote::Public(note, _) => note.metadata(),
34        }
35    }
36
37    /// Returns the note's ID.
38    pub fn id(&self) -> NoteId {
39        match self {
40            MockChainNote::Private(id, ..) => *id,
41            MockChainNote::Public(note, _) => note.id(),
42        }
43    }
44
45    /// Returns the underlying note if it is public.
46    pub fn note(&self) -> Option<&Note> {
47        match self {
48            MockChainNote::Private(..) => None,
49            MockChainNote::Public(note, _) => Some(note),
50        }
51    }
52}
53
54impl TryFrom<MockChainNote> for InputNote {
55    type Error = anyhow::Error;
56
57    fn try_from(value: MockChainNote) -> Result<Self, Self::Error> {
58        match value {
59            MockChainNote::Private(..) => Err(anyhow::anyhow!(
60                "private notes in the mock chain cannot be converted into input notes due to missing details"
61            )),
62            MockChainNote::Public(note, proof) => Ok(InputNote::Authenticated { note, proof }),
63        }
64    }
65}
66
67// SERIALIZATION
68// ================================================================================================
69
70impl Serializable for MockChainNote {
71    fn write_into<W: ByteWriter>(&self, target: &mut W) {
72        match self {
73            MockChainNote::Private(id, metadata, proof) => {
74                0u8.write_into(target);
75                id.write_into(target);
76                metadata.write_into(target);
77                proof.write_into(target);
78            },
79            MockChainNote::Public(note, proof) => {
80                1u8.write_into(target);
81                note.write_into(target);
82                proof.write_into(target);
83            },
84        }
85    }
86}
87
88impl Deserializable for MockChainNote {
89    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
90        let note_type = u8::read_from(source)?;
91        match note_type {
92            0 => {
93                let id = NoteId::read_from(source)?;
94                let metadata = NoteMetadata::read_from(source)?;
95                let proof = NoteInclusionProof::read_from(source)?;
96                Ok(MockChainNote::Private(id, metadata, proof))
97            },
98            1 => {
99                let note = Note::read_from(source)?;
100                let proof = NoteInclusionProof::read_from(source)?;
101                Ok(MockChainNote::Public(note, proof))
102            },
103            _ => Err(DeserializationError::InvalidValue(format!("Unknown note type: {note_type}"))),
104        }
105    }
106}