11use std:: collections:: BTreeMap ;
2- use std:: sync:: Arc ;
2+ use std:: sync:: { Arc , OnceLock } ;
33
44use apollo_config:: dumping:: { prepend_sub_config_name, ser_param, SerializeConfig } ;
55use apollo_config:: { ParamPath , ParamPrivacyInput , SerializedParam } ;
@@ -14,6 +14,7 @@ use starknet_api::transaction::fields::{
1414 Tip ,
1515 ValidResourceBounds ,
1616} ;
17+ use starknet_types_core:: felt:: Felt ;
1718
1819use crate :: blockifier_versioned_constants:: VersionedConstants ;
1920use crate :: bouncer:: BouncerConfig ;
@@ -121,6 +122,9 @@ pub struct BlockContext {
121122 pub ( crate ) chain_info : ChainInfo ,
122123 pub ( crate ) versioned_constants : VersionedConstants ,
123124 pub bouncer_config : BouncerConfig ,
125+ /// Cached on first access; derived from `chain_info`. Fixed for the lifetime of a block
126+ /// context.
127+ virtual_os_config_hash : OnceLock < Felt > ,
124128}
125129
126130impl BlockContext {
@@ -130,7 +134,13 @@ impl BlockContext {
130134 versioned_constants : VersionedConstants ,
131135 bouncer_config : BouncerConfig ,
132136 ) -> Self {
133- BlockContext { block_info, chain_info, versioned_constants, bouncer_config }
137+ BlockContext {
138+ block_info,
139+ chain_info,
140+ versioned_constants,
141+ bouncer_config,
142+ virtual_os_config_hash : OnceLock :: new ( ) ,
143+ }
134144 }
135145
136146 pub fn block_info ( & self ) -> & BlockInfo {
@@ -145,6 +155,15 @@ impl BlockContext {
145155 & self . versioned_constants
146156 }
147157
158+ /// Returns the virtual OS config hash, computing and caching it on first access.
159+ pub ( crate ) fn virtual_os_config_hash ( & self ) -> Felt {
160+ * self . virtual_os_config_hash . get_or_init ( || {
161+ OsChainInfo :: from ( & self . chain_info )
162+ . compute_virtual_os_config_hash ( )
163+ . expect ( "Failed to compute OS config hash" )
164+ } )
165+ }
166+
148167 pub fn to_tx_context (
149168 & self ,
150169 tx_info_creator : & impl TransactionInfoCreator ,
@@ -179,6 +198,12 @@ impl BlockContext {
179198 }
180199 }
181200
201+ /// Returns a new context with the given chain info, resetting the cached config hash.
202+ #[ cfg( any( test, feature = "testing" ) ) ]
203+ pub fn with_chain_info ( self , chain_info : ChainInfo ) -> Self {
204+ Self :: new ( self . block_info , chain_info, self . versioned_constants , self . bouncer_config )
205+ }
206+
182207 /// Test util to allow overriding block gas limits.
183208 #[ cfg( any( test, feature = "testing" ) ) ]
184209 pub fn set_sierra_gas_limits (
0 commit comments