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 3, 2024
1 parent e2a3b42 commit c553217
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
31 changes: 25 additions & 6 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 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};
Expand All @@ -28,7 +28,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 @@ -204,16 +206,33 @@ impl ComponentRunner for Gateway {
}
}

// TODO(Arni): Consider moving into Starknet-api.
// 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.
fn supported_builtins() -> &'static Vec<String> {
static SUPPORTED_BUILTINS: OnceLock<Vec<String>> = OnceLock::new();
SUPPORTED_BUILTINS.get_or_init(|| {
// 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 trait IntoEnumIteratorExt once EnumIter is implemented in starknet API.
pub trait IntoEnumIteratorExt {
/// Returns an iterator over all the builtins in the order the Starknet OS expects.
fn iter() -> impl Iterator<Item = Builtin>;
}

// TODO(Arni): When the trait IntoEnumIteratorExt is removed, Make sure the order is maintained or
// address the different order.
impl IntoEnumIteratorExt for Builtin {
fn iter() -> impl Iterator<Item = Builtin> {
// 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()
}
}
20 changes: 19 additions & 1 deletion crates/gateway/src/utils_test.rs
Original file line number Diff line number Diff line change
@@ -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, IntoEnumIteratorExt};

#[rstest]
#[case::empty(
Expand Down Expand Up @@ -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::iter().collect::<Vec<_>>();
assert_eq!(actual_order, expected_order);
}

0 comments on commit c553217

Please sign in to comment.