Skip to content

Prefix standard account components with miden::standards #2399

@PhilippGackstatter

Description

@PhilippGackstatter

We should make the library paths of standard account components consistent with the recent recategorisation into miden::protocol and miden::standards. For example, the basic fungible faucet's library path is currently:

basic_fungible_faucet::distribute

We should prefix that with miden::standards and preserve the auth, faucets, ... directories in the name. To avoid conflicts between account components of the same name and existing modules in standards (e.g. miden::auth::ecdsa_k256_keccak) we can add an account_components in the path. Overall, the basic fungible faucet's library path would then be:

miden::standards::account_components::faucets::basic_fungible_faucet

After #2373, each component has a name. Ideally this would be exactly that library path, so that overall we could address this issue and do the following for each standard account component:

impl BasicFungibleFaucet {
    pub const NAME: &str = "miden::standards::account_components::faucets::basic_fungible_faucet";

    const DISTRIBUTE_PROC_NAME: &str = "distribute";
    const BURN_PROC_NAME: &str = "burn";
}

Then:

  • We should be able to update the procedure_digest! macro to derive the full path for the distribute procedure from NAME and DISTRIBUTE_PROC_NAME.
  • We can use BasicFungibleFaucet::NAME for AccountComponentMetadata::new.
  • AccountComponent::new can enforce that the metadata name is a prefix of the library path defined in the library in AccountComponentCode. This way, we get components that have consistent names and library paths.

Here is an attempt at the miden-standards build.rs changes to derive the above path:

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 shared::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();

        let mut component_path =
            masm_file_path.strip_prefix(source_dir).into_diagnostic()?.to_path_buf();
        // Remove .masm extension.
        component_path.set_extension("");

        let mut absolute_component_path =
            PathBuf::new(STANDARDS_LIB_NAMESPACE).into_diagnostic()?;
        absolute_component_path.push(ASM_ACCOUNT_COMPONENTS_DIR);

        // Add each component to the absolute path to convert / separators to :: separators.
        for component in component_path.components() {
            absolute_component_path.push(component.as_os_str().to_str().expect("TODO"));
        }

        let component_source_code = fs::read_to_string(&masm_file_path)
            .expect("reading the component's MASM source code should succeed");

        let named_source =
            NamedSource::new(absolute_component_path.as_str(), component_source_code);

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

        // Preserve the subdirectory structure: compute relative path from source_dir
        let relative_dir = masm_file_path
            .parent()
            .and_then(|p| p.strip_prefix(source_dir).ok())
            .unwrap_or(Path::new(""));

        let output_dir = target_dir.join(relative_dir);
        if !output_dir.exists() {
            fs::create_dir_all(&output_dir).unwrap();
        }

        let component_file_path =
            output_dir.join(component_name).with_extension(Library::LIBRARY_EXTENSION);
        component_library.write_to_file(component_file_path).into_diagnostic()?;
    }

    Ok(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersstandardsRelated to standard note scripts or account components

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions