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 17, 2024
1 parent 18600c3 commit 9a5ac14
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 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 @@ -4,16 +4,24 @@ use std::process::Stdio;
use std::time::Duration;

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::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 @@ -25,6 +33,7 @@ async fn spawn_node_child_task(node_config_path: PathBuf) -> Child {
// Get the current working directory for the project
let project_path = env::current_dir().expect("Failed to get current directory").join("../..");

// TODO(Tsabary): Capture output to a log file, and present it in case of a failure.
// TODO(Tsabary): Change invocation from "cargo run" to separate compilation and invocation
// (build, and then invoke the binary).
Command::new("cargo")
Expand Down Expand Up @@ -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, contract_address).unwrap().unwrap()
}

/// Sample a storage until sufficiently many blocks have been stored. Returns an error if after
/// the given number of attempts the target block number has not been reached.
async fn await_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,34 @@ 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.");
let (batcher_storage_reader, _) =
papyrus_storage::open_storage(integration_test_setup.batcher_storage_config)
.expect("Failed to open batcher's storage");

match await_block(Duration::from_secs(5), EXPECTED_BLOCK_NUMBER, 15, &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!("Verifying 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(&batcher_storage_reader, EXPECTED_BLOCK_NUMBER, sender_address);
assert_eq!(nonce, expected_nonce);
}

0 comments on commit 9a5ac14

Please sign in to comment.