Skip to content

Commit

Permalink
chore(blockifier): add struct ExecutionFlags to AccountTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
avivg-starkware committed Nov 28, 2024
1 parent 02ea900 commit bec5223
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 62 deletions.
3 changes: 3 additions & 0 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::ExecutionFlags;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::test_utils::{
account_invoke_tx,
Expand Down Expand Up @@ -129,6 +130,7 @@ 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()),
ExecutionFlags::default(),
)
.into();
tx_executor_test_body(state, block_context, tx, expected_bouncer_weights);
Expand All @@ -150,6 +152,7 @@ fn test_deploy_account(
version,
},
&mut NonceManager::default(),
ExecutionFlags::default(),
)
.into();
let expected_bouncer_weights = BouncerWeights {
Expand Down
5 changes: 4 additions & 1 deletion 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::ExecutionFlags;
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 @@ -234,6 +235,7 @@ fn test_run_parallel_txs(default_all_resource_bounds: ValidResourceBounds) {
),
},
&mut NonceManager::default(),
ExecutionFlags::default(),
);
let enforce_fee = deploy_account_tx_1.enforce_fee();

Expand All @@ -247,7 +249,8 @@ 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 =
deploy_account_tx(deploy_tx_args, nonce_manager, ExecutionFlags::default());
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
3 changes: 2 additions & 1 deletion 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;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::objects::HasRelatedFeeType;
use crate::transaction::test_utils::{
account_invoke_tx,
Expand Down Expand Up @@ -560,6 +560,7 @@ fn test_deploy_before_declare(
nonce: nonce!(0_u8),
},
test_class_info.clone(),
ExecutionFlags::default(),
);

// Deploy test contract.
Expand Down
10 changes: 7 additions & 3 deletions crates/blockifier/src/test_utils/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use starknet_api::contract_class::ClassInfo;
use starknet_api::executable_transaction::AccountTransaction as ExecutableTransaction;
use starknet_api::test_utils::declare::{executable_declare_tx, DeclareTxArgs};

use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};

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

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

use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};

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

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

use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};

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

AccountTransaction { tx: invoke_tx, only_query }
AccountTransaction { tx: invoke_tx, execution_flags }
}
19 changes: 11 additions & 8 deletions crates/blockifier/src/test_utils/transfers_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::test_utils::dict_state_reader::DictStateReader;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::invoke::invoke_tx;
use crate::test_utils::{CairoVersion, BALANCE, MAX_FEE};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::transaction_execution::Transaction;

const N_ACCOUNTS: u16 = 10000;
Expand Down Expand Up @@ -180,13 +180,16 @@ impl TransfersGenerator {
felt!(0_u8) // Calldata: msb amount.
];

invoke_tx(invoke_tx_args! {
max_fee: self.config.max_fee,
sender_address,
calldata: execute_calldata,
version: self.config.tx_version,
nonce,
})
invoke_tx(
invoke_tx_args! {
max_fee: self.config.max_fee,
sender_address,
calldata: execute_calldata,
version: self.config.tx_version,
nonce,
},
ExecutionFlags::default(),
)
}
}

Expand Down
19 changes: 13 additions & 6 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{
Executable,
ExecutableTransaction,
ExecutionFlags,
ExecutionFlags as TransactionExecutionFlags,
ValidatableTransaction,
};

Expand All @@ -77,11 +77,18 @@ mod flavors_test;
#[path = "post_execution_test.rs"]
mod post_execution_test;

#[derive(Clone, Debug, Default, derive_more::From)]
pub struct ExecutionFlags {
pub only_query: bool,
pub charge_fee: bool,
pub skip_validate: bool,
}

/// Represents a paid Starknet transaction.
#[derive(Clone, Debug, derive_more::From)]
pub struct AccountTransaction {
pub tx: Transaction,
pub only_query: bool,
pub execution_flags: ExecutionFlags,
}
// TODO(AvivG): create additional macro that returns a reference.
macro_rules! implement_tx_getter_calls {
Expand Down Expand Up @@ -708,7 +715,7 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
&self,
state: &mut TransactionalState<'_, U>,
block_context: &BlockContext,
execution_flags: ExecutionFlags,
execution_flags: TransactionExecutionFlags,
) -> TransactionExecutionResult<TransactionExecutionInfo> {
let tx_context = Arc::new(block_context.to_tx_context(self));
self.verify_tx_version(tx_context.tx_info.version())?;
Expand Down Expand Up @@ -769,9 +776,9 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
impl TransactionInfoCreator for AccountTransaction {
fn create_tx_info(&self) -> TransactionInfo {
match &self.tx {
Transaction::Declare(tx) => tx.create_tx_info(self.only_query),
Transaction::DeployAccount(tx) => tx.create_tx_info(self.only_query),
Transaction::Invoke(tx) => tx.create_tx_info(self.only_query),
Transaction::Declare(tx) => tx.create_tx_info(self.execution_flags.only_query),
Transaction::DeployAccount(tx) => tx.create_tx_info(self.execution_flags.only_query),
Transaction::Invoke(tx) => tx.create_tx_info(self.execution_flags.only_query),
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ use crate::test_utils::{
DEFAULT_STRK_L2_GAS_PRICE,
MAX_FEE,
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{
AccountTransaction,
ExecutionFlags as AccountExecutionFlags,
};
use crate::transaction::objects::{HasRelatedFeeType, TransactionInfoCreator};
use crate::transaction::test_utils::{
account_invoke_tx,
Expand Down Expand Up @@ -211,6 +214,7 @@ fn test_fee_enforcement(
version,
},
&mut NonceManager::default(),
AccountExecutionFlags::default(),
);

let enforce_fee = deploy_account_tx.enforce_fee();
Expand Down Expand Up @@ -459,6 +463,7 @@ fn test_max_fee_limit_validate(
nonce: nonce_manager.next(account_address),
},
class_info,
AccountExecutionFlags::default(),
);
account_tx.execute(&mut state, &block_context, true, true).unwrap();

Expand Down Expand Up @@ -782,7 +787,11 @@ fn test_fail_declare(block_context: BlockContext, max_fee: Fee) {
};
let declare_account_tx = AccountTransaction {
tx: ApiExecutableTransaction::Declare(executable_declare),
only_query: false,
execution_flags: AccountExecutionFlags {
only_query: false,
charge_fee: false,
skip_validate: false,
},
};

// Fail execution, assert nonce and balance are unchanged.
Expand Down
31 changes: 19 additions & 12 deletions crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::test_utils::{
DEFAULT_STRK_L2_GAS_PRICE,
MAX_FEE,
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags};
use crate::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::ExecutableTransaction;
Expand Down Expand Up @@ -113,7 +113,8 @@ 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 =
deploy_account_tx(deploy_tx_args, nonce_manager, ExecutionFlags::default());
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 @@ -247,6 +248,7 @@ pub fn create_account_tx_for_validate_test(
compiled_class_hash: declared_contract.get_compiled_class_hash(),
},
class_info,
ExecutionFlags::default(),
)
}
TransactionType::DeployAccount => {
Expand All @@ -267,26 +269,31 @@ pub fn create_account_tx_for_validate_test(
constructor_calldata,
},
nonce_manager,
ExecutionFlags::default(),
)
}
TransactionType::InvokeFunction => {
let execute_calldata = create_calldata(sender_address, "foo", &[]);
invoke_tx(invoke_tx_args! {
max_fee,
resource_bounds,
signature,
sender_address,
calldata: execute_calldata,
version: tx_version,
nonce: nonce_manager.next(sender_address),
})
invoke_tx(
invoke_tx_args! {
max_fee,
resource_bounds,
signature,
sender_address,
calldata: execute_calldata,
version: tx_version,
nonce: nonce_manager.next(sender_address),
},
ExecutionFlags::default(),
)
}
_ => 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;
invoke_tx(invoke_args, ExecutionFlags { only_query, charge_fee: false, skip_validate: false })
}

pub fn run_invoke_tx(
Expand Down
24 changes: 21 additions & 3 deletions crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use crate::execution::entry_point::EntryPointExecutionContext;
use crate::fee::receipt::TransactionReceipt;
use crate::state::cached_state::TransactionalState;
use crate::state::state_api::UpdatableState;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::account_transaction::{
AccountTransaction,
ExecutionFlags as AccountExecutionFlags,
};
use crate::transaction::errors::TransactionFeeError;
use crate::transaction::objects::{
TransactionExecutionInfo,
Expand All @@ -40,7 +43,14 @@ 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, only_query: false })
Transaction::Account(AccountTransaction {
tx,
execution_flags: AccountExecutionFlags {
only_query: false,
charge_fee: false,
skip_validate: false,
},
})
}
starknet_api::executable_transaction::Transaction::L1Handler(tx) => {
Transaction::L1Handler(tx)
Expand Down Expand Up @@ -119,7 +129,15 @@ impl Transaction {
}
_ => unimplemented!(),
};
Ok(AccountTransaction { tx: executable_tx, only_query }.into())
Ok(AccountTransaction {
tx: executable_tx,
execution_flags: AccountExecutionFlags {
only_query,
charge_fee: false,
skip_validate: false,
},
}
.into())
}
}

Expand Down
Loading

0 comments on commit bec5223

Please sign in to comment.