Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
test(councurrency): add utilitis for commit tx test
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed May 30, 2024
1 parent ffaf4ad commit 818b4f3
Showing 1 changed file with 113 additions and 10 deletions.
123 changes: 113 additions & 10 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,140 @@ use std::collections::HashMap;
use num_bigint::BigUint;
use starknet_api::core::{ContractAddress, PatriciaKey};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::transaction::Fee;
use starknet_api::transaction::{Fee, TransactionVersion};
use starknet_api::{contract_address, patricia_key, stark_felt};

use super::WorkerExecutor;
use crate::abi::abi_utils::get_fee_token_var_address;
use crate::abi::sierra_types::next_storage_key;
use crate::concurrency::scheduler::{Task, TransactionStatus};
use crate::concurrency::test_utils::safe_versioned_state_for_testing;
use crate::concurrency::versioned_state::ThreadSafeVersionedState;
use crate::concurrency::worker_logic::add_fee_to_sequencer_balance;
use crate::context::BlockContext;
use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::fee::fee_utils::get_sequencer_balance_keys;
use crate::state::cached_state::StateMaps;
use crate::state::state_api::StateReader;
use crate::state::cached_state::{ContractClassMapping, StateMaps};
use crate::state::state_api::{StateReader, UpdatableState};
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state_reader;
use crate::test_utils::{
create_calldata, CairoVersion, NonceManager, BALANCE, MAX_FEE, TEST_ERC20_CONTRACT_ADDRESS,
create_calldata, create_trivial_calldata, CairoVersion, NonceManager, BALANCE, MAX_FEE,
MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE, TEST_ERC20_CONTRACT_ADDRESS,
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::constants::TRANSFER_ENTRY_POINT_NAME;
use crate::transaction::objects::FeeType;
use crate::transaction::test_utils::account_invoke_tx;
use crate::transaction::test_utils::{account_invoke_tx, l1_resource_bounds};
use crate::transaction::transaction_execution::Transaction;
use crate::{invoke_tx_args, nonce, storage_key};

fn _trivial_calldata_invoke_tx(
account_address: ContractAddress,
nonce_manager: &mut NonceManager,
) -> AccountTransaction {
account_invoke_tx(invoke_tx_args! {
sender_address: account_address,
calldata: create_trivial_calldata(account_address),
resource_bounds: l1_resource_bounds(MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE),
version: TransactionVersion::THREE,
nonce: nonce_manager.next(account_address),
})
}

fn _sequencer_transfer_invoke_tx(
account_address: ContractAddress,
block_context: &BlockContext,
transfer_amount: u128,
nonce_manager: &mut NonceManager,
) -> AccountTransaction {
let sequencer_address = block_context.block_info.sequencer_address;
let transfer_calldata = create_calldata(
block_context.chain_info().fee_token_address(&FeeType::Strk),
TRANSFER_ENTRY_POINT_NAME,
&[*sequencer_address.0.key(), stark_felt!(transfer_amount), stark_felt!(0_u8)],
);

// Invokes transfer to the sequencer.
account_invoke_tx(invoke_tx_args! {
sender_address: account_address,
calldata: transfer_calldata,
resource_bounds: l1_resource_bounds(MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE),
version: TransactionVersion::THREE,
nonce: nonce_manager.next(account_address)
})
}

fn _invoke_tx_without_calldata(
account_address: ContractAddress,
nonce_manager: &mut NonceManager,
) -> AccountTransaction {
account_invoke_tx(invoke_tx_args! {
sender_address: account_address,
resource_bounds: l1_resource_bounds(MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE),
version: TransactionVersion::THREE,
nonce: nonce_manager.next(account_address),
})
}

// This function checks that the storage values of the account and sequencer balances in the
// versioned state of tx_index are equal to the expected_storage_values.
fn _validate_fee_transfer<S: StateReader>(
executor: &WorkerExecutor<'_, S>,
account_address: ContractAddress,
tx_index: usize,
expected_storage_values: (StarkFelt, StarkFelt, StarkFelt, StarkFelt),
) {
let tx_version_state = executor.state.pin_version(tx_index + 1);
let account_balance_key_low = get_fee_token_var_address(account_address);
let account_balance_key_high = next_storage_key(&account_balance_key_low).unwrap();

let (sequencer_balance_key_low, sequencer_balance_key_high) =
get_sequencer_balance_keys(&executor.block_context);
for (balance, storage_key) in [
(expected_storage_values.0, account_balance_key_low),
(expected_storage_values.1, account_balance_key_high),
(expected_storage_values.2, sequencer_balance_key_low),
(expected_storage_values.3, sequencer_balance_key_high),
] {
assert_eq!(
tx_version_state
.get_storage_at(
executor.block_context.chain_info.fee_token_address(&FeeType::Strk),
storage_key
)
.unwrap(),
balance
);
}
}

fn _write_account_balance_at_versioned_state(
account_address: ContractAddress,
state: &mut ThreadSafeVersionedState<impl StateReader>,
tx_index: usize,
fee_token_address: ContractAddress,
new_balance_low: StarkFelt,
new_balance_high: StarkFelt,
) {
let mut versioned_state = state.pin_version(tx_index - 1);
let account_balance_key_low = get_fee_token_var_address(account_address);
let account_balance_key_high = next_storage_key(&account_balance_key_low).unwrap();
let writes = StateMaps {
storage: HashMap::from([
((fee_token_address, account_balance_key_low), new_balance_low),
((fee_token_address, account_balance_key_high), new_balance_high),
]),
..StateMaps::default()
};
versioned_state.apply_writes(&writes, &ContractClassMapping::default(), &HashMap::default());
}

#[test]
fn test_worker_execute() {
// Settings.
let concurrency_mode = true;
let block_context =
BlockContext::create_for_account_testing_with_concurrency_mode(concurrency_mode);
let block_context = BlockContext::create_for_testing_with_concurrency_mode(concurrency_mode);
let account_contract = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo0);
let chain_info = &block_context.chain_info;
Expand Down Expand Up @@ -203,8 +307,7 @@ fn test_worker_execute() {
fn test_worker_validate() {
// Settings.
let concurrency_mode = true;
let block_context =
BlockContext::create_for_account_testing_with_concurrency_mode(concurrency_mode);
let block_context = BlockContext::create_for_testing_with_concurrency_mode(concurrency_mode);

let account_contract = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
let test_contract = FeatureContract::TestContract(CairoVersion::Cairo0);
Expand Down Expand Up @@ -313,7 +416,7 @@ pub fn test_add_fee_to_sequencer_balance(
#[case] sequencer_balance_high: StarkFelt,
) {
let tx_index = 0;
let block_context = BlockContext::create_for_account_testing_with_concurrency_mode(true);
let block_context = BlockContext::create_for_testing_with_concurrency_mode(true);
let account = FeatureContract::Empty(CairoVersion::Cairo1);
let safe_versioned_state = safe_versioned_state_for_testing(test_state_reader(
&block_context.chain_info,
Expand Down

0 comments on commit 818b4f3

Please sign in to comment.