Skip to content

Commit

Permalink
objects: add account/note details to ProvenTransaction #403
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto committed Feb 26, 2024
1 parent 6290afb commit 51dce75
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
13 changes: 13 additions & 0 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ pub const NOTE_LEAF_DEPTH: u8 = NOTE_TREE_DEPTH + 1;
// NOTE
// ================================================================================================

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NoteType {
/// Notes with this type have only their hash published to the network.
OffChain,

/// Notes with this type are fully known by the network, but are intended to be used with local
/// proofs
PublicNote,

/// Notes with this type are fully known by the network, and are intended for network execution
PublicNetworkNote,
}

/// A note which can be used to transfer assets between accounts.
///
/// This struct is a full description of a note which is needed to execute a note in a transaction.
Expand Down
63 changes: 61 additions & 2 deletions objects/src/transaction/proven_tx.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
use miden_verifier::ExecutionProof;

use super::{AccountId, Digest, InputNotes, NoteEnvelope, Nullifier, OutputNotes, TransactionId};
use crate::utils::serde::{
ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
use crate::{
accounts::{Account, AccountDelta},
notes::Note,
utils::{
collections::*,
serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
},
};

// PROVEN TRANSACTION
// ================================================================================================

#[derive(Clone, Debug)]
pub enum AccountDetails {
/// The whole state is needed for new accounts
Full(Account),

/// For existing accounts, only the delta is needed.
Delta(AccountDelta),
}

/// The result of executing and proving a transaction.
///
/// This struct contains all the data required to verify that a transaction was executed correctly.
Expand All @@ -27,8 +41,10 @@ pub struct ProvenTransaction {
account_id: AccountId,
initial_account_hash: Digest,
final_account_hash: Digest,
account_details: Option<AccountDetails>,
input_notes: InputNotes<Nullifier>,
output_notes: OutputNotes<NoteEnvelope>,
output_note_details: BTreeMap<usize, Note>,
tx_script_root: Option<Digest>,
block_ref: Digest,
proof: ExecutionProof,
Expand Down Expand Up @@ -62,8 +78,10 @@ impl ProvenTransaction {
account_id,
initial_account_hash,
final_account_hash,
account_details: None,
input_notes,
output_notes,
output_note_details: BTreeMap::new(),
tx_script_root,
block_ref,
proof,
Expand Down Expand Up @@ -122,13 +140,47 @@ impl ProvenTransaction {
// SERIALIZATION
// ================================================================================================

impl Serializable for AccountDetails {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
match self {
AccountDetails::Full(account) => {
0u8.write_into(target);
account.write_into(target);
},
AccountDetails::Delta(delta) => {
1u8.write_into(target);
delta.write_into(target);
},
}
}
}

impl Deserializable for AccountDetails {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
match u8::read_from(source)? {
0u8 => Ok(Self::Full(Account::read_from(source)?)),
1u8 => Ok(Self::Delta(AccountDelta::read_from(source)?)),
v => Err(DeserializationError::InvalidValue(format!(

Check failure on line 163 in objects/src/transaction/proven_tx.rs

View workflow job for this annotation

GitHub Actions / build stable no-std for wasm32-unknown-unknown

cannot find macro `format` in this scope

Check failure on line 163 in objects/src/transaction/proven_tx.rs

View workflow job for this annotation

GitHub Actions / build nightly no-std for wasm32-unknown-unknown

cannot find macro `format` in this scope
"Unknown variant {v} for AccountDetails"
))),
}
}
}

impl Serializable for ProvenTransaction {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.account_id.write_into(target);
self.initial_account_hash.write_into(target);
self.final_account_hash.write_into(target);
self.account_details.write_into(target);
self.input_notes.write_into(target);
self.output_notes.write_into(target);

target.write_usize(self.output_note_details.len());
let elements: Vec<_> =
self.output_note_details.iter().map(|(k, v)| (*k, v.clone())).collect();
target.write_many(&elements);

self.tx_script_root.write_into(target);
self.block_ref.write_into(target);
self.proof.write_into(target);
Expand All @@ -140,10 +192,15 @@ impl Deserializable for ProvenTransaction {
let account_id = AccountId::read_from(source)?;
let initial_account_hash = Digest::read_from(source)?;
let final_account_hash = Digest::read_from(source)?;
let account_details = <Option<AccountDetails>>::read_from(source)?;

let input_notes = InputNotes::<Nullifier>::read_from(source)?;
let output_notes = OutputNotes::<NoteEnvelope>::read_from(source)?;

let output_notes_details_len = usize::read_from(source)?;
let details = source.read_many(output_notes_details_len)?;
let output_note_details = BTreeMap::from_iter(details);

let tx_script_root = Deserializable::read_from(source)?;

let block_ref = Digest::read_from(source)?;
Expand All @@ -161,8 +218,10 @@ impl Deserializable for ProvenTransaction {
account_id,
initial_account_hash,
final_account_hash,
account_details,
input_notes,
output_notes,
output_note_details,
tx_script_root,
block_ref,
proof,
Expand Down

0 comments on commit 51dce75

Please sign in to comment.