Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock: add proven tx builder #474

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ lto = true
assembly = { package = "miden-assembly", version = "0.8", default-features = false }
miden-verifier = { package = "miden-verifier", version = "0.8", default-features = false }
vm-processor = { package = "miden-processor", version = "0.8", default-features = false }
miden-prover = { package = "miden-prover", version = "0.8", default-features = false }
2 changes: 1 addition & 1 deletion miden-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ std = ["miden-lib/std", "miden-objects/std", "miden-prover/std", "miden-verifier
[dependencies]
miden-lib = { package = "miden-lib", path = "../miden-lib", default-features = false }
miden-objects = { package = "miden-objects", path = "../objects", default-features = false }
miden-prover = { package = "miden-prover", version = "0.8", default-features = false }
miden-prover = { workspace = true }
miden-verifier = { workspace = true }
vm-processor = { workspace = true }

Expand Down
5 changes: 3 additions & 2 deletions mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ std = ["miden-lib/std", "miden-objects/std"]
[dependencies]
clap = { version = "4.4", features = ["derive"], optional = true }
env_logger = { version = "0.11" }
hex = "0.4"
hex = { version = "0.4" }
miden-lib = { path = "../miden-lib" }
miden-objects = { path = "../objects" , features = ["serde", "log", "testing"] }
miden-prover = { workspace = true }
postcard = { version = "1.0", features = [ "alloc" ] }
rand = { version = "0.8" }
rand-utils = { package = "winter-rand-utils", version = "0.8" }
rand_pcg = { version = "0.3", features = ["serde1"] }
serde = { version = "1.0", optional = true, default-features = false, features = ["derive"] }
vm-processor = { workspace = true, features = ["internals"] }
rand-utils = { package = "winter-rand-utils", version = "0.8" }
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this meant to replace MockProvenTxBuilder in miden-node?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is possible, I would think it is a good idea. The rationale is that having fewer utilities doing the same is best, and the utilities in miden-base can be used on the other projects, like miden-client.

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 @@ -18,6 +18,7 @@ use crate::utils::collections::*;
/// - 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, string::*, 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())
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToEnvelope requires PartialEq, so I couldn't use trait objects.
The OutputNotes<T> struct is parametrized, and ProvenTransaction requires it to be OutputNotes<NoteEnvelope>. So I can't the generic OutputNotes<T> constructor.

}

impl ToEnvelope for OutputNote {
Expand Down
Loading