Skip to content

Commit

Permalink
chore: Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Nov 11, 2024
1 parent bb9ca40 commit 005e79e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 67 deletions.
33 changes: 10 additions & 23 deletions miden-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::{
};

use assembly::{
ast::{Module, ModuleKind},
diagnostics::{IntoDiagnostic, Result},
utils::Serializable,
Assembler, DefaultSourceManager, KernelLibrary, Library, LibraryNamespace,
Expand Down Expand Up @@ -88,7 +87,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 complied files are written as follows:
/// The compiled 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 @@ -262,7 +261,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 complied library.
/// library, saves the library into "{target_dir}/miden.masl", and returns the compiled library.
fn compile_miden_lib(
source_dir: &Path,
target_dir: &Path,
Expand All @@ -283,7 +282,7 @@ fn compile_miden_lib(
// ================================================================================================

/// Reads all MASM files from the "{source_dir}", compiles each file individually into a MASB
/// file, and stores the complied files into the "{target_dir}".
/// file, and stores the compiled 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 Down Expand Up @@ -311,12 +310,8 @@ fn compile_note_scripts(source_dir: &Path, target_dir: &Path, assembler: Assembl
// COMPILE ACCOUNT COMPONENTS
// ================================================================================================

/// Compiles the account components into a MASL library and stores the complied files in
/// `target_dir`. Those libraries have `account_components` as their namespace and the name of the
/// file as the module name.
///
/// For example, a `basic_wallet.masm` file could be imported as
/// `use.account_components::basic_wallet`.
/// 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,
Expand All @@ -334,22 +329,14 @@ fn compile_account_components(
.expect("file stem should be valid UTF-8")
.to_owned();

// Parse the module as a library with the given path so we can make all components available
// under the same library namespace without putting them in the same library.
// This is why we're not using Library::from_dir here because it would do that.
let component_module = Module::parser(ModuleKind::Library)
.parse_file(
format!("account_components::{component_name}")
.parse()
.expect("library path should be valid"),
&masm_file_path,
&DefaultSourceManager::default(),
)
.expect("module parsing should succeed");
// 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_module])
.assemble_library([component_source_code])
.expect("library assembly should succeed");

let component_file_path =
Expand Down
7 changes: 6 additions & 1 deletion miden-lib/src/accounts/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ use crate::accounts::components::rpo_falcon_512_library;
/// An [`AccountComponent`] implementing the RpoFalcon512 signature scheme for authentication of
/// transactions.
///
/// Its exported procedures are:
/// 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:
/// - `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
47 changes: 6 additions & 41 deletions miden-lib/src/accounts/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,16 @@ static BASIC_FUNGIBLE_FAUCET_LIBRARY: LazyLock<Library> = LazyLock::new(|| {
});

/// Returns the Basic Wallet Library.
pub fn basic_wallet_library() -> &'static Library {
BASIC_WALLET_LIBRARY.as_ref()
pub fn basic_wallet_library() -> Library {
BASIC_WALLET_LIBRARY.clone()
}

/// Returns the Rpo Falcon 512 Library.
pub fn rpo_falcon_512_library() -> &'static Library {
RPO_FALCON_512_LIBRARY.as_ref()
pub fn rpo_falcon_512_library() -> Library {
RPO_FALCON_512_LIBRARY.clone()
}

/// Returns the Basic Fungible Faucet Library.
pub fn basic_fungible_faucet_library() -> &'static Library {
BASIC_FUNGIBLE_FAUCET_LIBRARY.as_ref()
}

#[cfg(test)]
mod tests {
use miden_objects::assembly::Assembler;

use super::*;

/// Test that the account component libraries can be used to link against from other MASM code
/// and in particular that they are all available under the same library namespace
/// ("account_components").
#[test]
fn test_account_component_libraries_can_be_linked() {
let source = r#"
use.account_components::basic_wallet
use.account_components::rpo_falcon_512
use.account_components::basic_fungible_faucet
begin
call.basic_wallet::receive_asset
call.rpo_falcon_512::auth_tx_rpo_falcon512
call.basic_fungible_faucet::distribute
end
"#;

Assembler::default()
.with_library(basic_wallet_library())
.unwrap()
.with_library(rpo_falcon_512_library())
.unwrap()
.with_library(basic_fungible_faucet_library())
.unwrap()
.assemble_program(source)
.expect("we should be able to link against the account component libraries");
}
pub fn basic_fungible_faucet_library() -> Library {
BASIC_FUNGIBLE_FAUCET_LIBRARY.clone()
}
7 changes: 6 additions & 1 deletion miden-lib/src/accounts/faucets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_fungible_faucet_libr

/// An [`AccountComponent`] implementing a basic fungible faucet.
///
/// Its exported procedures are:
/// 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:
/// - `distribute`, which mints an assets and create a note for the provided recipient.
/// - `burn`, which burns the provided asset.
///
Expand All @@ -25,6 +28,8 @@ 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: 6 additions & 1 deletion miden-lib/src/accounts/wallets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use crate::accounts::{auth::RpoFalcon512, components::basic_wallet_library};

/// An [`AccountComponent`] implementing a basic wallet.
///
/// Its exported procedures are:
/// 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:
/// - `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 @@ -23,6 +26,8 @@ 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

0 comments on commit 005e79e

Please sign in to comment.