Skip to content

Commit

Permalink
Add an error if withdraw_gas/redeposit contain pre-cost metadata. (#4468
Browse files Browse the repository at this point in the history
)
  • Loading branch information
liorgold2 authored Nov 24, 2023
1 parent 46b323d commit 61b1371
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crates/cairo-lang-sierra-to-casm/src/invocations/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use cairo_lang_casm::casm_build_extend;
use cairo_lang_casm::cell_expression::{CellExpression, CellOperator};
use cairo_lang_casm::operand::{CellRef, DerefOrImmediate, Register};
use cairo_lang_sierra::extensions::gas::{CostTokenType, GasConcreteLibfunc};
use cairo_lang_sierra::program::StatementIdx;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use num_bigint::BigInt;

use super::misc::get_pointer_after_program_code;
Expand All @@ -27,6 +29,19 @@ pub fn build(
}
}

/// Checks that all the pre-cost variables of the given statement are zero.
fn check_zero_precost_values(
variable_values: &OrderedHashMap<(StatementIdx, CostTokenType), i64>,
statement_idx: StatementIdx,
) -> Result<(), InvocationError> {
for token_type in CostTokenType::iter_precost() {
if variable_values.get(&(statement_idx, *token_type)).copied().unwrap_or_default() != 0 {
return Err(InvocationError::PreCostMetadataNotSupported);
}
}
Ok(())
}

/// Handles the withdraw_gas invocation.
fn build_withdraw_gas(
builder: CompiledInvocationBuilder<'_>,
Expand All @@ -36,6 +51,9 @@ fn build_withdraw_gas(
.get(&(builder.idx, CostTokenType::Const))
.copied()
.ok_or(InvocationError::UnknownVariableData)?;

check_zero_precost_values(variable_values, builder.idx)?;

let [range_check, gas_counter] = builder.try_get_single_cells()?;

let failure_handle_statement_id = get_non_fallthrough_statement_id(&builder);
Expand Down Expand Up @@ -86,6 +104,9 @@ fn build_redeposit_gas(
.get(&(builder.idx, CostTokenType::Const))
.copied()
.ok_or(InvocationError::UnknownVariableData)?;

check_zero_precost_values(variable_values, builder.idx)?;

let gas_counter_value = builder.try_get_single_cells::<1>()?[0]
.to_deref()
.ok_or(InvocationError::InvalidReferenceExpressionForArgument)?;
Expand Down
3 changes: 3 additions & 0 deletions crates/cairo-lang-sierra-to-casm/src/invocations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub enum InvocationError {
IntegerOverflow,
#[error(transparent)]
FrameStateError(#[from] FrameStateError),
// TODO(lior): Remove this error once not used.
#[error("This libfunc does not support pre-cost metadata yet.")]
PreCostMetadataNotSupported,
}

/// Describes a simple change in the ap tracking itself.
Expand Down

0 comments on commit 61b1371

Please sign in to comment.