Skip to content

Commit

Permalink
chore(blockifier): add new constructor to AccountTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
avivg-starkware committed Dec 3, 2024
1 parent ccec3e2 commit 9f3f05f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 81 deletions.
8 changes: 3 additions & 5 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::test_utils::{
BALANCE,
TEST_ERC20_CONTRACT_ADDRESS2,
};
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{
account_invoke_tx,
Expand Down Expand Up @@ -549,7 +549,7 @@ fn test_deploy_before_declare(
let test_class_hash = test_contract.get_class_hash();
let test_class_info = calculate_class_info_for_testing(test_contract.get_class());
let test_compiled_class_hash = test_contract.get_compiled_class_hash();
let tx = declare_tx(
let declare_tx = AccountTransaction::new(declare_tx(
declare_tx_args! {
sender_address: account_address_0,
resource_bounds: default_all_resource_bounds,
Expand All @@ -560,9 +560,7 @@ fn test_deploy_before_declare(
nonce: nonce!(0_u8),
},
test_class_info.clone(),
);
let execution_flags = ExecutionFlags::default();
let declare_tx = AccountTransaction { tx, execution_flags };
));

// Deploy test contract.
let invoke_tx = account_invoke_tx(invoke_tx_args! {
Expand Down
6 changes: 5 additions & 1 deletion crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ macro_rules! implement_tx_getter_calls {
})*
};
}

impl AccountTransaction {
pub fn new(tx: Transaction) -> Self {
Self { tx, execution_flags: ExecutionFlags::default() }
}
}
impl HasRelatedFeeType for AccountTransaction {
fn version(&self) -> TransactionVersion {
self.tx.version()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,7 @@ fn test_max_fee_limit_validate(
},
class_info,
);
let execution_flags = AccountExecutionFlags::default();
let account_tx = AccountTransaction { tx, execution_flags };
let account_tx = AccountTransaction::new(tx);
account_tx.execute(&mut state, &block_context).unwrap();

// Deploy grindy account with a lot of grind in the constructor.
Expand Down Expand Up @@ -791,10 +790,8 @@ fn test_fail_declare(block_context: BlockContext, max_fee: Fee) {
tx_hash: TransactionHash::default(),
class_info,
};
let declare_account_tx = AccountTransaction {
tx: ApiExecutableTransaction::Declare(executable_declare),
execution_flags: AccountExecutionFlags::default(),
};
let declare_account_tx =
AccountTransaction::new(ApiExecutableTransaction::Declare(executable_declare));

// Fail execution, assert nonce and balance are unchanged.
let tx_info = declare_account_tx.create_tx_info();
Expand Down
6 changes: 2 additions & 4 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ pub fn deploy_and_fund_account(
deploy_tx_args: DeployAccountTxArgs,
) -> (AccountTransaction, ContractAddress) {
// Deploy an account contract.
let deploy_account_tx = AccountTransaction {
tx: deploy_account_tx(deploy_tx_args, nonce_manager),
execution_flags: ExecutionFlags::default(),
};
let deploy_account_tx =
AccountTransaction::new(deploy_account_tx(deploy_tx_args, nonce_manager));
let account_address = deploy_account_tx.sender_address();

// Update the balance of the about-to-be deployed account contract in the erc20 contract, so it
Expand Down
5 changes: 1 addition & 4 deletions crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ impl From<starknet_api::executable_transaction::Transaction> for Transaction {
fn from(value: starknet_api::executable_transaction::Transaction) -> Self {
match value {
starknet_api::executable_transaction::Transaction::Account(tx) => {
Transaction::Account(AccountTransaction {
tx,
execution_flags: AccountExecutionFlags::default(),
})
Transaction::Account(AccountTransaction::new(tx))
}
starknet_api::executable_transaction::Transaction::L1Handler(tx) => {
Transaction::L1Handler(tx)
Expand Down
103 changes: 42 additions & 61 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,11 @@ fn test_invoke_tx(
let test_contract_address = test_contract.get_instance_address(0);
let account_contract_address = account_contract.get_instance_address(0);
let calldata = create_trivial_calldata(test_contract_address);
let tx = invoke_tx(invoke_tx_args! {
let invoke_tx = AccountTransaction::new(invoke_tx(invoke_tx_args! {
sender_address: account_contract_address,
calldata: Calldata(Arc::clone(&calldata.0)),
resource_bounds,
});
let execution_flags = ExecutionFlags::default();
let invoke_tx = AccountTransaction { tx, execution_flags };
}));

// Extract invoke transaction fields for testing, as it is consumed when creating an account
// transaction.
Expand Down Expand Up @@ -969,16 +967,13 @@ fn test_max_fee_exceeds_balance(
)};

// Deploy.
let invalid_tx = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
resource_bounds,
class_hash: test_contract.get_class_hash()
},
&mut NonceManager::default(),
),
execution_flags: ExecutionFlags::default(),
};
let invalid_tx = AccountTransaction::new(deploy_account_tx(
deploy_account_tx_args! {
resource_bounds,
class_hash: test_contract.get_class_hash()
},
&mut NonceManager::default(),
));
assert_resource_bounds_exceed_balance_failure(state, block_context, invalid_tx);

// V1 Invoke.
Expand All @@ -1002,18 +997,15 @@ fn test_max_fee_exceeds_balance(
// Declare.
let contract_to_declare = FeatureContract::Empty(CairoVersion::Cairo1);
let class_info = calculate_class_info_for_testing(contract_to_declare.get_class());
let invalid_tx = AccountTransaction {
tx: declare_tx(
declare_tx_args! {
class_hash: contract_to_declare.get_class_hash(),
compiled_class_hash: contract_to_declare.get_compiled_class_hash(),
sender_address,
resource_bounds: $invalid_resource_bounds,
},
class_info,
),
execution_flags: ExecutionFlags::default(),
};
let invalid_tx = AccountTransaction::new(declare_tx(
declare_tx_args! {
class_hash: contract_to_declare.get_class_hash(),
compiled_class_hash: contract_to_declare.get_compiled_class_hash(),
sender_address,
resource_bounds: $invalid_resource_bounds,
},
class_info,
));
assert_resource_bounds_exceed_balance_failure(state, block_context, invalid_tx);
};
}
Expand Down Expand Up @@ -1516,7 +1508,7 @@ fn test_declare_tx(
None,
ExecutionSummary::default(),
);
let tx = declare_tx(
let account_tx = AccountTransaction::new(declare_tx(
declare_tx_args! {
max_fee: MAX_FEE,
sender_address,
Expand All @@ -1527,8 +1519,7 @@ fn test_declare_tx(
nonce: nonce_manager.next(sender_address),
},
class_info.clone(),
);
let account_tx = AccountTransaction { tx, execution_flags: ExecutionFlags::default() };
));

// Check state before transaction application.
assert_matches!(
Expand Down Expand Up @@ -1638,7 +1629,7 @@ fn test_declare_tx(
assert_eq!(contract_class_from_state, class_info.contract_class().try_into().unwrap());

// Checks that redeclaring the same contract fails.
let tx2 = declare_tx(
let account_tx2 = AccountTransaction::new(declare_tx(
declare_tx_args! {
max_fee: MAX_FEE,
sender_address,
Expand All @@ -1649,8 +1640,7 @@ fn test_declare_tx(
nonce: nonce_manager.next(sender_address),
},
class_info.clone(),
);
let account_tx2 = AccountTransaction { tx: tx2, execution_flags: ExecutionFlags::default() };
));
let result = account_tx2.execute(state, block_context);
assert_matches!(
result.unwrap_err(),
Expand Down Expand Up @@ -1709,16 +1699,13 @@ fn test_deploy_account_tx(
let account = FeatureContract::AccountWithoutValidations(cairo_version);
let account_class_hash = account.get_class_hash();
let state = &mut test_state(chain_info, BALANCE, &[(account, 1)]);
let deploy_account = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds,
class_hash: account_class_hash
},
&mut nonce_manager,
),
execution_flags: ExecutionFlags::default(),
};
let deploy_account = AccountTransaction::new(deploy_account_tx(
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds,
class_hash: account_class_hash
},
&mut nonce_manager,
));

// Extract deploy account transaction fields for testing, as it is consumed when creating an
// account transaction.
Expand Down Expand Up @@ -1869,16 +1856,13 @@ fn test_deploy_account_tx(

// Negative flow.
// Deploy to an existing address.
let deploy_account = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds,
class_hash: account_class_hash
},
&mut nonce_manager,
),
execution_flags: ExecutionFlags::default(),
};
let deploy_account = AccountTransaction::new(deploy_account_tx(
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds,
class_hash: account_class_hash
},
&mut nonce_manager,
));
let error = deploy_account.execute(state, block_context).unwrap_err();
assert_matches!(
error,
Expand All @@ -1903,15 +1887,12 @@ fn test_fail_deploy_account_undeclared_class_hash(
let state = &mut test_state(chain_info, BALANCE, &[]);
let mut nonce_manager = NonceManager::default();
let undeclared_hash = class_hash!("0xdeadbeef");
let deploy_account = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds, class_hash: undeclared_hash
},
&mut nonce_manager,
),
execution_flags: ExecutionFlags::default(),
};
let deploy_account = AccountTransaction::new(deploy_account_tx(
deploy_account_tx_args! {
resource_bounds: default_all_resource_bounds, class_hash: undeclared_hash
},
&mut nonce_manager,
));
let tx_context = block_context.to_tx_context(&deploy_account);
let fee_type = tx_context.tx_info.fee_type();

Expand Down

0 comments on commit 9f3f05f

Please sign in to comment.