From cbd18d8e57a03a9925598111f7d1a4a12be5e196 Mon Sep 17 00:00:00 2001 From: Yair Bakalchuk Date: Mon, 16 Dec 2024 15:54:36 +0200 Subject: [PATCH] feat(mempool_test_utils): register accounts as deployed or undeployed --- .../src/starknet_api_test_utils.rs | 59 ++++++++++++++----- .../starknet_integration_tests/src/utils.rs | 2 +- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/crates/mempool_test_utils/src/starknet_api_test_utils.rs b/crates/mempool_test_utils/src/starknet_api_test_utils.rs index 716a208b73..8517f71550 100644 --- a/crates/mempool_test_utils/src/starknet_api_test_utils.rs +++ b/crates/mempool_test_utils/src/starknet_api_test_utils.rs @@ -118,7 +118,7 @@ pub fn executable_invoke_tx(cairo_version: CairoVersion) -> AccountTransaction { let default_account = FeatureContract::AccountWithoutValidations(cairo_version); let mut tx_generator = MultiAccountTransactionGenerator::new(); - tx_generator.register_account(default_account); + tx_generator.register_deployed_account(default_account); tx_generator.account_with_id_mut(0).generate_executable_invoke() } @@ -141,6 +141,11 @@ pub type AccountId = usize; type SharedNonceManager = Rc>; +// TODO: Separate MultiAccountTransactionGenerator to phases: +// 1. Setup phase - register erc20 contract and initialy deployed account with some balance (produce +// the state diff that represents the initial state so it can be used in the test). +// 2. Execution phase - generate transactions. + /// Manages transaction generation for multiple pre-funded accounts, internally bumping nonces /// as needed. /// @@ -159,8 +164,8 @@ type SharedNonceManager = Rc>; /// let some_account_type = /// FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1(RunnableCairo1::Casm)); /// // Initialize multiple accounts, these can be any account type in `FeatureContract`. -/// tx_generator.register_account_for_flow_test(some_account_type.clone()); -/// tx_generator.register_account_for_flow_test(some_account_type); +/// tx_generator.register_deployed_account(some_account_type.clone()); +/// tx_generator.register_deployed_account(some_account_type); /// /// let account_0_tx_with_nonce_0 = tx_generator.account_with_id_mut(0).generate_invoke_with_tip(1); /// let account_1_tx_with_nonce_0 = tx_generator.account_with_id_mut(1).generate_invoke_with_tip(3); @@ -182,16 +187,33 @@ impl MultiAccountTransactionGenerator { Self::default() } - pub fn register_account(&mut self, account_contract: FeatureContract) -> RpcTransaction { + /// Registers a new account with the given contract, assuming it is already deployed. + /// Note: the state should reflect it if the account is already deployed. + pub fn register_deployed_account(&mut self, account_contract: FeatureContract) { let new_account_id = self.account_tx_generators.len(); - let (account_tx_generator, default_deploy_account_tx) = AccountTransactionGenerator::new( - new_account_id, + let salt = ContractAddressSalt(new_account_id.into()); + let (account_tx_generator, _default_deploy_account_tx) = AccountTransactionGenerator::new( account_contract, self.nonce_manager.clone(), + salt, + true, ); self.account_tx_generators.push(account_tx_generator); + } - default_deploy_account_tx + /// Registers a new undeployed account with the given contract. + pub fn register_undeployed_account( + &mut self, + account_contract: FeatureContract, + contract_address_salt: ContractAddressSalt, + ) { + let (account_tx_generator, _default_deploy_account_tx) = AccountTransactionGenerator::new( + account_contract, + self.nonce_manager.clone(), + contract_address_salt, + false, + ); + self.account_tx_generators.push(account_tx_generator); } pub fn account_with_id_mut( @@ -215,13 +237,6 @@ impl MultiAccountTransactionGenerator { }) } - // TODO(deploy_account_support): once we support deploy account in tests, remove this method and - // only use new_account_default in tests. In particular, deploy account txs must be then sent to - // the GW via the add tx endpoint just like other txs. - pub fn register_account_for_flow_test(&mut self, account_contract: FeatureContract) { - self.register_account(account_contract); - } - pub fn accounts(&self) -> Vec { self.account_tx_generators.iter().map(|tx_gen| &tx_gen.account).copied().collect() } @@ -238,11 +253,17 @@ impl MultiAccountTransactionGenerator { pub struct AccountTransactionGenerator { account: Contract, nonce_manager: SharedNonceManager, + is_deployed: bool, } impl AccountTransactionGenerator { /// Generate a valid `RpcTransaction` with default parameters. pub fn generate_invoke_with_tip(&mut self, tip: u64) -> RpcTransaction { + assert!( + self.is_deployed, + "Cannot invoke on behalf of an undeployed account: the first transaction of every \ + account must be a deploy account transaction." + ); let nonce = self.next_nonce(); assert_ne!( nonce, @@ -261,6 +282,11 @@ impl AccountTransactionGenerator { } pub fn generate_executable_invoke(&mut self) -> AccountTransaction { + assert!( + self.is_deployed, + "Cannot invoke on behalf of an undeployed account: the first transaction of every \ + account must be a deploy account transaction." + ); let nonce = self.next_nonce(); assert_ne!( nonce, @@ -307,11 +333,11 @@ impl AccountTransactionGenerator { // TODO: add a version that doesn't rely on the default deploy account constructor, but takes // deploy account args. fn new( - account_id: usize, account: FeatureContract, nonce_manager: SharedNonceManager, + contract_address_salt: ContractAddressSalt, + is_deployed: bool, ) -> (Self, RpcTransaction) { - let contract_address_salt = ContractAddressSalt(account_id.into()); // A deploy account transaction must be created now in order to affix an address to it. // If this doesn't happen now it'll be difficult to fund the account during test setup. let default_deploy_account_tx = @@ -320,6 +346,7 @@ impl AccountTransactionGenerator { let mut account_tx_generator = Self { account: Contract::new_for_account(account, &default_deploy_account_tx), nonce_manager, + is_deployed, }; // Bump the account nonce after transaction creation. account_tx_generator.next_nonce(); diff --git a/crates/starknet_integration_tests/src/utils.rs b/crates/starknet_integration_tests/src/utils.rs index 5b2dae24b3..f8c340df04 100644 --- a/crates/starknet_integration_tests/src/utils.rs +++ b/crates/starknet_integration_tests/src/utils.rs @@ -148,7 +148,7 @@ pub fn create_integration_test_tx_generator() -> MultiAccountTransactionGenerato FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1(RunnableCairo1::Casm)), FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0), ] { - tx_generator.register_account_for_flow_test(account); + tx_generator.register_deployed_account(account); } tx_generator }