Skip to content

Commit

Permalink
feat: use starknet_api builtin for name trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 2, 2024
1 parent 0c30c7a commit 307d91a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
32 changes: 24 additions & 8 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"]
Expand Down Expand Up @@ -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<String> = {
// 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::<Vec<String>>()
Builtin::iter()
.filter(is_supported_builtin)
.map(|builtin| builtin.name().to_string())
.collect::<Vec<String>>()
};
}

Expand Down
30 changes: 28 additions & 2 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -176,3 +177,28 @@ pub fn is_subsequence<T: Eq>(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<Item = Self> {
// 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<Item = Self>;
}

0 comments on commit 307d91a

Please sign in to comment.