|
1 | 1 | use std::collections::{BTreeMap, HashSet}; |
2 | 2 |
|
| 3 | +use blockifier::execution::contract_class::{CompiledClassV1, RunnableCompiledClass}; |
| 4 | +use blockifier::state::contract_class_manager::ContractClassManager; |
3 | 5 | use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; |
| 6 | +use cairo_lang_utils::bigint::BigUintAsHex; |
| 7 | +use cairo_vm::types::relocatable::MaybeRelocatable; |
| 8 | +use starknet_api::contract_class::compiled_class_hash::{HashVersion, HashableCompiledClass}; |
| 9 | +use starknet_api::contract_class::ContractClass; |
4 | 10 | use starknet_api::core::{ClassHash, CompiledClassHash}; |
5 | | -use starknet_api::deprecated_contract_class::ContractClass; |
| 11 | +use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContractClass; |
| 12 | +use starknet_types_core::felt::Felt; |
6 | 13 |
|
7 | 14 | use crate::errors::ClassesProviderError; |
8 | 15 |
|
9 | 16 | /// The classes required for a Starknet OS run. |
10 | | -/// Matches the fields in `StarknetOsInput` and `OsBlockInput`. |
| 17 | +/// Matches the fields in `StarknetOsInput`. |
11 | 18 | pub struct ClassesInput { |
12 | 19 | /// Deprecated (Cairo 0) contract classes. |
13 | 20 | /// Maps ClassHash to the contract class definition. |
14 | | - pub deprecated_compiled_classes: BTreeMap<ClassHash, ContractClass>, |
| 21 | + pub deprecated_compiled_classes: BTreeMap<ClassHash, DeprecatedContractClass>, |
15 | 22 | /// Cairo 1+ contract classes (CASM). |
16 | 23 | /// Maps CompiledClassHash to the CASM contract class definition. |
17 | 24 | pub compiled_classes: BTreeMap<CompiledClassHash, CasmContractClass>, |
18 | 25 | } |
19 | 26 |
|
20 | 27 | pub trait ClassesProvider { |
21 | | - /// Fetches all classes required for the OS run based on the executed class hashes. |
| 28 | + /// Fetches a class from an external source (e.g., storage, network). |
| 29 | + /// Called when the class is not found in the `ContractClassManager`. |
| 30 | + fn fetch_class(&self, class_hash: ClassHash) -> Result<ContractClass, ClassesProviderError>; |
| 31 | + |
| 32 | + /// Gets all classes required for the OS run. |
| 33 | + /// |
| 34 | + /// For each class hash: |
| 35 | + /// 1. Tries to get the class from `ContractClassManager` first |
| 36 | + /// 2. Falls back to `fetch_class` if not found in manager |
| 37 | + /// 3. Converts to the appropriate format for OS execution |
22 | 38 | fn get_classes( |
23 | 39 | &self, |
24 | 40 | executed_class_hashes: &HashSet<ClassHash>, |
25 | | - ) -> Result<ClassesInput, ClassesProviderError>; |
| 41 | + contract_class_manager: &ContractClassManager, |
| 42 | + ) -> Result<ClassesInput, ClassesProviderError> { |
| 43 | + let mut deprecated_compiled_classes = BTreeMap::new(); |
| 44 | + let mut compiled_classes = BTreeMap::new(); |
| 45 | + |
| 46 | + for &class_hash in executed_class_hashes { |
| 47 | + // Try manager first. |
| 48 | + if let Some(runnable) = contract_class_manager.get_runnable(&class_hash) { |
| 49 | + match runnable { |
| 50 | + RunnableCompiledClass::V0(_) => { |
| 51 | + unimplemented!("V0 class conversion from manager not yet supported") |
| 52 | + } |
| 53 | + RunnableCompiledClass::V1(v1) => { |
| 54 | + let casm = Self::compiled_class_v1_to_casm(&v1); |
| 55 | + // TODO(Aviv): The state reader in the contract class manager does not |
| 56 | + // support get compiled class hash v2. We need to |
| 57 | + // implement this in the state reader. |
| 58 | + let compiled_class_hash = contract_class_manager |
| 59 | + .get_compiled_class_hash_v2(&class_hash) |
| 60 | + .unwrap_or_else(|| casm.hash(&HashVersion::V2)); |
| 61 | + compiled_classes.insert(compiled_class_hash, casm); |
| 62 | + } |
| 63 | + } |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + // Fallback to fetch. |
| 68 | + match self.fetch_class(class_hash)? { |
| 69 | + ContractClass::V0(deprecated_class) => { |
| 70 | + deprecated_compiled_classes.insert(class_hash, deprecated_class); |
| 71 | + } |
| 72 | + ContractClass::V1((casm, _sierra_version)) => { |
| 73 | + let compiled_hash = casm.hash(&HashVersion::V2); |
| 74 | + compiled_classes.insert(compiled_hash, casm); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + Ok(ClassesInput { deprecated_compiled_classes, compiled_classes }) |
| 80 | + } |
| 81 | + |
| 82 | + /// Converts a `CompiledClassV1` to a `CasmContractClass` for OS execution. |
| 83 | + /// Note: Some fields are not preserved in `CompiledClassV1` and are set to default values: |
| 84 | + /// - `compiler_version`: Set to empty string |
| 85 | + /// - `hints`: Set to empty (OS doesn't use them from this struct) |
| 86 | + /// - `pythonic_hints`: Set to None |
| 87 | + fn compiled_class_v1_to_casm(class: &CompiledClassV1) -> CasmContractClass { |
| 88 | + // TODO(Aviv): Consider using dummy prime since it is not used in the OS. |
| 89 | + let prime = Felt::prime(); |
| 90 | + |
| 91 | + let bytecode: Vec<BigUintAsHex> = class |
| 92 | + .program |
| 93 | + .iter_data() |
| 94 | + .map(|maybe_relocatable| match maybe_relocatable { |
| 95 | + MaybeRelocatable::Int(felt) => BigUintAsHex { value: felt.to_biguint() }, |
| 96 | + _ => panic!("Expected all bytecode elements to be MaybeRelocatable::Int"), |
| 97 | + }) |
| 98 | + .collect(); |
| 99 | + |
| 100 | + CasmContractClass { |
| 101 | + prime, |
| 102 | + compiler_version: String::new(), |
| 103 | + bytecode, |
| 104 | + bytecode_segment_lengths: Some(class.bytecode_segment_felt_sizes().into()), |
| 105 | + hints: Vec::new(), |
| 106 | + pythonic_hints: None, |
| 107 | + entry_points_by_type: (&class.entry_points_by_type).into(), |
| 108 | + } |
| 109 | + } |
26 | 110 | } |
0 commit comments