diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index 431c6e405..1eecd41eb 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -14,7 +14,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_types::communication::SharedMempoolClient; use starknet_mempool_types::mempool_types::{Account, MempoolInput}; use starknet_sierra_compile::compile::compile_sierra_to_casm; @@ -27,7 +27,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, IntoEnumIteratorExt, +}; #[cfg(test)] #[path = "gateway_test.rs"] @@ -194,15 +196,29 @@ pub fn create_gateway( Gateway::new(config, state_reader_factory, client) } +// 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::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 6cebd08d4..afe69fecb 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,28 @@ pub fn is_subsequence(subsequence: &[T], sequence: &[T]) -> bool { offset == subsequence.len() } + +// TODO(Arni): Remove the traitIntoEnumIteratorExt once it is implemented in starknet API. +impl IntoEnumIteratorExt for Builtin { + fn iter() -> impl Iterator { + // The OS expects this order for the builtins. + // When EnumIter is derived for builtin - one must make sure the iteration order is as the + // os expects. + vec![ + Builtin::Pedersen, + Builtin::RangeCheck, + Builtin::Ecdsa, + Builtin::Bitwise, + Builtin::EcOp, + Builtin::Poseidon, + Builtin::SegmentArena, + Builtin::Keccak, + ] + .into_iter() + } +} + +pub trait IntoEnumIteratorExt { + #[allow(opaque_hidden_inferred_bound)] + fn iter() -> impl Iterator; +}