Skip to content

Commit

Permalink
Select fee based on consensus height
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Dec 9, 2024
1 parent 2f49d3b commit 16d71a8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
2 changes: 1 addition & 1 deletion leo/cli/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn handle_deploy<A: Aleo<Network = N, BaseField = N::Field>, N: Network>(
}
None => {
// Make sure the user has enough public balance to pay for the deployment.
check_balance(&private_key, endpoint, &network.to_string(), context.clone(), total_cost)?;
check_balance(&private_key, endpoint, &network.to_string(), &context, total_cost)?;
let fee_authorization = vm.authorize_fee_public(
&private_key,
total_cost,
Expand Down
27 changes: 24 additions & 3 deletions leo/cli/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use snarkvm::{
VM,
Value,
execution_cost_v1,
execution_cost_v2,
query::Query as SnarkVMQuery,
store::{
ConsensusStore,
Expand Down Expand Up @@ -213,8 +214,28 @@ fn handle_execute<A: Aleo>(
// Check the transaction cost.
let (mut total_cost, (storage_cost, finalize_cost)) = if let ExecuteTransaction(_, execution, _) = &transaction
{
// TODO: Update to V2 after block migration.
execution_cost_v1(&vm.process().read(), execution)?
// Attempt to get the height of the latest block to determine which version of the execution cost to use.
if let Ok(height) = get_latest_block_height(endpoint, &network.to_string(), &context) {
if height < A::Network::CONSENSUS_V2_HEIGHT {
execution_cost_v1(&vm.process().read(), execution)?
} else {
execution_cost_v2(&vm.process().read(), execution)?
}
}
// Otherwise, default to the one provided in `fee_options`.
else {
println!(
"Failed to get the latest block height. Defaulting to V{}.",
command.fee_options.consensus_version
);
match command.fee_options.consensus_version {
1 => execution_cost_v1(&vm.process().read(), execution)?,
2 => execution_cost_v2(&vm.process().read(), execution)?,
version => {
panic!("Invalid consensus version: {version}. Please specify a valid version.")
}
}
}
} else {
panic!("All transactions should be of type Execute.")
};
Expand All @@ -231,7 +252,7 @@ fn handle_execute<A: Aleo>(

// Check if the public balance is sufficient.
if fee_record.is_none() {
check_balance::<A::Network>(&private_key, endpoint, &network.to_string(), context, total_cost)?;
check_balance::<A::Network>(&private_key, endpoint, &network.to_string(), &context, total_cost)?;
}

// Broadcast the execution transaction.
Expand Down
30 changes: 28 additions & 2 deletions leo/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ pub struct FeeOptions {
long
)]
record: Option<String>,
#[clap(long, help = "Consensus version to use for the transaction.", default_value = "2")]
pub(crate) consensus_version: u8,
}

/// Parses the record string. If the string is a ciphertext, then attempt to decrypt it. Lifted from snarkOS.
Expand All @@ -241,7 +243,7 @@ fn check_balance<N: Network>(
private_key: &PrivateKey<N>,
endpoint: &str,
network: &str,
context: Context,
context: &Context,
total_cost: u64,
) -> Result<()> {
// Derive the account address.
Expand All @@ -251,7 +253,7 @@ fn check_balance<N: Network>(
endpoint: Some(endpoint.to_string()),
network: Some(network.to_string()),
command: QueryCommands::Program {
command: crate::cli::commands::query::LeoProgram {
command: query::LeoProgram {
name: "credits".to_string(),
mappings: false,
mapping_value: Some(vec!["account".to_string(), address.to_string()]),
Expand All @@ -276,6 +278,30 @@ fn check_balance<N: Network>(
}
}

// A helper function to query for the latest block height.
fn get_latest_block_height(endpoint: &str, network: &str, context: &Context) -> Result<u32> {
// Query the latest block height.
let height = LeoQuery {
endpoint: Some(endpoint.to_string()),
network: Some(network.to_string()),
command: QueryCommands::Block {
command: query::LeoBlock {
id: None,
latest: false,
latest_hash: false,
latest_height: true,
range: None,
transactions: false,
to_height: false,
},
},
}
.execute(Context::new(context.path.clone(), context.home.clone(), true)?)?;
// Parse the height.
let height = height.parse::<u32>().map_err(CliError::string_parse_error)?;
Ok(height)
}

/// Determine if the transaction should be broadcast or displayed to user.
fn handle_broadcast<N: Network>(endpoint: &String, transaction: Transaction<N>, operation: &String) -> Result<()> {
println!("Broadcasting transaction to {}...\n", endpoint.clone());
Expand Down
24 changes: 12 additions & 12 deletions leo/cli/commands/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@
use super::*;
use snarkvm::prelude::{CanaryV0, MainnetV0, TestnetV0};

mod block;
use block::LeoBlock;
pub mod block;
pub use block::LeoBlock;

pub mod program;
pub use program::LeoProgram;

mod state_root;
use state_root::StateRoot;
pub mod state_root;
pub use state_root::StateRoot;

mod committee;
use committee::LeoCommittee;
pub mod committee;
pub use committee::LeoCommittee;

mod mempool;
use mempool::LeoMempool;
pub mod mempool;
pub use mempool::LeoMempool;

mod peers;
use peers::LeoPeers;
pub mod peers;
pub use peers::LeoPeers;

mod transaction;
use transaction::LeoTransaction;
pub mod transaction;
pub use transaction::LeoTransaction;

mod utils;
use utils::*;
Expand Down

0 comments on commit 16d71a8

Please sign in to comment.