miden_objects/transaction/outputs.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
use alloc::{collections::BTreeSet, string::ToString, vec::Vec};
use core::fmt::Debug;
use miden_crypto::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::DeserializationError;
use crate::{
accounts::AccountHeader,
notes::{compute_note_hash, Note, NoteAssets, NoteHeader, NoteId, NoteMetadata, PartialNote},
Digest, Felt, Hasher, TransactionOutputError, Word, MAX_OUTPUT_NOTES_PER_TX,
};
// TRANSACTION OUTPUTS
// ================================================================================================
/// Describes the result of executing a transaction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionOutputs {
/// Information related to the account's final state.
pub account: AccountHeader,
/// Set of output notes created by the transaction.
pub output_notes: OutputNotes,
/// Defines up to which block the transaction is considered valid.
pub expiration_block_num: u32,
}
// OUTPUT NOTES
// ================================================================================================
/// Contains a list of output notes of a transaction. The list can be empty if the transaction does
/// not produce any notes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutputNotes {
notes: Vec<OutputNote>,
commitment: Digest,
}
impl OutputNotes {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
/// Returns new [OutputNotes] instantiated from the provide vector of notes.
///
/// # Errors
/// Returns an error if:
/// - The total number of notes is greater than 1024.
/// - The vector of notes contains duplicates.
pub fn new(notes: Vec<OutputNote>) -> Result<Self, TransactionOutputError> {
if notes.len() > MAX_OUTPUT_NOTES_PER_TX {
return Err(TransactionOutputError::TooManyOutputNotes(notes.len()));
}
let mut seen_notes = BTreeSet::new();
for note in notes.iter() {
if !seen_notes.insert(note.id()) {
return Err(TransactionOutputError::DuplicateOutputNote(note.id()));
}
}
let commitment = build_output_notes_commitment(¬es);
Ok(Self { notes, commitment })
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Returns the commitment to the output notes.
///
/// The commitment is computed as a sequential hash of (hash, metadata) tuples for the notes
/// created in a transaction.
pub fn commitment(&self) -> Digest {
self.commitment
}
/// Returns total number of output notes.
pub fn num_notes(&self) -> usize {
self.notes.len()
}
/// Returns true if this [OutputNotes] does not contain any notes.
pub fn is_empty(&self) -> bool {
self.notes.is_empty()
}
/// Returns a reference to the note located at the specified index.
pub fn get_note(&self, idx: usize) -> &OutputNote {
&self.notes[idx]
}
// ITERATORS
// --------------------------------------------------------------------------------------------
/// Returns an iterator over notes in this [OutputNotes].
pub fn iter(&self) -> impl Iterator<Item = &OutputNote> {
self.notes.iter()
}
}
// SERIALIZATION
// ------------------------------------------------------------------------------------------------
impl Serializable for OutputNotes {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
// assert is OK here because we enforce max number of notes in the constructor
assert!(self.notes.len() <= u16::MAX.into());
target.write_u16(self.notes.len() as u16);
target.write_many(&self.notes);
}
}
impl Deserializable for OutputNotes {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let num_notes = source.read_u16()?;
let notes = source.read_many::<OutputNote>(num_notes.into())?;
Self::new(notes).map_err(|err| DeserializationError::InvalidValue(err.to_string()))
}
}
// HELPER FUNCTIONS
// ------------------------------------------------------------------------------------------------
/// Build a commitment to output notes.
///
/// For a non-empty list of notes, this is a sequential hash of (note_id, metadata) tuples for the
/// notes created in a transaction. For an empty list, [EMPTY_WORD] is returned.
fn build_output_notes_commitment(notes: &[OutputNote]) -> Digest {
if notes.is_empty() {
return Digest::default();
}
let mut elements: Vec<Felt> = Vec::with_capacity(notes.len() * 8);
for note in notes.iter() {
elements.extend_from_slice(note.id().as_elements());
elements.extend_from_slice(&Word::from(note.metadata()));
}
Hasher::hash_elements(&elements)
}
// OUTPUT NOTE
// ================================================================================================
const FULL: u8 = 0;
const PARTIAL: u8 = 1;
const HEADER: u8 = 2;
/// The types of note outputs supported by the transaction kernel.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OutputNote {
Full(Note),
Partial(PartialNote),
Header(NoteHeader),
}
impl OutputNote {
/// The assets contained in the note.
pub fn assets(&self) -> Option<&NoteAssets> {
match self {
OutputNote::Full(note) => Some(note.assets()),
OutputNote::Partial(note) => Some(note.assets()),
OutputNote::Header(_) => None,
}
}
/// Unique note identifier.
///
/// This value is both an unique identifier and a commitment to the note.
pub fn id(&self) -> NoteId {
match self {
OutputNote::Full(note) => note.id(),
OutputNote::Partial(note) => note.id(),
OutputNote::Header(note) => note.id(),
}
}
/// Value that represents under which condition a note can be consumed.
///
/// See [crate::notes::NoteRecipient] for more details.
pub fn recipient_digest(&self) -> Option<Digest> {
match self {
OutputNote::Full(note) => Some(note.recipient().digest()),
OutputNote::Partial(note) => Some(note.recipient_digest()),
OutputNote::Header(_) => None,
}
}
/// Note's metadata.
pub fn metadata(&self) -> &NoteMetadata {
match self {
OutputNote::Full(note) => note.metadata(),
OutputNote::Partial(note) => note.metadata(),
OutputNote::Header(note) => note.metadata(),
}
}
/// Erase private note information.
///
/// Specifically:
/// - Full private notes are converted into note headers.
/// - All partial notes are converted into note headers.
pub fn shrink(&self) -> Self {
match self {
OutputNote::Full(note) if note.metadata().is_private() => {
OutputNote::Header(*note.header())
},
OutputNote::Partial(note) => OutputNote::Header(note.into()),
_ => self.clone(),
}
}
/// Returns a commitment to the note and its metadata.
///
/// > hash(NOTE_ID || NOTE_METADATA)
pub fn hash(&self) -> Digest {
compute_note_hash(self.id(), self.metadata())
}
}
// CONVERSIONS
// ------------------------------------------------------------------------------------------------
impl From<OutputNote> for NoteHeader {
fn from(value: OutputNote) -> Self {
(&value).into()
}
}
impl From<&OutputNote> for NoteHeader {
fn from(value: &OutputNote) -> Self {
match value {
OutputNote::Full(note) => note.into(),
OutputNote::Partial(note) => note.into(),
OutputNote::Header(note) => *note,
}
}
}
// SERIALIZATION
// ------------------------------------------------------------------------------------------------
impl Serializable for OutputNote {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
match self {
OutputNote::Full(note) => {
target.write(FULL);
target.write(note);
},
OutputNote::Partial(note) => {
target.write(PARTIAL);
target.write(note);
},
OutputNote::Header(note) => {
target.write(HEADER);
target.write(note);
},
}
}
}
impl Deserializable for OutputNote {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
match source.read_u8()? {
FULL => Ok(OutputNote::Full(Note::read_from(source)?)),
PARTIAL => Ok(OutputNote::Partial(PartialNote::read_from(source)?)),
HEADER => Ok(OutputNote::Header(NoteHeader::read_from(source)?)),
v => Err(DeserializationError::InvalidValue(format!("Invalid note type: {v}"))),
}
}
}