Skip to content

Commit

Permalink
chore: refactor deploy account as wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 24, 2024
1 parent c13342e commit 7431abd
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/stack_trace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 32 additions & 19 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -283,32 +292,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 struct DeployAccountTransaction(pub InternalDeployAccountTransaction);

impl DeployAccountTransaction {
pub fn new(
deploy_account_tx: starknet_api::transaction::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(
deploy_account_tx: starknet_api::transaction::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),
Expand All @@ -317,7 +330,7 @@ impl DeployAccountTransaction {
);

pub fn tx(&self) -> &starknet_api::transaction::DeployAccountTransaction {
&self.tx
&self.0.tx
}
}

Expand All @@ -333,7 +346,7 @@ impl<S: State> Executable<S> 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(
Expand All @@ -353,15 +366,15 @@ impl<S: State> Executable<S> 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,
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down

0 comments on commit 7431abd

Please sign in to comment.