Skip to content

Commit

Permalink
chore(blockifier): invoke() declare() deploy_account() change ret val…
Browse files Browse the repository at this point in the history
… to api_tx
  • Loading branch information
avivg-starkware committed Dec 2, 2024
1 parent 0e846bc commit d1f3d9d
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 129 deletions.
26 changes: 15 additions & 11 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::test_utils::{
BALANCE,
DEFAULT_STRK_L1_GAS_PRICE,
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::test_utils::{
account_invoke_tx,
Expand Down Expand Up @@ -129,9 +130,9 @@ fn test_declare(
resource_bounds: l1_resource_bounds(0_u8.into(), DEFAULT_STRK_L1_GAS_PRICE.into()),
},
calculate_class_info_for_testing(declared_contract.get_class()),
)
.into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
);
let account_tx = AccountTransaction { tx, only_query: false }.into();
tx_executor_test_body(state, block_context, account_tx, expected_bouncer_weights);
}

#[rstest]
Expand All @@ -143,14 +144,17 @@ fn test_deploy_account(
let account_contract = FeatureContract::AccountWithoutValidations(cairo_version);
let state = test_state(&block_context.chain_info, BALANCE, &[(account_contract, 0)]);

let tx = deploy_account_tx(
deploy_account_tx_args! {
class_hash: account_contract.get_class_hash(),
resource_bounds: l1_resource_bounds(0_u8.into(), DEFAULT_STRK_L1_GAS_PRICE.into()),
version,
},
&mut NonceManager::default(),
)
let tx = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
class_hash: account_contract.get_class_hash(),
resource_bounds: l1_resource_bounds(0_u8.into(), DEFAULT_STRK_L1_GAS_PRICE.into()),
version,
},
&mut NonceManager::default(),
),
only_query: false,
}
.into();
let expected_bouncer_weights = BouncerWeights {
state_diff_size: 3,
Expand Down
29 changes: 18 additions & 11 deletions crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use crate::test_utils::deploy_account::deploy_account_tx;
use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{CairoVersion, BALANCE, DEFAULT_STRK_L1_GAS_PRICE};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{default_all_resource_bounds, l1_resource_bounds};
use crate::transaction::transactions::ExecutableTransaction;
Expand Down Expand Up @@ -225,16 +226,19 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
let mut state_2 = TransactionalState::create_transactional(&mut versioned_state_proxy_2);

// Prepare transactions
let deploy_account_tx_1 = deploy_account_tx(
deploy_account_tx_args! {
class_hash: account_without_validation.get_class_hash(),
resource_bounds: l1_resource_bounds(
u8::from(!zero_bounds).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
},
&mut NonceManager::default(),
);
let deploy_account_tx_1 = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
class_hash: account_without_validation.get_class_hash(),
resource_bounds: l1_resource_bounds(
u8::from(!zero_bounds).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
},
&mut NonceManager::default(),
),
only_query: false,
};
let enforce_fee = deploy_account_tx_1.enforce_fee();

let class_hash = grindy_account.get_class_hash();
Expand All @@ -247,7 +251,10 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
constructor_calldata: constructor_calldata.clone(),
};
let nonce_manager = &mut NonceManager::default();
let delpoy_account_tx_2 = deploy_account_tx(deploy_tx_args, nonce_manager);
let delpoy_account_tx_2 = AccountTransaction {
tx: deploy_account_tx(deploy_tx_args, nonce_manager),
only_query: false,
};
let account_address = delpoy_account_tx_2.sender_address();
let tx_context = block_context.to_tx_context(&delpoy_account_tx_2);
let fee_type = tx_context.tx_info.fee_type();
Expand Down
7 changes: 5 additions & 2 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ fn test_deploy_before_declare(
},
test_class_info.clone(),
);
let declare_account_tx = AccountTransaction { tx: declare_tx, only_query: false };

// Deploy test contract.
let invoke_tx = account_invoke_tx(invoke_tx_args! {
Expand All @@ -580,8 +581,10 @@ fn test_deploy_before_declare(
nonce: nonce!(0_u8)
});

let txs =
[declare_tx, invoke_tx].into_iter().map(Transaction::Account).collect::<Vec<Transaction>>();
let txs = [declare_account_tx, invoke_tx]
.into_iter()
.map(Transaction::Account)
.collect::<Vec<Transaction>>();

let mut bouncer = Bouncer::new(block_context.bouncer_config.clone());
let worker_executor =
Expand Down
6 changes: 2 additions & 4 deletions crates/blockifier/src/test_utils/declare.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use starknet_api::contract_class::ClassInfo;
use starknet_api::executable_transaction::AccountTransaction as ExecutableTransaction;
use starknet_api::executable_transaction::AccountTransaction;
use starknet_api::test_utils::declare::{executable_declare_tx, DeclareTxArgs};

use crate::transaction::account_transaction::AccountTransaction;

pub fn declare_tx(declare_tx_args: DeclareTxArgs, class_info: ClassInfo) -> AccountTransaction {
let declare_tx = executable_declare_tx(declare_tx_args, class_info);

AccountTransaction { tx: ExecutableTransaction::Declare(declare_tx), only_query: false }
AccountTransaction::Declare(declare_tx)
}
9 changes: 2 additions & 7 deletions crates/blockifier/src/test_utils/deploy_account.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use starknet_api::executable_transaction::AccountTransaction as ExecutableTransaction;
use starknet_api::executable_transaction::AccountTransaction;
use starknet_api::test_utils::deploy_account::{executable_deploy_account_tx, DeployAccountTxArgs};
use starknet_api::test_utils::NonceManager;

use crate::transaction::account_transaction::AccountTransaction;

pub fn deploy_account_tx(
deploy_tx_args: DeployAccountTxArgs,
nonce_manager: &mut NonceManager,
) -> AccountTransaction {
let deploy_account_tx = executable_deploy_account_tx(deploy_tx_args, nonce_manager);

AccountTransaction {
tx: ExecutableTransaction::DeployAccount(deploy_account_tx),
only_query: false,
}
AccountTransaction::DeployAccount(deploy_account_tx)
}
9 changes: 2 additions & 7 deletions crates/blockifier/src/test_utils/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use starknet_api::executable_transaction::AccountTransaction as ExecutableTransaction;
use starknet_api::executable_transaction::AccountTransaction;
use starknet_api::test_utils::invoke::{executable_invoke_tx, InvokeTxArgs};

use crate::transaction::account_transaction::AccountTransaction;

pub fn invoke_tx(invoke_args: InvokeTxArgs) -> AccountTransaction {
let only_query = invoke_args.only_query;
let invoke_tx = ExecutableTransaction::Invoke(executable_invoke_tx(invoke_args));

AccountTransaction { tx: invoke_tx, only_query }
AccountTransaction::Invoke(executable_invoke_tx(invoke_args))
}
6 changes: 4 additions & 2 deletions crates/blockifier/src/test_utils/transfers_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use starknet_api::abi::abi_utils::selector_from_name;
use starknet_api::core::ContractAddress;
use starknet_api::executable_transaction::AccountTransaction as ApiExecutableTransaction;
use starknet_api::test_utils::NonceManager;
use starknet_api::transaction::constants::TRANSFER_ENTRY_POINT_NAME;
use starknet_api::transaction::fields::Fee;
Expand Down Expand Up @@ -143,7 +144,8 @@ impl TransfersGenerator {
let recipient_address = self.get_next_recipient();
self.sender_index = (self.sender_index + 1) % self.account_addresses.len();

let account_tx = self.generate_transfer(sender_address, recipient_address);
let tx = self.generate_transfer(sender_address, recipient_address);
let account_tx = AccountTransaction { tx, only_query: false };
txs.push(Transaction::Account(account_tx));
}
let results = self.executor.execute_txs(&txs);
Expand All @@ -159,7 +161,7 @@ impl TransfersGenerator {
&mut self,
sender_address: ContractAddress,
recipient_address: ContractAddress,
) -> AccountTransaction {
) -> ApiExecutableTransaction {
let nonce = self.nonce_manager.next(sender_address);

let entry_point_selector = selector_from_name(TRANSFER_ENTRY_POINT_NAME);
Expand Down
48 changes: 26 additions & 22 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,31 @@ fn test_fee_enforcement(
) {
let account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0);
let state = &mut test_state(&block_context.chain_info, BALANCE, &[(account, 1)]);
let deploy_account_tx = deploy_account_tx(
deploy_account_tx_args! {
class_hash: account.get_class_hash(),
max_fee: Fee(if zero_bounds { 0 } else { MAX_FEE.0 }),
resource_bounds: match gas_bounds_mode {
GasVectorComputationMode::NoL2Gas => l1_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
GasVectorComputationMode::All => create_all_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L2_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L2_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L1_DATA_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_DATA_GAS_PRICE.into(),
),
let deploy_account_tx = AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
class_hash: account.get_class_hash(),
max_fee: Fee(if zero_bounds { 0 } else { MAX_FEE.0 }),
resource_bounds: match gas_bounds_mode {
GasVectorComputationMode::NoL2Gas => l1_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into()
),
GasVectorComputationMode::All => create_all_resource_bounds(
(if zero_bounds { 0 } else { DEFAULT_L1_GAS_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L2_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L2_GAS_PRICE.into(),
(if zero_bounds { 0 } else { DEFAULT_L1_DATA_GAS_MAX_AMOUNT.0 }).into(),
DEFAULT_STRK_L1_DATA_GAS_PRICE.into(),
),
},
version,
},
version,
},
&mut NonceManager::default(),
);
&mut NonceManager::default(),
),
only_query: false,
};

let enforce_fee = deploy_account_tx.enforce_fee();
assert_ne!(zero_bounds, enforce_fee);
Expand Down Expand Up @@ -451,7 +454,7 @@ fn test_max_fee_limit_validate(
let class_info = calculate_class_info_for_testing(grindy_validate_account.get_class());

// Declare the grindy-validation account.
let account_tx = declare_tx(
let tx = declare_tx(
declare_tx_args! {
class_hash: grindy_class_hash,
sender_address: account_address,
Expand All @@ -460,6 +463,7 @@ fn test_max_fee_limit_validate(
},
class_info,
);
let account_tx = AccountTransaction { tx, only_query: false };
account_tx.execute(&mut state, &block_context, true, true).unwrap();

// Deploy grindy account with a lot of grind in the constructor.
Expand Down
45 changes: 27 additions & 18 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ pub fn deploy_and_fund_account(
deploy_tx_args: DeployAccountTxArgs,
) -> (AccountTransaction, ContractAddress) {
// Deploy an account contract.
let deploy_account_tx = deploy_account_tx(deploy_tx_args, nonce_manager);
let deploy_account_tx = AccountTransaction {
tx: deploy_account_tx(deploy_tx_args, nonce_manager),
only_query: false,
};
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 Expand Up @@ -235,7 +238,7 @@ pub fn create_account_tx_for_validate_test(
};
let class_hash = declared_contract.get_class_hash();
let class_info = calculate_class_info_for_testing(declared_contract.get_class());
declare_tx(
let tx = declare_tx(
declare_tx_args! {
max_fee,
resource_bounds,
Expand All @@ -247,7 +250,8 @@ pub fn create_account_tx_for_validate_test(
compiled_class_hash: declared_contract.get_compiled_class_hash(),
},
class_info,
)
);
AccountTransaction { tx, only_query: false }
}
TransactionType::DeployAccount => {
// We do not use the sender address here because the transaction generates the actual
Expand All @@ -256,37 +260,42 @@ pub fn create_account_tx_for_validate_test(
true => constants::FELT_TRUE,
false => constants::FELT_FALSE,
})];
deploy_account_tx(
deploy_account_tx_args! {
max_fee,
resource_bounds,
signature,
version: tx_version,
class_hash,
contract_address_salt,
constructor_calldata,
},
nonce_manager,
)
AccountTransaction {
tx: deploy_account_tx(
deploy_account_tx_args! {
max_fee,
resource_bounds,
signature,
version: tx_version,
class_hash,
contract_address_salt,
constructor_calldata,
},
nonce_manager,
),
only_query: false,
}
}
TransactionType::InvokeFunction => {
let execute_calldata = create_calldata(sender_address, "foo", &[]);
invoke_tx(invoke_tx_args! {
let tx = invoke_tx(invoke_tx_args! {
max_fee,
resource_bounds,
signature,
sender_address,
calldata: execute_calldata,
version: tx_version,
nonce: nonce_manager.next(sender_address),
})
});
AccountTransaction { tx, only_query: false }
}
_ => panic!("{tx_type:?} is not an account transaction."),
}
}

pub fn account_invoke_tx(invoke_args: InvokeTxArgs) -> AccountTransaction {
invoke_tx(invoke_args)
let only_query = invoke_args.only_query;
AccountTransaction { tx: invoke_tx(invoke_args), only_query }
}

pub fn run_invoke_tx(
Expand Down
Loading

0 comments on commit d1f3d9d

Please sign in to comment.