Skip to content

Commit

Permalink
Return execution costs
Browse files Browse the repository at this point in the history
  • Loading branch information
vicsn committed Jan 15, 2024
1 parent 6d0d4c9 commit 7b3c9bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
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
17 changes: 15 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 {

This comment has been minimized.

Copy link
@iamalwaysuncomfortable

iamalwaysuncomfortable Jan 15, 2024

Contributor

Looks good! Just needs accessor methods :)!

storage_cost: u64,
finalize_cost: u64,
execution_cost: u64,
}

/// 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 @@ -52,7 +60,10 @@ pub fn deployment_cost<N: Network>(deployment: &Deployment<N>) -> Result<(u64, (
}

/// Returns the *minimum* cost in microcredits to publish the given execution (total cost, (storage cost, namespace cost)).
pub fn execution_cost<N: Network, C: ConsensusStorage<N>>(vm: &VM<N, C>, execution: &Execution<N>) -> Result<u64> {
pub fn execution_cost<N: Network, C: ConsensusStorage<N>>(
vm: &VM<N, C>,
execution: &Execution<N>,
) -> Result<(u64, ExecutionCosts)> {
// Compute the storage cost in microcredits.
let storage_cost = execution.size_in_bytes()?;

Expand Down Expand Up @@ -95,7 +106,9 @@ pub fn execution_cost<N: Network, C: ConsensusStorage<N>>(vm: &VM<N, C>, executi
.and_then(|x| x.checked_add(execution_cost))
.ok_or(anyhow!("The total cost computation overflowed for an execution"))?;

Ok(total_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
2 changes: 1 addition & 1 deletion synthesizer/src/vm/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
// If the fee is required, then check that the base fee amount is satisfied.
if is_fee_required {
// Compute the execution cost.
let cost = execution_cost(self, execution)?;
let (cost, _) = execution_cost(self, execution)?;
// Ensure the fee is sufficient to cover the cost.
if *fee.base_amount()? < cost {
bail!(
Expand Down

0 comments on commit 7b3c9bc

Please sign in to comment.