Skip to content

Commit

Permalink
change(blockifier): account_tx from enum to struct w api::executable_tx
Browse files Browse the repository at this point in the history
  • Loading branch information
avivg-starkware committed Oct 30, 2024
1 parent 166b9ed commit 16be6ad
Show file tree
Hide file tree
Showing 25 changed files with 220 additions and 226 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl BlockBuilderTrait for BlockBuilder {
let mut executor_input_chunk = vec![];
for tx in &next_tx_chunk {
executor_input_chunk
.push(BlockifierTransaction::Account(AccountTransaction::try_from(tx)?));
.push(BlockifierTransaction::Account(AccountTransaction::new(tx.clone())));
}
let results = self.executor.lock().await.add_txs_to_block(&executor_input_chunk);
trace!("Transaction execution results: {:?}", results);
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/blockifier/stateful_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<S: StateReader> StatefulValidator<S> {
// Deploy account transactions should be fully executed, since the constructor must run
// before `__validate_deploy__`. The execution already includes all necessary validations,
// so they are skipped here.
if let AccountTransaction::DeployAccount(_) = tx {
if let starknet_api::executable_transaction::Transaction::DeployAccount(_) = tx.tx {
self.execute(tx)?;
return Ok(());
}
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/src/blockifier/stateful_validator_test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use assert_matches::assert_matches;
use rstest::rstest;
use starknet_api::executable_transaction::Transaction;
use starknet_api::transaction::{TransactionVersion, ValidResourceBounds};

use crate::blockifier::stateful_validator::StatefulValidator;
use crate::context::BlockContext;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::{fund_account, test_state};
use crate::test_utils::{CairoVersion, BALANCE};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::test_utils::{
block_context,
create_account_tx_for_validate_test_nonce_0,
Expand Down Expand Up @@ -61,18 +61,18 @@ fn test_tx_validator(
};

// Positive flow.
let tx = create_account_tx_for_validate_test_nonce_0(FaultyAccountTxCreatorArgs {
let account_tx = create_account_tx_for_validate_test_nonce_0(FaultyAccountTxCreatorArgs {
scenario: VALID,
..tx_args
});
if let AccountTransaction::DeployAccount(deploy_tx) = &tx {
if let Transaction::DeployAccount(deploy_tx) = &account_tx.tx {
fund_account(chain_info, deploy_tx.contract_address(), BALANCE, &mut state.state);
}

// Test the stateful validator.
let mut stateful_validator = StatefulValidator::create(state, block_context);
let skip_validate = false;
let result = stateful_validator.perform_validations(tx, skip_validate);
let result = stateful_validator.perform_validations(account_tx, skip_validate);
assert!(result.is_ok(), "Validation failed: {:?}", result.unwrap_err());
}

Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ 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(
let deploy_account_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(),
)
.into();
);
let tx = Transaction::Account(deploy_account_tx);
let expected_bouncer_weights = BouncerWeights {
state_diff_size: 3,
message_segment_length: 0,
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ pub enum ContractClassError {
contract_class_version: u8,
sierra_program_length: usize,
},
}
}
14 changes: 8 additions & 6 deletions crates/blockifier/src/execution/stack_trace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use starknet_api::transaction::{
ValidResourceBounds,
};
use starknet_api::{calldata, felt, invoke_tx_args};
use starknet_api::executable_transaction::Transaction;

use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME;
use crate::context::{BlockContext, ChainInfo};
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::{fund_account, test_state};
use crate::test_utils::{create_calldata, CairoVersion, BALANCE};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::constants::{
DEPLOY_CONTRACT_FUNCTION_ENTRY_POINT_NAME,
EXECUTE_ENTRY_POINT_NAME,
Expand Down Expand Up @@ -571,9 +571,9 @@ fn test_validate_trace(

if let TransactionType::DeployAccount = tx_type {
// Deploy account uses the actual address as the sender address.
match &account_tx {
AccountTransaction::DeployAccount(tx) => {
sender_address = tx.contract_address();
match &account_tx.tx {
Transaction::DeployAccount(_) => {
sender_address = account_tx.sender_address();
}
_ => panic!("Expected DeployAccountTransaction type"),
}
Expand Down Expand Up @@ -640,8 +640,10 @@ 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(),
let deploy_address = match &deploy_account_tx.tx {
Transaction::DeployAccount(_) => {
deploy_account_tx.sender_address()
}
_ => unreachable!("deploy_account_tx is a DeployAccount"),
};
fund_account(chain_info, deploy_address, Fee(BALANCE.0 * 2), &mut state.state);
Expand Down
9 changes: 5 additions & 4 deletions crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::executable_transaction::Transaction;
use starknet_api::execution_resources::GasVector;
use starknet_api::transaction::GasVectorComputationMode;

Expand Down Expand Up @@ -160,24 +161,24 @@ pub fn estimate_minimal_gas_vector(
) -> GasVector {
// TODO(Dori, 1/8/2023): Give names to the constant VM step estimates and regression-test them.
let BlockContext { block_info, versioned_constants, .. } = block_context;
let state_changes_by_account_tx = match tx {
let state_changes_by_account_tx = match &tx.tx {
// We consider the following state changes: sender balance update (storage update) + nonce
// increment (contract modification) (we exclude the sequencer balance update and the ERC20
// contract modification since it occurs for every tx).
AccountTransaction::Declare(_) => StateChangesCount {
Transaction::Declare(_) => StateChangesCount {
n_storage_updates: 1,
n_class_hash_updates: 0,
n_compiled_class_hash_updates: 0,
n_modified_contracts: 1,
},
AccountTransaction::Invoke(_) => StateChangesCount {
Transaction::Invoke(_) => StateChangesCount {
n_storage_updates: 1,
n_class_hash_updates: 0,
n_compiled_class_hash_updates: 0,
n_modified_contracts: 1,
},
// DeployAccount also updates the address -> class hash mapping.
AccountTransaction::DeployAccount(_) => StateChangesCount {
Transaction::DeployAccount(_) => StateChangesCount {
n_storage_updates: 1,
n_class_hash_updates: 1,
n_compiled_class_hash_updates: 0,
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
@@ -1,12 +1,16 @@
use starknet_api::contract_class::ClassInfo;
use starknet_api::executable_transaction::{DeclareTransaction, Transaction};
use starknet_api::test_utils::declare::DeclareTxArgs;

use crate::execution::contract_class::ClassInfo;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::transactions::DeclareTransaction;

pub fn declare_tx(declare_tx_args: DeclareTxArgs, class_info: ClassInfo) -> AccountTransaction {
let tx_hash = declare_tx_args.tx_hash;
let declare_tx = starknet_api::test_utils::declare::declare_tx(declare_tx_args);

AccountTransaction::Declare(DeclareTransaction::new(declare_tx, tx_hash, class_info).unwrap())
AccountTransaction::new(Transaction::Declare(DeclareTransaction {
tx: declare_tx,
tx_hash,
class_info,
}))
}
11 changes: 5 additions & 6 deletions crates/blockifier/src/test_utils/deploy_account.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use starknet_api::core::calculate_contract_address;
use starknet_api::executable_transaction::{Transaction, DeployAccountTransaction};
use starknet_api::test_utils::deploy_account::DeployAccountTxArgs;
use starknet_api::test_utils::NonceManager;

use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::transactions::DeployAccountTransaction;

pub fn deploy_account_tx(
deploy_tx_args: DeployAccountTxArgs,
Expand All @@ -22,9 +22,8 @@ pub fn deploy_account_tx(
deploy_tx_args,
nonce_manager.next(contract_address),
);
AccountTransaction::DeployAccount(DeployAccountTransaction::new(
deploy_account_tx,
tx_hash,
contract_address,

AccountTransaction::new(Transaction::DeployAccount(
DeployAccountTransaction { tx: deploy_account_tx, tx_hash, contract_address },
))
}
}
20 changes: 15 additions & 5 deletions crates/blockifier/src/test_utils/invoke.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use starknet_api::executable_transaction::{
InvokeTransaction as ExecutableInvokeTransaction,
Transaction as ExecutableTransaction,
};
use starknet_api::test_utils::invoke::InvokeTxArgs;
use starknet_api::transaction::{InvokeTransactionV0, TransactionVersion};
use starknet_api::transaction::{InvokeTransaction, InvokeTransactionV0, TransactionVersion};

use crate::abi::abi_utils::selector_from_name;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::constants::EXECUTE_ENTRY_POINT_NAME;
use crate::transaction::transactions::InvokeTransaction;

pub fn invoke_tx(invoke_args: InvokeTxArgs) -> AccountTransaction {
let tx_hash = invoke_args.tx_hash;
let only_query = invoke_args.only_query;
// TODO: Make TransactionVersion an enum and use match here.
let invoke_tx = if invoke_args.version == TransactionVersion::ZERO {
starknet_api::transaction::InvokeTransaction::V0(InvokeTransactionV0 {
InvokeTransaction::V0(InvokeTransactionV0 {
max_fee: invoke_args.max_fee,
calldata: invoke_args.calldata,
contract_address: invoke_args.sender_address,
Expand All @@ -24,7 +27,14 @@ pub fn invoke_tx(invoke_args: InvokeTxArgs) -> AccountTransaction {
};

match only_query {
true => AccountTransaction::Invoke(InvokeTransaction::new_for_query(invoke_tx, tx_hash)),
false => AccountTransaction::Invoke(InvokeTransaction::new(invoke_tx, tx_hash)),
true => AccountTransaction::new_for_query(ExecutableTransaction::Invoke(
ExecutableInvokeTransaction { tx: invoke_tx, tx_hash },
)),
false => {
AccountTransaction::new(ExecutableTransaction::Invoke(ExecutableInvokeTransaction {
tx: invoke_tx,
tx_hash,
}))
}
}
}
Loading

0 comments on commit 16be6ad

Please sign in to comment.