From 5f2855e8b03aa3f733cd9f4767e77cd854208985 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 24 Jul 2024 09:45:59 +0300 Subject: [PATCH] refactor: use deploy account transaction extension trait --- .../src/test_utils/deploy_account.rs | 2 +- .../src/transaction/account_transaction.rs | 1 + .../src/transaction/transaction_execution.rs | 1 + .../src/transaction/transactions.rs | 58 +++++++++++++++---- .../src/transaction/transactions_test.rs | 6 +- crates/gateway/src/utils.rs | 1 + .../src/py_deploy_account.rs | 5 +- 7 files changed, 59 insertions(+), 15 deletions(-) diff --git a/crates/blockifier/src/test_utils/deploy_account.rs b/crates/blockifier/src/test_utils/deploy_account.rs index f3157add88..966a3ff3ab 100644 --- a/crates/blockifier/src/test_utils/deploy_account.rs +++ b/crates/blockifier/src/test_utils/deploy_account.rs @@ -15,7 +15,7 @@ use starknet_api::transaction::{ }; use crate::test_utils::{default_testing_resource_bounds, NonceManager}; -use crate::transaction::transactions::DeployAccountTransaction; +use crate::transaction::transactions::{DeployAccountTransaction, DeployAccountTransactionExt}; #[derive(Clone)] pub struct DeployAccountTxArgs { diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 56ecba63bf..4f61132026 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -43,6 +43,7 @@ use crate::transaction::transaction_utils::update_remaining_gas; use crate::transaction::transactions::{ DeclareTransaction, DeployAccountTransaction, + DeployAccountTransactionExt, Executable, ExecutableTransaction, ExecutionFlags, diff --git a/crates/blockifier/src/transaction/transaction_execution.rs b/crates/blockifier/src/transaction/transaction_execution.rs index 9d0b209c7b..b559a62235 100644 --- a/crates/blockifier/src/transaction/transaction_execution.rs +++ b/crates/blockifier/src/transaction/transaction_execution.rs @@ -22,6 +22,7 @@ use crate::transaction::objects::{ use crate::transaction::transactions::{ DeclareTransaction, DeployAccountTransaction, + DeployAccountTransactionExt, Executable, ExecutableTransaction, ExecutionFlags, diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index 830efbd7b3..0f0fcf780c 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,20 @@ macro_rules! implement_inner_tx_getter_calls { }; } +macro_rules! trait_inner_tx_getter_calls { + ($(($field:ident, $field_type:ty)),*) => { + $(fn $field(&self) -> $field_type;)* + }; +} + +macro_rules! implement_trait_inner_tx_getter_calls { + ($(($field:ident, $field_type:ty)),*) => { + $(fn $field(&self) -> $field_type { + self.tx.$field().clone() + })* + }; +} + #[derive(Clone, Copy, Debug)] pub struct ExecutionFlags { pub charge_fee: bool, @@ -282,17 +297,36 @@ 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 type DeployAccountTransaction = InternalDeployAccountTransaction; + +/// A trait for extending the `DeployAccountTransaction` with additional functionality. +pub trait DeployAccountTransactionExt { + fn new( + deploy_account_tx: starknet_api::transaction::DeployAccountTransaction, + tx_hash: TransactionHash, + contract_address: ContractAddress, + ) -> Self; + + fn new_for_query( + deploy_account_tx: starknet_api::transaction::DeployAccountTransaction, + tx_hash: TransactionHash, + contract_address: ContractAddress, + ) -> Self; + + trait_inner_tx_getter_calls!( + (class_hash, ClassHash), + (constructor_calldata, Calldata), + (contract_address_salt, ContractAddressSalt), + (nonce, Nonce), + (signature, TransactionSignature) + ); + + fn tx(&self) -> &starknet_api::transaction::DeployAccountTransaction; } -impl DeployAccountTransaction { - pub fn new( +impl DeployAccountTransactionExt for DeployAccountTransaction { + fn new( deploy_account_tx: starknet_api::transaction::DeployAccountTransaction, tx_hash: TransactionHash, contract_address: ContractAddress, @@ -300,7 +334,7 @@ impl DeployAccountTransaction { Self { tx: deploy_account_tx, tx_hash, contract_address, only_query: false } } - pub fn new_for_query( + fn new_for_query( deploy_account_tx: starknet_api::transaction::DeployAccountTransaction, tx_hash: TransactionHash, contract_address: ContractAddress, @@ -308,7 +342,7 @@ impl DeployAccountTransaction { Self { tx: deploy_account_tx, tx_hash, contract_address, only_query: true } } - implement_inner_tx_getter_calls!( + implement_trait_inner_tx_getter_calls!( (class_hash, ClassHash), (constructor_calldata, Calldata), (contract_address_salt, ContractAddressSalt), @@ -316,7 +350,7 @@ impl DeployAccountTransaction { (signature, TransactionSignature) ); - pub fn tx(&self) -> &starknet_api::transaction::DeployAccountTransaction { + fn tx(&self) -> &starknet_api::transaction::DeployAccountTransaction { &self.tx } } diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 6e7ef6058c..87888c61e2 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -119,7 +119,11 @@ use crate::transaction::test_utils::{ VALID, }; use crate::transaction::transaction_types::TransactionType; -use crate::transaction::transactions::{ExecutableTransaction, L1HandlerTransaction}; +use crate::transaction::transactions::{ + DeployAccountTransactionExt, + ExecutableTransaction, + L1HandlerTransaction, +}; use crate::versioned_constants::VersionedConstants; use crate::{ check_transaction_execution_error_for_custom_hint, diff --git a/crates/gateway/src/utils.rs b/crates/gateway/src/utils.rs index bc3c99de0e..2a0ab32e74 100644 --- a/crates/gateway/src/utils.rs +++ b/crates/gateway/src/utils.rs @@ -3,6 +3,7 @@ use blockifier::transaction::account_transaction::AccountTransaction; use blockifier::transaction::transactions::{ DeclareTransaction as BlockifierDeclareTransaction, DeployAccountTransaction as BlockifierDeployAccountTransaction, + DeployAccountTransactionExt, InvokeTransaction as BlockifierInvokeTransaction, }; use starknet_api::core::{calculate_contract_address, ChainId, ClassHash, ContractAddress, Nonce}; diff --git a/crates/native_blockifier/src/py_deploy_account.rs b/crates/native_blockifier/src/py_deploy_account.rs index 2b2833472a..d463525a68 100644 --- a/crates/native_blockifier/src/py_deploy_account.rs +++ b/crates/native_blockifier/src/py_deploy_account.rs @@ -1,7 +1,10 @@ use std::sync::Arc; use blockifier::transaction::transaction_types::TransactionType; -use blockifier::transaction::transactions::DeployAccountTransaction; +use blockifier::transaction::transactions::{ + DeployAccountTransaction, + DeployAccountTransactionExt, +}; use pyo3::prelude::*; use starknet_api::core::{ClassHash, ContractAddress, Nonce}; use starknet_api::data_availability::DataAvailabilityMode;