@@ -9,15 +9,21 @@ use blockifier::execution::contract_class::{EntryPointV1, EntryPointsByType, Nes
99use blockifier:: test_utils:: contracts:: FeatureContractTrait ;
1010use blockifier_test_utils:: cairo_versions:: { CairoVersion , RunnableCairo1 } ;
1111use blockifier_test_utils:: contracts:: FeatureContract ;
12- use cairo_lang_starknet_classes:: casm_contract_class:: CasmContractClass ;
12+ use cairo_lang_starknet_classes:: casm_contract_class:: {
13+ CasmContractClass ,
14+ CasmContractEntryPoint ,
15+ CasmContractEntryPoints ,
16+ } ;
1317use cairo_lang_starknet_classes:: NestedIntList ;
18+ use cairo_lang_utils:: bigint:: BigUintAsHex ;
1419use cairo_vm:: any_box;
1520use cairo_vm:: types:: builtin_name:: BuiltinName ;
1621use cairo_vm:: types:: layout_name:: LayoutName ;
1722use cairo_vm:: types:: relocatable:: MaybeRelocatable ;
1823use cairo_vm:: vm:: runners:: cairo_runner:: ExecutionResources ;
1924use expect_test:: { expect, Expect } ;
2025use log:: info;
26+ use num_bigint:: BigUint ;
2127use rstest:: rstest;
2228use starknet_api:: contract_class:: compiled_class_hash:: {
2329 HashVersion ,
@@ -37,6 +43,7 @@ use crate::test_utils::cairo_runner::{
3743 ImplicitArg ,
3844 ValueArg ,
3945} ;
46+ use crate :: test_utils:: utils:: DEFAULT_PRIME ;
4047use crate :: vm_utils:: LoadCairoObject ;
4148
4249// V1 (Poseidon) HASH CONSTS
@@ -180,6 +187,42 @@ impl HashVersionTestSpec for HashVersion {
180187 }
181188}
182189
190+ fn get_dummy_compiled_class ( contract_segmentation : bool ) -> CasmContractClass {
191+ CasmContractClass {
192+ prime : DEFAULT_PRIME . clone ( ) . to_biguint ( ) . unwrap ( ) ,
193+ compiler_version : "" . into ( ) ,
194+ bytecode : ( 1u8 ..=10 ) . map ( BigUintAsHex :: from) . collect ( ) ,
195+ bytecode_segment_lengths : Some ( if contract_segmentation {
196+ NestedIntList :: Node ( vec ! [
197+ NestedIntList :: Leaf ( 3 ) ,
198+ NestedIntList :: Node ( vec![
199+ NestedIntList :: Leaf ( 1 ) ,
200+ NestedIntList :: Leaf ( 1 ) ,
201+ NestedIntList :: Node ( vec![ NestedIntList :: Leaf ( 1 ) ] ) ,
202+ ] ) ,
203+ NestedIntList :: Leaf ( 4 ) ,
204+ ] )
205+ } else {
206+ NestedIntList :: Leaf ( 10 )
207+ } ) ,
208+ entry_points_by_type : CasmContractEntryPoints {
209+ external : vec ! [ CasmContractEntryPoint {
210+ selector: BigUint :: from( 1u8 ) ,
211+ offset: 1 ,
212+ builtins: vec![ "237" . into( ) ] ,
213+ } ] ,
214+ constructor : vec ! [ CasmContractEntryPoint {
215+ selector: BigUint :: from( 5u8 ) ,
216+ offset: 0 ,
217+ builtins: vec![ ] ,
218+ } ] ,
219+ l1_handler : vec ! [ ] ,
220+ } ,
221+ hints : vec ! [ ] ,
222+ pythonic_hints : None ,
223+ }
224+ }
225+
183226/// Runs the compiled class hash entry point for the given contract class,
184227/// with the specified load_full_contract flag and hash version.
185228/// Returns the execution resources and the computed hash.
@@ -267,6 +310,32 @@ fn run_compiled_class_hash_entry_point(
267310 ( actual_execution_resources, hash_computed_by_cairo)
268311}
269312
313+ #[ rstest]
314+ #[ case:: no_segmentation(
315+ false ,
316+ "0xB268995DD0EE80DEBFB8718852750B5FD22082D0C729121C48A0487A4D2F64" ,
317+ 16
318+ ) ]
319+ #[ case:: segmentation( true , "0x5517AD8471C9AA4D1ADD31837240DEAD9DC6653854169E489A813DB4376BE9C" , 28 ) ]
320+ fn test_compiled_class_hash_basic (
321+ #[ case] segmentation : bool ,
322+ #[ case] expected_hash : & str ,
323+ #[ case] expected_n_poseidons : usize ,
324+ ) {
325+ let ( resources, compiled_class_hash) = run_compiled_class_hash_entry_point (
326+ & get_dummy_compiled_class ( segmentation) ,
327+ // TODO(Dori): Instead of setting load_full_contract to true, we should mark all segments
328+ // as accessed.
329+ true ,
330+ & HashVersion :: V1 ,
331+ ) ;
332+ assert_eq ! ( compiled_class_hash, Felt :: from_hex_unchecked( expected_hash) ) ;
333+ assert_eq ! (
334+ * resources. builtin_instance_counter. get( & BuiltinName :: poseidon) . unwrap( ) ,
335+ expected_n_poseidons
336+ ) ;
337+ }
338+
270339#[ rstest]
271340fn test_compiled_class_hash (
272341 #[ values( true , false ) ] load_full_contract : bool ,
0 commit comments