Skip to main content

miden_testing/mock_chain/
note.rs

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