11use std:: collections:: HashSet ;
22
3+ use apollo_gateway:: rpc_objects:: {
4+ BlockHeader ,
5+ BlockId as GatewayBlockId ,
6+ GetBlockWithTxHashesParams ,
7+ } ;
38use blockifier:: blockifier:: config:: TransactionExecutorConfig ;
49use blockifier:: blockifier:: transaction_executor:: {
510 TransactionExecutionOutput ,
611 TransactionExecutor ,
712} ;
13+ use blockifier:: blockifier_versioned_constants:: VersionedConstants ;
14+ use blockifier:: bouncer:: BouncerConfig ;
815use blockifier:: context:: BlockContext ;
916use blockifier:: state:: cached_state:: { CachedState , StateMaps } ;
1017use blockifier:: state:: contract_class_manager:: ContractClassManager ;
@@ -14,29 +21,64 @@ use blockifier::state::state_reader_and_contract_manager::{
1421} ;
1522use blockifier:: transaction:: account_transaction:: ExecutionFlags ;
1623use blockifier:: transaction:: transaction_execution:: Transaction as BlockifierTransaction ;
17- use blockifier_reexecution:: state_reader:: reexecution_state_reader:: ReexecutionStateReader ;
1824use blockifier_reexecution:: state_reader:: rpc_state_reader:: RpcStateReader ;
19- use starknet_api:: block:: { BlockHash , BlockNumber } ;
25+ use blockifier_reexecution:: utils:: get_chain_info;
26+ use starknet_api:: block:: { BlockHash , BlockInfo , BlockNumber } ;
2027use starknet_api:: core:: { ChainId , ClassHash } ;
2128use starknet_api:: transaction:: fields:: Fee ;
2229use starknet_api:: transaction:: { InvokeTransaction , Transaction , TransactionHash } ;
30+ use starknet_api:: versioned_constants_logic:: VersionedConstantsTrait ;
2331
2432use crate :: errors:: VirtualBlockExecutorError ;
2533
2634/// Captures execution data for a virtual block (multiple transactions).
2735///
2836/// This struct contains all the execution data needed for proof generation.
37+ pub struct BaseBlockInfo {
38+ pub ( crate ) block_context : BlockContext ,
39+ /// The block hash of the base block,
40+ /// in which the virtual block is executed.
41+ pub ( crate ) base_block_hash : BlockHash ,
42+ /// The block hash of the previous base block.
43+ /// Used to compute the base block hash in the os.
44+ pub ( crate ) prev_base_block_hash : BlockHash ,
45+ }
46+
47+ impl TryFrom < ( BlockHeader , ChainId ) > for BaseBlockInfo {
48+ type Error = VirtualBlockExecutorError ;
49+
50+ fn try_from ( ( header, chain_id) : ( BlockHeader , ChainId ) ) -> Result < Self , Self :: Error > {
51+ let base_block_hash = header. block_hash ;
52+ let prev_base_block_hash = header. parent_hash ;
53+
54+ let block_info: BlockInfo = header. try_into ( ) ?;
55+ let chain_info = get_chain_info ( & chain_id) ;
56+ let versioned_constants =
57+ VersionedConstants :: get ( & block_info. starknet_version ) . map_err ( |e| {
58+ VirtualBlockExecutorError :: TransactionExecutionError ( format ! (
59+ "Failed to get versioned constants: {e}"
60+ ) )
61+ } ) ?;
62+ let block_context = BlockContext :: new (
63+ block_info,
64+ chain_info,
65+ versioned_constants. clone ( ) ,
66+ BouncerConfig :: default ( ) ,
67+ ) ;
68+
69+ Ok ( BaseBlockInfo { block_context, base_block_hash, prev_base_block_hash } )
70+ }
71+ }
72+
2973pub struct VirtualBlockExecutionData {
3074 /// Execution outputs for all transactions in the virtual block.
3175 pub execution_outputs : Vec < TransactionExecutionOutput > ,
32- /// The block context in which the transactions were executed.
33- pub block_context : BlockContext ,
3476 /// The initial state reads (accessed state) during execution.
3577 pub initial_reads : StateMaps ,
3678 /// The class hashes of all contracts executed in the virtual block.
3779 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 ,
80+ /// The base block info for the virtual block.
81+ pub base_block_info : BaseBlockInfo ,
4082}
4183
4284/// Executes a virtual block of transactions.
@@ -85,9 +127,8 @@ pub trait VirtualBlockExecutor {
85127 txs : Vec < ( InvokeTransaction , TransactionHash ) > ,
86128 ) -> Result < VirtualBlockExecutionData , VirtualBlockExecutorError > {
87129 let blockifier_txs = self . convert_invoke_txs ( txs) ?;
88- let block_context = self . block_context ( block_number) ?;
130+ let base_block_info = self . base_block_info ( block_number) ?;
89131 let state_reader = self . state_reader ( block_number) ?;
90- let prev_base_block_hash = self . prev_base_block_hash ( block_number) ?;
91132
92133 // Create state reader with contract manager.
93134 let state_reader_and_contract_manager =
@@ -98,7 +139,7 @@ pub trait VirtualBlockExecutor {
98139 // Create executor WITHOUT preprocessing (no pre_process_block call).
99140 let mut transaction_executor = TransactionExecutor :: new (
100141 block_state,
101- block_context. clone ( ) ,
142+ base_block_info . block_context . clone ( ) ,
102143 TransactionExecutorConfig :: default ( ) ,
103144 ) ;
104145
@@ -131,10 +172,9 @@ pub trait VirtualBlockExecutor {
131172
132173 Ok ( VirtualBlockExecutionData {
133174 execution_outputs,
134- block_context ,
175+ base_block_info ,
135176 initial_reads,
136177 executed_class_hashes,
137- prev_base_block_hash,
138178 } )
139179 }
140180
@@ -169,17 +209,12 @@ pub trait VirtualBlockExecutor {
169209 } )
170210 . collect ( )
171211 }
172- /// Returns the block context for the given block number.
173- fn block_context (
174- & self ,
175- block_number : BlockNumber ,
176- ) -> Result < BlockContext , VirtualBlockExecutorError > ;
177212
178- /// Returns the block hash of the state at the start of the virtual block.
179- fn prev_base_block_hash (
213+ /// Returns the base block info for the given block number .
214+ fn base_block_info (
180215 & self ,
181216 block_number : BlockNumber ,
182- ) -> Result < BlockHash , VirtualBlockExecutorError > ;
217+ ) -> Result < BaseBlockInfo , VirtualBlockExecutorError > ;
183218
184219 /// Returns a state reader that implements `FetchCompiledClasses` for the given block number.
185220 /// Must be `Send + Sync + 'static` to be used in the transaction executor.
@@ -194,6 +229,7 @@ pub trait VirtualBlockExecutor {
194229
195230#[ allow( dead_code) ]
196231pub ( crate ) struct RpcVirtualBlockExecutor {
232+ /// The state reader for the virtual block executor.
197233 pub rpc_state_reader : RpcStateReader ,
198234 /// Whether transaction validation is enabled during execution.
199235 pub validate_txs : bool ,
@@ -219,22 +255,19 @@ impl RpcVirtualBlockExecutor {
219255/// without block preprocessing. Validation and fee charging are always skipped,
220256/// making it suitable for simulation and OS input generation.
221257impl VirtualBlockExecutor for RpcVirtualBlockExecutor {
222- fn block_context (
223- & self ,
224- _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 (
258+ fn base_block_info (
232259 & self ,
233260 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) ) )
261+ ) -> Result < BaseBlockInfo , VirtualBlockExecutorError > {
262+ let get_block_params =
263+ GetBlockWithTxHashesParams { block_id : GatewayBlockId :: Number ( block_number) } ;
264+ let get_block_with_tx_hashes_result = self
265+ . rpc_state_reader
266+ . rpc_state_reader
267+ . send_rpc_request ( "starknet_getBlockWithTxHashes" , get_block_params) ?;
268+ let block_header: BlockHeader = serde_json:: from_value ( get_block_with_tx_hashes_result) ?;
269+
270+ BaseBlockInfo :: try_from ( ( block_header, self . rpc_state_reader . chain_id . clone ( ) ) )
238271 }
239272
240273 fn state_reader (
0 commit comments