Skip to content

Commit

Permalink
Merge pull request #399 from 0xPolygonMiden/dominik_move_kernel_api_t…
Browse files Browse the repository at this point in the history
…o_kernels

refactor: moving kernel api to kernels
  • Loading branch information
bobbinth authored Jan 5, 2024
2 parents eeef30b + e6b95ea commit 5390a2c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
File renamed without changes.
File renamed without changes.
38 changes: 33 additions & 5 deletions miden-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ASSETS_DIR: &str = "assets";
const ASM_DIR: &str = "asm";
const ASM_MIDEN_DIR: &str = "miden";
const ASM_NOTE_SCRIPTS_DIR: &str = "note_scripts";
const ASM_KERNELS_DIR: &str = "kernels";
const ASM_KERNELS_DIR: &str = "kernels/transaction";

// PRE-PROCESSING
// ================================================================================================
Expand Down Expand Up @@ -48,8 +48,11 @@ fn main() -> io::Result<()> {
compile_miden_lib(&source_dir, &target_dir)?;

// compile kernel and note scripts
compile_executable_modules(&source_dir.join(ASM_KERNELS_DIR), &target_dir)?;
compile_executable_modules(&source_dir.join(ASM_NOTE_SCRIPTS_DIR), &target_dir)?;
compile_kernels(&source_dir.join(ASM_KERNELS_DIR), &target_dir.join("kernels"))?;
compile_note_scripts(
&source_dir.join(ASM_NOTE_SCRIPTS_DIR),
&target_dir.join(ASM_NOTE_SCRIPTS_DIR),
)?;

Ok(())
}
Expand Down Expand Up @@ -111,7 +114,11 @@ fn decrease_pow(line: io::Result<String>) -> io::Result<String> {
// COMPILE EXECUTABLE MODULES
// ================================================================================================

fn compile_executable_modules(source_dir: &Path, target_dir: &Path) -> io::Result<()> {
fn compile_note_scripts(source_dir: &Path, target_dir: &Path) -> io::Result<()> {
if let Err(e) = fs::create_dir_all(target_dir) {
println!("Failed to create note_scripts directory: {}", e);
}

for masm_file_path in get_masm_files(source_dir)? {
// read the MASM file, parse it, and serialize the parsed AST to bytes
let ast = ProgramAst::parse(&fs::read_to_string(masm_file_path.clone())?)?;
Expand All @@ -121,10 +128,31 @@ fn compile_executable_modules(source_dir: &Path, target_dir: &Path) -> io::Resul
let masb_file_name = masm_file_path.file_name().unwrap().to_str().unwrap();
let mut masb_file_path = target_dir.join(masb_file_name);

// write the binary MASM to the output dir
// write the binary MASB to the output dir
masb_file_path.set_extension("masb");
fs::write(masb_file_path, bytes)?;
}
Ok(())
}

// COMPILE KERNELS
// ================================================================================================

fn compile_kernels(source_dir: &Path, target_dir: &Path) -> io::Result<()> {
// read the MASM file, parse it, and serialize the parsed AST to bytes
let ast = ProgramAst::parse(&fs::read_to_string(source_dir.join("main.masm").clone())?)?;
let bytes = ast.to_bytes(AstSerdeOptions { serialize_imports: true });

// create the output file path
let masb_file_name = "transaction";
let mut masb_file_path = target_dir.join(masb_file_name);
if let Err(e) = fs::create_dir_all(masb_file_path.clone()) {
println!("Failed to create kernels directory: {}", e);
}

// write the binary MASB to the output dir
masb_file_path.set_extension("masb");
fs::write(masb_file_path.clone(), bytes)?;

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions miden-lib/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub fn create_note(
let note_assembler = TransactionKernel::assembler();

// Include the binary version of the scripts into the source file at compile time
let p2id_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/P2ID.masb"));
let p2idr_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/P2IDR.masb"));
let swap_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/SWAP.masb"));
let p2id_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2ID.masb"));
let p2idr_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2IDR.masb"));
let swap_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/SWAP.masb"));

let (note_script_ast, inputs): (ProgramAst, Vec<Felt>) = match script {
Script::P2ID { target } => (
Expand Down Expand Up @@ -79,7 +79,7 @@ fn build_p2id_recipient(target: AccountId, serial_num: Word) -> Result<Digest, N
// the script hash every time we call the SWAP script
let assembler = TransactionKernel::assembler();

let p2id_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/P2ID.masb"));
let p2id_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2ID.masb"));

let note_script_ast =
ProgramAst::from_bytes(p2id_bytes).map_err(NoteError::NoteDeserializationError)?;
Expand Down
5 changes: 3 additions & 2 deletions miden-lib/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ impl TransactionKernel {

/// Returns MASM source code which encodes the transaction kernel system procedures.
pub fn kernel() -> &'static str {
include_str!("../../asm/miden/kernels/tx/mod.masm")
include_str!("../../asm/kernels/transaction/api.masm")
}

/// Returns an AST of the transaction kernel executable program.
///
/// # Errors
/// Returns an error if deserialization of the binary fails.
pub fn main() -> Result<ProgramAst, DeserializationError> {
let kernel_bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/transaction.masb"));
let kernel_bytes =
include_bytes!(concat!(env!("OUT_DIR"), "/assets/kernels/transaction.masb"));
ProgramAst::from_bytes(kernel_bytes)
}

Expand Down

0 comments on commit 5390a2c

Please sign in to comment.