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 d9b06e3 commit ac80823
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
32 changes: 24 additions & 8 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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"]
Expand Down Expand Up @@ -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<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::os_order_iter()
.filter(is_supported_builtin)
.map(|builtin| builtin.name().to_string())
.collect::<Vec<String>>()
};
}

Expand Down
28 changes: 26 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,26 @@ pub fn is_subsequence<T: Eq>(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<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()
}
}

pub trait IntoOsOrderEnumIteratorExt {
/// Returns an iterator over all the builtins in the order the Starknet OS expects.
fn os_order_iter() -> impl Iterator<Item = Builtin>;
}
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, IntoOsOrderEnumIteratorExt};

#[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::os_order_iter().collect::<Vec<_>>();
assert_eq!(actual_order, expected_order);
}

0 comments on commit ac80823

Please sign in to comment.