Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(starknet_integration_tests): fund contract address for deployment #2806

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ type SharedNonceManager = Rc<RefCell<NonceManager>>;
/// let undeployed_account = tx_generator.account_with_id(2).account;
/// // Generate a transfer to fund the undeployed account.
/// let transfer_tx = tx_generator.account_with_id_mut(0).generate_transfer(&undeployed_account);
/// // Generate a deploy account transaction for the undeployed account.
/// let deploy_account_tx = tx_generator.account_with_id_mut(2).generate_deploy_account();
/// ```
// Note: when moving this to starknet api crate, see if blockifier's
// [blockifier::transaction::test_utils::FaultyAccountTxCreatorArgs] can be made to use this.
Expand All @@ -207,7 +209,7 @@ impl MultiAccountTransactionGenerator {

/// Registers a new account with the given contract, assuming it is already deployed.
/// Note: the state should reflect that the account is already deployed.
pub fn register_deployed_account(&mut self, account_contract: FeatureContract) {
pub fn register_deployed_account(&mut self, account_contract: FeatureContract) -> AccountId {
let new_account_id = self.account_tx_generators.len();
let salt = ContractAddressSalt(new_account_id.into());
let (account_tx_generator, _default_deploy_account_tx) = AccountTransactionGenerator::new(
Expand All @@ -217,21 +219,24 @@ impl MultiAccountTransactionGenerator {
true,
);
self.account_tx_generators.push(account_tx_generator);
new_account_id
}

/// Registers a new undeployed account with the given contract.
pub fn register_undeployed_account(
&mut self,
account_contract: FeatureContract,
contract_address_salt: ContractAddressSalt,
) {
) -> AccountId {
let new_account_id = self.account_tx_generators.len();
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);
new_account_id
}

pub fn account_with_id_mut(
Expand Down Expand Up @@ -271,6 +276,7 @@ impl MultiAccountTransactionGenerator {
pub struct AccountTransactionGenerator {
pub account: Contract,
nonce_manager: SharedNonceManager,
contract_address_salt: ContractAddressSalt,
}

impl AccountTransactionGenerator {
Expand Down Expand Up @@ -351,6 +357,22 @@ impl AccountTransactionGenerator {
rpc_invoke_tx(invoke_args)
}

pub fn generate_deploy_account(&mut self) -> RpcTransaction {
assert!(
!self.is_deployed(),
"Cannot deploy an already deployed account: the first transaction of every account \
must be a deploy account transaction."
);
let nonce = self.next_nonce();
assert_eq!(nonce, nonce!(0), "The deploy account tx should have nonce 0.");
let deploy_account_args = deploy_account_tx_args!(
class_hash: self.account.class_hash(),
resource_bounds: test_valid_resource_bounds(),
contract_address_salt: ContractAddressSalt(self.contract_address_salt.0)
);
rpc_deploy_account_tx(deploy_account_args)
}

pub fn sender_address(&self) -> ContractAddress {
self.account.sender_address
}
Expand Down Expand Up @@ -379,6 +401,7 @@ impl AccountTransactionGenerator {
let mut account_tx_generator = Self {
account: Contract::new_for_account(account, &default_deploy_account_tx),
nonce_manager,
contract_address_salt,
};
if is_deployed {
// Bump the account nonce after transaction creation.
Expand Down
6 changes: 3 additions & 3 deletions crates/starknet_integration_tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ use starknet_state_sync::config::StateSyncConfig;
use starknet_types_core::felt::Felt;
use url::Url;

pub const ACCOUNT_ID_0: AccountId = 0;
pub const ACCOUNT_ID_1: AccountId = 1;

pub fn create_chain_info() -> ChainInfo {
let mut chain_info = ChainInfo::create_for_testing();
// Note that the chain_id affects hashes of transactions and blocks, therefore affecting the
Expand Down Expand Up @@ -174,9 +177,6 @@ pub fn create_integration_test_tx_generator() -> MultiAccountTransactionGenerato
pub fn create_txs_for_integration_test(
tx_generator: &mut MultiAccountTransactionGenerator,
) -> Vec<RpcTransaction> {
const ACCOUNT_ID_0: AccountId = 0;
const ACCOUNT_ID_1: AccountId = 1;

// Create RPC transactions.
let account0_invoke_nonce1 =
tx_generator.account_with_id_mut(ACCOUNT_ID_0).generate_invoke_with_tip(2);
Expand Down
32 changes: 29 additions & 3 deletions crates/starknet_integration_tests/tests/end_to_end_flow_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashSet;

use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::{CairoVersion, RunnableCairo1};
use futures::StreamExt;
use mempool_test_utils::starknet_api_test_utils::MultiAccountTransactionGenerator;
use papyrus_consensus::types::ValidatorId;
Expand All @@ -15,6 +17,8 @@ use papyrus_storage::test_utils::CHAIN_ID_FOR_TESTS;
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::fields::ContractAddressSalt;
use starknet_api::transaction::TransactionHash;
use starknet_integration_tests::flow_test_setup::{FlowSequencerSetup, FlowTestSetup};
use starknet_integration_tests::test_identifiers::TestIdentifier;
Expand All @@ -23,13 +27,15 @@ use starknet_integration_tests::utils::{
create_txs_for_integration_test,
run_integration_test_scenario,
test_tx_hashes_for_integration_test,
ACCOUNT_ID_0,
};
use starknet_sequencer_infra::trace_util::configure_tracing;
use starknet_types_core::felt::Felt;
use tracing::debug;

const INITIAL_HEIGHT: BlockNumber = BlockNumber(0);
const LAST_HEIGHT: BlockNumber = BlockNumber(2);
const LAST_HEIGHT: BlockNumber = BlockNumber(3);
const NEW_ACCOUNT_SALT: ContractAddressSalt = ContractAddressSalt(Felt::THREE);

#[fixture]
fn tx_generator() -> MultiAccountTransactionGenerator {
Expand Down Expand Up @@ -65,6 +71,9 @@ async fn end_to_end_flow(mut tx_generator: MultiAccountTransactionGenerator) {
Felt::from_hex_unchecked(
"0x79b59c5036c9427b5194796ede67bdfffed1f311a77382d715174fcfcc33003",
),
Felt::from_hex_unchecked(
"0x36e1f3e0c71b77474494a5baa0a04a4e406626141eba2b2944e4b568f70ff48",
),
];

let sequencers = [&mock_running_system.sequencer_0, &mock_running_system.sequencer_1];
Expand All @@ -75,10 +84,10 @@ async fn end_to_end_flow(mut tx_generator: MultiAccountTransactionGenerator) {
expected_proposer_iter.next().unwrap();

let create_rpc_txs_scenarios =
[create_txs_for_integration_test, create_txs_for_integration_test];
[create_txs_for_integration_test, create_txs_for_integration_test, fund_new_account];

let test_tx_hashes_scenarios =
[test_tx_hashes_for_integration_test, test_tx_hashes_for_integration_test];
[test_tx_hashes_for_integration_test, test_tx_hashes_for_integration_test, test_funding];

assert_eq!(
n_heights,
Expand Down Expand Up @@ -227,3 +236,20 @@ async fn listen_to_broadcasted_messages(
"Unexpected transactions"
);
}

fn fund_new_account(tx_generator: &mut MultiAccountTransactionGenerator) -> Vec<RpcTransaction> {
let new_account_id = tx_generator.register_undeployed_account(
FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1(RunnableCairo1::Casm)),
NEW_ACCOUNT_SALT,
);

let to = tx_generator.account_with_id(new_account_id).account;

let funding_tx = tx_generator.account_with_id_mut(ACCOUNT_ID_0).generate_transfer(&to);
vec![funding_tx]
}

fn test_funding(tx_hashes: &[TransactionHash]) -> Vec<TransactionHash> {
assert_eq!(tx_hashes.len(), 1, "Expected a single transaction");
tx_hashes.to_vec()
}