Skip to content

Commit

Permalink
Refactor errors into variants
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianGCalderon committed Sep 25, 2024
1 parent 7bb8dd3 commit ae0f943
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
54 changes: 39 additions & 15 deletions vm/src/types/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::types::layout_name::LayoutName;

use super::instance_definitions::{
builtins_instance_def::BuiltinsInstanceDef, diluted_pool_instance_def::DilutedPoolInstanceDef,
use crate::{types::layout_name::LayoutName, vm::errors::runner_errors::RunnerError};

use super::{
builtin_name::BuiltinName,
instance_definitions::{
builtins_instance_def::BuiltinsInstanceDef,
diluted_pool_instance_def::DilutedPoolInstanceDef,
},
};

pub(crate) const MEMORY_UNITS_PER_STEP: u32 = 8;
Expand Down Expand Up @@ -214,38 +218,58 @@ pub struct RawCairoLayoutParams {
}

impl TryFrom<RawCairoLayoutParams> for CairoLayoutParams {
type Error = &'static str;
type Error = RunnerError;

fn try_from(value: RawCairoLayoutParams) -> Result<Self, Self::Error> {
if !value.uses_pedersen_builtin && value.pedersen_ratio != 0 {
return Err("pedersen ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::pedersen,
));
}
if !value.uses_range_check_builtin && value.range_check_ratio != 0 {
return Err("range_check ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::range_check,
));
}
if !value.uses_ecdsa_builtin && value.ecdsa_ratio != 0 {
return Err("ecdsa ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::ecdsa,
));
}
if !value.uses_bitwise_builtin && value.bitwise_ratio != 0 {
return Err("bitwise ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::bitwise,
));
}
if !value.uses_ec_op_builtin && value.ec_op_ratio != 0 {
return Err("ec_op ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::ec_op,
));
}
if !value.uses_keccak_builtin && value.keccak_ratio != 0 {
return Err("keccak ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::keccak,
));
}
if !value.uses_poseidon_builtin && value.poseidon_ratio != 0 {
return Err("poseidon ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::poseidon,
));
}
if !value.uses_range_check96_builtin && value.range_check96_ratio != 0 {
return Err("range_check96 ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::range_check96,
));
}
if !value.uses_add_mod_builtin && value.add_mod_ratio != 0 {
return Err("add_mod ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::add_mod,
));
}
if !value.uses_mul_mod_builtin && value.mul_mod_ratio != 0 {
return Err("mul_mod ratio should be 0 when disabled");
return Err(RunnerError::BadDynamicLayoutBuiltinRatio(
BuiltinName::mul_mod,
));
}

Ok(CairoLayoutParams {
Expand Down
6 changes: 4 additions & 2 deletions vm/src/vm/errors/runner_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ pub enum RunnerError {
CairoPieProofMode,
#[error("{0}: Invalid additional data")]
InvalidAdditionalData(BuiltinName),
#[error("Bad dynamic layout params: {0}")]
BadDynamicLayoutParams(String),
#[error("dynamic layout params is missing")]
MissingDynamicLayoutParams,
#[error("dynamic layout {0} ratio should be 0 when disabled")]
BadDynamicLayoutBuiltinRatio(BuiltinName),
}

#[cfg(test)]
Expand Down
5 changes: 2 additions & 3 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ impl CairoRunner {
LayoutName::all_cairo => CairoLayout::all_cairo_instance(),
LayoutName::all_solidity => CairoLayout::all_solidity_instance(),
LayoutName::dynamic => {
let params = dynamic_layout_params.ok_or(RunnerError::BadDynamicLayoutParams(
"cairo layout param is missing".to_string(),
))?;
let params =
dynamic_layout_params.ok_or(RunnerError::MissingDynamicLayoutParams)?;

CairoLayout::dynamic_instance(params)
}
Expand Down

0 comments on commit ae0f943

Please sign in to comment.