From 7431abd0f1927147c1bc55a17f0043962b41c472 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 23 Jul 2024 14:57:06 +0300 Subject: [PATCH] chore: refactor deploy account as wrapper --- .../src/blockifier/stateful_validator_test.rs | 2 +- .../src/concurrency/versioned_state_test.rs | 2 +- .../src/execution/stack_trace_test.rs | 4 +- .../src/transaction/account_transaction.rs | 2 +- .../transaction/account_transactions_test.rs | 2 +- .../blockifier/src/transaction/test_utils.rs | 2 +- .../src/transaction/transactions.rs | 51 ++++++++++++------- .../src/transaction/transactions_test.rs | 4 +- crates/gateway/src/utils.rs | 2 +- 9 files changed, 42 insertions(+), 29 deletions(-) diff --git a/crates/blockifier/src/blockifier/stateful_validator_test.rs b/crates/blockifier/src/blockifier/stateful_validator_test.rs index dc1fd08cc3..756dcf430b 100644 --- a/crates/blockifier/src/blockifier/stateful_validator_test.rs +++ b/crates/blockifier/src/blockifier/stateful_validator_test.rs @@ -63,7 +63,7 @@ fn test_transaction_validator( ..transaction_args }); if let AccountTransaction::DeployAccount(deploy_tx) = &tx { - fund_account(chain_info, deploy_tx.contract_address, BALANCE, &mut state.state); + fund_account(chain_info, deploy_tx.0.contract_address, BALANCE, &mut state.state); } // Test the stateful validator. diff --git a/crates/blockifier/src/concurrency/versioned_state_test.rs b/crates/blockifier/src/concurrency/versioned_state_test.rs index 79cac9ee12..32bf209c0c 100644 --- a/crates/blockifier/src/concurrency/versioned_state_test.rs +++ b/crates/blockifier/src/concurrency/versioned_state_test.rs @@ -246,7 +246,7 @@ fn test_run_parallel_txs(max_resource_bounds: ResourceBoundsMapping) { }; let nonce_manager = &mut NonceManager::default(); let deploy_account_tx_2 = deploy_account_tx(deploy_tx_args, nonce_manager); - let account_address = deploy_account_tx_2.contract_address; + let account_address = deploy_account_tx_2.0.contract_address; let account_tx_2 = AccountTransaction::DeployAccount(deploy_account_tx_2); let tx_context = block_context.to_tx_context(&account_tx_2); let fee_type = tx_context.tx_info.fee_type(); diff --git a/crates/blockifier/src/execution/stack_trace_test.rs b/crates/blockifier/src/execution/stack_trace_test.rs index 6aa7523507..2023bdf6d6 100644 --- a/crates/blockifier/src/execution/stack_trace_test.rs +++ b/crates/blockifier/src/execution/stack_trace_test.rs @@ -491,7 +491,7 @@ fn test_validate_trace( // Deploy account uses the actual address as the sender address. match &account_tx { AccountTransaction::DeployAccount(tx) => { - sender_address = tx.contract_address; + sender_address = tx.0.contract_address; } _ => panic!("Expected DeployAccountTransaction type"), } @@ -558,7 +558,7 @@ fn test_account_ctor_frame_stack_trace( // Fund the account so it can afford the deployment. let deploy_address = match &deploy_account_tx { - AccountTransaction::DeployAccount(deploy_tx) => deploy_tx.contract_address, + AccountTransaction::DeployAccount(deploy_tx) => deploy_tx.0.contract_address, _ => unreachable!("deploy_account_tx is a DeployAccount"), }; fund_account(chain_info, deploy_address, BALANCE * 2, &mut state.state); diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 56ecba63bf..c643198089 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -74,7 +74,7 @@ impl HasRelatedFeeType for AccountTransaction { fn version(&self) -> TransactionVersion { match self { Self::Declare(tx) => tx.tx.version(), - Self::DeployAccount(tx) => tx.tx.version(), + Self::DeployAccount(tx) => tx.0.tx.version(), Self::Invoke(tx) => match tx.tx { starknet_api::transaction::InvokeTransaction::V0(_) => TransactionVersion::ZERO, starknet_api::transaction::InvokeTransaction::V1(_) => TransactionVersion::ONE, diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 5e99b92386..603679bcbe 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -599,7 +599,7 @@ fn test_fail_deploy_account( let fee_token_address = chain_info.fee_token_address(&deploy_account_tx.fee_type()); let deploy_address = match &deploy_account_tx { - AccountTransaction::DeployAccount(deploy_tx) => deploy_tx.contract_address, + AccountTransaction::DeployAccount(deploy_tx) => deploy_tx.0.contract_address, _ => unreachable!("deploy_account_tx is a DeployAccount"), }; fund_account(chain_info, deploy_address, BALANCE * 2, &mut state.state); diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index d6b1724e3a..a64a3fcb92 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -109,7 +109,7 @@ pub fn deploy_and_fund_account( ) -> (AccountTransaction, ContractAddress) { // Deploy an account contract. let deploy_account_tx = deploy_account_tx(deploy_tx_args, nonce_manager); - let account_address = deploy_account_tx.contract_address; + let account_address = deploy_account_tx.0.contract_address; let account_tx = AccountTransaction::DeployAccount(deploy_account_tx); // Update the balance of the about-to-be deployed account contract in the erc20 contract, so it diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 830efbd7b3..1f8085779a 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -4,6 +4,7 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::calldata; use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::deprecated_contract_class::EntryPointType; +use starknet_api::internal_transaction::InternalDeployAccountTransaction; use starknet_api::transaction::{ AccountDeploymentData, Calldata, @@ -57,6 +58,14 @@ macro_rules! implement_inner_tx_getter_calls { }; } +macro_rules! implement_wrapped_inner_tx_getter_calls { + ($(($field:ident, $field_type:ty)),*) => { + $(pub fn $field(&self) -> $field_type { + self.0.tx.$field().clone() + })* + }; +} + #[derive(Clone, Copy, Debug)] pub struct ExecutionFlags { pub charge_fee: bool, @@ -283,13 +292,7 @@ impl TransactionInfoCreator for DeclareTransaction { } } #[derive(Debug, Clone)] -pub struct DeployAccountTransaction { - pub tx: starknet_api::transaction::DeployAccountTransaction, - pub tx_hash: TransactionHash, - pub contract_address: ContractAddress, - // Indicates the presence of the only_query bit in the version. - pub only_query: bool, -} +pub struct DeployAccountTransaction(pub InternalDeployAccountTransaction); impl DeployAccountTransaction { pub fn new( @@ -297,7 +300,12 @@ impl DeployAccountTransaction { tx_hash: TransactionHash, contract_address: ContractAddress, ) -> Self { - Self { tx: deploy_account_tx, tx_hash, contract_address, only_query: false } + Self(InternalDeployAccountTransaction { + tx: deploy_account_tx, + tx_hash, + contract_address, + only_query: false, + }) } pub fn new_for_query( @@ -305,10 +313,15 @@ impl DeployAccountTransaction { tx_hash: TransactionHash, contract_address: ContractAddress, ) -> Self { - Self { tx: deploy_account_tx, tx_hash, contract_address, only_query: true } + Self(InternalDeployAccountTransaction { + tx: deploy_account_tx, + tx_hash, + contract_address, + only_query: true, + }) } - implement_inner_tx_getter_calls!( + implement_wrapped_inner_tx_getter_calls!( (class_hash, ClassHash), (constructor_calldata, Calldata), (contract_address_salt, ContractAddressSalt), @@ -317,7 +330,7 @@ impl DeployAccountTransaction { ); pub fn tx(&self) -> &starknet_api::transaction::DeployAccountTransaction { - &self.tx + &self.0.tx } } @@ -333,7 +346,7 @@ impl Executable for DeployAccountTransaction { let ctor_context = ConstructorContext { class_hash, code_address: None, - storage_address: self.contract_address, + storage_address: self.0.contract_address, caller_address: ContractAddress::default(), }; let call_info = execute_deployment( @@ -353,15 +366,15 @@ impl Executable for DeployAccountTransaction { impl TransactionInfoCreator for DeployAccountTransaction { fn create_tx_info(&self) -> TransactionInfo { let common_fields = CommonAccountFields { - transaction_hash: self.tx_hash, - version: self.tx.version(), - signature: self.tx.signature(), - nonce: self.tx.nonce(), - sender_address: self.contract_address, - only_query: self.only_query, + transaction_hash: self.0.tx_hash, + version: self.0.tx.version(), + signature: self.0.tx.signature(), + nonce: self.0.tx.nonce(), + sender_address: self.0.contract_address, + only_query: self.0.only_query, }; - match &self.tx { + match &self.0.tx { starknet_api::transaction::DeployAccountTransaction::V1(tx) => { TransactionInfo::Deprecated(DeprecatedTransactionInfo { common_fields, diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 6e7ef6058c..732065c6de 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -1310,7 +1310,7 @@ fn test_deploy_account_tx( // Extract deploy account transaction fields for testing, as it is consumed when creating an // account transaction. let class_hash = deploy_account.class_hash(); - let deployed_account_address = deploy_account.contract_address; + let deployed_account_address = deploy_account.0.contract_address; let constructor_calldata = deploy_account.constructor_calldata(); let salt = deploy_account.contract_address_salt(); @@ -1482,7 +1482,7 @@ fn test_fail_deploy_account_undeclared_class_hash( state .set_storage_at( chain_info.fee_token_address(&fee_type), - get_fee_token_var_address(deploy_account.contract_address), + get_fee_token_var_address(deploy_account.0.contract_address), felt!(BALANCE), ) .unwrap(); diff --git a/crates/gateway/src/utils.rs b/crates/gateway/src/utils.rs index bc3c99de0e..30f859df22 100644 --- a/crates/gateway/src/utils.rs +++ b/crates/gateway/src/utils.rs @@ -166,7 +166,7 @@ pub fn external_tx_to_account_tx( pub fn get_tx_hash(tx: &AccountTransaction) -> TransactionHash { match tx { AccountTransaction::Declare(tx) => tx.tx_hash, - AccountTransaction::DeployAccount(tx) => tx.tx_hash, + AccountTransaction::DeployAccount(tx) => tx.0.tx_hash, AccountTransaction::Invoke(tx) => tx.tx_hash, } }