use aleo_rust_sdk::{ExecutionEngine, FixedStateRootQuery};
use snarkvm::prelude::{ConsensusVersion, Network, PrivateKey, Program, TestRng, TestnetV0};
use std::str::FromStr;
#[test]
fn test_execution_engine_v0_fee_keys() {
let engine = ExecutionEngine::new_with_v0_fee_keys().unwrap();
let guard = engine.process.lock();
let ids = guard.program_ids();
assert!(!ids.is_empty());
drop(guard);
}
#[test]
fn test_execution_engine_add_program() {
let engine = ExecutionEngine::new().unwrap();
let credits = Program::<TestnetV0>::credits().unwrap();
let r = engine.add_program(&credits);
assert!(r.is_ok());
}
#[test]
fn test_execution_engine_inner_with_credits() {
let engine = ExecutionEngine::new().unwrap();
let process_ref = engine.inner();
let guard = process_ref.lock();
assert!(!guard.program_ids().is_empty());
}
const HELLO_PROGRAM: &str = r"
program hello_deploy_test.aleo;
function hello:
input r0 as u32.public;
output r0 as u32.public;
";
#[test]
fn test_deploy_program_generates_deployment() {
let engine = ExecutionEngine::new_with_v0_fee_keys().unwrap();
let program = Program::<TestnetV0>::from_str(HELLO_PROGRAM).unwrap();
engine.add_program(&program).unwrap();
let mut rng = TestRng::default();
let deployment = engine.deploy_program(&program, &mut rng).unwrap();
assert_eq!(
deployment.program().id().to_string(),
"hello_deploy_test.aleo"
);
assert!(
!deployment.program().functions().is_empty(),
"deployed program must have at least one function"
);
}
#[test]
fn test_deployment_cost_minimum_computed() {
let engine = ExecutionEngine::new_with_v0_fee_keys().unwrap();
let program = Program::<TestnetV0>::from_str(HELLO_PROGRAM).unwrap();
engine.add_program(&program).unwrap();
let mut rng = TestRng::default();
let deployment = engine.deploy_program(&program, &mut rng).unwrap();
let min_cost = engine.deployment_cost_minimum(&deployment, ConsensusVersion::V14).unwrap();
assert!(min_cost > 0, "deployment cost must be > 0, got {min_cost}");
println!(
"Minimum deployment cost: {min_cost} microcredits ({:.2} credits)",
min_cost as f64 / 1_000_000.0
);
}
#[test]
fn test_build_deployment_transaction_local() {
let engine = ExecutionEngine::new_with_v0_fee_keys().unwrap();
let program = Program::<TestnetV0>::from_str(HELLO_PROGRAM).unwrap();
engine.add_program(&program).unwrap();
let mut rng = TestRng::default();
let private_key = PrivateKey::new(&mut rng).unwrap();
let deployment = engine.deploy_program(&program, &mut rng).unwrap();
let min_cost = engine.deployment_cost_minimum(&deployment, ConsensusVersion::V14).unwrap();
let dummy_sr = <TestnetV0 as Network>::StateRoot::from_str(
"sr1lkr8fzg8mk69qrtycxjvtrg8rvh77puaq7fm56cjkh04xhprdqzq3a355s",
)
.unwrap();
let query = FixedStateRootQuery {
state_root: dummy_sr,
block_height: 42,
};
let tx = engine
.build_deployment_transaction(
&private_key,
&program,
&deployment,
min_cost,
0,
ConsensusVersion::V14,
&query,
&mut rng,
)
.unwrap();
assert!(tx.is_deploy(), "expected a deployment transaction");
let inner_deployment = tx.deployment().unwrap();
assert_eq!(
inner_deployment.program().id().to_string(),
"hello_deploy_test.aleo",
"deployment program ID mismatch"
);
println!("Deployment transaction built successfully");
}