Skip to content

Commit

Permalink
chore(tests_integration): add nonce verification
Browse files Browse the repository at this point in the history
commit-id:ab21c59b
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 14, 2024
1 parent d42f58a commit 4aab3a1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/starknet_integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ indexmap.workspace = true
mempool_test_utils.workspace = true
papyrus_common.workspace = true
papyrus_consensus.workspace = true
papyrus_execution.workspace = true
papyrus_network = { workspace = true, features = ["testing"] }
papyrus_protobuf.workspace = true
papyrus_rpc.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;

use blockifier::concurrency::test_utils::contract_address;
use mempool_test_utils::starknet_api_test_utils::MultiAccountTransactionGenerator;
use papyrus_execution::execution_utils::get_nonce_at;
use papyrus_storage::state::StateStorageReader;
use papyrus_storage::StorageReader;
use rstest::{fixture, rstest};
use starknet_api::block::{Block, BlockNumber};
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::state::StateNumber;
use starknet_integration_tests::integration_test_setup::IntegrationTestSetup;
use starknet_integration_tests::utils::{
create_integration_test_tx_generator,
run_transaction_generator_test_scenario,
};
use starknet_sequencer_infra::trace_util::configure_tracing;
use starknet_types_core::felt::Felt;
use tokio::process::{Child, Command};
use tokio::task::{self, JoinHandle};
use tracing::info;
use tokio::time::interval;
use tracing::{error, info};

#[fixture]
fn tx_generator() -> MultiAccountTransactionGenerator {
Expand All @@ -37,7 +46,7 @@ async fn spawn_node_child_task(node_config_path: PathBuf) -> Child {
.arg("--config_file")
.arg(node_config_path.to_str().unwrap())
.stderr(Stdio::inherit())
.stdout(Stdio::null())
.stdout(Stdio::inherit())
.kill_on_drop(true) // Required for stopping the node when the handle is dropped.
.spawn()
.expect("Failed to spawn the sequencer node.")
Expand All @@ -54,9 +63,63 @@ async fn spawn_run_node(node_config_path: PathBuf) -> JoinHandle<()> {
})
}

/// Reads the latest block number from the storage.
fn get_latest_block_number(storage_reader: &StorageReader) -> BlockNumber {
let txn = storage_reader.begin_ro_txn().unwrap();
txn.get_state_marker().unwrap().prev().unwrap()
}

/// Reads an account nonce after a block number from storage.
fn get_account_nonce(
storage_reader: &StorageReader,
block_number: BlockNumber,
contract_address: ContractAddress,
) -> Nonce {
let txn = storage_reader.begin_ro_txn().unwrap();
let state_number = StateNumber::unchecked_right_after_block(block_number);
get_nonce_at(&txn, state_number, None.as_ref(), contract_address).unwrap().unwrap()
}

/// Sample a storage until sufficiently many blocks have been stored. Returns an error if the after
/// a certain number of attempts the target block number has not been reached.
async fn await_until_storage_contains_block(
interval_duration: Duration,
target_block_number: BlockNumber,
max_attempts: usize,
storage_reader: &StorageReader,
) -> Result<(), ()> {
let mut interval = interval(interval_duration);
let mut count = 0;
loop {
// Read the latest block number.
let latest_block_number = get_latest_block_number(storage_reader);
count += 1;

// Check if reached the target block number.
if latest_block_number >= target_block_number {
info!("Found block {} after {} queries.", target_block_number, count);
return Ok(());
}

// Check if reached the maximum attempts.
if count > max_attempts {
error!(
"Latest block is {}, expected {}, stopping sampling.",
latest_block_number, target_block_number
);
return Err(());
}

// Wait for the next interval.
interval.tick().await;
}
}

#[rstest]
#[tokio::test]
async fn test_end_to_end_integration(tx_generator: MultiAccountTransactionGenerator) {
const EXPECTED_BLOCK_NUMBER: BlockNumber = BlockNumber(15);

configure_tracing();
info!("Running integration test setup.");

Expand All @@ -81,13 +144,39 @@ async fn test_end_to_end_integration(tx_generator: MultiAccountTransactionGenera

let n_txs = 50;
info!("Sending {n_txs} txs.");
run_transaction_generator_test_scenario(tx_generator, n_txs, send_rpc_tx_fn).await;
let (tx_hashes, sender_address) =
run_transaction_generator_test_scenario(tx_generator, n_txs, send_rpc_tx_fn).await;

info!("Awaiting until {EXPECTED_BLOCK_NUMBER} blocks have been created.");

info!("Shutting down.");
match await_until_storage_contains_block(
Duration::from_secs(5),
EXPECTED_BLOCK_NUMBER,
15,
&integration_test_setup.batcher_storage_reader,
)
.await
{
Ok(_) => {}
Err(_) => panic!("Did not reach expected block number."),
}

info!("Shutting the node down.");
node_run_handle.abort();
let res = node_run_handle.await;
assert!(
res.expect_err("Node should have been stopped.").is_cancelled(),
"Node should have been stopped."
);

info!("Verify tx sender account nonce.");
let expected_nonce_value = tx_hashes.len() + 1;
let expected_nonce =
Nonce(Felt::from_hex_unchecked(format!("0x{:X}", expected_nonce_value).as_str()));
let nonce = get_account_nonce(
&integration_test_setup.batcher_storage_reader,
EXPECTED_BLOCK_NUMBER,
sender_address,
);
assert_eq!(nonce, expected_nonce);
}

0 comments on commit 4aab3a1

Please sign in to comment.