Skip to content

Commit

Permalink
chore: move transaction creator test util to starknet api (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware authored Sep 8, 2024
1 parent 2395f9a commit a4f71a0
Show file tree
Hide file tree
Showing 25 changed files with 419 additions and 397 deletions.
4 changes: 2 additions & 2 deletions crates/blockifier/src/blockifier/transaction_executor_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use assert_matches::assert_matches;
use pretty_assertions::assert_eq;
use rstest::rstest;
use starknet_api::felt;
use starknet_api::transaction::{Fee, TransactionVersion};
use starknet_api::{declare_tx_args, deploy_account_tx_args, felt, invoke_tx_args};
use starknet_types_core::felt::Felt;

use crate::blockifier::config::TransactionExecutorConfig;
Expand All @@ -13,6 +13,7 @@ use crate::blockifier::transaction_executor::{
};
use crate::bouncer::{Bouncer, BouncerWeights};
use crate::context::BlockContext;
use crate::nonce;
use crate::state::cached_state::CachedState;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
Expand All @@ -39,7 +40,6 @@ use crate::transaction::test_utils::{
};
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::L1HandlerTransaction;
use crate::{declare_tx_args, deploy_account_tx_args, invoke_tx_args, nonce};

fn tx_executor_test_body<S: StateReader>(
state: CachedState<S>,
Expand Down
3 changes: 1 addition & 2 deletions crates/blockifier/src/concurrency/fee_utils_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use num_bigint::BigUint;
use rstest::rstest;
use starknet_api::felt;
use starknet_api::transaction::{Fee, ValidResourceBounds};
use starknet_api::{felt, invoke_tx_args};
use starknet_types_core::felt::Felt;

use crate::concurrency::fee_utils::{add_fee_to_sequencer_balance, fill_sequencer_balance_reads};
use crate::concurrency::test_utils::create_fee_transfer_call_info;
use crate::context::BlockContext;
use crate::fee::fee_utils::get_sequencer_balance_keys;
use crate::invoke_tx_args;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::{fund_account, test_state, test_state_inner};
Expand Down
11 changes: 9 additions & 2 deletions crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ use starknet_api::core::{
PatriciaKey,
};
use starknet_api::transaction::{Calldata, ContractAddressSalt, ValidResourceBounds};
use starknet_api::{calldata, class_hash, contract_address, felt, patricia_key};
use starknet_api::{
calldata,
class_hash,
contract_address,
deploy_account_tx_args,
felt,
patricia_key,
};

use crate::abi::abi_utils::{get_fee_token_var_address, get_storage_var_address};
use crate::concurrency::test_utils::{
Expand Down Expand Up @@ -44,7 +51,7 @@ use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::{HasRelatedFeeType, TransactionInfoCreator};
use crate::transaction::test_utils::{l1_resource_bounds, max_resource_bounds};
use crate::transaction::transactions::ExecutableTransaction;
use crate::{compiled_class_hash, deploy_account_tx_args, nonce, storage_key};
use crate::{compiled_class_hash, nonce, storage_key};

#[fixture]
pub fn safe_versioned_state(
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use starknet_api::transaction::{
TransactionVersion,
ValidResourceBounds,
};
use starknet_api::{contract_address, felt, patricia_key};
use starknet_api::{contract_address, declare_tx_args, felt, invoke_tx_args, patricia_key};
use starknet_types_core::felt::Felt;

use super::WorkerExecutor;
Expand Down Expand Up @@ -47,7 +47,7 @@ use crate::transaction::test_utils::{
max_resource_bounds,
};
use crate::transaction::transaction_execution::Transaction;
use crate::{declare_tx_args, invoke_tx_args, nonce, storage_key};
use crate::{nonce, storage_key};

fn trivial_calldata_invoke_tx(
account_address: ContractAddress,
Expand Down
3 changes: 1 addition & 2 deletions crates/blockifier/src/execution/stack_trace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use starknet_api::transaction::{
TransactionVersion,
ValidResourceBounds,
};
use starknet_api::{calldata, felt};
use starknet_api::{calldata, felt, invoke_tx_args};

use crate::abi::abi_utils::selector_from_name;
use crate::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME;
use crate::context::{BlockContext, ChainInfo};
use crate::invoke_tx_args;
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};
Expand Down
4 changes: 3 additions & 1 deletion crates/blockifier/src/fee/actual_cost_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rstest::{fixture, rstest};
use starknet_api::invoke_tx_args;
use starknet_api::transaction::{L2ToL1Payload, ValidResourceBounds};
use starknet_types_core::felt::Felt;

Expand All @@ -10,6 +11,7 @@ use crate::fee::gas_usage::{
get_log_message_to_l1_emissions_cost,
get_message_segment_length,
};
use crate::nonce;
use crate::state::cached_state::StateChangesCount;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
Expand All @@ -24,7 +26,7 @@ use crate::transaction::test_utils::{
use crate::transaction::transactions::ExecutableTransaction;
use crate::utils::{u128_from_usize, usize_from_u128};
use crate::versioned_constants::VersionedConstants;
use crate::{invoke_tx_args, nonce};

#[fixture]
fn versioned_constants() -> &'static VersionedConstants {
VersionedConstants::latest_constants()
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use assert_matches::assert_matches;
use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use rstest::rstest;
use starknet_api::invoke_tx_args;
use starknet_api::transaction::Fee;

use crate::abi::constants::N_STEPS_RESOURCE;
Expand All @@ -12,7 +13,6 @@ use crate::context::BlockContext;
use crate::fee::actual_cost::TransactionReceipt;
use crate::fee::fee_checks::{FeeCheckError, FeeCheckReportFields, PostExecutionReport};
use crate::fee::fee_utils::calculate_l1_gas_by_vm_usage;
use crate::invoke_tx_args;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::num::NonZeroU128;

use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use starknet_api::invoke_tx_args;
use starknet_api::transaction::{EventContent, EventData, EventKey, Fee};
use starknet_types_core::felt::Felt;

Expand All @@ -15,7 +16,6 @@ use crate::fee::gas_usage::{
get_da_gas_cost,
get_message_segment_length,
};
use crate::invoke_tx_args;
use crate::state::cached_state::StateChangesCount;
use crate::test_utils::{DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE};
use crate::transaction::objects::{FeeType, GasVector, StarknetResources};
Expand Down
13 changes: 1 addition & 12 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ use std::path::PathBuf;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::{ClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata,
ContractAddressSalt,
ResourceBounds,
TransactionVersion,
ValidResourceBounds,
};
use starknet_api::transaction::{Calldata, ContractAddressSalt, TransactionVersion};
use starknet_api::{contract_address, felt, patricia_key};
use starknet_types_core::felt::Felt;

Expand Down Expand Up @@ -210,11 +204,6 @@ pub fn trivial_external_entry_point_with_address(
}
}

// TODO: Default testing bounds should probably be AllResourceBounds variant.
pub fn default_testing_resource_bounds() -> ValidResourceBounds {
ValidResourceBounds::L1Gas(ResourceBounds { max_amount: 0, max_price_per_unit: 1 })
}

#[macro_export]
macro_rules! check_inner_exc_for_custom_hint {
($inner_exc:expr, $expected_hint:expr) => {
Expand Down
124 changes: 2 additions & 122 deletions crates/blockifier/src/test_utils/declare.rs
Original file line number Diff line number Diff line change
@@ -1,132 +1,12 @@
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::transaction::{
AccountDeploymentData,
DeclareTransactionV0V1,
DeclareTransactionV2,
DeclareTransactionV3,
Fee,
PaymasterData,
Tip,
TransactionHash,
TransactionSignature,
TransactionVersion,
ValidResourceBounds,
};
use starknet_api::test_utils::declare::DeclareTxArgs;

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

#[derive(Clone)]
pub struct DeclareTxArgs {
pub max_fee: Fee,
pub signature: TransactionSignature,
pub sender_address: ContractAddress,
pub version: TransactionVersion,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub nonce_data_availability_mode: DataAvailabilityMode,
pub fee_data_availability_mode: DataAvailabilityMode,
pub paymaster_data: PaymasterData,
pub account_deployment_data: AccountDeploymentData,
pub nonce: Nonce,
pub class_hash: ClassHash,
pub compiled_class_hash: CompiledClassHash,
// TODO(Arni): Consider removing this field.
pub tx_hash: TransactionHash,
}

impl Default for DeclareTxArgs {
fn default() -> Self {
Self {
max_fee: Fee::default(),
signature: TransactionSignature::default(),
sender_address: ContractAddress::default(),
version: TransactionVersion::THREE,
resource_bounds: default_testing_resource_bounds(),
tip: Tip::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L1,
paymaster_data: PaymasterData::default(),
account_deployment_data: AccountDeploymentData::default(),
nonce: Nonce::default(),
class_hash: ClassHash::default(),
compiled_class_hash: CompiledClassHash::default(),
tx_hash: TransactionHash::default(),
}
}
}

/// Utility macro for creating `DeclareTxArgs` to reduce boilerplate.
#[macro_export]
macro_rules! declare_tx_args {
($($field:ident $(: $value:expr)?),* $(,)?) => {
$crate::test_utils::declare::DeclareTxArgs {
$($field $(: $value)?,)*
..Default::default()
}
};
($($field:ident $(: $value:expr)?),* , ..$defaults:expr) => {
$crate::test_utils::declare::DeclareTxArgs {
$($field $(: $value)?,)*
..$defaults
}
};
}

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

AccountTransaction::Declare(DeclareTransaction::new(declare_tx, tx_hash, class_info).unwrap())
}

fn inner_declare_tx(
declare_tx_args: DeclareTxArgs,
) -> starknet_api::transaction::DeclareTransaction {
// TODO: Make TransactionVersion an enum and use match here.
if declare_tx_args.version == TransactionVersion::ZERO {
starknet_api::transaction::DeclareTransaction::V0(DeclareTransactionV0V1 {
max_fee: declare_tx_args.max_fee,
signature: declare_tx_args.signature,
sender_address: declare_tx_args.sender_address,
nonce: declare_tx_args.nonce,
class_hash: declare_tx_args.class_hash,
})
} else if declare_tx_args.version == TransactionVersion::ONE {
starknet_api::transaction::DeclareTransaction::V1(DeclareTransactionV0V1 {
max_fee: declare_tx_args.max_fee,
signature: declare_tx_args.signature,
sender_address: declare_tx_args.sender_address,
nonce: declare_tx_args.nonce,
class_hash: declare_tx_args.class_hash,
})
} else if declare_tx_args.version == TransactionVersion::TWO {
starknet_api::transaction::DeclareTransaction::V2(DeclareTransactionV2 {
max_fee: declare_tx_args.max_fee,
signature: declare_tx_args.signature,
sender_address: declare_tx_args.sender_address,
nonce: declare_tx_args.nonce,
class_hash: declare_tx_args.class_hash,
compiled_class_hash: declare_tx_args.compiled_class_hash,
})
} else if declare_tx_args.version == TransactionVersion::THREE {
starknet_api::transaction::DeclareTransaction::V3(DeclareTransactionV3 {
signature: declare_tx_args.signature,
sender_address: declare_tx_args.sender_address,
resource_bounds: declare_tx_args.resource_bounds,
tip: declare_tx_args.tip,
nonce_data_availability_mode: declare_tx_args.nonce_data_availability_mode,
fee_data_availability_mode: declare_tx_args.fee_data_availability_mode,
paymaster_data: declare_tx_args.paymaster_data,
account_deployment_data: declare_tx_args.account_deployment_data,
nonce: declare_tx_args.nonce,
class_hash: declare_tx_args.class_hash,
compiled_class_hash: declare_tx_args.compiled_class_hash,
})
} else {
panic!("Unsupported transaction version: {:?}.", declare_tx_args.version)
}
}
Loading

0 comments on commit a4f71a0

Please sign in to comment.