extern crate alloc;
use miden_protocol::Word;
use miden_protocol::account::{Account, AccountBuilder, AccountType};
use miden_standards::account::inspection::CodeInspection;
use miden_standards::account::wallets::BasicWallet;
use miden_standards::code_builder::CodeBuilder;
use miden_testing::{Auth, MockChain};
fn create_inspectable_account() -> anyhow::Result<Account> {
let account = AccountBuilder::new([1; 32])
.account_type(AccountType::Public)
.with_auth_component(Auth::IncrNonce)
.with_component(BasicWallet)
.with_component(CodeInspection)
.build_existing()?;
Ok(account)
}
async fn run_has_procedure_script(proc_root: Word, body: &str) -> anyhow::Result<()> {
let account = create_inspectable_account()?;
let mut builder = MockChain::builder();
builder.add_account(account.clone())?;
let mock_chain = builder.build()?;
let tx_script_code = format!(
r#"
use miden::standards::components::inspection::code_inspection
@transaction_script
pub proc main
# => [PROC_ROOT, pad(12)]
call.code_inspection::has_procedure
# => [is_procedure_available, pad(15)]
{body}
end
"#
);
let tx_script = CodeBuilder::default()
.with_dynamically_linked_library(CodeInspection::code())?
.compile_tx_script(tx_script_code)?;
mock_chain
.build_tx_context(account.id(), &[], &[])?
.tx_script(tx_script)
.tx_script_args(proc_root)
.build()?
.execute()
.await?;
Ok(())
}
#[tokio::test]
async fn code_inspection_has_procedure_reports_exposed_procedure() -> anyhow::Result<()> {
let exposed_root: Word = *CodeInspection::has_procedure_root().mast_root();
run_has_procedure_script(
exposed_root,
r#"assert.err="has_procedure should report an exposed procedure as available""#,
)
.await?;
Ok(())
}
#[tokio::test]
async fn code_inspection_has_procedure_reports_unknown_root() -> anyhow::Result<()> {
let unknown_root = Word::from([5u32, 3, 15, 686]);
run_has_procedure_script(
unknown_root,
r#"assertz.err="has_procedure should report an unknown root as unavailable""#,
)
.await?;
Ok(())
}