|
| 1 | +use crate::errors::StarknetRpcApiError; |
| 2 | +use crate::errors::StarknetRpcResult; |
| 3 | +use crate::utils::tx_api_to_blockifier; |
| 4 | +use crate::Starknet; |
| 5 | +use blockifier::transaction::account_transaction::ExecutionFlags; |
| 6 | +use mc_exec::execution::TxInfo; |
| 7 | +use mc_exec::MadaraBlockViewExecutionExt; |
| 8 | +use mc_exec::EXECUTION_UNSUPPORTED_BELOW_VERSION; |
| 9 | +use mp_convert::ToFelt; |
| 10 | +use mp_rpc::v0_10_0::{BlockId, BroadcastedTxn, FeeEstimate, SimulationFlagForEstimateFee}; |
| 11 | +use mp_transactions::{IntoStarknetApiExt, ToBlockifierError}; |
| 12 | +use starknet_types_core::felt::Felt; |
| 13 | + |
| 14 | +/// Extract contract address from a broadcasted transaction for validation |
| 15 | +fn get_contract_address_from_tx(tx: &BroadcastedTxn) -> Option<Felt> { |
| 16 | + match tx { |
| 17 | + BroadcastedTxn::Invoke(invoke_tx) => { |
| 18 | + match invoke_tx { |
| 19 | + mp_rpc::v0_10_0::BroadcastedInvokeTxn::V0(tx) => Some(tx.contract_address), |
| 20 | + mp_rpc::v0_10_0::BroadcastedInvokeTxn::V1(tx) => Some(tx.sender_address), |
| 21 | + mp_rpc::v0_10_0::BroadcastedInvokeTxn::V3(tx) => Some(tx.sender_address), |
| 22 | + mp_rpc::v0_10_0::BroadcastedInvokeTxn::QueryV0(tx) => Some(tx.contract_address), |
| 23 | + mp_rpc::v0_10_0::BroadcastedInvokeTxn::QueryV1(tx) => Some(tx.sender_address), |
| 24 | + mp_rpc::v0_10_0::BroadcastedInvokeTxn::QueryV3(tx) => Some(tx.sender_address), |
| 25 | + } |
| 26 | + } |
| 27 | + BroadcastedTxn::Declare(declare_tx) => { |
| 28 | + match declare_tx { |
| 29 | + mp_rpc::v0_10_0::BroadcastedDeclareTxn::V1(tx) => Some(tx.sender_address), |
| 30 | + mp_rpc::v0_10_0::BroadcastedDeclareTxn::V2(tx) => Some(tx.sender_address), |
| 31 | + mp_rpc::v0_10_0::BroadcastedDeclareTxn::V3(tx) => Some(tx.sender_address), |
| 32 | + mp_rpc::v0_10_0::BroadcastedDeclareTxn::QueryV1(tx) => Some(tx.sender_address), |
| 33 | + mp_rpc::v0_10_0::BroadcastedDeclareTxn::QueryV2(tx) => Some(tx.sender_address), |
| 34 | + mp_rpc::v0_10_0::BroadcastedDeclareTxn::QueryV3(tx) => Some(tx.sender_address), |
| 35 | + } |
| 36 | + } |
| 37 | + // DeployAccount transactions are deploying new contracts, so we don't check for existence |
| 38 | + BroadcastedTxn::DeployAccount(_) => None, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Estimate the fee associated with transaction |
| 43 | +/// |
| 44 | +/// # Arguments |
| 45 | +/// |
| 46 | +/// * `request` - starknet transaction request |
| 47 | +/// * `block_id` - hash of the requested block, number (height), or tag |
| 48 | +/// |
| 49 | +/// # Returns |
| 50 | +/// |
| 51 | +/// * `fee_estimate` - fee estimate in gwei |
| 52 | +pub async fn estimate_fee( |
| 53 | + starknet: &Starknet, |
| 54 | + request: Vec<BroadcastedTxn>, |
| 55 | + simulation_flags: Vec<SimulationFlagForEstimateFee>, |
| 56 | + block_id: BlockId, |
| 57 | +) -> StarknetRpcResult<Vec<FeeEstimate>> { |
| 58 | + tracing::debug!("estimate fee on block_id {block_id:?}"); |
| 59 | + let view = starknet.resolve_block_view(block_id)?; |
| 60 | + let mut exec_context = view.new_execution_context()?; |
| 61 | + |
| 62 | + if exec_context.protocol_version < EXECUTION_UNSUPPORTED_BELOW_VERSION { |
| 63 | + return Err(StarknetRpcApiError::unsupported_txn_version()); |
| 64 | + } |
| 65 | + |
| 66 | + let validate = !simulation_flags.contains(&SimulationFlagForEstimateFee::SkipValidate); |
| 67 | + |
| 68 | + // RPC 0.10.0: Check contract existence for transactions |
| 69 | + let state_view = view.state_view(); |
| 70 | + for tx in &request { |
| 71 | + if let Some(contract_address) = get_contract_address_from_tx(tx) { |
| 72 | + if state_view.get_contract_class_hash(&contract_address)?.is_none() { |
| 73 | + return Err(StarknetRpcApiError::ContractNotFound { |
| 74 | + error: format!("Contract at address {:#x} not found", contract_address).into(), |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + let transactions = request |
| 81 | + .into_iter() |
| 82 | + .map(|tx| { |
| 83 | + let only_query = tx.is_query(); |
| 84 | + let (api_tx, _) = |
| 85 | + tx.into_starknet_api(view.backend().chain_config().chain_id.to_felt(), exec_context.protocol_version)?; |
| 86 | + let execution_flags = ExecutionFlags { only_query, charge_fee: false, validate, strict_nonce_check: true }; |
| 87 | + Ok(tx_api_to_blockifier(api_tx, execution_flags)?) |
| 88 | + }) |
| 89 | + .collect::<Result<Vec<_>, ToBlockifierError>>()?; |
| 90 | + |
| 91 | + let tips = transactions.iter().map(|tx| tx.tip().unwrap_or_default()).collect::<Vec<_>>(); |
| 92 | + |
| 93 | + // spawn_blocking: avoid starving the tokio workers during execution. |
| 94 | + let (execution_results, exec_context) = mp_utils::spawn_blocking(move || { |
| 95 | + Ok::<_, mc_exec::Error>((exec_context.execute_transactions([], transactions)?, exec_context)) |
| 96 | + }) |
| 97 | + .await?; |
| 98 | + |
| 99 | + let fee_estimates = execution_results |
| 100 | + .iter() |
| 101 | + .zip(tips) |
| 102 | + .enumerate() |
| 103 | + .map(|(index, (result, tip))| { |
| 104 | + if result.execution_info.is_reverted() { |
| 105 | + return Err(StarknetRpcApiError::TxnExecutionError { |
| 106 | + tx_index: index, |
| 107 | + error: result.execution_info.revert_error.as_ref().map(|e| e.to_string()).unwrap_or_default(), |
| 108 | + }); |
| 109 | + } |
| 110 | + Ok(FeeEstimate { |
| 111 | + common: exec_context.execution_result_to_fee_estimate_v0_9(result, tip)?, |
| 112 | + unit: mp_rpc::v0_10_0::PriceUnitFri::Fri, |
| 113 | + }) |
| 114 | + }) |
| 115 | + .collect::<Result<_, _>>()?; |
| 116 | + |
| 117 | + Ok(fee_estimates) |
| 118 | +} |
0 commit comments