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 9, 2024
1 parent 952373d commit 7ede3d2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
46 changes: 43 additions & 3 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::sync::Mutex;

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 @@ -23,9 +23,10 @@ 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 @@ -34,6 +35,45 @@ 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,
test_contract_address: ContractAddress,
nonce: Nonce,
) -> AccountTransaction {
account_invoke_tx(invoke_tx_args! {
sender_address: account_address,
calldata: create_trivial_calldata(test_contract_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 _verify_sequencer_balance_update<S: StateReader>(
executor: &WorkerExecutor<'_, S>,
tx_index: usize,
// We assume the balance is at most 2^128, so the "low" value is sufficient.
expected_sequencer_balance_low: StarkFelt,
) {
let tx_version_state = executor.state.pin_version(tx_index);
let (sequencer_balance_key_low, sequencer_balance_key_high) =
get_sequencer_balance_keys(executor.block_context);
for (expected_balance, storage_key) in [
(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
14 changes: 10 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,13 @@ 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).unwrap_or_else(|_| {
panic!("Failed to get balance_key_high for address: {:?}", address.0);
});
(balance_key_low, balance_key_high)
}

0 comments on commit 7ede3d2

Please sign in to comment.