@@ -22,8 +22,10 @@ from starkware.starknet.core.aggregator.combine_blocks import combine_blocks
2222from starkware.starknet.core.os.block_context import (
2323 BlockContext,
2424 CompiledClassFactsBundle,
25+ OsGlobalContext,
2526 get_block_context,
2627)
28+ from starkware.starknet.core.os.builtins import get_builtin_params
2729from starkware.starknet.core.os.constants import (
2830 BLOCK_HASH_CONTRACT_ADDRESS,
2931 STORED_BLOCK_HASH_BUFFER,
@@ -94,56 +96,38 @@ func main{
9496 let initial_range_check_ptr = range_check_ptr;
9597 let range_check_ptr = range_check_ptr + 1 ;
9698
97- local n_blocks = nondet %{ len (os_input.block_inputs) %} ;
98-
99- let (
100- local n_compiled_class_facts, local compiled_class_facts, local builtin_costs
101- ) = guess_compiled_class_facts();
99+ local public_keys: felt *;
100+ local n_public_keys: felt ;
101+ %{ fill_public_keys_array(os_hints[' public_keys' ], public_keys, n_public_keys) %}
102102
103- let (
104- local n_deprecated_compiled_class_facts, deprecated_compiled_class_facts
105- ) = deprecated_load_compiled_class_facts();
103+ // Build OS global context.
104+ let os_global_context = get_os_global_context(
105+ n_public_keys=n_public_keys, public_keys=public_keys
106+ );
106107
108+ // Execute blocks.
109+ local n_blocks = nondet %{ len (os_input.block_inputs) %} ;
107110 let (local os_outputs: OsOutput*) = alloc();
108111 %{
109112 from starkware.starknet.core.os.execution_helper import StateUpdatePointers
110113 state_update_pointers = StateUpdatePointers(segments = segments)
111114 %}
112- local compiled_class_facts_bundle: CompiledClassFactsBundle* = new CompiledClassFactsBundle(
113- n_compiled_class_facts=n_compiled_class_facts,
114- compiled_class_facts=compiled_class_facts,
115- n_deprecated_compiled_class_facts=n_deprecated_compiled_class_facts,
116- deprecated_compiled_class_facts=deprecated_compiled_class_facts,
117- );
118-
119- local public_keys: felt *;
120- local n_public_keys: felt ;
121- %{ fill_public_keys_array(os_hints[' public_keys' ], public_keys, n_public_keys) %}
122- let (public_keys_hash) = get_public_keys_hash{hash_ptr=pedersen_ptr}(
123- n_public_keys=n_public_keys, public_keys=public_keys
124- );
125- local starknet_os_config: StarknetOsConfig* = new StarknetOsConfig(
126- chain_id=nondet %{ os_hints_config.starknet_os_config.chain_id %} ,
127- fee_token_address=nondet %{ os_hints_config.starknet_os_config.fee_token_address %} ,
128- public_keys_hash=public_keys_hash,
129- );
130-
131115 local initial_txs_range_check_ptr = nondet %{ segments.add_temp_segment() %} ;
132116 let txs_range_check_ptr = initial_txs_range_check_ptr;
133117 with txs_range_check_ptr {
134118 execute_blocks(
135119 n_blocks=n_blocks,
136120 os_output_per_block_dst=os_outputs,
137- compiled_class_facts_bundle=compiled_class_facts_bundle,
138- starknet_os_config=starknet_os_config,
121+ os_global_context=os_global_context,
139122 );
140123 }
141124
142125 // Validate the guessed compile class facts.
126+ let compiled_class_facts_bundle = os_global_context.compiled_class_facts_bundle;
143127 validate_compiled_class_facts_post_execution(
144128 n_compiled_class_facts=compiled_class_facts_bundle.n_compiled_class_facts,
145129 compiled_class_facts=compiled_class_facts_bundle.compiled_class_facts,
146- builtin_costs=builtin_costs,
130+ builtin_costs=compiled_class_facts_bundle. builtin_costs,
147131 );
148132
149133 // Guess whether to use KZG commitment scheme and whether to output the full state.
@@ -245,12 +229,7 @@ func execute_blocks{
245229 add_mod_ptr: ModBuiltin*,
246230 mul_mod_ptr: ModBuiltin*,
247231 txs_range_check_ptr,
248- } (
249- n_blocks: felt ,
250- os_output_per_block_dst: OsOutput*,
251- compiled_class_facts_bundle: CompiledClassFactsBundle*,
252- starknet_os_config: StarknetOsConfig*,
253- ) {
232+ } (n_blocks: felt , os_output_per_block_dst: OsOutput*, os_global_context: OsGlobalContext*) {
254233 %{ print (f " execute_blocks: { ids.n_blocks} blocks remaining. " ) %}
255234 if (n_blocks == 0 ) {
256235 return ();
@@ -284,17 +263,7 @@ func execute_blocks{
284263 let contract_class_changes_start = contract_class_changes;
285264
286265 // Build block context.
287- let (execute_syscalls_ptr) = get_label_location(label_value=execute_syscalls);
288- let (execute_deprecated_syscalls_ptr) = get_label_location(
289- label_value=execute_deprecated_syscalls
290- );
291-
292- let (block_context: BlockContext*) = get_block_context(
293- execute_syscalls_ptr=execute_syscalls_ptr,
294- execute_deprecated_syscalls_ptr=execute_deprecated_syscalls_ptr,
295- compiled_class_facts_bundle=compiled_class_facts_bundle,
296- starknet_os_config=starknet_os_config,
297- );
266+ let (block_context: BlockContext*) = get_block_context(os_global_context=os_global_context);
298267
299268 // Pre-process block.
300269 with contract_state_changes {
@@ -342,17 +311,6 @@ func execute_blocks{
342311
343312 %{ vm_exit_scope() %}
344313
345- // Compute the general config hash.
346- // This is done here to avoid passing pedersen_ptr to serialize_output_header.
347- let hash_ptr = pedersen_ptr;
348- with hash_ptr {
349- let (starknet_os_config_hash) = get_starknet_os_config_hash(
350- starknet_os_config=&block_context.starknet_os_config
351- );
352- }
353-
354- let pedersen_ptr = hash_ptr;
355-
356314 // All blocks inside of a multi block should be off-chain and therefore
357315 // should not be compressed.
358316 assert os_output_per_block_dst[0 ] = OsOutput(
@@ -363,7 +321,7 @@ func execute_blocks{
363321 prev_block_hash=nondet %{ block_input.prev_block_hash %} ,
364322 new_block_hash=nondet %{ block_input.new_block_hash %} ,
365323 os_program_hash=0 ,
366- starknet_os_config_hash=starknet_os_config_hash,
324+ starknet_os_config_hash=os_global_context. starknet_os_config_hash,
367325 use_kzg_da=FALSE,
368326 full_output=TRUE,
369327 ),
@@ -375,8 +333,7 @@ func execute_blocks{
375333 return execute_blocks(
376334 n_blocks=n_blocks - 1 ,
377335 os_output_per_block_dst=&os_output_per_block_dst[1 ],
378- compiled_class_facts_bundle=compiled_class_facts_bundle,
379- starknet_os_config=starknet_os_config,
336+ os_global_context=os_global_context,
380337 );
381338}
382339
@@ -499,3 +456,52 @@ func migrate_classes_to_v2_casm_hash{
499456 migrate_classes_to_v2_casm_hash(n_classes=n_classes - 1 , block_context=block_context);
500457 return ();
501458}
459+
460+ // Returns an OsGlobalContext instance.
461+ //
462+ // Note: the compiled class facts are guessed here, and must be validated post-execution.
463+ func get_os_global_context {
464+ pedersen_ptr: HashBuiltin*, range_check_ptr, poseidon_ptr: PoseidonBuiltin*
465+ } (n_public_keys: felt , public_keys: felt *) -> OsGlobalContext* {
466+ alloc_locals ;
467+ // Compiled class facts bundle.
468+ let (n_compiled_class_facts, compiled_class_facts, builtin_costs) = guess_compiled_class_facts(
469+ );
470+ let (
471+ n_deprecated_compiled_class_facts, deprecated_compiled_class_facts
472+ ) = deprecated_load_compiled_class_facts();
473+
474+ // Starknet OS config.
475+ let (public_keys_hash) = get_public_keys_hash{hash_ptr=pedersen_ptr}(
476+ n_public_keys=n_public_keys, public_keys=public_keys
477+ );
478+ tempvar starknet_os_config = new StarknetOsConfig(
479+ chain_id=nondet %{ os_hints_config.starknet_os_config.chain_id %} ,
480+ fee_token_address=nondet %{ os_hints_config.starknet_os_config.fee_token_address %} ,
481+ public_keys_hash=public_keys_hash,
482+ );
483+ let (starknet_os_config_hash) = get_starknet_os_config_hash{hash_ptr=pedersen_ptr}(
484+ starknet_os_config=starknet_os_config
485+ );
486+
487+ // Function pointers.
488+ let (execute_syscalls_ptr) = get_label_location(label_value=execute_syscalls);
489+ let (execute_deprecated_syscalls_ptr) = get_label_location(
490+ label_value=execute_deprecated_syscalls
491+ );
492+ tempvar os_global_context: OsGlobalContext* = new OsGlobalContext(
493+ starknet_os_config=starknet_os_config,
494+ starknet_os_config_hash=starknet_os_config_hash,
495+ compiled_class_facts_bundle=new CompiledClassFactsBundle(
496+ n_compiled_class_facts=n_compiled_class_facts,
497+ compiled_class_facts=compiled_class_facts,
498+ builtin_costs=builtin_costs,
499+ n_deprecated_compiled_class_facts=n_deprecated_compiled_class_facts,
500+ deprecated_compiled_class_facts=deprecated_compiled_class_facts,
501+ ),
502+ builtin_params=get_builtin_params(),
503+ execute_syscalls_ptr=execute_syscalls_ptr,
504+ execute_deprecated_syscalls_ptr=execute_deprecated_syscalls_ptr,
505+ );
506+ return os_global_context;
507+ }
0 commit comments