Skip to content

Commit

Permalink
mock: add proven tx builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto committed Feb 19, 2024
1 parent bfc4019 commit 357c06d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ env_logger = { version = "0.11" }
hex = "0.4"
miden-lib = { path = "../miden-lib" }
miden-objects = { path = "../objects" , features = ["serde", "log", "testing"]}
miden-prover = { package = "miden-prover", git = "https://github.com/0xPolygonMiden/miden-vm", branch = "next", default-features = false }
miden-test-utils = { package = "miden-test-utils", git = "https://github.com/0xPolygonMiden/miden-vm", branch = "next", default-features = false }
postcard = { version = "1.0", features = [ "alloc" ] }
rand = { version = "0.8" }
Expand Down
2 changes: 2 additions & 0 deletions mock/src/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod error;
mod fungible_asset;
mod nonfungible_asset;
mod note;
mod tx;

// RE-EXPORTS
// ================================================================================================
Expand All @@ -19,6 +20,7 @@ pub use error::AccountBuilderError;
pub use fungible_asset::FungibleAssetBuilder;
pub use nonfungible_asset::{NonFungibleAssetBuilder, NonFungibleAssetDetailsBuilder};
pub use note::NoteBuilder;
pub use tx::ProvenTransactionBuilder;

pub fn str_to_account_code(source: &str) -> Result<AccountCode, AccountError> {
let assembler = TransactionKernel::assembler();
Expand Down
68 changes: 68 additions & 0 deletions mock/src/builders/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use miden_objects::{
accounts::AccountId,
notes::{NoteEnvelope, Nullifier},
transaction::{InputNotes, OutputNotes, ProvenTransaction, ToEnvelope, ToNullifier},
vm::ExecutionProof,
Digest,
};
use miden_prover::{HashFunction, StarkProof};

/// Builder for an `ProvenTransaction`, the builder allows for a fluent API to construct an account.
/// Each transaction needs a unique builder.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ProvenTransactionBuilder {
account_id: AccountId,
note_envelopes: Vec<NoteEnvelope>,
nullifiers: Vec<Nullifier>,
initial_account_hash: Digest,
final_account_hash: Digest,
}

impl ProvenTransactionBuilder {
pub fn new(
account_id: AccountId,
initial_account_hash: Digest,
final_account_hash: Digest,
) -> Self {
Self {
account_id,
initial_account_hash,
final_account_hash,
note_envelopes: Vec::new(),
nullifiers: Vec::new(),
}
}

pub fn add_note_envelope<I: ToEnvelope>(mut self, note_envelope: I) -> Self {
self.note_envelopes.push(note_envelope.to_envelope());
self
}
pub fn add_note_envelopes<I: IntoIterator<Item = NoteEnvelope>>(
mut self,
note_envelopes: I,
) -> Self {
for note_envelope in note_envelopes.into_iter() {
self.note_envelopes.push(note_envelope.to_envelope());
}
self
}

pub fn add_nullifier<I: ToNullifier>(mut self, nullifier: I) -> Self {
self.nullifiers.push(nullifier.nullifier());
self
}

pub fn build(self) -> ProvenTransaction {
ProvenTransaction::new(
self.account_id,
self.initial_account_hash,
self.final_account_hash,
InputNotes::new(self.nullifiers).unwrap(),
OutputNotes::new(self.note_envelopes).unwrap(),
None,
Digest::default(),
ExecutionProof::new(StarkProof::new_dummy(), HashFunction::Blake3_192),
)
}
}
1 change: 1 addition & 0 deletions objects/src/notes/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::{
/// - num assets
/// - ZERO
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct NoteEnvelope {
note_id: NoteId,
note_metadata: NoteMetadata,
Expand Down
1 change: 1 addition & 0 deletions objects/src/notes/note_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::utils::{
/// - To compute a note ID, we do not need to know the note's serial_num. Knowing the hash
/// of the serial_num (as well as script hash, input hash, and note assets) is sufficient.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct NoteId(Digest);

impl NoteId {
Expand Down
1 change: 1 addition & 0 deletions objects/src/notes/nullifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::utils::{hex_to_bytes, HexParseError};
/// - To compute the nullifier we must know all components of the note: serial_num, script_hash,
/// input_hash and asset_hash.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Nullifier(Digest);

impl Nullifier {
Expand Down
4 changes: 2 additions & 2 deletions objects/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mod tx_witness;

pub use chain_mmr::ChainMmr;
pub use executed_tx::ExecutedTransaction;
pub use inputs::{InputNote, InputNotes, TransactionInputs};
pub use outputs::{OutputNote, OutputNotes, TransactionOutputs};
pub use inputs::{InputNote, InputNotes, ToNullifier, TransactionInputs};
pub use outputs::{OutputNote, OutputNotes, ToEnvelope, TransactionOutputs};
pub use prepared_tx::PreparedTransaction;
pub use proven_tx::ProvenTransaction;
pub use transaction_id::TransactionId;
Expand Down
4 changes: 4 additions & 0 deletions objects/src/transaction/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub trait ToEnvelope:
{
fn id(&self) -> NoteId;
fn metadata(&self) -> NoteMetadata;

fn to_envelope(&self) -> NoteEnvelope {
NoteEnvelope::new(self.id(), self.metadata())
}
}

impl ToEnvelope for OutputNote {
Expand Down

0 comments on commit 357c06d

Please sign in to comment.