feat: allow building account code with ScriptBuilder#2142
Conversation
| // CONVERSIONS | ||
| // ================================================================================================ | ||
|
|
||
| impl TryFrom<&Package> for AccountComponentMetadata { |
There was a problem hiding this comment.
Moved this since it seems better placed here
681d2ef to
91fc33f
Compare
| let library = Assembler::default().assemble_library([CODE]).unwrap(); | ||
| let component = AccountComponent::new(library, vec![]).unwrap().with_supports_all_types(); |
There was a problem hiding this comment.
I removed AccountComponent::compile, but because these tests live in miden-objects we can't get around using Assembler
9074789 to
ed37996
Compare
e5fb980 to
d6601f6
Compare
PhilippGackstatter
left a comment
There was a problem hiding this comment.
Looks good to me!
I think the main comments I have are around:
- Naming
ProtocolAssembler - Whether inserting (mock) libraries in
TxContextis necessary - Renaming
compile_*methods toparse_* - Removing
TransactionKernel::with*testing APIs.
| .unwrap() | ||
| .with_supports_all_types(); | ||
| let account_code = | ||
| ProtocolAssembler::with_kernel_library(Arc::new(DefaultSourceManager::default())) |
There was a problem hiding this comment.
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.
| #[cfg(any(feature = "testing", test))] | ||
| pub fn with_kernel_library(source_manager: Arc<dyn SourceManagerSync>) -> Self { |
There was a problem hiding this comment.
Nit: I would basically add docs which are mostly a copy of TransactionKernel::with_kernel_library (or link to it?)
| 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()); |
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
I think this was just a change I made while the refactors were still in progress and which I did not revisit later. Removed
There was a problem hiding this comment.
I think these are still present in the latest version.
| use crate::transaction::TransactionKernel; | ||
|
|
||
| // SCRIPT BUILDER |
There was a problem hiding this comment.
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_libraryis needed inCodeExecutor::runto assemble aProgram.TransactionKernel::with_mock_librariesis needed inTransactionContext::execute_codeto assemble aProgram.
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.
bobbinth
left a comment
There was a problem hiding this comment.
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.
PhilippGackstatter
left a comment
There was a problem hiding this comment.
Looks great!
As for using compile or parse, my preference is slightly on the latter, but happy to go with the majority.
| 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()); |
There was a problem hiding this comment.
I think these are still present in the latest version.
| let assembler = assembler.with_debug_mode(true); | ||
|
|
||
| let program = assembler | ||
| .with_debug_mode(true) |
There was a problem hiding this comment.
Nit: We seem to enable debug mode twice.
|
I tried to resolve the merge conflict - but seems like it broke things (I think there are some new tests that use the old |
Fixed! |
|
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. |
Closes #1756
AccountComponentCode, wrapper ofLibraryAccountComponentCodewithScriptBuilderTransactionKernel::assembler(). Some of them were not doable for different reasons, though mainly in tests (assemblingProgram, tests located inmiden-objects, etc.)ScriptBuildertoCodeBuilderI thinkProtocolAssembleris 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.