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 Aug 19, 2024
1 parent b6955bb commit 78b45af
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 24 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.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.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.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.contract_address(),
_ => unreachable!("deploy_account_tx is a DeployAccount"),
};
fund_account(chain_info, deploy_address, BALANCE * 2, &mut state.state);
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.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.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
36 changes: 23 additions & 13 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,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,
pub tx: starknet_api::executable_transaction::DeployAccountTransaction,
// Indicates the presence of the only_query bit in the version.
pub only_query: bool,
}
Expand All @@ -322,32 +320,44 @@ impl DeployAccountTransaction {
tx_hash: TransactionHash,
contract_address: ContractAddress,
) -> Self {
Self { tx: deploy_account_tx, tx_hash, contract_address, only_query: false }
Self {
tx: starknet_api::executable_transaction::DeployAccountTransaction {
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 {
tx: starknet_api::executable_transaction::DeployAccountTransaction {
tx: deploy_account_tx,
tx_hash,
contract_address,
},
only_query: true,
}
}

implement_inner_tx_getter_calls!(
(class_hash, ClassHash),
(constructor_calldata, Calldata),
(contract_address, ContractAddress),
(contract_address_salt, ContractAddressSalt),
(nonce, Nonce),
(signature, TransactionSignature),
(tx_hash, TransactionHash),
(version, TransactionVersion)
);

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

pub fn tx_hash(&self) -> TransactionHash {
self.tx_hash
self.tx.tx()
}
}

Expand All @@ -363,7 +373,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.contract_address(),
caller_address: ContractAddress::default(),
};
let call_info = execute_deployment(
Expand All @@ -387,11 +397,11 @@ impl TransactionInfoCreator for DeployAccountTransaction {
version: self.version(),
signature: self.signature(),
nonce: self.nonce(),
sender_address: self.contract_address,
sender_address: self.contract_address(),
only_query: self.only_query,
};

match &self.tx {
match &self.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 @@ -1304,7 +1304,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.contract_address();
let constructor_calldata = deploy_account.constructor_calldata();
let salt = deploy_account.contract_address_salt();

Expand Down Expand Up @@ -1472,7 +1472,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.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 @@ -145,7 +145,7 @@ pub fn get_sender_address(tx: &AccountTransaction) -> ContractAddress {
DeclareTransaction::V3(tx) => tx.sender_address,
_ => panic!("Unsupported transaction version"),
},
AccountTransaction::DeployAccount(tx) => tx.contract_address,
AccountTransaction::DeployAccount(tx) => tx.contract_address(),
AccountTransaction::Invoke(tx) => match &tx.tx {
InvokeTransaction::V3(tx) => tx.sender_address,
_ => panic!("Unsupported transaction version"),
Expand Down
43 changes: 41 additions & 2 deletions crates/starknet_api/src/executable_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
use serde::{Deserialize, Serialize};

use crate::contract_class::ClassInfo;
use crate::core::{ContractAddress, Nonce};
use crate::transaction::{Tip, TransactionHash};
use crate::core::{ClassHash, ContractAddress, Nonce};
use crate::transaction::{
Calldata,
ContractAddressSalt,
Tip,
TransactionHash,
TransactionSignature,
TransactionVersion,
};

macro_rules! implement_inner_tx_getter_calls {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
self.tx.$field().clone()
})*
};
}

macro_rules! implement_getter_calls {
($(($field:ident, $field_type:ty)),*) => {
$(pub fn $field(&self) -> $field_type {
self.$field
})*
};
}

/// Represents a paid Starknet transaction.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
Expand Down Expand Up @@ -70,6 +93,22 @@ pub struct DeployAccountTransaction {
pub contract_address: ContractAddress,
}

impl DeployAccountTransaction {
implement_inner_tx_getter_calls!(
(class_hash, ClassHash),
(constructor_calldata, Calldata),
(contract_address_salt, ContractAddressSalt),
(nonce, Nonce),
(signature, TransactionSignature),
(version, TransactionVersion)
);
implement_getter_calls!((tx_hash, TransactionHash), (contract_address, ContractAddress));

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

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct InvokeTransaction {
pub tx: crate::transaction::InvokeTransaction,
Expand Down

0 comments on commit 78b45af

Please sign in to comment.