Skip to content

Commit

Permalink
feat(mempool_test_utils): register accounts as deployed or undeployed
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Dec 19, 2024
1 parent c1f3c9c commit cbd18d8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
59 changes: 43 additions & 16 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -141,6 +141,11 @@ pub type AccountId = usize;

type SharedNonceManager = Rc<RefCell<NonceManager>>;

// 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.
///
Expand All @@ -159,8 +164,8 @@ type SharedNonceManager = Rc<RefCell<NonceManager>>;
/// 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);
Expand All @@ -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(
Expand All @@ -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<Contract> {
self.account_tx_generators.iter().map(|tx_gen| &tx_gen.account).copied().collect()
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 =
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_integration_tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit cbd18d8

Please sign in to comment.