Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fixed execution cost #2294

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions console/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub trait Network:
const DEPLOYMENT_FEE_MULTIPLIER: u64 = 1_000; // 1 millicredit per byte
/// The maximum number of microcredits that can be spent as a fee.
const MAX_FEE: u64 = 1_000_000_000_000_000;
/// The fixed cost in microcredits to verify Executions.
const EXECUTION_FEE: u64 = 25_000; // 25 millicredits

/// The anchor height, defined as the expected number of blocks to reach the coinbase target.
const ANCHOR_HEIGHT: u32 = Self::ANCHOR_TIME as u32 / Self::BLOCK_TIME as u32;
Expand Down
2 changes: 1 addition & 1 deletion synthesizer/src/vm/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
let fee = match is_fee_required || is_priority_fee_declared {
true => {
// Compute the minimum execution cost.
let (minimum_execution_cost, (_, _)) = execution_cost(self, &execution)?;
let (minimum_execution_cost, _) = execution_cost(self, &execution)?;
// Compute the execution ID.
let execution_id = execution.to_execution_id()?;
// Authorize the fee.
Expand Down
18 changes: 16 additions & 2 deletions synthesizer/src/vm/helpers/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ use synthesizer_program::{Command, Finalize, Instruction};

use std::collections::HashMap;

#[allow(unused)]
/// Helper struct for i.a. SDK to return costs for users.
pub struct ExecutionCosts {
pub storage_cost: u64,
pub finalize_cost: u64,
pub execution_cost: u64,
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just needs accessor methods for end users, and perhaps a display method for printing it.

Suggested change
impl ExecutionCosts {
/// Cost for storing the execution transaction on the Aleo Network
pub fn storage_cost() -> u64 {
self.storage_cost
}
/// Cost for executing all operations within the function's finalize scope on the Aleo Network
pub fn finalize_cost() -> u64 {
self.finalize_cost
}
/// Cost for verification of the function execution
pub fn execution_cost() -> u64 {
self.storage_cost
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like a rebel today: cc10cce

/// Returns the *minimum* cost in microcredits to publish the given deployment (total cost, (storage cost, namespace cost)).
pub fn deployment_cost<N: Network>(deployment: &Deployment<N>) -> Result<(u64, (u64, u64))> {
// Determine the number of bytes in the deployment.
Expand Down Expand Up @@ -55,7 +63,7 @@ pub fn deployment_cost<N: Network>(deployment: &Deployment<N>) -> Result<(u64, (
pub fn execution_cost<N: Network, C: ConsensusStorage<N>>(
vm: &VM<N, C>,
execution: &Execution<N>,
) -> Result<(u64, (u64, u64))> {
) -> Result<(u64, ExecutionCosts)> {
// Compute the storage cost in microcredits.
let storage_cost = execution.size_in_bytes()?;

Expand Down Expand Up @@ -89,12 +97,18 @@ pub fn execution_cost<N: Network, C: ConsensusStorage<N>>(
.ok_or(anyhow!("The finalize cost computation overflowed for an execution"))?;
}

// Compute the fixed execution cost in microcredits
let execution_cost = N::EXECUTION_FEE;

// Compute the total cost in microcredits.
let total_cost = storage_cost
.checked_add(finalize_cost)
.and_then(|x| x.checked_add(execution_cost))
.ok_or(anyhow!("The total cost computation overflowed for an execution"))?;

Ok((total_cost, (storage_cost, finalize_cost)))
let execution_costs = ExecutionCosts { storage_cost, execution_cost, finalize_cost };

Ok((total_cost, execution_costs))
}

/// Returns the minimum number of microcredits required to run the finalize.
Expand Down