|
| 1 | +use blockifier::blockifier::transaction_executor::TransactionExecutionOutput; |
| 2 | +use blockifier::context::BlockContext; |
| 3 | +use blockifier::state::cached_state::StateMaps; |
| 4 | +use blockifier_reexecution::execute_single_transaction; |
| 5 | +use starknet_api::block::BlockNumber; |
| 6 | +use starknet_api::core::ChainId; |
| 7 | +use starknet_api::transaction::Transaction; |
| 8 | + |
| 9 | +use crate::errors::ExecutionDataError; |
| 10 | + |
| 11 | +/// Captures all execution data from blockifier needed for OS input generation. |
| 12 | +/// |
| 13 | +/// This struct contains the complete execution state and context information that blockifier |
| 14 | +/// produces when executing a transaction. It serves as the foundation for generating the OS |
| 15 | +/// (Operating System) input required for proof generation. |
| 16 | +pub struct BlockifierExecutionData { |
| 17 | + /// The output from executing the transaction, including execution info and state changes. |
| 18 | + pub execution_output: TransactionExecutionOutput, |
| 19 | + /// The block context in which the transaction was executed. |
| 20 | + pub block_context: BlockContext, |
| 21 | + /// The initial state reads (accessed state) during transaction execution. |
| 22 | + pub initial_reads: StateMaps, |
| 23 | +} |
| 24 | + |
| 25 | +/// Provides execution data from various sources for OS input generation. |
| 26 | +/// |
| 27 | +/// This trait abstracts over different ways of obtaining transaction execution data from |
| 28 | +/// blockifier. Implementations can fetch execution data from different sources (RPC nodes, |
| 29 | +/// local state, mocked data, etc.). |
| 30 | +/// |
| 31 | +/// The primary use case is the first step in OS input generation: capturing the necessary |
| 32 | +/// execution information from blockifier that will be used to construct the full OS input |
| 33 | +/// for proving. |
| 34 | +/// |
| 35 | +/// # Examples |
| 36 | +/// |
| 37 | +/// ```ignore |
| 38 | +/// let provider = RpcExecutionDataProvider { |
| 39 | +/// rpc_url: "http://localhost:9545".to_string(), |
| 40 | +/// chain_id: ChainId::Mainnet, |
| 41 | +/// }; |
| 42 | +/// |
| 43 | +/// let execution_data = provider.get_execution_data(block_number, transaction)?; |
| 44 | +/// // Use execution_data to build OS input... |
| 45 | +/// ``` |
| 46 | +pub trait ExecutionDataProvider { |
| 47 | + /// Retrieves execution data for a transaction at a specific block. |
| 48 | + /// |
| 49 | + /// # Arguments |
| 50 | + /// |
| 51 | + /// * `block_number` - The block number at which to execute the transaction |
| 52 | + /// * `tx` - The transaction to execute |
| 53 | + /// |
| 54 | + /// # Returns |
| 55 | + /// |
| 56 | + /// Returns `BlockifierExecutionData` containing all execution information needed for OS |
| 57 | + /// input generation, or an error if execution fails. |
| 58 | + fn get_execution_data( |
| 59 | + &self, |
| 60 | + block_number: BlockNumber, |
| 61 | + tx: Transaction, |
| 62 | + ) -> Result<BlockifierExecutionData, ExecutionDataError>; |
| 63 | +} |
| 64 | + |
| 65 | +/// RPC-based implementation of `ExecutionDataProvider`. |
| 66 | +/// |
| 67 | +/// This provider fetches historical state from an RPC node and uses blockifier's re-execution |
| 68 | +/// functionality to obtain execution data. |
| 69 | +pub struct RpcExecutionDataProvider { |
| 70 | + /// URL of the RPC node to fetch state from. |
| 71 | + pub rpc_url: String, |
| 72 | + /// The chain ID for which to execute transactions. |
| 73 | + pub chain_id: ChainId, |
| 74 | +} |
| 75 | + |
| 76 | +impl ExecutionDataProvider for RpcExecutionDataProvider { |
| 77 | + fn get_execution_data( |
| 78 | + &self, |
| 79 | + block_number: BlockNumber, |
| 80 | + tx: Transaction, |
| 81 | + ) -> Result<BlockifierExecutionData, ExecutionDataError> { |
| 82 | + let (execution_output, initial_reads, block_context) = execute_single_transaction( |
| 83 | + block_number, |
| 84 | + self.rpc_url.clone(), |
| 85 | + self.chain_id.clone(), |
| 86 | + tx, |
| 87 | + )?; |
| 88 | + |
| 89 | + Ok(BlockifierExecutionData { execution_output, block_context, initial_reads }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
0 commit comments