11use std:: collections:: HashSet ;
22
3+ use apollo_gateway:: rpc_objects:: BlockHeader ;
34use blockifier:: blockifier:: config:: TransactionExecutorConfig ;
45use blockifier:: blockifier:: transaction_executor:: {
56 TransactionExecutionOutput ,
67 TransactionExecutor ,
78} ;
9+ use blockifier:: blockifier_versioned_constants:: VersionedConstants ;
10+ use blockifier:: bouncer:: BouncerConfig ;
811use blockifier:: context:: BlockContext ;
912use blockifier:: state:: cached_state:: { CachedState , StateMaps } ;
1013use blockifier:: state:: contract_class_manager:: ContractClassManager ;
@@ -14,29 +17,88 @@ use blockifier::state::state_reader_and_contract_manager::{
1417} ;
1518use blockifier:: transaction:: account_transaction:: ExecutionFlags ;
1619use blockifier:: transaction:: transaction_execution:: Transaction as BlockifierTransaction ;
17- use blockifier_reexecution:: state_reader:: reexecution_state_reader:: ReexecutionStateReader ;
1820use blockifier_reexecution:: state_reader:: rpc_state_reader:: RpcStateReader ;
19- use starknet_api:: block:: { BlockHash , BlockNumber } ;
21+ use blockifier_reexecution:: utils:: get_chain_info;
22+ use starknet_api:: block:: { BlockHash , BlockInfo , BlockNumber } ;
23+ use starknet_api:: block_hash:: block_hash_calculator:: { concat_counts, BlockHeaderCommitments } ;
2024use starknet_api:: core:: { ChainId , ClassHash } ;
2125use starknet_api:: transaction:: fields:: Fee ;
2226use starknet_api:: transaction:: { InvokeTransaction , Transaction , TransactionHash } ;
27+ use starknet_api:: versioned_constants_logic:: VersionedConstantsTrait ;
2328
2429use crate :: errors:: VirtualBlockExecutorError ;
2530
2631/// Captures execution data for a virtual block (multiple transactions).
2732///
2833/// This struct contains all the execution data needed for proof generation.
34+ pub struct BaseBlockInfo {
35+ pub ( crate ) block_context : BlockContext ,
36+ /// The block hash of the base block,
37+ /// in which the virtual block is executed.
38+ pub ( crate ) base_block_hash : BlockHash ,
39+ /// The commitment used for computing the block hash of the base block.
40+ pub ( crate ) base_block_header_commitments : BlockHeaderCommitments ,
41+ /// The block hash of the previous base block.
42+ /// Used to compute the base block hash in the os.
43+ pub ( crate ) prev_base_block_hash : BlockHash ,
44+ }
45+
46+ impl TryFrom < ( BlockHeader , ChainId ) > for BaseBlockInfo {
47+ type Error = VirtualBlockExecutorError ;
48+
49+ fn try_from ( ( header, chain_id) : ( BlockHeader , ChainId ) ) -> Result < Self , Self :: Error > {
50+ let base_block_hash = header. block_hash ;
51+ let prev_base_block_hash = header. parent_hash ;
52+ let base_block_header_commitments = BlockHeaderCommitments {
53+ transaction_commitment : header. transaction_commitment ,
54+ event_commitment : header. event_commitment ,
55+ receipt_commitment : header. receipt_commitment ,
56+ state_diff_commitment : header. state_diff_commitment ,
57+ concatenated_counts : concat_counts (
58+ header. transaction_count ,
59+ header. event_count ,
60+ header. state_diff_length ,
61+ header. l1_da_mode ,
62+ ) ,
63+ } ;
64+
65+ let block_info: BlockInfo = header. try_into ( ) . map_err ( |e| {
66+ VirtualBlockExecutorError :: TransactionExecutionError ( format ! (
67+ "Failed to convert block header to block info: {e}"
68+ ) )
69+ } ) ?;
70+ let chain_info = get_chain_info ( & chain_id) ;
71+ let versioned_constants =
72+ VersionedConstants :: get ( & block_info. starknet_version ) . map_err ( |e| {
73+ VirtualBlockExecutorError :: TransactionExecutionError ( format ! (
74+ "Failed to get versioned constants: {e}"
75+ ) )
76+ } ) ?;
77+ let block_context = BlockContext :: new (
78+ block_info,
79+ chain_info,
80+ versioned_constants. clone ( ) ,
81+ BouncerConfig :: default ( ) ,
82+ ) ;
83+
84+ Ok ( BaseBlockInfo {
85+ block_context,
86+ base_block_hash,
87+ base_block_header_commitments,
88+ prev_base_block_hash,
89+ } )
90+ }
91+ }
92+
2993pub struct VirtualBlockExecutionData {
3094 /// Execution outputs for all transactions in the virtual block.
3195 pub execution_outputs : Vec < TransactionExecutionOutput > ,
32- /// The block context in which the transactions were executed.
33- pub block_context : BlockContext ,
3496 /// The initial state reads (accessed state) during execution.
3597 pub initial_reads : StateMaps ,
3698 /// The class hashes of all contracts executed in the virtual block.
3799 pub executed_class_hashes : HashSet < ClassHash > ,
38- /// The block hash of the state at the start of the virtual block.
39- pub prev_base_block_hash : BlockHash ,
100+ /// The base block info for the virtual block.
101+ pub base_block_info : BaseBlockInfo ,
40102}
41103
42104/// Executes a virtual block of transactions.
@@ -85,9 +147,8 @@ pub trait VirtualBlockExecutor {
85147 txs : Vec < ( InvokeTransaction , TransactionHash ) > ,
86148 ) -> Result < VirtualBlockExecutionData , VirtualBlockExecutorError > {
87149 let blockifier_txs = self . convert_invoke_txs ( txs) ?;
88- let block_context = self . block_context ( block_number) ?;
150+ let base_block_info = self . base_block_info ( block_number) ?;
89151 let state_reader = self . state_reader ( block_number) ?;
90- let prev_base_block_hash = self . prev_base_block_hash ( block_number) ?;
91152
92153 // Create state reader with contract manager.
93154 let state_reader_and_contract_manager =
@@ -98,7 +159,7 @@ pub trait VirtualBlockExecutor {
98159 // Create executor WITHOUT preprocessing (no pre_process_block call).
99160 let mut transaction_executor = TransactionExecutor :: new (
100161 block_state,
101- block_context. clone ( ) ,
162+ base_block_info . block_context . clone ( ) ,
102163 TransactionExecutorConfig :: default ( ) ,
103164 ) ;
104165
@@ -131,10 +192,9 @@ pub trait VirtualBlockExecutor {
131192
132193 Ok ( VirtualBlockExecutionData {
133194 execution_outputs,
134- block_context ,
195+ base_block_info ,
135196 initial_reads,
136197 executed_class_hashes,
137- prev_base_block_hash,
138198 } )
139199 }
140200
@@ -169,17 +229,12 @@ pub trait VirtualBlockExecutor {
169229 } )
170230 . collect ( )
171231 }
172- /// Returns the block context for the given block number.
173- fn block_context (
174- & self ,
175- block_number : BlockNumber ,
176- ) -> Result < BlockContext , VirtualBlockExecutorError > ;
177232
178- /// Returns the block hash of the state at the start of the virtual block.
179- fn prev_base_block_hash (
233+ /// Returns the base block info for the given block number .
234+ fn base_block_info (
180235 & self ,
181236 block_number : BlockNumber ,
182- ) -> Result < BlockHash , VirtualBlockExecutorError > ;
237+ ) -> Result < BaseBlockInfo , VirtualBlockExecutorError > ;
183238
184239 /// Returns a state reader that implements `FetchCompiledClasses` for the given block number.
185240 /// Must be `Send + Sync + 'static` to be used in the transaction executor.
@@ -194,6 +249,7 @@ pub trait VirtualBlockExecutor {
194249
195250#[ allow( dead_code) ]
196251pub ( crate ) struct RpcVirtualBlockExecutor {
252+ /// The state reader for the virtual block executor.
197253 pub rpc_state_reader : RpcStateReader ,
198254 /// Whether transaction validation is enabled during execution.
199255 pub validate_txs : bool ,
@@ -219,22 +275,15 @@ impl RpcVirtualBlockExecutor {
219275/// without block preprocessing. Validation and fee charging are always skipped,
220276/// making it suitable for simulation and OS input generation.
221277impl VirtualBlockExecutor for RpcVirtualBlockExecutor {
222- fn block_context (
278+ fn base_block_info (
223279 & self ,
224280 _block_number : BlockNumber ,
225- ) -> Result < BlockContext , VirtualBlockExecutorError > {
226- self . rpc_state_reader
227- . get_block_context ( )
228- . map_err ( |e| VirtualBlockExecutorError :: ReexecutionError ( Box :: new ( e) ) )
229- }
230-
231- fn prev_base_block_hash (
232- & self ,
233- block_number : BlockNumber ,
234- ) -> Result < BlockHash , VirtualBlockExecutorError > {
235- self . rpc_state_reader
236- . get_old_block_hash ( block_number)
237- . map_err ( |e| VirtualBlockExecutorError :: ReexecutionError ( Box :: new ( e) ) )
281+ ) -> Result < BaseBlockInfo , VirtualBlockExecutorError > {
282+ let block_header = self
283+ . rpc_state_reader
284+ . get_block_header ( )
285+ . map_err ( |e| VirtualBlockExecutorError :: ReexecutionError ( Box :: new ( e) ) ) ?;
286+ BaseBlockInfo :: try_from ( ( block_header, self . rpc_state_reader . chain_id . clone ( ) ) )
238287 }
239288
240289 fn state_reader (
0 commit comments