deploy working

This commit is contained in:
evan-schott 2024-06-05 17:40:05 -07:00
parent 8254320846
commit f8cef0f8e8
5 changed files with 75 additions and 43 deletions

8
Cargo.lock generated
View File

@ -1568,6 +1568,7 @@ dependencies = [
"snarkvm", "snarkvm",
"sys-info", "sys-info",
"test_dir", "test_dir",
"text-tables",
"toml 0.8.14", "toml 0.8.14",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -1588,6 +1589,7 @@ dependencies = [
"serde", "serde",
"serial_test", "serial_test",
"snarkvm", "snarkvm",
"text-tables",
"toml 0.8.14", "toml 0.8.14",
"tracing", "tracing",
] ]
@ -3791,6 +3793,12 @@ dependencies = [
"rand", "rand",
] ]
[[package]]
name = "text-tables"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dc41925991e82af3c3e21e25a9aad92e72930af57fbcc4b07867a18d1cd0459"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.61" version = "1.0.61"

View File

@ -61,6 +61,7 @@ ci_skip = [ "leo-compiler/ci_skip" ]
noconfig = [ ] noconfig = [ ]
[dependencies] [dependencies]
text-tables = "0.3.1"
ureq = "2.9.7" ureq = "2.9.7"
[dependencies.leo-ast] [dependencies.leo-ast]

View File

@ -33,6 +33,7 @@ use snarkvm::{
}, },
}; };
use std::{path::PathBuf, str::FromStr}; use std::{path::PathBuf, str::FromStr};
use text_tables;
/// Deploys an Aleo program. /// Deploys an Aleo program.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -115,61 +116,70 @@ fn handle_deploy<A: Aleo<Network = N, BaseField = N::Field>, N: Network>(
// Fetch the package from the directory. // Fetch the package from the directory.
let package = SnarkVMPackage::<N>::open(path)?; let package = SnarkVMPackage::<N>::open(path)?;
println!("📦 Creating deployment transaction for '{}'...\n", &name.bold()); if !command.fee_options.estimate_fee {
println!("📦 Creating deployment transaction for '{}'...\n", &name.bold());
} else {
println!("📦 Estimating deployment cost for '{}'...\n", &name.bold());
}
// Generate the deployment // Generate the deployment
let deployment = package.deploy::<A>(None)?; let deployment = package.deploy::<A>(None)?;
let deployment_id = deployment.to_deployment_id()?; let deployment_id = deployment.to_deployment_id()?;
// Generate the deployment transaction.
let transaction = {
// Initialize an RNG.
let rng = &mut rand::thread_rng();
let store = ConsensusStore::<N, ConsensusMemory<N>>::open(StorageMode::Production)?; let store = ConsensusStore::<N, ConsensusMemory<N>>::open(StorageMode::Production)?;
// Initialize the VM. // Initialize the VM.
let vm = VM::from(store)?; let vm = VM::from(store)?;
// Compute the minimum deployment cost. // Compute the minimum deployment cost.
let (minimum_deployment_cost, _) = deployment_cost(&deployment)?; let (total_cost, (storage_cost, synthesis_cost, namespace_cost)) = deployment_cost(&deployment)?;
if command.fee_options.estimate_fee {
// Use `credit` denomination instead of `microcredit`.
display_cost_breakdown(name, total_cost as f64 / 1_000_000.0, storage_cost as f64 / 1_000_000.0, synthesis_cost as f64 / 1_000_000.0, namespace_cost as f64 / 1_000_000.0);
continue;
}
// Prepare the fees. // Initialize an RNG.
let fee = match &command.fee_options.record { let rng = &mut rand::thread_rng();
Some(record) => {
let fee_record = parse_record(&private_key, record)?; // Prepare the fees.
let fee_authorization = vm.authorize_fee_private( let fee = match &command.fee_options.record {
&private_key, Some(record) => {
fee_record, let fee_record = parse_record(&private_key, record)?;
minimum_deployment_cost, let fee_authorization = vm.authorize_fee_private(
command.fee_options.priority_fee, &private_key,
deployment_id, fee_record,
rng, total_cost,
)?; command.fee_options.priority_fee,
vm.execute_fee_authorization(fee_authorization, Some(query.clone()), rng)? deployment_id,
} rng,
None => { )?;
let fee_authorization = vm.authorize_fee_public( vm.execute_fee_authorization(fee_authorization, Some(query.clone()), rng)?
&private_key, }
minimum_deployment_cost, None => {
command.fee_options.priority_fee, let fee_authorization = vm.authorize_fee_public(
deployment_id, &private_key,
rng, total_cost,
)?; command.fee_options.priority_fee,
vm.execute_fee_authorization(fee_authorization, Some(query.clone()), rng)? deployment_id,
} rng,
}; )?;
// Construct the owner. vm.execute_fee_authorization(fee_authorization, Some(query.clone()), rng)?
let owner = ProgramOwner::new(&private_key, deployment_id, rng)?; }
// Create a new transaction.
Transaction::from_deployment(owner, deployment, fee)?
}; };
// Construct the owner.
let owner = ProgramOwner::new(&private_key, deployment_id, rng)?;
// Generate the deployment transaction.
let transaction = Transaction::from_deployment(owner, deployment, fee)?;
println!("✅ Created deployment transaction for '{}'", name.bold()); println!("✅ Created deployment transaction for '{}'", name.bold());
// Determine if the transaction should be broadcast, stored, or displayed to the user. // Determine if the transaction should be broadcast, stored, or displayed to the user.
handle_broadcast( handle_broadcast(
&format!("{}/{}/transaction/broadcast", command.options.endpoint, command.fee_options.network), &format!("{}/{}/transaction/broadcast", command.options.endpoint, command.options.network),
transaction, transaction,
name, name,
)?; )?;
@ -181,3 +191,13 @@ fn handle_deploy<A: Aleo<Network = N, BaseField = N::Field>, N: Network>(
Ok(()) Ok(())
} }
// A helper function to display a cost breakdown of the deployment.
fn display_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, synthesis_cost: f64, namespace_cost: f64) {
println!("✅ Estimated deployment cost for '{}' is {} credits.", name.bold(), total_cost);
// Display the cost breakdown in a table.
let data = [[name, "Cost (credits)", "Cost reduction tips"], ["Storage", &format!("{:.6}", storage_cost), "Use less instructions"], ["Synthesis", &format!("{:.6}", synthesis_cost), "Remove expensive operations (Ex: SHA3), or unnecessary imports"], ["Namespace", &format!("{:.6}", namespace_cost), "Lengthen the program name (each additional character makes it 10x cheaper)"], ["Total", &format!("{:.6}", total_cost), ""]];
let mut out = Vec::new();
text_tables::render(&mut out, data).unwrap();
println!("{}", ::std::str::from_utf8(&out).unwrap());
}

View File

@ -205,10 +205,10 @@ impl Default for BuildOptions {
/// Used by Execute and Deploy commands. /// Used by Execute and Deploy commands.
#[derive(Parser, Clone, Debug)] #[derive(Parser, Clone, Debug)]
pub struct FeeOptions { pub struct FeeOptions {
#[clap(long, help = "Estimate the deploy or execution fee without broadcasting to the network.", default_value = "false")]
pub(crate) estimate_fee: bool,
#[clap(long, help = "Priority fee in microcredits. Defaults to 0.", default_value = "0")] #[clap(long, help = "Priority fee in microcredits. Defaults to 0.", default_value = "0")]
pub(crate) priority_fee: u64, pub(crate) priority_fee: u64,
#[clap(long, help = "Network to broadcast to. Defaults to mainnet.", default_value = "mainnet")]
pub(crate) network: String,
#[clap(long, help = "Private key to authorize fee expenditure.")] #[clap(long, help = "Private key to authorize fee expenditure.")]
pub(crate) private_key: Option<String>, pub(crate) private_key: Option<String>,
#[clap( #[clap(

View File

@ -47,6 +47,9 @@ features = [ "derive" ]
[dependencies.serial_test] [dependencies.serial_test]
version = "3.1.1" version = "3.1.1"
[dependencies.text-tables]
text-tables = "0.3.1"
[dependencies.toml] [dependencies.toml]
version = "0.8" version = "0.8"