From 8eaa73189d05dc19b62bd3bc8433ba59302dd87d Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 28 Jun 2023 13:14:05 -0300 Subject: [PATCH 01/14] Add skip flags to transactions --- src/lib.rs | 6 ++-- src/transaction/declare.rs | 46 ++++++++++++++++++++++++------ src/transaction/deploy.rs | 23 ++++++++++++++- src/transaction/invoke_function.rs | 10 +++++-- src/transaction/mod.rs | 35 +++++++++++++++++++++++ 5 files changed, 104 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 813f2c5a6..0c20f260e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,6 @@ use crate::{ use definitions::block_context::BlockContext; use state::cached_state::CachedState; -use transaction::InvokeFunction; use transaction::L1Handler; use utils::Address; @@ -46,15 +45,14 @@ pub mod transaction; pub mod utils; pub fn simulate_transaction( - transaction: &InvokeFunction, + transaction: &Transaction, state: S, block_context: BlockContext, remaining_gas: u128, skip_validate: bool, skip_execute: bool, ) -> Result { - let tx_for_simulation = - transaction.create_for_simulation(transaction.clone(), skip_validate, skip_execute); + let tx_for_simulation = transaction.create_for_simulation(skip_validate, skip_execute); tx_for_simulation.simulate_transaction(state, block_context, remaining_gas) } diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index 4123161d1..db36cc7f9 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -28,10 +28,12 @@ use num_traits::Zero; use starknet_contract_class::EntryPointType; use std::collections::HashMap; +use super::Transaction; + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// Represents an internal transaction in the StarkNet network that is a declaration of a Cairo /// contract class. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Declare { pub class_hash: ClassHash, pub sender_address: Address, @@ -43,6 +45,8 @@ pub struct Declare { pub nonce: Felt252, pub hash_value: Felt252, pub contract_class: ContractClass, + pub skip_validate: bool, + pub skip_execute: bool, } // ------------------------------------------------------------ @@ -88,6 +92,8 @@ impl Declare { nonce, hash_value, contract_class, + skip_execute: false, + skip_validate: false, }; internal_declare.verify_version()?; @@ -128,9 +134,11 @@ impl Declare { // validate transaction let mut resources_manager = ExecutionResourcesManager::default(); - let validate_info = - self.run_validate_entrypoint(state, &mut resources_manager, block_context)?; - + let validate_info = if self.skip_validate { + None + } else { + self.run_validate_entrypoint(state, &mut resources_manager, block_context)? + }; let changes = state.count_actual_storage_changes(); let actual_resources = calculate_tx_resources( resources_manager, @@ -222,10 +230,17 @@ impl Declare { let mut tx_execution_context = self.get_execution_context(block_context.invoke_tx_max_n_steps); - let fee_transfer_info = - execute_fee_transfer(state, block_context, &mut tx_execution_context, actual_fee)?; - - Ok((Some(fee_transfer_info), actual_fee)) + let fee_transfer_info = if self.skip_execute { + None + } else { + Some(execute_fee_transfer( + state, + block_context, + &mut tx_execution_context, + actual_fee, + )?) + }; + Ok((fee_transfer_info, actual_fee)) } fn handle_nonce(&self, state: &mut S) -> Result<(), TransactionError> { @@ -280,6 +295,21 @@ impl Declare { ), ) } + + pub(crate) fn create_for_simulation( + &self, + tx: Declare, + skip_validate: bool, + skip_execute: bool, + ) -> Transaction { + let tx = Declare { + skip_validate, + skip_execute, + ..tx + }; + + Transaction::Declare(tx) + } } // --------------- diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index 49ece6d72..a583580a3 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -28,7 +28,9 @@ use cairo_vm::felt::Felt252; use num_traits::Zero; use starknet_contract_class::EntryPointType; -#[derive(Debug)] +use super::Transaction; + +#[derive(Debug, Clone)] pub struct Deploy { pub hash_value: Felt252, pub version: Felt252, @@ -37,6 +39,8 @@ pub struct Deploy { pub contract_hash: ClassHash, pub constructor_calldata: Vec, pub tx_type: TransactionType, + pub skip_validate: bool, + pub skip_execute: bool, } impl Deploy { @@ -77,6 +81,8 @@ impl Deploy { contract_hash, constructor_calldata, tx_type: TransactionType::Deploy, + skip_validate: false, + skip_execute: false, }) } @@ -223,6 +229,21 @@ impl Deploy { ), ) } + + pub(crate) fn create_for_simulation( + &self, + tx: Deploy, + skip_validate: bool, + skip_execute: bool, + ) -> Transaction { + let tx = Deploy { + skip_validate, + skip_execute, + ..tx + }; + + Transaction::Deploy(tx) + } } #[cfg(test)] diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index bf9fec939..c8827c7c2 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -28,6 +28,8 @@ use getset::Getters; use num_traits::Zero; use starknet_contract_class::EntryPointType; +use super::Transaction; + #[derive(Debug, Getters, Clone)] pub struct InvokeFunction { #[getset(get = "pub")] @@ -326,12 +328,14 @@ impl InvokeFunction { tx: InvokeFunction, skip_validation: bool, skip_execute: bool, - ) -> InvokeFunction { - InvokeFunction { + ) -> Transaction { + let tx = InvokeFunction { skip_validation, skip_execute, ..tx - } + }; + + Transaction::InvokeFunction(tx) } pub(crate) fn simulate_transaction( diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index bdc850531..64ac0a79f 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -64,4 +64,39 @@ impl Transaction { Transaction::L1Handler(tx) => tx.execute(state, block_context, remaining_gas), } } + + pub fn create_for_simulation(&self, skip_validate: bool, skip_execute: bool) -> Self { + match self { + Transaction::Declare(tx) => { + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + } + Transaction::DeclareV2(_) => todo!(), + Transaction::Deploy(tx) => { + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + } + Transaction::DeployAccount(_) => todo!(), + Transaction::InvokeFunction(tx) => { + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + } + Transaction::L1Handler(_) => todo!(), + } + } + + pub fn simulate_transaction( + &self, + state: S, + block_context: BlockContext, + remaining_gas: u128, + ) -> Result { + match self { + Transaction::Declare(_) => todo!(), + Transaction::DeclareV2(_) => todo!(), + Transaction::Deploy(_) => todo!(), + Transaction::DeployAccount(_) => todo!(), + Transaction::InvokeFunction(tx) => { + tx.simulate_transaction(state, block_context, remaining_gas) + } + Transaction::L1Handler(_) => todo!(), + } + } } From b74cb995d1c70bac5b8039ff8a2008d2a1840a29 Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 28 Jun 2023 14:34:34 -0300 Subject: [PATCH 02/14] implement functions in deploy --- src/transaction/declare.rs | 13 ++++++++++++- src/transaction/deploy.rs | 15 ++++++++++++++- src/transaction/mod.rs | 4 ++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index db36cc7f9..a52887197 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -13,7 +13,7 @@ use crate::{ }, services::api::contract_classes::deprecated_contract_class::ContractClass, state::state_api::{State, StateReader}, - state::ExecutionResourcesManager, + state::{ExecutionResourcesManager, cached_state::CachedState}, transaction::{ error::TransactionError, fee::{calculate_tx_fee, execute_fee_transfer, FeeInfo}, @@ -310,6 +310,17 @@ impl Declare { Transaction::Declare(tx) } + + + pub(crate) fn simulate_transaction( + &self, + state: S, + block_context: BlockContext, + ) -> Result { + let mut cache_state = CachedState::new(state, None, None); + // init simulation + self.execute(&mut cache_state, &block_context) + } } // --------------- diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index a583580a3..e0037bbbc 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -19,7 +19,7 @@ use crate::{ }, }, state::state_api::{State, StateReader}, - state::ExecutionResourcesManager, + state::{cached_state::CachedState, ExecutionResourcesManager}, syscalls::syscall_handler_errors::SyscallHandlerError, transaction::error::TransactionError, utils::{calculate_tx_resources, felt_to_hash, Address, ClassHash}, @@ -230,6 +230,9 @@ impl Deploy { ) } +// --------------- +// Simulation +// --------------- pub(crate) fn create_for_simulation( &self, tx: Deploy, @@ -244,6 +247,16 @@ impl Deploy { Transaction::Deploy(tx) } + + pub(crate) fn simulate_transaction( + &self, + state: S, + block_context: BlockContext, + ) -> Result { + let mut cache_state = CachedState::new(state, None, None); + // init simulation + self.execute(&mut cache_state, &block_context) + } } #[cfg(test)] diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index 64ac0a79f..07490d1c2 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -89,9 +89,9 @@ impl Transaction { remaining_gas: u128, ) -> Result { match self { - Transaction::Declare(_) => todo!(), + Transaction::Declare(tx) => tx.simulate_transaction(state, block_context), Transaction::DeclareV2(_) => todo!(), - Transaction::Deploy(_) => todo!(), + Transaction::Deploy(tx) => tx.simulate_transaction(state, block_context), Transaction::DeployAccount(_) => todo!(), Transaction::InvokeFunction(tx) => { tx.simulate_transaction(state, block_context, remaining_gas) From bcd932e2c6eac5139c2c32aa0bf446a6aadd114b Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 28 Jun 2023 14:54:57 -0300 Subject: [PATCH 03/14] implement functions in deploy account --- src/transaction/declare.rs | 3 +- src/transaction/declare_v2.rs | 73 +++++++++++++++++++++++++------ src/transaction/deploy.rs | 6 +-- src/transaction/deploy_account.rs | 25 ++++++++--- src/transaction/mod.rs | 6 ++- 5 files changed, 88 insertions(+), 25 deletions(-) diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index a52887197..e44dcf2c9 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -13,7 +13,7 @@ use crate::{ }, services::api::contract_classes::deprecated_contract_class::ContractClass, state::state_api::{State, StateReader}, - state::{ExecutionResourcesManager, cached_state::CachedState}, + state::{cached_state::CachedState, ExecutionResourcesManager}, transaction::{ error::TransactionError, fee::{calculate_tx_fee, execute_fee_transfer, FeeInfo}, @@ -311,7 +311,6 @@ impl Declare { Transaction::Declare(tx) } - pub(crate) fn simulate_transaction( &self, state: S, diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index bf2ed2f40..2de33f689 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -10,7 +10,7 @@ use crate::{ TransactionExecutionContext, TransactionExecutionInfo, }, state::state_api::{State, StateReader}, - state::ExecutionResourcesManager, + state::{cached_state::CachedState, ExecutionResourcesManager}, transaction::{ error::TransactionError, fee::{calculate_tx_fee, execute_fee_transfer, FeeInfo}, @@ -24,7 +24,9 @@ use cairo_vm::felt::Felt252; use num_traits::Zero; use starknet_contract_class::EntryPointType; use std::collections::HashMap; -#[derive(Debug)] + +use super::Transaction; +#[derive(Debug, Clone)] pub struct DeclareV2 { pub sender_address: Address, pub tx_type: TransactionType, @@ -37,6 +39,8 @@ pub struct DeclareV2 { pub sierra_contract_class: SierraContractClass, pub hash_value: Felt252, pub casm_class: once_cell::unsync::OnceCell, + pub skip_validate: bool, + pub skip_execute: bool, } impl DeclareV2 { @@ -79,6 +83,8 @@ impl DeclareV2 { compiled_class_hash, hash_value, casm_class: Default::default(), + skip_execute: false, + skip_validate: false, }; internal_declare.verify_version()?; @@ -147,10 +153,18 @@ impl DeclareV2 { let mut tx_execution_context = self.get_execution_context(block_context.invoke_tx_max_n_steps); - let fee_transfer_info = - execute_fee_transfer(state, block_context, &mut tx_execution_context, actual_fee)?; + let fee_transfer_info = if self.skip_execute { + None + } else { + Some(execute_fee_transfer( + state, + block_context, + &mut tx_execution_context, + actual_fee, + )?) + }; - Ok((Some(fee_transfer_info), actual_fee)) + Ok((fee_transfer_info, actual_fee)) } // TODO: delete once used @@ -185,17 +199,22 @@ impl DeclareV2 { let mut resources_manager = ExecutionResourcesManager::default(); - let (validate_info, _remaining_gas) = self.run_validate_entrypoint( - initial_gas, - state, - &mut resources_manager, - block_context, - )?; + let (validate_info, _remaining_gas) = if self.skip_validate { + (None, 0) + } else { + let (info, gas) = self.run_validate_entrypoint( + initial_gas, + state, + &mut resources_manager, + block_context, + )?; + (Some(info), gas) + }; let storage_changes = state.count_actual_storage_changes(); let actual_resources = calculate_tx_resources( resources_manager, - &[Some(validate_info.clone())], + &[validate_info.clone()], self.tx_type, storage_changes, None, @@ -207,7 +226,7 @@ impl DeclareV2 { self.charge_fee(state, &actual_resources, block_context)?; let concurrent_exec_info = TransactionExecutionInfo::create_concurrent_stage_execution_info( - Some(validate_info), + validate_info, None, actual_resources, Some(self.tx_type), @@ -276,6 +295,34 @@ impl DeclareV2 { Ok((call_info, remaining_gas)) } + + // --------------- + // Simulation + // --------------- + pub(crate) fn create_for_simulation( + &self, + tx: DeclareV2, + skip_validate: bool, + skip_execute: bool, + ) -> Transaction { + let tx = DeclareV2 { + skip_validate, + skip_execute, + ..tx + }; + + Transaction::DeclareV2(Box::new(tx)) + } + + pub(crate) fn simulate_transaction( + &self, + state: S, + block_context: BlockContext, + ) -> Result { + let mut cache_state = CachedState::new(state, None, None); + // init simulation + self.execute(&mut cache_state, &block_context) + } } #[cfg(test)] diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index e0037bbbc..c1f4bae6d 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -230,9 +230,9 @@ impl Deploy { ) } -// --------------- -// Simulation -// --------------- + // --------------- + // Simulation + // --------------- pub(crate) fn create_for_simulation( &self, tx: Deploy, diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index 1e9a4973e..fb2bad5e7 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -58,6 +58,8 @@ pub struct DeployAccount { hash_value: Felt252, #[getset(get = "pub")] signature: Vec, + skip_validate: bool, + skip_execute: bool, } impl DeployAccount { @@ -104,6 +106,8 @@ impl DeployAccount { max_fee, hash_value, signature, + skip_execute: false, + skip_validate: false, }) } @@ -169,8 +173,11 @@ impl DeployAccount { let constructor_call_info = self.handle_constructor(contract_class, state, block_context, &mut resources_manager)?; - let validate_info = - self.run_validate_entrypoint(state, &mut resources_manager, block_context)?; + let validate_info = if self.skip_validate { + None + } else { + self.run_validate_entrypoint(state, &mut resources_manager, block_context)? + }; let actual_resources = calculate_tx_resources( resources_manager, @@ -344,10 +351,18 @@ impl DeployAccount { let mut tx_execution_context = self.get_execution_context(block_context.invoke_tx_max_n_steps); - let fee_transfer_info = - execute_fee_transfer(state, block_context, &mut tx_execution_context, actual_fee)?; + let fee_transfer_info = if self.skip_execute { + None + } else { + Some(execute_fee_transfer( + state, + block_context, + &mut tx_execution_context, + actual_fee, + )?) + }; - Ok((Some(fee_transfer_info), actual_fee)) + Ok((fee_transfer_info, actual_fee)) } } diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index 07490d1c2..f14794b67 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -70,7 +70,9 @@ impl Transaction { Transaction::Declare(tx) => { tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) } - Transaction::DeclareV2(_) => todo!(), + Transaction::DeclareV2(tx) => { + tx.create_for_simulation(tx.as_ref().clone(), skip_validate, skip_execute) + } Transaction::Deploy(tx) => { tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) } @@ -90,7 +92,7 @@ impl Transaction { ) -> Result { match self { Transaction::Declare(tx) => tx.simulate_transaction(state, block_context), - Transaction::DeclareV2(_) => todo!(), + Transaction::DeclareV2(tx) => tx.simulate_transaction(state, block_context), Transaction::Deploy(tx) => tx.simulate_transaction(state, block_context), Transaction::DeployAccount(_) => todo!(), Transaction::InvokeFunction(tx) => { From c33004dc673ab9e79e18a57d04fd30aa18684108 Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 28 Jun 2023 15:11:20 -0300 Subject: [PATCH 04/14] implement functions in l1handler --- src/transaction/deploy_account.rs | 29 ++++++++++++++-- src/transaction/l1_handler.rs | 56 +++++++++++++++++++++++++------ src/transaction/mod.rs | 14 +++++--- 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index fb2bad5e7..bf0e68c6a 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -1,4 +1,4 @@ -use super::invoke_function::verify_no_calls_to_other_contracts; +use super::{invoke_function::verify_no_calls_to_other_contracts, Transaction}; use crate::{ core::{ errors::state_errors::StateError, @@ -21,7 +21,7 @@ use crate::{ contract_class_errors::ContractClassError, contract_classes::compiled_class::CompiledClass, }, state::state_api::{State, StateReader}, - state::ExecutionResourcesManager, + state::{cached_state::CachedState, ExecutionResourcesManager}, syscalls::syscall_handler_errors::SyscallHandlerError, transaction::{ error::TransactionError, @@ -364,6 +364,31 @@ impl DeployAccount { Ok((fee_transfer_info, actual_fee)) } + + pub(crate) fn create_for_simulation( + &self, + tx: DeployAccount, + skip_validate: bool, + skip_execute: bool, + ) -> Transaction { + let tx = DeployAccount { + skip_validate, + skip_execute, + ..tx + }; + + Transaction::DeployAccount(tx) + } + + pub(crate) fn simulate_transaction( + &self, + state: S, + block_context: BlockContext, + ) -> Result { + let mut cache_state = CachedState::new(state, None, None); + // init simulation + self.execute(&mut cache_state, &block_context) + } } #[cfg(test)] diff --git a/src/transaction/l1_handler.rs b/src/transaction/l1_handler.rs index 71fa12702..24d18dd2b 100644 --- a/src/transaction/l1_handler.rs +++ b/src/transaction/l1_handler.rs @@ -14,6 +14,7 @@ use crate::{ TransactionExecutionInfo, }, state::{ + cached_state::CachedState, state_api::{State, StateReader}, ExecutionResourcesManager, }, @@ -21,8 +22,10 @@ use crate::{ utils::{calculate_tx_resources, Address}, }; +use super::Transaction; + #[allow(dead_code)] -#[derive(Debug, Getters)] +#[derive(Debug, Getters, Clone)] pub struct L1Handler { #[getset(get = "pub")] hash_value: Felt252, @@ -32,6 +35,8 @@ pub struct L1Handler { calldata: Vec, nonce: Option, paid_fee_on_l1: Option, + skip_validate: bool, + skip_execute: bool, } impl L1Handler { @@ -61,6 +66,8 @@ impl L1Handler { calldata, nonce: Some(nonce), paid_fee_on_l1, + skip_execute: false, + skip_validate: false, }) } @@ -86,18 +93,22 @@ impl L1Handler { remaining_gas, ); - let call_info = entrypoint.execute( - state, - block_context, - &mut resources_manager, - &mut self.get_execution_context(block_context.invoke_tx_max_n_steps)?, - false, - )?; + let call_info = if self.skip_execute { + None + } else { + Some(entrypoint.execute( + state, + block_context, + &mut resources_manager, + &mut self.get_execution_context(block_context.invoke_tx_max_n_steps)?, + false, + )?) + }; let changes = state.count_actual_storage_changes(); let actual_resources = calculate_tx_resources( resources_manager, - &[Some(call_info.clone())], + &[call_info.clone()], TransactionType::L1Handler, changes, Some(self.get_payload_size()), @@ -126,7 +137,7 @@ impl L1Handler { Ok( TransactionExecutionInfo::create_concurrent_stage_execution_info( None, - Some(call_info), + call_info, actual_resources, Some(TransactionType::L1Handler), ), @@ -155,6 +166,31 @@ impl L1Handler { L1_HANDLER_VERSION.into(), )) } + pub(crate) fn create_for_simulation( + &self, + tx: L1Handler, + skip_validate: bool, + skip_execute: bool, + ) -> Transaction { + let tx = L1Handler { + skip_validate, + skip_execute, + ..tx + }; + + Transaction::L1Handler(tx) + } + + pub(crate) fn simulate_transaction( + &self, + state: S, + block_context: BlockContext, + remaining_gas: u128, + ) -> Result { + let mut cache_state = CachedState::new(state, None, None); + // init simulation + self.execute(&mut cache_state, &block_context, remaining_gas) + } } #[cfg(test)] diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index f14794b67..f9cd0e776 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -76,11 +76,15 @@ impl Transaction { Transaction::Deploy(tx) => { tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) } - Transaction::DeployAccount(_) => todo!(), + Transaction::DeployAccount(tx) => { + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + } Transaction::InvokeFunction(tx) => { tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) } - Transaction::L1Handler(_) => todo!(), + Transaction::L1Handler(tx) => { + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + } } } @@ -94,11 +98,13 @@ impl Transaction { Transaction::Declare(tx) => tx.simulate_transaction(state, block_context), Transaction::DeclareV2(tx) => tx.simulate_transaction(state, block_context), Transaction::Deploy(tx) => tx.simulate_transaction(state, block_context), - Transaction::DeployAccount(_) => todo!(), + Transaction::DeployAccount(tx) => tx.simulate_transaction(state, block_context), Transaction::InvokeFunction(tx) => { tx.simulate_transaction(state, block_context, remaining_gas) } - Transaction::L1Handler(_) => todo!(), + Transaction::L1Handler(tx) => { + tx.simulate_transaction(state, block_context, remaining_gas) + } } } } From e997cfd7d29a30233880b5a9f041f8323b4cd88f Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 28 Jun 2023 15:48:13 -0300 Subject: [PATCH 05/14] avoid execution --- src/transaction/declare_v2.rs | 21 +++++++++------- src/transaction/deploy_account.rs | 40 ++++++++++++++++++------------ src/transaction/error.rs | 2 ++ src/transaction/invoke_function.rs | 32 ++++++++++++++++-------- 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index 2de33f689..9559f4c6c 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -282,16 +282,19 @@ impl DeclareV2 { let mut tx_execution_context = self.get_execution_context(block_context.validate_max_n_steps); - let call_info = entry_point.execute( - state, - block_context, - resources_manager, - &mut tx_execution_context, - false, - )?; - + let call_info = if self.skip_execute { + None + } else { + Some(entry_point.execute( + state, + block_context, + resources_manager, + &mut tx_execution_context, + false, + )?) + }; + let call_info = verify_no_calls_to_other_contracts(&call_info)?; remaining_gas -= call_info.gas_consumed; - verify_no_calls_to_other_contracts(&call_info)?; Ok((call_info, remaining_gas)) } diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index bf0e68c6a..5ded126cd 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -261,15 +261,19 @@ impl DeployAccount { INITIAL_GAS_COST, ); - let call_info = entry_point.execute( - state, - block_context, - resources_manager, - &mut self.get_execution_context(block_context.validate_max_n_steps), - false, - )?; + let call_info = if self.skip_execute { + None + } else { + Some(entry_point.execute( + state, + block_context, + resources_manager, + &mut self.get_execution_context(block_context.validate_max_n_steps), + false, + )?) + }; - verify_no_calls_to_other_contracts(&call_info) + let call_info = verify_no_calls_to_other_contracts(&call_info) .map_err(|_| TransactionError::InvalidContractCall)?; Ok(call_info) } @@ -316,18 +320,22 @@ impl DeployAccount { INITIAL_GAS_COST, ); - let call_info = call.execute( - state, - block_context, - resources_manager, - &mut self.get_execution_context(block_context.validate_max_n_steps), - false, - )?; + let call_info = if self.skip_execute { + None + } else { + Some(call.execute( + state, + block_context, + resources_manager, + &mut self.get_execution_context(block_context.validate_max_n_steps), + false, + )?) + }; verify_no_calls_to_other_contracts(&call_info) .map_err(|_| TransactionError::InvalidContractCall)?; - Ok(Some(call_info)) + Ok(call_info) } fn charge_fee( diff --git a/src/transaction/error.rs b/src/transaction/error.rs index 7243a730a..53787a63f 100644 --- a/src/transaction/error.rs +++ b/src/transaction/error.rs @@ -127,4 +127,6 @@ pub enum TransactionError { InvalidBlockTimestamp, #[error("{0:?}")] CustomError(String), + #[error("call info es None")] + CallInfoIsNone, } diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index c8827c7c2..c0971dd22 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -150,15 +150,19 @@ impl InvokeFunction { 0, ); - let call_info = call.execute( - state, - block_context, - resources_manager, - &mut self.get_execution_context(block_context.validate_max_n_steps)?, - false, - )?; + let call_info = if self.skip_execute { + None + } else { + Some(call.execute( + state, + block_context, + resources_manager, + &mut self.get_execution_context(block_context.validate_max_n_steps)?, + false, + )?) + }; - verify_no_calls_to_other_contracts(&call_info) + let call_info = verify_no_calls_to_other_contracts(&call_info) .map_err(|_| TransactionError::InvalidContractCall)?; Ok(Some(call_info)) @@ -354,14 +358,20 @@ impl InvokeFunction { // Invoke internal functions utils // ------------------------------------ -pub fn verify_no_calls_to_other_contracts(call_info: &CallInfo) -> Result<(), TransactionError> { +pub fn verify_no_calls_to_other_contracts( + call_info: &Option, +) -> Result { + if call_info.is_none() { + return Err(TransactionError::CallInfoIsNone); + }; + let call_info = call_info.clone().unwrap(); let invoked_contract_address = call_info.contract_address.clone(); for internal_call in call_info.gen_call_topology() { if internal_call.contract_address != invoked_contract_address { return Err(TransactionError::UnauthorizedActionOnValidate); } } - Ok(()) + Ok(call_info) } // Performs validation on fields related to function invocation transaction. @@ -981,7 +991,7 @@ mod tests { internal_calls.push(internal_call); call_info.internal_calls = internal_calls; - let expected_error = verify_no_calls_to_other_contracts(&call_info); + let expected_error = verify_no_calls_to_other_contracts(&Some(call_info)); assert!(expected_error.is_err()); assert_matches!( From b8d18f392aa2e627abebe9505db82b4de991cb07 Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 28 Jun 2023 20:01:36 -0300 Subject: [PATCH 06/14] Add skip_fee_transfer flag --- src/lib.rs | 33 ++++++++++++++++++++++++------ src/transaction/declare.rs | 6 +++++- src/transaction/declare_v2.rs | 6 +++++- src/transaction/deploy.rs | 4 ++++ src/transaction/deploy_account.rs | 6 +++++- src/transaction/invoke_function.rs | 15 +++++++++++++- src/transaction/mod.rs | 24 ++++++++++++++-------- tests/internals.rs | 9 ++++++++ 8 files changed, 85 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0c20f260e..41cc13197 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,8 +51,10 @@ pub fn simulate_transaction( remaining_gas: u128, skip_validate: bool, skip_execute: bool, + skip_fee_transfer: bool, ) -> Result { - let tx_for_simulation = transaction.create_for_simulation(skip_validate, skip_execute); + let tx_for_simulation = + transaction.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer); tx_for_simulation.simulate_transaction(state, block_context, remaining_gas) } @@ -356,7 +358,10 @@ mod test { .unwrap(); let block_context = BlockContext::default(); - let simul_invoke = invoke.create_for_simulation(invoke.clone(), true, false); + let Transaction::InvokeFunction(simul_invoke) = + invoke.create_for_simulation(invoke.clone(), true, false, false) else { + unreachable!() + }; let call_info = simul_invoke .run_validate_entrypoint( @@ -440,8 +445,16 @@ mod test { let block_context = BlockContext::default(); - let context = - simulate_transaction(&invoke, state_reader, block_context, 1000, false, true).unwrap(); + let context = simulate_transaction( + &Transaction::InvokeFunction(invoke), + state_reader, + block_context, + 1000, + false, + true, + true, + ) + .unwrap(); assert!(context.validate_info.is_some()); assert!(context.call_info.is_none()); @@ -518,8 +531,16 @@ mod test { let block_context = BlockContext::default(); - let context = - simulate_transaction(&invoke, state_reader, block_context, 1000, true, true).unwrap(); + let context = simulate_transaction( + &Transaction::InvokeFunction(invoke), + state_reader, + block_context, + 1000, + true, + true, + true, + ) + .unwrap(); assert!(context.validate_info.is_none()); assert!(context.call_info.is_none()); diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index e44dcf2c9..c35bedc5a 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -47,6 +47,7 @@ pub struct Declare { pub contract_class: ContractClass, pub skip_validate: bool, pub skip_execute: bool, + pub skip_fee_transfer: bool, } // ------------------------------------------------------------ @@ -94,6 +95,7 @@ impl Declare { contract_class, skip_execute: false, skip_validate: false, + skip_fee_transfer: false, }; internal_declare.verify_version()?; @@ -230,7 +232,7 @@ impl Declare { let mut tx_execution_context = self.get_execution_context(block_context.invoke_tx_max_n_steps); - let fee_transfer_info = if self.skip_execute { + let fee_transfer_info = if self.skip_fee_transfer { None } else { Some(execute_fee_transfer( @@ -301,10 +303,12 @@ impl Declare { tx: Declare, skip_validate: bool, skip_execute: bool, + skip_fee_transfer: bool, ) -> Transaction { let tx = Declare { skip_validate, skip_execute, + skip_fee_transfer, ..tx }; diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index 9559f4c6c..fbb469b07 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -41,6 +41,7 @@ pub struct DeclareV2 { pub casm_class: once_cell::unsync::OnceCell, pub skip_validate: bool, pub skip_execute: bool, + pub skip_fee_transfer: bool, } impl DeclareV2 { @@ -85,6 +86,7 @@ impl DeclareV2 { casm_class: Default::default(), skip_execute: false, skip_validate: false, + skip_fee_transfer: false, }; internal_declare.verify_version()?; @@ -153,7 +155,7 @@ impl DeclareV2 { let mut tx_execution_context = self.get_execution_context(block_context.invoke_tx_max_n_steps); - let fee_transfer_info = if self.skip_execute { + let fee_transfer_info = if self.skip_fee_transfer { None } else { Some(execute_fee_transfer( @@ -307,10 +309,12 @@ impl DeclareV2 { tx: DeclareV2, skip_validate: bool, skip_execute: bool, + skip_fee_transfer: bool, ) -> Transaction { let tx = DeclareV2 { skip_validate, skip_execute, + skip_fee_transfer, ..tx }; diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index c1f4bae6d..6f31c9fb7 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -41,6 +41,7 @@ pub struct Deploy { pub tx_type: TransactionType, pub skip_validate: bool, pub skip_execute: bool, + pub skip_fee_transfer: bool, } impl Deploy { @@ -83,6 +84,7 @@ impl Deploy { tx_type: TransactionType::Deploy, skip_validate: false, skip_execute: false, + skip_fee_transfer: false, }) } @@ -238,10 +240,12 @@ impl Deploy { tx: Deploy, skip_validate: bool, skip_execute: bool, + skip_fee_transfer: bool, ) -> Transaction { let tx = Deploy { skip_validate, skip_execute, + skip_fee_transfer, ..tx }; diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index 5ded126cd..ef0565cbd 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -60,6 +60,7 @@ pub struct DeployAccount { signature: Vec, skip_validate: bool, skip_execute: bool, + skip_fee_transfer: bool, } impl DeployAccount { @@ -108,6 +109,7 @@ impl DeployAccount { signature, skip_execute: false, skip_validate: false, + skip_fee_transfer: false, }) } @@ -359,7 +361,7 @@ impl DeployAccount { let mut tx_execution_context = self.get_execution_context(block_context.invoke_tx_max_n_steps); - let fee_transfer_info = if self.skip_execute { + let fee_transfer_info = if self.skip_fee_transfer { None } else { Some(execute_fee_transfer( @@ -378,10 +380,12 @@ impl DeployAccount { tx: DeployAccount, skip_validate: bool, skip_execute: bool, + skip_fee_transfer: bool, ) -> Transaction { let tx = DeployAccount { skip_validate, skip_execute, + skip_fee_transfer, ..tx }; diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index c0971dd22..5122a49f9 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -49,6 +49,7 @@ pub struct InvokeFunction { nonce: Option, skip_validation: bool, skip_execute: bool, + skip_fee_transfer: bool, } impl InvokeFunction { @@ -98,6 +99,7 @@ impl InvokeFunction { hash_value, skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }) } @@ -262,7 +264,7 @@ impl InvokeFunction { let mut tx_execution_context = self.get_execution_context(block_context.invoke_tx_max_n_steps)?; - let fee_transfer_info = if self.skip_execute { + let fee_transfer_info = if self.skip_fee_transfer { None } else { Some(execute_fee_transfer( @@ -332,10 +334,12 @@ impl InvokeFunction { tx: InvokeFunction, skip_validation: bool, skip_execute: bool, + skip_fee_transfer: bool, ) -> Transaction { let tx = InvokeFunction { skip_validation, skip_execute, + skip_fee_transfer, ..tx }; @@ -434,6 +438,7 @@ mod tests { nonce: Some(0.into()), skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -502,6 +507,7 @@ mod tests { nonce: Some(0.into()), skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -566,6 +572,7 @@ mod tests { nonce: Some(0.into()), skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -624,6 +631,7 @@ mod tests { nonce: None, skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -688,6 +696,7 @@ mod tests { nonce: None, skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -744,6 +753,7 @@ mod tests { nonce: Some(0.into()), skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -805,6 +815,7 @@ mod tests { nonce: Some(0.into()), skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -867,6 +878,7 @@ mod tests { nonce: Some(0.into()), skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState @@ -929,6 +941,7 @@ mod tests { nonce: None, skip_validation: false, skip_execute: false, + skip_fee_transfer: false, }; // Instantiate CachedState diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index f9cd0e776..d6164147d 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -65,22 +65,30 @@ impl Transaction { } } - pub fn create_for_simulation(&self, skip_validate: bool, skip_execute: bool) -> Self { + pub fn create_for_simulation( + &self, + skip_validate: bool, + skip_execute: bool, + skip_fee_transfer: bool, + ) -> Self { match self { Transaction::Declare(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) - } - Transaction::DeclareV2(tx) => { - tx.create_for_simulation(tx.as_ref().clone(), skip_validate, skip_execute) + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) } + Transaction::DeclareV2(tx) => tx.create_for_simulation( + tx.as_ref().clone(), + skip_validate, + skip_execute, + skip_fee_transfer, + ), Transaction::Deploy(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) } Transaction::DeployAccount(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) } Transaction::InvokeFunction(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) + tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) } Transaction::L1Handler(tx) => { tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) diff --git a/tests/internals.rs b/tests/internals.rs index 8a15b83b4..069b690f0 100644 --- a/tests/internals.rs +++ b/tests/internals.rs @@ -691,6 +691,9 @@ fn declare_tx() -> Declare { signature: vec![], nonce: 0.into(), hash_value: 0.into(), + skip_execute: false, + skip_fee_transfer: false, + skip_validate: false, } } @@ -710,6 +713,9 @@ fn declarev2_tx() -> DeclareV2 { compiled_class_hash: TEST_FIB_COMPILED_CONTRACT_CLASS_HASH.clone(), sierra_contract_class, casm_class: Default::default(), + skip_execute: false, + skip_fee_transfer: false, + skip_validate: false, } } @@ -722,6 +728,9 @@ fn deploy_fib_syscall() -> Deploy { contract_hash: felt_to_hash(&TEST_FIB_COMPILED_CONTRACT_CLASS_HASH.clone()), constructor_calldata: Vec::new(), tx_type: TransactionType::Deploy, + skip_execute: false, + skip_fee_transfer: false, + skip_validate: false, } } From 9dd58cdfaae21366c79f8abfcc02467b64198587 Mon Sep 17 00:00:00 2001 From: Milton Date: Wed, 28 Jun 2023 20:14:59 -0300 Subject: [PATCH 07/14] Fix broken test --- src/transaction/error.rs | 2 +- src/transaction/invoke_function.rs | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/transaction/error.rs b/src/transaction/error.rs index 53787a63f..c330e3878 100644 --- a/src/transaction/error.rs +++ b/src/transaction/error.rs @@ -127,6 +127,6 @@ pub enum TransactionError { InvalidBlockTimestamp, #[error("{0:?}")] CustomError(String), - #[error("call info es None")] + #[error("call info is None")] CallInfoIsNone, } diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index 5122a49f9..863d713ad 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -152,17 +152,13 @@ impl InvokeFunction { 0, ); - let call_info = if self.skip_execute { - None - } else { - Some(call.execute( - state, - block_context, - resources_manager, - &mut self.get_execution_context(block_context.validate_max_n_steps)?, - false, - )?) - }; + let call_info = Some(call.execute( + state, + block_context, + resources_manager, + &mut self.get_execution_context(block_context.validate_max_n_steps)?, + false, + )?); let call_info = verify_no_calls_to_other_contracts(&call_info) .map_err(|_| TransactionError::InvalidContractCall)?; From 0c813a11fd63eb1733afc0e83c5f35f7c9a54acd Mon Sep 17 00:00:00 2001 From: SantiagoPittella Date: Thu, 29 Jun 2023 12:23:40 -0300 Subject: [PATCH 08/14] remove simulate_Tx method, refactor create tx methods --- src/lib.rs | 5 +++-- src/transaction/declare.rs | 13 +----------- src/transaction/declare_v2.rs | 10 ---------- src/transaction/deploy.rs | 13 +----------- src/transaction/deploy_account.rs | 13 +----------- src/transaction/invoke_function.rs | 14 +------------ src/transaction/l1_handler.rs | 14 +------------ src/transaction/mod.rs | 32 +++++------------------------- 8 files changed, 13 insertions(+), 101 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 41cc13197..554737606 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,9 +53,10 @@ pub fn simulate_transaction( skip_execute: bool, skip_fee_transfer: bool, ) -> Result { + let mut cached_state = CachedState::new(state, None, None); let tx_for_simulation = transaction.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer); - tx_for_simulation.simulate_transaction(state, block_context, remaining_gas) + tx_for_simulation.execute(&mut cached_state, &block_context, remaining_gas) } /// Estimate the fee associated with transaction @@ -359,7 +360,7 @@ mod test { let block_context = BlockContext::default(); let Transaction::InvokeFunction(simul_invoke) = - invoke.create_for_simulation(invoke.clone(), true, false, false) else { + invoke.create_for_simulation(true, false, false) else { unreachable!() }; diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index c35bedc5a..09038116a 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -300,7 +300,6 @@ impl Declare { pub(crate) fn create_for_simulation( &self, - tx: Declare, skip_validate: bool, skip_execute: bool, skip_fee_transfer: bool, @@ -309,21 +308,11 @@ impl Declare { skip_validate, skip_execute, skip_fee_transfer, - ..tx + ..self.clone() }; Transaction::Declare(tx) } - - pub(crate) fn simulate_transaction( - &self, - state: S, - block_context: BlockContext, - ) -> Result { - let mut cache_state = CachedState::new(state, None, None); - // init simulation - self.execute(&mut cache_state, &block_context) - } } // --------------- diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index fbb469b07..498d0fb84 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -320,16 +320,6 @@ impl DeclareV2 { Transaction::DeclareV2(Box::new(tx)) } - - pub(crate) fn simulate_transaction( - &self, - state: S, - block_context: BlockContext, - ) -> Result { - let mut cache_state = CachedState::new(state, None, None); - // init simulation - self.execute(&mut cache_state, &block_context) - } } #[cfg(test)] diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index 6f31c9fb7..cacd343b5 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -237,7 +237,6 @@ impl Deploy { // --------------- pub(crate) fn create_for_simulation( &self, - tx: Deploy, skip_validate: bool, skip_execute: bool, skip_fee_transfer: bool, @@ -246,21 +245,11 @@ impl Deploy { skip_validate, skip_execute, skip_fee_transfer, - ..tx + ..self.clone() }; Transaction::Deploy(tx) } - - pub(crate) fn simulate_transaction( - &self, - state: S, - block_context: BlockContext, - ) -> Result { - let mut cache_state = CachedState::new(state, None, None); - // init simulation - self.execute(&mut cache_state, &block_context) - } } #[cfg(test)] diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index ef0565cbd..669daaf3a 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -377,7 +377,6 @@ impl DeployAccount { pub(crate) fn create_for_simulation( &self, - tx: DeployAccount, skip_validate: bool, skip_execute: bool, skip_fee_transfer: bool, @@ -386,21 +385,11 @@ impl DeployAccount { skip_validate, skip_execute, skip_fee_transfer, - ..tx + ..self.clone() }; Transaction::DeployAccount(tx) } - - pub(crate) fn simulate_transaction( - &self, - state: S, - block_context: BlockContext, - ) -> Result { - let mut cache_state = CachedState::new(state, None, None); - // init simulation - self.execute(&mut cache_state, &block_context) - } } #[cfg(test)] diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index 863d713ad..930edbef5 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -327,7 +327,6 @@ impl InvokeFunction { pub(crate) fn create_for_simulation( &self, - tx: InvokeFunction, skip_validation: bool, skip_execute: bool, skip_fee_transfer: bool, @@ -336,22 +335,11 @@ impl InvokeFunction { skip_validation, skip_execute, skip_fee_transfer, - ..tx + ..self.clone() }; Transaction::InvokeFunction(tx) } - - pub(crate) fn simulate_transaction( - &self, - state: S, - block_context: BlockContext, - remaining_gas: u128, - ) -> Result { - let mut cache_state = CachedState::new(state, None, None); - // init simulation - self.execute(&mut cache_state, &block_context, remaining_gas) - } } // ------------------------------------ diff --git a/src/transaction/l1_handler.rs b/src/transaction/l1_handler.rs index 24d18dd2b..1ab611094 100644 --- a/src/transaction/l1_handler.rs +++ b/src/transaction/l1_handler.rs @@ -168,29 +168,17 @@ impl L1Handler { } pub(crate) fn create_for_simulation( &self, - tx: L1Handler, skip_validate: bool, skip_execute: bool, ) -> Transaction { let tx = L1Handler { skip_validate, skip_execute, - ..tx + ..self.clone() }; Transaction::L1Handler(tx) } - - pub(crate) fn simulate_transaction( - &self, - state: S, - block_context: BlockContext, - remaining_gas: u128, - ) -> Result { - let mut cache_state = CachedState::new(state, None, None); - // init simulation - self.execute(&mut cache_state, &block_context, remaining_gas) - } } #[cfg(test)] diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index d6164147d..bbf8e40c3 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -73,7 +73,7 @@ impl Transaction { ) -> Self { match self { Transaction::Declare(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) + tx.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer) } Transaction::DeclareV2(tx) => tx.create_for_simulation( tx.as_ref().clone(), @@ -82,37 +82,15 @@ impl Transaction { skip_fee_transfer, ), Transaction::Deploy(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) + tx.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer) } Transaction::DeployAccount(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) + tx.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer) } Transaction::InvokeFunction(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute, skip_fee_transfer) - } - Transaction::L1Handler(tx) => { - tx.create_for_simulation(tx.clone(), skip_validate, skip_execute) - } - } - } - - pub fn simulate_transaction( - &self, - state: S, - block_context: BlockContext, - remaining_gas: u128, - ) -> Result { - match self { - Transaction::Declare(tx) => tx.simulate_transaction(state, block_context), - Transaction::DeclareV2(tx) => tx.simulate_transaction(state, block_context), - Transaction::Deploy(tx) => tx.simulate_transaction(state, block_context), - Transaction::DeployAccount(tx) => tx.simulate_transaction(state, block_context), - Transaction::InvokeFunction(tx) => { - tx.simulate_transaction(state, block_context, remaining_gas) - } - Transaction::L1Handler(tx) => { - tx.simulate_transaction(state, block_context, remaining_gas) + tx.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer) } + Transaction::L1Handler(tx) => tx.create_for_simulation(skip_validate, skip_execute), } } } From 56fd5b94ee3c894a6d564e34d04db01826ade710 Mon Sep 17 00:00:00 2001 From: SantiagoPittella Date: Thu, 29 Jun 2023 12:32:54 -0300 Subject: [PATCH 09/14] remove cached state imports --- src/transaction/declare.rs | 2 +- src/transaction/declare_v2.rs | 2 +- src/transaction/deploy.rs | 2 +- src/transaction/deploy_account.rs | 2 +- src/transaction/invoke_function.rs | 5 +---- src/transaction/l1_handler.rs | 1 - 6 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/transaction/declare.rs b/src/transaction/declare.rs index 09038116a..363100104 100644 --- a/src/transaction/declare.rs +++ b/src/transaction/declare.rs @@ -13,7 +13,7 @@ use crate::{ }, services::api::contract_classes::deprecated_contract_class::ContractClass, state::state_api::{State, StateReader}, - state::{cached_state::CachedState, ExecutionResourcesManager}, + state::ExecutionResourcesManager, transaction::{ error::TransactionError, fee::{calculate_tx_fee, execute_fee_transfer, FeeInfo}, diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index 498d0fb84..2d4fd6434 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -10,7 +10,7 @@ use crate::{ TransactionExecutionContext, TransactionExecutionInfo, }, state::state_api::{State, StateReader}, - state::{cached_state::CachedState, ExecutionResourcesManager}, + state::ExecutionResourcesManager, transaction::{ error::TransactionError, fee::{calculate_tx_fee, execute_fee_transfer, FeeInfo}, diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index cacd343b5..dafb7bdeb 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -19,7 +19,7 @@ use crate::{ }, }, state::state_api::{State, StateReader}, - state::{cached_state::CachedState, ExecutionResourcesManager}, + state::ExecutionResourcesManager, syscalls::syscall_handler_errors::SyscallHandlerError, transaction::error::TransactionError, utils::{calculate_tx_resources, felt_to_hash, Address, ClassHash}, diff --git a/src/transaction/deploy_account.rs b/src/transaction/deploy_account.rs index 669daaf3a..6179e14f0 100644 --- a/src/transaction/deploy_account.rs +++ b/src/transaction/deploy_account.rs @@ -21,7 +21,7 @@ use crate::{ contract_class_errors::ContractClassError, contract_classes::compiled_class::CompiledClass, }, state::state_api::{State, StateReader}, - state::{cached_state::CachedState, ExecutionResourcesManager}, + state::ExecutionResourcesManager, syscalls::syscall_handler_errors::SyscallHandlerError, transaction::{ error::TransactionError, diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index 930edbef5..e03af8f40 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -12,10 +12,7 @@ use crate::{ TransactionExecutionInfo, }, state::ExecutionResourcesManager, - state::{ - cached_state::CachedState, - state_api::{State, StateReader}, - }, + state::state_api::{State, StateReader}, transaction::{ error::TransactionError, fee::{calculate_tx_fee, execute_fee_transfer, FeeInfo}, diff --git a/src/transaction/l1_handler.rs b/src/transaction/l1_handler.rs index 1ab611094..fa2c37045 100644 --- a/src/transaction/l1_handler.rs +++ b/src/transaction/l1_handler.rs @@ -14,7 +14,6 @@ use crate::{ TransactionExecutionInfo, }, state::{ - cached_state::CachedState, state_api::{State, StateReader}, ExecutionResourcesManager, }, From 696f61d26c743e51ead7e742e64fe5e01987433b Mon Sep 17 00:00:00 2001 From: SantiagoPittella Date: Thu, 29 Jun 2023 15:45:02 -0300 Subject: [PATCH 10/14] add tests for invoke, declare, deploy and deploy_account --- src/lib.rs | 210 +++++++++++++++++++++++++++++ src/transaction/declare_v2.rs | 3 +- src/transaction/invoke_function.rs | 2 +- src/transaction/mod.rs | 9 +- 4 files changed, 215 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 554737606..dad477d85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,15 +172,23 @@ pub fn execute_transaction( #[cfg(test)] mod test { use std::collections::HashMap; + use std::fs::File; + use std::io::BufReader; use std::path::PathBuf; use crate::core::contract_address::compute_deprecated_class_hash; use crate::definitions::block_context::StarknetChainId; use crate::definitions::constants::EXECUTE_ENTRY_POINT_SELECTOR; + use crate::definitions::constants::VALIDATE_ENTRY_POINT_SELECTOR; use crate::estimate_fee; use crate::estimate_message_fee; + use crate::hash_utils::calculate_contract_address; use crate::services::api::contract_classes::deprecated_contract_class::ContractClass; + use crate::state::state_api::State; use crate::testing::{create_account_tx_test_state, TEST_CONTRACT_ADDRESS, TEST_CONTRACT_PATH}; + use crate::transaction::Declare; + use crate::transaction::Deploy; + use crate::transaction::DeployAccount; use crate::transaction::{InvokeFunction, L1Handler, Transaction}; use crate::utils::felt_to_hash; use cairo_lang_starknet::casm_contract_class::CasmContractClass; @@ -199,6 +207,27 @@ mod test { utils::{Address, ClassHash}, }; + use lazy_static::lazy_static; + + lazy_static! { + // include_str! doesn't seem to work in CI + static ref CONTRACT_CLASS: ContractClass = ContractClass::try_from(BufReader::new(File::open( + "starknet_programs/account_without_validation.json", + ).unwrap())) + .unwrap(); + static ref CLASS_HASH: Felt252 = compute_deprecated_class_hash(&CONTRACT_CLASS).unwrap(); + static ref CLASS_HASH_BYTES: [u8; 32] = CLASS_HASH.clone().to_be_bytes(); + static ref SALT: Felt252 = felt_str!( + "2669425616857739096022668060305620640217901643963991674344872184515580705509" + ); + static ref CONTRACT_ADDRESS: Address = Address(calculate_contract_address(&SALT.clone(), &CLASS_HASH.clone(), &[], Address(0.into())).unwrap()); + static ref SIGNATURE: Vec = vec![ + felt_str!("3233776396904427614006684968846859029149676045084089832563834729503047027074"), + felt_str!("707039245213420890976709143988743108543645298941971188668773816813012281203"), + ]; + pub static ref TRANSACTION_VERSION: Felt252 = 1.into(); + } + #[test] fn estimate_fee_test() { let contract_class: ContractClass = @@ -461,6 +490,7 @@ mod test { assert!(context.call_info.is_none()); assert!(context.fee_transfer_info.is_none()); } + #[test] fn test_skip_execute_and_validate_flags() { let path = PathBuf::from("starknet_programs/account_without_validation.json"); @@ -547,4 +577,184 @@ mod test { assert!(context.call_info.is_none()); assert!(context.fee_transfer_info.is_none()); } + + #[test] + fn test_simulate_deploy() { + let state_reader = InMemoryStateReader::default(); + let mut state = CachedState::new(state_reader, Some(Default::default()), None); + + state + .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .unwrap(); + + let block_context = Default::default(); + let salt = felt_str!( + "2669425616857739096022668060305620640217901643963991674344872184515580705509" + ); + // new consumes more execution time than raw struct instantiation + let internal_deploy = Transaction::Deploy( + Deploy::new( + salt, + CONTRACT_CLASS.clone(), + vec![], + StarknetChainId::TestNet.to_felt(), + 0.into(), + None, + ) + .unwrap(), + ); + + simulate_transaction( + &internal_deploy, + state, + block_context, + 100_000_000, + false, + false, + false, + ) + .unwrap(); + } + + #[test] + fn test_simulate_declare() { + let state_reader = InMemoryStateReader::default(); + let state = CachedState::new(state_reader, Some(Default::default()), None); + + let block_context = Default::default(); + + let class = CONTRACT_CLASS.clone(); + let address = CONTRACT_ADDRESS.clone(); + // new consumes more execution time than raw struct instantiation + let declare_tx = Transaction::Declare( + Declare::new( + class, + StarknetChainId::TestNet.to_felt(), + address, + 0, + 0.into(), + vec![], + Felt252::zero(), + None, + ) + .expect("couldn't create transaction"), + ); + + simulate_transaction( + &declare_tx, + state, + block_context, + 100_000_000, + false, + false, + false, + ) + .unwrap(); + } + + #[test] + fn test_simulate_invoke() { + let state_reader = InMemoryStateReader::default(); + let mut state = CachedState::new(state_reader, Some(Default::default()), None); + + state + .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .unwrap(); + + let block_context = Default::default(); + + let salt = felt_str!( + "2669425616857739096022668060305620640217901643963991674344872184515580705509" + ); + let class = CONTRACT_CLASS.clone(); + let deploy = Deploy::new( + salt, + class, + vec![], + StarknetChainId::TestNet.to_felt(), + 0.into(), + None, + ) + .unwrap(); + + let _deploy_exec_info = deploy.execute(&mut state, &block_context).unwrap(); + + let selector = VALIDATE_ENTRY_POINT_SELECTOR.clone(); + let calldata = vec![ + CONTRACT_ADDRESS.0.clone(), + selector.clone(), + Felt252::zero(), + ]; + // new consumes more execution time than raw struct instantiation + let invoke_tx = Transaction::InvokeFunction( + InvokeFunction::new( + CONTRACT_ADDRESS.clone(), + selector, + 0, + TRANSACTION_VERSION.clone(), + calldata, + SIGNATURE.clone(), + StarknetChainId::TestNet.to_felt(), + Some(Felt252::zero()), + None, + ) + .unwrap(), + ); + + simulate_transaction( + &invoke_tx, + state, + block_context, + 100_000_000, + false, + false, + false, + ) + .unwrap(); + } + + #[test] + fn test_simulate_deploy_account() { + let state_reader = InMemoryStateReader::default(); + let mut state = CachedState::new(state_reader, Some(Default::default()), None); + + state + .set_contract_class(&CLASS_HASH_BYTES, &CONTRACT_CLASS) + .unwrap(); + + let block_context = Default::default(); + + // new consumes more execution time than raw struct instantiation + let deploy_account_tx = &Transaction::DeployAccount( + DeployAccount::new( + *CLASS_HASH_BYTES, + 0, + 0.into(), + Felt252::zero(), + vec![], + SIGNATURE.clone(), + SALT.clone(), + StarknetChainId::TestNet.to_felt(), + None, + ) + .unwrap(), + ); + + simulate_transaction( + deploy_account_tx, + state, + block_context, + 100_000_000, + false, + false, + false, + ) + .unwrap(); + } + + #[test] + fn test_simulate_declare_v2() {} + + #[test] + fn test_simulate_l1_handler() {} } diff --git a/src/transaction/declare_v2.rs b/src/transaction/declare_v2.rs index 2d4fd6434..8cdca7afe 100644 --- a/src/transaction/declare_v2.rs +++ b/src/transaction/declare_v2.rs @@ -306,7 +306,6 @@ impl DeclareV2 { // --------------- pub(crate) fn create_for_simulation( &self, - tx: DeclareV2, skip_validate: bool, skip_execute: bool, skip_fee_transfer: bool, @@ -315,7 +314,7 @@ impl DeclareV2 { skip_validate, skip_execute, skip_fee_transfer, - ..tx + ..self.clone() }; Transaction::DeclareV2(Box::new(tx)) diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index e03af8f40..e159b68ae 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -11,8 +11,8 @@ use crate::{ execution_entry_point::ExecutionEntryPoint, CallInfo, TransactionExecutionContext, TransactionExecutionInfo, }, - state::ExecutionResourcesManager, state::state_api::{State, StateReader}, + state::ExecutionResourcesManager, transaction::{ error::TransactionError, fee::{calculate_tx_fee, execute_fee_transfer, FeeInfo}, diff --git a/src/transaction/mod.rs b/src/transaction/mod.rs index bbf8e40c3..827b72621 100644 --- a/src/transaction/mod.rs +++ b/src/transaction/mod.rs @@ -75,12 +75,9 @@ impl Transaction { Transaction::Declare(tx) => { tx.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer) } - Transaction::DeclareV2(tx) => tx.create_for_simulation( - tx.as_ref().clone(), - skip_validate, - skip_execute, - skip_fee_transfer, - ), + Transaction::DeclareV2(tx) => { + tx.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer) + } Transaction::Deploy(tx) => { tx.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer) } From cd4c5f834a30d56697c9d0ed1466558cbb884e6c Mon Sep 17 00:00:00 2001 From: SantiagoPittella Date: Thu, 29 Jun 2023 15:56:19 -0300 Subject: [PATCH 11/14] Add declare v2 simulation test --- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dad477d85..e603ef33d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ #![deny(warnings)] #![forbid(unsafe_code)] #![cfg_attr(coverage_nightly, feature(no_coverage))] +use std::collections::HashMap; + use crate::{ execution::{ execution_entry_point::ExecutionEntryPoint, CallType, TransactionExecutionContext, @@ -53,7 +55,7 @@ pub fn simulate_transaction( skip_execute: bool, skip_fee_transfer: bool, ) -> Result { - let mut cached_state = CachedState::new(state, None, None); + let mut cached_state = CachedState::new(state, None, Some(HashMap::new())); let tx_for_simulation = transaction.create_for_simulation(skip_validate, skip_execute, skip_fee_transfer); tx_for_simulation.execute(&mut cached_state, &block_context, remaining_gas) @@ -179,19 +181,25 @@ mod test { use crate::core::contract_address::compute_deprecated_class_hash; use crate::definitions::block_context::StarknetChainId; use crate::definitions::constants::EXECUTE_ENTRY_POINT_SELECTOR; + use crate::definitions::constants::VALIDATE_DECLARE_ENTRY_POINT_SELECTOR; use crate::definitions::constants::VALIDATE_ENTRY_POINT_SELECTOR; + use crate::definitions::transaction_type::TransactionType; use crate::estimate_fee; use crate::estimate_message_fee; use crate::hash_utils::calculate_contract_address; use crate::services::api::contract_classes::deprecated_contract_class::ContractClass; use crate::state::state_api::State; + use crate::testing::TEST_ACCOUNT_CONTRACT_ADDRESS; + use crate::testing::TEST_FIB_COMPILED_CONTRACT_CLASS_HASH; use crate::testing::{create_account_tx_test_state, TEST_CONTRACT_ADDRESS, TEST_CONTRACT_PATH}; use crate::transaction::Declare; + use crate::transaction::DeclareV2; use crate::transaction::Deploy; use crate::transaction::DeployAccount; use crate::transaction::{InvokeFunction, L1Handler, Transaction}; use crate::utils::felt_to_hash; use cairo_lang_starknet::casm_contract_class::CasmContractClass; + use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass; use cairo_vm::felt::{felt_str, Felt252}; use num_traits::{Num, One, Zero}; use starknet_contract_class::EntryPointType; @@ -752,8 +760,45 @@ mod test { .unwrap(); } + fn declarev2_tx() -> DeclareV2 { + let program_data = include_bytes!("../starknet_programs/cairo1/fibonacci.sierra"); + let sierra_contract_class: SierraContractClass = + serde_json::from_slice(program_data).unwrap(); + + DeclareV2 { + sender_address: TEST_ACCOUNT_CONTRACT_ADDRESS.clone(), + tx_type: TransactionType::Declare, + validate_entry_point_selector: VALIDATE_DECLARE_ENTRY_POINT_SELECTOR.clone(), + version: 1.into(), + max_fee: 2, + signature: vec![], + nonce: 0.into(), + hash_value: 0.into(), + compiled_class_hash: TEST_FIB_COMPILED_CONTRACT_CLASS_HASH.clone(), + sierra_contract_class, + casm_class: Default::default(), + skip_execute: false, + skip_fee_transfer: false, + skip_validate: false, + } + } + #[test] - fn test_simulate_declare_v2() {} + fn test_simulate_declare_v2() { + let (block_context, state) = create_account_tx_test_state().unwrap(); + let declare_tx = Transaction::DeclareV2(Box::new(declarev2_tx())); + + simulate_transaction( + &declare_tx, + state, + block_context, + 100_000_000, + false, + false, + false, + ) + .unwrap(); + } #[test] fn test_simulate_l1_handler() {} From d58d4654e7326d2bb99e7c54f89e352581a86db4 Mon Sep 17 00:00:00 2001 From: SantiagoPittella Date: Thu, 29 Jun 2023 16:04:09 -0300 Subject: [PATCH 12/14] Add l1 handler tx test --- src/lib.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e603ef33d..c02213023 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -801,5 +801,71 @@ mod test { } #[test] - fn test_simulate_l1_handler() {} + fn test_simulate_l1_handler() { + let l1_handler_tx = Transaction::L1Handler( + L1Handler::new( + Address(0.into()), + Felt252::from_str_radix( + "c73f681176fc7b3f9693986fd7b14581e8d540519e27400e88b8713932be01", + 16, + ) + .unwrap(), + vec![ + Felt252::from_str_radix("8359E4B0152ed5A731162D3c7B0D8D56edB165A0", 16) + .unwrap(), + 1.into(), + 10.into(), + ], + 0.into(), + 0.into(), + Some(10000.into()), + ) + .unwrap(), + ); + + // Instantiate CachedState + let mut state_reader = InMemoryStateReader::default(); + // Set contract_class + let class_hash = [1; 32]; + let contract_class = + ContractClass::try_from(PathBuf::from("starknet_programs/l1l2.json")).unwrap(); + // Set contact_state + let contract_address = Address(0.into()); + let nonce = Felt252::zero(); + + state_reader + .address_to_class_hash_mut() + .insert(contract_address.clone(), class_hash); + state_reader + .address_to_nonce + .insert(contract_address, nonce); + + let mut state = CachedState::new(state_reader.clone(), None, None); + + // Initialize state.contract_classes + state.set_contract_classes(HashMap::new()).unwrap(); + + state + .set_contract_class(&class_hash, &contract_class) + .unwrap(); + + let mut block_context = BlockContext::default(); + block_context.cairo_resource_fee_weights = HashMap::from([ + (String::from("l1_gas_usage"), 0.into()), + (String::from("pedersen_builtin"), 16.into()), + (String::from("range_check_builtin"), 70.into()), + ]); + block_context.starknet_os_config.gas_price = 1; + + simulate_transaction( + &l1_handler_tx, + state, + block_context, + 100_000_000, + false, + false, + false, + ) + .unwrap(); + } } From 3573a4f6416ac8a7d11b3b9316669ba59b774158 Mon Sep 17 00:00:00 2001 From: SantiagoPittella Date: Thu, 29 Jun 2023 16:08:03 -0300 Subject: [PATCH 13/14] re arrange imports --- src/lib.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c02213023..86b0ed1ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -179,24 +179,26 @@ mod test { use std::path::PathBuf; use crate::core::contract_address::compute_deprecated_class_hash; - use crate::definitions::block_context::StarknetChainId; - use crate::definitions::constants::EXECUTE_ENTRY_POINT_SELECTOR; - use crate::definitions::constants::VALIDATE_DECLARE_ENTRY_POINT_SELECTOR; - use crate::definitions::constants::VALIDATE_ENTRY_POINT_SELECTOR; - use crate::definitions::transaction_type::TransactionType; + use crate::definitions::{ + block_context::StarknetChainId, + constants::{ + EXECUTE_ENTRY_POINT_SELECTOR, VALIDATE_DECLARE_ENTRY_POINT_SELECTOR, + VALIDATE_ENTRY_POINT_SELECTOR, + }, + transaction_type::TransactionType, + }; use crate::estimate_fee; use crate::estimate_message_fee; use crate::hash_utils::calculate_contract_address; use crate::services::api::contract_classes::deprecated_contract_class::ContractClass; use crate::state::state_api::State; - use crate::testing::TEST_ACCOUNT_CONTRACT_ADDRESS; - use crate::testing::TEST_FIB_COMPILED_CONTRACT_CLASS_HASH; - use crate::testing::{create_account_tx_test_state, TEST_CONTRACT_ADDRESS, TEST_CONTRACT_PATH}; - use crate::transaction::Declare; - use crate::transaction::DeclareV2; - use crate::transaction::Deploy; - use crate::transaction::DeployAccount; - use crate::transaction::{InvokeFunction, L1Handler, Transaction}; + use crate::testing::{ + create_account_tx_test_state, TEST_ACCOUNT_CONTRACT_ADDRESS, TEST_CONTRACT_ADDRESS, + TEST_CONTRACT_PATH, TEST_FIB_COMPILED_CONTRACT_CLASS_HASH, + }; + use crate::transaction::{ + Declare, DeclareV2, Deploy, DeployAccount, InvokeFunction, L1Handler, Transaction, + }; use crate::utils::felt_to_hash; use cairo_lang_starknet::casm_contract_class::CasmContractClass; use cairo_lang_starknet::contract_class::ContractClass as SierraContractClass; From 475284157b62f32a975dae59960134da4f4dcdc8 Mon Sep 17 00:00:00 2001 From: SantiagoPittella Date: Fri, 30 Jun 2023 15:27:39 -0300 Subject: [PATCH 14/14] rmeove unwrap --- src/transaction/invoke_function.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index e159b68ae..96ab2bf4d 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -346,10 +346,7 @@ impl InvokeFunction { pub fn verify_no_calls_to_other_contracts( call_info: &Option, ) -> Result { - if call_info.is_none() { - return Err(TransactionError::CallInfoIsNone); - }; - let call_info = call_info.clone().unwrap(); + let call_info = call_info.clone().ok_or(TransactionError::CallInfoIsNone)?; let invoked_contract_address = call_info.contract_address.clone(); for internal_call in call_info.gen_call_topology() { if internal_call.contract_address != invoked_contract_address {