Skip to content

Commit 37d178c

Browse files
authored
Merge pull request #3324 from eranrund/compute-spend-helper
Extract transaction compute spend helper
2 parents 733ae9b + 454e81b commit 37d178c

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

synthesizer/process/src/cost.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,27 @@ pub fn execute_compute_cost_in_microcredits(
212212
}
213213
}
214214

215+
/// Returns the compute spend for a transaction in microcredits.
216+
/// This is used to limit the amount of single-threaded compute in block generation and finalization hot paths.
217+
/// This does NOT represent the full cost which a user has to pay.
218+
pub fn transaction_compute_spend_in_microcredits<N: Network>(
219+
process: &Process<N>,
220+
transaction: &Transaction<N>,
221+
consensus_version: ConsensusVersion,
222+
) -> Result<u64> {
223+
match transaction {
224+
Transaction::Deploy(_, _, _, deployment, _) => {
225+
let (_, cost_details) = deployment_cost(process, deployment, consensus_version)?;
226+
Ok(deploy_compute_cost_in_microcredits(cost_details, consensus_version))
227+
}
228+
Transaction::Execute(_, _, execution, _) => {
229+
let (_, cost_details) = execution_cost(process, execution, consensus_version)?;
230+
Ok(execute_compute_cost_in_microcredits(cost_details, consensus_version))
231+
}
232+
Transaction::Fee(id, _) => bail!("Fee transaction '{id}' does not have deployment or execution spend"),
233+
}
234+
}
235+
215236
/// Returns the minimum cost in microcredits to publish the given deployment (V4).
216237
///
217238
/// Identical to V3 except in that it replaces the factor (`num_combined_variables` + `num_combined_constraints`)

synthesizer/src/vm/finalize.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,26 +1103,11 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
11031103
ShouldAbortResult::Finalize(0)
11041104
// If the consensus version is >= V16, ensure that the transaction is not exceeding spend or deployment limits.
11051105
} else {
1106-
// Compute microcredit spend from deployment or execution cost details.
1107-
let compute_spend = match transaction {
1108-
Transaction::Deploy(_, _, _, deployment, _) => {
1109-
match deployment_cost(self.process(), deployment, consensus_version) {
1110-
Ok((_, cost_details)) => deploy_compute_cost_in_microcredits(cost_details, consensus_version),
1111-
Err(e) => {
1112-
return ShouldAbortResult::Abort(format!("Failed to compute the deployment cost: {e}"));
1113-
}
1114-
}
1115-
}
1116-
Transaction::Execute(_, _, execution, _) => {
1117-
match execution_cost(self.process(), execution, consensus_version) {
1118-
Ok((_, cost_details)) => execute_compute_cost_in_microcredits(cost_details, consensus_version),
1119-
Err(e) => {
1120-
return ShouldAbortResult::Abort(format!("Failed to compute the execution cost: {e}"));
1121-
}
1122-
}
1123-
}
1124-
Transaction::Fee(..) => 0, // Fee transactions are already aborted above and don't contribute compute spend.
1125-
};
1106+
let compute_spend =
1107+
match transaction_compute_spend_in_microcredits(self.process(), transaction, consensus_version) {
1108+
Ok(compute_spend) => compute_spend,
1109+
Err(e) => return ShouldAbortResult::Abort(format!("Failed to compute transaction spend: {e}")),
1110+
};
11261111

11271112
if compute_spend > transaction_spend_limit {
11281113
return ShouldAbortResult::Abort(format!(

synthesizer/src/vm/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ use snarkvm_synthesizer_process::{
8686
deployment_cost,
8787
execute_compute_cost_in_microcredits,
8888
execution_cost,
89+
transaction_compute_spend_in_microcredits,
8990
};
9091
use snarkvm_synthesizer_program::{
9192
FinalizeCore,

0 commit comments

Comments
 (0)