Skip to content

Commit

Permalink
chore: use builtins iter to define os_order_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 3, 2024
1 parent 6a332d4 commit 1a35bd7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use starknet_sierra_compile::errors::CompilationUtilError;
use starknet_sierra_compile::utils::into_contract_class_for_compilation;

use crate::errors::{GatewayError, GatewayResult};
use crate::utils::{is_subsequence, IntoOsOrderEnumIteratorExt};
use crate::utils::{is_subsequence, IntoIterExt};

#[cfg(test)]
#[path = "compilation_test.rs"]
Expand Down
32 changes: 22 additions & 10 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ pub fn is_subsequence<T: Eq>(subsequence: &[T], sequence: &[T]) -> bool {
offset == subsequence.len()
}

// TODO(Arni): Remove the traitIntoEnumIteratorExt once EnumIter is implemented in starknet API.
// Make sure the order is saved or address the different order.
impl IntoOsOrderEnumIteratorExt for Builtin {
// TODO(Arni): Remove the IntoIterExt once IntoIterEnum is implemented in starknet API.
pub trait IntoIterExt {
fn iter() -> impl Iterator<Item = Builtin>;
}

impl IntoIterExt for Builtin {
fn iter() -> impl Iterator<Item = Builtin> {
// The OS expects this order for the builtins.
vec![
let mut builtins_vector: Vec<Builtin> = vec![
Builtin::Pedersen,
Builtin::RangeCheck,
Builtin::Ecdsa,
Expand All @@ -192,12 +194,22 @@ impl IntoOsOrderEnumIteratorExt for Builtin {
Builtin::Poseidon,
Builtin::SegmentArena,
Builtin::Keccak,
]
.into_iter()
];
builtins_vector.sort_by_key(builtin_order);
builtins_vector.into_iter().into_iter()
}
}

pub trait IntoOsOrderEnumIteratorExt {
/// Returns an iterator over all the builtins in the order the Starknet OS expects.
fn iter() -> impl Iterator<Item = Builtin>;
// The OS expects this order for the builtins.
fn builtin_order(builtin: &Builtin) -> usize {
match builtin {
Builtin::Pedersen => 0,
Builtin::RangeCheck => 1,
Builtin::Ecdsa => 2,
Builtin::Bitwise => 3,
Builtin::EcOp => 4,
Builtin::Poseidon => 5,
Builtin::SegmentArena => 6,
Builtin::Keccak => 7,
}
}
2 changes: 1 addition & 1 deletion crates/gateway/src/utils_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pretty_assertions::assert_eq;
use rstest::rstest;
use starknet_api::transaction::Builtin;

use crate::utils::{is_subsequence, IntoOsOrderEnumIteratorExt};
use crate::utils::{is_subsequence, IntoIterExt};

#[rstest]
#[case::empty(
Expand Down

0 comments on commit 1a35bd7

Please sign in to comment.