Skip to content

feat: allow building account code with ScriptBuilder#2142

Merged
bobbinth merged 12 commits intonextfrom
igamigo-acc-code-builder
Dec 13, 2025
Merged

feat: allow building account code with ScriptBuilder#2142
bobbinth merged 12 commits intonextfrom
igamigo-acc-code-builder

Conversation

@igamigo
Copy link
Copy Markdown
Collaborator

@igamigo igamigo commented Dec 5, 2025

Closes #1756

  • Adds AccountComponentCode, wrapper of Library
  • Adds capability of building AccountComponentCode with ScriptBuilder
  • Attempts to remove all occurrences of TransactionKernel::assembler(). Some of them were not doable for different reasons, though mainly in tests (assembling Program, tests located in miden-objects, etc.)
  • Does minor refactors (move code that was out of place)
  • Renames ScriptBuilder to CodeBuilder

I think ProtocolAssembler is the most correct of all the alternatives that were discussed throughout the issues. However, I realize this might not be ideal so I'm more than open to changing this.

// CONVERSIONS
// ================================================================================================

impl TryFrom<&Package> for AccountComponentMetadata {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Moved this since it seems better placed here

@igamigo igamigo force-pushed the igamigo-acc-code-builder branch 2 times, most recently from 681d2ef to 91fc33f Compare December 7, 2025 19:32
Comment on lines +527 to +528
let library = Assembler::default().assemble_library([CODE]).unwrap();
let component = AccountComponent::new(library, vec![]).unwrap().with_supports_all_types();
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I removed AccountComponent::compile, but because these tests live in miden-objects we can't get around using Assembler

@igamigo igamigo force-pushed the igamigo-acc-code-builder branch 2 times, most recently from 9074789 to ed37996 Compare December 9, 2025 17:39
@igamigo igamigo force-pushed the igamigo-acc-code-builder branch from e5fb980 to d6601f6 Compare December 10, 2025 18:51
@igamigo igamigo marked this pull request as ready for review December 10, 2025 18:53
Copy link
Copy Markdown
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Looks good to me!

I think the main comments I have are around:

  • Naming ProtocolAssembler
  • Whether inserting (mock) libraries in TxContext is necessary
  • Renaming compile_* methods to parse_*
  • Removing TransactionKernel::with* testing APIs.

.unwrap()
.with_supports_all_types();
let account_code =
ProtocolAssembler::with_kernel_library(Arc::new(DefaultSourceManager::default()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know you just kept the previous functionality, but this should work with just ProtocolAssembler::default since the kernel library is never accessed in this module.

The same should largely be true for test_fpi.rs where we typically construct actual transactions that cannot access the $kernel library, and so we should be able to just use default. Does not apply whenever $kernel is used.

Comment on lines +345 to +346
#[cfg(any(feature = "testing", test))]
pub fn with_kernel_library(source_manager: Arc<dyn SourceManagerSync>) -> Self {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: I would basically add docs which are mostly a copy of TransactionKernel::with_kernel_library (or link to it?)

Comment on lines 143 to 149
self.mast_store.insert(TransactionKernel::library().mast_forest().clone());
self.mast_store.insert(miden_lib::StdLibrary::default().mast_forest().clone());
self.mast_store.insert(miden_lib::MidenLib::default().mast_forest().clone());
self.mast_store
.insert(AccountCode::mock_account_library().mast_forest().clone());
self.mast_store.insert(AccountCode::mock_faucet_library().mast_forest().clone());
self.mast_store.insert(program.mast_forest().clone());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why did this work without the stdlib and miden lib before?

Do we really have to insert the mock account and faucet libraries? These should only be relevant when executed against a mock account, in which case the account's code itself will bring these libraries and so no dynamic lookup should be required:

https://github.com/0xMiden/miden-base/blob/a5b4b5bf65ee5153a7412873914e6d8b4f04288b/crates/miden-lib/src/testing/account_component/mock_account_component.rs#L55-L61

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think this was just a change I made while the refactors were still in progress and which I did not revisit later. Removed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think these are still present in the latest version.

use crate::transaction::TransactionKernel;

// SCRIPT BUILDER
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we should also move TransactionKernel::mock_libraries here? I think we only had it there because it was the best place at the time, but they are not very related to the tx kernel. I think ProtocolAssembler::mock_libraries would make a bit more sense.

Related to this, it would be really nice if we could get rid of TransactionKernel::{with_kernel_library,with_mock_libraries} and replace all of those with ProtocolAssembler. Basically to not have to maintain a "transaction kernel assembler" even for testing purposes and force ourselves to use the ProtocolAssembler.

  • TransactionKernel::with_kernel_library is needed in CodeExecutor::run to assemble a Program.
  • TransactionKernel::with_mock_libraries is needed in TransactionContext::execute_code to assemble a Program.

I think we can either do this by constructing an Assembler and add the libraries a bit more manually, or by adding a From<ProtocolAssembler> for Assembler and then do ProtocolAssembler::with*, convert into Assembler and assemble the Programs for the above use cases. I would try the latter.

Copy link
Copy Markdown
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! Not a super deep review, but I left a few small comments inline.

Also, I do actually think that compile_ was better than parse_ as we are taking textual representation of code and transforming it into a binary representation. But not a strong preference, and I'm fine keeping it as is.

Copy link
Copy Markdown
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Looks great!

As for using compile or parse, my preference is slightly on the latter, but happy to go with the majority.

Comment on lines 143 to 149
self.mast_store.insert(TransactionKernel::library().mast_forest().clone());
self.mast_store.insert(miden_lib::StdLibrary::default().mast_forest().clone());
self.mast_store.insert(miden_lib::MidenLib::default().mast_forest().clone());
self.mast_store
.insert(AccountCode::mock_account_library().mast_forest().clone());
self.mast_store.insert(AccountCode::mock_faucet_library().mast_forest().clone());
self.mast_store.insert(program.mast_forest().clone());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think these are still present in the latest version.

Comment on lines 135 to 138
let assembler = assembler.with_debug_mode(true);

let program = assembler
.with_debug_mode(true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: We seem to enable debug mode twice.

@bobbinth
Copy link
Copy Markdown
Contributor

I tried to resolve the merge conflict - but seems like it broke things (I think there are some new tests that use the old ScriptBuilder). I can fix it later tonight unless @igamigo you'll get to it first.

@igamigo
Copy link
Copy Markdown
Collaborator Author

igamigo commented Dec 12, 2025

I tried to resolve the merge conflict - but seems like it broke things (I think there are some new tests that use the old ScriptBuilder). I can fix it later tonight unless @igamigo you'll get to it first.

Fixed!

@bobbinth bobbinth merged commit 7ffb33a into next Dec 13, 2025
17 checks passed
@bobbinth bobbinth deleted the igamigo-acc-code-builder branch December 13, 2025 17:25
@bobbinth
Copy link
Copy Markdown
Contributor

I've merged this PR, but I'm not 100% all of @PhilippGackstatter comments have been addressed. @igamigo - could you double check? And if anything slipped through the cracks, we can address that in a follow-up PR.

@igamigo
Copy link
Copy Markdown
Collaborator Author

igamigo commented Dec 13, 2025

I've merged this PR, but I'm not 100% all of @PhilippGackstatter comments have been addressed. @igamigo - could you double check? And if anything slipped through the cracks, we can address that in a follow-up PR.

Opened #2175. I believe everything else was addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable ScriptBuilder to assemble account component libraries

4 participants