Skip to content

Commit

Permalink
test: Add tests, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
igamigo committed Nov 13, 2024
1 parent 31b1d7a commit 8cf400d
Show file tree
Hide file tree
Showing 32 changed files with 1,159 additions and 1,635 deletions.
5 changes: 3 additions & 2 deletions bin/tx-prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod test {
transaction::{ProvenTransaction, TransactionScript, TransactionWitness},
};
use miden_tx::{
testing::mock_chain::{Auth, MockChain},
testing::{Auth, MockChain},
utils::Serializable,
};
use miden_tx_prover::generated::{
Expand Down Expand Up @@ -81,14 +81,15 @@ mod test {
account.id(),
&[fungible_asset_1],
NoteType::Private,
None,
)
.unwrap();

let tx_script =
TransactionScript::compile(DEFAULT_AUTH_SCRIPT, vec![], TransactionKernel::assembler())
.unwrap();
let tx_context = mock_chain
.build_tx_context(account.id())
.build_tx_context(account.id(), &[], &[])
.input_notes(vec![note_1])
.tx_script(tx_script)
.build();
Expand Down
70 changes: 30 additions & 40 deletions miden-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ fn main() -> Result<()> {
)?;

// compile account components
compile_account_components(
&source_dir.join(ASM_ACCOUNT_COMPONENTS_DIR),
&target_dir.join(ASM_ACCOUNT_COMPONENTS_DIR),
assembler,
)?;
compile_account_components(&target_dir.join(ASM_ACCOUNT_COMPONENTS_DIR), assembler)?;

Ok(())
}
Expand All @@ -87,7 +83,7 @@ fn main() -> Result<()> {
/// - {source_dir}/main.masm -> defines the executable program of the transaction kernel.
/// - {source_dir}/lib -> contains common modules used by both api.masm and main.masm.
///
/// The compiled files are written as follows:
/// The complied files are written as follows:
///
/// - {target_dir}/tx_kernel.masl -> contains kernel library compiled from api.masm.
/// - {target_dir}/tx_kernel.masb -> contains the executable compiled from main.masm.
Expand Down Expand Up @@ -261,7 +257,7 @@ fn parse_proc_offsets(filename: impl AsRef<Path>) -> Result<BTreeMap<String, usi
// ================================================================================================

/// Reads the MASM files from "{source_dir}/miden" directory, compiles them into a Miden assembly
/// library, saves the library into "{target_dir}/miden.masl", and returns the compiled library.
/// library, saves the library into "{target_dir}/miden.masl", and returns the complied library.
fn compile_miden_lib(
source_dir: &Path,
target_dir: &Path,
Expand All @@ -281,8 +277,8 @@ fn compile_miden_lib(
// COMPILE EXECUTABLE MODULES
// ================================================================================================

/// Reads all MASM files from the "{source_dir}", compiles each file individually into a MASB
/// file, and stores the compiled files into the "{target_dir}".
/// Reads all MASM files from the "{source_dir}", complies each file individually into a MASB
/// file, and stores the complied files into the "{target_dir}".
///
/// The source files are expected to contain executable programs.
fn compile_note_scripts(source_dir: &Path, target_dir: &Path, assembler: Assembler) -> Result<()> {
Expand All @@ -307,41 +303,35 @@ fn compile_note_scripts(source_dir: &Path, target_dir: &Path, assembler: Assembl
Ok(())
}

// COMPILE ACCOUNT COMPONENTS
// COMPILE DEFAULT ACCOUNT COMPONENTS
// ================================================================================================

/// Compiles the account components in `source_dir` into MASL libraries and stores the compiled
/// files in `target_dir`.
fn compile_account_components(
source_dir: &Path,
target_dir: &Path,
assembler: Assembler,
) -> Result<()> {
if !target_dir.exists() {
fs::create_dir_all(target_dir).unwrap();
}

for masm_file_path in get_masm_files(source_dir).unwrap() {
let component_name = masm_file_path
.file_stem()
.expect("masm file should have a file stem")
.to_str()
.expect("file stem should be valid UTF-8")
.to_owned();

// Read the source code to string instead of passing it to assemble_library directly since
// that would attempt to interpret the path as a LibraryPath which would fail.
let component_source_code = fs::read_to_string(masm_file_path)
.expect("reading the component's MASM source code should succeed");

let component_library = assembler
.clone()
.assemble_library([component_source_code])
.expect("library assembly should succeed");

const BASIC_WALLET_CODE: &str = "
export.::miden::contracts::wallets::basic::receive_asset
export.::miden::contracts::wallets::basic::create_note
export.::miden::contracts::wallets::basic::move_asset_to_note
";

const RPO_FALCON_AUTH_CODE: &str = "
export.::miden::contracts::auth::basic::auth_tx_rpo_falcon512
";

const BASIC_FUNGIBLE_FAUCET_CODE: &str = "
export.::miden::contracts::faucets::basic_fungible::distribute
export.::miden::contracts::faucets::basic_fungible::burn
";

/// Compiles the default account components into a MASL library and stores the complied files in
/// `target_dir`.
fn compile_account_components(target_dir: &Path, assembler: Assembler) -> Result<()> {
for (component_name, component_code) in [
("basic_wallet", BASIC_WALLET_CODE),
("rpo_falcon_512", RPO_FALCON_AUTH_CODE),
("basic_fungible_faucet", BASIC_FUNGIBLE_FAUCET_CODE),
] {
let component_library = assembler.clone().assemble_library([component_code])?;
let component_file_path =
target_dir.join(component_name).with_extension(Library::LIBRARY_EXTENSION);

component_library.write_to_file(component_file_path).into_diagnostic()?;
}

Expand Down
7 changes: 1 addition & 6 deletions miden-lib/src/accounts/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ use crate::accounts::components::rpo_falcon_512_library;
/// An [`AccountComponent`] implementing the RpoFalcon512 signature scheme for authentication of
/// transactions.
///
/// It reexports the procedures from `miden::contracts::auth::basic`. When linking against this
/// component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be available to the
/// assembler which is the case when using [`TransactionKernel::assembler()`][kasm]. The procedures
/// of this component are:
/// Its exported procedures are:
/// - `auth_tx_rpo_falcon512`, which can be used to verify a signature provided via the advice stack
/// to authenticate a transaction.
///
/// This component supports all account types.
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct RpoFalcon512 {
public_key: PublicKey,
}
Expand Down
7 changes: 1 addition & 6 deletions miden-lib/src/accounts/faucets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_fungible_faucet_libr

/// An [`AccountComponent`] implementing a basic fungible faucet.
///
/// It reexports the procedures from `miden::contracts::faucets::basic_fungible`. When linking
/// against this component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be
/// available to the assembler which is the case when using
/// [`TransactionKernel::assembler()`][kasm]. The procedures of this component are:
/// Its exported procedures are:
/// - `distribute`, which mints an assets and create a note for the provided recipient.
/// - `burn`, which burns the provided asset.
///
Expand All @@ -28,8 +25,6 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_fungible_faucet_libr
/// authentication.
///
/// This component supports accounts of type [`AccountType::FungibleFaucet`].
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct BasicFungibleFaucet {
symbol: TokenSymbol,
decimals: u8,
Expand Down
7 changes: 1 addition & 6 deletions miden-lib/src/accounts/wallets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_wallet_library};

/// An [`AccountComponent`] implementing a basic wallet.
///
/// It reexports the procedures from `miden::contracts::wallets::basic`. When linking against this
/// component, the `miden` library (i.e. [`MidenLib`](crate::MidenLib)) must be available to the
/// assembler which is the case when using [`TransactionKernel::assembler()`][kasm]. The procedures
/// of this component are:
/// Its exported procedures are:
/// - `receive_asset`, which can be used to add an asset to the account.
/// - `create_note`, which can be used to create a new note without any assets attached to it.
/// - `move_asset_to_note`, which can be used to remove the specified asset from the account and add
Expand All @@ -26,8 +23,6 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_wallet_library};
/// providing authentication.
///
/// This component supports all account types.
///
/// [kasm]: crate::transaction::TransactionKernel::assembler
pub struct BasicWallet;

impl From<BasicWallet> for AccountComponent {
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use tx_progress::TransactionProgress;

use crate::{
auth::TransactionAuthenticator, errors::TransactionHostError, executor::TransactionMastStore,
TX_KERNEL_ERRORS,
tx_kernel_errors::TX_KERNEL_ERRORS,
};

// TRANSACTION HOST
Expand Down
5 changes: 1 addition & 4 deletions miden-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ mod verifier;
pub use verifier::TransactionVerifier;

mod errors;
pub use errors::{
tx_kernel_errors::TX_KERNEL_ERRORS, AuthenticationError, DataStoreError,
TransactionExecutorError, TransactionProverError, TransactionVerifierError,
};
pub use errors::*;

pub mod auth;

Expand Down
2 changes: 1 addition & 1 deletion miden-tx/src/testing/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct CodeExecutor<H> {
impl<H: Host> CodeExecutor<H> {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
pub fn new(host: H) -> Self {
pub(crate) fn new(host: H) -> Self {
Self {
host,
stack_inputs: None,
Expand Down
Loading

0 comments on commit 8cf400d

Please sign in to comment.