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

Commit

Permalink
test(concurrency): add utilitis for commit tx test
Browse files Browse the repository at this point in the history
  • Loading branch information
meship-starkware committed Jun 5, 2024
1 parent 79b6ae8 commit 7e88771
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
54 changes: 50 additions & 4 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use num_bigint::BigUint;
use starknet_api::core::{ContractAddress, PatriciaKey};
use starknet_api::core::{ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::transaction::{ContractAddressSalt, Fee, TransactionVersion};
use starknet_api::{contract_address, patricia_key, stark_felt};
Expand All @@ -14,16 +14,17 @@ use crate::concurrency::test_utils::safe_versioned_state_for_testing;
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::fee::fee_utils::{get_address_balance_keys, get_sequencer_balance_keys};
use crate::state::cached_state::StateMaps;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::declare::declare_tx;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
create_calldata, CairoVersion, NonceManager, BALANCE, MAX_FEE, MAX_L1_GAS_AMOUNT,
MAX_L1_GAS_PRICE, 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::DEPLOY_CONTRACT_FUNCTION_ENTRY_POINT_NAME;
use crate::transaction::objects::FeeType;
use crate::transaction::test_utils::{
Expand All @@ -32,6 +33,51 @@ use crate::transaction::test_utils::{
use crate::transaction::transaction_execution::Transaction;
use crate::{declare_tx_args, invoke_tx_args, nonce, storage_key};

fn _trivial_calldata_invoke_tx(
account_address: ContractAddress,
nonce: Nonce,
) -> 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,
})
}

/// Checks that the storage values of the account and sequencer balances in the
/// versioned state of tx_index equals the expected values.
fn _validate_fee_transfer<S: StateReader>(
executor: &WorkerExecutor<'_, S>,
account_address: ContractAddress,
tx_index: usize,
expected_account_balance_low: StarkFelt,
expected_sequencer_balance_low: StarkFelt,
) {
let tx_version_state = executor.state.pin_version(tx_index + 1);
let (account_balance_key_low, account_balance_key_high) =
get_address_balance_keys(account_address);

let (sequencer_balance_key_low, sequencer_balance_key_high) =
get_sequencer_balance_keys(&executor.block_context);
for (expected_balance, storage_key) in [
(expected_account_balance_low, account_balance_key_low),
// In tests, we assume that the account balance can be represented as u128.
(StarkFelt::ZERO, account_balance_key_high),
(expected_sequencer_balance_low, sequencer_balance_key_low),
(StarkFelt::ZERO, sequencer_balance_key_high),
] {
let actual_balance = tx_version_state
.get_storage_at(
executor.block_context.chain_info.fee_token_address(&FeeType::Strk),
storage_key,
)
.unwrap();
assert_eq!(expected_balance, actual_balance);
}
}

#[test]
fn test_worker_execute() {
// Settings.
Expand Down
13 changes: 9 additions & 4 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashSet;

use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::core::ContractAddress;
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
use starknet_api::transaction::Fee;
Expand Down Expand Up @@ -147,8 +148,12 @@ pub fn verify_can_pay_committed_bounds(

pub fn get_sequencer_balance_keys(block_context: &BlockContext) -> (StorageKey, StorageKey) {
let sequencer_address = block_context.block_info.sequencer_address;
let sequencer_balance_key_low = get_fee_token_var_address(sequencer_address);
let sequencer_balance_key_high = next_storage_key(&sequencer_balance_key_low)
.expect("Cannot get sequencer balance high key.");
(sequencer_balance_key_low, sequencer_balance_key_high)
get_address_balance_keys(sequencer_address)
}

pub fn get_address_balance_keys(address: ContractAddress) -> (StorageKey, StorageKey) {
let balance_key_low = get_fee_token_var_address(address);
let balance_key_high =
next_storage_key(&balance_key_low).expect("Cannot get balance high key.");
(balance_key_low, balance_key_high)
}

0 comments on commit 7e88771

Please sign in to comment.