From fd9e01ea157defb57673f1506ad8a0898f216779 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Sun, 30 Jun 2024 11:38:51 +0300 Subject: [PATCH] feat: use starknet_api builtin for name trait --- crates/gateway/src/gateway.rs | 17 ++++++++++++----- crates/gateway/src/utils.rs | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index b8afb53cf..4870a10c6 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -13,7 +13,7 @@ use cairo_lang_starknet_classes::casm_contract_class::{ }; 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, CompilationUtilError}; @@ -25,7 +25,7 @@ use crate::starknet_api_test_utils::get_sender_address; 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, is_subsequence}; +use crate::utils::{external_tx_to_thin_tx, is_subsequence, NameExt}; #[cfg(test)] #[path = "gateway_test.rs"] @@ -185,9 +185,16 @@ pub fn compile_contract_class(declare_tx: &RPCDeclareTransaction) -> GatewayResu // TODO(Arni): Add to a config. fn get_supported_builtins() -> Vec { - let builtins = - ["pedersen", "range_check", "ecdsa", "bitwise", "ec_op", "poseidon", "segment_arena"]; - builtins.iter().map(|builtin| builtin.to_string()).collect() + let builtins = [ + Builtin::Pedersen, + Builtin::RangeCheck, + Builtin::Ecdsa, + Builtin::Bitwise, + Builtin::EcOp, + Builtin::Poseidon, + Builtin::SegmentArena, + ]; + builtins.iter().map(|builtin| builtin.name().to_string()).collect() } // TODO(Arni): Add test. diff --git a/crates/gateway/src/utils.rs b/crates/gateway/src/utils.rs index b371f57a4..b0d9b02ce 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; @@ -160,3 +161,32 @@ pub fn is_subsequence(subsequence: &[String], sequence: &[String]) -> bool { } true } + +// TODO(Arni): Remove the trait NameExt once it is implemented in starknet API. +const RANGE_CHACK_BUILTIN_NAME: &str = "range_check"; +const PEDERSEN_BUILTIN_NAME: &str = "pedersen"; +const POSEIDON_BUILTIN_NAME: &str = "poseidon"; +const EC_OP_BUILTIN_NAME: &str = "ec_op"; +const ECDSA_BUILTIN_NAME: &str = "ecdsa"; +const BITWISE_BUILTIN_NAME: &str = "bitwise"; +const KECCAK_BUILTIN_NAME: &str = "keccak"; +const SEGMENT_ARENA_BUILTIN_NAME: &str = "segment_arena"; + +impl NameExt for Builtin { + fn name(&self) -> &'static str { + match self { + Builtin::RangeCheck => RANGE_CHACK_BUILTIN_NAME, + Builtin::Pedersen => PEDERSEN_BUILTIN_NAME, + Builtin::Poseidon => POSEIDON_BUILTIN_NAME, + Builtin::EcOp => EC_OP_BUILTIN_NAME, + Builtin::Ecdsa => ECDSA_BUILTIN_NAME, + Builtin::Bitwise => BITWISE_BUILTIN_NAME, + Builtin::Keccak => KECCAK_BUILTIN_NAME, + Builtin::SegmentArena => SEGMENT_ARENA_BUILTIN_NAME, + } + } +} + +pub trait NameExt { + fn name(&self) -> &'static str; +}