diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index a9bc173f..cc3c5a93 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -15,7 +15,7 @@ use cairo_lang_starknet_classes::casm_contract_class::{ use lazy_static::lazy_static; use starknet_api::core::CompiledClassHash; use starknet_api::rpc_transaction::{RPCDeclareTransaction, RPCTransaction}; -use starknet_api::transaction::TransactionHash; +use starknet_api::transaction::{Builtin, TransactionHash}; use starknet_mempool_infra::component_runner::{ComponentRunner, ComponentStartError}; use starknet_mempool_types::communication::SharedMempoolClient; use starknet_mempool_types::mempool_types::{Account, MempoolInput}; @@ -29,7 +29,9 @@ use crate::rpc_state_reader::RpcStateReaderFactory; use crate::state_reader::StateReaderFactory; use crate::stateful_transaction_validator::StatefulTransactionValidator; use crate::stateless_transaction_validator::StatelessTransactionValidator; -use crate::utils::{external_tx_to_thin_tx, get_sender_address, is_subsequence}; +use crate::utils::{ + external_tx_to_thin_tx, get_sender_address, is_subsequence, IntoOsOrderEnumIteratorExt, +}; #[cfg(test)] #[path = "gateway_test.rs"] @@ -205,15 +207,29 @@ impl ComponentRunner for Gateway { } } +// List of supported builtins. +// This is an explicit function so that it is explicitly desiced which builtins are supported. +// If new builtins are added, they should be added here. +fn is_supported_builtin(builtin: &Builtin) -> bool { + match builtin { + Builtin::RangeCheck + | Builtin::Pedersen + | Builtin::Poseidon + | Builtin::EcOp + | Builtin::Ecdsa + | Builtin::Bitwise + | Builtin::SegmentArena => true, + Builtin::Keccak => false, + } +} + // TODO(Arni): Add to a config. -// TODO(Arni): Use the Builtin enum from Starknet-api, and explicitly tag each builtin as supported -// or unsupported so that the compiler would alert us on new builtins. lazy_static! { static ref SUPPORTED_BUILTINS: Vec = { - // The OS expects this order for the builtins. - const SUPPORTED_BUILTIN_NAMES: [&str; 7] = - ["pedersen", "range_check", "ecdsa", "bitwise", "ec_op", "poseidon", "segment_arena"]; - SUPPORTED_BUILTIN_NAMES.iter().map(|builtin| builtin.to_string()).collect::>() + Builtin::os_order_iter() + .filter(is_supported_builtin) + .map(|builtin| builtin.name().to_string()) + .collect::>() }; } diff --git a/crates/gateway/src/utils.rs b/crates/gateway/src/utils.rs index 6cebd08d..271859a9 100644 --- a/crates/gateway/src/utils.rs +++ b/crates/gateway/src/utils.rs @@ -10,8 +10,9 @@ use starknet_api::rpc_transaction::{ RPCDeclareTransaction, RPCDeployAccountTransaction, RPCInvokeTransaction, RPCTransaction, }; use starknet_api::transaction::{ - DeclareTransaction, DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV3, - InvokeTransaction, InvokeTransactionV3, Tip, TransactionHash, TransactionHasher, + Builtin, DeclareTransaction, DeclareTransactionV3, DeployAccountTransaction, + DeployAccountTransactionV3, InvokeTransaction, InvokeTransactionV3, Tip, TransactionHash, + TransactionHasher, }; use starknet_mempool_types::mempool_types::ThinTransaction; @@ -176,3 +177,26 @@ pub fn is_subsequence(subsequence: &[T], sequence: &[T]) -> bool { offset == subsequence.len() } + +// TODO(Arni): Remove the traitIntoEnumIteratorExt once EnumIter is implemented in starknet API. +impl IntoOsOrderEnumIteratorExt for Builtin { + fn os_order_iter() -> impl Iterator { + // The OS expects this order for the builtins. + vec![ + Builtin::Pedersen, + Builtin::RangeCheck, + Builtin::Ecdsa, + Builtin::Bitwise, + Builtin::EcOp, + Builtin::Poseidon, + Builtin::SegmentArena, + Builtin::Keccak, + ] + .into_iter() + } +} + +pub trait IntoOsOrderEnumIteratorExt { + /// Returns an iterator over all the builtins in the order the Starknet OS expects. + fn os_order_iter() -> impl Iterator; +} diff --git a/crates/gateway/src/utils_test.rs b/crates/gateway/src/utils_test.rs index 7b9d2fdb..6f2293e1 100644 --- a/crates/gateway/src/utils_test.rs +++ b/crates/gateway/src/utils_test.rs @@ -1,7 +1,8 @@ use pretty_assertions::assert_eq; use rstest::rstest; +use starknet_api::transaction::Builtin; -use crate::utils::is_subsequence; +use crate::utils::{is_subsequence, IntoOsOrderEnumIteratorExt}; #[rstest] #[case::empty( @@ -76,3 +77,20 @@ fn test_is_subsequence( ) { assert_eq!(is_subsequence(subsequence, sequence), expected_result); } + +#[test] +fn test_os_order_iter() { + let expected_order = vec![ + Builtin::Pedersen, + Builtin::RangeCheck, + Builtin::Ecdsa, + Builtin::Bitwise, + Builtin::EcOp, + Builtin::Poseidon, + Builtin::SegmentArena, + Builtin::Keccak, + ]; + + let actual_order = Builtin::os_order_iter().collect::>(); + assert_eq!(actual_order, expected_order); +}