This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: share benchmark code with simple flow test
- Loading branch information
1 parent
e2fd958
commit 7d30fc1
Showing
6 changed files
with
134 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use crate::test_utils::transfers_simulator::TransfersSimulator; | ||
|
||
#[test] | ||
pub fn transfers_flow_test() { | ||
let n_chunks = 100; | ||
let mut transfers_simulator = TransfersSimulator::new(); | ||
for _ in 0..n_chunks { | ||
transfers_simulator.execute_chunk_of_transfers(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
crates/blockifier/src/test_utils/transfers_simulator.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use rand::{Rng, SeedableRng}; | ||
use starknet_api::core::ContractAddress; | ||
use starknet_api::hash::StarkFelt; | ||
use starknet_api::transaction::{Calldata, Fee, TransactionVersion}; | ||
use starknet_api::{calldata, stark_felt}; | ||
|
||
use crate::abi::abi_utils::selector_from_name; | ||
use crate::blockifier::config::TransactionExecutorConfig; | ||
use crate::blockifier::transaction_executor::TransactionExecutor; | ||
use crate::bouncer::BouncerConfig; | ||
use crate::context::{BlockContext, ChainInfo}; | ||
use crate::invoke_tx_args; | ||
use crate::test_utils::contracts::FeatureContract; | ||
use crate::test_utils::dict_state_reader::DictStateReader; | ||
use crate::test_utils::initial_test_state::test_state; | ||
use crate::test_utils::invoke::invoke_tx; | ||
use crate::test_utils::{CairoVersion, NonceManager, BALANCE, MAX_FEE}; | ||
use crate::transaction::account_transaction::AccountTransaction; | ||
use crate::transaction::constants::TRANSFER_ENTRY_POINT_NAME; | ||
use crate::transaction::transaction_execution::Transaction; | ||
|
||
const N_ACCOUNTS: u16 = 10000; | ||
const CHUNK_SIZE: usize = 10; | ||
const RANDOMIZATION_SEED: u64 = 0; | ||
const CHARGE_FEE: bool = false; | ||
const TRANSACTION_VERSION: TransactionVersion = TransactionVersion(StarkFelt::ONE); | ||
|
||
pub struct TransfersSimulator { | ||
accounts: Vec<ContractAddress>, | ||
nonce_manager: NonceManager, | ||
chain_info: ChainInfo, | ||
executor: TransactionExecutor<DictStateReader>, | ||
sender_index: usize, | ||
recipient_generator: rand::rngs::StdRng, | ||
} | ||
|
||
impl Default for TransfersSimulator { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl TransfersSimulator { | ||
pub fn new() -> Self { | ||
let account_contract = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0); | ||
let block_context = BlockContext::create_for_account_testing(); | ||
let chain_info = &block_context.chain_info().clone(); | ||
let state = test_state(chain_info, BALANCE * 1000, &[(account_contract, N_ACCOUNTS)]); | ||
// TODO(Avi, 20/05/2024): Enable concurrency. | ||
let executor_config = TransactionExecutorConfig::default(); | ||
let executor = | ||
TransactionExecutor::new(state, block_context, BouncerConfig::max(), executor_config); | ||
let accounts = (0..N_ACCOUNTS) | ||
.map(|instance_id| account_contract.get_instance_address(instance_id)) | ||
.collect::<Vec<_>>(); | ||
let nonce_manager = NonceManager::default(); | ||
let random_generator = rand::rngs::StdRng::seed_from_u64(RANDOMIZATION_SEED); | ||
Self { | ||
accounts, | ||
nonce_manager, | ||
chain_info: chain_info.clone(), | ||
executor, | ||
sender_index: 0, | ||
recipient_generator: random_generator, | ||
} | ||
} | ||
|
||
pub fn execute_chunk_of_transfers(&mut self) { | ||
let mut chunk: Vec<Transaction> = Vec::with_capacity(CHUNK_SIZE); | ||
for _ in 0..CHUNK_SIZE { | ||
let account_tx = self.generate_transfer(); | ||
chunk.push(Transaction::AccountTransaction(account_tx)); | ||
} | ||
let results = self.executor.execute_txs(&chunk, CHARGE_FEE); | ||
assert_eq!(results.len(), CHUNK_SIZE); | ||
for result in results { | ||
assert!(!result.unwrap().is_reverted()); | ||
} | ||
} | ||
|
||
pub fn generate_transfer(&mut self) -> AccountTransaction { | ||
let sender_address = self.accounts[self.sender_index]; | ||
self.sender_index = (self.sender_index + 1) % self.accounts.len(); | ||
let recipient_index = self.recipient_generator.gen::<usize>() % self.accounts.len(); | ||
let recipient_address = self.accounts[recipient_index]; | ||
let nonce = self.nonce_manager.next(sender_address); | ||
|
||
let entry_point_selector = selector_from_name(TRANSFER_ENTRY_POINT_NAME); | ||
let contract_address = match TRANSACTION_VERSION { | ||
TransactionVersion::ONE => { | ||
*self.chain_info.fee_token_addresses.eth_fee_token_address.0.key() | ||
} | ||
TransactionVersion::THREE => { | ||
*self.chain_info.fee_token_addresses.strk_fee_token_address.0.key() | ||
} | ||
_ => panic!("Unsupported transaction version: {TRANSACTION_VERSION:?}"), | ||
}; | ||
|
||
let execute_calldata = calldata![ | ||
contract_address, // Contract address. | ||
entry_point_selector.0, // EP selector. | ||
stark_felt!(3_u8), // Calldata length. | ||
*recipient_address.0.key(), // Calldata: recipient. | ||
stark_felt!(1_u8), // Calldata: lsb amount. | ||
stark_felt!(0_u8) // Calldata: msb amount. | ||
]; | ||
|
||
let tx = invoke_tx(invoke_tx_args! { | ||
max_fee: Fee(MAX_FEE), | ||
sender_address, | ||
calldata: execute_calldata, | ||
version: TRANSACTION_VERSION, | ||
nonce, | ||
}); | ||
AccountTransaction::Invoke(tx) | ||
} | ||
} |